rust/hedgewars-server/src/server/handlers/common.rs
author alfadur <mail@none>
Wed, 06 Feb 2019 00:14:04 +0300
changeset 14707 9f98086de1b6
parent 14704 932ff7683653
child 14708 5122c584804e
permissions -rw-r--r--
Server action refactoring part 9 of N
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14707
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
     1
use crate::protocol::messages::server_chat;
14695
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
     2
use crate::server::client::HWClient;
14704
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
     3
use crate::server::coretypes::ClientId;
14695
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
     4
use crate::server::room::HWRoom;
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
     5
use crate::utils::to_engine_msg;
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13500
diff changeset
     6
use crate::{
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13500
diff changeset
     7
    protocol::messages::{
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
     8
        HWProtocolMessage::{self, Rnd},
14695
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
     9
        HWServerMessage::{
14704
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    10
            self, Bye, ChatMsg, ClientFlags, ForwardEngineMessage, LobbyJoined, LobbyLeft, Notice,
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    11
            RoomLeft, RoomRemove, RoomUpdated, Rooms, ServerMessage, TeamRemove,
14695
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    12
        },
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
    13
    },
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
    14
    server::{actions::Action, core::HWServer},
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    15
};
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
    16
use rand::{self, thread_rng, Rng};
14695
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    17
use std::iter::once;
14707
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
    18
use std::mem::replace;
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    19
13500
5359ff75da3a indulge clippy
alfadur
parents: 13492
diff changeset
    20
pub fn rnd_reply(options: &[String]) -> HWServerMessage {
13492
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
    21
    let mut rng = thread_rng();
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
    22
    let reply = if options.is_empty() {
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
    23
        (*rng.choose(&["heads", "tails"]).unwrap()).to_owned()
13445
d3c86ade3d4d Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13444
diff changeset
    24
    } else {
13492
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
    25
        rng.choose(&options).unwrap().clone()
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
    26
    };
13445
d3c86ade3d4d Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13444
diff changeset
    27
13492
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
    28
    ChatMsg {
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    29
        nick: "[random]".to_owned(),
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    30
        msg: reply.clone(),
13492
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
    31
    }
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    32
}
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    33
14704
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    34
pub fn process_login(server: &mut HWServer, response: &mut super::Response) {
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    35
    let client_id = response.client_id();
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    36
    let nick = server.clients[client_id].nick.clone();
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    37
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    38
    let has_nick_clash = server
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    39
        .clients
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    40
        .iter()
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    41
        .any(|(id, c)| id != client_id && c.nick == nick);
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    42
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    43
    let client = &mut server.clients[client_id];
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    44
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    45
    if !client.is_checker() && has_nick_clash {
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    46
        if client.protocol_number < 38 {
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    47
            remove_client(server, response, "Nickname is already in use".to_string());
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    48
        } else {
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    49
            client.nick.clear();
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    50
            response.add(Notice("NickAlreadyInUse".to_string()).send_self());
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    51
        }
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    52
    } else {
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    53
        server.clients[client_id].room_id = Some(server.lobby_id);
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    54
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    55
        let lobby_nicks: Vec<_> = server
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    56
            .clients
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    57
            .iter()
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    58
            .filter_map(|(_, c)| c.room_id.and(Some(c.nick.clone())))
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    59
            .collect();
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    60
        let joined_msg = LobbyJoined(lobby_nicks);
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    61
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    62
        let everyone_msg = LobbyJoined(vec![server.clients[client_id].nick.clone()]);
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    63
        let flags_msg = ClientFlags(
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    64
            "+i".to_string(),
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    65
            server
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    66
                .clients
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    67
                .iter()
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    68
                .filter(|(_, c)| c.room_id.is_some())
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    69
                .map(|(_, c)| c.nick.clone())
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    70
                .collect(),
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    71
        );
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    72
        let server_msg = ServerMessage("\u{1f994} is watching".to_string());
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    73
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    74
        let rooms_msg = Rooms(
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    75
            server
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    76
                .rooms
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    77
                .iter()
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    78
                .filter(|(id, _)| *id != server.lobby_id)
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    79
                .flat_map(|(_, r)| r.info(r.master_id.map(|id| &server.clients[id])))
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    80
                .collect(),
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    81
        );
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    82
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    83
        response.add(everyone_msg.send_all().but_self());
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    84
        response.add(joined_msg.send_self());
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    85
        response.add(flags_msg.send_self());
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    86
        response.add(server_msg.send_self());
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    87
        response.add(rooms_msg.send_self());
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    88
    }
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    89
}
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    90
14696
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
    91
