gameServer2/src/server/handlers/inroom.rs
author alfadur
Thu, 04 Oct 2018 13:26:48 -0400
changeset 13859 ff468792298a
parent 13810 0463a4221327
child 14395 e5db279308d7
permissions -rw-r--r--
merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
     1
use mio;
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
     2
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
     3
use crate::{
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
     4
    server::{
13709
e335daaa77a9 Add hogs per team named constant that absolutely no one asked for
alfadur
parents: 13666
diff changeset
     5
        coretypes::{
13775
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13709
diff changeset
     6
            ClientId, RoomId, Voting, VoteType, GameCfg,
13709
e335daaa77a9 Add hogs per team named constant that absolutely no one asked for
alfadur
parents: 13666
diff changeset
     7
            MAX_HEDGEHOGS_PER_TEAM
e335daaa77a9 Add hogs per team named constant that absolutely no one asked for
alfadur
parents: 13666
diff changeset
     8
        },
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
     9
        server::HWServer,
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
    10
        room::{HWRoom, RoomFlags},
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
    11
        actions::{Action, Action::*}
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
    12
    },
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
    13
    protocol::messages::{
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
    14
        HWProtocolMessage,
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
    15
        HWServerMessage::*,
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
    16
        server_chat
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
    17
    },
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
    18
    utils::is_name_illegal
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    19
};
13529
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
    20
use std::{
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
    21
    mem::swap, fs::{File, OpenOptions},
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
    22
    io::{Read, Write, Result, Error, ErrorKind}
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
    23
};
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    24
use base64::{encode, decode};
13492
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13486
diff changeset
    25
use super::common::rnd_reply;
13810
0463a4221327 cleanup crate imports
alfadur
parents: 13775
diff changeset
    26
