rust/hedgewars-engine-messages/src/messages.rs
author unC0Rr
Fri, 16 Nov 2018 16:32:51 +0100
changeset 14235 77b95406631e
parent 14232 0f7b92397b9e
child 14275 9f4d75fa4a2c
permissions -rw-r--r--
Implement more of parsing, start implementation of engine messages rendering
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14230
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
     1
#[derive(Debug, PartialEq)]
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
     2
pub enum KeystrokeAction {
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
     3
    Press,
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
     4
    Release,
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
     5
}
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
     6
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
     7
#[derive(Debug, PartialEq)]
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
     8
pub enum SyncedEngineMessage {
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
     9
    Left(KeystrokeAction),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    10
    Right(KeystrokeAction),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    11
    Up(KeystrokeAction),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    12
    Down(KeystrokeAction),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    13
    Precise(KeystrokeAction),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    14
    Attack(KeystrokeAction),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    15
    NextTurn,
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    16
    Switch,
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    17
    Timer(u8),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    18
    Slot(u8),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    19
    SetWeapon(u8),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    20
    Put(i32, i32),
14232
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14231
diff changeset
    21
    CursorMove(i32, i32),
14230
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    22
    HighJump,
14232
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14231
diff changeset
    23
    LongJump,
14230
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    24
    Skip,
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    25
    TeamControlGained(String),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    26
    TeamControlLost(String),
14231
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14230
diff changeset
    27
    TimeWrap,
14235
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
    28
    Taunt(u8),
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
    29
    HogSay(String),
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
    30
    Heartbeat,
14230
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    31
}
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    32
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    33
#[derive(Debug, PartialEq)]
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    34
pub enum UnsyncedEngineMessage {
14235
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
    35
    TeamControlGained(String),
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
    36
    TeamControlLost(String),
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
    37
}
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
    38
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
    39
#[derive(Debug, PartialEq)]
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
    40
