code cleanup, starts testing
This commit is contained in:
@@ -1,19 +1,32 @@
|
||||
use crate::models::item::Loot;
|
||||
use crate::schema::claims;
|
||||
use diesel::prelude::*;
|
||||
|
||||
/// A Claim is a request by a single player on an item from group chest.
|
||||
#[derive(Identifiable, Queryable, Associations, Serialize, Debug)]
|
||||
#[belongs_to(Loot)]
|
||||
pub struct Claim {
|
||||
id: i32,
|
||||
player_id: i32,
|
||||
loot_id: i32,
|
||||
resolve: i32,
|
||||
/// DB Identifier
|
||||
pub id: i32,
|
||||
/// ID that references the player making this claim
|
||||
pub player_id: i32,
|
||||
/// ID that references the loot claimed
|
||||
pub loot_id: i32,
|
||||
/// WIP: How bad the player wants this item
|
||||
pub resolve: i32,
|
||||
}
|
||||
|
||||
#[derive(Insertable, Debug)]
|
||||
#[table_name = "claims"]
|
||||
pub struct NewClaim {
|
||||
pub player_id: i32,
|
||||
pub loot_id: i32,
|
||||
pub(crate) struct NewClaim {
|
||||
player_id: i32,
|
||||
loot_id: i32,
|
||||
}
|
||||
|
||||
impl NewClaim {
|
||||
pub(crate) fn new(player_id: i32, loot_id: i32) -> Self {
|
||||
Self {
|
||||
player_id,
|
||||
loot_id
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
use crate::schema::{items, looted};
|
||||
use crate::DbConnection;
|
||||
use crate::schema::{looted};
|
||||
use diesel::dsl::{exists, Eq, Filter, Find, Select};
|
||||
use diesel::expression::exists::Exists;
|
||||
use diesel::prelude::*;
|
||||
@@ -60,7 +59,7 @@ type ItemDesc<'a> = (&'a str, i32);
|
||||
/// to the id of buying player otherwise.
|
||||
#[derive(Insertable)]
|
||||
#[table_name = "looted"]
|
||||
struct NewLoot<'a> {
|
||||
pub(crate) struct NewLoot<'a> {
|
||||
name: &'a str,
|
||||
base_price: i32,
|
||||
owner_id: i32,
|
||||
@@ -68,7 +67,7 @@ struct NewLoot<'a> {
|
||||
|
||||
impl<'a> NewLoot<'a> {
|
||||
/// A new loot going to the group (loot procedure)
|
||||
fn to_group(desc: ItemDesc<'a>) -> Self {
|
||||
pub(crate) fn to_group(desc: ItemDesc<'a>) -> Self {
|
||||
Self {
|
||||
name: desc.0,
|
||||
base_price: desc.1,
|
||||
@@ -77,7 +76,7 @@ impl<'a> NewLoot<'a> {
|
||||
}
|
||||
|
||||
/// A new loot going to a specific player (buy procedure)
|
||||
fn to_player(player: i32, desc: ItemDesc<'a>) -> Self {
|
||||
pub(crate) fn to_player(player: i32, desc: ItemDesc<'a>) -> Self {
|
||||
Self {
|
||||
name: desc.0,
|
||||
base_price: desc.1,
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
mod claim;
|
||||
mod item;
|
||||
mod player;
|
||||
pub(super) mod claim;
|
||||
pub(super) mod item;
|
||||
pub(super) mod player;
|
||||
|
||||
pub use claim::{Claim, NewClaim};
|
||||
pub use claim::Claim;
|
||||
pub use item::Item;
|
||||
pub(crate) use item::Loot;
|
||||
pub use player::{NewPlayer, Player, WealthUpdate};
|
||||
pub use player::{Player, Wealth};
|
||||
|
||||
@@ -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