1use crate::rps::Weapon;
2use core::cmp::Ordering;
3
4#[repr(C)]
6#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]
7pub enum GameResult {
8 Win {
10 player: Weapon,
12 robo: Weapon,
14 },
15 Lose {
17 player: Weapon,
19 robo: Weapon,
21 },
22 Draw {
24 player: Weapon,
26 robo: Weapon,
28 },
29}
30
31#[repr(C)]
33#[derive(
34 Copy, Clone, Debug, serde::Serialize, serde::Deserialize, Eq, PartialEq, Ord, PartialOrd, Hash,
35)]
36pub struct RpsGame {
37 pub(crate) player: Weapon,
39 pub(crate) robo: Weapon,
41}
42
43impl RpsGame {
44 pub fn new_game(weapon: Weapon) -> Self {
47 Self {
48 player: weapon,
49
50 robo: Weapon::rand(),
51 }
52 }
53 pub fn result(self) -> GameResult {
55 self.into()
56 }
57}
58
59impl From<RpsGame> for GameResult {
60 fn from(val: RpsGame) -> Self {
61 match val.player.cmp(&val.robo) {
62 Ordering::Greater => GameResult::Win {
63 player: val.player,
64 robo: val.robo,
65 },
66 Ordering::Equal => GameResult::Draw {
67 player: val.player,
68 robo: val.robo,
69 },
70 Ordering::Less => GameResult::Lose {
71 player: val.player,
72 robo: val.robo,
73 },
74 }
75 }
76}