1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
use super::*;
/// Achievement API.
///
/// Methods require
/// [`request_current_stats()`](../struct.UserStats.html#method.request_current_stats)
/// to have been called and a successful [`UserStatsReceived`](../struct.UserStatsReceived.html)
/// callback processed.
///
/// # Example
///
/// ```no_run
/// # use steamworks::*;
/// # let (client, single) = steamworks::Client::init().unwrap();
/// // Unlock the 'WIN_THE_GAME' achievement
/// client.user_stats().achievement("WIN_THE_GAME").set()?;
/// # Err(())
/// ```
pub struct AchievementHelper<'parent, M> {
pub(crate) name: CString,
pub(crate) parent: &'parent UserStats<M>,
}
impl<M> AchievementHelper<'_, M> {
/// Gets the unlock status of the Achievement.
///
/// This call only modifies Steam's in-memory state so it is quite cheap. To send the unlock
/// status to the server and to trigger the Steam overlay notification you must call
/// [`store_stats()`](../struct.UserStats.html#method.store_stats).
///
/// Fails if this achievement's 'API Name' is unknown, or unsuccessful
/// [`UserStatsReceived`](../struct.UserStatsReceived.html).
pub fn get(&self) -> Result<bool, ()> {
unsafe {
let mut achieved = false;
let success = sys::SteamAPI_ISteamUserStats_GetAchievement(
self.parent.user_stats,
self.name.as_ptr() as *const _,
&mut achieved as *mut _,
);
if success {
Ok(achieved)
} else {
Err(())
}
}
}
/// Unlocks an achievement.
///
/// This call only modifies Steam's in-memory state so it is quite cheap. To send the unlock
/// status to the server and to trigger the Steam overlay notification you must call
/// [`store_stats()`](../struct.UserStats.html#method.store_stats).
///
/// Fails if this achievement's 'API Name' is unknown, or unsuccessful
/// [`UserStatsReceived`](../struct.UserStatsReceived.html).
pub fn set(&self) -> Result<(), ()> {
let success = unsafe {
sys::SteamAPI_ISteamUserStats_SetAchievement(
self.parent.user_stats,
self.name.as_ptr() as *const _,
)
};
if success {
Ok(())
} else {
Err(())
}
}
/// Resets the unlock status of an achievement.
///
/// This call only modifies Steam's in-memory state so it is quite cheap. To send the unlock
/// status to the server and to trigger the Steam overlay notification you must call
/// [`store_stats()`](../struct.UserStats.html#method.store_stats).
///
/// Fails if this achievement's 'API Name' is unknown, or unsuccessful
/// [`UserStatsReceived`](../struct.UserStatsReceived.html).
pub fn clear(&self) -> Result<(), ()> {
let success = unsafe {
sys::SteamAPI_ISteamUserStats_ClearAchievement(
self.parent.user_stats,
self.name.as_ptr() as *const _,
)
};
if success {
Ok(())
} else {
Err(())
}
}
}