refactors, makes Loot private to module

This commit is contained in:
2019-10-14 16:28:18 +02:00
parent b2e319dc15
commit c72169281d
3 changed files with 54 additions and 26 deletions

View File

@@ -247,21 +247,19 @@ impl<'q> AsAdmin<'q> {
/// ///
/// When a player gets an item, it's debt is increased by this item sell value /// When a player gets an item, it's debt is increased by this item sell value
pub fn resolve_claims(self) -> ActionResult<()> { pub fn resolve_claims(self) -> ActionResult<()> {
let data = models::claim::Claims(self.0).grouped_by_loot()?; let data = models::claim::Claims(self.0).grouped_by_item()?;
dbg!(&data); dbg!(&data);
for (loot, claims) in data { for (item, claims) in data {
match claims.len() { match claims.len() {
1 => { 1 => {
let claim = claims.get(0).unwrap(); let claim = claims.get(0).unwrap();
let player_id = claim.player_id; let player_id = claim.player_id;
self.0.transaction(|| { self.0.transaction(|| {
loot.set_owner(claim.player_id) claim.resolve_claim(self.0)?;
.execute(self.0)?; //models::item::LootManager(self.0, 0).set_owner(claim.loot_id, claim.player_id)?;
models::player::AsPlayer(self.0, player_id) models::player::AsPlayer(self.0, player_id)
.update_debt(loot.sell_value())?; .update_debt(item.sell_value())
models::claim::Claims(self.0).remove(player_id, claim.loot_id)?;
Ok(())
})?; })?;
}, },
_ => (), _ => (),

View File

@@ -18,6 +18,21 @@ pub struct Claim {
pub resolve: i32, pub resolve: i32,
} }
impl Claim {
pub fn resolve_claim(&self, conn: &DbConnection) -> QueryResult<()> {
let loot: Loot = Loot::find(self.loot_id).first(conn)?;
loot.set_owner(self.player_id, conn)?;
self.remove(conn)?;
Ok(())
}
fn remove(&self, conn: &DbConnection) -> QueryResult<()> {
diesel::delete(claims::table.find(self.id))
.execute(conn)?;
Ok(())
}
}
pub struct Claims<'q>(pub &'q DbConnection); pub struct Claims<'q>(pub &'q DbConnection);
impl<'q> Claims<'q> { impl<'q> Claims<'q> {
@@ -48,8 +63,7 @@ impl<'q> Claims<'q> {
/// Removes a claim from database, returning it /// Removes a claim from database, returning it
pub fn remove(self, player_id: i32, loot_id: i32) -> QueryResult<Claim> { pub fn remove(self, player_id: i32, loot_id: i32) -> QueryResult<Claim> {
let claim = self.find(player_id, loot_id)?; let claim = self.find(player_id, loot_id)?;
diesel::delete(claims::table.find(claim.id)) claim.remove(self.0)?;
.execute(self.0)?;
Ok(claim) Ok(claim)
} }
@@ -59,13 +73,14 @@ impl<'q> Claims<'q> {
.load(self.0) .load(self.0)
} }
pub(crate) fn grouped_by_loot(&self) -> QueryResult<Vec<(models::item::Loot, Vec<Claim>)>> { pub(crate) fn grouped_by_item(&self) -> QueryResult<Vec<(models::item::Item, Vec<Claim>)>> {
let loot = models::item::Loot::owned_by(0).load(self.0)?; let group_loot: Vec<Loot> = Loot::owned_by(0).load(self.0)?;
let claims = claims::table let claims = claims::table
.load(self.0)? .load(self.0)?
.grouped_by(&loot); .grouped_by(&group_loot);
Ok( Ok(
loot.into_iter() group_loot.into_iter()
.map(|loot| loot.into_item())
.zip(claims) .zip(claims)
.collect::<Vec<_>>() .collect::<Vec<_>>()
) )

View File

@@ -19,7 +19,14 @@ pub struct Item {
} }
impl Item { impl Item {
/// Public proxy for Loot::owned_by that selects only Item fields pub fn value(&self) -> i32 {
self.base_price
}
pub fn sell_value(&self) -> i32 {
self.base_price / 2
}
fn owned_by(player: i32) -> OwnedBy { fn owned_by(player: i32) -> OwnedBy {
Loot::owned_by(player).select(ITEM_COLUMNS) Loot::owned_by(player).select(ITEM_COLUMNS)
} }
@@ -62,24 +69,30 @@ impl Loot {
looted::table.filter(looted::owner_id.eq(id)) looted::table.filter(looted::owner_id.eq(id))
} }
pub(super) fn exists(id: i32) -> Exists<Find<looted::table, i32>> { fn exists(id: i32) -> Exists<Find<looted::table, i32>> {
exists(looted::table.find(id)) exists(looted::table.find(id))
} }
pub(crate) fn set_owner(&self, owner: i32) -> () { pub(super) fn set_owner(&self, owner: i32, conn: &DbConnection) -> QueryResult<()> {
diesel::update(looted::table.find(self.id)) diesel::update(looted::table.find(self.id))
.set(looted::dsl::owner_id.eq(owner)) .set(looted::dsl::owner_id.eq(owner))
.execute(conn)?;
Ok(())
} }
/// TODO: should belong inside Item impl pub(super) fn into_item(self) -> Item {
pub(crate) fn value(&self) -> i32 { Item {
self.base_price id: self.id,
name: self.name,
base_price: self.base_price,
}
} }
/// TODO: should belong inside Item impl pub(super) fn find(id: i32) -> Find<looted::table, i32> {
pub(crate) fn sell_value(&self) -> i32 { looted::table
self.base_price / 2 .find(id)
} }
} }
/// Manager for a player's loot /// Manager for a player's loot
@@ -94,10 +107,10 @@ impl<'q> LootManager<'q> {
/// Finds an item by id /// Finds an item by id
pub fn find(&self, loot_id: i32) -> QueryResult<Item> { pub fn find(&self, loot_id: i32) -> QueryResult<Item> {
Ok( Ok(
looted::table
.find(loot_id) Loot::find(loot_id)
.first::<Loot>(self.0) .first(self.0)
.and_then(|loot| { .and_then(|loot: Loot| {
if loot.owner != self.1 { if loot.owner != self.1 {
Err(diesel::result::Error::NotFound) Err(diesel::result::Error::NotFound)
} else { } else {
@@ -135,6 +148,8 @@ impl<'q> LootManager<'q> {
diesel::delete(looted::table.find(deleted.id)).execute(self.0)?; diesel::delete(looted::table.find(deleted.id)).execute(self.0)?;
Ok(deleted) Ok(deleted)
} }
} }
/// An item being looted or bought. /// An item being looted or bought.