gameServer2/src/server/handlers/inroom.rs
author alfadur
Wed, 27 Jun 2018 02:34:46 +0300
changeset 13423 87a6cad20c90
parent 13422 5fb27f94fc3b
child 13424 d8354cb98b98
permissions -rw-r--r--
Implement game start & engine messages
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
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
     3
use protocol::messages::{
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
     4
    HWProtocolMessage,
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
     5
    HWServerMessage::*
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
     6
};
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
     7
use server::{
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
     8
    server::HWServer,
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
     9
    client::ClientId,
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    10
    room::HWRoom,
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    11
    actions::{Action, Action::*}
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    12
};
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    13
use utils::is_name_illegal;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    14
use std::mem::swap;
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    15
use base64::{encode, decode};
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    16
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    17
#[derive(Clone)]
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    18
struct ByMsg<'a> {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    19
    messages: &'a[u8]
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    20
}
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    21
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    22
impl <'a> Iterator for ByMsg<'a> {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    23
    type Item = &'a[u8];
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    24
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    25
    fn next(&mut self) -> Option<<Self as Iterator>::Item> {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    26
        if let Some(size) = self.messages.get(0) {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    27
            let (msg, next) = self.messages.split_at(*size as usize + 1);
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    28
            self.messages = next;
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    29
            Some(msg)
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    30
        } else {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    31
            None
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    32
        }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    33
    }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    34
}
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 by_msg(source: &Vec<u8>) -> ByMsg {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    37
    ByMsg {messages: &source[..]}
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    38
}
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    39
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    40
const VALID_MESSAGES: &[u8] =
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    41
    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
    42
const NON_TIMED_MESSAGES: &[u8] = b"M#hb";
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    43
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    44
fn is_msg_valid(msg: &[u8], team_indices: &[u8]) -> bool {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    45
    if let [size, typ, body..] = msg {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    46
        VALID_MESSAGES.contains(typ) &&
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    47
            match body {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    48
                [1...8, team, ..] if *typ == b'h' => team_indices.contains(team),
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    49
                _ => *typ != b'h'
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    50
            }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    51
    } else {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    52
        false
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    53
    }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    54
}
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    55
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    56
fn is_msg_empty(msg: &[u8]) -> bool {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    57
    match msg {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    58
        [_, b'+', ..] => true,
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    59
        _ => false
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    60
    }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    61
}
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    62
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    63
pub fn handle(server: &mut HWServer, client_id: ClientId, message: HWProtocolMessage) {
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    64
    use protocol::messages::HWProtocolMessage::*;
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    65
    match message {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    66
        Part(None) => server.react(client_id, vec![
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    67
            MoveToLobby("part".to_string())]),
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    68
        Part(Some(msg)) => server.react(client_id, vec![
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    69
            MoveToLobby(format!("part: {}", msg))]),
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    70
        Chat(msg) => {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    71
            let actions = {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    72
                let c = &mut server.clients[client_id];
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    73
                let chat_msg = ChatMsg(c.nick.clone(), msg);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    74
                if let Some(room_id) = c.room_id {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    75
                    vec![chat_msg.send_all().in_room(room_id).but_self().action()]
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    76
                } else {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    77
                    Vec::new()
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    78
                }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    79
            };
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    80
            server.react(client_id, actions);
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    81
        },
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    82
        RoomName(new_name) => {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    83
            let actions =
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    84
                if is_name_illegal(&new_name) {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    85
                    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())]
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    86
                } else if server.has_room(&new_name) {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    87
                    vec![Warn("A room with the same name already exists.".to_string())]
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    88
                } else {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    89
                    let mut old_name = new_name.clone();
13422
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
    90
                    if let (_, Some(r)) = server.client_and_room(client_id) {
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    91
                        swap(&mut r.name, &mut old_name);
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    92
                        vec![SendRoomUpdate(Some(old_name))]
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    93
                    } else {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    94
                        Vec::new()
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    95
                    }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    96
                };
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    97
            server.react(client_id, actions);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    98
        },
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    99
        ToggleReady => {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   100
            let actions = if let (c, Some(r)) = server.client_and_room(client_id) {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   101
                let flags = if c.is_ready {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   102
                    r.ready_players_number -= 1;
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   103
                    "-r"
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   104
                } else {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   105
                    r.ready_players_number += 1;
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   106
                    "+r"
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   107
                };
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   108
                c.is_ready = !c.is_ready;
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   109
                vec![ClientFlags(flags.to_string(), vec![c.nick.clone()])
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   110
                    .send_all().in_room(r.id).action()]
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   111
            } else {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   112
                Vec::new()
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   113
            };
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   114
            server.react(client_id, actions);
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   115
        }
