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