rust/hedgewars-server/src/server/handlers/lobby.rs
author alfadur <mail@none>
Sat, 02 Feb 2019 15:06:39 +0300
changeset 14671 455865ccd36c
parent 14504 6cc0fce249f9
child 14672 6e6632068a33
permissions -rw-r--r--
Server action refactoring part 2 of N

use mio;

use super::common::rnd_reply;
use crate::{
    protocol::messages::{HWProtocolMessage, HWServerMessage::*},
    server::{
        actions::{Action, Action::*},
        core::HWServer,
        coretypes::ClientId,
    },
    utils::is_name_illegal,
};
use log::*;

pub fn handle(
    server: &mut HWServer,
    client_id: ClientId,
    response: &mut super::Response,
    message: HWProtocolMessage,
) {
    use crate::protocol::messages::HWProtocolMessage::*;
    match message {
        CreateRoom(name, password) => {
            let actions = if is_name_illegal(&name) {
                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())]
            } else if server.has_room(&name) {
                vec![Warn(
                    "A room with the same name already exists.".to_string(),
                )]
            } else {
                let flags_msg = ClientFlags(
                    "+hr".to_string(),
                    vec![server.clients[client_id].nick.clone()],
                );

                let room_id = server.create_room(client_id, name, password);
                let room = &server.rooms[room_id];
                let client = &server.clients[client_id];

                response.add(
                    RoomAdd(room.info(Some(&client)))
                        .send_all()
                        .with_protocol(room.protocol_number),
                );
                response.add(flags_msg.send_self());

                response.add(ClientFlags("+i".to_string(), vec![client.nick.clone()]).send_self());
                vec![]
            };
            server.react(client_id, actions);
        }
        Chat(msg) => {
            let actions = vec![ChatMsg {
                nick: server.clients[client_id].nick.clone(),
                msg,
            }
            .send_all()
            .in_room(server.lobby_id)
            .but_self()
            .action()];
            server.react(client_id, actions);
        }
        JoinRoom(name, _password) => {
            let room = server.rooms.iter().find(|(_, r)| r.name == name);
            let room_id = room.map(|(_, r)| r.id);
            let nicks = server
                .clients
                .iter()
                .filter(|(_, c)| c.room_id == room_id)
                .map(|(_, c)| c.nick.clone())
                .collect();
            let c = &mut server.clients[client_id];

            let actions = if let Some((_, r)) = room {
                if c.protocol_number != r.protocol_number {
                    vec![Warn(
                        "Room version incompatible to your Hedgewars version!".to_string(),
                    )]
                } else if r.is_join_restricted() {
                    vec![Warn(
                        "Access denied. This room currently doesn't allow joining.".to_string(),
                    )]
                } else if r.players_number == u8::max_value() {
                    vec![Warn("This room is already full".to_string())]
                } else if let Some(room_id) = room_id {
                    let nick = c.nick.clone();
                    server.move_to_room(client_id, room_id);

                    response.add(RoomJoined(vec![nick.clone()]).send_all().in_room(room_id));
                    response.add(ClientFlags("+i".to_string(), vec![nick]).send_all());
                    response.add(RoomJoined(nicks).send_self());

                    let room = &server.rooms[room_id];

                    if !room.greeting.is_empty() {
                        response.add(
                            ChatMsg {
                                nick: "[greeting]".to_string(),
                                msg: room.greeting.clone(),
                            }
                            .send_self(),
                        );
                    }

                    vec![]
                } else {
                    vec![]
                }
            } else {
                vec![Warn("No such room.".to_string())]
            };
            server.react(client_id, actions);
        }
        Rnd(v) => {
            server.react(client_id, vec![rnd_reply(&v).send_self().action()]);
        }
        List => warn!("Deprecated LIST message received"),
        _ => warn!("Incorrect command in lobby state"),
    }
}