13422
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   116
        AddTeam(info) => {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   117
            let mut actions = Vec::new();
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   118
            if let (c, Some(r)) = server.client_and_room(client_id) {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   119
                let room_id = r.id;
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   120
                if r.teams.len() >= r.team_limit as usize {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   121
                    actions.push(Warn("Too many teams!".to_string()))
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   122
                } else if r.addable_hedgehogs() == 0 {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   123
                    actions.push(Warn("Too many hedgehogs!".to_string()))
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   124
                } else if r.find_team(|t| t.name == info.name) != None {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   125
                    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
   126
                } else if r.game_info.is_some() {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   127
                    actions.push(Warn("Joining not possible: Round is in progress.".to_string()))
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   128
                } else {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   129
                    let team = r.add_team(c.id, info);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   130
                    c.teams_in_game += 1;
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   131
                    c.clan = Some(team.color);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   132
                    actions.push(TeamAccepted(team.name.clone())
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   133
                        .send_self().action());
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   134
                    actions.push(TeamAdd(HWRoom::team_info(&c, team))
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   135
                        .send_all().in_room(room_id).but_self().action());
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   136
                    actions.push(TeamColor(team.name.clone(), team.color)
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   137
                        .send_all().in_room(room_id).action());
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   138
                    actions.push(HedgehogsNumber(team.name.clone(), team.hedgehogs_number)
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   139
                        .send_all().in_room(room_id).action());
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   140
                    actions.push(SendRoomUpdate(None));
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   141
                }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   142
            }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   143
            server.react(client_id, actions);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   144
        },
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   145
        RemoveTeam(name) => {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   146
            let mut actions = Vec::new();
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   147
            if let (c, Some(r)) = server.client_and_room(client_id) {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   148
                match r.find_team_owner(&name) {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   149
                    None =>
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   150
                        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
   151
                    Some((id, _)) if id != client_id =>
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   152
                        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
   153
                    Some((_, name)) => {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   154
                        c.teams_in_game -= 1;
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   155
                        c.clan = r.find_team_color(c.id);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   156
                        actions.push(Action::RemoveTeam(name.to_string()));
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   157
                    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   158
                }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   159
            };
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   160
            server.react(client_id, actions);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   161
        },
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   162
        SetHedgehogsNumber(team_name, number) => {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   163
            let actions = if let (c, Some(r)) = server.client_and_room(client_id) {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   164
                let room_id = r.id;
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   165
                let addable_hedgehogs = r.addable_hedgehogs();
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   166
                if let Some((_, mut team)) = r.find_team_and_owner_mut(|t| t.name == team_name) {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   167
                    if !c.is_master {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   168
                        vec![ProtocolError("You're not the room master!".to_string())]
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   169
                    } else if number < 1 || number > 8
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   170
                           || number > addable_hedgehogs + team.hedgehogs_number {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   171
                        vec![HedgehogsNumber(team.name.clone(), team.hedgehogs_number)
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   172
                            .send_self().action()]
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   173
                    } else {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   174
                        team.hedgehogs_number = number;
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   175
                        vec![HedgehogsNumber(team.name.clone(), number)
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   176
                            .send_all().in_room(room_id).but_self().action()]
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   177
                    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   178
                } else {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   179
                    vec![(Warn("No such team.".to_string()))]
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   180
                }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   181
            } else {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   182
                Vec::new()
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   183
            };
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   184
            server.react(client_id, actions);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   185
        },
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   186
        SetTeamColor(team_name, color) => {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   187
            let mut owner_id = None;
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   188
            let actions = if let (c, Some(r)) = server.client_and_room(client_id) {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   189
                let room_id = r.id;
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   190
                if let Some((owner, mut team)) = r.find_team_and_owner_mut(|t| t.name == team_name) {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   191
                    if !c.is_master {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   192
                        vec![ProtocolError("You're not the room master!".to_string())]
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   193
                    } else if false  {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   194
                        Vec::new()
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   195
                    } else {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   196
                        owner_id = Some(owner);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   197
                        team.color = color;
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   198
                        vec![TeamColor(team.name.clone(), color)
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   199
                            .send_all().in_room(room_id).but_self().action()]
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   200
                    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   201
                } else {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   202
                    vec![(Warn("No such team.".to_string()))]
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   203
                }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   204
            } else {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   205
                Vec::new()
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   206
            };
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   207
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   208
            if let Some(id) = owner_id {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   209
                server.clients[id].clan = Some(color);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   210
            }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   211
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   212
            server.react(client_id, actions);
13422
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   213
        },
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   214
        Cfg(cfg) => {
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   215
            let actions = if let (c, Some(r)) = server.client_and_room(client_id) {
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   216
                if !c.is_master {
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   217
                    vec![ProtocolError("You're not the room master!".to_string())]
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   218
                } else {
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   219
                    r.set_config(cfg.clone());
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   220
                    vec![cfg.into_server_msg()
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   221
                        .send_all().in_room(r.id).but_self().action()]
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   222
                }
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   223
            } else {
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   224
                Vec::new()
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   225
            };
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   226
            server.react(client_id, actions);
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   227
        }
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   228
        StartGame => {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   229
            let actions = if let (_, Some(r)) = server.client_and_room(client_id) {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   230
                vec![StartRoomGame(r.id)]
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   231
            } else {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   232
                Vec::new()
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   233
            };
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   234
            server.react(client_id, actions);
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   235
        }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   236
        EngineMessage(em) => {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   237
            let mut actions = Vec::new();
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   238
            if let (c, Some(r)) = server.client_and_room(client_id) {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   239
                if c.teams_in_game > 0 {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   240
                    let decoding = decode(&em[..]).unwrap();
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   241
                    let messages = by_msg(&decoding);
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   242
                    let valid = messages.clone().filter(|m| is_msg_valid(m, &c.team_indices));
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   243
                    let _non_empty = messages.filter(|m| !is_msg_empty(m));
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   244
                    let _last_valid_timed_msg = valid.clone().scan(None, |res, msg| match msg {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   245
                        [_, b'+', ..] => Some(msg),
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   246
                        [_, typ, ..] if NON_TIMED_MESSAGES.contains(typ) => *res,
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   247
                        _ => None
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   248
                    }).next();
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   249
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   250
                    let em_response = encode(&valid.flat_map(|msg| msg).cloned().collect::<Vec<_>>());
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   251
                    if !em_response.is_empty() {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   252
                        actions.push(ForwardEngineMessage(em_response)
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   253
                            .send_all().in_room(r.id).but_self().action());
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   254
                    }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   255
                }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   256
            }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   257
            server.react(client_id, actions)
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   258
        }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   259
        RoundFinished => {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   260
            let mut actions = Vec::new();
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   261
            if let (c, Some(r)) = server.client_and_room(client_id) {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   262
                if c.is_in_game {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   263
                    c.is_in_game = false;
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   264
                    actions.push(ClientFlags("-g".to_string(), vec![c.nick.clone()]).
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   265
                        send_all().in_room(r.id).action());
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   266
                    for team in r.client_teams(c.id) {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   267
                        actions.push(SendTeamRemovalMessage(team.name.clone()));
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   268
                    }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   269
                }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   270
            }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   271
            server.react(client_id, actions)
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   272
        }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   273
        _ => warn!("Unimplemented!")
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
   274
    }
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
   275
}