1
0
mirror of https://git.ngram.ca/OpenJam/rc-servers synced 2026-08-23 23:08:52 +00:00

Save current game event to database for use in the match

This commit is contained in:
NG (Graham)
2025-07-22 21:36:24 -04:00
parent 0683de965b
commit fd11b8746b
11 changed files with 240 additions and 23 deletions

View File

@@ -0,0 +1,45 @@
use sea_orm_migration::prelude::*;
pub struct Migration;
impl MigrationName for Migration {
fn name(&self) -> &str {
"m20250722_000001_create_game_event_table"
}
}
#[async_trait::async_trait]
impl MigrationTrait for Migration {
// Define how to apply this migration: Create the Permissions table.
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(crate::schema::game_event::Entity)
.col(
ColumnDef::new(crate::schema::game_event::Column::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(crate::schema::game_event::Column::CreationTime).big_integer().not_null())
.col(ColumnDef::new(crate::schema::game_event::Column::Map).string().not_null())
.col(ColumnDef::new(crate::schema::game_event::Column::Mode).string().not_null())
.col(ColumnDef::new(crate::schema::game_event::Column::Visibility).string().not_null())
.col(ColumnDef::new(crate::schema::game_event::Column::AutoHeal).boolean().not_null())
.col(ColumnDef::new(crate::schema::game_event::Column::Start).big_integer().not_null())
.col(ColumnDef::new(crate::schema::game_event::Column::End).big_integer().not_null())
.col(ColumnDef::new(crate::schema::game_event::Column::Variant).string().not_null())
.to_owned(),
)
.await
}
// Define how to rollback this migration: Drop the Permissions table.
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(crate::schema::game_event::Entity).to_owned())
.await
}
}

View File

@@ -9,6 +9,7 @@ mod m20250526_000001_add_garage_customisation;
mod m20250529_000001_create_sanction_table;
mod m20250713_000001_create_game_table;
mod m20250713_000002_create_player_table;
mod m20250722_000001_create_game_event_table;
pub struct Migrator;
@@ -25,6 +26,7 @@ impl MigratorTrait for Migrator {
Box::new(m20250529_000001_create_sanction_table::Migration),
Box::new(m20250713_000001_create_game_table::Migration),
Box::new(m20250713_000002_create_player_table::Migration),
Box::new(m20250722_000001_create_game_event_table::Migration),
]
}
}

View File

@@ -0,0 +1,28 @@
use sea_orm::entity::prelude::*;
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
#[sea_orm(table_name = "game_events")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub creation_time: i64, // seconds since unix epoch
pub map: String,
pub mode: super::multiplayer_game::GameMode,
pub visibility: super::multiplayer_game::MapVisibility,
pub auto_heal: bool,
pub start: i64, // seconds since unix epoch
pub end: i64, // seconds since unix epoch
pub variant: EventVariant,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}
#[derive(Clone, Debug, PartialEq, Eq, EnumIter, DeriveActiveEnum)]
#[sea_orm(rs_type = "String", db_type = "String(StringLen::None)", rename_all = "PascalCase")]
pub enum EventVariant {
Multiplayer,
Singleplayer,
}

View File

@@ -8,6 +8,7 @@ pub mod common_query;
pub mod sanction;
pub mod multiplayer_game;
pub mod multiplayer_game_player;
pub mod game_event;
pub fn parse_int_csv(s: &str) -> Vec<u32> {
s.split(',').filter_map(|i_as_s| {

View File

@@ -345,4 +345,18 @@ impl Database {
crate::schema::multiplayer_game_player::Entity::insert_many(entities.into_iter()).exec(&self.orm).await?;
Ok(())
}
pub async fn game_event_at_time(&self, time: i64, variant: crate::schema::game_event::EventVariant) -> Result<Option<crate::schema::game_event::Model>, sea_orm::DbErr> {
crate::schema::game_event::Entity::find()
.filter(crate::schema::game_event::Column::Start.lte(time))
.filter(crate::schema::game_event::Column::End.gte(time))
.filter(crate::schema::game_event::Column::Variant.eq(variant))
.order_by_desc(crate::schema::game_event::Column::Start)
.one(&self.orm)
.await
}
pub async fn insert_game_event(&self, entity: crate::schema::game_event::ActiveModel) -> Result<crate::schema::game_event::Model, sea_orm::DbErr> {
entity.insert(&self.orm).await
}
}