rust/hedgewars-server/src/server/handlers/inroom.rs
author alfadur
Thu, 07 Feb 2019 18:04:53 +0300
changeset 14695 216d39de1a44
parent 14694 25c564f77b7d
child 14697 f64e21f164a5
permissions -rw-r--r--
fix official server build & bump dependencies
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
     1
use mio;
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
     2
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
     3
use super::common::rnd_reply;
14689
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
     4
use crate::utils::to_engine_msg;
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
     5
use crate::{
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
     6
    protocol::messages::{server_chat, HWProtocolMessage, HWServerMessage::*},
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
     7
    server::{
14374
e5db279308d7 dispose of server mods
alfadur
parents: 13805
diff changeset
     8
        core::HWServer,
14687
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
     9
        coretypes,
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    10
        coretypes::{ClientId, GameCfg, RoomId, VoteType, Voting, MAX_HEDGEHOGS_PER_TEAM},
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
    11
        room::{HWRoom, RoomFlags},
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
    12
    },
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    13
    utils::is_name_illegal,
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    14
};
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    15
use base64::{decode, encode};
13805
0463a4221327 cleanup crate imports
alfadur
parents: 13801
diff changeset
    16
use log::*;
14689
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
    17
use std::iter::once;
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    18
use std::mem::swap;
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    19
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    20
#[derive(Clone)]
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    21
struct ByMsg<'a> {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    22
    messages: &'a [u8],
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    23
}
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    24
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    25
impl<'a> Iterator for ByMsg<'a> {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    26
    type Item = &'a [u8];
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    27
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    28
    fn next(&mut self) -> Option<<Self as Iterator>::Item> {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    29
        if let Some(size) = self.messages.get(0) {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    30
            let (msg, next) = self.messages.split_at(*size as usize + 1);
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    31
            self.messages = next;
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    32
            Some(msg)
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    33
        } else {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    34
            None
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    35
        }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    36
    }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    37
}
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    38
13524
5359ff75da3a indulge clippy
alfadur
parents: 13523
diff changeset
    39
fn by_msg(source: &[u8]) -> ByMsg {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    40
    ByMsg { messages: source }
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    41
}
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    42
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    43
const VALID_MESSAGES: &[u8] =
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    44
    b"M#+LlRrUuDdZzAaSjJ,NpPwtgfhbc12345\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A";
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    45
const NON_TIMED_MESSAGES: &[u8] = b"M#hb";
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    46
13429
4c5ed27b1ff8 stabilization
alfadur
parents: 13428
diff changeset
    47
#[cfg(canhazslicepatterns)]
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    48
fn is_msg_valid(msg: &[u8], team_indices: &[u8]) -> bool {
13424
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
    49
    match msg {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    50
        [size, typ, body..] => {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    51
            VALID_MESSAGES.contains(typ)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    52
                && match body {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    53
                    [1...MAX_HEDGEHOGS_PER_TEAM, team, ..] if *typ == b'h' => {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    54
                        team_indices.contains(team)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    55
                    }
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    56
                    _ => *typ != b'h',
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    57
                }
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    58
        }
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    59
        _ => false,
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    60
    }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    61
}
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    62
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
    63
fn is_msg_valid(msg: &[u8], _team_indices: &[u8]) -> bool {
13429
4c5ed27b1ff8 stabilization
alfadur
parents: 13428
diff changeset
    64
    if let Some(typ) = msg.get(1) {
4c5ed27b1ff8 stabilization
alfadur
parents: 13428
diff changeset
    65
        VALID_MESSAGES.contains(typ)
4c5ed27b1ff8 stabilization
alfadur
parents: 13428
diff changeset
    66
    } else {
4c5ed27b1ff8 stabilization
alfadur
parents: 13428
diff changeset
    67
        false
4c5ed27b1ff8 stabilization
alfadur
parents: 13428
diff changeset
    68
    }
4c5ed27b1ff8 stabilization
alfadur
parents: 13428
diff changeset
    69
}
4c5ed27b1ff8 stabilization
alfadur
parents: 13428
diff changeset
    70
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    71
fn is_msg_empty(msg: &[u8]) -> bool {
13429
4c5ed27b1ff8 stabilization
alfadur
parents: 13428
diff changeset
    72
    msg.get(1).filter(|t| **t == b'+').is_some()
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    73
}
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    74
13443
2501428303a2 Fix team remove synchronization
alfadur
parents: 13439
diff changeset
    75
fn is_msg_timed(msg: &[u8]) -> bool {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    76
    msg.get(1)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    77
        .filter(|t| !NON_TIMED_MESSAGES.contains(t))
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    78
        .is_some()
13443
2501428303a2 Fix team remove synchronization
alfadur
parents: 13439
diff changeset
    79
}
2501428303a2 Fix team remove synchronization
alfadur
parents: 13439
diff changeset
    80
13480
fb37745c5bca complete newseed voting
alfadur
parents: 13478
diff changeset
    81
