begins work on updates
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
use crate::schema::players;
|
||||
use crate::DbConnection;
|
||||
|
||||
/// Representation of a player in database
|
||||
#[derive(Debug, Queryable, Serialize)]
|
||||
@@ -14,28 +15,26 @@ pub struct Player {
|
||||
|
||||
|
||||
/// Wealth represented as a single fractionnal amount of gold pieces
|
||||
struct WealthInGold(f32);
|
||||
/// Data used to update wealth
|
||||
struct WealthUpdate(WealthInGold);
|
||||
#[derive(Identifiable, AsChangeset)]
|
||||
#[table_name="players"]
|
||||
struct WealthUpdate {
|
||||
id: i32,
|
||||
cp: i32,
|
||||
sp: i32,
|
||||
gp: i32,
|
||||
pp: i32
|
||||
}
|
||||
|
||||
impl WealthInGold {
|
||||
impl WealthUpdate{
|
||||
/// Unpack individual pieces counts from gold value
|
||||
fn unpack(self) -> (i32, i32, i32, i32) {
|
||||
fn from_gp(id: i32, gp: f32) -> Self {
|
||||
// TODO: 0,01 pp = 1 gp = 10 sp = 100 cp
|
||||
(0,0,0,0)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
impl WealthUpdate {
|
||||
/// Apply the update to the specified player
|
||||
fn commit(self, player_id: i32) {
|
||||
// Extract (cp, sp, gp, pp) from floating gold piece value
|
||||
// Update record in db
|
||||
let (cp, sp, gp, pp) = (0,0,0,0);
|
||||
Self { id, cp, sp, gp, pp }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Representation of a new player record
|
||||
#[derive(Insertable)]
|
||||
#[table_name = "players"]
|
||||
@@ -47,23 +46,6 @@ pub struct NewPlayer<'a> {
|
||||
pp: i32,
|
||||
}
|
||||
|
||||
impl<'a> NewPlayer<'a> {
|
||||
fn new(name: &'a str, wealth: Option<WealthInGold>) -> Self {
|
||||
let (cp, sp, gp, pp) = wealth.map(|w| w.unpack()).unwrap_or((0, 0, 0, 0));
|
||||
NewPlayer {
|
||||
name,
|
||||
cp,
|
||||
sp,
|
||||
gp,
|
||||
pp,
|
||||
}
|
||||
}
|
||||
|
||||
fn insert(self) -> Result<(), ()> {
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
Reference in New Issue
Block a user