impls wealth updating
This commit is contained in:
Binary file not shown.
@@ -6,8 +6,7 @@ extern crate serde_derive;
|
|||||||
|
|
||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
use diesel::r2d2::{self, ConnectionManager};
|
use diesel::r2d2::{self, ConnectionManager};
|
||||||
// Convenience re-export, maybe DbApi should me move here instead ??
|
use diesel::query_dsl::RunQueryDsl;
|
||||||
pub use diesel::query_dsl::RunQueryDsl;
|
|
||||||
|
|
||||||
mod models;
|
mod models;
|
||||||
mod schema;
|
mod schema;
|
||||||
@@ -101,13 +100,20 @@ impl<'q> AsPlayer<'q> {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
pub fn update_wealth(self, value: f32) -> ActionResult {
|
pub fn update_wealth(self, value: f32) -> ActionResult {
|
||||||
Ok(
|
use schema::players::dsl::*;
|
||||||
false
|
let current_wealth = players.find(self.id)
|
||||||
// TODO:
|
.select((cp, sp, gp, pp))
|
||||||
// models::player::WealthUpdate::from_gp(self.id, value)
|
.first::<models::WealthUpdate>(self.conn)?;
|
||||||
// .save_changes(&conn)?;
|
// TODO: improve this
|
||||||
)
|
// should be move inside a WealthUpdate method
|
||||||
|
let update =
|
||||||
|
models::WealthUpdate::from_gp(current_wealth.to_gp() + value);
|
||||||
|
diesel::update(players)
|
||||||
|
.filter(id.eq(self.id))
|
||||||
|
.set(&update)
|
||||||
|
.execute(self.conn)
|
||||||
|
// TODO: need to work out what this boolean REALLY means
|
||||||
|
.map(|r| match r { 1 => true, _ => false })
|
||||||
}
|
}
|
||||||
/// Put a claim on a specific item
|
/// Put a claim on a specific item
|
||||||
pub fn claim(self, item: i32) -> ActionResult {
|
pub fn claim(self, item: i32) -> ActionResult {
|
||||||
|
|||||||
@@ -3,3 +3,4 @@ mod player;
|
|||||||
|
|
||||||
pub use item::Item;
|
pub use item::Item;
|
||||||
pub use player::Player;
|
pub use player::Player;
|
||||||
|
pub use player::WealthUpdate;
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
use diesel::prelude::*;
|
||||||
use crate::schema::players;
|
use crate::schema::players;
|
||||||
use crate::DbConnection;
|
use crate::DbConnection;
|
||||||
|
|
||||||
@@ -14,11 +15,31 @@ pub struct Player {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Wealth represented as a single fractionnal amount of gold pieces
|
/// Unpack a floating value in gold pieces to integer
|
||||||
#[derive(Identifiable, AsChangeset)]
|
/// values of copper, silver, gold and platinum pieces
|
||||||
|
///
|
||||||
|
/// # Note
|
||||||
|
///
|
||||||
|
/// The conversion is slightly different than standard rules :
|
||||||
|
/// ``` 1pp = 100gp = 1000sp = 10000 cp ```
|
||||||
|
///
|
||||||
|
fn unpack_gold_value(gold: f32) -> (i32, i32, i32, i32) {
|
||||||
|
let rest = (gold.fract() * 100.0).round() as i32;
|
||||||
|
let gold = gold.trunc() as i32;
|
||||||
|
let pp = gold / 100;
|
||||||
|
let gp = gold % 100;
|
||||||
|
let sp = rest / 10;
|
||||||
|
let cp = rest % 10;
|
||||||
|
(cp, sp, gp, pp)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Represent an update on a player's wealth
|
||||||
|
///
|
||||||
|
/// The values held here are the amount of pieces to add or
|
||||||
|
/// substract to player wealth.
|
||||||
|
#[derive(Queryable, AsChangeset, Debug)]
|
||||||
#[table_name="players"]
|
#[table_name="players"]
|
||||||
struct WealthUpdate {
|
pub struct WealthUpdate {
|
||||||
id: i32,
|
|
||||||
cp: i32,
|
cp: i32,
|
||||||
sp: i32,
|
sp: i32,
|
||||||
gp: i32,
|
gp: i32,
|
||||||
@@ -27,10 +48,15 @@ struct WealthUpdate {
|
|||||||
|
|
||||||
impl WealthUpdate{
|
impl WealthUpdate{
|
||||||
/// Unpack individual pieces counts from gold value
|
/// Unpack individual pieces counts from gold value
|
||||||
fn from_gp(id: i32, gp: f32) -> Self {
|
pub fn from_gp(gp: f32) -> Self {
|
||||||
// TODO: 0,01 pp = 1 gp = 10 sp = 100 cp
|
let (cp, sp, gp, pp) = unpack_gold_value(gp);
|
||||||
let (cp, sp, gp, pp) = (0,0,0,0);
|
Self { cp, sp, gp, pp }
|
||||||
Self { id, cp, sp, gp, pp }
|
}
|
||||||
|
|
||||||
|
pub fn to_gp(&self) -> f32 {
|
||||||
|
let i = self.pp * 100 + self.gp;
|
||||||
|
let f = ( self.sp * 10 + self.cp ) as f32 / 100.0;
|
||||||
|
i as f32 + f
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -62,6 +62,13 @@ pub(crate) fn serve() -> std::io::Result<()> {
|
|||||||
db_call(pool, move |api| api.fetch_players())
|
db_call(pool, move |api| api.fetch_players())
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
.route(
|
||||||
|
"/update-wealth/{player_id}/{amount}",
|
||||||
|
web::get().to_async(move |pool: AppPool, data: web::Path<(i32, f32)>| {
|
||||||
|
let (player, gold) = *data;
|
||||||
|
db_call(pool, move |api| api.as_player(player).update_wealth(gold))
|
||||||
|
}),
|
||||||
|
)
|
||||||
.route(
|
.route(
|
||||||
"/loot/{player_id}",
|
"/loot/{player_id}",
|
||||||
web::get().to_async(move |pool: AppPool, player_id: web::Path<i32>| {
|
web::get().to_async(move |pool: AppPool, player_id: web::Path<i32>| {
|
||||||
|
|||||||
Reference in New Issue
Block a user