Skip to content

Commit af320f5

Browse files
committed
feat: game reporting database tables and store game report
1 parent 5ab41f2 commit af320f5

File tree

9 files changed

+359
-55
lines changed

9 files changed

+359
-55
lines changed

Cargo.lock

Lines changed: 101 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,13 @@ rand = "0.9"
8282
[dependencies.sea-orm]
8383
version = "^1"
8484
default-features = false
85-
features = ["runtime-tokio-rustls", "macros", "with-chrono", "sqlx-sqlite"]
85+
features = [
86+
"runtime-tokio-rustls",
87+
"macros",
88+
"with-chrono",
89+
"with-json",
90+
"sqlx-sqlite",
91+
]
8692

8793
# SeaORM Migration
8894
[dependencies.sea-orm-migration]
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
use crate::{
2+
database::DbResult,
3+
utils::{
4+
parsing::player_character::{PlayerCharacterPower, PlayerCharacterWeaponMod, WeaponId},
5+
types::PlayerID,
6+
},
7+
};
8+
use futures_util::future::BoxFuture;
9+
use sea_orm::{entity::prelude::*, ActiveValue::Set, FromJsonQueryResult};
10+
use serde::{Deserialize, Serialize};
11+
use std::collections::HashMap;
12+
13+
pub type GameReportModel = Model;
14+
15+
/// Structure for player data
16+
#[derive(Serialize, Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
17+
#[sea_orm(table_name = "game_report")]
18+
pub struct Model {
19+
/// Unique Identifier for the player data
20+
#[sea_orm(primary_key, column_type = "Integer")]
21+
pub id: i64,
22+
pub data: GameReportData,
23+
pub created_at: DateTimeUtc,
24+
pub finished_at: DateTimeUtc,
25+
}
26+
27+
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, FromJsonQueryResult)]
28+
pub struct GameReportData {
29+
pub attributes: HashMap<String, String>,
30+
pub players: Vec<GameReportPlayer>,
31+
/// Randomness seed used by players
32+
pub seed: u32,
33+
/// Whether the extraction
34+
pub extracted: bool,
35+
}
36+
37+
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
38+
pub struct GameReportPlayer {
39+
/// ID of the player
40+
pub player_id: PlayerID,
41+
42+
/// Player username
43+
pub player_name: String,
44+
45+
/// Name of the player character kit the player was using
46+
pub kit_name: Option<String>,
47+
48+
/// Player weapon list
49+
pub weapons: Option<Vec<WeaponId>>,
50+
51+
/// Player weapon mods
52+
pub weapon_mods: Option<Vec<PlayerCharacterWeaponMod>>,
53+
54+
/// Player power choices and levels
55+
pub powers: Option<Vec<PlayerCharacterPower>>,
56+
}
57+
58+
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
59+
pub enum Relation {}
60+
61+
impl ActiveModelBehavior for ActiveModel {}
62+
63+
impl Model {
64+
pub fn create(
65+
db: &DatabaseConnection,
66+
data: GameReportData,
67+
created_at: DateTimeUtc,
68+
finished_at: DateTimeUtc,
69+
) -> BoxFuture<'_, DbResult<Self>> {
70+
ActiveModel {
71+
data: Set(data),
72+
created_at: Set(created_at),
73+
finished_at: Set(finished_at),
74+
..Default::default()
75+
}
76+
.insert(db)
77+
}
78+
}

src/database/entities/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub mod galaxy_at_war;
2+
pub mod game_report;
23
pub mod leaderboard_data;
34
pub mod player_data;
45
pub mod players;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use sea_orm_migration::{prelude::*, schema::*};
2+
3+
#[derive(DeriveMigrationName)]
4+
pub struct Migration;
5+
6+
#[async_trait::async_trait]
7+
impl MigrationTrait for Migration {
8+
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
9+
manager
10+
.create_table(
11+
Table::create()
12+
.table(GameReport::Table)
13+
.if_not_exists()
14+
.col(pk_auto(GameReport::Id))
15+
.col(json_binary(GameReport::Data))
16+
.col(date_time(GameReport::CreatedAt))
17+
.col(date_time(GameReport::FinishedAt))
18+
.to_owned(),
19+
)
20+
.await
21+
}
22+
23+
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
24+
manager
25+
.drop_table(Table::drop().table(GameReport::Table).to_owned())
26+
.await
27+
}
28+
}
29+
30+
#[derive(DeriveIden)]
31+
enum GameReport {
32+
Table,
33+
/// Unique ID of the game report
34+
Id,
35+
/// The actual game report data
36+
Data,
37+
/// Timestamp of the game creation
38+
CreatedAt,
39+
/// Timestamp of the game finish
40+
FinishedAt,
41+
}

src/database/migration/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod m20221222_174733_player_data;
66
mod m20230913_185124_player_data_unique;
77
mod m20231205_121139_leaderboard_data;
88
mod m20240714_023535_add_player_timestamps;
9+
mod m20250312_083054_game_reports;
910

1011
pub struct Migrator;
1112

@@ -19,6 +20,7 @@ impl MigratorTrait for Migrator {
1920
Box::new(m20230913_185124_player_data_unique::Migration),
2021
Box::new(m20231205_121139_leaderboard_data::Migration),
2122
Box::new(m20240714_023535_add_player_timestamps::Migration),
23+
Box::new(m20250312_083054_game_reports::Migration),
2224
]
2325
}
2426
}

src/services/game/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,9 @@ impl Game {
271271
pub fn replay(&mut self) {
272272
self.set_state(GameState::PreGame);
273273

274+
// Update game creation timestamp
275+
self.created_at = Utc::now();
276+
274277
// TODO: Rotate game reporting ID to a new ID
275278
self.set_game_reporting_id(18014398695176361);
276279
}

0 commit comments

Comments
 (0)