rust/hedgewars-server/src/server/handlers/common.rs
author alfadur <mail@none>
Wed, 06 Feb 2019 20:48:40 +0300
changeset 14709 4569d8d50286
parent 14708 5122c584804e
child 14710 aae29ba56aec
permissions -rw-r--r--
Server action refactoring part B of N
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13500
diff changeset
     1
use crate::{
14709
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
     2
    protocol::messages::server_chat,
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13500
diff changeset
     3
    protocol::messages::{
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
     4
        HWProtocolMessage::{self, Rnd},
14709
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
     5
        HWServerMessage::{self, *},
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
     6
    },
14709
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
     7
    server::{
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
     8
        actions::Action,
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
     9
        client::HWClient,
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
    10
        core::HWServer,
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
    11
        coretypes::{ClientId, GameCfg, RoomId, Vote, VoteType},
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
    12
        room::HWRoom,
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
    13
    },
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
    14
    utils::to_engine_msg,
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    15
};
14709
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
    16
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
    17
use super::Response;
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
    18
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
    19
use rand::{self, thread_rng, Rng};
14709
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
    20
use std::{iter::once, mem::replace};
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    21
13500
5359ff75da3a indulge clippy
alfadur
parents: 13492
diff changeset
    22
pub fn rnd_reply(options: &[String]) -> HWServerMessage {
13492
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
    23
    let mut rng = thread_rng();
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
    24
    let reply = if options.is_empty() {
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
    25
        (*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
    26
    } else {
13492
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
    27
        rng.choose(&options).unwrap().clone()
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
    28
    };
13445
d3c86ade3d4d Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13444
diff changeset
    29
13492
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
    30
    ChatMsg {
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    31
        nick: "[random]".to_owned(),
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    32
        msg: reply.clone(),
13492
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
    33
    }
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    34
}
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    35
14709
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
    36
pub fn process_login(server: &mut HWServer, response: &mut Response) {
14704
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    37
    let client_id = response.client_id();
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    38
    let nick = server.clients[client_id].nick.clone();
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    39
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    40
    let has_nick_clash = server
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    41
        .clients
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    42
        .iter()
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    43
        .any(|(id, c)| id != client_id && c.nick == nick);
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
    let client = &mut server.clients[client_id];
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    46
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    47
    if !client.is_checker() && has_nick_clash {
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    48
        if client.protocol_number < 38 {
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    49
            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
    50
        } else {
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    51
            client.nick.clear();
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    52
            response.add(Notice("NickAlreadyInUse".to_string()).send_self());
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    53
        }
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    54
    } else {
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    55
        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
    56
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    57
        let lobby_nicks: Vec<_> = server
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    58
            .clients
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    59
            .iter()
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    60
            .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
    61
            .collect();
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    62
        let joined_msg = LobbyJoined(lobby_nicks);
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    63
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    64
        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
    65
        let flags_msg = ClientFlags(
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    66
            "+i".to_string(),
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    67
            server
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    68
                .clients
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    69
                .iter()
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    70
                .filter(|(_, c)| c.room_id.is_some())
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    71
                .map(|(_, c)| c.nick.clone())
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    72
                .collect(),
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 server_msg = ServerMessage("\u{1f994} is watching".to_string());
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    75
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    76
        let rooms_msg = Rooms(
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    77
            server
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    78
                .rooms
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    79
                .iter()
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    80
                .filter(|(id, _)| *id != server.lobby_id)
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    81
                .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
    82
                .collect(),
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    83
        );
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    84
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    85
        response.add(everyone_msg.send_all().but_self());
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    86
        response.add(joined_msg.send_self());
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    87
        response.add(flags_msg.send_self());
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    88
        response.add(server_msg.send_self());
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    89
        response.add(rooms_msg.send_self());
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    90
    }
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    91
}
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
    92
14696
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
    93
pub fn remove_teams(
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
    94
    room: &mut HWRoom,
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
    95
    team_names: Vec<String>,
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
    96
    is_in_game: bool,
14709
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
    97
    response: &mut Response,
14696
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
    98
) {
14695
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    99
    if let Some(ref mut info) = room.game_info {
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   100
        for team_name in &team_names {
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   101
            info.left_teams.push(team_name.clone());
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   102
14696
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   103
            if is_in_game {
14695
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   104
                let msg = once(b'F').chain(team_name.bytes());
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   105
                response.add(
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   106
                    ForwardEngineMessage(vec![to_engine_msg(msg)])
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   107
                        .send_all()
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   108
                        .in_room(room.id)
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   109
                        .but_self(),
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   110
                );
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   111
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   112
                info.teams_in_game -= 1;
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   113
                if info.teams_in_game == 0 {
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   114
                    //FinishRoomGame(room.id)
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   115
                }
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   116
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   117
                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
   118
                if let Some(m) = &info.sync_msg {
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   119
                    info.msg_log.push(m.clone());
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   120
                    info.sync_msg = None
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
                info.msg_log.push(remove_msg.clone());
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   123
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   124
                response.add(
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   125
                    ForwardEngineMessage(vec![remove_msg])
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   126
                        .send_all()
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   127
                        .in_room(room.id)
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   128
                        .but_self(),
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
    }
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   133
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   134
    for team_name in team_names {
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   135
        room.remove_team(&team_name);
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   136
        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
   137
    }
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   138
}
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   139
14709
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   140
pub fn exit_room(client: &HWClient, room: &mut HWRoom, response: &mut Response, msg: &str) {
14696
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   141
    if room.players_number > 1 || room.is_fixed() {
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   142
        room.players_number -= 1;
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   143
        if client.is_ready() && room.ready_players_number > 0 {
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   144
            room.ready_players_number -= 1;
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   145
        }
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   146
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   147
        let team_names: Vec<_> = room
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   148
            .client_teams(client.id)
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   149
            .map(|t| t.name.clone())
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   150
            .collect();
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   151
        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
   152
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   153
        if room.players_number > 0 {
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   154
            response.add(
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   155
                RoomLeft(client.nick.clone(), msg.to_string())
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   156
                    .send_all()
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   157
                    .in_room(room.id)
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   158
                    .but_self(),
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
        }
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
        //ChangeMaster(room.id, None));
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   163
    }
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   164
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   165
    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
   166
        RoomRemove(room.name.clone())
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   167
    } else {
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   168
        RoomUpdated(room.name.clone(), room.info(Some(&client)))
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(update_msg.send_all().with_protocol(room.protocol_number));
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
    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
   173
}
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   174
14709
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   175
pub fn remove_client(server: &mut HWServer, response: &mut Response, msg: String) {
14695
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   176
    let client_id = response.client_id();
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   177
    let lobby_id = server.lobby_id;
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   178
    let client = &mut server.clients[client_id];
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   179
    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
   180
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   181
    if let Some(room_id) = room_id {
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   182
        let room = &mut server.rooms[room_id];
14696
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14695
diff changeset
   183
        exit_room(client, room, response, &msg);
14695
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   184
        client.room_id = Some(lobby_id);
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   185
    }
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   186
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   187
    server.remove_client(client_id);
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   188
14694
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14478
diff changeset
   189
    response.add(LobbyLeft(nick, msg.to_string()).send_all());
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14478
diff changeset
   190
    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
   191
}
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14478
diff changeset
   192
14707
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   193
pub fn get_room_update(
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   194
    room_name: Option<String>,
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   195
    room: &HWRoom,
14708
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   196
    master: Option<&HWClient>,
14709
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   197
    response: &mut Response,
14707
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   198
) {
14708
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   199
    let update_msg = RoomUpdated(room_name.unwrap_or(room.name.clone()), room.info(master));
14707
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   200
    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
   201
}
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   202
14708
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   203
pub fn apply_voting_result(
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   204
    server: &mut HWServer,
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   205
    room_id: RoomId,
14709
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   206
    response: &mut Response,
14708
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   207
    kind: VoteType,
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   208
) {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   209
    let client_id = response.client_id;
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   210
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   211
    match kind {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   212
        VoteType::Kick(nick) => {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   213
            if let Some(client) = server.find_client(&nick) {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   214
                if client.room_id == Some(room_id) {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   215
                    let id = client.id;
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   216
                    response.add(Kicked.send_self());
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   217
                    exit_room(
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   218
                        &mut server.clients[client_id],
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   219
                        &mut server.rooms[room_id],
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   220
                        response,
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   221
                        "kicked",
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   222
                    );
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   223
                }
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   224
            }
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   225
        }
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   226
        VoteType::Map(None) => (),
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   227
        VoteType::Map(Some(name)) => {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   228
            if let Some(location) = server.rooms[room_id].load_config(&name) {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   229
                response.add(
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   230
                    server_chat(location.to_string())
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   231
                        .send_all()
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   232
                        .in_room(room_id),
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   233
                );
14709
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   234
                let room = &server.rooms[room_id];
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   235
                let room_master = if let Some(id) = room.master_id {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   236
                    Some(&server.clients[id])
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   237
                } else {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   238
                    None
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   239
                };
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   240
                get_room_update(None, room, room_master, response);
14708
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   241
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   242
                for (_, c) in server.clients.iter() {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   243
                    if c.room_id == Some(room_id) {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   244
                        /*SendRoomData {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   245
                            to: c.id,
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   246
                            teams: false,
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   247
                            config: true,
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   248
                            flags: false,
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   249
                        }*/
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   250
                    }
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   251
                }
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   252
            }
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   253
        }
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   254
        VoteType::Pause => {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   255
            if let Some(ref mut info) = server.rooms[room_id].game_info {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   256
                info.is_paused = !info.is_paused;
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   257
                response.add(
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   258
                    server_chat("Pause toggled.".to_string())
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   259
                        .send_all()
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   260
                        .in_room(room_id),
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   261
                );
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   262
                response.add(
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   263
                    ForwardEngineMessage(vec![to_engine_msg(once(b'I'))])
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   264
                        .send_all()
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   265
                        .in_room(room_id),
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   266
                );
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   267
            }
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   268
        }
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   269
        VoteType::NewSeed => {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   270
            let seed = thread_rng().gen_range(0, 1_000_000_000).to_string();
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   271
            let cfg = GameCfg::Seed(seed);
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   272
            response.add(cfg.to_server_msg().send_all().in_room(room_id));
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   273
            server.rooms[room_id].set_config(cfg);
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   274
        }
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   275
        VoteType::HedgehogsPerTeam(number) => {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   276
            let r = &mut server.rooms[room_id];
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   277
            let nicks = r.set_hedgehogs_number(number);
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   278
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   279
            response.extend(
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   280
                nicks
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   281
                    .into_iter()
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   282
                    .map(|n| HedgehogsNumber(n, number).send_all().in_room(room_id)),
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   283
            );
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   284
        }
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   285
    }
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   286
}
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   287
14709
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   288
fn add_vote(room: &mut HWRoom, response: &mut Response, vote: Vote) -> Option<bool> {
14707
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   289
    let client_id = response.client_id;
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   290
    let mut result = None;
14708
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   291
14707
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   292
    if let Some(ref mut voting) = room.voting {
14708
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   293
        if vote.is_forced || voting.votes.iter().all(|(id, _)| client_id != *id) {
14707
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   294
            response.add(server_chat("Your vote has been counted.".to_string()).send_self());
14708
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   295
            voting.votes.push((client_id, vote.is_pro));
14707
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   296
            let i = voting.votes.iter();
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   297
            let pro = i.clone().filter(|(_, v)| *v).count();
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   298
            let contra = i.filter(|(_, v)| !*v).count();
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   299
            let success_quota = voting.voters.len() / 2 + 1;
14708
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   300
            if vote.is_forced && vote.is_pro || pro >= success_quota {
14707
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   301
                result = Some(true);
14708
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   302
            } else if vote.is_forced && !vote.is_pro || contra > voting.voters.len() - success_quota
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   303
            {
14707
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   304
                result = Some(false);
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   305
            }
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   306
        } else {
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   307
            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
   308
        }
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   309
    } else {
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   310
        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
   311
    }
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   312
14708
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   313
    result
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   314
}
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   315
14709
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   316
pub fn submit_vote(server: &mut HWServer, vote: Vote, response: &mut Response) {
14708
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   317
    let client_id = response.client_id;
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   318
    let client = &server.clients[client_id];
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   319
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   320
    if let Some(room_id) = client.room_id {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   321
        let room = &mut server.rooms[room_id];
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   322
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   323
        if let Some(res) = add_vote(room, response, vote) {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   324
            response.add(
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   325
                server_chat("Voting closed.".to_string())
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   326
                    .send_all()
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   327
                    .in_room(room.id),
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   328
            );
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   329
            let voting = replace(&mut room.voting, None).unwrap();
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   330
            if res {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   331
                apply_voting_result(server, room_id, response, voting.kind);
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14707
diff changeset
   332
            }
14707
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   333
        }
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   334
    }
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   335
}
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
   336
14709
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   337
pub fn start_game(server: &mut HWServer, room_id: RoomId, response: &mut Response) {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   338
    let (room_clients, room_nicks): (Vec<_>, Vec<_>) = server
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   339
        .clients
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   340
        .iter()
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   341
        .map(|(id, c)| (id, c.nick.clone()))
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   342
        .unzip();
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   343
    let room = &mut server.rooms[room_id];
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   344
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   345
    if !room.has_multiple_clans() {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   346
        response.add(
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   347
            Warning("The game can't be started with less than two clans!".to_string()).send_self(),
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   348
        );
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   349
    } else if room.protocol_number <= 43 && room.players_number != room.ready_players_number {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   350
        response.add(Warning("Not all players are ready".to_string()).send_self());
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   351
    } else if room.game_info.is_some() {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   352
        response.add(Warning("The game is already in progress".to_string()).send_self());
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   353
    } else {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   354
        room.start_round();
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   355
        for id in room_clients {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   356
            let c = &mut server.clients[id];
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   357
            c.set_is_in_game(false);
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   358
            c.team_indices = room.client_team_indices(c.id);
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   359
        }
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   360
        response.add(RunGame.send_all().in_room(room.id));
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   361
        response.add(
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   362
            ClientFlags("+g".to_string(), room_nicks)
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   363
                .send_all()
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   364
                .in_room(room.id),
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   365
        );
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   366
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   367
        let room_master = if let Some(id) = room.master_id {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   368
            Some(&server.clients[id])
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   369
        } else {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   370
            None
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   371
        };
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   372
        get_room_update(None, room, room_master, response);
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   373
    }
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   374
}
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   375
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   376
pub fn end_game(server: &mut HWServer, room_id: RoomId, response: &mut Response) {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   377
    let room = &mut server.rooms[room_id];
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   378
    room.ready_players_number = 1;
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   379
    let room_master = if let Some(id) = room.master_id {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   380
        Some(&server.clients[id])
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   381
    } else {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   382
        None
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   383
    };
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   384
    get_room_update(None, room, room_master, response);
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   385
    response.add(RoundFinished.send_all().in_room(room_id));
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   386
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   387
    if let Some(info) = replace(&mut room.game_info, None) {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   388
        for (_, client) in server.clients.iter() {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   389
            if client.room_id == Some(room_id) && client.is_joined_mid_game() {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   390
                //SendRoomData { to: client.id, teams: false, config: true, flags: false}
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   391
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   392
                response.extend(
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   393
                    info.left_teams
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   394
                        .iter()
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   395
                        .map(|name| TeamRemove(name.clone()).send(client.id)),
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   396
                );
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   397
            }
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   398
        }
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   399
    }
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   400
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   401
    let nicks: Vec<_> = server
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   402
        .clients
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   403
        .iter_mut()
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   404
        .filter(|(_, c)| c.room_id == Some(room_id))
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   405
        .map(|(_, c)| {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   406
            c.set_is_ready(c.is_master());
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   407
            c.set_is_joined_mid_game(false);
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   408
            c
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   409
        })
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   410
        .filter_map(|c| {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   411
            if !c.is_master() {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   412
                Some(c.nick.clone())
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   413
            } else {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   414
                None
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   415
            }
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   416
        })
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   417
        .collect();
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   418
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   419
    if !nicks.is_empty() {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   420
        let msg = if room.protocol_number < 38 {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   421
            LegacyReady(false, nicks)
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   422
        } else {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   423
            ClientFlags("-r".to_string(), nicks)
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   424
        };
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   425
        response.add(msg.send_all().in_room(room_id));
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   426
    }
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   427
}
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14708
diff changeset
   428
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   429
#[cfg(test)]
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   430
mod tests {
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   431
    use super::*;
13713
59ea2403f62d move everything test related into test cfg
alfadur
parents: 13666
diff changeset
   432
    use crate::protocol::messages::HWServerMessage::ChatMsg;
14704
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14696
diff changeset
   433
    use crate::server::actions::PendingMessage;
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   434
13445
d3c86ade3d4d Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13444
diff changeset
   435
    fn reply2string(r: HWServerMessage) -> String {
d3c86ade3d4d Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13444
diff changeset
   436
        match r {
d3c86ade3d4d Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13444
diff changeset
   437
            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
   438
            _ => panic!("expected a ChatMsg"),
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   439
        }
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   440
    }
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   441
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   442
    fn run_handle_test(opts: Vec<String>) {
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   443
        let opts2 = opts.clone();
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   444
        for opt in opts {
13492
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
   445
            while reply2string(rnd_reply(&opts2)) != opt {}
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   446
        }
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   447
    }
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   448
13446
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   449
    /// This test terminates almost surely.
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   450
    #[test]
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   451
    fn test_handle_rnd_empty() {
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   452
        run_handle_test(vec![])
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   453
    }
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   454
13446
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   455
    /// This test terminates almost surely.
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   456
    #[test]
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   457
    fn test_handle_rnd_nonempty() {
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   458
        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
   459
    }
13446
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   460
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   461
    /// 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
   462
    #[test]
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   463
    fn test_distribution() {
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   464
        let eps = 0.000001;
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   465
        let lim = 0.5;
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   466
        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
   467
        let mut ones = 0;
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   468
        let mut tries = 0;
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   469
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   470
        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
   471
            tries += 1;
13492
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
   472
            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
   473
                ones += 1;
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   474
            }
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   475
        }
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   476
    }
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   477
}