Implement item shop #70

This commit is contained in:
NG (Graham) committed 2026-01-01 20:34:12 -05:00
1 parent 83843377d7
commit 3aadbb91ba
16 files changed
+602 -151

No files matched your search

+66
View File
@@ -1,4 +1,5 @@
use argon2::PasswordVerifier;
use oj_rc_database::sea_orm::IntoActiveModel;
use sha2::Digest;
use crate::persist::config::ConfigProvider;
@@ -666,6 +667,19 @@ impl UserData {
}
).await?
},
super::CurrencyOp::AddSub(to_addsub) => {
self.db.update_user_aux_by_user_id_and_descriptor_custom(
self.account.id,
desc.clone(),
move |model| {
use oj_rc_database::sea_orm::IntoActiveModel;
let new_currency = (model.data.parse::<u64>().unwrap_or_default() as i64) + to_addsub;
let mut am = model.to_owned().into_active_model();
am.data = oj_rc_database::sea_orm::ActiveValue::Set(new_currency.clamp(0, i64::MAX).to_string());
Some(am)
}
).await?
},
};
let num: u64 = if let Some(model) = model_opt {
model.data.parse().unwrap_or_default()
@@ -708,6 +722,58 @@ impl <C: Clone> super::User<C> for UserData {
}
}
async fn unlock_parts(&self, parts: &[u32]) -> Result<(), polariton_server::operations::SimpleOpError> {
let parts_row = self.db.user_aux_by_user_id_and_descriptor(self.account.id, oj_rc_database::schema::user_aux::Descriptor::UnlockedParts).await
.map_err(|e| {
log::error!("Failed to retrieve UnlockedParts to unlock parts for user {}: {}", self.account.id, e);
polariton_server::operations::SimpleOpError::with_message(
crate::data::error_codes::WebServicesError::DatabaseError as i16,
format!("Failed to retrieve UnlockedParts to unlock parts: {}", e),
)
})?
.ok_or_else(|| {
log::error!("Failed to find UnlockedParts to unlock parts for user {}", self.account.id);
polariton_server::operations::SimpleOpError::with_message(
crate::data::error_codes::WebServicesError::DatabaseError as i16,
"Failed to find UnlockedParts to unlock parts".to_owned(),
)
})?;
let mut unlocked_parts = serde_json::from_str::<super::inventory::UnlockedParts>(&parts_row.data)
.map_err(|e| {
log::error!("Failed to deserialize UnlockedParts to unlock parts for user {}: {}", self.account.id, e);
polariton_server::operations::SimpleOpError::with_message(
crate::data::error_codes::WebServicesError::DatabaseError as i16,
format!("Failed to deserialize UnlockedParts to unlock parts: {}", e),
)
})?;
for new_part in parts {
unlocked_parts.unlocked.push(*new_part);
}
let mut parts_row = parts_row.into_active_model();
let parts_json = serde_json::to_string(&unlocked_parts)
.map_err(|e| {
log::error!("Failed to serialize UnlockedParts to unlock parts for user {}: {}", self.account.id, e);
polariton_server::operations::SimpleOpError::with_message(
crate::data::error_codes::WebServicesError::DatabaseError as i16,
format!("Failed to serialize UnlockedParts to unlock parts: {}", e),
)
})?;
parts_row.data = oj_rc_database::sea_orm::ActiveValue::Set(parts_json);
self.db.update_user_aux_by_user_id_and_descriptor(
parts_row,
self.account.id,
oj_rc_database::schema::user_aux::Descriptor::UnlockedParts
).await
.map_err(|e| {
log::error!("Failed to update UnlockedParts to unlock parts for user {}: {}", self.account.id, e);
polariton_server::operations::SimpleOpError::with_message(
crate::data::error_codes::WebServicesError::DatabaseError as i16,
format!("Failed to update UnlockedParts to unlock parts: {}", e),
)
})?;
Ok(())
}
async fn selected_garage(&self) -> (String, u32) {
match self.db.garage_selected(self.account.id).await {
Ok(Some(selected)) => (super::i64_as_uuid_str(selected.uuid), selected.slot as u32),
+1 -1
View File
@@ -8,7 +8,7 @@ mod initial_data;
pub use initial_data::{setup_new_user, register_new_user};
mod inventory;
pub use inventory::UnlockedParts;
pub use inventory::{UnlockedParts, UnlockOverride};
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, PlayerScore, MultiplayerError, MultiplayerErrorCode, PlayerDescriptor, GameEventSetter, CurrentGameEvent, AuthError, IntercomUser, FakePlayers, ResolvedVehicle, CommonUser, IntercomListener, UserRole, SocialUser, CurrencyType, CurrencyOp, MatchRewards, SingleplayerUser};
+2
View File
@@ -63,6 +63,7 @@ pub trait UserAuthenticator {
#[async_trait::async_trait]
pub trait User<C>: ChatUser + SocialUser + LobbyUser + MultiplayerUser + SingleplayerUser + IntercomUser + CommonUser {
async fn unlocked_parts(&self) -> Vec<u32>;
async fn unlock_parts(&self, parts: &[u32]) -> Result<(), polariton_server::operations::SimpleOpError>;
async fn selected_garage(&self) -> (String, u32);
async fn select_garage(&self, slot: i32) -> Result<(), i16>;
async fn all_slots(&self) -> UserSlots<C>;
@@ -417,6 +418,7 @@ pub enum CurrencyOp {
Get,
Add(u64),
Sub(u64),
AddSub(i64),
}
#[async_trait::async_trait]