pub fn remove_teams(
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
    92
    room: &mut HWRoom,
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
    93
    team_names: Vec<String>,
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
    94
    is_in_game: bool,
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
    95
    response: &mut super::Response,
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
    96
) {
14695
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    97
    if let Some(ref mut info) = room.game_info {
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    98
        for team_name in &team_names {
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    99
            info.left_teams.push(team_name.clone());
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   100
14696
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   101
            if is_in_game {
14695
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   102
                let msg = once(b'F').chain(team_name.bytes());
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   103
                response.add(
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   104
                    ForwardEngineMessage(vec![to_engine_msg(msg)])
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   105
                        .send_all()
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   106
                        .in_room(room.id)
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   107
                        .but_self(),
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   108
                );
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   109
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   110
                info.teams_in_game -= 1;
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   111
                if info.teams_in_game == 0 {
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   112
                    //FinishRoomGame(room.id)
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   113
                }
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   114
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   115
                let remove_msg = to_engine_msg(once(b'F').chain(team_name.bytes()));
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   116
                if let Some(m) = &info.sync_msg {
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   117
                    info.msg_log.push(m.clone());
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   118
                    info.sync_msg = None
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   119
                }
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   120
                info.msg_log.push(remove_msg.clone());
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   121
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   122
                response.add(
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   123
                    ForwardEngineMessage(vec![remove_msg])
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   124
                        .send_all()
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   125
                        .in_room(room.id)
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   126
                        .but_self(),
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   127
                );
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   128
            }
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   129
        }
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   130
    }
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   131
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   132
    for team_name in team_names {
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   133
        room.remove_team(&team_name);
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   134
        response.add(TeamRemove(team_name).send_all().in_room(room.id));
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   135
    }
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   136
}
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   137
14696
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   138
pub fn exit_room(client: &HWClient, room: &mut HWRoom, response: &mut super::Response, msg: &str) {
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   139
    if room.players_number > 1 || room.is_fixed() {
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   140
        room.players_number -= 1;
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   141
        if client.is_ready() && room.ready_players_number > 0 {
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   142
            room.ready_players_number -= 1;
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   143
        }
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   144
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   145
        let team_names: Vec<_> = room
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   146
            .client_teams(client.id)
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   147
            .map(|t| t.name.clone())
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   148
            .collect();
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   149
        remove_teams(room, team_names, client.is_in_game(), response);
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   150
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   151
        if room.players_number > 0 {
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   152
            response.add(
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   153
                RoomLeft(client.nick.clone(), msg.to_string())
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   154
                    .send_all()
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   155
                    .in_room(room.id)
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   156
                    .but_self(),
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   157
            );
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   158
        }
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   159
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   160
        //ChangeMaster(room.id, None));
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   161
    }
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   162
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   163
    let update_msg = if room.players_number == 0 && !room.is_fixed() {
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   164
        RoomRemove(room.name.clone())
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   165
    } else {
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   166
        RoomUpdated(room.name.clone(), room.info(Some(&client)))
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   167
    };
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   168
    response.add(update_msg.send_all().with_protocol(room.protocol_number));
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   169
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   170
    response.add(ClientFlags("-i".to_string(), vec![client.nick.clone()]).send_all());
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   171
}
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   172
14694
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14478
diff changeset
   173
pub fn remove_client(server: &mut HWServer, response: &mut super::Response, msg: String) {
14695
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   174
    let client_id = response.client_id();
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   175
    let lobby_id = server.lobby_id;
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   176
    let client = &mut server.clients[client_id];
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   177
    let (nick, room_id) = (client.nick.clone(), client.room_id);
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   178
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   179
    if let Some(room_id) = room_id {
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   180
        let room = &mut server.rooms[room_id];
14696
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   181
        exit_room(client, room, response, &msg);
14695
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   182
        client.room_id = Some(lobby_id);
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   183
    }
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   184
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   185
    server.remove_client(client_id);
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   186
14694
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14478
diff changeset
   187
    response.add(LobbyLeft(nick, msg.to_string()).send_all());
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14478
diff changeset
   188
    response.add(Bye("User quit: ".to_string() + &msg).send_self());
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14478
diff changeset
   189
}
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14478
diff changeset
   190
