gameServer2/src/protocol/messages.rs
author Wuzzy <almikes@aol.com>
Sun, 16 Apr 2017 02:02:07 +0200
changeset 12264 41542ed28fe4
parent 12147 03ccb89820f3
child 13119 1e39b8749072
permissions -rw-r--r--
Allow the diaogues in ACF missions 8 and 10 to be translated

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"]),
        }
    }
}