-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.rs
1503 lines (1340 loc) · 61.8 KB
/
message.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! All communication is done via discrete messages, each having a type and zero or more key-value properties.
//! Three definitions below: protocol in abstract terms, encoding of the messages (currently JSON) and transport/framing (currently TCP/WebSocket).
//! Ping/pong and timeouts are handled in the transport, so there shouldn't be any messages doing that in the protocol.
//!
//! I'm including some data bits that aren't necessarily needed, but will make tuning the game logic easier
//! (because we won't have to change the client as well as the server), this can go but it shouldn't be too much of an issue —
//! all of those things will be constants to begin with.
//!
//! All speed/position values are in the same scale. Distance unit is the same as in HTML5 canvas, i.e. pixels. Time unit is a second.
//! Player vehicles are assumed to be squares, and the size is the square's side. Player bullets are assumed to be circles,
//! and the size is the circle's radius.
//!
//! See [#2](https://github.com/LoungeCPP/Tatsoryk/issues/2) for discussion.
//!
//!
//! # Encoding (JSON)
//!
//! To start with, we encode all messages as JSON objects, with the type ID being stored in `type` key,
//! and message properties being stored as another object in `data` key (and that object has key per property), e.g.
//!
//! ```json
//! {
//! "type": "world_state",
//! "data": {
//! "player_count": 32,
//! "alive_players": [
//! { "id": 1, "x": 34.66, "y": 21.44 },
//! { "id": 6, "x": 67.34, "y": 22.22 }
//! ]
//! }
//! }
//! ```
//!
//! The `data` key MAY be omitted if the message doesn't define any properties.
//! Optional properties MUST be omitted if they're not present (and not set to `null`).
//!
//! Newlines and indenting added for example purposes: all the exchanged messages SHOULD NOT contain any unnecessary whitespace.
//!
//! A message is malformed if it:
//! * contains unknown `type` value, or
//! * contains extra fields, or
//! * doesn't contain any required fields, or
//! * contains values of types differing from the specification, or
//! * doesn't decode properly (or violates JSON specification in any other way)
//!
//! All malformed messages MUST be rejected.
use std::str::FromStr;
use std::collections::BTreeMap;
use serde;
use serde_json;
/// Representation of discrete messages used for communication with the client.
///
/// Refer to the module-level documentation for more.
///
/// # Examples
///
/// Serialising a message for sending to a client:
///
/// ```
/// # let (id, x, y) = (0, 0, 0);
/// let message = Message::PlayerSpawned{
/// id: id,
/// x: x,
/// y: y,
/// }
/// let to_send = message.to_string();
/// ```
///
/// Deserialising a message received from a client:
///
/// ```
/// let msg_text = r#"{"type": "stop_moving"}"#.to_string(); // example
/// match str::parse(&msg_text) {
/// Ok(message: Message) => println!("Great! Message correct!"),
/// Err(error) => println!("Message malformed: {:?}", error),
/// }
/// ```
#[derive(Clone, Debug, PartialEq)]
pub enum Message {
/// **welcome** message, as defined by [Protocol spec](https://github.com/LoungeCPP/Tatsoryk/wiki/Protocol-spec)
///
/// **welcome** — sent by the server to a client, after the client successfully connects (what that means is defined by the transport) —
/// all data values apply to all players and are constant
/// - `id` (u32) — server-assigned ID of the player, MUST NOT change during the connection
/// - `speed` (f32) — speed of movement of player ships
/// - `size` (f32) — size of the player vehicle
/// - `bullet_speed` (f32) — speed of movement of player bullets
/// - `bullet_size` (f32) — size of the player bullets
Welcome {
id: u32,
speed: f32,
size: f32,
bullet_speed: f32,
bullet_size: f32,
},
/// **go_away** message, as defined by [Protocol spec](https://github.com/LoungeCPP/Tatsoryk/wiki/Protocol-spec)
///
/// **go_away** — sent by the server if it rejects/terminates client connection for any reason
/// - `reason` (str) — a message to be displayed to the user
GoAway {
reason: String,
},
/// **player_joined** message, as defined by [Protocol spec](https://github.com/LoungeCPP/Tatsoryk/wiki/Protocol-spec)
///
/// **player_joined** — sent by the server to all connected clients when a new player joins the game.
/// - `id` (u32) — server-assigned ID of the player
PlayerJoined {
id: u32,
},
/// **player_left** message, as defined by [Protocol spec](https://github.com/LoungeCPP/Tatsoryk/wiki/Protocol-spec)
///
/// **player_left** — sent by the server to all connected clients when a player disconnects
/// - `id` (u32) — ID of the player that just left; server MAY recycle this ID, and client MUST be ready for that
PlayerLeft {
id: u32,
},
/// **shots_fired** message, as defined by [Protocol spec](https://github.com/LoungeCPP/Tatsoryk/wiki/Protocol-spec)
///
/// **shots_fired** — sent by the server to all connected clients when a player fires a bullet
/// (I'm giving bullets their own ID to make them easier to despawn but honestly not sure if that's the best of ideas)
/// - `id` (u32) — ID of the shooting player
/// - `bullet_id` (u32) — ID of the bullet; server MAY recycle this ID, and client MUST be ready for that
/// - `x` (f32) — position X of the player at the moment of firing (center)
/// - `y` (f32) — position Y of the player at the moment of firing (center)
/// - `aim_x` (f32) — player's aiming vector X at the moment of firing
/// - `aim_y` (f32) — player's aiming direction vector Y at the moment of firing
/// (aiming direction vector MUST be normalised, i.e. its magnitude MUST be equal to 1)
ShotsFired {
id: u32,
bullet_id: u32,
x: f32,
y: f32,
aim_x: f32,
aim_y: f32,
},
/// **player_spawned** message, as defined by [Protocol spec](https://github.com/LoungeCPP/Tatsoryk/wiki/Protocol-spec)
///
/// **player_spawned** — sent by the server to all connected clients when a player (re)spawns on the map
/// - `id` (u32) — ID of the player
/// - `x` (f32) — position X of the player vehicle (center)
/// - `y` (f32) — position Y of the player vehicle (center)
PlayerSpawned {
id: u32,
x: f32,
y: f32,
},
/// **player_destroyed** message, as defined by [Protocol spec](https://github.com/LoungeCPP/Tatsoryk/wiki/Protocol-spec)
///
/// **player_destroyed** — sent by the server to all connected clients when a player despawns from the map
/// - `id` (u32) — ID of the player
/// - `killer_id` (Option<u32>) — ID of the killer, if any
/// - `bullet_id` (Option<u32>) — ID of the bullet, if any; MUST be present if `killer_id` is present
PlayerDestroyed {
id: u32,
killer_id: Option<u32>,
bullet_id: Option<u32>,
},
/// **player_moving** message, as defined by [Protocol spec](https://github.com/LoungeCPP/Tatsoryk/wiki/Protocol-spec)
///
/// **player_moving** — sent by the server to all connected clients when a player starts moving
/// - `id` (u32) — ID of the player
/// - `x` (f32) — position X of the player when they started to move (center)
/// - `y` (f32) — position Y of the player when they started to move (center)
/// - `move_x` (f32) — player's movement vector X
/// - `move_y` (f32) — player's movement vector Y (movement vector MUST be normalised)
PlayerMoving {
id: u32,
x: f32,
y: f32,
move_x: f32,
move_y: f32,
},
/// **player_stopped** message, as defined by [Protocol spec](https://github.com/LoungeCPP/Tatsoryk/wiki/Protocol-spec)
///
/// **player_stopped** — sent by the server to all connected clients when a player stops moving
/// - `id` (u32) — ID of the player
/// - `x` (f32) — final position X of the player (center)
/// - `y` (f32) — final position Y of the player (center)
PlayerStopped {
id: u32,
x: f32,
y: f32,
},
/// **world_state** message, as defined by [Protocol spec](https://github.com/LoungeCPP/Tatsoryk/wiki/Protocol-spec)
///
/// **world_state** — full update of the world, sent by the server to all connected clients periodically (interval up to the implementation)
/// - `player_count` (u32) — count of all connected players
/// - `alive_players` (Player[]) — an array of all currently alive players, each containing:
/// - `id` (u32) — ID of the player
/// - `x` (f32) — current position X of the player
/// - `y` (f32) — current position Y of the player
/// - `move_x` (Optional<f32>) — current movement vector X of the player, if player is moving
/// - `move_y` (Optional<f32>) — current movement vector Y of the player, if player is moving
/// - `alive_bullets` (Bullet[]) — an array of all currently alive bullets, each containing:
/// - `id` (u32) — ID of the bullet
/// - `x` (f32) — current position X of the bullet
/// - `y` (f32) — current position Y of the bullet
/// - `move_x` (f32) — current movement vector X of the bullet
/// - `move_y` (f32) — current movement direction vector Y of the bullet
/// (movement direction vectors MUST be normalised, i.e. their magnitude MUST be equal to 1)
WorldState,
/// **start_moving** message, as defined by [Protocol spec](https://github.com/LoungeCPP/Tatsoryk/wiki/Protocol-spec)
///
/// **start_moving** — sent by the client to the server when the player wants to start moving or change its movement direction
/// (i.e. presses/releases one or more movement keys, as long as at least one of them is still held)
/// - `move_x` (f32) — player's movement vector X
/// - `move_y` (f32) — player's movement vector Y
/// (movement vector SHOULD be normalised, but the server MUST NOT assume that it is)
StartMoving {
move_x: f32,
move_y: f32,
},
/// **stop_moving** message, as defined by [Protocol spec](https://github.com/LoungeCPP/Tatsoryk/wiki/Protocol-spec)
///
/// **stop_moving** — sent by the client to the server when the player wants to stop moving (i.e. releases held movement keys)
StopMoving,
/// **fire** message, as defined by [Protocol spec](https://github.com/LoungeCPP/Tatsoryk/wiki/Protocol-spec)
///
/// **fire** — sent by the client to the server when the player wants to fire (i.e. presses the mouse button)
/// - `move_x` (f32) — player's aiming vector X
/// - `move_y` (f32) — player's aiming direction vector Y (aiming direction vector SHOULD be normalised, but the server MUST NOT assume that it is)
Fire {
move_x: f32,
move_y: f32,
},
}
impl ToString for Message {
fn to_string(&self) -> String {
let mut values = BTreeMap::new();
let msg_type = match self {
&Message::Welcome{id, speed, size, bullet_speed, bullet_size} => {
add_data_id_speeds_sizes_entries(&mut values,
id,
speed,
size,
bullet_speed,
bullet_size);
"welcome"
}
&Message::GoAway{ref reason} => {
add_data_entry(&mut values, "reason", &reason);
"go_away"
}
&Message::PlayerJoined{id} => {
add_data_entry(&mut values, "id", &id);
"player_joined"
}
&Message::PlayerLeft{id} => {
add_data_entry(&mut values, "id", &id);
"player_left"
}
&Message::ShotsFired{id, bullet_id, x, y, aim_x, aim_y} => {
add_shot_data_entries(&mut values, id, bullet_id, x, y, aim_x, aim_y);
"shots_fired"
}
&Message::PlayerSpawned{id, x, y} => {
add_data_id_pos_entries(&mut values, id, x, y);
"player_spawned"
}
&Message::PlayerDestroyed{id, killer_id, bullet_id} => {
add_data_entry(&mut values, "id", &id);
match (killer_id, bullet_id) {
(Some(killer_id), Some(bullet_id)) => {
add_data_entry(&mut values, "killer_id", &killer_id);
add_data_entry(&mut values, "bullet_id", &bullet_id);
}
(None, None) => {}
_ => panic!("killer_id and bullet_id must be either both Some or both None"),
}
"player_destroyed"
}
&Message::PlayerMoving{id, x, y, move_x, move_y} => {
add_data_id_pos_moves_entries(&mut values, id, x, y, move_x, move_y);
"player_moving"
}
&Message::PlayerStopped{id, x, y} => {
add_data_id_pos_entries(&mut values, id, x, y);
"player_stopped"
}
&Message::WorldState => "world_state", //TODO
&Message::StartMoving{move_x, move_y} => {
add_data_move_entries(&mut values, move_x, move_y);
"start_moving"
}
&Message::StopMoving => "stop_moving",
&Message::Fire{move_x, move_y} => {
add_data_move_entries(&mut values, move_x, move_y);
"fire"
}
};
let mut root_obj = BTreeMap::new();
let _ = root_obj.insert("type".to_string(),
serde_json::Value::String(msg_type.to_string()));
if !values.is_empty() {
let _ = root_obj.insert("data".to_string(), serde_json::Value::Object(values));
}
serde_json::to_string(&serde_json::Value::Object(root_obj)).unwrap()
}
}
impl FromStr for Message {
type Err = MessageError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let json: serde_json::Value = try!(serde_json::from_str(s));
match json.as_object() {
Some(msg) => {
let msg_type = try!(match msg.get("type") {
None => Err(MessageError::PropertyMissing(r#"Top-level Object doesn't have "type""#.to_string())),
Some(msg_type) => {
match msg_type {
&serde_json::Value::String(ref msg_type) => Ok(msg_type),
_ => {
Err(MessageError::BadType(r#"Message type not String"#.to_string()))
}
}
}
});
let keys = msg.keys().collect::<Vec<_>>();
// TODO: implement world_state
if msg_type == "stop_moving" || msg_type == "world_state" {
if keys != vec!["data", "type"] && keys != vec!["type"] {
return Err(MessageError::PropertyMissing(format!(r#"Top-level Object is a mismatch for `{{"type"[, "data"]}}`: {:?}"#, keys)));
}
} else if keys != vec!["data", "type"] {
return Err(MessageError::PropertyMissing(format!(r#"Top-level Object is a mismatch for `{{"type", "data"}}`: {:?}"#, keys)));
}
match msg.get("data") {
None => {
if msg_type == "stop_moving" {
Ok(Message::StopMoving)
} else if msg_type == "world_state" {
// TODO: implement WorldState
Ok(Message::WorldState)
} else {
Err(MessageError::PropertyMissing(r#"Top-level Object doesn't have "data""#.to_string()))
}
}
Some(data) => {
match data.as_object() {
None => {
Err(MessageError::BadType(r#"Top-level "data" not an Object"#
.to_string()))
}
Some(data) => {
// TODO: implement WorldState
if (msg_type == "stop_moving" || msg_type == "world_state") &&
!data.is_empty() {
return Err(MessageError::ExtraneousProperty(r#"Non-empty "data" for dataless message"#.to_string()));
}
match &msg_type[..] {
"welcome" => {
let (id, speed, size, bullet_speed, bullet_size) =
try!(decompose_stats(&data));
Ok(Message::Welcome {
id: id,
speed: speed,
size: size,
bullet_speed: bullet_speed,
bullet_size: bullet_size,
})
}
"go_away" => {
Ok(Message::GoAway {
reason: try!(decompose_reason(&data)),
})
}
"player_joined" => {
Ok(Message::PlayerJoined { id: try!(decompose_id(&data)) })
}
"player_left" => {
Ok(Message::PlayerLeft { id: try!(decompose_id(&data)) })
}
"shots_fired" => {
let (id, bullet_id, x, y, aim_x, aim_y) =
try!(decompose_shot(&data));
Ok(Message::ShotsFired {
id: id,
bullet_id: bullet_id,
x: x,
y: y,
aim_x: aim_x,
aim_y: aim_y,
})
}
"player_spawned" => {
let (id, x, y) = try!(decompose_id_pos(&data));
Ok(Message::PlayerSpawned {
id: id,
x: x,
y: y,
})
}
"player_destroyed" => {
let (id, killer_id, bullet_id) =
try!(decompose_destruction(&data));
Ok(Message::PlayerDestroyed {
id: id,
killer_id: killer_id,
bullet_id: bullet_id,
})
}
"player_moving" => {
let (id, x, y, move_x, move_y) =
try!(decompose_id_pos_moves(&data));
Ok(Message::PlayerMoving {
id: id,
x: x,
y: y,
move_x: move_x,
move_y: move_y,
})
}
"player_stopped" => {
let (id, x, y) = try!(decompose_id_pos(&data));
Ok(Message::PlayerStopped {
id: id,
x: x,
y: y,
})
}
"world_state" => Ok(Message::WorldState), // TODO
"start_moving" => {
let (move_x, move_y) = try!(decompose_moves(&data));
Ok(Message::StartMoving {
move_x: move_x,
move_y: move_y,
})
}
"stop_moving" => Ok(Message::StopMoving),
"fire" => {
let (move_x, move_y) = try!(decompose_moves(&data));
Ok(Message::Fire {
move_x: move_x,
move_y: move_y,
})
}
msg_type => Err(MessageError::BadType(format!(r#"Expected any of {:?}, got: {:?}"#,
vec!["welcome", "go_away", "player_joined", "player_left",
"shots_fired", "player_spawned", "player_destroyed", "player_moving",
"player_stopped", "world_state", "start_moving", "stop_moving", "fire"],
msg_type))),
}
}
}
}
}
}
None => Err(MessageError::BadType("Top-level JSON not an Object".to_string())),
}
}
}
#[derive(Debug)]
pub enum MessageError {
JsonError(serde_json::Error),
PropertyMissing(String),
ExtraneousProperty(String),
BadType(String),
}
impl From<serde_json::Error> for MessageError {
fn from(sje: serde_json::Error) -> Self {
MessageError::JsonError(sje)
}
}
fn add_data_id_speeds_sizes_entries(data: &mut BTreeMap<String, serde_json::Value>,
id: u32,
speed: f32,
size: f32,
bullet_speed: f32,
bullet_size: f32) {
add_data_entry(data, "id", &id);
add_data_entry(data, "speed", &speed);
add_data_entry(data, "size", &size);
add_data_entry(data, "bullet_speed", &bullet_speed);
add_data_entry(data, "bullet_size", &bullet_size);
}
fn add_data_id_pos_moves_entries(data: &mut BTreeMap<String, serde_json::Value>,
id: u32,
x: f32,
y: f32,
move_x: f32,
move_y: f32) {
add_data_id_pos_entries(data, id, x, y);
add_data_move_entries(data, move_x, move_y);
}
fn add_data_id_pos_entries(data: &mut BTreeMap<String, serde_json::Value>,
id: u32,
x: f32,
y: f32) {
add_data_entry(data, "id", &id);
add_data_entry(data, "x", &x);
add_data_entry(data, "y", &y);
}
fn add_data_move_entries(data: &mut BTreeMap<String, serde_json::Value>,
move_x: f32,
move_y: f32) {
add_data_entry(data, "move_x", &move_x);
add_data_entry(data, "move_y", &move_y);
}
fn add_shot_data_entries(data: &mut BTreeMap<String, serde_json::Value>,
id: u32,
bullet_id: u32,
x: f32,
y: f32,
aim_x: f32,
aim_y: f32) {
add_data_entry(data, "id", &id);
add_data_entry(data, "bullet_id", &bullet_id);
add_data_entry(data, "x", &x);
add_data_entry(data, "y", &y);
add_data_entry(data, "aim_x", &aim_x);
add_data_entry(data, "aim_y", &aim_y);
}
fn add_data_entry<T: serde::Serialize>(data: &mut BTreeMap<String, serde_json::Value>,
name: &str,
what: &T) {
let _ = data.insert(name.to_string(), serde_json::to_value(what));
}
fn decompose_moves(data: &BTreeMap<String, serde_json::Value>) -> Result<(f32, f32), MessageError> {
try!(decompose_assert_size(data.len(), 2));
try!(decompose_assert_keys(data.keys().collect::<Vec<_>>(), vec!["move_x", "move_y"]));
Ok((try!(unpack_f32(data.get("move_x").unwrap())),
try!(unpack_f32(data.get("move_y").unwrap()))))
}
fn decompose_id_pos(data: &BTreeMap<String, serde_json::Value>)
-> Result<(u32, f32, f32), MessageError> {
try!(decompose_assert_size(data.len(), 3));
try!(decompose_assert_keys(data.keys().collect::<Vec<_>>(), vec!["id", "x", "y"]));
Ok((try!(unpack_u32(data.get("id").unwrap())),
try!(unpack_f32(data.get("x").unwrap())),
try!(unpack_f32(data.get("y").unwrap()))))
}
fn decompose_stats(data: &BTreeMap<String, serde_json::Value>)
-> Result<(u32, f32, f32, f32, f32), MessageError> {
try!(decompose_assert_size(data.len(), 5));
try!(decompose_assert_keys(data.keys().collect::<Vec<_>>(),
vec!["bullet_size", "bullet_speed", "id", "size", "speed"]));
Ok((try!(unpack_u32(data.get("id").unwrap())),
try!(unpack_f32(data.get("speed").unwrap())),
try!(unpack_f32(data.get("size").unwrap())),
try!(unpack_f32(data.get("bullet_speed").unwrap())),
try!(unpack_f32(data.get("bullet_size").unwrap()))))
}
fn decompose_reason(data: &BTreeMap<String, serde_json::Value>) -> Result<String, MessageError> {
try!(decompose_assert_size(data.len(), 1));
try!(decompose_assert_keys(data.keys().collect::<Vec<_>>(), vec!["reason"]));
Ok(try!(unpack_str(data.get("reason").unwrap())))
}
fn decompose_id(data: &BTreeMap<String, serde_json::Value>) -> Result<u32, MessageError> {
try!(decompose_assert_size(data.len(), 1));
try!(decompose_assert_keys(data.keys().collect::<Vec<_>>(), vec!["id"]));
Ok(try!(unpack_u32(data.get("id").unwrap())))
}
fn decompose_shot(data: &BTreeMap<String, serde_json::Value>)
-> Result<(u32, u32, f32, f32, f32, f32), MessageError> {
try!(decompose_assert_size(data.len(), 6));
try!(decompose_assert_keys(data.keys().collect::<Vec<_>>(),
vec!["aim_x", "aim_y", "bullet_id", "id", "x", "y"]));
Ok((try!(unpack_u32(data.get("id").unwrap())),
try!(unpack_u32(data.get("bullet_id").unwrap())),
try!(unpack_f32(data.get("x").unwrap())),
try!(unpack_f32(data.get("y").unwrap())),
try!(unpack_f32(data.get("aim_x").unwrap())),
try!(unpack_f32(data.get("aim_y").unwrap()))))
}
fn decompose_destruction(data: &BTreeMap<String, serde_json::Value>)
-> Result<(u32, Option<u32>, Option<u32>), MessageError> {
match data.len() {
1 => Ok((try!(decompose_id(data)), None, None)),
3 => {
try!(decompose_assert_keys(data.keys().collect::<Vec<_>>(),
vec!["bullet_id", "id", "killer_id"]));
Ok((try!(unpack_u32(data.get("id").unwrap())),
Some(try!(unpack_u32(data.get("killer_id").unwrap()))),
Some(try!(unpack_u32(data.get("bullet_id").unwrap())))))
}
len => {
if len > 3 {
Err(MessageError::ExtraneousProperty(format!(r#"Expected 1 or 3, got {}"#, len)))
} else {
Err(MessageError::PropertyMissing(format!(r#"Expected 1 or 3, got {}"#, len)))
}
}
}
}
fn decompose_id_pos_moves(data: &BTreeMap<String, serde_json::Value>)
-> Result<(u32, f32, f32, f32, f32), MessageError> {
try!(decompose_assert_size(data.len(), 5));
try!(decompose_assert_keys(data.keys().collect::<Vec<_>>(),
vec!["id", "move_x", "move_y", "x", "y"]));
Ok((try!(unpack_u32(data.get("id").unwrap())),
try!(unpack_f32(data.get("x").unwrap())),
try!(unpack_f32(data.get("y").unwrap())),
try!(unpack_f32(data.get("move_x").unwrap())),
try!(unpack_f32(data.get("move_y").unwrap()))))
}
fn decompose_assert_size(len: usize, expected: usize) -> Result<(), MessageError> {
if len > expected {
return Err(MessageError::ExtraneousProperty(format!(r#"Expected {}, got {}"#,
expected,
len)));
} else if len < expected {
return Err(MessageError::PropertyMissing(format!(r#"Expected {}, got {}"#, expected, len)));
} else {
Ok(())
}
}
fn decompose_assert_keys(keys: Vec<&String>,
expected: Vec<&'static str>)
-> Result<(), MessageError> {
if keys != expected {
return Err(MessageError::ExtraneousProperty(format!(r#"Data Object is a mismatch for {:?}: {:?}"#, expected, keys)));
} else {
Ok(())
}
}
fn unpack_f32(val: &serde_json::Value) -> Result<f32, MessageError> {
match val {
&serde_json::Value::F64(f) => Ok(f as f32),
&serde_json::Value::I64(i) => Ok(i as f32),
&serde_json::Value::U64(u) => Ok(u as f32),
_ => Err(MessageError::BadType("Expected f32-compatible type".to_string())),
}
}
fn unpack_u32(val: &serde_json::Value) -> Result<u32, MessageError> {
match val {
&serde_json::Value::I64(i) => Ok(i as u32),
&serde_json::Value::U64(u) => Ok(u as u32),
_ => Err(MessageError::BadType("Expected u32-compatible type".to_string())),
}
}
fn unpack_str(val: &serde_json::Value) -> Result<String, MessageError> {
match val {
&serde_json::Value::String(ref s) => Ok(s.clone()),
_ => Err(MessageError::BadType("Expected String".to_string())),
}
}
#[cfg(test)]
mod tests {
extern crate rand;
use std::iter::FromIterator;
use std::collections::BTreeMap;
use self::rand::Rng;
use serde_json::Value;
mod ser {
use self::super::*;
use self::super::rand::{Rng, thread_rng};
use self::super::super::Message;
use serde_json::{self, Value};
#[test]
fn welcome_serializes_properly() {
let mut rng = thread_rng();
let id: u32 = rng.gen();
let speed = gen_f32(&mut rng);
let size = gen_f32(&mut rng);
let bullet_speed = gen_f32(&mut rng);
let bullet_size = gen_f32(&mut rng);
let json_txt = Message::Welcome {
id: id,
speed: speed,
size: size,
bullet_speed: bullet_speed,
bullet_size: bullet_size,
}
.to_string();
assert_eq!(serde_json::from_str::<Value>(&json_txt).unwrap(),
welcome_expected_json(id, speed, size, bullet_speed, bullet_size));
}
#[test]
fn go_away_serializes_properly() {
let mut rng = thread_rng();
let reason: String = {
let len = rng.gen_range(1, 100);
rng.gen_ascii_chars().take(len).collect()
};
let json_txt = Message::GoAway { reason: reason.clone() }.to_string();
assert_eq!(serde_json::from_str::<Value>(&json_txt).unwrap(),
go_away_expected_json(reason));
}
#[test]
fn player_joined_serializes_properly() {
let mut rng = thread_rng();
let id: u32 = rng.gen();
let json_txt = Message::PlayerJoined { id: id }.to_string();
assert_eq!(serde_json::from_str::<Value>(&json_txt).unwrap(),
player_joined_expected_json(id));
}
#[test]
fn player_left_serializes_properly() {
let mut rng = thread_rng();
let id: u32 = rng.gen();
let json_txt = Message::PlayerLeft { id: id }.to_string();
assert_eq!(serde_json::from_str::<Value>(&json_txt).unwrap(),
player_left_expected_json(id));
}
#[test]
fn shots_fired_serializes_properly() {
let mut rng = thread_rng();
let id: u32 = rng.gen();
let bullet_id: u32 = rng.gen();
let x = gen_f32(&mut rng);
let y = gen_f32(&mut rng);
let aim_x = gen_f32(&mut rng);
let aim_y = gen_f32(&mut rng);
let json_txt = Message::ShotsFired {
id: id,
bullet_id: bullet_id,
x: x,
y: y,
aim_x: aim_x,
aim_y: aim_y,
}
.to_string();
assert_eq!(serde_json::from_str::<Value>(&json_txt).unwrap(),
shots_fired_expected_json(id, bullet_id, x, y, aim_x, aim_y));
}
#[test]
fn player_spawned_serializes_properly() {
let mut rng = thread_rng();
let id: u32 = rng.gen();
let x = gen_f32(&mut rng);
let y = gen_f32(&mut rng);
let json_txt = Message::PlayerSpawned {
id: id,
x: x,
y: y,
}
.to_string();
assert_eq!(serde_json::from_str::<Value>(&json_txt).unwrap(),
player_spawned_expected_json(id, x, y));
}
#[test]
fn player_destroyed_no_killer_serializes_properly() {
let mut rng = thread_rng();
let id: u32 = rng.gen();
let json_txt = Message::PlayerDestroyed {
id: id,
killer_id: None,
bullet_id: None,
}
.to_string();
assert_eq!(serde_json::from_str::<Value>(&json_txt).unwrap(),
player_destroyed_no_killer_expected_json(id));
}
#[test]
fn player_destroyed_with_killer_serializes_properly() {
let mut rng = thread_rng();
let id: u32 = rng.gen();
let killer_id: u32 = rng.gen();
let bullet_id: u32 = rng.gen();
let json_txt = Message::PlayerDestroyed {
id: id,
killer_id: Some(killer_id),
bullet_id: Some(bullet_id),
}
.to_string();
assert_eq!(serde_json::from_str::<Value>(&json_txt).unwrap(),
player_destroyed_with_killer_expected_json(id, killer_id, bullet_id));
}
#[test]
#[should_panic]
fn player_destroyed_with_killer_no_bullet_panics() {
let mut rng = thread_rng();
let id: u32 = rng.gen();
let killer_id: u32 = rng.gen();
let _ = Message::PlayerDestroyed {
id: id,
killer_id: Some(killer_id),
bullet_id: None,
}
.to_string();
}
#[test]
#[should_panic]
fn player_destroyed_with_bullet_no_killer_panics() {
let mut rng = thread_rng();
let id: u32 = rng.gen();
let bullet_id: u32 = rng.gen();
let _ = Message::PlayerDestroyed {
id: id,
killer_id: None,
bullet_id: Some(bullet_id),
}
.to_string();
}
#[test]
fn player_moving_serializes_properly() {
let mut rng = thread_rng();
let id: u32 = rng.gen();
let x = gen_f32(&mut rng);
let y = gen_f32(&mut rng);
let move_x = gen_f32(&mut rng);
let move_y = gen_f32(&mut rng);
let json_txt = Message::PlayerMoving {
id: id,
x: x,
y: y,
move_x: move_x,
move_y: move_y,
}
.to_string();
assert_eq!(serde_json::from_str::<Value>(&json_txt).unwrap(),
player_moving_expected_json(id, x, y, move_x, move_y));
}
#[test]
fn player_stopped_serializes_properly() {
let mut rng = thread_rng();
let id: u32 = rng.gen();
let x = gen_f32(&mut rng);
let y = gen_f32(&mut rng);
let json_txt = Message::PlayerStopped {
id: id,
x: x,
y: y,
}
.to_string();
assert_eq!(serde_json::from_str::<Value>(&json_txt).unwrap(),
player_stopped_expected_json(id, x, y));
}
#[test]
fn world_state_serializes_properly() {
// TODO implement WorldState
assert_eq!(serde_json::from_str::<Value>(&Message::WorldState.to_string()).unwrap(),
world_state_expected_json());
}
#[test]
fn start_moving_serializes_properly() {
let mut rng = thread_rng();
let move_x = gen_f32(&mut rng);
let move_y = gen_f32(&mut rng);
let json_txt = Message::StartMoving {
move_x: move_x,
move_y: move_y,
}
.to_string();
assert_eq!(serde_json::from_str::<Value>(&json_txt).unwrap(),
start_moving_expected_json(move_x, move_y));
}
#[test]
fn stop_moving_serializes_properly() {
let json_txt = Message::StopMoving.to_string();
assert_eq!(serde_json::from_str::<Value>(&json_txt).unwrap(),
stop_moving_expected_json());
}
#[test]
fn fire_serializes_properly() {
let mut rng = thread_rng();
let move_x = gen_f32(&mut rng);
let move_y = gen_f32(&mut rng);
let json_txt = Message::Fire {
move_x: move_x,
move_y: move_y,
}
.to_string();
assert_eq!(serde_json::from_str::<Value>(&json_txt).unwrap(),
fire_expected_json(move_x, move_y));
}
}
mod de {
mod correct {
use self::super::super::*;
use self::super::super::rand::{Rng, thread_rng};
use self::super::super::super::Message;
use serde_json;
#[test]
fn welcome_deserializes_properly() {
let mut rng = thread_rng();
let id: u32 = rng.gen();
let speed = gen_f32(&mut rng);
let size = gen_f32(&mut rng);
let bullet_speed = gen_f32(&mut rng);
let bullet_size = gen_f32(&mut rng);
let expected_message = Message::Welcome {
id: id,
speed: speed,
size: size,
bullet_speed: bullet_speed,
bullet_size: bullet_size,
};
assert_eq!(str::parse::<Message>(&serde_json::to_string(&welcome_expected_json(id, speed, size, bullet_speed, bullet_size))
.unwrap())
.unwrap(),
expected_message);
}
#[test]
fn go_away_deserializes_properly() {
let mut rng = thread_rng();
let reason: String = {
let len = rng.gen_range(1, 100);
rng.gen_ascii_chars().take(len).collect()
};
let expected_message = Message::GoAway { reason: reason.clone() };
assert_eq!(str::parse::<Message>(&serde_json::to_string(&go_away_expected_json(reason))
.unwrap())
.unwrap(),
expected_message);
}
#[test]
fn player_joined_deserializes_properly() {
let mut rng = thread_rng();
let id: u32 = rng.gen();
let expected_message = Message::PlayerJoined { id: id };
assert_eq!(str::parse::<Message>(&serde_json::to_string(&player_joined_expected_json(id))
.unwrap())
.unwrap(),