mirror of
https://git.ngram.ca/OpenJam/rc-servers
synced 2026-08-23 23:08:52 +00:00
Allow two or more people to load into the same multiplayer match #30
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
use sea_orm_migration::prelude::*;
|
||||
|
||||
pub struct Migration;
|
||||
|
||||
impl MigrationName for Migration {
|
||||
fn name(&self) -> &str {
|
||||
"m20250713_000001_create_game_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::multiplayer_game::Entity)
|
||||
.col(
|
||||
ColumnDef::new(crate::schema::multiplayer_game::Column::Id)
|
||||
.integer()
|
||||
.not_null()
|
||||
.auto_increment()
|
||||
.primary_key(),
|
||||
)
|
||||
.col(ColumnDef::new(crate::schema::multiplayer_game::Column::CreationTime).big_integer().not_null())
|
||||
.col(ColumnDef::new(crate::schema::multiplayer_game::Column::Guid).big_integer().not_null())
|
||||
.col(ColumnDef::new(crate::schema::multiplayer_game::Column::Map).string().not_null())
|
||||
.col(ColumnDef::new(crate::schema::multiplayer_game::Column::Mode).string().not_null())
|
||||
.col(ColumnDef::new(crate::schema::multiplayer_game::Column::Visibility).string().not_null())
|
||||
.col(ColumnDef::new(crate::schema::multiplayer_game::Column::AutoHeal).boolean().not_null())
|
||||
.col(ColumnDef::new(crate::schema::multiplayer_game::Column::Variant).string().not_null())
|
||||
.col(ColumnDef::new(crate::schema::multiplayer_game::Column::IsComplete).boolean().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::multiplayer_game::Entity).to_owned())
|
||||
.await
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
use sea_orm_migration::prelude::*;
|
||||
|
||||
pub struct Migration;
|
||||
|
||||
impl MigrationName for Migration {
|
||||
fn name(&self) -> &str {
|
||||
"m20250713_000002_create_player_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::multiplayer_game_player::Entity)
|
||||
.col(
|
||||
ColumnDef::new(crate::schema::multiplayer_game_player::Column::Id)
|
||||
.integer()
|
||||
.not_null()
|
||||
.auto_increment()
|
||||
.primary_key(),
|
||||
)
|
||||
.col(ColumnDef::new(crate::schema::multiplayer_game_player::Column::UserId).integer().not_null())
|
||||
.foreign_key(
|
||||
ForeignKey::create()
|
||||
.name("fk-players-user_id")
|
||||
.from(crate::schema::multiplayer_game_player::Entity, crate::schema::multiplayer_game_player::Column::UserId)
|
||||
.to(crate::schema::user::Entity, crate::schema::user::Column::Id),
|
||||
)
|
||||
.col(ColumnDef::new(crate::schema::multiplayer_game_player::Column::GameId).integer().not_null())
|
||||
.foreign_key(
|
||||
ForeignKey::create()
|
||||
.name("fk-players-game_id")
|
||||
.from(crate::schema::multiplayer_game_player::Entity, crate::schema::multiplayer_game_player::Column::GameId)
|
||||
.to(crate::schema::multiplayer_game::Entity, crate::schema::multiplayer_game::Column::Id),
|
||||
)
|
||||
.col(ColumnDef::new(crate::schema::multiplayer_game_player::Column::CreationTime).big_integer().not_null())
|
||||
.col(ColumnDef::new(crate::schema::multiplayer_game_player::Column::PlayerId).small_integer().not_null())
|
||||
.col(ColumnDef::new(crate::schema::multiplayer_game_player::Column::Team).integer().not_null())
|
||||
.col(ColumnDef::new(crate::schema::multiplayer_game_player::Column::Group).integer().null())
|
||||
.col(ColumnDef::new(crate::schema::multiplayer_game_player::Column::IsClaimed).boolean().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::multiplayer_game_player::Entity).to_owned())
|
||||
.await
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,8 @@ mod m20250424_000004_create_user_aux_table;
|
||||
mod m20250424_000005_create_campaign_tables;
|
||||
mod m20250526_000001_add_garage_customisation;
|
||||
mod m20250529_000001_create_sanction_table;
|
||||
mod m20250713_000001_create_game_table;
|
||||
mod m20250713_000002_create_player_table;
|
||||
|
||||
pub struct Migrator;
|
||||
|
||||
@@ -21,6 +23,8 @@ impl MigratorTrait for Migrator {
|
||||
Box::new(m20250424_000005_create_campaign_tables::Migration),
|
||||
Box::new(m20250526_000001_add_garage_customisation::Migration),
|
||||
Box::new(m20250529_000001_create_sanction_table::Migration),
|
||||
Box::new(m20250713_000001_create_game_table::Migration),
|
||||
Box::new(m20250713_000002_create_player_table::Migration),
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,8 @@ pub mod campaign;
|
||||
pub mod campaign_difficulty_completion;
|
||||
pub mod common_query;
|
||||
pub mod sanction;
|
||||
pub mod multiplayer_game;
|
||||
pub mod multiplayer_game_player;
|
||||
|
||||
pub fn parse_int_csv(s: &str) -> Vec<u32> {
|
||||
s.split(',').filter_map(|i_as_s| {
|
||||
|
||||
58
rc_database/src/schema/multiplayer_game.rs
Normal file
58
rc_database/src/schema/multiplayer_game.rs
Normal file
@@ -0,0 +1,58 @@
|
||||
use sea_orm::entity::prelude::*;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
|
||||
#[sea_orm(table_name = "games")]
|
||||
pub struct Model {
|
||||
#[sea_orm(primary_key)]
|
||||
pub id: i32,
|
||||
pub creation_time: i64, // seconds since unix epoch
|
||||
pub guid: i64,
|
||||
pub map: String,
|
||||
pub mode: GameMode,
|
||||
pub visibility: MapVisibility,
|
||||
pub auto_heal: bool,
|
||||
pub variant: GameType,
|
||||
pub is_complete: bool,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {
|
||||
#[sea_orm(has_many = "super::multiplayer_game_player::Entity")]
|
||||
Player,
|
||||
}
|
||||
|
||||
impl Related<super::multiplayer_game_player::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::Player.def()
|
||||
}
|
||||
}
|
||||
|
||||
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 GameMode {
|
||||
BattleArena,
|
||||
SuddenDeath,
|
||||
Pit,
|
||||
TestMode,
|
||||
SinglePlayer,
|
||||
TeamDeathmatch,
|
||||
Campaign,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, EnumIter, DeriveActiveEnum)]
|
||||
#[sea_orm(rs_type = "String", db_type = "String(StringLen::None)", rename_all = "PascalCase")]
|
||||
pub enum MapVisibility {
|
||||
Good,
|
||||
Poor,
|
||||
Bad,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, EnumIter, DeriveActiveEnum)]
|
||||
#[sea_orm(rs_type = "String", db_type = "String(StringLen::None)", rename_all = "PascalCase")]
|
||||
pub enum GameType {
|
||||
Standard,
|
||||
Ranked,
|
||||
Custom,
|
||||
}
|
||||
45
rc_database/src/schema/multiplayer_game_player.rs
Normal file
45
rc_database/src/schema/multiplayer_game_player.rs
Normal file
@@ -0,0 +1,45 @@
|
||||
use sea_orm::entity::prelude::*;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
|
||||
#[sea_orm(table_name = "players")]
|
||||
pub struct Model {
|
||||
#[sea_orm(primary_key)]
|
||||
pub id: i32,
|
||||
pub user_id: i32,
|
||||
pub game_id: i32,
|
||||
pub creation_time: i64, // seconds since unix epoch
|
||||
pub player_id: i16, // actually u8
|
||||
pub team: i32,
|
||||
pub group: Option<i32>, // probably a user id
|
||||
pub is_claimed: bool,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {
|
||||
#[sea_orm(
|
||||
belongs_to = "super::multiplayer_game::Entity",
|
||||
from = "Column::GameId",
|
||||
to = "super::multiplayer_game::Column::Id"
|
||||
)]
|
||||
Game,
|
||||
#[sea_orm(
|
||||
belongs_to = "super::user::Entity",
|
||||
from = "Column::UserId",
|
||||
to = "super::user::Column::Id"
|
||||
)]
|
||||
User,
|
||||
}
|
||||
|
||||
impl Related<super::multiplayer_game::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::Game.def()
|
||||
}
|
||||
}
|
||||
|
||||
impl Related<super::user::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::User.def()
|
||||
}
|
||||
}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
||||
@@ -23,6 +23,8 @@ pub enum Relation {
|
||||
Aux,
|
||||
#[sea_orm(has_many = "super::campaign::Entity")]
|
||||
Campaigns,
|
||||
#[sea_orm(has_many = "super::multiplayer_game_player::Entity")]
|
||||
Player,
|
||||
}
|
||||
|
||||
impl Related<super::permissions::Entity> for Entity {
|
||||
@@ -49,4 +51,10 @@ impl Related<super::campaign::Entity> for Entity {
|
||||
}
|
||||
}
|
||||
|
||||
impl Related<super::multiplayer_game_player::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::Player.def()
|
||||
}
|
||||
}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use sea_orm_migration::MigratorTrait;
|
||||
use sea_orm::{ActiveModelTrait, ColumnTrait, EntityTrait, PaginatorTrait, QueryFilter, QueryOrder, QuerySelect, TransactionTrait};
|
||||
use sea_orm::{ActiveModelTrait, ColumnTrait, EntityTrait, PaginatorTrait, QueryFilter, QueryOrder, QuerySelect, TransactionTrait, RelationTrait};
|
||||
|
||||
pub struct Database {
|
||||
orm: sea_orm::DatabaseConnection,
|
||||
@@ -272,4 +272,77 @@ impl Database {
|
||||
pub async fn insert_sanction(&self, entity: crate::schema::sanction::ActiveModel) -> Result<crate::schema::sanction::Model, sea_orm::DbErr> {
|
||||
entity.insert(&self.orm).await
|
||||
}
|
||||
|
||||
pub async fn game_by_user_id_and_completion(&self, user_id: i32, is_complete: bool) -> Result<Option<crate::schema::multiplayer_game::Model>, sea_orm::DbErr> {
|
||||
Ok(crate::schema::multiplayer_game::Entity::find()
|
||||
.find_also_related(crate::schema::multiplayer_game_player::Entity)
|
||||
//.join(sea_orm::JoinType::InnerJoin, crate::schema::multiplayer_game_player::Relation::Game.def())
|
||||
.filter(crate::schema::multiplayer_game::Column::IsComplete.eq(is_complete))
|
||||
.filter(crate::schema::multiplayer_game_player::Column::UserId.eq(user_id))
|
||||
.order_by_asc(crate::schema::multiplayer_game::Column::CreationTime)
|
||||
//.into_model()
|
||||
.one(&self.orm)
|
||||
.await?
|
||||
.map(|(x, _)| x))
|
||||
}
|
||||
|
||||
pub async fn update_complete_game_by_game_guid(&self, game_guid: i64) -> Result<(), sea_orm::DbErr> {
|
||||
crate::schema::multiplayer_game::Entity::update_many()
|
||||
.col_expr(crate::schema::multiplayer_game::Column::IsComplete, sea_orm::sea_query::Expr::value(true))
|
||||
.filter(crate::schema::multiplayer_game::Column::Guid.eq(game_guid))
|
||||
.exec(&self.orm)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn complete_all_games(&self) -> Result<(), sea_orm::DbErr> {
|
||||
crate::schema::multiplayer_game::Entity::update_many()
|
||||
.col_expr(crate::schema::multiplayer_game::Column::IsComplete, sea_orm::sea_query::Expr::value(true))
|
||||
.filter(crate::schema::multiplayer_game::Column::IsComplete.eq(false))
|
||||
.exec(&self.orm)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn insert_game(&self, entity: crate::schema::multiplayer_game::ActiveModel) -> Result<crate::schema::multiplayer_game::Model, sea_orm::DbErr> {
|
||||
entity.insert(&self.orm).await
|
||||
}
|
||||
|
||||
pub async fn players_by_game_guid_and_completion(&self, game_guid: i64, is_complete: bool) -> Result<Vec<crate::schema::multiplayer_game_player::Model>, sea_orm::DbErr> {
|
||||
crate::schema::multiplayer_game_player::Entity::find()
|
||||
.join(sea_orm::JoinType::InnerJoin, crate::schema::multiplayer_game::Relation::Player.def())
|
||||
.filter(crate::schema::multiplayer_game::Column::Guid.eq(game_guid))
|
||||
.filter(crate::schema::multiplayer_game::Column::IsComplete.eq(is_complete))
|
||||
.into_model::<crate::schema::multiplayer_game_player::Model>()
|
||||
.all(&self.orm)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn players_by_game_guid_and_completion_heavy(&self, game_guid: i64, is_complete: bool) -> Result<Vec<(crate::schema::multiplayer_game_player::Model, crate::schema::user::Model)>, sea_orm::DbErr> {
|
||||
Ok(crate::schema::multiplayer_game_player::Entity::find()
|
||||
.find_also_related(crate::schema::user::Entity)
|
||||
.find_also_related(crate::schema::multiplayer_game::Entity)
|
||||
//.join(sea_orm::JoinType::InnerJoin, crate::schema::multiplayer_game::Relation::Player.def())
|
||||
//.join(sea_orm::JoinType::InnerJoin, crate::schema::user::Relation::Player.def())
|
||||
.filter(crate::schema::multiplayer_game::Column::Guid.eq(game_guid))
|
||||
.filter(crate::schema::multiplayer_game::Column::IsComplete.eq(is_complete))
|
||||
.all(&self.orm)
|
||||
.await?
|
||||
.into_iter()
|
||||
.filter_map(|(player, user, _)| user.map(|user| (player, user)))
|
||||
.collect())
|
||||
}
|
||||
|
||||
pub async fn players_by_game_id_and_completion(&self, game_id: i32) -> Result<Vec<crate::schema::multiplayer_game_player::Model>, sea_orm::DbErr> {
|
||||
crate::schema::multiplayer_game_player::Entity::find()
|
||||
.join(sea_orm::JoinType::InnerJoin, crate::schema::multiplayer_game::Relation::Player.def())
|
||||
.filter(crate::schema::multiplayer_game::Column::Id.eq(game_id))
|
||||
.all(&self.orm)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn insert_players(&self, entities: Vec<crate::schema::multiplayer_game_player::ActiveModel>) -> Result<(), sea_orm::DbErr> {
|
||||
crate::schema::multiplayer_game_player::Entity::insert_many(entities.into_iter()).exec(&self.orm).await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user