From 3a1f8ec4f6a4479c68be4505c90602fd781b3d98 Mon Sep 17 00:00:00 2001 From: Artus Date: Thu, 10 Oct 2019 16:38:59 +0200 Subject: [PATCH] thinking in progress --- lootalot_db/src/lib.rs | 1 + lootalot_db/src/models/player.rs | 8 ++++- lootalot_db/src/updates.rs | 59 ++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 lootalot_db/src/updates.rs diff --git a/lootalot_db/src/lib.rs b/lootalot_db/src/lib.rs index 3509296..6d8f2d0 100644 --- a/lootalot_db/src/lib.rs +++ b/lootalot_db/src/lib.rs @@ -12,6 +12,7 @@ use diesel::query_dsl::RunQueryDsl; use diesel::r2d2::{self, ConnectionManager}; pub mod models; +mod updates; mod schema; /// The connection used diff --git a/lootalot_db/src/models/player.rs b/lootalot_db/src/models/player.rs index a6ec804..b26f47c 100644 --- a/lootalot_db/src/models/player.rs +++ b/lootalot_db/src/models/player.rs @@ -20,7 +20,13 @@ pub struct Player { pub pp: i32, } -/// Unpack a floating value in gold pieces to integer +impl Player { + pub fn by_id(id: i32) -> Self { + + } +} + +/// Unpack a floating value of gold pieces to integer /// values of copper, silver, gold and platinum pieces /// /// # Note diff --git a/lootalot_db/src/updates.rs b/lootalot_db/src/updates.rs new file mode 100644 index 0000000..5be23f5 --- /dev/null +++ b/lootalot_db/src/updates.rs @@ -0,0 +1,59 @@ +//! +//! updates.rs +//! +//! Contains semantic mutations of database +//! +use crate::DbConnection; +use crate::models::player::Wealth; + +type PlayerId = i32; +type ItemId = i32; + +enum LootUpdate { + AddedItem(ItemId), + RemovedItem(Item), + GivenToPlayer(ItemId), +} + +impl LootUpdate { + fn add_loot(conn: &DbConnection, to_player: PlayerId, item_desc: Item) -> Result { + use schema::looted::dsl::*; + let new_item = models::item::NewLoot::to_player(to_player, &item_desc); + diesel::insert_into(looted) + .values(&new_item) + .execute(conn)?; + // Return newly created + let created_id = looted.select(id).order(id.desc()).first(conn)?; + Ok(LootUpdate::AddedItem(created_id)) + } + + fn remove_loot(conn: &DbConnection, loot_id: ItemId) -> Result { + use schema::looted::dsl::*; + } + + fn give_to_player(conn: &DbConnection, loot_id: ItemId) -> Result { + use schema::looted::dsl::*; + } +} + +impl LootUpdate { + fn undo(self, conn: &DbConnection) -> Result<(), diesel::result::Error> { + match self { + LootUpdate::AddedItem(item_id) => { + // Remove the item + }, + LootUpdate::RemovedItem(item) => { + // Add the item back + }, + LootUpdate::GivenToPlayer(item_id) => { + // Change owner to group + } + } + } +} + +enum PlayerUpdate { + Wealth(Wealth), + ClaimItem(ItemId), + UnclaimItem(ItemId), +}