gameServer2/src/server/handlers/inroom.rs
author alfadur
Tue, 26 Jun 2018 23:22:38 +0300
changeset 13422 5fb27f94fc3b
parent 13419 81e0ed105f5d
child 13423 87a6cad20c90
permissions -rw-r--r--
Implement game config 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;
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    15
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    16
pub fn handle(server: &mut HWServer, client_id: ClientId, message: HWProtocolMessage) {
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    17
    use protocol::messages::HWProtocolMessage::*;
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    18
    match message {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    19
        Part(None) => server.react(client_id, vec![
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    20
            MoveToLobby("part".to_string())]),
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    21
        Part(Some(msg)) => server.react(client_id, vec![
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    22
            MoveToLobby(format!("part: {}", msg))]),
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    23
        Chat(msg) => {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    24
            let actions = {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    25
                let c = &mut server.clients[client_id];
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    26
                let chat_msg = ChatMsg(c.nick.clone(), msg);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    27
                if let Some(room_id) = c.room_id {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    28
                    vec![chat_msg.send_all().in_room(room_id).but_self().action()]
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    29
                } else {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    30
                    Vec::new()
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    31
                }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    32
            };
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    33
            server.react(client_id, actions);
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    34
        },
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    35
        RoomName(new_name) => {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    36
            let actions =
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    37
                if is_name_illegal(&new_name) {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    38
                    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
    39
                } else if server.has_room(&new_name) {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    40
                    vec![Warn("A room with the same name already exists.".to_string())]
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    41
                } else {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    42
                    let mut old_name = new_name.clone();
13422
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
    43
                    if let (_, Some(r)) = server.client_and_room(client_id) {
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    44
                        swap(&mut r.name, &mut old_name);
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    45
                        vec![SendRoomUpdate(Some(old_name))]
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    46
                    } else {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    47
                        Vec::new()
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    48
                    }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    49
                };
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    50
            server.react(client_id, actions);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    51
        },
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    52
        ToggleReady => {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    53
            let actions = if let (c, Some(r)) = server.client_and_room(client_id) {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    54
                let flags = if c.is_ready {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    55
                    r.ready_players_number -= 1;
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    56
                    "-r"
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    57
                } else {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    58
                    r.ready_players_number += 1;
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    59
                    "+r"
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    60
                };
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    61
                c.is_ready = !c.is_ready;
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    62
                vec![ClientFlags(flags.to_string(), vec![c.nick.clone()])
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    63
                    .send_all().in_room(r.id).action()]
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    64
            } else {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    65
                Vec::new()
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    66
            };
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    67
            server.react(client_id, actions);
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    68
        }
13422
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
    69
        AddTeam(info) => {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    70
            let mut actions = Vec::new();
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    71
            if let (c, Some(r)) = server.client_and_room(client_id) {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    72
                let room_id = r.id;
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    73
                if r.teams.len() >= r.team_limit as usize {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    74
                    actions.push(Warn("Too many teams!".to_string()))
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    75
                } else if r.addable_hedgehogs() == 0 {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    76
                    actions.push(Warn("Too many hedgehogs!".to_string()))
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    77
                } else if r.find_team(|t| t.name == info.name) != None {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    78
                    actions.push(Warn("There's already a team with same name in the list.".to_string()))
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    79
                } else if r.game_info != None {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    80
                    actions.push(Warn("Joining not possible: Round is in progress.".to_string()))
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    81
                } else {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    82
                    let team = r.add_team(c.id, info);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    83
                    c.teams_in_game += 1;
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    84
                    c.clan = Some(team.color);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    85
                    actions.push(TeamAccepted(team.name.clone())
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    86
                        .send_self().action());
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    87
                    actions.push(TeamAdd(HWRoom::team_info(&c, team))
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    88
                        .send_all().in_room(room_id).but_self().action());
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    89
                    actions.push(TeamColor(team.name.clone(), team.color)
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    90
                        .send_all().in_room(room_id).action());
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    91
                    actions.push(HedgehogsNumber(team.name.clone(), team.hedgehogs_number)
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    92
                        .send_all().in_room(room_id).action());
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    93
                    actions.push(SendRoomUpdate(None));
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    94
                }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    95
            }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    96
            server.react(client_id, actions);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    97
        },
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    98
        RemoveTeam(name) => {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    99
            let mut actions = Vec::new();
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   100
            if let (c, Some(r)) = server.client_and_room(client_id) {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   101
                match r.find_team_owner(&name) {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   102
                    None =>
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   103
                        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
   104
                    Some((id, _)) if id != client_id =>
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   105
                        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
   106
                    Some((_, name)) => {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   107
                        c.teams_in_game -= 1;
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   108
                        c.clan = r.find_team_color(c.id);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   109
                        actions.push(Action::RemoveTeam(name.to_string()));
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   110
                    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   111
                }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   112
            };
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   113
            server.react(client_id, actions);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   114
        },
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   115
        SetHedgehogsNumber(team_name, number) => {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   116
            let actions = if let (c, Some(r)) = server.client_and_room(client_id) {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   117
                let room_id = r.id;
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   118
                let addable_hedgehogs = r.addable_hedgehogs();
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   119
                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
   120
                    if !c.is_master {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   121
                        vec![ProtocolError("You're not the room master!".to_string())]
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   122
                    } else if number < 1 || number > 8
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   123
                           || number > addable_hedgehogs + team.hedgehogs_number {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   124
                        vec![HedgehogsNumber(team.name.clone(), team.hedgehogs_number)
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   125
                            .send_self().action()]
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   126
                    } else {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   127
                        team.hedgehogs_number = number;
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   128
                        vec![HedgehogsNumber(team.name.clone(), number)
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   129
                            .send_all().in_room(room_id).but_self().action()]
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   130
                    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   131
                } else {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   132
                    vec![(Warn("No such team.".to_string()))]
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   133
                }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   134
            } else {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   135
                Vec::new()
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   136
            };
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   137
            server.react(client_id, actions);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   138
        },
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   139
        SetTeamColor(team_name, color) => {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   140
            let mut owner_id = None;
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   141
            let actions = if let (c, Some(r)) = server.client_and_room(client_id) {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   142
                let room_id = r.id;
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   143
                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
   144
                    if !c.is_master {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   145
                        vec![ProtocolError("You're not the room master!".to_string())]
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   146
                    } else if false  {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   147
                        Vec::new()
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   148
                    } else {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   149
                        owner_id = Some(owner);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   150
                        team.color = color;
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   151
                        vec![TeamColor(team.name.clone(), color)
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   152
                            .send_all().in_room(room_id).but_self().action()]
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   153
                    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   154
                } else {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   155
                    vec![(Warn("No such team.".to_string()))]
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   156
                }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   157
            } else {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   158
                Vec::new()
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   159
            };
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   160
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   161
            if let Some(id) = owner_id {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   162
                server.clients[id].clan = Some(color);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   163
            }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   164
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   165
            server.react(client_id, actions);
13422
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   166
        },
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   167
        Cfg(cfg) => {
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   168
            let actions = if let (c, Some(r)) = server.client_and_room(client_id) {
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   169
                if !c.is_master {
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   170
                    vec![ProtocolError("You're not the room master!".to_string())]
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   171
                } else {
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   172
                    r.set_config(cfg.clone());
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   173
                    vec![cfg.into_server_msg()
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   174
                        .send_all().in_room(r.id).but_self().action()]
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   175
                }
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   176
            } else {
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   177
                Vec::new()
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   178
            };
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   179
            server.react(client_id, actions);
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   180
        }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   181
        _ => warn!("Unimplemented!")
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
   182
    }
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
   183
}