use std::collections::HashMap;
use std::sync::{Arc, Mutex, MutexGuard};
use crate::backend::{BackendEvent, SteamBackend};
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum FakeCall {
#[cfg(feature = "lobby")]
CreateLobby {
kind: crate::LobbyKind,
max_members: u32,
},
#[cfg(feature = "lobby")]
JoinLobby(u64),
#[cfg(feature = "lobby")]
LeaveLobby(u64),
#[cfg(feature = "lobby")]
SetLobbyData {
lobby: u64,
key: String,
value: String,
},
#[cfg(feature = "lobby")]
SetLobbyJoinable {
lobby: u64,
joinable: bool,
},
#[cfg(feature = "lobby")]
SetRichPresence {
key: String,
value: Option<String>,
},
#[cfg(feature = "lobby")]
ClearRichPresence,
#[cfg(feature = "lobby")]
InviteToGame {
friend: u64,
connect: String,
},
#[cfg(feature = "stats")]
SetStat {
name: String,
value: crate::StatValue,
},
#[cfg(feature = "stats")]
UnlockAchievement(String),
#[cfg(feature = "stats")]
ClearAchievement(String),
#[cfg(feature = "stats")]
IndicateAchievementProgress {
name: String,
current: u32,
max: u32,
},
#[cfg(feature = "stats")]
StoreStats,
#[cfg(feature = "stats")]
ResetAllStats {
achievements_too: bool,
},
#[cfg(feature = "leaderboards")]
FindLeaderboard {
name: String,
create: bool,
},
#[cfg(feature = "leaderboards")]
UploadScore {
board: u64,
method: crate::UploadMethod,
score: i32,
details: Vec<i32>,
},
#[cfg(feature = "leaderboards")]
DownloadScores {
board: u64,
range: crate::ScoreRange,
},
}
#[derive(Debug)]
pub(crate) struct FakeState {
pub(crate) local_id: u64,
pumps: u64,
pub(crate) calls: Vec<FakeCall>,
pub(crate) queued: Vec<BackendEvent>,
friend_names: HashMap<u64, String>,
launch_command_line: String,
#[cfg(feature = "lobby")]
pub(crate) lobby: crate::lobby::fake::FakeLobbyState,
#[cfg(feature = "stats")]
pub(crate) stats: crate::stats::fake::FakeStatsState,
#[cfg(feature = "leaderboards")]
pub(crate) boards: crate::leaderboards::fake::FakeBoardsState,
}
#[derive(Clone, Debug)]
pub struct FakeSteamBackend {
state: Arc<Mutex<FakeState>>,
}
impl Default for FakeSteamBackend {
fn default() -> Self {
Self::new()
}
}
impl FakeSteamBackend {
pub fn new() -> Self {
Self {
state: Arc::new(Mutex::new(FakeState {
local_id: 76_561_197_960_265_729,
pumps: 0,
calls: Vec::new(),
queued: Vec::new(),
friend_names: HashMap::new(),
launch_command_line: String::new(),
#[cfg(feature = "lobby")]
lobby: crate::lobby::fake::FakeLobbyState::default(),
#[cfg(feature = "stats")]
stats: crate::stats::fake::FakeStatsState::default(),
#[cfg(feature = "leaderboards")]
boards: crate::leaderboards::fake::FakeBoardsState::default(),
})),
}
}
pub(crate) fn lock(&self) -> MutexGuard<'_, FakeState> {
self.state.lock().unwrap_or_else(|p| p.into_inner())
}
pub fn calls(&self) -> Vec<FakeCall> {
self.lock().calls.clone()
}
pub fn pump_count(&self) -> u64 {
self.lock().pumps
}
pub fn set_local_id(&self, id: u64) {
self.lock().local_id = id;
}
pub fn push_event(&self, event: BackendEvent) {
self.lock().queued.push(event);
}
pub fn set_friend_name(&self, id: u64, name: &str) {
self.lock().friend_names.insert(id, name.to_string());
}
pub fn set_launch_command_line(&self, text: &str) {
self.lock().launch_command_line = text.to_string();
}
}
impl SteamBackend for FakeSteamBackend {
fn local_id(&self) -> u64 {
self.lock().local_id
}
fn friend_name(&self, id: u64) -> String {
self.lock().friend_names.get(&id).cloned().unwrap_or_default()
}
fn launch_command_line(&self) -> String {
self.lock().launch_command_line.clone()
}
fn pump(&self) -> Vec<BackendEvent> {
let mut s = self.lock();
s.pumps = s.pumps.saturating_add(1);
std::mem::take(&mut s.queued)
}
#[cfg(feature = "lobby")]
fn lobby(&self) -> Option<&dyn crate::LobbyBackend> {
Some(self)
}
#[cfg(feature = "stats")]
fn stats(&self) -> Option<&dyn crate::StatsBackend> {
Some(self)
}
#[cfg(feature = "leaderboards")]
fn leaderboards(&self) -> Option<&dyn crate::LeaderboardBackend> {
Some(self)
}
}