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

@@ -144,7 +144,7 @@ pub struct GameEventSequence {
}
impl GameEventSequence {
pub fn now(&mut self) -> GameEventTransmissible {
pub fn now(&mut self, updater: Box<dyn crate::persist::user::GameEventSetter>) -> GameEventTransmissible {
let time_now = chrono::Utc::now().timestamp();
let mut item_now = &self.modes[self.index];
if time_now >= (item_now.duration.as_secs() as i64) + self.started {
@@ -152,6 +152,26 @@ impl GameEventSequence {
self.index = self.strategy.next(self.index, self.modes.len());
item_now = &self.modes[self.index];
self.started = time_now;
let mp = crate::persist::user::CurrentGameEvent {
map: crate::data::game_mode::GameMap::from_persist(item_now.multiplayer.map).as_str().to_owned(),
visibility: crate::data::game_mode::MapVisibility::from_persist(item_now.multiplayer.visibility),
mode: crate::data::game_mode::GameMode::from_persist(item_now.multiplayer.mode),
auto_heal: item_now.multiplayer.auto_heal,
start: self.started,
end: self.started + item_now.duration.as_secs() as i64,
};
let sp = crate::persist::user::CurrentGameEvent {
map: crate::data::game_mode::GameMap::from_persist(item_now.singleplayer.map).as_str().to_owned(),
visibility: crate::data::game_mode::MapVisibility::from_persist(item_now.singleplayer.visibility),
mode: crate::data::game_mode::GameMode::from_persist(item_now.singleplayer.mode),
auto_heal: item_now.singleplayer.auto_heal,
start: self.started,
end: self.started + item_now.duration.as_secs() as i64,
};
tokio::spawn(async move {
updater.set_multiplayer(mp).await;
updater.set_singleplayer(sp).await;
});
}
let remaining_ticks = ((item_now.duration.as_secs() as i64) - (time_now - self.started)) * 10_000_000;
GameEventTransmissible {

View File

@@ -978,6 +978,79 @@ impl <C: Clone> super::User<C> for UserData {
})?;
Ok(())
}
fn current_game_event_setter(&self) -> Box<dyn super::GameEventSetter> {
Box::new(GameEventSetterImpl {
db: self.db.clone(),
})
}
}
struct GameEventSetterImpl {
db: std::sync::Arc<oj_rc_database::Database>,
}
impl GameEventSetterImpl {
async fn insert_event(&self, variant: oj_rc_database::schema::game_event::EventVariant, event: super::CurrentGameEvent) {
let now = chrono::Utc::now().timestamp();
let model = oj_rc_database::schema::game_event::ActiveModel {
id: oj_rc_database::sea_orm::ActiveValue::NotSet,
creation_time: oj_rc_database::sea_orm::ActiveValue::Set(now),
map: oj_rc_database::sea_orm::ActiveValue::Set(event.map),
mode: oj_rc_database::sea_orm::ActiveValue::Set(event.mode.to_db()),
visibility: oj_rc_database::sea_orm::ActiveValue::Set(event.visibility.to_db()),
auto_heal: oj_rc_database::sea_orm::ActiveValue::Set(event.auto_heal),
start: oj_rc_database::sea_orm::ActiveValue::Set(event.start),
end: oj_rc_database::sea_orm::ActiveValue::Set(event.end),
variant: oj_rc_database::sea_orm::ActiveValue::Set(variant),
};
if let Err(e) = self.db.insert_game_event(model).await {
log::error!("Failed to save new game event: {}", e);
}
}
async fn select_event_now(&self, variant: oj_rc_database::schema::game_event::EventVariant) -> Option<super::CurrentGameEvent> {
let now = chrono::Utc::now().timestamp();
match self.db.game_event_at_time(now, variant).await {
Err(e) => {
log::error!("Failed to retrieve current game event: {}", e);
None
},
Ok(None) => {
log::warn!("Failed to find current game event");
None
},
Ok(Some(event)) => {
Some(super::CurrentGameEvent {
map: event.map,
visibility: crate::data::game_mode::MapVisibility::from_db(event.visibility),
mode: crate::data::game_mode::GameMode::from_db(event.mode),
auto_heal: event.auto_heal,
start: event.start,
end: event.end,
})
},
}
}
}
#[async_trait::async_trait]
impl super::GameEventSetter for GameEventSetterImpl {
async fn set_multiplayer(&self, event: super::CurrentGameEvent) {
self.insert_event(oj_rc_database::schema::game_event::EventVariant::Multiplayer, event).await
}
async fn get_multiplayer(&self) -> Option<super::CurrentGameEvent> {
self.select_event_now(oj_rc_database::schema::game_event::EventVariant::Multiplayer).await
}
async fn set_singleplayer(&self, event: super::CurrentGameEvent) {
self.insert_event(oj_rc_database::schema::game_event::EventVariant::Singleplayer, event).await
}
async fn get_singleplayer(&self) -> Option<super::CurrentGameEvent> {
self.select_event_now(oj_rc_database::schema::game_event::EventVariant::Singleplayer).await
}
}
#[async_trait::async_trait]

View File

@@ -11,7 +11,7 @@ mod inventory;
pub use inventory::UnlockedParts;
mod traits;
pub use traits::{UserProvider, User, UserToken, UserSlots, UserSlotData, VehicleData, UserInfo, UserLoginInfo, ExtraUserInfo, UserAuthenticator, NewSlotData, UserId, RegistrationInfo, VehicleUploadData, ChatUser, AvatarInfo, GetAvatarInfo, ControlData, ControlType, CustomisationData, GetCustomisationData, SetSanction, SanctionType, LobbyUser, GameDescriptor, PlayerLobbyDescriptor, MultiplayerUser, MultiplayerError, MultiplayerErrorCode, PlayerDescriptor};
pub use traits::{UserProvider, User, UserToken, UserSlots, UserSlotData, VehicleData, UserInfo, UserLoginInfo, ExtraUserInfo, UserAuthenticator, NewSlotData, UserId, RegistrationInfo, VehicleUploadData, ChatUser, AvatarInfo, GetAvatarInfo, ControlData, ControlType, CustomisationData, GetCustomisationData, SetSanction, SanctionType, LobbyUser, GameDescriptor, PlayerLobbyDescriptor, MultiplayerUser, MultiplayerError, MultiplayerErrorCode, PlayerDescriptor, GameEventSetter, CurrentGameEvent};
pub const TOKEN_SECRET_FILENAME: &str = "token_secret.key";

View File

@@ -81,6 +81,15 @@ pub trait User<C>: ChatUser + LobbyUser + MultiplayerUser {
async fn last_seen(&self) -> Result<u64, i16>;
async fn get_avatar_info(&self) -> Result<GetAvatarInfo<C>, i16>;
async fn set_avatar_info(&self, info: AvatarInfo) -> Result<(), i16>;
fn current_game_event_setter(&self) -> Box<dyn GameEventSetter>;
}
#[async_trait::async_trait]
pub trait GameEventSetter: Send + Sync + 'static {
async fn set_multiplayer(&self, event: CurrentGameEvent);
async fn get_multiplayer(&self) -> Option<CurrentGameEvent>;
async fn set_singleplayer(&self, event: CurrentGameEvent);
async fn get_singleplayer(&self) -> Option<CurrentGameEvent>;
}
pub struct UserSlots<C> {
@@ -234,6 +243,15 @@ pub trait LobbyUser {
async fn start_game(&self, game: GameDescriptor, players: Vec<PlayerLobbyDescriptor>) -> Result<(), polariton_server::operations::SimpleOpError>;
}
pub struct CurrentGameEvent {
pub map: String,
pub visibility: crate::data::game_mode::MapVisibility,
pub mode: crate::data::game_mode::GameMode,
pub auto_heal: bool,
pub start: i64, // seconds since Unix epoch
pub end: i64, // seconds since Unix epoch
}
pub struct GameDescriptor {
pub guid: String,
pub map: String,