refactors, makes Loot private to module
This commit is contained in:
@@ -247,21 +247,19 @@ impl<'q> AsAdmin<'q> {
|
||||
///
|
||||
/// When a player gets an item, it's debt is increased by this item sell value
|
||||
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);
|
||||
|
||||
for (loot, claims) in data {
|
||||
for (item, claims) in data {
|
||||
match claims.len() {
|
||||
1 => {
|
||||
let claim = claims.get(0).unwrap();
|
||||
let player_id = claim.player_id;
|
||||
self.0.transaction(|| {
|
||||
loot.set_owner(claim.player_id)
|
||||
.execute(self.0)?;
|
||||
claim.resolve_claim(self.0)?;
|
||||
//models::item::LootManager(self.0, 0).set_owner(claim.loot_id, claim.player_id)?;
|
||||
models::player::AsPlayer(self.0, player_id)
|
||||
.update_debt(loot.sell_value())?;
|
||||
models::claim::Claims(self.0).remove(player_id, claim.loot_id)?;
|
||||
Ok(())
|
||||
.update_debt(item.sell_value())
|
||||
})?;
|
||||
},
|
||||
_ => (),
|
||||
|
||||
@@ -18,6 +18,21 @@ pub struct Claim {
|
||||
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);
|
||||
|
||||
impl<'q> Claims<'q> {
|
||||
@@ -48,8 +63,7 @@ impl<'q> Claims<'q> {
|
||||
/// Removes a claim from database, returning it
|
||||
pub fn remove(self, player_id: i32, loot_id: i32) -> QueryResult<Claim> {
|
||||
let claim = self.find(player_id, loot_id)?;
|
||||
diesel::delete(claims::table.find(claim.id))
|
||||
.execute(self.0)?;
|
||||
claim.remove(self.0)?;
|
||||
Ok(claim)
|
||||
}
|
||||
|
||||
@@ -59,13 +73,14 @@ impl<'q> Claims<'q> {
|
||||
.load(self.0)
|
||||
}
|
||||
|
||||
pub(crate) fn grouped_by_loot(&self) -> QueryResult<Vec<(models::item::Loot, Vec<Claim>)>> {
|
||||
let loot = models::item::Loot::owned_by(0).load(self.0)?;
|
||||
pub(crate) fn grouped_by_item(&self) -> QueryResult<Vec<(models::item::Item, Vec<Claim>)>> {
|
||||
let group_loot: Vec<Loot> = Loot::owned_by(0).load(self.0)?;
|
||||
let claims = claims::table
|
||||
.load(self.0)?
|
||||
.grouped_by(&loot);
|
||||
.grouped_by(&group_loot);
|
||||
Ok(
|
||||
loot.into_iter()
|
||||
group_loot.into_iter()
|
||||
.map(|loot| loot.into_item())
|
||||
.zip(claims)
|
||||
.collect::<Vec<_>>()
|
||||
)
|
||||
|
||||
@@ -19,7 +19,14 @@ pub struct 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 {
|
||||
Loot::owned_by(player).select(ITEM_COLUMNS)
|
||||
}
|
||||
@@ -62,24 +69,30 @@ impl Loot {
|
||||
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))
|
||||
}
|
||||
|
||||
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))
|
||||
.set(looted::dsl::owner_id.eq(owner))
|
||||
.execute(conn)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// TODO: should belong inside Item impl
|
||||
pub(crate) fn value(&self) -> i32 {
|
||||
self.base_price
|
||||
pub(super) fn into_item(self) -> Item {
|
||||
Item {
|
||||
id: self.id,
|
||||
name: self.name,
|
||||
base_price: self.base_price,
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO: should belong inside Item impl
|
||||
pub(crate) fn sell_value(&self) -> i32 {
|
||||
self.base_price / 2
|
||||
pub(super) fn find(id: i32) -> Find<looted::table, i32> {
|
||||
looted::table
|
||||
.find(id)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// Manager for a player's loot
|
||||
@@ -94,10 +107,10 @@ impl<'q> LootManager<'q> {
|
||||
/// Finds an item by id
|
||||
pub fn find(&self, loot_id: i32) -> QueryResult<Item> {
|
||||
Ok(
|
||||
looted::table
|
||||
.find(loot_id)
|
||||
.first::<Loot>(self.0)
|
||||
.and_then(|loot| {
|
||||
|
||||
Loot::find(loot_id)
|
||||
.first(self.0)
|
||||
.and_then(|loot: Loot| {
|
||||
if loot.owner != self.1 {
|
||||
Err(diesel::result::Error::NotFound)
|
||||
} else {
|
||||
@@ -135,6 +148,8 @@ impl<'q> LootManager<'q> {
|
||||
diesel::delete(looted::table.find(deleted.id)).execute(self.0)?;
|
||||
Ok(deleted)
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
/// An item being looted or bought.
|
||||
|
||||
Reference in New Issue
Block a user