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