code cleanup, starts testing
This commit is contained in:
@@ -1,17 +1,23 @@
|
||||
use crate::schema::players;
|
||||
use crate::DbConnection;
|
||||
use diesel::prelude::*;
|
||||
|
||||
/// Representation of a player in database
|
||||
#[derive(Debug, Queryable, Serialize)]
|
||||
pub struct Player {
|
||||
id: i32,
|
||||
name: String,
|
||||
debt: i32,
|
||||
cp: i32,
|
||||
sp: i32,
|
||||
gp: i32,
|
||||
pp: i32,
|
||||
/// DB Identitier
|
||||
pub id: i32,
|
||||
/// Full name of the character
|
||||
pub name: String,
|
||||
/// Amount of gold coins owed to the group.
|
||||
/// Taking a looted items will increase the debt by it's sell value
|
||||
pub debt: i32,
|
||||
/// Count of copper pieces
|
||||
pub cp: i32,
|
||||
/// Count of silver pieces
|
||||
pub sp: i32,
|
||||
/// Count of gold pieces
|
||||
pub gp: i32,
|
||||
/// Count of platinum pieces
|
||||
pub pp: i32,
|
||||
}
|
||||
|
||||
/// Unpack a floating value in gold pieces to integer
|
||||
@@ -32,21 +38,28 @@ fn unpack_gold_value(gold: f32) -> (i32, i32, i32, i32) {
|
||||
(cp, sp, gp, pp)
|
||||
}
|
||||
|
||||
/// Represent an update on a player's wealth
|
||||
/// State of a player's wealth
|
||||
///
|
||||
/// The values held here are the amount of pieces to add or
|
||||
/// substract to player wealth.
|
||||
/// Values are held as individual pieces counts.
|
||||
/// Allows conversion from and to a floating amount of gold pieces.
|
||||
#[derive(Queryable, AsChangeset, Debug)]
|
||||
#[table_name = "players"]
|
||||
pub struct WealthUpdate {
|
||||
cp: i32,
|
||||
sp: i32,
|
||||
gp: i32,
|
||||
pp: i32,
|
||||
pub struct Wealth {
|
||||
pub cp: i32,
|
||||
pub sp: i32,
|
||||
pub gp: i32,
|
||||
pub pp: i32,
|
||||
}
|
||||
|
||||
impl WealthUpdate {
|
||||
impl Wealth {
|
||||
/// Unpack individual pieces counts from gold value
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// # use lootalot_db::models::Wealth;
|
||||
/// let wealth = Wealth::from_gp(403.21);
|
||||
/// assert_eq!(wealth.as_tuple(), (1, 2, 3, 4));
|
||||
/// ```
|
||||
pub fn from_gp(gp: f32) -> Self {
|
||||
let (cp, sp, gp, pp) = unpack_gold_value(gp);
|
||||
Self { cp, sp, gp, pp }
|
||||
@@ -55,8 +68,8 @@ impl WealthUpdate {
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// # use lootalot_db::models::WealthUpdate;
|
||||
/// let wealth = WealthUpdate::from_gp(403.21);
|
||||
/// # use lootalot_db::models::Wealth;
|
||||
/// let wealth = Wealth{ pp: 4, gp: 3, sp: 2, cp: 1};
|
||||
/// assert_eq!(wealth.to_gp(), 403.21);
|
||||
/// ```
|
||||
pub fn to_gp(&self) -> f32 {
|
||||
@@ -73,7 +86,7 @@ impl WealthUpdate {
|
||||
/// Representation of a new player record
|
||||
#[derive(Insertable)]
|
||||
#[table_name = "players"]
|
||||
pub struct NewPlayer<'a> {
|
||||
pub(crate) struct NewPlayer<'a> {
|
||||
name: &'a str,
|
||||
cp: i32,
|
||||
sp: i32,
|
||||
@@ -82,22 +95,17 @@ pub struct NewPlayer<'a> {
|
||||
}
|
||||
|
||||
impl<'a> NewPlayer<'a> {
|
||||
pub fn create(name: &'a str, wealth: f32) -> Self {
|
||||
let wealth = WealthUpdate::from_gp(wealth);
|
||||
pub(crate) fn create(name: &'a str, wealth_in_gp: f32) -> Self {
|
||||
let (cp, sp, gp, pp) = Wealth::from_gp(wealth_in_gp).as_tuple();
|
||||
Self {
|
||||
name,
|
||||
cp: wealth.cp,
|
||||
sp: wealth.sp,
|
||||
gp: wealth.gp,
|
||||
pp: wealth.pp,
|
||||
cp, sp, gp, pp,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::tests;
|
||||
|
||||
#[test]
|
||||
fn test_unpack_gold_values() {
|
||||
|
||||
Reference in New Issue
Block a user