pub enum UnorderedEngineMessage {
14230
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    41
    Ping,
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    42
    Pong,
14235
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
    43
    ChatMessage(String),
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
    44
    TeamMessage(String),
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
    45
    Error(String),
14230
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    46
    Warning(String),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    47
    StopSyncing,
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    48
    GameOver,
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    49
    GameInterrupted,
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    50
    GameSetupChecksum(String),
14235
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
    51
    PauseToggled,
14230
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    52
}
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    53
#[derive(Debug, PartialEq)]
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    54
pub enum ConfigEngineMessage {
14235
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
    55
    GameType(u8),
14230
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    56
    ConfigRequest,
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    57
    SetAmmo(String),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    58
    SetScript(String),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    59
    SetScriptParam(String),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    60
    Spectate,
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    61
    TeamLocality(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    62
    SetMap(String),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    63
    SetTheme(String),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    64
    SetSeed(String),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    65
    SetTemplateFilter(String),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    66
    SetMapGenerator(String),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    67
    SetFeatureSize(u8),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    68
    SetDelay(u32),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    69
    SetReadyDelay(u32),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    70
    SetCratesFrequency(u8),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    71
    SetHealthCrateProbability(u8),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    72
    SetHealthCratesNumber(u8),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    73
    SetRoundsTilSuddenDeath(u8),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    74
    SetSuddenDeathWaterRiseSpeed(u8),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    75
    SetSuddenDeathHealthDecreaseRate(u8),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    76
    SetDamageMultiplier(u32),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    77
    SetRopeLength(u32),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    78
    SetGetawayTime(u32),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    79
    SetDudMinesPercent(u8),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    80
    SetMinesNumber(u32),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    81
    SetAirMinesNumber(u32),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    82
    SetBarrelsNumber(u32),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    83
    SetTurnTime(u32),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    84
    SetMinesTime(u32),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    85
    SetWorldEdge(u8),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    86
    Draw,
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    87
    // TODO
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    88
    SetVoicePack(String),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    89
    AddHedgehog(String, u8, u32),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    90
    AddTeam(String, u8),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    91
    SetHedgehogCoordinates(i32, i32),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    92
    SetFort(String),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    93
    SetGrave(String),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    94
    SetHat(String),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    95
    SetFlag(String),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    96
    SetOwner(String),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    97
    SetOneClanMode(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    98
    SetMultishootMode(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    99
    SetSolidLand(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   100
    SetBorders(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   101
    SetDivideTeams(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   102
    SetLowGravity(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   103
    SetLaserSight(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   104
    SetInvulnerability(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   105
    SetHealthReset(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   106
    SetVampiric(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   107
    SetKarma(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   108
    SetArtilleryMode(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   109
    SetHedgehogSwitch(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   110
    SetRandomOrder(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   111
    SetKingMode(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   112
    SetPlaceHedgehog(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   113
    SetSharedAmmo(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   114
    SetGirdersEnabled(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   115
    SetLandObjectsEnabled(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   116
    SetAISurvivalMode(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   117
    SetInfiniteAttack(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   118
    SetResetWeapons(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   119
    SetAmmoPerHedgehog(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   120
    SetWindMode(u8),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   121
    SetTagTeam(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   122
    SetBottomBorder(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   123
    SetShoppaBorder(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   124
}
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   125
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   126
#[derive(Debug, PartialEq)]
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   127
pub enum EngineMessage {
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   128
    Unknown,
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   129
    Empty,
14235
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   130
    Synced(SyncedEngineMessage, u32),
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   131
    Unsynced(UnsyncedEngineMessage),
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   132
    Unordered(UnorderedEngineMessage),
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   133
    Config(ConfigEngineMessage),
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   134
}
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   135
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   136
macro_rules! em {
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   137
    [$msg: expr] => {
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   138
        vec![($msg) as u8]
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   139
    };
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   140
}
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   141
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   142
macro_rules! ems {
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   143
    [$msg: expr, $param: expr] => {
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   144
        {
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   145
            let mut v = vec![($msg) as u8];
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   146
            v.extend(String::into_bytes($param.to_string()).iter());
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   147
            v
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   148
        }
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   149
    };
14230
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   150
}
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   151
14235
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   152
impl SyncedEngineMessage {
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   153
    fn to_bytes(&self) -> Vec<u8> {
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   154
        use self::KeystrokeAction::*;
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   155
        use self::SyncedEngineMessage::*;
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   156
        match self {
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   157
            Left(Press) => em!['L'],
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   158
            Left(Release) => em!['l'],
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   159
            Right(Press) => em!['R'],
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   160
            Right(Release) => em!['r'],
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   161
            Up(Press) => em!['U'],
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   162
            Up(Release) => em!['u'],
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   163
            Down(Press) => em!['D'],
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   164
            Down(Release) => em!['d'],
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   165
            Precise(Press) => em!['Z'],
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   166
            Precise(Release) => em!['z'],
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   167
            Attack(Press) => em!['A'],
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   168
            Attack(Release) => em!['a'],
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   169
            NextTurn => em!['N'],
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   170
            Switch => em!['S'],
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   171
            Timer(t) => vec!['0' as u8 + t],
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   172
            Slot(s) => vec!['~' as u8, *s],
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   173
            SetWeapon(s) => vec!['~' as u8, *s],
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   174
            Put(x, y) => unimplemented!(),
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   175
            CursorMove(x, y) => unimplemented!(),
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   176
            HighJump => em!['J'],
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   177
            LongJump => em!['j'],
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   178
            Skip => em![','],
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   179
            TeamControlGained(str) => ems!['g', str],
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   180
            TeamControlLost(str) => ems!['f', str],
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   181
            Taunt(s) => vec!['t' as u8, *s],
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   182
            HogSay(str) => ems!['h', str],
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   183
            Heartbeat => em!['+'],
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   184
            TimeWrap => unreachable!(),
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   185
        }
14230
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   186
    }
14235
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   187
}
14230
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   188
14235
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   189
impl UnsyncedEngineMessage {
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   190
    fn to_bytes(&self) -> Vec<u8> {
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   191
        use self::UnsyncedEngineMessage::*;
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   192
        match self {
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   193
            TeamControlGained(str) => ems!['G', str],
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   194
            TeamControlLost(str) => ems!['F', str],
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   195
        }
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   196
    }
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   197
}
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   198
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   199
impl UnorderedEngineMessage {
14230
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   200
    fn to_bytes(&self) -> Vec<u8> {
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   201
        unimplemented!()
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   202
    }
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   203
}
14235
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   204
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   205
impl ConfigEngineMessage {
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   206
    fn to_bytes(&self) -> Vec<u8> {
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   207
        unimplemented!()
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   208
    }
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   209
}
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   210
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   211
impl EngineMessage {
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   212
    pub const MAX_LEN: u16 = 49215;
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   213
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   214
    fn to_unwrapped(&self) -> Vec<u8> {
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   215
        use self::EngineMessage::*;
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   216
        match self {
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   217
            Unknown => unreachable!("you're not supposed to construct such messages"),
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   218
            Empty => unreachable!("you're not supposed to construct such messages"),
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   219
            Synced(SyncedEngineMessage::TimeWrap, _) => vec!['#' as u8, 0xff, 0xff],
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   220
            Synced(msg, timestamp) => {
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   221
                let mut v = msg.to_bytes();
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   222
                v.push((*timestamp / 256) as u8);
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   223
                v.push(*timestamp as u8);
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   224
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   225
                v
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   226
            }
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   227
            Unsynced(msg) => msg.to_bytes(),
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   228
            Unordered(msg) => msg.to_bytes(),
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   229
            Config(msg) => msg.to_bytes(),
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   230
        }
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   231
    }
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   232
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   233
    pub fn to_bytes(&self) -> Vec<u8> {
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   234
        let mut unwrapped = self.to_unwrapped();
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   235
        let mut size = unwrapped.len();
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   236
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   237
        if size > EngineMessage::MAX_LEN as usize - 2 {
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   238
            size = EngineMessage::MAX_LEN as usize - 2;
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   239
            unwrapped.truncate(size);
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   240
        }
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   241
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   242
        if size < 64 {
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   243
            unwrapped.insert(0, size as u8);
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   244
        } else {
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   245
            size -= 64;
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   246
            unwrapped.insert(0, (size / 256 + 64) as u8);
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   247
            unwrapped.insert(1, size as u8);
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   248
        }
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   249
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   250
        unwrapped
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   251
    }
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   252
}
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   253
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   254
#[test]
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   255
fn message_contruction() {
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   256
    assert_eq!(
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   257
        EngineMessage::Synced(SyncedEngineMessage::TimeWrap, 0).to_bytes(),
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   258
        vec![3, '#' as u8, 255, 255]
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   259
    );
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   260
    assert_eq!(
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   261
        EngineMessage::Synced(SyncedEngineMessage::NextTurn, 258).to_bytes(),
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   262
        vec![3, 'N' as u8, 1, 2]
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   263
    );
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14232
diff changeset
   264
}