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
|
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
|
-
+
-
+
-
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
|
/// Data structure of the game.
pub struct Rps {
/// A HashSet with the players waiting to play as account strings.
lobby: HashSet<String>,
/// capacity determines how many people a match contains.
capacity: u8,
// A vector of ongoing matches.
/// A vector of ongoing matches.
matches: Vec<Match>,
// HashSet indicating for each player which match they are in.
/// HashSet indicating for each player which match they are in.
players: HashMap<String, usize>,
}
impl Game for Rps {
/// Creation of a new and empty Rps game structure.
fn new() -> Self {
fn new(
c: tokio::sync::mpsc::UnboundedSender<GameEvent>,
token: tokio_util::sync::CancellationToken,
) -> Self {
Rps {
lobby: HashSet::new(),
capacity: 2,
matches: Vec::new(),
players: HashMap::new(),
}
}
/// State machine that accepts a command, changes state and delivers replies if required.
fn next(&mut self, m: &Command) -> Reply {
let mut r = Reply::new();
let m = match m {
Command::PlayerCommand(c) => c,
Command::InternalCommand(_) => {
return r;
}
};
// The entire state depends on two factors: are we waiting to join a game, and are we playing a game?
// It is possible for both to be false, and either one to be true.
// If both are true, this is an error.
let waiting = self.lobby.contains(&m.sender);
let playing = self.players.contains_key(&m.sender);
assert!(!(waiting & playing));
// It is possible for a command to be: rps, a move in the game, or cancelrps.
let joining = m.content == "rps";
let quitting = m.content == "cancelrps";
let choice = match m.content.as_str() {
"rock" => Some(Play::Rock),
"paper" => Some(Play::Paper),
"scissors" => Some(Play::Scissors),
_ => None,
};
// At most, one of these conditions can hold.
assert!(!(joining && quitting));
assert!(!(joining && choice.is_some()));
assert!(!(quitting && choice.is_some()));
// At this point we have all necessary information to match.
/* println!(
"{}, {}, {:?}, {}, {}.",
joining, quitting, choice, playing, waiting
);
*/
match (joining, quitting, choice, playing, waiting) {
// We don't bother with the impossible cases that are already excluded by assertion.
// Let's start with joining the game.
// There are 3 cases we need care about:
// We're joining, not playing and not waiting.
(true, false, None, false, false) => {
// This has two cases.
|
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
|
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
|
-
+
+
+
+
+
+
+
+
-
+
+
+
+
+
+
+
+
-
-
-
+
+
+
+
+
-
-
-
+
+
+
-
-
-
+
+
+
+
+
+
+
-
-
-
+
-
-
-
+
+
+
+
+
+
+
-
-
-
-
+
+
+
+
+
-
-
-
+
+
+
+
+
-
-
-
-
-
+
+
-
-
-
+
-
-
-
+
+
+
+
+
-
-
-
-
+
-
-
-
-
+
-
-
-
-
-
-
+
+
+
+
-
-
-
+
+
+
+
+
-
-
-
+
-
-
-
+
+
+
+
+
-
-
-
-
+
-
-
-
+
-
+
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
}
}
}
/// Tests.
#[cfg(test)]
mod test {
use crate::game::{Command, Game};
use crate::game::{Command, Game, PlayerCommand, Reply};
use crate::rps::Rps;
fn command(g: &mut dyn Game, m: (String, String)) -> Reply {
let (sender, content) = m;
let p = PlayerCommand { sender, content };
let r = g.next(&Command::PlayerCommand(&p));
r
}
#[test]
fn test_new() {
let g = Rps::new();
let g = Rps::new(
tokio::sync::mpsc::unbounded_channel().0,
crate::CancellationToken::new(),
);
assert_eq!(g.capacity, 2);
assert_eq!(g.lobby.len(), 0);
}
#[test]
fn test_nonsense() {
let mut g = Rps::new(
tokio::sync::mpsc::unbounded_channel().0,
crate::CancellationToken::new(),
);
let c: Command = Command {
sender: "modulux@node.isonomia.net".to_string(),
content: "nonsense".to_string(),
let r = command(
&mut g,
(
"modulux@node.isonomia.net".to_string(),
"nonsense".to_string(),
};
let mut g = Rps::new();
assert!(g.next(&c).0.is_empty());
),
);
assert!(r.0.is_empty());
}
#[test]
fn test_first_join_game() {
let mut g = Rps::new();
let c = Command {
sender: "modulux@node.isonomia.net".to_string(),
let mut g = Rps::new(
tokio::sync::mpsc::unbounded_channel().0,
crate::CancellationToken::new(),
);
let r = command(
&mut g,
("modulux@node.isonomia.net".to_string(), "rps".to_string()),
content: "rps".to_string(),
};
let r = g.next(&c);
);
assert_eq!(r.0.len(), 1, "Incorrect number of replies: {}.", r.0.len());
assert_eq!(r.0[0], "@modulux@node.isonomia.net You've asked to join a game of Rock, Paper, Scissors. As soon as someone else wants to play, I'll send you a message so you can tell me your choice.".to_string(), "Incorrect reply message: {}.", r.0[0]);
assert_eq!(
g.capacity, 1,
"Capacity in lobby should be 1, is {}.",
g.capacity
);
}
#[test]
fn test_join_game_twice() {
let mut g = Rps::new();
let c = Command {
sender: "modulux@node.isonomia.net".to_string(),
let mut g = Rps::new(
tokio::sync::mpsc::unbounded_channel().0,
crate::CancellationToken::new(),
);
command(
&mut g,
("modulux@node.isonomia.net".to_string(), "rps".to_string()),
content: "rps".to_string(),
};
g.next(&c);
let r = g.next(&c);
);
let r = command(
&mut g,
("modulux@node.isonomia.net".to_string(), "rps".to_string()),
);
assert_eq!(r.0.len(), 1, "Incorrect number of replies: {:}.", r.0.len());
assert_eq!(r.0[0], "@modulux@node.isonomia.net You're already waiting for a game of Rock, Paper, Scissors. Be patient.".to_string(), "Incorrect reply message: {}.", r.0[0]);
assert_eq!(
g.capacity, 1,
"Capacity in lobby should be 1, is {}.",
g.capacity
);
}
#[test]
fn test_join_game_complete() {
let mut g = Rps::new();
let c1 = Command {
sender: "modulux@node.isonomia.net".to_string(),
let mut g = Rps::new(
tokio::sync::mpsc::unbounded_channel().0,
crate::CancellationToken::new(),
);
let c1 = ("modulux@node.isonomia.net".to_string(), "rps".to_string());
content: "rps".to_string(),
};
g.next(&c1);
let c2 = Command {
sender: "modulux2@node.isonomia.net".to_string(),
command(&mut g, c1);
let c2 = ("modulux2@node.isonomia.net".to_string(), "rps".to_string());
content: "rps".to_string(),
};
let r = g.next(&c2);
let r = command(&mut g, c2);
assert_eq!(r.0.len(), 2, "Incorrect number of replies: {}.", r.0.len());
assert!(r.0.contains(&"@modulux@node.isonomia.net Got a partner! Your opponent is modulux2@node.isonomia.net
Tell me your choice: *rock*, *paper*, or *scissors*?".to_string()), "Missing reply.");
assert!(r.0.contains(&"@modulux2@node.isonomia.net Got a partner! Your opponent is modulux@node.isonomia.net
Tell me your choice: *rock*, *paper*, or *scissors*?".to_string()), "Missing reply.");
}
#[test]
fn test_play_twice() {
let mut g = Rps::new();
let c1 = Command {
sender: "modulux@node.isonomia.net".to_string(),
let mut g = Rps::new(
tokio::sync::mpsc::unbounded_channel().0,
crate::CancellationToken::new(),
);
let c1 = ("modulux@node.isonomia.net".to_string(), "rps".to_string());
content: "rps".to_string(),
};
let c2 = Command {
sender: "modulux2@node.isonomia.net".to_string(),
let c2 = ("modulux2@node.isonomia.net".to_string(), "rps".to_string());
content: "rps".to_string(),
};
let c3 = Command {
sender: "modulux@node.isonomia.net".to_string(),
let c3 = ("modulux@node.isonomia.net".to_string(), "rock".to_string());
content: "rock".to_string(),
};
g.next(&c1);
g.next(&c2);
g.next(&c3);
let r = g.next(&c3);
command(&mut g, c1);
command(&mut g, c2);
command(&mut g, c3.clone());
let r = command(&mut g, c3);
assert_eq!(
r.0.len(),
1,
"Incorrect number of replies. Reply: {:#?}.",
r
);
assert_eq!(r.0[0], "@modulux@node.isonomia.net You already sent me your choice. You need to wait for modulux2@node.isonomia.net
If you get bored, you can send me *cancelrps* to cancel the game.", "Incorrect reply: {}.", r.0[0]);
}
#[test]
fn test_play_too_early() {
let mut g = Rps::new();
let c = Command {
sender: "modulux@node.isonomia.net".to_string(),
let mut g = Rps::new(
tokio::sync::mpsc::unbounded_channel().0,
crate::CancellationToken::new(),
);
let c = ("modulux@node.isonomia.net".to_string(), "rock".to_string());
content: "rock".to_string(),
};
let r = g.next(&c);
let r = command(&mut g, c);
assert_eq!(r.0.len(), 1, "Incorrect number of replies in: {:?}", r);
assert_eq!(r.0[0], "@modulux@node.isonomia.net You haven't joined a game yet. You can do so by sending me *rps* whenever you like.", "{:?}", r);
}
#[test]
fn twice_early_play() {
let mut r;
let mut g = Rps::new();
let c1 = Command {
sender: "modulux@node.isonomia.net".to_string(),
let mut g = Rps::new(
tokio::sync::mpsc::unbounded_channel().0,
crate::CancellationToken::new(),
);
let c1 = ("modulux@node.isonomia.net".to_string(), "rock".to_string());
content: "rock".to_string(),
};
let c2 = Command {
sender: "modulux2@node.isonomia.net".to_string(),
let c2 = ("modulux2@node.isonomia.net".to_string(), "rock".to_string());
content: "rock".to_string(),
};
r = g.next(&c1);
r = command(&mut g, c1);
assert_eq!(r.0.len(), 1, "Incorrect number of replies in: {:?}", r);
assert_eq!(r.0[0], "@modulux@node.isonomia.net You haven't joined a game yet. You can do so by sending me *rps* whenever you like.", "{:?}", r);
r = g.next(&c2);
r = command(&mut g, c2);
assert_eq!(r.0.len(), 1, "Incorrect number of replies in: {:?}", r);
assert_eq!(r.0[0], "@modulux2@node.isonomia.net You haven't joined a game yet. You can do so by sending me *rps* whenever you like.", "{:?}", r);
}
fn command(sender: &str, content: &str) -> Command {
Command {
sender: sender.to_string(),
content: content.to_string(),
}
}
#[test]
fn test_join_full_then_cancel() {
let r;
let mut g = Rps::new();
g.next(&command("modulux@node.isonomia.net", "rps"));
g.next(&command("modulux2@node.isonomia.net", "rps"));
r = g.next(&command("modulux@node.isonomia.net", "cancelrps"));
let mut g = Rps::new(
tokio::sync::mpsc::unbounded_channel().0,
crate::CancellationToken::new(),
);
command(
&mut g,
("modulux@node.isonomia.net".to_string(), "rps".to_string()),
);
command(
&mut g,
("modulux2@node.isonomia.net".to_string(), "rps".to_string()),
);
r = command(
&mut g,
(
"modulux@node.isonomia.net".to_string(),
"cancelrps".to_string(),
),
);
assert_eq!(r.0.len(), 1, "Incorrect number of replies: {:?}", r);
assert_eq!(
r.0[0],
"@modulux@node.isonomia.net has cancelled the game with @modulux2@node.isonomia.net
You're both welcome to play again any time. Use *rps* to start a new match.",
"Incorrect response: {}",
r.0[0]
);
}
#[test]
fn test_join_full_play_then_cancel() {
let r;
let mut g = Rps::new();
g.next(&command("modulux@node.isonomia.net", "rps"));
g.next(&command("modulux2@node.isonomia.net", "rps"));
g.next(&command("modulux@node.isonomia.net", "rock"));
r = g.next(&command("modulux@node.isonomia.net", "cancelrps"));
let mut g = Rps::new(
tokio::sync::mpsc::unbounded_channel().0,
crate::CancellationToken::new(),
);
command(
&mut g,
("modulux@node.isonomia.net".to_string(), "rps".to_string()),
);
command(
&mut g,
("modulux2@node.isonomia.net".to_string(), "rps".to_string()),
);
command(
&mut g,
("modulux@node.isonomia.net".to_string(), "rock".to_string()),
);
r = command(
&mut g,
(
"modulux@node.isonomia.net".to_string(),
"cancelrps".to_string(),
),
);
assert_eq!(r.0.len(), 1, "Incorrect length. {:?}", r);
assert_eq!(
r.0[0],
"@modulux@node.isonomia.net has cancelled the game with @modulux2@node.isonomia.net
You're both welcome to play again any time. Use *rps* to start a new match.",
"Incorrect response. {}.",
r.0[0]
);
}
}
|