14707
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   191
pub fn get_room_update(
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   192
    room_name: Option<String>,
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   193
    room: &HWRoom,
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   194
    client: Option<&HWClient>,
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   195
    response: &mut super::Response,
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   196
) {
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   197
    let update_msg = RoomUpdated(room_name.unwrap_or(room.name.clone()), room.info(client));
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   198
    response.add(update_msg.send_all().with_protocol(room.protocol_number));
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   199
}
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   200
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   201
pub fn add_vote(room: &mut HWRoom, response: &mut super::Response, vote: bool, is_forced: bool) {
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   202
    let client_id = response.client_id;
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   203
    let mut result = None;
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   204
    if let Some(ref mut voting) = room.voting {
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   205
        if is_forced || voting.votes.iter().all(|(id, _)| client_id != *id) {
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   206
            response.add(server_chat("Your vote has been counted.".to_string()).send_self());
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   207
            voting.votes.push((client_id, vote));
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   208
            let i = voting.votes.iter();
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   209
            let pro = i.clone().filter(|(_, v)| *v).count();
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   210
            let contra = i.filter(|(_, v)| !*v).count();
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   211
            let success_quota = voting.voters.len() / 2 + 1;
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   212
            if is_forced && vote || pro >= success_quota {
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   213
                result = Some(true);
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   214
            } else if is_forced && !vote || contra > voting.voters.len() - success_quota {
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   215
                result = Some(false);
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   216
            }
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   217
        } else {
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   218
            response.add(server_chat("You already have voted.".to_string()).send_self());
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   219
        }
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   220
    } else {
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   221
        response.add(server_chat("There's no voting going on.".to_string()).send_self());
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   222
    }
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   223
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   224
    if let Some(res) = result {
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   225
        response.add(
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   226
            server_chat("Voting closed.".to_string())
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   227
                .send_all()
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   228
                .in_room(room.id),
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   229
        );
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   230
        let voting = replace(&mut room.voting, None).unwrap();
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   231
        if res {
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   232
            //ApplyVoting(voting.kind, room.id));
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   233
        }
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   234
    }
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   235
}
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   236
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   237
#[cfg(test)]
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   238
mod tests {
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   239
    use super::*;
13713
59ea2403f62d move everything test related into test cfg
alfadur
parents: 13666
diff changeset
   240
    use crate::protocol::messages::HWServerMessage::ChatMsg;
14704
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
   241
    use crate::server::actions::PendingMessage;
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   242
13445
d3c86ade3d4d Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13444
diff changeset
   243
    fn reply2string(r: HWServerMessage) -> String {
d3c86ade3d4d Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13444
diff changeset
   244
        match r {
d3c86ade3d4d Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13444
diff changeset
   245
            ChatMsg { msg: p, .. } => String::from(p),
d3c86ade3d4d Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13444
diff changeset
   246
            _ => panic!("expected a ChatMsg"),
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   247
        }
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   248
    }
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   249
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   250
    fn run_handle_test(opts: Vec<String>) {
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   251
        let opts2 = opts.clone();
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   252
        for opt in opts {
13492
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
   253
            while reply2string(rnd_reply(&opts2)) != opt {}
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   254
        }
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   255
    }
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   256
13446
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   257
    /// This test terminates almost surely.
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   258
    #[test]
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   259
    fn test_handle_rnd_empty() {
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   260
        run_handle_test(vec![])
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   261
    }
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   262
13446
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   263
    /// This test terminates almost surely.
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   264
    #[test]
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   265
    fn test_handle_rnd_nonempty() {
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   266
        run_handle_test(vec!["A".to_owned(), "B".to_owned(), "C".to_owned()])
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   267
    }
13446
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   268
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   269
    /// This test terminates almost surely (strong law of large numbers)
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   270
    #[test]
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   271
    fn test_distribution() {
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   272
        let eps = 0.000001;
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   273
        let lim = 0.5;
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   274
        let opts = vec![0.to_string(), 1.to_string()];
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   275
        let mut ones = 0;
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   276
        let mut tries = 0;
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   277
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   278
        while tries < 1000 || ((ones as f64 / tries as f64) - lim).abs() >= eps {
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   279
            tries += 1;
13492
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
   280
            if reply2string(rnd_reply(&opts)) == 1.to_string() {
13446
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   281
                ones += 1;
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   282
            }
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   283
        }
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   284
    }
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   285
}