rust/hedgewars-server/src/handlers/inroom.rs
author alfadur <mail@none>
Sat, 11 Jan 2020 00:44:25 +0300
changeset 15540 479911540e17
parent 15536 a798e6441a36
child 15541 d122b65bdf6f
permissions -rw-r--r--
backout a798e6441a36
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::*};
15519
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
     2
use crate::core::room::GameInfo;
15523
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
     3
use crate::core::server::{AddTeamError, SetTeamCountError};
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
     4
use crate::{
15074
c5a6e8566425 shuffle server files
alfadur
parents: 15026
diff changeset
     5
    core::{
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
     6
        room::{HwRoom, RoomFlags, MAX_TEAMS_IN_ROOM},
15492
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
     7
        server::{
15525
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
     8
            ChangeMasterError, ChangeMasterResult, HwRoomControl, HwServer, LeaveRoomResult,
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
     9
            ModifyTeamError, StartGameError,
15492
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
    10
        },
15074
c5a6e8566425 shuffle server files
alfadur
parents: 15026
diff changeset
    11
        types,
c5a6e8566425 shuffle server files
alfadur
parents: 15026
diff changeset
    12
        types::{ClientId, GameCfg, RoomId, VoteType, Voting, MAX_HEDGEHOGS_PER_TEAM},
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
    13
    },
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
    14
    protocol::messages::{
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
    15
        add_flags, remove_flags, server_chat, HwProtocolMessage, HwServerMessage::*,
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
    16
        ProtocolFlags as Flags,
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
    17
    },
15482
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15439
diff changeset
    18
    utils::{is_name_illegal, to_engine_msg},
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    19
};
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    20
use base64::{decode, encode};
13805
0463a4221327 cleanup crate imports
alfadur
parents: 13801
diff changeset
    21
use log::*;
15026
a479916799ea use contains where possible
alfadur
parents: 14790
diff changeset
    22
use std::{cmp::min, iter::once, mem::swap};
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    23
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    24
#[derive(Clone)]
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    25
struct ByMsg<'a> {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    26
    messages: &'a [u8],
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    27
}
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    28
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    29
impl<'a> Iterator for ByMsg<'a> {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    30
    type Item = &'a [u8];
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    31
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    32
    fn next(&mut self) -> Option<<Self as Iterator>::Item> {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    33
        if let Some(size) = self.messages.get(0) {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    34
            let (msg, next) = self.messages.split_at(*size as usize + 1);
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    35
            self.messages = next;
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    36
            Some(msg)
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    37
        } else {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    38
            None
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    39
        }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    40
    }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    41
}
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    42
13524
5359ff75da3a indulge clippy
alfadur
parents: 13523
diff changeset
    43
fn by_msg(source: &[u8]) -> ByMsg {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    44
    ByMsg { messages: source }
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    45
}
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    46
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    47
const VALID_MESSAGES: &[u8] =
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    48
    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
    49
const NON_TIMED_MESSAGES: &[u8] = b"M#hb";
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    50
15528
17ad5d43e820 update subslice pattern to the new syntax
alfadur <mail@none>
parents: 15527
diff changeset
    51
#[cfg(canhazslicepatterns)]
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    52
fn is_msg_valid(msg: &[u8], team_indices: &[u8]) -> bool {
13424
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
    53
    match msg {
15530
a859f08ebb4f update tls configuration
alfadur <mail@none>
parents: 15528
diff changeset
    54
        [size, typ, body @ ..] => {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    55
            VALID_MESSAGES.contains(typ)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    56
                && match body {
15439
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15326
diff changeset
    57
                    [1..=MAX_HEDGEHOGS_PER_TEAM, team, ..] if *typ == b'h' => {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    58
                        team_indices.contains(team)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    59
                    }
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    60
                    _ => *typ != b'h',
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    61
                }
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    62
        }
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    63
        _ => false,
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    64
    }
15528
17ad5d43e820 update subslice pattern to the new syntax
alfadur <mail@none>
parents: 15527
diff changeset
    65
}
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    66
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
    67