use log::*;
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
#[derive(Clone)]
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    29
struct ByMsg<'a> {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    30
    messages: &'a[u8]
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    31
}
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    32
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    33
impl <'a> Iterator for ByMsg<'a> {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    34
    type Item = &'a[u8];
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    35
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    36
    fn next(&mut self) -> Option<<Self as Iterator>::Item> {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    37
        if let Some(size) = self.messages.get(0) {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    38
            let (msg, next) = self.messages.split_at(*size as usize + 1);
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    39
            self.messages = next;
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    40
            Some(msg)
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    41
        } else {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    42
            None
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
}
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    46
13500
5359ff75da3a indulge clippy
alfadur
parents: 13494
diff changeset
    47
fn by_msg(source: &[u8]) -> ByMsg {
5359ff75da3a indulge clippy
alfadur
parents: 13494
diff changeset
    48
    ByMsg {messages: source}
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    49
}
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    50
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    51
const VALID_MESSAGES: &[u8] =
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    52
    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
    53
const NON_TIMED_MESSAGES: &[u8] = b"M#hb";
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    54
13429
4c5ed27b1ff8 stabilization
alfadur
parents: 13428
diff changeset
    55
#[cfg(canhazslicepatterns)]
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    56
fn is_msg_valid(msg: &[u8], team_indices: &[u8]) -> bool {
13424
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
    57
    match msg {
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
    58
        [size, typ, body..] => VALID_MESSAGES.contains(typ)
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
    59
            && match body {
13709
e335daaa77a9 Add hogs per team named constant that absolutely no one asked for
alfadur
parents: 13666
diff changeset
    60
                [1...MAX_HEDGEHOGS_PER_TEAM, team, ..] if *typ == b'h' =>
e335daaa77a9 Add hogs per team named constant that absolutely no one asked for
alfadur
parents: 13666
diff changeset
    61
                    team_indices.contains(team),
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    62
                _ => *typ != b'h'
13424
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
    63
            },
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
    64
        _ => false
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    65
    }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    66
}
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    67
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
    68
fn is_msg_valid(msg: &[u8], _team_indices: &[u8]) -> bool {
13429
4c5ed27b1ff8 stabilization
alfadur
parents: 13428
diff changeset
    69
    if let Some(typ) = msg.get(1) {
4c5ed27b1ff8 stabilization
alfadur
parents: 13428
diff changeset
    70
        VALID_MESSAGES.contains(typ)
4c5ed27b1ff8 stabilization
alfadur
parents: 13428
diff changeset
    71
    } else {
4c5ed27b1ff8 stabilization
alfadur
parents: 13428
diff changeset
    72
        false
4c5ed27b1ff8 stabilization
alfadur
parents: 13428
diff changeset
    73
    }
4c5ed27b1ff8 stabilization
alfadur
parents: 13428
diff changeset
    74
}
4c5ed27b1ff8 stabilization
alfadur
parents: 13428
diff changeset
    75
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    76
fn is_msg_empty(msg: &[u8]) -> bool {
13429
4c5ed27b1ff8 stabilization
alfadur
parents: 13428
diff changeset
    77
    msg.get(1).filter(|t| **t == b'+').is_some()
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    78
}
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    79
13443
2501428303a2 Fix team remove synchronization
alfadur
parents: 13439
diff changeset
    80
fn is_msg_timed(msg: &[u8]) -> bool {
2501428303a2 Fix team remove synchronization
alfadur
parents: 13439
diff changeset
    81
    msg.get(1).filter(|t| !NON_TIMED_MESSAGES.contains(t)).is_some()
2501428303a2 Fix team remove synchronization
alfadur
parents: 13439
diff changeset
    82
}
2501428303a2 Fix team remove synchronization
alfadur
parents: 13439
diff changeset
    83
13460
fb37745c5bca complete newseed voting
alfadur
parents: 13450
diff changeset
    84
fn voting_description(kind: &VoteType) -> String {
fb37745c5bca complete newseed voting
alfadur
parents: 13450
diff changeset
    85
    format!("New voting started: {}", match kind {
fb37745c5bca complete newseed voting
alfadur
parents: 13450
diff changeset
    86
        VoteType::Kick(nick) => format!("kick {}", nick),
fb37745c5bca complete newseed voting
alfadur
parents: 13450
diff changeset
    87
        VoteType::Map(name) => format!("map {}", name.as_ref().unwrap()),
fb37745c5bca complete newseed voting
alfadur
parents: 13450
diff changeset
    88
        VoteType::Pause => "pause".to_string(),
fb37745c5bca complete newseed voting
alfadur
parents: 13450
diff changeset
    89
        VoteType::NewSeed => "new seed".to_string(),
fb37745c5bca complete newseed voting
alfadur
parents: 13450
diff changeset
    90
        VoteType::HedgehogsPerTeam(number) => format!("hedgehogs per team: {}", number)
fb37745c5bca complete newseed voting
alfadur
parents: 13450
diff changeset
    91
    })
fb37745c5bca complete newseed voting
alfadur
parents: 13450
diff changeset
    92
}
fb37745c5bca complete newseed voting
alfadur
parents: 13450
diff changeset
    93
13494
8c5dd562c9f7 Add room flags
alfadur
parents: 13492
diff changeset
    94
fn room_message_flag(msg: &HWProtocolMessage) -> RoomFlags {
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
    95
    use crate::protocol::messages::HWProtocolMessage::*;
13494
8c5dd562c9f7 Add room flags
alfadur
parents: 13492
diff changeset
    96
    match msg {
8c5dd562c9f7 Add room flags
alfadur
parents: 13492
diff changeset
    97
        ToggleRestrictJoin => RoomFlags::RESTRICTED_JOIN,
8c5dd562c9f7 Add room flags
alfadur
parents: 13492
diff changeset
    98
        ToggleRestrictTeams => RoomFlags::RESTRICTED_TEAM_ADD,
8c5dd562c9f7 Add room flags
alfadur
parents: 13492
diff changeset
    99
        ToggleRegisteredOnly => RoomFlags::RESTRICTED_UNREGISTERED_PLAYERS,
8c5dd562c9f7 Add room flags
alfadur
parents: 13492
diff changeset
   100
        _ => RoomFlags::empty()
8c5dd562c9f7 Add room flags
alfadur
parents: 13492
diff changeset
   101
    }
8c5dd562c9f7 Add room flags
alfadur
parents: 13492
diff changeset
   102
}
8c5dd562c9f7 Add room flags
alfadur
parents: 13492
diff changeset
   103
13529
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   104
fn read_file(filename: &str) -> Result<String> {
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   105
    let mut reader = File::open(filename)?;
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   106
    let mut result = String::new();
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   107
    reader.read_to_string(&mut result)?;
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   108
    Ok(result)
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   109
}
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   110
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   111
fn write_file(filename: &str, content: &str) -> Result<()> {
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   112
    let mut writer = OpenOptions::new().create(true).write(true).open(filename)?;
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   113
    writer.write_all(content.as_bytes())
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   114
}
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   115
13492
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13486
diff changeset
   116
pub fn handle(server: &mut HWServer, client_id: ClientId, room_id: RoomId, message: HWProtocolMessage) {
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
   117
    use crate::protocol::messages::HWProtocolMessage::*;
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
   118
    match message {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   119
        Part(None) => server.react(client_id, vec![
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   120
            MoveToLobby("part".to_string())]),
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   121
        Part(Some(msg)) => server.react(client_id, vec![
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   122
            MoveToLobby(format!("part: {}", msg))]),
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   123
        Chat(msg) => {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   124
            let actions = {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   125
                let c = &mut server.clients[client_id];
13500
5359ff75da3a indulge clippy
alfadur
parents: 13494
diff changeset
   126
                let chat_msg = ChatMsg {nick: c.nick.clone(), msg};
13492
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13486
diff changeset
   127
                vec![chat_msg.send_all().in_room(room_id).but_self().action()]
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   128
            };
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   129
            server.react(client_id, actions);
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   130
        },
13447
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13445
diff changeset
   131
        Fix => {
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13445
diff changeset
   132
            if let (c, Some(r)) = server.client_and_room(client_id) {
13494
8c5dd562c9f7 Add room flags
alfadur
parents: 13492
diff changeset
   133
                if c.is_admin() { r.set_is_fixed(true) }
13447
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13445
diff changeset
   134
            }
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13445
diff changeset
   135
        }
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13445
diff changeset
   136
        Unfix => {
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13445
diff changeset
   137
            if let (c, Some(r)) = server.client_and_room(client_id) {
13494
8c5dd562c9f7 Add room flags
alfadur
parents: 13492
diff changeset
   138
                if c.is_admin() { r.set_is_fixed(false) }
13447
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13445
diff changeset
   139
            }
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13445
diff changeset
   140
        }
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13445
diff changeset
   141
        Greeting(text) => {
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13445
diff changeset
   142
            if let (c, Some(r)) = server.client_and_room(client_id) {
13494
8c5dd562c9f7 Add room flags
alfadur
parents: 13492
diff changeset
   143
                if c.is_admin() || c.is_master() && !r.is_fixed() {
13447
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13445
diff changeset
   144
                    r.greeting = text
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13445
diff changeset
   145
                }
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13445
diff changeset
   146
            }
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13445
diff changeset
   147
        }
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   148
        RoomName(new_name) => {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   149
            let actions =
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   150
                if is_name_illegal(&new_name) {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   151
                    vec![Warn("Illegal room name! A room name must be between 1-40 characters long, must not have a trailing or leading space and must not have any of these characters: $()*+?[]^{|}".to_string())]
13520
3f69acc7e268 clean up room lookups
alfadur
parents: 13511
diff changeset
   152
                } else if server.rooms[room_id].is_fixed() {
13447
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13445
diff changeset
   153
                    vec![Warn("Access denied.".to_string())]
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   154
                } else if server.has_room(&new_name) {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   155
                    vec![Warn("A room with the same name already exists.".to_string())]
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   156
                } else {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   157
                    let mut old_name = new_name.clone();
13520
3f69acc7e268 clean up room lookups
alfadur
parents: 13511
diff changeset
   158
                    swap(&mut server.rooms[room_id].name, &mut old_name);
3f69acc7e268 clean up room lookups
alfadur
parents: 13511
diff changeset
   159
                    vec![SendRoomUpdate(Some(old_name))]
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   160
                };
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   161
            server.react(client_id, actions);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   162
        },
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   163
        ToggleReady => {
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
   164
            if let (c, Some(r)) = server.client_and_room(client_id) {
13486
1ee192f13456 Better packing for clients
alfadur
parents: 13460
diff changeset
   165
                let flags = if c.is_ready() {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   166
                    r.ready_players_number -= 1;
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   167
                    "-r"
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   168
                } else {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   169
                    r.ready_players_number += 1;
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   170
                    "+r"
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   171
                };
13775
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13709
diff changeset
   172
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13709
diff changeset
   173
                let msg = if c.protocol_number < 38 {
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13709
diff changeset
   174
                    LegacyReady(c.is_ready(), vec![c.nick.clone()])
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13709
diff changeset
   175
                } else {
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13709
diff changeset
   176
                    ClientFlags(flags.to_string(), vec![c.nick.clone()])
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13709
diff changeset
   177
                };
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13709
diff changeset
   178
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13709
diff changeset
   179
                let mut v = vec![msg.send_all().in_room(r.id).action()];
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13709
diff changeset
   180
13494
8c5dd562c9f7 Add room flags
alfadur
parents: 13492
diff changeset
   181
                if r.is_fixed() && r.ready_players_number == r.players_number {
13447
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13445
diff changeset
   182
                    v.push(StartRoomGame(r.id))
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13445
diff changeset
   183
                }
13775
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13709
diff changeset
   184
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13709
diff changeset
   185
                c.set_is_ready(!c.is_ready());
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
   186
                server.react(client_id, v);
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
   187
            }
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   188
        }
13422
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   189
        AddTeam(info) => {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   190
            let mut actions = Vec::new();
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   191
            if let (c, Some(r)) = server.client_and_room(client_id) {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   192
                if r.teams.len() >= r.team_limit as usize {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   193
                    actions.push(Warn("Too many teams!".to_string()))
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   194
                } else if r.addable_hedgehogs() == 0 {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   195
                    actions.push(Warn("Too many hedgehogs!".to_string()))
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   196
                } else if r.find_team(|t| t.name == info.name) != None {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   197
                    actions.push(Warn("There's already a team with same name in the list.".to_string()))
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   198
                } else if r.game_info.is_some() {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   199
                    actions.push(Warn("Joining not possible: Round is in progress.".to_string()))
13494
8c5dd562c9f7 Add room flags
alfadur
parents: 13492
diff changeset
   200
                } else if r.is_team_add_restricted() {
8c5dd562c9f7 Add room flags
alfadur
parents: 13492
diff changeset
   201
                    actions.push(Warn("This room currently does not allow adding new teams.".to_string()));
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   202
                } else {
13775
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13709
diff changeset
   203
                    let team = r.add_team(c.id, *info, c.protocol_number < 42);
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   204
                    c.teams_in_game += 1;
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   205
                    c.clan = Some(team.color);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   206
                    actions.push(TeamAccepted(team.name.clone())
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   207
                        .send_self().action());
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   208
                    actions.push(TeamAdd(HWRoom::team_info(&c, team))
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   209
                        .send_all().in_room(room_id).but_self().action());
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   210
                    actions.push(TeamColor(team.name.clone(), team.color)
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   211
                        .send_all().in_room(room_id).action());
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   212
                    actions.push(HedgehogsNumber(team.name.clone(), team.hedgehogs_number)
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   213
                        .send_all().in_room(room_id).action());
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   214
                    actions.push(SendRoomUpdate(None));
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   215
                }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   216
            }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   217
            server.react(client_id, actions);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   218
        },
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   219
        RemoveTeam(name) => {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   220
            let mut actions = Vec::new();
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   221
            if let (c, Some(r)) = server.client_and_room(client_id) {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   222
                match r.find_team_owner(&name) {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   223
                    None =>
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   224
                        actions.push(Warn("Error: The team you tried to remove does not exist.".to_string())),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   225
                    Some((id, _)) if id != client_id =>
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   226
                        actions.push(Warn("You can't remove a team you don't own.".to_string())),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   227
                    Some((_, name)) => {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   228
                        c.teams_in_game -= 1;
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   229
                        c.clan = r.find_team_color(c.id);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   230
                        actions.push(Action::RemoveTeam(name.to_string()));
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   231
                    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   232
                }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   233
            };
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   234
            server.react(client_id, actions);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   235
        },
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   236
        SetHedgehogsNumber(team_name, number) => {
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
   237
            if let (c, Some(r)) = server.client_and_room(client_id) {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   238
                let addable_hedgehogs = r.addable_hedgehogs();
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
   239
                let actions = if let Some((_, team)) = r.find_team_and_owner_mut(|t| t.name == team_name) {
13486
1ee192f13456 Better packing for clients
alfadur
parents: 13460
diff changeset
   240
                    if !c.is_master() {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   241
                        vec![ProtocolError("You're not the room master!".to_string())]
13709
e335daaa77a9 Add hogs per team named constant that absolutely no one asked for
alfadur
parents: 13666
diff changeset
   242
                    } else if number < 1 || number > MAX_HEDGEHOGS_PER_TEAM
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   243
                           || number > addable_hedgehogs + team.hedgehogs_number {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   244
                        vec![HedgehogsNumber(team.name.clone(), team.hedgehogs_number)
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   245
                            .send_self().action()]
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   246
                    } else {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   247
                        team.hedgehogs_number = number;
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   248
                        vec![HedgehogsNumber(team.name.clone(), number)
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   249
                            .send_all().in_room(room_id).but_self().action()]
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   250
                    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   251
                } else {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   252
                    vec![(Warn("No such team.".to_string()))]
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
   253
                };
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
   254
                server.react(client_id, actions);
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
   255
            }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   256
        },
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   257
        SetTeamColor(team_name, color) => {
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
   258
            if let (c, Some(r)) = server.client_and_room(client_id) {
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
   259
                let mut owner_id = None;
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
   260
                let actions = if let Some((owner, team)) = r.find_team_and_owner_mut(|t| t.name == team_name) {
13486
1ee192f13456 Better packing for clients
alfadur
parents: 13460
diff changeset
   261
                    if !c.is_master() {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   262
                        vec![ProtocolError("You're not the room master!".to_string())]
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   263
                    } else if false  {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   264
                        Vec::new()
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   265
                    } else {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   266
                        owner_id = Some(owner);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   267
                        team.color = color;
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   268
                        vec![TeamColor(team.name.clone(), color)
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   269
                            .send_all().in_room(room_id).but_self().action()]
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   270
                    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   271
                } else {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   272
                    vec![(Warn("No such team.".to_string()))]
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
   273
                };
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   274
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
   275
                if let Some(id) = owner_id {
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
   276
                    server.clients[id].clan = Some(color);
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
   277
                }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   278
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
   279
                server.react(client_id, actions);
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
   280
            };
13422
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   281
        },
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   282
        Cfg(cfg) => {
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
   283
            if let (c, Some(r)) = server.client_and_room(client_id) {
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
   284
                let actions = if r.is_fixed() {
13447
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13445
diff changeset
   285
                    vec![Warn("Access denied.".to_string())]
13486
1ee192f13456 Better packing for clients
alfadur
parents: 13460
diff changeset
   286
                } else if !c.is_master() {
13422
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   287
                    vec![ProtocolError("You're not the room master!".to_string())]
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   288
                } else {
13775
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13709
diff changeset
   289
                    let cfg = match cfg {
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13709
diff changeset
   290
                        GameCfg::Scheme(name, mut values) => {
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13709
diff changeset
   291
                            if c.protocol_number == 49 && values.len() >= 2 {
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13709
diff changeset
   292
                                let mut s = "X".repeat(50);
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13709
diff changeset
   293
                                s.push_str(&values.pop().unwrap());
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13709
diff changeset
   294
                                values.push(s);
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13709
diff changeset
   295
                            }
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13709
diff changeset
   296
                            GameCfg::Scheme(name, values)
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13709
diff changeset
   297
                        }
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13709
diff changeset
   298
                        cfg => cfg
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13709
diff changeset
   299
                    };
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13709
diff changeset
   300
13439
c4f917c6be51 add missing message tests
alfadur
parents: 13429
diff changeset
   301
                    let v = vec![cfg.to_server_msg()
c4f917c6be51 add missing message tests
alfadur
parents: 13429
diff changeset
   302
                        .send_all().in_room(r.id).but_self().action()];
c4f917c6be51 add missing message tests
alfadur
parents: 13429
diff changeset
   303
                    r.set_config(cfg);
c4f917c6be51 add missing message tests
alfadur
parents: 13429
diff changeset
   304
                    v
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
   305
                };
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
   306
                server.react(client_id, actions);
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
   307
            }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   308
        }
13528
c8b626b0a3ad Implement save/delete commands
alfadur
parents: 13521
diff changeset
   309
        Save(name, location) => {
c8b626b0a3ad Implement save/delete commands
alfadur
parents: 13521
diff changeset
   310
            let actions = vec![server_chat(format!("Room config saved as {}", name))
c8b626b0a3ad Implement save/delete commands
alfadur
parents: 13521
diff changeset
   311
                .send_all().in_room(room_id).action()];
c8b626b0a3ad Implement save/delete commands
alfadur
parents: 13521
diff changeset
   312
            server.rooms[room_id].save_config(name, location);
c8b626b0a3ad Implement save/delete commands
alfadur
parents: 13521
diff changeset
   313
            server.react(client_id, actions);
c8b626b0a3ad Implement save/delete commands
alfadur
parents: 13521
diff changeset
   314
        }
13529
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   315
        SaveRoom(filename) => {
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
   316
            if server.clients[client_id].is_admin() {
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
   317
                let actions = match server.rooms[room_id].get_saves() {
13529
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   318
                    Ok(text) => match write_file(&filename, &text) {
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   319
                        Ok(_) => vec![server_chat("Room configs saved successfully.".to_string())
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   320
                            .send_self().action()],
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   321
                        Err(e) => {
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   322
                            warn!("Error while writing the config file \"{}\": {}", filename, e);
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   323
                            vec![Warn("Unable to save the room configs.".to_string())]
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   324
                        }
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   325
                    }
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   326
                    Err(e) => {
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   327
                        warn!("Error while serializing the room configs: {}", e);
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   328
                        vec![Warn("Unable to serialize the room configs.".to_string())]
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   329
                    }
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
   330
                };
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
   331
                server.react(client_id, actions);
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
   332
            }
13529
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   333
        }
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   334
        LoadRoom(filename) => {
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
   335
            if server.clients[client_id].is_admin() {
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
   336
                let actions = match read_file(&filename) {
13529
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   337
                    Ok(text) => match server.rooms[room_id].set_saves(&text) {
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   338
                        Ok(_) => vec![server_chat("Room configs loaded successfully.".to_string())
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   339
                            .send_self().action()],
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   340
                        Err(e) => {
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   341
                            warn!("Error while deserializing the room configs: {}", e);
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   342
                            vec![Warn("Unable to deserialize the room configs.".to_string())]
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   343
                        }
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   344
                    }
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   345
                    Err(e) => {
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   346
                        warn!("Error while reading the config file \"{}\": {}", filename, e);
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   347
                        vec![Warn("Unable to load the room configs.".to_string())]
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   348
                    }
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
   349
                };
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
   350
                server.react(client_id, actions);
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13530
diff changeset
   351
            }
13529
662f7df89d06 Implement room config export
alfadur
parents: 13528
diff changeset
   352
        }
13528
c8b626b0a3ad Implement save/delete commands
alfadur
parents: 13521
diff changeset
   353
        Delete(name) => {
c8b626b0a3ad Implement save/delete commands
alfadur
parents: 13521
diff changeset
   354
            let actions = if !server.rooms[room_id].delete_config(&name) {
c8b626b0a3ad Implement save/delete commands
alfadur
parents: 13521
diff changeset
   355
                vec![Warn(format!("Save doesn't exist: {}", name))]
c8b626b0a3ad Implement save/delete commands
alfadur
parents: 13521
diff changeset
   356
            } else {
c8b626b0a3ad Implement save/delete commands
alfadur
parents: 13521
diff changeset
   357
                vec![server_chat(format!("Room config {} has been deleted", name))
c8b626b0a3ad Implement save/delete commands
alfadur
parents: 13521
diff changeset
   358
                    .send_all().in_room(room_id).action()]
c8b626b0a3ad Implement save/delete commands
alfadur
parents: 13521
diff changeset
   359
            };
c8b626b0a3ad Implement save/delete commands
alfadur
parents: 13521
diff changeset
   360
            server.react(client_id, actions);
c8b626b0a3ad Implement save/delete commands
alfadur
parents: 13521
diff changeset
   361
        }
13450
d79795acaa73 Mostly implement voting
alfadur
parents: 13447
diff changeset
   362
        CallVote(None) => {
d79795acaa73 Mostly implement voting
alfadur
parents: 13447
diff changeset
   363
            server.react(client_id, vec![
13521
e3ae9eea0689 Implement map voting
alfadur
parents: 13520
diff changeset
   364
                server_chat("Available callvote commands: kick <nickname>, map <name>, pause, newseed, hedgehogs <number>".to_string())
13450
d79795acaa73 Mostly implement voting
alfadur
parents: 13447
diff changeset
   365
                    .send_self().action()])
d79795acaa73 Mostly implement voting
alfadur
parents: 13447
diff changeset
   366
        }
d79795acaa73 Mostly implement voting
alfadur
parents: 13447
diff changeset
   367
        CallVote(Some(kind)) => {
13492
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13486
diff changeset
   368
            let is_in_game = server.rooms[room_id].game_info.is_some();
13450
d79795acaa73 Mostly implement voting
alfadur
parents: 13447
diff changeset
   369
            let error = match &kind {
d79795acaa73 Mostly implement voting
alfadur
parents: 13447
diff changeset
   370
                VoteType::Kick(nick) => {
d79795acaa73 Mostly implement voting
alfadur
parents: 13447
diff changeset
   371
                    if server.find_client(&nick).filter(|c| c.room_id == Some(room_id)).is_some() {
d79795acaa73 Mostly implement voting
alfadur
parents: 13447
diff changeset
   372
                        None
d79795acaa73 Mostly implement voting
alfadur
parents: 13447
diff changeset
   373
                    } else {
13521
e3ae9eea0689 Implement map voting
alfadur
parents: 13520
diff changeset
   374
                        Some("/callvote kick: No such user!".to_string())
13450
d79795acaa73 Mostly implement voting
alfadur
parents: 13447
diff changeset
   375
                    }
d79795acaa73 Mostly implement voting
alfadur
parents: 13447
diff changeset
   376
                },
d79795acaa73 Mostly implement voting
alfadur
parents: 13447
diff changeset
   377
                VoteType::Map(None) => {
13521
e3ae9eea0689 Implement map voting
alfadur
parents: 13520
diff changeset
   378
                    let names: Vec<_> = server.rooms[room_id].saves.keys().cloned().collect();
e3ae9eea0689 Implement map voting
alfadur
parents: 13520
diff changeset
   379
                    if names.is_empty() {
e3ae9eea0689 Implement map voting
alfadur
parents: 13520
diff changeset
   380
                        Some("/callvote map: No maps saved in this room!".to_string())
e3ae9eea0689 Implement map voting
alfadur
parents: 13520
diff changeset
   381
                    } else {
e3ae9eea0689 Implement map voting
alfadur
parents: 13520
diff changeset
   382
                        Some(format!("Available maps: {}", names.join(", ")))
e3ae9eea0689 Implement map voting
alfadur
parents: 13520
diff changeset
   383
                    }
13450
d79795acaa73 Mostly implement voting
alfadur
parents: 13447
diff changeset
   384
                },
d79795acaa73 Mostly implement voting
alfadur
parents: 13447
diff changeset
   385
                VoteType::Map(Some(name)) => {
13521
e3ae9eea0689 Implement map voting
alfadur
parents: 13520
diff changeset
   386
                    if server.rooms[room_id].saves.get(&name[..]).is_some() {
13530
80db7232b4b5 Fix map voting verification
alfadur
parents: 13529
diff changeset
   387
                        None
13521
e3ae9eea0689 Implement map voting
alfadur
parents: 13520
diff changeset
   388
                    } else {
13530
80db7232b4b5 Fix map voting verification
alfadur
parents: 13529
diff changeset
   389
                        Some("/callvote map: No such map!".to_string())
13521
e3ae9eea0689 Implement map voting
alfadur
parents: 13520
diff changeset
   390
                    }
13450
d79795acaa73 Mostly implement voting
alfadur
parents: 13447
diff changeset
   391
                },
d79795acaa73 Mostly implement voting
alfadur
parents: 13447
diff changeset
   392
                VoteType::Pause => {
d79795acaa73 Mostly implement voting
alfadur
parents: 13447
diff changeset
   393
                    if is_in_game {
d79795acaa73 Mostly implement voting
alfadur
parents: 13447
diff changeset
   394
                        None
d79795acaa73 Mostly implement voting
alfadur
parents: 13447
diff changeset
   395
                    } else {
13521
e3ae9eea0689 Implement map voting
alfadur
parents: 13520
diff changeset
   396
                        Some("/callvote pause: No game in progress!".to_string())
13450
d79795acaa73 Mostly implement voting
alfadur
parents: 13447
diff changeset
   397
                    }
d79795acaa73 Mostly implement voting
alfadur
parents: 13447
diff changeset
   398
                },
d79795acaa73 Mostly implement voting
alfadur
parents: 13447
diff changeset
   399
                VoteType::NewSeed => {
d79795acaa73 Mostly implement voting
alfadur
parents: 13447
diff changeset
   400
                    None
d79795acaa73 Mostly implement voting
alfadur
parents: 13447
diff changeset
   401
                },
d79795acaa73 Mostly implement voting
alfadur
parents: 13447
diff changeset
   402
                VoteType::HedgehogsPerTeam(number) => {
d79795acaa73 Mostly implement voting
alfadur
parents: 13447
diff changeset
   403
                    match number {
13709
e335daaa77a9 Add hogs per team named constant that absolutely no one asked for
alfadur
parents: 13666
diff changeset
   404
                        1...MAX_HEDGEHOGS_PER_TEAM => None,
13521
e3ae9eea0689 Implement map voting
alfadur
parents: 13520
diff changeset
   405
                        _ => Some("/callvote hedgehogs: Specify number from 1 to 8.".to_string())
13450
d79795acaa73 Mostly implement voting
alfadur
parents: 13447
diff changeset
   406
                    }
d79795acaa73 Mostly implement voting
alfadur
parents: 13447
diff changeset
   407
                },
d79795acaa73 Mostly implement voting
alfadur
parents: 13447
diff changeset
   408
            };
d79795acaa73 Mostly implement voting
alfadur
parents: 13447
diff changeset
   409
            match error {
d79795acaa73 Mostly implement voting
alfadur
parents: 13447
diff changeset
   410
                None => {
13460
fb37745c5bca complete newseed voting
alfadur
parents: 13450
diff changeset
   411
                    let msg = voting_description(&kind);
13450
d79795acaa73 Mostly implement voting
alfadur
parents: 13447
diff changeset
   412
                    let voting = Voting::new(kind, server.room_clients(client_id));
13520
3f69acc7e268 clean up room lookups
alfadur
parents: 13511
diff changeset
   413
                    server.rooms[room_id].voting = Some(voting);
13450
d79795acaa73 Mostly implement voting
alfadur
parents: 13447
diff changeset
   414
                    server.react(client_id, vec![
13521
e3ae9eea0689 Implement map voting
alfadur
parents: 13520
diff changeset
   415
                        server_chat(msg).send_all().in_room(room_id).action(),
13450
d79795acaa73 Mostly implement voting
alfadur
parents: 13447
diff changeset
   416
                        AddVote{ vote: true, is_forced: false}]);
d79795acaa73 Mostly implement voting
alfadur
parents: 13447
diff changeset
   417
                }
d79795acaa73 Mostly implement voting
alfadur
parents: 13447
diff changeset
   418
                Some(msg) => {
d79795acaa73 Mostly implement voting
alfadur
parents: 13447
diff changeset
   419
                    server.react(client_id, vec![
d79795acaa73 Mostly implement voting
alfadur
parents: 13447
diff changeset
   420
                        server_chat(msg).send_self().action()])
d79795acaa73 Mostly implement voting
alfadur
parents: 13447
diff changeset
   421
                }
d79795acaa73 Mostly implement voting
alfadur
parents: 13447
diff changeset
   422
            }
d79795acaa73 Mostly implement voting
alfadur
parents: 13447
diff changeset
   423
        }
d79795acaa73 Mostly implement voting
alfadur
parents: 13447
diff changeset
   424
        Vote(vote) => {
13520
3f69acc7e268 clean up room lookups
alfadur
parents: 13511
diff changeset
   425
            server.react(client_id, vec![AddVote{ vote, is_forced: false }]);
13450
d79795acaa73 Mostly implement voting
alfadur
parents: 13447
diff changeset
   426
        }
d79795acaa73 Mostly implement voting
alfadur
parents: 13447
diff changeset
   427
        ForceVote(vote) => {
13520
3f69acc7e268 clean up room lookups
alfadur
parents: 13511
diff changeset
   428
            let is_forced = server.clients[client_id].is_admin();
3f69acc7e268 clean up room lookups
alfadur
parents: 13511
diff changeset
   429
            server.react(client_id, vec![AddVote{ vote, is_forced }]);
13450
d79795acaa73 Mostly implement voting
alfadur
parents: 13447
diff changeset
   430
        }
13494
8c5dd562c9f7 Add room flags
alfadur
parents: 13492
diff changeset
   431
        ToggleRestrictJoin | ToggleRestrictTeams | ToggleRegisteredOnly  => {
8c5dd562c9f7 Add room flags
alfadur
parents: 13492
diff changeset
   432
            if server.clients[client_id].is_master() {
8c5dd562c9f7 Add room flags
alfadur
parents: 13492
diff changeset
   433
                server.rooms[room_id].flags.toggle(room_message_flag(&message));
8c5dd562c9f7 Add room flags
alfadur
parents: 13492
diff changeset
   434
            }
8c5dd562c9f7 Add room flags
alfadur
parents: 13492
diff changeset
   435
            server.react(client_id, vec![SendRoomUpdate(None)]);
8c5dd562c9f7 Add room flags
alfadur
parents: 13492
diff changeset
   436
        }
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   437
        StartGame => {
13520
3f69acc7e268 clean up room lookups
alfadur
parents: 13511
diff changeset
   438
            server.react(client_id, vec![StartRoomGame(room_id)]);
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   439
        }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   440
        EngineMessage(em) => {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   441
            let mut actions = Vec::new();
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   442
            if let (c, Some(r)) = server.client_and_room(client_id) {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   443
                if c.teams_in_game > 0 {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   444
                    let decoding = decode(&em[..]).unwrap();
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   445
                    let messages = by_msg(&decoding);
13443
2501428303a2 Fix team remove synchronization
alfadur
parents: 13439
diff changeset
   446
                    let valid = messages.filter(|m| is_msg_valid(m, &c.team_indices));
2501428303a2 Fix team remove synchronization
alfadur
parents: 13439
diff changeset
   447
                    let non_empty = valid.clone().filter(|m| !is_msg_empty(m));
2501428303a2 Fix team remove synchronization
alfadur
parents: 13439
diff changeset
   448
                    let sync_msg = valid.clone().filter(|m| is_msg_timed(m))
2501428303a2 Fix team remove synchronization
alfadur
parents: 13439
diff changeset
   449
                        .last().map(|m| if is_msg_empty(m) {Some(encode(m))} else {None});
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   450
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   451
                    let em_response = encode(&valid.flat_map(|msg| msg).cloned().collect::<Vec<_>>());
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   452
                    if !em_response.is_empty() {
13428
c8425fbcf1d9 Fix spectators joining midgame
alfadur
parents: 13427
diff changeset
   453
                        actions.push(ForwardEngineMessage(vec![em_response])
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   454
                            .send_all().in_room(r.id).but_self().action());
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   455
                    }
13427
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   456
                    let em_log = encode(&non_empty.flat_map(|msg| msg).cloned().collect::<Vec<_>>());
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   457
                    if let Some(ref mut info) = r.game_info {
13429
4c5ed27b1ff8 stabilization
alfadur
parents: 13428
diff changeset
   458
                        if !em_log.is_empty() {
13428
c8425fbcf1d9 Fix spectators joining midgame
alfadur
parents: 13427
diff changeset
   459
                            info.msg_log.push(em_log);
c8425fbcf1d9 Fix spectators joining midgame
alfadur
parents: 13427
diff changeset
   460
                        }
13443
2501428303a2 Fix team remove synchronization
alfadur
parents: 13439
diff changeset
   461
                        if let Some(msg) = sync_msg {
2501428303a2 Fix team remove synchronization
alfadur
parents: 13439
diff changeset
   462
                            info.sync_msg = msg;
13427
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   463
                        }
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   464
                    }
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   465
                }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   466
            }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   467
            server.react(client_id, actions)
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   468
        }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   469
        RoundFinished => {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   470
            let mut actions = Vec::new();
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   471
            if let (c, Some(r)) = server.client_and_room(client_id) {
13486
1ee192f13456 Better packing for clients
alfadur
parents: 13460
diff changeset
   472
                if c.is_in_game() {
1ee192f13456 Better packing for clients
alfadur
parents: 13460
diff changeset
   473
                    c.set_is_in_game(false);
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   474
                    actions.push(ClientFlags("-g".to_string(), vec![c.nick.clone()]).
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   475
                        send_all().in_room(r.id).action());
13426
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   476
                    if r.game_info.is_some() {
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   477
                        for team in r.client_teams(c.id) {
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   478
                            actions.push(SendTeamRemovalMessage(team.name.clone()));
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   479
                        }
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   480
                    }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   481
                }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   482
            }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   483
            server.react(client_id, actions)
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13443
diff changeset
   484
        },
13445
d3c86ade3d4d Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13444
diff changeset
   485
        Rnd(v) => {
13492
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13486
diff changeset
   486
            let result = rnd_reply(&v);
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13486
diff changeset
   487
            let mut echo = vec!["/rnd".to_string()];
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13486
diff changeset
   488
            echo.extend(v.into_iter());
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13486
diff changeset
   489
            let chat_msg = ChatMsg {
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13486
diff changeset
   490
                nick: server.clients[client_id].nick.clone(),
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13486
diff changeset
   491
                msg: echo.join(" ")
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13486
diff changeset
   492
            };
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13486
diff changeset
   493
            server.react(client_id, vec![
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13486
diff changeset
   494
                chat_msg.send_all().in_room(room_id).action(),
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13486
diff changeset
   495
                result.send_all().in_room(room_id).action()])
13445
d3c86ade3d4d Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13444
diff changeset
   496
        },
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   497
        _ => warn!("Unimplemented!")
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
   498
    }
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
   499
}