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:
1 parent
0683de965b
commit
fd11b8746b
11 files changed
+240
-23
No files matched your search
@@ -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]
|
||||
|
||||
@@ -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";
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user