fn is_msg_valid(msg: &[u8], _team_indices: &[u8]) -> bool {
13429
4c5ed27b1ff8 stabilization
alfadur
parents: 13428
diff changeset
    68
    if let Some(typ) = msg.get(1) {
4c5ed27b1ff8 stabilization
alfadur
parents: 13428
diff changeset
    69
        VALID_MESSAGES.contains(typ)
4c5ed27b1ff8 stabilization
alfadur
parents: 13428
diff changeset
    70
    } else {
4c5ed27b1ff8 stabilization
alfadur
parents: 13428
diff changeset
    71
        false
4c5ed27b1ff8 stabilization
alfadur
parents: 13428
diff changeset
    72
    }
4c5ed27b1ff8 stabilization
alfadur
parents: 13428
diff changeset
    73
}
4c5ed27b1ff8 stabilization
alfadur
parents: 13428
diff changeset
    74
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    75
fn is_msg_empty(msg: &[u8]) -> bool {
13429
4c5ed27b1ff8 stabilization
alfadur
parents: 13428
diff changeset
    76
    msg.get(1).filter(|t| **t == b'+').is_some()
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    77
}
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    78
13443
2501428303a2 Fix team remove synchronization
alfadur
parents: 13439
diff changeset
    79
fn is_msg_timed(msg: &[u8]) -> bool {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    80
    msg.get(1)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    81
        .filter(|t| !NON_TIMED_MESSAGES.contains(t))
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    82
        .is_some()
13443
2501428303a2 Fix team remove synchronization
alfadur
parents: 13439
diff changeset
    83
}
2501428303a2 Fix team remove synchronization
alfadur
parents: 13439
diff changeset
    84
13480
fb37745c5bca complete newseed voting
alfadur
parents: 13478
diff changeset
    85
