DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

ref: 49a7a70c12a34314d776ea936c0d9d89fcf4679c deck-builder/client/src/api/game/mod.rs -rw-r--r-- 3.4 KiB
49a7a70cJonni Liljamo feat(client): show what the user rolled in log 1 year, 7 months ago
                                                                                
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
/*
 * This file is part of laurelin_client
 * Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
 *
 * Licensed under GPL-3.0-only.
 * See LICENSE for licensing information.
 */

use bevy::reflect::{FromReflect, Reflect};
use serde::{Deserialize, Serialize};
use serde_repr::Deserialize_repr;

use crate::{
    game_status::{Card, PlayerState},
    seed_gen,
};

use super::user::User;

mod forming;
pub use forming::*;

mod my_games;
pub use my_games::*;

mod join;
pub use join::*;

mod create;
pub use create::*;

mod create_action;
pub use create_action::*;

mod details;
pub use details::*;

#[derive(Deserialize, Serialize, Clone, PartialEq)]
pub enum Command {
    InitSupplyPile {
        card: Card,
        amount: usize,
    },
    /// take a card from pile N
    TakeFromPile {
        index: usize,
        for_cost: usize,
    },
    /// play card from hand index N
    PlayCard {
        index: usize,
    },
    /// draw N amount of cards from deck
    Draw {
        amount: usize,
    },
    /// discard card from hand in slot N
    Discard {
        index: usize,
    },
    /// end the invokers turn, and invoke StartTurn for the target
    EndTurn {},
    /// start the targets turn
    StartTurn {},
    /// change player state to another
    ChangePlayerState {
        state: PlayerState,
    },
    RollForCurrency {
        amount: usize,
        sides: usize,
    },
    GivePlays {
        amount: usize,
    },
    GiveBuys {
        amount: usize,
    },
    GiveVP {
        amount: usize,
    },
    /// mark a card in the targets deck to be trashed on play.
    /// if index in None, a random card will be chosen
    MarkCardInDeckToBeTrashed {
        index: Option<usize>,
    },
}

#[derive(Deserialize, Serialize, Clone, PartialEq)]
pub struct Action {
    pub id: String,
    pub created_at: chrono::DateTime<chrono::Utc>,
    pub updated_at: chrono::DateTime<chrono::Utc>,
    pub game_id: String,
    pub invoker: String,
    pub target: String,
    pub command: Command,
    /// it's actually u64, but we handle it as a string to make psql happy
    pub seed: String,
}

impl Action {
    pub fn new(
        game_id: &str,
        invoker: &str,
        target: &str,
        command: &Command,
        seed: Option<u64>,
    ) -> Self {
        let seed = if seed.is_none() {
            seed_gen!()
        } else {
            seed.unwrap()
        };

        Self {
            id: "".to_string(),
            created_at: chrono::Utc::now(),
            updated_at: chrono::Utc::now(),
            game_id: game_id.to_string(),
            invoker: invoker.to_string(),
            target: target.to_string(),
            command: command.clone(),
            seed: seed.to_string(),
        }
    }
}

#[derive(Debug, Deserialize_repr, Clone, PartialEq, Reflect, FromReflect)]
#[repr(u8)]
pub enum GameState {
    Forming = 0,
    InProgress = 1,
    Finished = 2,
    Cancelled = 3,
}

#[derive(Deserialize, Clone, Reflect, FromReflect)]
pub struct Game {
    pub id: String,
    #[reflect(ignore)]
    pub created_at: chrono::DateTime<chrono::Utc>,
    #[reflect(ignore)]
    pub updated_at: chrono::DateTime<chrono::Utc>,
    #[reflect(ignore)]
    pub ended_at: chrono::DateTime<chrono::Utc>,
    pub host_id: String,
    pub host: Option<User>,
    pub guest_id: String,
    pub guest: Option<User>,
    pub state: GameState,
    pub turn: u8,
    #[reflect(ignore)]
    pub actions: Option<Vec<Action>>,
}