fn voting_description(kind: &VoteType) -> String {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    82
    format!(
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    83
        "New voting started: {}",
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    84
        match kind {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    85
            VoteType::Kick(nick) => format!("kick {}", nick),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    86
            VoteType::Map(name) => format!("map {}", name.as_ref().unwrap()),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    87
            VoteType::Pause => "pause".to_string(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    88
            VoteType::NewSeed => "new seed".to_string(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    89
            VoteType::HedgehogsPerTeam(number) => format!("hedgehogs per team: {}", number),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    90
        }
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    91
    )
13480
fb37745c5bca complete newseed voting
alfadur
parents: 13478
diff changeset
    92
}
fb37745c5bca complete newseed voting
alfadur
parents: 13478
diff changeset
    93
13523
8c5dd562c9f7 Add room flags
alfadur
parents: 13521
diff changeset
    94
fn room_message_flag(msg: &HWProtocolMessage) -> RoomFlags {
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
    95
    use crate::protocol::messages::HWProtocolMessage::*;
13523
8c5dd562c9f7 Add room flags
alfadur
parents: 13521
diff changeset
    96
    match msg {
8c5dd562c9f7 Add room flags
alfadur
parents: 13521
diff changeset
    97
        ToggleRestrictJoin => RoomFlags::RESTRICTED_JOIN,
8c5dd562c9f7 Add room flags
alfadur
parents: 13521
diff changeset
    98
        ToggleRestrictTeams => RoomFlags::RESTRICTED_TEAM_ADD,
8c5dd562c9f7 Add room flags
alfadur
parents: 13521
diff changeset
    99
        ToggleRegisteredOnly => RoomFlags::RESTRICTED_UNREGISTERED_PLAYERS,
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   100
        _ => RoomFlags::empty(),
13523
8c5dd562c9f7 Add room flags
alfadur
parents: 13521
diff changeset
   101
    }
8c5dd562c9f7 Add room flags
alfadur
parents: 13521
diff changeset
   102
}
8c5dd562c9f7 Add room flags
alfadur
parents: 13521
diff changeset
   103
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   104
pub fn handle(
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   105
    server: &mut HWServer,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   106
    client_id: ClientId,
14671
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
   107
    response: &mut super::Response,
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   108
    room_id: RoomId,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   109
    message: HWProtocolMessage,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   110
) {
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
   111
    use crate::protocol::messages::HWProtocolMessage::*;
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
   112
    match message {
14675
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14671
diff changeset
   113
        Part(msg) => {
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14671
diff changeset
   114
            if let (client, Some(room)) = server.client_and_room(client_id) {
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14671
diff changeset
   115
                let msg = match msg {
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14671
diff changeset
   116
                    Some(s) => format!("part: {}", s),
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   117
                    None => "part".to_string(),
14675
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14671
diff changeset
   118
                };
14690
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   119
                super::common::exit_room(server, client_id, response, &msg);
14675
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14671
diff changeset
   120
            }
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   121
        }
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   122
        Chat(msg) => {
14675
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14671
diff changeset
   123
            let client = &mut server.clients[client_id];
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   124
            response.add(
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   125
                ChatMsg {
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   126
                    nick: client.nick.clone(),
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   127
                    msg,
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   128
                }
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   129
                .send_all()
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   130
                .in_room(room_id),
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   131
            );
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   132
        }
13477
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13445
diff changeset
   133
        Fix => {
14675
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14671
diff changeset
   134
            if let (client, Some(room)) = server.client_and_room(client_id) {
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   135
                if client.is_admin() {
14690
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   136
                    room.set_is_fixed(true);
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   137
                    room.set_join_restriction(false);
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   138
                    room.set_team_add_restriction(false);
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   139
                    room.set_unregistered_players_restriction(true);
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   140
                }
13477
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13445
diff changeset
   141
            }
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13445
diff changeset
   142
        }
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13445
diff changeset
   143
        Unfix => {
14675
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14671
diff changeset
   144
            if let (client, Some(room)) = server.client_and_room(client_id) {
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   145
                if client.is_admin() {
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   146
                    room.set_is_fixed(false)
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   147
                }
13477
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13445
diff changeset
   148
            }
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13445
diff changeset
   149
        }
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13445
diff changeset
   150
        Greeting(text) => {
14675
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14671
diff changeset
   151
            if let (clienr, Some(room)) = server.client_and_room(client_id) {
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14671
diff changeset
   152
                if clienr.is_admin() || clienr.is_master() && !room.is_fixed() {
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14671
diff changeset
   153
                    room.greeting = text
13477
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13445
diff changeset
   154
                }
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13445
diff changeset
   155
            }
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13445
diff changeset
   156
        }
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   157
        RoomName(new_name) => {
14675
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14671
diff changeset
   158
            if is_name_illegal(&new_name) {
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14671
diff changeset
   159
                response.add(Warning("Illegal room name! A room name must be between 1-40 characters long, must not have a trailing or leading space and must not have any of these characters: $()*+?[]^{|}".to_string()).send_self());
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14671
diff changeset
   160
            } else if server.has_room(&new_name) {
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   161
                response.add(
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   162
                    Warning("A room with the same name already exists.".to_string()).send_self(),
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   163
                );
14675
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14671
diff changeset
   164
            } else {
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14671
diff changeset
   165
                let room = &mut server.rooms[room_id];
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   166
                if room.is_fixed() || room.master_id != Some(client_id) {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   167
                    response.add(Warning("Access denied.".to_string()).send_self());
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   168
                } else {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   169
                    let mut old_name = new_name.clone();
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   170
                    let client = &server.clients[client_id];
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   171
                    swap(&mut room.name, &mut old_name);
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   172
                    super::common::get_room_update(Some(old_name), room, Some(&client), response);
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   173
                }
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   174
            }
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   175
        }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   176
        ToggleReady => {
14675
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14671
diff changeset
   177
            if let (client, Some(room)) = server.client_and_room(client_id) {
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14671
diff changeset
   178
                let flags = if client.is_ready() {
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14671
diff changeset
   179
                    room.ready_players_number -= 1;
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   180
                    "-r"
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   181
                } else {
14675
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14671
diff changeset
   182
                    room.ready_players_number += 1;
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   183
                    "+r"
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   184
                };
13801
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13795
diff changeset
   185
14675
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14671
diff changeset
   186
                let msg = if client.protocol_number < 38 {
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14671
diff changeset
   187
                    LegacyReady(client.is_ready(), vec![client.nick.clone()])
13801
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13795
diff changeset
   188
                } else {
14675
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14671
diff changeset
   189
                    ClientFlags(flags.to_string(), vec![client.nick.clone()])
13801
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13795
diff changeset
   190
                };
14675
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14671
diff changeset
   191
                response.add(msg.send_all().in_room(room.id));
14691
2071da901c63 reconnect some message handlers
alfadur <mail@none>
parents: 14690
diff changeset
   192
                client.set_is_ready(!client.is_ready());
2071da901c63 reconnect some message handlers
alfadur <mail@none>
parents: 14690
diff changeset
   193
14675
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14671
diff changeset
   194
                if room.is_fixed() && room.ready_players_number == room.players_number {
14691
2071da901c63 reconnect some message handlers
alfadur <mail@none>
parents: 14690
diff changeset
   195
                    super::common::start_game(server, room_id, response);
13477
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13445
diff changeset
   196
                }
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
   197
            }
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   198
        }
13422
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   199
        AddTeam(info) => {
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   200
            let client = &mut server.clients[client_id];
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   201
            let room = &mut server.rooms[room_id];
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   202
            if room.teams.len() >= room.team_limit as usize {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   203
                response.add(Warning("Too many teams!".to_string()).send_self());
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   204
            } else if room.addable_hedgehogs() == 0 {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   205
                response.add(Warning("Too many hedgehogs!".to_string()).send_self());
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   206
            } else if room.find_team(|t| t.name == info.name) != None {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   207
                response.add(
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   208
                    Warning("There's already a team with same name in the list.".to_string())
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   209
                        .send_self(),
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   210
                );
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   211
            } else if room.game_info.is_some() {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   212
                response.add(
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   213
                    Warning("Joining not possible: Round is in progress.".to_string()).send_self(),
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   214
                );
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   215
            } else if room.is_team_add_restricted() {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   216
                response.add(
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   217
                    Warning("This room currently does not allow adding new teams.".to_string())
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   218
                        .send_self(),
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   219
                );
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   220
            } else {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   221
                let team = room.add_team(client.id, *info, client.protocol_number < 42);
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   222
                client.teams_in_game += 1;
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   223
                client.clan = Some(team.color);
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   224
                response.add(TeamAccepted(team.name.clone()).send_self());
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   225
                response.add(
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   226
                    TeamAdd(HWRoom::team_info(&client, team))
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   227
                        .send_all()
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   228
                        .in_room(room_id)
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   229
                        .but_self(),
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   230
                );
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   231
                response.add(
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   232
                    TeamColor(team.name.clone(), team.color)
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   233
                        .send_all()
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   234
                        .in_room(room_id),
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   235
                );
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   236
                response.add(
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   237
                    HedgehogsNumber(team.name.clone(), team.hedgehogs_number)
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   238
                        .send_all()
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   239
                        .in_room(room_id),
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   240
                );
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   241
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   242
                let room_master = if let Some(id) = room.master_id {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   243
                    Some(&server.clients[id])
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   244
                } else {
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   245
                    None
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   246
                };
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   247
                super::common::get_room_update(None, room, room_master, response);
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   248
            }
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   249
        }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   250
        RemoveTeam(name) => {
14691
2071da901c63 reconnect some message handlers
alfadur <mail@none>
parents: 14690
diff changeset
   251
            let client = &mut server.clients[client_id];
2071da901c63 reconnect some message handlers
alfadur <mail@none>
parents: 14690
diff changeset
   252
            let room = &mut server.rooms[room_id];
2071da901c63 reconnect some message handlers
alfadur <mail@none>
parents: 14690
diff changeset
   253
            match room.find_team_owner(&name) {
2071da901c63 reconnect some message handlers
alfadur <mail@none>
parents: 14690
diff changeset
   254
                None => response.add(
2071da901c63 reconnect some message handlers
alfadur <mail@none>
parents: 14690
diff changeset
   255
                    Warning("Error: The team you tried to remove does not exist.".to_string())
2071da901c63 reconnect some message handlers
alfadur <mail@none>
parents: 14690
diff changeset
   256
                        .send_self(),
2071da901c63 reconnect some message handlers
alfadur <mail@none>
parents: 14690
diff changeset
   257
                ),
2071da901c63 reconnect some message handlers
alfadur <mail@none>
parents: 14690
diff changeset
   258
                Some((id, _)) if id != client_id => response
2071da901c63 reconnect some message handlers
alfadur <mail@none>
parents: 14690
diff changeset
   259
                    .add(Warning("You can't remove a team you don't own.".to_string()).send_self()),
2071da901c63 reconnect some message handlers
alfadur <mail@none>
parents: 14690
diff changeset
   260
                Some((_, name)) => {
2071da901c63 reconnect some message handlers
alfadur <mail@none>
parents: 14690
diff changeset
   261
                    client.teams_in_game -= 1;
2071da901c63 reconnect some message handlers
alfadur <mail@none>
parents: 14690
diff changeset
   262
                    client.clan = room.find_team_color(client.id);
2071da901c63 reconnect some message handlers
alfadur <mail@none>
parents: 14690
diff changeset
   263
                    super::common::remove_teams(
2071da901c63 reconnect some message handlers
alfadur <mail@none>
parents: 14690
diff changeset
   264
                        room,
2071da901c63 reconnect some message handlers
alfadur <mail@none>
parents: 14690
diff changeset
   265
                        vec![name.to_string()],
2071da901c63 reconnect some message handlers
alfadur <mail@none>
parents: 14690
diff changeset
   266
                        client.is_in_game(),
2071da901c63 reconnect some message handlers
alfadur <mail@none>
parents: 14690
diff changeset
   267
                        response,
2071da901c63 reconnect some message handlers
alfadur <mail@none>
parents: 14690
diff changeset
   268
                    );
2071da901c63 reconnect some message handlers
alfadur <mail@none>
parents: 14690
diff changeset
   269
2071da901c63 reconnect some message handlers
alfadur <mail@none>
parents: 14690
diff changeset
   270
                    match room.game_info {
2071da901c63 reconnect some message handlers
alfadur <mail@none>
parents: 14690
diff changeset
   271
                        Some(ref info) if info.teams_in_game == 0 => {
2071da901c63 reconnect some message handlers
alfadur <mail@none>
parents: 14690
diff changeset
   272
                            super::common::end_game(server, room_id, response)
2071da901c63 reconnect some message handlers
alfadur <mail@none>
parents: 14690
diff changeset
   273
                        }
2071da901c63 reconnect some message handlers
alfadur <mail@none>
parents: 14690
diff changeset
   274
                        _ => (),
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   275
                    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   276
                }
14675
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14671
diff changeset
   277
            }
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   278
        }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   279
        SetHedgehogsNumber(team_name, number) => {
14676
9377ee00f1f1 Server action refactoring part 7 of N
alfadur <mail@none>
parents: 14675
diff changeset
   280
            if let (client, Some(room)) = server.client_and_room(client_id) {
9377ee00f1f1 Server action refactoring part 7 of N
alfadur <mail@none>
parents: 14675
diff changeset
   281
                let addable_hedgehogs = room.addable_hedgehogs();
9377ee00f1f1 Server action refactoring part 7 of N
alfadur <mail@none>
parents: 14675
diff changeset
   282
                if let Some((_, team)) = room.find_team_and_owner_mut(|t| t.name == team_name) {
9377ee00f1f1 Server action refactoring part 7 of N
alfadur <mail@none>
parents: 14675
diff changeset
   283
                    if !client.is_master() {
9377ee00f1f1 Server action refactoring part 7 of N
alfadur <mail@none>
parents: 14675
diff changeset
   284
                        response.add(Error("You're not the room master!".to_string()).send_self());
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   285
                    } else if number < 1
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   286
                        || number > MAX_HEDGEHOGS_PER_TEAM
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   287
                        || number > addable_hedgehogs + team.hedgehogs_number
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   288
                    {
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   289
                        response.add(
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   290
                            HedgehogsNumber(team.name.clone(), team.hedgehogs_number).send_self(),
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   291
                        );
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   292
                    } else {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   293
                        team.hedgehogs_number = number;
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   294
                        response.add(
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   295
                            HedgehogsNumber(team.name.clone(), number)
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   296
                                .send_all()
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   297
                                .in_room(room_id)
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   298
                                .but_self(),
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   299
                        );
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   300
                    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   301
                } else {
14676
9377ee00f1f1 Server action refactoring part 7 of N
alfadur <mail@none>
parents: 14675
diff changeset
   302
                    response.add(Warning("No such team.".to_string()).send_self());
9377ee00f1f1 Server action refactoring part 7 of N
alfadur <mail@none>
parents: 14675
diff changeset
   303
                }
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
   304
            }
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   305
        }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   306
        SetTeamColor(team_name, color) => {
14676
9377ee00f1f1 Server action refactoring part 7 of N
alfadur <mail@none>
parents: 14675
diff changeset
   307
            if let (client, Some(room)) = server.client_and_room(client_id) {
9377ee00f1f1 Server action refactoring part 7 of N
alfadur <mail@none>
parents: 14675
diff changeset
   308
                if let Some((owner, team)) = room.find_team_and_owner_mut(|t| t.name == team_name) {
9377ee00f1f1 Server action refactoring part 7 of N
alfadur <mail@none>
parents: 14675
diff changeset
   309
                    if !client.is_master() {
9377ee00f1f1 Server action refactoring part 7 of N
alfadur <mail@none>
parents: 14675
diff changeset
   310
                        response.add(Error("You're not the room master!".to_string()).send_self());
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   311
                    } else {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   312
                        team.color = color;
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   313
                        response.add(
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   314
                            TeamColor(team.name.clone(), color)
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   315
                                .send_all()
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   316
                                .in_room(room_id)
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   317
                                .but_self(),
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   318
                        );
14676
9377ee00f1f1 Server action refactoring part 7 of N
alfadur <mail@none>
parents: 14675
diff changeset
   319
                        server.clients[owner].clan = Some(color);
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   320
                    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   321
                } else {
14676
9377ee00f1f1 Server action refactoring part 7 of N
alfadur <mail@none>
parents: 14675
diff changeset
   322
                    response.add(Warning("No such team.".to_string()).send_self());
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
   323
                }
14676
9377ee00f1f1 Server action refactoring part 7 of N
alfadur <mail@none>
parents: 14675
diff changeset
   324
            }
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   325
        }
13422
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   326
        Cfg(cfg) => {
14676
9377ee00f1f1 Server action refactoring part 7 of N
alfadur <mail@none>
parents: 14675
diff changeset
   327
            if let (client, Some(room)) = server.client_and_room(client_id) {
9377ee00f1f1 Server action refactoring part 7 of N
alfadur <mail@none>
parents: 14675
diff changeset
   328
                if room.is_fixed() {
9377ee00f1f1 Server action refactoring part 7 of N
alfadur <mail@none>
parents: 14675
diff changeset
   329
                    response.add(Warning("Access denied.".to_string()).send_self());
9377ee00f1f1 Server action refactoring part 7 of N
alfadur <mail@none>
parents: 14675
diff changeset
   330
                } else if !client.is_master() {
9377ee00f1f1 Server action refactoring part 7 of N
alfadur <mail@none>
parents: 14675
diff changeset
   331
                    response.add(Error("You're not the room master!".to_string()).send_self());
13422
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   332
                } else {
13801
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13795
diff changeset
   333
                    let cfg = match cfg {
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13795
diff changeset
   334
                        GameCfg::Scheme(name, mut values) => {
14676
9377ee00f1f1 Server action refactoring part 7 of N
alfadur <mail@none>
parents: 14675
diff changeset
   335
                            if client.protocol_number == 49 && values.len() >= 2 {
13801
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13795
diff changeset
   336
                                let mut s = "X".repeat(50);
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13795
diff changeset
   337
                                s.push_str(&values.pop().unwrap());
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13795
diff changeset
   338
                                values.push(s);
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13795
diff changeset
   339
                            }
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13795
diff changeset
   340
                            GameCfg::Scheme(name, values)
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13795
diff changeset
   341
                        }
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   342
                        cfg => cfg,
13801
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13795
diff changeset
   343
                    };
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13795
diff changeset
   344
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   345
                    response.add(cfg.to_server_msg().send_all().in_room(room.id).but_self());
14676
9377ee00f1f1 Server action refactoring part 7 of N
alfadur <mail@none>
parents: 14675
diff changeset
   346
                    room.set_config(cfg);
9377ee00f1f1 Server action refactoring part 7 of N
alfadur <mail@none>
parents: 14675
diff changeset
   347
                }
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
   348
            }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   349
        }
13528
c8b626b0a3ad Implement save/delete commands
alfadur
parents: 13527
diff changeset
   350
        Save(name, location) => {
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   351
            response.add(
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   352
                server_chat(format!("Room config saved as {}", name))
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   353
                    .send_all()
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   354
                    .in_room(room_id),
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   355
            );
13528
c8b626b0a3ad Implement save/delete commands
alfadur
parents: 13527
diff changeset
   356
            server.rooms[room_id].save_config(name, location);
c8b626b0a3ad Implement save/delete commands
alfadur
parents: 13527
diff changeset
   357
        }
13529
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   358
        SaveRoom(filename) => {
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
   359
            if server.clients[client_id].is_admin() {
14676
9377ee00f1f1 Server action refactoring part 7 of N
alfadur <mail@none>
parents: 14675
diff changeset
   360
                match server.rooms[room_id].get_saves() {
14392
e335b3120f59 pull file io out of server handler
alfadur
parents: 14374
diff changeset
   361
                    Ok(text) => match server.io.write_file(&filename, &text) {
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   362
                        Ok(_) => response.add(
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   363
                            server_chat("Room configs saved successfully.".to_string()).send_self(),
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   364
                        ),
13529
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   365
                        Err(e) => {
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   366
                            warn!(
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   367
                                "Error while writing the config file \"{}\": {}",
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   368
                                filename, e
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   369
                            );
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   370
                            response.add(
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   371
                                Warning("Unable to save the room configs.".to_string()).send_self(),
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   372
                            );
13529
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   373
                        }
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   374
                    },
13529
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   375
                    Err(e) => {
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   376
                        warn!("Error while serializing the room configs: {}", e);
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   377
                        response.add(
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   378
                            Warning("Unable to serialize the room configs.".to_string())
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   379
                                .send_self(),
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   380
                        )
13529
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   381
                    }
14676
9377ee00f1f1 Server action refactoring part 7 of N
alfadur <mail@none>
parents: 14675
diff changeset
   382
                }
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
   383
            }
13529
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   384
        }
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   385
        LoadRoom(filename) => {
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
   386
            if server.clients[client_id].is_admin() {
14676
9377ee00f1f1 Server action refactoring part 7 of N
alfadur <mail@none>
parents: 14675
diff changeset
   387
                match server.io.read_file(&filename) {
13529
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   388
                    Ok(text) => match server.rooms[room_id].set_saves(&text) {
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   389
                        Ok(_) => response.add(
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   390
                            server_chat("Room configs loaded successfully.".to_string())
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   391
                                .send_self(),
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   392
                        ),
13529
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   393
                        Err(e) => {
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   394
                            warn!("Error while deserializing the room configs: {}", e);
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   395
                            response.add(
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   396
                                Warning("Unable to deserialize the room configs.".to_string())
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   397
                                    .send_self(),
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   398
                            );
13529
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   399
                        }
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   400
                    },
13529
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   401
                    Err(e) => {
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   402
                        warn!(
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   403
                            "Error while reading the config file \"{}\": {}",
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   404
                            filename, e
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   405
                        );
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   406
                        response.add(
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   407
                            Warning("Unable to load the room configs.".to_string()).send_self(),
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   408
                        );
13529
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   409
                    }
14676
9377ee00f1f1 Server action refactoring part 7 of N
alfadur <mail@none>
parents: 14675
diff changeset
   410
                }
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
   411
            }
13529
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   412
        }
13528
c8b626b0a3ad Implement save/delete commands
alfadur
parents: 13527
diff changeset
   413
        Delete(name) => {
14676
9377ee00f1f1 Server action refactoring part 7 of N
alfadur <mail@none>
parents: 14675
diff changeset
   414
            if !server.rooms[room_id].delete_config(&name) {
9377ee00f1f1 Server action refactoring part 7 of N
alfadur <mail@none>
parents: 14675
diff changeset
   415
                response.add(Warning(format!("Save doesn't exist: {}", name)).send_self());
13528
c8b626b0a3ad Implement save/delete commands
alfadur
parents: 13527
diff changeset
   416
            } else {
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   417
                response.add(
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   418
                    server_chat(format!("Room config {} has been deleted", name))
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   419
                        .send_all()
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   420
                        .in_room(room_id),
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   421
                );
14676
9377ee00f1f1 Server action refactoring part 7 of N
alfadur <mail@none>
parents: 14675
diff changeset
   422
            }
13528
c8b626b0a3ad Implement save/delete commands
alfadur
parents: 13527
diff changeset
   423
        }
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   424
        CallVote(None) => {
14676
9377ee00f1f1 Server action refactoring part 7 of N
alfadur <mail@none>
parents: 14675
diff changeset
   425
            response.add(server_chat("Available callvote commands: kick <nickname>, map <name>, pause, newseed, hedgehogs <number>".to_string())
9377ee00f1f1 Server action refactoring part 7 of N
alfadur <mail@none>
parents: 14675
diff changeset
   426
                .send_self());
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   427
        }
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   428
        CallVote(Some(kind)) => {
13521
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13520
diff changeset
   429
            let is_in_game = server.rooms[room_id].game_info.is_some();
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   430
            let error = match &kind {
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   431
                VoteType::Kick(nick) => {
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   432
                    if server
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   433
                        .find_client(&nick)
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   434
                        .filter(|c| c.room_id == Some(room_id))
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   435
                        .is_some()
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   436
                    {
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   437
                        None
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   438
                    } else {
13527
e3ae9eea0689 Implement map voting
alfadur
parents: 13526
diff changeset
   439
                        Some("/callvote kick: No such user!".to_string())
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   440
                    }
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   441
                }
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   442
                VoteType::Map(None) => {
13527
e3ae9eea0689 Implement map voting
alfadur
parents: 13526
diff changeset
   443
                    let names: Vec<_> = server.rooms[room_id].saves.keys().cloned().collect();
e3ae9eea0689 Implement map voting
alfadur
parents: 13526
diff changeset
   444
                    if names.is_empty() {
e3ae9eea0689 Implement map voting
alfadur
parents: 13526
diff changeset
   445
                        Some("/callvote map: No maps saved in this room!".to_string())
e3ae9eea0689 Implement map voting
alfadur
parents: 13526
diff changeset
   446
                    } else {
e3ae9eea0689 Implement map voting
alfadur
parents: 13526
diff changeset
   447
                        Some(format!("Available maps: {}", names.join(", ")))
e3ae9eea0689 Implement map voting
alfadur
parents: 13526
diff changeset
   448
                    }
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   449
                }
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   450
                VoteType::Map(Some(name)) => {
13527
e3ae9eea0689 Implement map voting
alfadur
parents: 13526
diff changeset
   451
                    if server.rooms[room_id].saves.get(&name[..]).is_some() {
13530
80db7232b4b5 Fix map voting verification
alfadur
parents: 13529
diff changeset
   452
                        None
13527
e3ae9eea0689 Implement map voting
alfadur
parents: 13526
diff changeset
   453
                    } else {
13530
80db7232b4b5 Fix map voting verification
alfadur
parents: 13529
diff changeset
   454
                        Some("/callvote map: No such map!".to_string())
13527
e3ae9eea0689 Implement map voting
alfadur
parents: 13526
diff changeset
   455
                    }
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   456
                }
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   457
                VoteType::Pause => {
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   458
                    if is_in_game {
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   459
                        None
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   460
                    } else {
13527
e3ae9eea0689 Implement map voting
alfadur
parents: 13526
diff changeset
   461
                        Some("/callvote pause: No game in progress!".to_string())
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   462
                    }
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   463
                }
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   464
                VoteType::NewSeed => None,
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   465
                VoteType::HedgehogsPerTeam(number) => match number {
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   466
                    1...MAX_HEDGEHOGS_PER_TEAM => None,
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   467
                    _ => Some("/callvote hedgehogs: Specify number from 1 to 8.".to_string()),
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   468
                },
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   469
            };
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   470
            match error {
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   471
                None => {
13480
fb37745c5bca complete newseed voting
alfadur
parents: 13478
diff changeset
   472
                    let msg = voting_description(&kind);
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   473
                    let voting = Voting::new(kind, server.room_clients(client_id));
14686
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   474
                    let room = &mut server.rooms[room_id];
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   475
                    room.voting = Some(voting);
14676
9377ee00f1f1 Server action refactoring part 7 of N
alfadur <mail@none>
parents: 14675
diff changeset
   476
                    response.add(server_chat(msg).send_all().in_room(room_id));
14687
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   477
                    super::common::submit_vote(
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   478
                        server,
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   479
                        coretypes::Vote {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   480
                            is_pro: true,
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   481
                            is_forced: false,
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   482
                        },
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   483
                        response,
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   484
                    );
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   485
                }
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   486
                Some(msg) => {
14676
9377ee00f1f1 Server action refactoring part 7 of N
alfadur <mail@none>
parents: 14675
diff changeset
   487
                    response.add(server_chat(msg).send_self());
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   488
                }
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   489
            }
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   490
        }
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   491
        Vote(vote) => {
14687
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   492
            super::common::submit_vote(
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   493
                server,
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   494
                coretypes::Vote {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   495
                    is_pro: vote,
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   496
                    is_forced: false,
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   497
                },
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   498
                response,
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   499
            );
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   500
        }
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   501
        ForceVote(vote) => {
13526
3f69acc7e268 clean up room lookups
alfadur
parents: 13525
diff changeset
   502
            let is_forced = server.clients[client_id].is_admin();
14687
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   503
            super::common::submit_vote(
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   504
                server,
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   505
                coretypes::Vote {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   506
                    is_pro: vote,
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   507
                    is_forced,
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   508
                },
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   509
                response,
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   510
            );
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   511
        }
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   512
        ToggleRestrictJoin | ToggleRestrictTeams | ToggleRegisteredOnly => {
14686
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   513
            let client = &server.clients[client_id];
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   514
            let room = &mut server.rooms[room_id];
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   515
            if client.is_master() {
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   516
                room.flags.toggle(room_message_flag(&message));
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   517
                super::common::get_room_update(None, room, Some(&client), response);
13523
8c5dd562c9f7 Add room flags
alfadur
parents: 13521
diff changeset
   518
            }
8c5dd562c9f7 Add room flags
alfadur
parents: 13521
diff changeset
   519
        }
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   520
        StartGame => {
14691
2071da901c63 reconnect some message handlers
alfadur <mail@none>
parents: 14690
diff changeset
   521
            super::common::start_game(server, room_id, response);
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   522
        }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   523
        EngineMessage(em) => {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   524
            if let (c, Some(r)) = server.client_and_room(client_id) {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   525
                if c.teams_in_game > 0 {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   526
                    let decoding = decode(&em[..]).unwrap();
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   527
                    let messages = by_msg(&decoding);
13443
2501428303a2 Fix team remove synchronization
alfadur
parents: 13439
diff changeset
   528
                    let valid = messages.filter(|m| is_msg_valid(m, &c.team_indices));
2501428303a2 Fix team remove synchronization
alfadur
parents: 13439
diff changeset
   529
                    let non_empty = valid.clone().filter(|m| !is_msg_empty(m));
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   530
                    let sync_msg = valid.clone().filter(|m| is_msg_timed(m)).last().map(|m| {
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   531
                        if is_msg_empty(m) {
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   532
                            Some(encode(m))
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   533
                        } else {
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   534
                            None
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   535
                        }
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   536
                    });
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   537
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   538
                    let em_response =
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   539
                        encode(&valid.flat_map(|msg| msg).cloned().collect::<Vec<_>>());
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   540
                    if !em_response.is_empty() {
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   541
                        response.add(
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   542
                            ForwardEngineMessage(vec![em_response])
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   543
                                .send_all()
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   544
                                .in_room(r.id)
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   545
                                .but_self(),
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   546
                        );
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   547
                    }
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   548
                    let em_log =
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   549
                        encode(&non_empty.flat_map(|msg| msg).cloned().collect::<Vec<_>>());
13427
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   550
                    if let Some(ref mut info) = r.game_info {
13429
4c5ed27b1ff8 stabilization
alfadur
parents: 13428
diff changeset
   551
                        if !em_log.is_empty() {
13428
c8425fbcf1d9 Fix spectators joining midgame
alfadur
parents: 13427
diff changeset
   552
                            info.msg_log.push(em_log);
c8425fbcf1d9 Fix spectators joining midgame
alfadur
parents: 13427
diff changeset
   553
                        }
13443
2501428303a2 Fix team remove synchronization
alfadur
parents: 13439
diff changeset
   554
                        if let Some(msg) = sync_msg {
2501428303a2 Fix team remove synchronization
alfadur
parents: 13439
diff changeset
   555
                            info.sync_msg = msg;
13427
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   556
                        }
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   557
                    }
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   558
                }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   559
            }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   560
        }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   561
        RoundFinished => {
14689
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   562
            let mut game_ended = false;
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   563
            let client = &mut server.clients[client_id];
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   564
            if client.is_in_game() {
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   565
                let room = &mut server.rooms[room_id];
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   566
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   567
                client.set_is_in_game(false);
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   568
                response.add(
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   569
                    ClientFlags("-g".to_string(), vec![client.nick.clone()])
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   570
                        .send_all()
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   571
                        .in_room(room.id),
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   572
                );
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   573
                let team_names: Vec<_> = room
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   574
                    .client_teams(client_id)
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   575
                    .map(|t| t.name.clone())
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   576
                    .collect();
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   577
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   578
                if let Some(ref mut info) = room.game_info {
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   579
                    info.teams_in_game -= team_names.len() as u8;
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   580
                    if info.teams_in_game == 0 {
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   581
                        game_ended = true;
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   582
                    }
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   583
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   584
                    for team_name in team_names {
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   585
                        let msg = once(b'F').chain(team_name.bytes());
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   586
                        response.add(
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   587
                            ForwardEngineMessage(vec![to_engine_msg(msg)])
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   588
                                .send_all()
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   589
                                .in_room(room_id)
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   590
                                .but_self(),
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   591
                        );
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   592
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   593
                        let remove_msg = to_engine_msg(once(b'F').chain(team_name.bytes()));
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   594
                        if let Some(m) = &info.sync_msg {
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   595
                            info.msg_log.push(m.clone());
13426
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   596
                        }
14689
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   597
                        if info.sync_msg.is_some() {
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   598
                            info.sync_msg = None
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   599
                        }
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   600
                        info.msg_log.push(remove_msg.clone());
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   601
                        response.add(
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   602
                            ForwardEngineMessage(vec![remove_msg])
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   603
                                .send_all()
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   604
                                .in_room(room_id)
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   605
                                .but_self(),
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   606
                        );
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   607
                    }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   608
                }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   609
            }
14689
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   610
            if game_ended {
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   611
                super::common::end_game(server, room_id, response)
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   612
            }
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   613
        }
13445
d3c86ade3d4d Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13444
diff changeset
   614
        Rnd(v) => {
13521
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13520
diff changeset
   615
            let result = rnd_reply(&v);
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13520
diff changeset
   616
            let mut echo = vec!["/rnd".to_string()];
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13520
diff changeset
   617
            echo.extend(v.into_iter());
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13520
diff changeset
   618
            let chat_msg = ChatMsg {
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13520
diff changeset
   619
                nick: server.clients[client_id].nick.clone(),
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   620
                msg: echo.join(" "),
13521
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13520
diff changeset
   621
            };
14676
9377ee00f1f1 Server action refactoring part 7 of N
alfadur <mail@none>
parents: 14675
diff changeset
   622
            response.add(chat_msg.send_all().in_room(room_id));
9377ee00f1f1 Server action refactoring part 7 of N
alfadur <mail@none>
parents: 14675
diff changeset
   623
            response.add(result.send_all().in_room(room_id));
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   624
        }
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   625
        _ => warn!("Unimplemented!"),
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
   626
    }
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
   627
}