gameServer2/src/protocol/messages.rs
author Wuzzy <Wuzzy2@mail.ru>
Mon, 26 Feb 2018 21:17:36 +0100
changeset 13076 bcb205281f38
parent 12152 03ccb89820f3
child 13124 1e39b8749072
permissions -rw-r--r--
Some minor convenience improvements in Basic Flying Saucer Training

use server::coretypes::{ServerVar, GameCfg, TeamInfo, HedgehogInfo};
use std;
use std::ops;
use std::convert::From;

#[derive(PartialEq, Debug)]
pub enum HWProtocolMessage {
    // core
    Ping,
    Pong,
    Quit(Option<String>),
    //Cmd(String, Vec<String>),
    Global(String),
    Watch(String),
    ToggleServerRegisteredOnly,
    SuperPower,
    Info(String),
    // not entered state
    Nick(String),
    Proto(u32),
    Password(String, String),
    Checker(u32, String, String),
    // lobby
    List,
    Chat(String),
    CreateRoom(String, Option<String>),
    Join(String, Option<String>),
    Follow(String),
    Rnd(Vec<String>),
    Kick(String),
    Ban(String, String, u32),
    BanIP(String, String, u32),
    BanNick(String, String, u32),
    BanList,
    Unban(String),
    SetServerVar(ServerVar),
    GetServerVar,
    RestartServer,
    Stats,
    // in room
    Part(Option<String>),
    Cfg(GameCfg),
    AddTeam(TeamInfo),
    RemoveTeam(String),
    SetHedgehogsNumber(String, u8),
    SetTeamColor(String, u8),
    ToggleReady,
    StartGame,
    EngineMessage(String),
    RoundFinished,
    ToggleRestrictJoin,
    ToggleRestrictTeams,
    ToggleRegisteredOnly,
    RoomName(String),
    Delegate(String),
    TeamChat(String),
    MaxTeams(u8),
    Fix,
    Unfix,
    Greeting(String),
    CallVote(Option<(String, Option<String>)>),
    Vote(String),
    ForceVote(String),
    Save(String, String),
    Delete(String),
    SaveRoom(String),
    LoadRoom(String),
    Malformed,
    Empty,
}

pub enum HWServerMessage<'a> {
    Ping,
    Pong,
    Bye(&'a str),
    Nick(&'a str),
    LobbyLeft(&'a str),
    LobbyJoined(&'a [&'a str]),
    ChatMsg(&'a str, &'a str),
    ClientFlags(&'a str, &'a [&'a str]),

    Warning(&'a str),
    Connected(u32),
    Unreachable,
}

fn construct_message(msg: & [&str]) -> String {
    let mut m = String::with_capacity(64);

    for s in msg {
        m.push_str(s);
        m.push('\n');
    }
    m.push('\n');

    m
}

impl<'a> HWServerMessage<'a> {
    pub fn to_raw_protocol(&self) -> String {
        match self {
            &HWServerMessage::Ping
                => "PING\n\n".to_string(),
            &HWServerMessage::Pong
                => "PONG\n\n".to_string(),
            &HWServerMessage::Connected(protocol_version)
                => construct_message(&[
                    "CONNECTED",
                    "Hedgewars server http://www.hedgewars.org/",
                    &protocol_version.to_string()
                ]),
            &HWServerMessage::Bye(msg)
                => construct_message(&["BYE", &msg]),
            &HWServerMessage::Nick(nick)
                => construct_message(&["NICK", &nick]),
            &HWServerMessage::LobbyLeft(nick)
                => construct_message(&["LOBBY_LEFT", &nick]),
            &HWServerMessage::LobbyJoined(nicks)
                => {
                let mut v = vec!["LOBBY:JOINED"];
                v.extend_from_slice(nicks);
                construct_message(&v)
            },
            &HWServerMessage::ClientFlags(flags, nicks)
            => {
                let mut v = vec!["CLIENT_FLAGS"];
                v.push(flags);
                v.extend_from_slice(nicks);
                construct_message(&v)
            },
            &HWServerMessage::ChatMsg(nick, msg)
                => construct_message(&["CHAT", &nick, &msg]),
            &HWServerMessage::Warning(msg)
                => construct_message(&["WARNING", &msg]),
            _ => construct_message(&["ERROR", "UNIMPLEMENTED"]),
        }
    }
}