fn voting_description(kind: &VoteType) -> String {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    86
    format!(
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    87
        "New voting started: {}",
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    88
        match kind {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    89
            VoteType::Kick(nick) => format!("kick {}", nick),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    90
            VoteType::Map(name) => format!("map {}", name.as_ref().unwrap()),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    91
            VoteType::Pause => "pause".to_string(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    92
            VoteType::NewSeed => "new seed".to_string(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    93
            VoteType::HedgehogsPerTeam(number) => format!("hedgehogs per team: {}", number),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    94
        }
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    95
    )
13480
fb37745c5bca complete newseed voting
alfadur
parents: 13478
diff changeset
    96
}
fb37745c5bca complete newseed voting
alfadur
parents: 13478
diff changeset
    97
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
    98
fn room_message_flag(msg: &HwProtocolMessage) -> RoomFlags {
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
    99
    use crate::protocol::messages::HwProtocolMessage::*;
13523
8c5dd562c9f7 Add room flags
alfadur
parents: 13521
diff changeset
   100
    match msg {
8c5dd562c9f7 Add room flags
alfadur
parents: 13521
diff changeset
   101
        ToggleRestrictJoin => RoomFlags::RESTRICTED_JOIN,
8c5dd562c9f7 Add room flags
alfadur
parents: 13521
diff changeset
   102
        ToggleRestrictTeams => RoomFlags::RESTRICTED_TEAM_ADD,
15534
bb93e9642b5b send cleared flags on room join
alfadur <mail@none>
parents: 15531
diff changeset
   103
        ToggleRegisteredOnly => RoomFlags::REGISTRATION_REQUIRED,
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   104
        _ => RoomFlags::empty(),
13523
8c5dd562c9f7 Add room flags
alfadur
parents: 13521
diff changeset
   105
    }
8c5dd562c9f7 Add room flags
alfadur
parents: 13521
diff changeset
   106
}
8c5dd562c9f7 Add room flags
alfadur
parents: 13521
diff changeset
   107
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   108
pub fn handle(
15523
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   109
    mut room_control: HwRoomControl,
14671
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
   110
    response: &mut super::Response,
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
) {
15523
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   113
    let (client, room) = room_control.get();
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   114
    let (client_id, room_id) = (client.id, room.id);
14697
f64e21f164a5 fix some warnings
alfadur
parents: 14694
diff changeset
   115
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   116
    use crate::protocol::messages::HwProtocolMessage::*;
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
   117
    match message {
14675
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14671
diff changeset
   118
        Part(msg) => {
14697
f64e21f164a5 fix some warnings
alfadur
parents: 14694
diff changeset
   119
            let msg = match msg {
f64e21f164a5 fix some warnings
alfadur
parents: 14694
diff changeset
   120
                Some(s) => format!("part: {}", s),
f64e21f164a5 fix some warnings
alfadur
parents: 14694
diff changeset
   121
                None => "part".to_string(),
f64e21f164a5 fix some warnings
alfadur
parents: 14694
diff changeset
   122
            };
15482
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15439
diff changeset
   123
15523
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   124
            let result = room_control.leave_room();
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   125
            super::common::get_room_leave_result(
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   126
                room_control.server(),
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   127
                room_control.room(),
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   128
                &msg,
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   129
                result,
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   130
                response,
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   131
            );
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   132
        }
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   133
        Chat(msg) => {
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   134
            response.add(
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   135
                ChatMsg {
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   136
                    nick: client.nick.clone(),
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   137
                    msg,
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   138
                }
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   139
                .send_all()
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   140
                .in_room(room_id),
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   141
            );
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   142
        }
14788
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14785
diff changeset
   143
        TeamChat(msg) => {
15531
ede5f4ec48f3 fix official server
alfadur <mail@none>
parents: 15530
diff changeset
   144
            if room.game_info.is_some() {
14788
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14785
diff changeset
   145
                if let Some(clan_color) = room.find_team_color(client_id) {
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14785
diff changeset
   146
                    let engine_msg =
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14785
diff changeset
   147
                        to_engine_msg(format!("b{}]{}\x20\x20", client.nick, msg).bytes());
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14785
diff changeset
   148
                    let team = room.clan_team_owners(clan_color).collect();
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14785
diff changeset
   149
                    response.add(ForwardEngineMessage(vec![engine_msg]).send_many(team))
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14785
diff changeset
   150
                }
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14785
diff changeset
   151
            }
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14785
diff changeset
   152
        }
13477
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13445
diff changeset
   153
        Fix => {
15523
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   154
            if let Err(_) = room_control.fix_room() {
15492
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   155
                response.warn(ACCESS_DENIED)
13477
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13445
diff changeset
   156
            }
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13445
diff changeset
   157
        }
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13445
diff changeset
   158
        Unfix => {
15523
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   159
            if let Err(_) = room_control.unfix_room() {
15492
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) => {
15523
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   164
            if let Err(_) = room_control.set_room_greeting(text) {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   165
                response.warn(ACCESS_DENIED)
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) => {
15523
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   169
            use crate::core::server::SetTeamCountError;
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   170
            match room_control.set_room_max_teams(count) {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   171
                Ok(()) => {}
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   172
                Err(SetTeamCountError::NotMaster) => response.warn(NOT_MASTER),
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   173
                Err(SetTeamCountError::InvalidNumber) => {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   174
                    response.warn("/maxteams: specify number from 2 to 8")
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   175
                }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   176
            };
14788
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14785
diff changeset
   177
        }
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   178
        RoomName(new_name) => {
15518
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15516
diff changeset
   179
            use crate::core::server::ModifyRoomNameError;
15523
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   180
            match room_control.set_room_name(new_name) {
15518
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15516
diff changeset
   181
                Ok(old_name) => {
15523
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   182
                    let (client, room) = room_control.get();
15518
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15516
diff changeset
   183
                    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
   184
                }
15518
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15516
diff changeset
   185
                Err(ModifyRoomNameError::AccessDenied) => response.warn(ACCESS_DENIED),
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15516
diff changeset
   186
                Err(ModifyRoomNameError::InvalidName) => response.warn(ILLEGAL_ROOM_NAME),
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15516
diff changeset
   187
                Err(ModifyRoomNameError::DuplicateName) => response.warn(ROOM_EXISTS),
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   188
            }
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   189
        }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   190
        ToggleReady => {
15523
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   191
            let flags = if room_control.toggle_ready() {
15518
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15516
diff changeset
   192
                add_flags(&[Flags::Ready])
14697
f64e21f164a5 fix some warnings
alfadur
parents: 14694
diff changeset
   193
            } else {
15518
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15516
diff changeset
   194
                remove_flags(&[Flags::Ready])
14697
f64e21f164a5 fix some warnings
alfadur
parents: 14694
diff changeset
   195
            };
15523
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   196
            let (client, room) = room_control.get();
13801
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13795
diff changeset
   197
14697
f64e21f164a5 fix some warnings
alfadur
parents: 14694
diff changeset
   198
            let msg = if client.protocol_number < 38 {
f64e21f164a5 fix some warnings
alfadur
parents: 14694
diff changeset
   199
                LegacyReady(client.is_ready(), vec![client.nick.clone()])
f64e21f164a5 fix some warnings
alfadur
parents: 14694
diff changeset
   200
            } else {
14782
50fcef24003f add typed protocol flags
alfadur
parents: 14781
diff changeset
   201
                ClientFlags(flags, vec![client.nick.clone()])
14697
f64e21f164a5 fix some warnings
alfadur
parents: 14694
diff changeset
   202
            };
15518
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15516
diff changeset
   203
            response.add(msg.send_all().in_room(room_id));
14691
2071da901c63 reconnect some message handlers
alfadur <mail@none>
parents: 14690
diff changeset
   204
14697
f64e21f164a5 fix some warnings
alfadur
parents: 14694
diff changeset
   205
            if room.is_fixed() && room.ready_players_number == room.players_number {
15523
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   206
                let result = room_control.start_game();
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   207
                super::common::get_start_game_data(
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   208
                    room_control.server(),
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   209
                    room_id,
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   210
                    result,
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   211
                    response,
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   212
                );
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
   213
            }
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   214
        }
15519
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   215
        AddTeam(info) => {
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   216
            use crate::core::server::AddTeamError;
15523
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   217
            match room_control.add_team(info) {
15519
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   218
                Ok(team) => {
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   219
                    response.add(TeamAccepted(team.name.clone()).send_self());
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   220
                    response.add(
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   221
                        TeamAdd(team.to_protocol())
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   222
                            .send_all()
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   223
                            .in_room(room_id)
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   224
                            .but_self(),
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   225
                    );
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   226
                    response.add(
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   227
                        TeamColor(team.name.clone(), team.color)
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   228
                            .send_all()
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   229
                            .in_room(room_id),
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   230
                    );
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   231
                    response.add(
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   232
                        HedgehogsNumber(team.name.clone(), team.hedgehogs_number)
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   233
                            .send_all()
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   234
                            .in_room(room_id),
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   235
                    );
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   236
15523
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   237
                    let room = room_control.room();
15525
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   238
                    let room_master = room.master_id.map(|id| room_control.server().client(id));
15519
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   239
                    super::common::get_room_update(None, room, room_master, response);
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   240
                }
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   241
                Err(AddTeamError::TooManyTeams) => response.warn(TOO_MANY_TEAMS),
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   242
                Err(AddTeamError::TooManyHedgehogs) => response.warn(TOO_MANY_HEDGEHOGS),
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   243
                Err(AddTeamError::TeamAlreadyExists) => response.warn(TEAM_EXISTS),
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   244
                Err(AddTeamError::GameInProgress) => response.warn(ROUND_IN_PROGRESS),
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   245
                Err(AddTeamError::Restricted) => response.warn(TEAM_ADD_RESTRICTED),
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   246
            }
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   247
        }
15519
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   248
        RemoveTeam(name) => {
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   249
            use crate::core::server::RemoveTeamError;
15523
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   250
            match room_control.remove_team(&name) {
15519
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   251
                Ok(()) => {
15523
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   252
                    let (client, room) = room_control.get();
15519
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   253
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   254
                    let removed_teams = vec![name];
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   255
                    super::common::get_remove_teams_data(
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   256
                        room_id,
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   257
                        client.is_in_game(),
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   258
                        removed_teams,
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   259
                        response,
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   260
                    );
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   261
15540
479911540e17 backout a798e6441a36
alfadur <mail@none>
parents: 15536
diff changeset
   262
                    match room.game_info {
479911540e17 backout a798e6441a36
alfadur <mail@none>
parents: 15536
diff changeset
   263
                        Some(ref info) if info.teams_in_game == 0 => {
479911540e17 backout a798e6441a36
alfadur <mail@none>
parents: 15536
diff changeset
   264
                            if let Some(result) = room_control.end_game() {
479911540e17 backout a798e6441a36
alfadur <mail@none>
parents: 15536
diff changeset
   265
                                super::common::get_end_game_result(
479911540e17 backout a798e6441a36
alfadur <mail@none>
parents: 15536
diff changeset
   266
                                    room_control.server(),
479911540e17 backout a798e6441a36
alfadur <mail@none>
parents: 15536
diff changeset
   267
                                    room_id,
479911540e17 backout a798e6441a36
alfadur <mail@none>
parents: 15536
diff changeset
   268
                                    result,
479911540e17 backout a798e6441a36
alfadur <mail@none>
parents: 15536
diff changeset
   269
                                    response,
479911540e17 backout a798e6441a36
alfadur <mail@none>
parents: 15536
diff changeset
   270
                                );
479911540e17 backout a798e6441a36
alfadur <mail@none>
parents: 15536
diff changeset
   271
                            }
15519
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   272
                        }
15540
479911540e17 backout a798e6441a36
alfadur <mail@none>
parents: 15536
diff changeset
   273
                        _ => (),
15519
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   274
                    }
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   275
                }
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   276
                Err(RemoveTeamError::NoTeam) => response.warn(NO_TEAM_TO_REMOVE),
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   277
                Err(RemoveTeamError::TeamNotOwned) => response.warn(TEAM_NOT_OWNED),
15482
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15439
diff changeset
   278
            }
15519
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   279
        }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   280
        SetHedgehogsNumber(team_name, number) => {
15523
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   281
            use crate::core::server::SetHedgehogsError;
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   282
            match room_control.set_team_hedgehogs_number(&team_name, number) {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   283
                Ok(()) => {
14697
f64e21f164a5 fix some warnings
alfadur
parents: 14694
diff changeset
   284
                    response.add(
15523
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   285
                        HedgehogsNumber(team_name.clone(), number)
14697
f64e21f164a5 fix some warnings
alfadur
parents: 14694
diff changeset
   286
                            .send_all()
f64e21f164a5 fix some warnings
alfadur
parents: 14694
diff changeset
   287
                            .in_room(room_id)
f64e21f164a5 fix some warnings
alfadur
parents: 14694
diff changeset
   288
                            .but_self(),
f64e21f164a5 fix some warnings
alfadur
parents: 14694
diff changeset
   289
                    );
14676
9377ee00f1f1 Server action refactoring part 7 of N
alfadur <mail@none>
parents: 14675
diff changeset
   290
                }
15523
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   291
                Err(SetHedgehogsError::NotMaster) => response.error(NOT_MASTER),
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   292
                Err(SetHedgehogsError::NoTeam) => response.warn(NO_TEAM),
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   293
                Err(SetHedgehogsError::InvalidNumber(previous_number)) => {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   294
                    response.add(HedgehogsNumber(team_name.clone(), previous_number).send_self())
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   295
                }
14676
9377ee00f1f1 Server action refactoring part 7 of N
alfadur <mail@none>
parents: 14675
diff changeset
   296
            }
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   297
        }
15523
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   298
        SetTeamColor(team_name, color) => match room_control.set_team_color(&team_name, color) {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   299
            Ok(()) => response.add(
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   300
                TeamColor(team_name, color)
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   301
                    .send_all()
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   302
                    .in_room(room_id)
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   303
                    .but_self(),
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   304
            ),
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   305
            Err(ModifyTeamError::NoTeam) => response.warn(NO_TEAM),
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   306
            Err(ModifyTeamError::NotMaster) => response.error(NOT_MASTER),
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   307
        },
13422
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   308
        Cfg(cfg) => {
15523
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   309
            use crate::core::server::SetConfigError;
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   310
            let msg = cfg.to_server_msg();
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   311
            match room_control.set_config(cfg) {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   312
                Ok(()) => {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   313
                    response.add(msg.send_all().in_room(room_control.room().id).but_self());
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   314
                }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   315
                Err(SetConfigError::NotMaster) => response.error(NOT_MASTER),
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   316
                Err(SetConfigError::RoomFixed) => response.warn(ACCESS_DENIED),
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
   317
            }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   318
        }
13528
c8b626b0a3ad Implement save/delete commands
alfadur
parents: 13527
diff changeset
   319
        Save(name, location) => {
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   320
            response.add(
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   321
                server_chat(format!("Room config saved as {}", name))
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   322
                    .send_all()
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   323
                    .in_room(room_id),
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   324
            );
15523
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   325
            room_control.save_config(name, location);
13528
c8b626b0a3ad Implement save/delete commands
alfadur
parents: 13527
diff changeset
   326
        }
14785
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   327
        #[cfg(feature = "official-server")]
13529
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   328
        SaveRoom(filename) => {
14697
f64e21f164a5 fix some warnings
alfadur
parents: 14694
diff changeset
   329
            if client.is_admin() {
f64e21f164a5 fix some warnings
alfadur
parents: 14694
diff changeset
   330
                match room.get_saves() {
14781
01f8ab45f806 fix lobby joining
alfadur
parents: 14780
diff changeset
   331
                    Ok(contents) => response.request_io(super::IoTask::SaveRoom {
01f8ab45f806 fix lobby joining
alfadur
parents: 14780
diff changeset
   332
                        room_id,
01f8ab45f806 fix lobby joining
alfadur
parents: 14780
diff changeset
   333
                        filename,
01f8ab45f806 fix lobby joining
alfadur
parents: 14780
diff changeset
   334
                        contents,
01f8ab45f806 fix lobby joining
alfadur
parents: 14780
diff changeset
   335
                    }),
13529
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   336
                    Err(e) => {
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   337
                        warn!("Error while serializing the room configs: {}", e);
15482
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15439
diff changeset
   338
                        response.warn("Unable to serialize the room configs.")
13529
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   339
                    }
14676
9377ee00f1f1 Server action refactoring part 7 of N
alfadur <mail@none>
parents: 14675
diff changeset
   340
                }
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
   341
            }
13529
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   342
        }
14785
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   343
        #[cfg(feature = "official-server")]
13529
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   344
        LoadRoom(filename) => {
14697
f64e21f164a5 fix some warnings
alfadur
parents: 14694
diff changeset
   345
            if client.is_admin() {
14781
01f8ab45f806 fix lobby joining
alfadur
parents: 14780
diff changeset
   346
                response.request_io(super::IoTask::LoadRoom { room_id, filename });
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
   347
            }
13529
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   348
        }
13528
c8b626b0a3ad Implement save/delete commands
alfadur
parents: 13527
diff changeset
   349
        Delete(name) => {
15523
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   350
            if !room_control.delete_config(&name) {
14676
9377ee00f1f1 Server action refactoring part 7 of N
alfadur <mail@none>
parents: 14675
diff changeset
   351
                response.add(Warning(format!("Save doesn't exist: {}", name)).send_self());
13528
c8b626b0a3ad Implement save/delete commands
alfadur
parents: 13527
diff changeset
   352
            } else {
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   353
                response.add(
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   354
                    server_chat(format!("Room config {} has been deleted", name))
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   355
                        .send_all()
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   356
                        .in_room(room_id),
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   357
                );
14676
9377ee00f1f1 Server action refactoring part 7 of N
alfadur <mail@none>
parents: 14675
diff changeset
   358
            }
13528
c8b626b0a3ad Implement save/delete commands
alfadur
parents: 13527
diff changeset
   359
        }
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   360
        CallVote(None) => {
14676
9377ee00f1f1 Server action refactoring part 7 of N
alfadur <mail@none>
parents: 14675
diff changeset
   361
            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
   362
                .send_self());
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   363
        }
15525
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   364
        CallVote(Some(kind)) => {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   365
            use crate::core::server::StartVoteError;
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   366
            let room_id = room_control.room().id;
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   367
            if super::common::check_vote(
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   368
                room_control.server(),
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   369
                room_control.room(),
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   370
                &kind,
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   371
                response,
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   372
            ) {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   373
                match room_control.start_vote(kind.clone()) {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   374
                    Ok(()) => {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   375
                        let msg = voting_description(&kind);
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   376
                        response.add(server_chat(msg).send_all().in_room(room_id));
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   377
                        let vote_result = room_control.vote(types::Vote {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   378
                            is_pro: true,
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   379
                            is_forced: false,
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   380
                        });
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   381
                        super::common::handle_vote(room_control, vote_result, response);
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   382
                    }
15525
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   383
                    Err(StartVoteError::VotingInProgress) => {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   384
                        response.add(
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   385
                            server_chat("There is already voting in progress".to_string())
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   386
                                .send_self(),
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   387
                        );
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   388
                    }
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   389
                }
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   390
            }
15525
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   391
        }
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   392
        Vote(vote) => {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   393
            let vote_result = room_control.vote(types::Vote {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   394
                is_pro: vote,
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   395
                is_forced: false,
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   396
            });
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   397
            super::common::handle_vote(room_control, vote_result, response);
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   398
        }
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   399
        ForceVote(vote) => {
14697
f64e21f164a5 fix some warnings
alfadur
parents: 14694
diff changeset
   400
            let is_forced = client.is_admin();
15525
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   401
            let vote_result = room_control.vote(types::Vote {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   402
                is_pro: vote,
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   403
                is_forced,
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   404
            });
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   405
            super::common::handle_vote(room_control, vote_result, response);
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15523
diff changeset
   406
        }
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   407
        ToggleRestrictJoin | ToggleRestrictTeams | ToggleRegisteredOnly => {
15523
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   408
            if room_control.toggle_flag(room_message_flag(&message)) {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   409
                let (client, room) = room_control.get();
14686
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   410
                super::common::get_room_update(None, room, Some(&client), response);
13523
8c5dd562c9f7 Add room flags
alfadur
parents: 13521
diff changeset
   411
            }
8c5dd562c9f7 Add room flags
alfadur
parents: 13521
diff changeset
   412
        }
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   413
        StartGame => {
15523
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   414
            let result = room_control.start_game();
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   415
            super::common::get_start_game_data(room_control.server(), room_id, result, response);
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   416
        }
15527
428a0e7da27b restore engine message handler
alfadur <mail@none>
parents: 15525
diff changeset
   417
        EngineMessage(em) => {
14697
f64e21f164a5 fix some warnings
alfadur
parents: 14694
diff changeset
   418
            if client.teams_in_game > 0 {
f64e21f164a5 fix some warnings
alfadur
parents: 14694
diff changeset
   419
                let decoding = decode(&em[..]).unwrap();
f64e21f164a5 fix some warnings
alfadur
parents: 14694
diff changeset
   420
                let messages = by_msg(&decoding);
f64e21f164a5 fix some warnings
alfadur
parents: 14694
diff changeset
   421
                let valid = messages.filter(|m| is_msg_valid(m, &client.team_indices));
f64e21f164a5 fix some warnings
alfadur
parents: 14694
diff changeset
   422
                let non_empty = valid.clone().filter(|m| !is_msg_empty(m));
f64e21f164a5 fix some warnings
alfadur
parents: 14694
diff changeset
   423
                let sync_msg = valid.clone().filter(|m| is_msg_timed(m)).last().map(|m| {
f64e21f164a5 fix some warnings
alfadur
parents: 14694
diff changeset
   424
                    if is_msg_empty(m) {
f64e21f164a5 fix some warnings
alfadur
parents: 14694
diff changeset
   425
                        Some(encode(m))
f64e21f164a5 fix some warnings
alfadur
parents: 14694
diff changeset
   426
                    } else {
f64e21f164a5 fix some warnings
alfadur
parents: 14694
diff changeset
   427
                        None
f64e21f164a5 fix some warnings
alfadur
parents: 14694
diff changeset
   428
                    }
f64e21f164a5 fix some warnings
alfadur
parents: 14694
diff changeset
   429
                });
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   430
14697
f64e21f164a5 fix some warnings
alfadur
parents: 14694
diff changeset
   431
                let em_response = encode(&valid.flat_map(|msg| msg).cloned().collect::<Vec<_>>());
f64e21f164a5 fix some warnings
alfadur
parents: 14694
diff changeset
   432
                if !em_response.is_empty() {
f64e21f164a5 fix some warnings
alfadur
parents: 14694
diff changeset
   433
                    response.add(
f64e21f164a5 fix some warnings
alfadur
parents: 14694
diff changeset
   434
                        ForwardEngineMessage(vec![em_response])
f64e21f164a5 fix some warnings
alfadur
parents: 14694
diff changeset
   435
                            .send_all()
f64e21f164a5 fix some warnings
alfadur
parents: 14694
diff changeset
   436
                            .in_room(room.id)
f64e21f164a5 fix some warnings
alfadur
parents: 14694
diff changeset
   437
                            .but_self(),
f64e21f164a5 fix some warnings
alfadur
parents: 14694
diff changeset
   438
                    );
f64e21f164a5 fix some warnings
alfadur
parents: 14694
diff changeset
   439
                }
f64e21f164a5 fix some warnings
alfadur
parents: 14694
diff changeset
   440
                let em_log = encode(&non_empty.flat_map(|msg| msg).cloned().collect::<Vec<_>>());
15527
428a0e7da27b restore engine message handler
alfadur <mail@none>
parents: 15525
diff changeset
   441
428a0e7da27b restore engine message handler
alfadur <mail@none>
parents: 15525
diff changeset
   442
                room_control.log_engine_msg(em_log, sync_msg);
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   443
            }
15527
428a0e7da27b restore engine message handler
alfadur <mail@none>
parents: 15525
diff changeset
   444
        }
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   445
        RoundFinished => {
15523
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   446
            if let Some(team_names) = room_control.leave_game() {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   447
                let (client, room) = room_control.get();
14689
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   448
                response.add(
14782
50fcef24003f add typed protocol flags
alfadur
parents: 14781
diff changeset
   449
                    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
   450
                        .send_all()
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   451
                        .in_room(room.id),
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   452
                );
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   453
15519
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   454
                for team_name in team_names {
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   455
                    let msg = once(b'F').chain(team_name.bytes());
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   456
                    response.add(
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   457
                        ForwardEngineMessage(vec![to_engine_msg(msg)])
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   458
                            .send_all()
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   459
                            .in_room(room_id)
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   460
                            .but_self(),
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   461
                    );
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   462
                }
15519
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   463
15540
479911540e17 backout a798e6441a36
alfadur <mail@none>
parents: 15536
diff changeset
   464
                if let Some(GameInfo {
479911540e17 backout a798e6441a36
alfadur <mail@none>
parents: 15536
diff changeset
   465
                    teams_in_game: 0, ..
479911540e17 backout a798e6441a36
alfadur <mail@none>
parents: 15536
diff changeset
   466
                }) = room.game_info
479911540e17 backout a798e6441a36
alfadur <mail@none>
parents: 15536
diff changeset
   467
                {
15523
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   468
                    if let Some(result) = room_control.end_game() {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   469
                        super::common::get_end_game_result(
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   470
                            room_control.server(),
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   471
                            room_id,
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   472
                            result,
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   473
                            response,
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   474
                        );
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   475
                    }
15519
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15518
diff changeset
   476
                }
14689
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   477
            }
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   478
        }
13445
d3c86ade3d4d Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13444
diff changeset
   479
        Rnd(v) => {
13521
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13520
diff changeset
   480
            let result = rnd_reply(&v);
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13520
diff changeset
   481
            let mut echo = vec!["/rnd".to_string()];
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13520
diff changeset
   482
            echo.extend(v.into_iter());
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13520
diff changeset
   483
            let chat_msg = ChatMsg {
15523
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   484
                nick: client.nick.clone(),
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   485
                msg: echo.join(" "),
13521
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13520
diff changeset
   486
            };
14676
9377ee00f1f1 Server action refactoring part 7 of N
alfadur <mail@none>
parents: 14675
diff changeset
   487
            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
   488
            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
   489
        }
15523
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   490
        Delegate(nick) => match room_control.change_master(nick) {
15487
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15482
diff changeset
   491
            Ok(ChangeMasterResult {
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15482
diff changeset
   492
                old_master_id,
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15482
diff changeset
   493
                new_master_id,
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15482
diff changeset
   494
            }) => {
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15482
diff changeset
   495
                if let Some(master_id) = old_master_id {
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15482
diff changeset
   496
                    response.add(
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15482
diff changeset
   497
                        ClientFlags(
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15482
diff changeset
   498
                            remove_flags(&[Flags::RoomMaster]),
15523
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   499
                            vec![room_control.server().client(master_id).nick.clone()],
15487
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15482
diff changeset
   500
                        )
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15482
diff changeset
   501
                        .send_all()
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15482
diff changeset
   502
                        .in_room(room_id),
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15482
diff changeset
   503
                    );
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15482
diff changeset
   504
                }
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15482
diff changeset
   505
                response.add(
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15482
diff changeset
   506
                    ClientFlags(
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15482
diff changeset
   507
                        add_flags(&[Flags::RoomMaster]),
15523
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15522
diff changeset
   508
                        vec![room_control.server().client(new_master_id).nick.clone()],
15487
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15482
diff changeset
   509
                    )
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15482
diff changeset
   510
                    .send_all()
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15482
diff changeset
   511
                    .in_room(room_id),
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15482
diff changeset
   512
                );
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15482
diff changeset
   513
            }
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15482
diff changeset
   514
            Err(ChangeMasterError::NoAccess) => {
15482
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15439
diff changeset
   515
                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
   516
            }
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15482
diff changeset
   517
            Err(ChangeMasterError::AlreadyMaster) => {
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15482
diff changeset
   518
                response.warn("You're already the room master.")
14784
8390d5e4e39c implememg global&delegate messages
alfadur
parents: 14782
diff changeset
   519
            }
15487
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15482
diff changeset
   520
            Err(ChangeMasterError::NoClient) => response.warn("Player is not online."),
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15482
diff changeset
   521
            Err(ChangeMasterError::ClientNotInRoom) => {
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15482
diff changeset
   522
                response.warn("The player is not in your room.")
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15482
diff changeset
   523
            }
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15482
diff changeset
   524
        },
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14676
diff changeset
   525
        _ => warn!("Unimplemented!"),
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
   526
    }
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
   527
}