Compare commits
6 Commits
refactor_u
...
2991a88a30
| Author | SHA1 | Date | |
|---|---|---|---|
| 2991a88a30 | |||
| a3eaeed807 | |||
| d280d0f095 | |||
| dbb084b0ec | |||
| 7350d5222c | |||
| 89172177eb |
7
.gitignore
vendored
7
.gitignore
vendored
@@ -1,11 +1,10 @@
|
|||||||
/target
|
/target
|
||||||
**/*.rs.bk
|
**/*.rs.bk
|
||||||
|
|
||||||
node_modules
|
|
||||||
fontawesome
|
|
||||||
package-lock.json
|
|
||||||
|
|
||||||
Cargo.lock
|
Cargo.lock
|
||||||
**/*.sqlite3
|
**/*.sqlite3
|
||||||
**/.env
|
**/.env
|
||||||
|
|
||||||
|
package-lock.json
|
||||||
|
fontawesome
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
@@ -4,19 +4,15 @@
|
|||||||
//! This module wraps all needed database operations.
|
//! This module wraps all needed database operations.
|
||||||
//! It exports a public API for integration with various clients (REST Api, CLI, ...)
|
//! It exports a public API for integration with various clients (REST Api, CLI, ...)
|
||||||
extern crate dotenv;
|
extern crate dotenv;
|
||||||
#[macro_use]
|
#[macro_use] extern crate diesel;
|
||||||
extern crate diesel;
|
#[macro_use] extern crate serde_derive;
|
||||||
#[macro_use]
|
|
||||||
extern crate serde_derive;
|
|
||||||
|
|
||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
use diesel::query_dsl::RunQueryDsl;
|
use diesel::query_dsl::RunQueryDsl;
|
||||||
use diesel::r2d2::{self, ConnectionManager};
|
use diesel::r2d2::{self, ConnectionManager};
|
||||||
|
|
||||||
mod transactions;
|
|
||||||
pub mod models;
|
pub mod models;
|
||||||
mod schema;
|
mod schema;
|
||||||
use transactions::{DbTransaction};
|
|
||||||
|
|
||||||
/// The connection used
|
/// The connection used
|
||||||
pub type DbConnection = SqliteConnection;
|
pub type DbConnection = SqliteConnection;
|
||||||
@@ -24,39 +20,10 @@ pub type DbConnection = SqliteConnection;
|
|||||||
pub type Pool = r2d2::Pool<ConnectionManager<DbConnection>>;
|
pub type Pool = r2d2::Pool<ConnectionManager<DbConnection>>;
|
||||||
/// The result of a query on DB
|
/// The result of a query on DB
|
||||||
pub type QueryResult<T> = Result<T, diesel::result::Error>;
|
pub type QueryResult<T> = Result<T, diesel::result::Error>;
|
||||||
pub type ActionResult<T> = QueryResult<ActionStatus<T>>;
|
/// The result of an action provided by DbApi
|
||||||
/// Return status of an Action
|
pub type ActionResult<R> = Result<R, diesel::result::Error>;
|
||||||
#[derive(Serialize, Debug)]
|
|
||||||
pub struct ActionStatus<R: serde::Serialize> {
|
|
||||||
/// Has the action made changes ?
|
|
||||||
pub executed: bool,
|
|
||||||
/// Response payload
|
|
||||||
pub response: R,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ActionStatus<()> {
|
|
||||||
pub fn was_updated(updated_lines: usize) -> Self {
|
|
||||||
match updated_lines {
|
|
||||||
1 => Self::ok(),
|
|
||||||
_ => Self::nop(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn ok() -> ActionStatus<()> {
|
|
||||||
Self {
|
|
||||||
executed: true,
|
|
||||||
response: (),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: Default + serde::Serialize> ActionStatus<T> {
|
|
||||||
pub fn nop() -> ActionStatus<T> {
|
|
||||||
Self {
|
|
||||||
executed: false,
|
|
||||||
response: Default::default(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/// A wrapper providing an API over the database
|
/// A wrapper providing an API over the database
|
||||||
/// It offers a convenient way to deal with connection.
|
/// It offers a convenient way to deal with connection.
|
||||||
///
|
///
|
||||||
@@ -75,7 +42,7 @@ impl<T: Default + serde::Serialize> ActionStatus<T> {
|
|||||||
/// x .sell_loot([players], [excluded_item_ids]) -> Success status (bool, player_share)
|
/// x .sell_loot([players], [excluded_item_ids]) -> Success status (bool, player_share)
|
||||||
/// // Claims should be resolved after a certain delay
|
/// // Claims should be resolved after a certain delay
|
||||||
/// x .set_claims_timeout()
|
/// x .set_claims_timeout()
|
||||||
/// x .resolve_claims()
|
/// v .resolve_claims()
|
||||||
/// v .add_player(player_data)
|
/// v .add_player(player_data)
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
@@ -159,91 +126,106 @@ impl<'q> AsPlayer<'q> {
|
|||||||
/// # Panics
|
/// # Panics
|
||||||
///
|
///
|
||||||
/// This currently panics if player wealth fails to be updated, as this is
|
/// This currently panics if player wealth fails to be updated, as this is
|
||||||
/// a serious error. TODO: handle deletion of bought item in case of wealth update failure.
|
/// a serious error.
|
||||||
pub fn buy<S: Into<String>>(self, name: S, price: i32) -> ActionResult<Option<(i32, i32, i32, i32)>> {
|
pub fn buy<'a>(self, name: &'a str, price: i32) -> ActionResult<(i32, i32, i32, i32)> {
|
||||||
match transactions::player::Buy.execute(
|
self.conn.transaction(|| {
|
||||||
self.conn,
|
let new_item = models::item::NewLoot::to_player(self.id, (name, price));
|
||||||
transactions::player::AddLootParams {
|
let _item_added = diesel::insert_into(schema::looted::table)
|
||||||
player_id: self.id,
|
.values(&new_item)
|
||||||
loot_name: name.into(),
|
.execute(self.conn)
|
||||||
loot_price: price,
|
.map(|rows_updated| match rows_updated {
|
||||||
},
|
1 => (),
|
||||||
) {
|
_ => panic!("RuntimeError: Buy made no changes at all"),
|
||||||
Ok(res) => Ok(ActionStatus { executed: true, response: Some(res.loot_cost) }),
|
})?;
|
||||||
Err(e) => { dbg!(&e); Ok(ActionStatus { executed: false, response: None}) },
|
self.update_wealth(-(price as f32))
|
||||||
}
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
/// Sell an item from this player chest
|
/// Sell an item from this player chest
|
||||||
///
|
///
|
||||||
/// # Panics
|
/// # Panics
|
||||||
///
|
///
|
||||||
/// This currently panics if player wealth fails to be updated, as this is
|
/// This currently panics if player wealth fails to be updated, as this is
|
||||||
/// a serious error. TODO: handle restoring of sold item in case of wealth update failure.
|
/// a serious error.
|
||||||
pub fn sell(
|
pub fn sell(
|
||||||
self,
|
self,
|
||||||
loot_id: i32,
|
loot_id: i32,
|
||||||
_price_mod: Option<f32>,
|
price_mod: Option<f32>,
|
||||||
) -> ActionResult<Option<(i32, i32, i32, i32)>> {
|
) -> ActionResult<(i32, i32, i32, i32)> {
|
||||||
// Check that the item belongs to player
|
self.conn.transaction(|| {
|
||||||
let exists_and_owned: bool =
|
use schema::looted::dsl::*;
|
||||||
diesel::select(models::Loot::owns(self.id, loot_id))
|
let loot = looted
|
||||||
.get_result(self.conn)?;
|
.find(loot_id)
|
||||||
if !exists_and_owned {
|
.first::<models::Loot>(self.conn)?;
|
||||||
return Ok(ActionStatus::nop());
|
if loot.owner != self.id {
|
||||||
}
|
// If the item does not belong to player,
|
||||||
transactions::player::Sell.execute(
|
// it can't be what we're looking for
|
||||||
self.conn,
|
return Err(diesel::result::Error::NotFound);
|
||||||
transactions::player::LootParams {
|
}
|
||||||
player_id: self.id,
|
let mut sell_value = (loot.base_price / 2) as f32;
|
||||||
loot_id,
|
if let Some(modifier) = price_mod {
|
||||||
},
|
sell_value *= modifier;
|
||||||
)
|
}
|
||||||
.map(|res| ActionStatus { executed: true, response: Some(res.loot_cost) })
|
let _deleted = diesel::delete(looted.find(loot_id))
|
||||||
.or_else(|e| { dbg!(&e); Ok(ActionStatus::nop()) })
|
.execute(self.conn)?;
|
||||||
|
self.update_wealth(sell_value)
|
||||||
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Adds the value in gold to the player's wealth.
|
/// Adds the value in gold to the player's wealth.
|
||||||
///
|
///
|
||||||
/// Value can be negative to substract wealth.
|
/// Value can be negative to substract wealth.
|
||||||
pub fn update_wealth(self, value_in_gp: f32) -> ActionResult<Option<(i32, i32, i32, i32)>> {
|
pub fn update_wealth(self, value_in_gp: f32) -> ActionResult<(i32, i32, i32, i32)> {
|
||||||
transactions::player::UpdateWealth.execute(
|
use schema::players::dsl::*;
|
||||||
self.conn,
|
let current_wealth = players
|
||||||
transactions::player::WealthParams {
|
.find(self.id)
|
||||||
player_id: self.id,
|
.select((cp, sp, gp, pp))
|
||||||
value_in_gp,
|
.first::<models::Wealth>(self.conn)?;
|
||||||
},
|
// TODO: improve thisdiesel dependant transaction
|
||||||
)
|
// should be move inside a WealthUpdate method
|
||||||
.map(|res| ActionStatus { executed: true, response: Some(res) })
|
let updated_wealth = models::Wealth::from_gp(current_wealth.to_gp() + value_in_gp);
|
||||||
.or_else(|e| { dbg!(&e); Ok(ActionStatus::nop())})
|
// Difference in coins that is sent back
|
||||||
|
let (old, new) = (current_wealth.as_tuple(), updated_wealth.as_tuple());
|
||||||
|
let diff = (new.0 - old.0, new.1 - old.1, new.2 - old.2, new.3 - old.3);
|
||||||
|
diesel::update(players)
|
||||||
|
.filter(id.eq(self.id))
|
||||||
|
.set(&updated_wealth)
|
||||||
|
.execute(self.conn)
|
||||||
|
.map(|r| match r {
|
||||||
|
1 => diff,
|
||||||
|
_ => panic!("RuntimeError: UpdateWealth did no changes at all!"),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
/// 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<()> {
|
||||||
let exists: bool =
|
let exists: bool = diesel::select(models::Loot::exists(item)).get_result(self.conn)?;
|
||||||
diesel::select(models::Loot::exists(item)).get_result(self.conn)?;
|
|
||||||
if !exists {
|
if !exists {
|
||||||
return Ok(ActionStatus::nop());
|
return Err(diesel::result::Error::NotFound);
|
||||||
};
|
};
|
||||||
transactions::player::PutClaim.execute(
|
let claim = models::claim::NewClaim::new(self.id, item);
|
||||||
self.conn,
|
diesel::insert_into(schema::claims::table)
|
||||||
transactions::player::LootParams {
|
.values(&claim)
|
||||||
player_id: self.id,
|
.execute(self.conn)
|
||||||
loot_id: item,
|
.map(|rows_updated| match rows_updated {
|
||||||
},
|
1 => (),
|
||||||
)
|
_ => panic!("RuntimeError: Claim did no change at all!"),
|
||||||
.map(|_| ActionStatus { executed: true, response: () })
|
})
|
||||||
.or_else(|e| { dbg!(&e); Ok(ActionStatus::nop())})
|
|
||||||
}
|
}
|
||||||
/// Withdraw claim
|
/// Withdraw claim
|
||||||
pub fn unclaim(self, item: i32) -> ActionResult<()> {
|
pub fn unclaim(self, item: i32) -> ActionResult<()> {
|
||||||
transactions::player::WithdrawClaim.execute(
|
use schema::claims::dsl::*;
|
||||||
self.conn,
|
diesel::delete(
|
||||||
transactions::player::LootParams {
|
claims
|
||||||
player_id: self.id,
|
.filter(loot_id.eq(item))
|
||||||
loot_id: item,
|
.filter(player_id.eq(self.id)),
|
||||||
},
|
)
|
||||||
)
|
.execute(self.conn)
|
||||||
.map(|_| ActionStatus { executed: true, response: () })
|
.and_then(|rows_updated| match rows_updated {
|
||||||
.or_else(|e| { dbg!(&e); Ok(ActionStatus::nop())})
|
1 => Ok(()),
|
||||||
|
0 => Err(diesel::result::Error::NotFound),
|
||||||
|
_ => panic!("RuntimeError: UnclaimItem did not make expected changes"),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -254,22 +236,25 @@ impl<'q> AsAdmin<'q> {
|
|||||||
/// Adds a player to the database
|
/// Adds a player to the database
|
||||||
///
|
///
|
||||||
/// Takes the player name and starting wealth (in gold value).
|
/// Takes the player name and starting wealth (in gold value).
|
||||||
pub fn add_player(self, name: String, start_wealth: f32) -> ActionResult<()> {
|
pub fn add_player(self, name: &str, start_wealth: f32) -> ActionResult<()> {
|
||||||
diesel::insert_into(schema::players::table)
|
diesel::insert_into(schema::players::table)
|
||||||
.values(&models::player::NewPlayer::create(&name, start_wealth))
|
.values(&models::player::NewPlayer::create(name, start_wealth))
|
||||||
.execute(self.0)
|
.execute(self.0)
|
||||||
.map(ActionStatus::was_updated)
|
.map(|rows_updated| match rows_updated {
|
||||||
|
1 => (),
|
||||||
|
_ => panic!("RuntimeError: UnclaimItem did not make expected changes"),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Adds a list of items to the group loot
|
/// Adds a list of items to the group loot
|
||||||
pub fn add_loot<'a>(self, items: Vec<(&'a str, i32)>) -> ActionResult<()> {
|
pub fn add_loot(self, items: Vec<(&str, i32)>) -> ActionResult<()> {
|
||||||
for item_desc in items.into_iter() {
|
for item_desc in items.into_iter() {
|
||||||
let new_item = models::item::NewLoot::to_group(item_desc);
|
let new_item = models::item::NewLoot::to_group(item_desc);
|
||||||
diesel::insert_into(schema::looted::table)
|
diesel::insert_into(schema::looted::table)
|
||||||
.values(&new_item)
|
.values(&new_item)
|
||||||
.execute(self.0)?;
|
.execute(self.0)?;
|
||||||
}
|
}
|
||||||
Ok(ActionStatus::ok())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Resolve all pending claims and dispatch claimed items.
|
/// Resolve all pending claims and dispatch claimed items.
|
||||||
@@ -283,10 +268,32 @@ impl<'q> AsAdmin<'q> {
|
|||||||
.grouped_by(&loot);
|
.grouped_by(&loot);
|
||||||
// For each claimed item
|
// For each claimed item
|
||||||
let data = loot.into_iter().zip(claims).collect::<Vec<_>>();
|
let data = loot.into_iter().zip(claims).collect::<Vec<_>>();
|
||||||
dbg!(data);
|
dbg!(&data);
|
||||||
// If mutiples claims -> find highest resolve, give to this player
|
|
||||||
// If only one claim -> give to claiming
|
for (loot, claims) in data {
|
||||||
Ok(ActionStatus::nop())
|
match claims.len() {
|
||||||
|
1 => {
|
||||||
|
let claim = claims.get(0).unwrap();
|
||||||
|
let player_id = claim.player_id;
|
||||||
|
self.0.transaction(|| {
|
||||||
|
use schema::looted::dsl::*;
|
||||||
|
diesel::update(looted.find(claim.loot_id))
|
||||||
|
.set(owner_id.eq(player_id))
|
||||||
|
.execute(self.0)?;
|
||||||
|
diesel::delete(schema::claims::table.find(claim.id))
|
||||||
|
.execute(self.0)?;
|
||||||
|
{
|
||||||
|
use schema::players::dsl::*;
|
||||||
|
diesel::update(players.find(player_id))
|
||||||
|
.set(debt.eq(debt + (loot.base_price / 2)))
|
||||||
|
.execute(self.0)
|
||||||
|
}
|
||||||
|
})?;
|
||||||
|
},
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -316,7 +323,7 @@ mod tests {
|
|||||||
/// When migrations are run, a special player with id 0 and name "Groupe"
|
/// When migrations are run, a special player with id 0 and name "Groupe"
|
||||||
/// must be created.
|
/// must be created.
|
||||||
#[test]
|
#[test]
|
||||||
fn test_group_is_autocreated() {
|
fn global_group_is_autocreated() {
|
||||||
let conn = test_connection();
|
let conn = test_connection();
|
||||||
let players = DbApi::with_conn(&conn).fetch_players().unwrap();
|
let players = DbApi::with_conn(&conn).fetch_players().unwrap();
|
||||||
assert_eq!(players.len(), 1);
|
assert_eq!(players.len(), 1);
|
||||||
@@ -328,20 +335,19 @@ mod tests {
|
|||||||
/// When a player updates wealth, a difference is returned by API.
|
/// When a player updates wealth, a difference is returned by API.
|
||||||
/// Added to the previous amount of coins, it should equal the updated weath.
|
/// Added to the previous amount of coins, it should equal the updated weath.
|
||||||
#[test]
|
#[test]
|
||||||
fn test_player_updates_wealth() {
|
fn as_player_updates_wealth() {
|
||||||
let conn = test_connection();
|
let conn = test_connection();
|
||||||
DbApi::with_conn(&conn)
|
DbApi::with_conn(&conn)
|
||||||
.as_admin()
|
.as_admin()
|
||||||
.add_player("PlayerName".to_string(), 403.21)
|
.add_player("PlayerName", 403.21)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let diff = DbApi::with_conn(&conn)
|
let diff = DbApi::with_conn(&conn)
|
||||||
.as_player(1)
|
.as_player(1)
|
||||||
.update_wealth(-401.21)
|
.update_wealth(-401.21)
|
||||||
.unwrap()
|
.ok();
|
||||||
.response
|
|
||||||
.unwrap();
|
|
||||||
// Check the returned diff
|
// Check the returned diff
|
||||||
assert_eq!(diff, (-1, -2, -1, -4));
|
assert_eq!(diff, Some((-1, -2, -1, -4)));
|
||||||
|
let diff = diff.unwrap();
|
||||||
let players = DbApi::with_conn(&conn).fetch_players().unwrap();
|
let players = DbApi::with_conn(&conn).fetch_players().unwrap();
|
||||||
let player = players.get(1).unwrap();
|
let player = players.get(1).unwrap();
|
||||||
// Check that we can add old value to return diff to get resulting value
|
// Check that we can add old value to return diff to get resulting value
|
||||||
@@ -352,13 +358,12 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_admin_add_player() {
|
fn as_admin_add_player() {
|
||||||
let conn = test_connection();
|
let conn = test_connection();
|
||||||
let result = DbApi::with_conn(&conn)
|
let result = DbApi::with_conn(&conn)
|
||||||
.as_admin()
|
.as_admin()
|
||||||
.add_player("PlayerName".to_string(), 403.21)
|
.add_player("PlayerName", 403.21);
|
||||||
.unwrap();
|
assert_eq!(result.is_ok(), true);
|
||||||
assert_eq!(result.executed, true);
|
|
||||||
let players = DbApi::with_conn(&conn).fetch_players().unwrap();
|
let players = DbApi::with_conn(&conn).fetch_players().unwrap();
|
||||||
assert_eq!(players.len(), 2);
|
assert_eq!(players.len(), 2);
|
||||||
let new_player = players.get(1).unwrap();
|
let new_player = players.get(1).unwrap();
|
||||||
@@ -370,56 +375,80 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_admin_resolve_claims() {
|
fn as_admin_resolve_claims() {
|
||||||
let conn = test_connection();
|
let conn = test_connection();
|
||||||
let claims = DbApi::with_conn(&conn).fetch_claims().unwrap();
|
let claims = DbApi::with_conn(&conn).fetch_claims().unwrap();
|
||||||
assert_eq!(claims.len(), 0);
|
assert_eq!(claims.len(), 0);
|
||||||
assert_eq!(true, false); // Failing as test is not complete
|
|
||||||
|
// Add items
|
||||||
|
assert_eq!(DbApi::with_conn(&conn).as_admin().add_loot(vec![
|
||||||
|
("Épée", 40),
|
||||||
|
("Arc", 40),
|
||||||
|
]).is_ok(), true);
|
||||||
|
// Add players
|
||||||
|
DbApi::with_conn(&conn).as_admin().add_player("Player1", 0.0).unwrap();
|
||||||
|
DbApi::with_conn(&conn).as_admin().add_player("Player2", 0.0).unwrap();
|
||||||
|
// Put claims on one different item each
|
||||||
|
DbApi::with_conn(&conn).as_player(1).claim(1).unwrap();
|
||||||
|
DbApi::with_conn(&conn).as_player(2).claim(2).unwrap();
|
||||||
|
let result = DbApi::with_conn(&conn).as_admin().resolve_claims();
|
||||||
|
assert_eq!(result.is_ok(), true);
|
||||||
|
// Check that both players received an item
|
||||||
|
let players = DbApi::with_conn(&conn).fetch_players().unwrap();
|
||||||
|
for &i in [1, 2].into_iter() {
|
||||||
|
assert_eq!(DbApi::with_conn(&conn).as_player(i).loot().unwrap().len(), 1);
|
||||||
|
let player = players.get(i as usize).unwrap();
|
||||||
|
assert_eq!(player.debt, 20);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_player_claim_item() {
|
fn as_player_claim_item() {
|
||||||
let conn = test_connection();
|
let conn = test_connection();
|
||||||
DbApi::with_conn(&conn)
|
DbApi::with_conn(&conn)
|
||||||
.as_admin()
|
.as_admin()
|
||||||
.add_player("Player".to_string(), 0.0)
|
.add_player("Player", 0.0)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
DbApi::with_conn(&conn)
|
DbApi::with_conn(&conn)
|
||||||
.as_admin()
|
.as_admin()
|
||||||
.add_loot(vec![("Épée", 25)])
|
.add_loot(vec![("Épée", 25)])
|
||||||
.unwrap();
|
.unwrap();
|
||||||
// Claim an existing item
|
// Claim an existing item
|
||||||
let result = DbApi::with_conn(&conn).as_player(1).claim(1).unwrap();
|
let result = DbApi::with_conn(&conn).as_player(1).claim(1);
|
||||||
assert_eq!(result.executed, true);
|
assert_eq!(result.is_ok(), true);
|
||||||
let claims = DbApi::with_conn(&conn).fetch_claims().unwrap();
|
let claims = DbApi::with_conn(&conn).fetch_claims().unwrap();
|
||||||
assert_eq!(claims.len(), 1);
|
assert_eq!(claims.len(), 1);
|
||||||
let claim = claims.get(0).unwrap();
|
let claim = claims.get(0).unwrap();
|
||||||
assert_eq!(claim.player_id, 1);
|
assert_eq!(claim.player_id, 1);
|
||||||
assert_eq!(claim.loot_id, 1);
|
assert_eq!(claim.loot_id, 1);
|
||||||
// Claim an inexistant item
|
// Claim an inexistant item
|
||||||
let result = DbApi::with_conn(&conn).as_player(1).claim(2).unwrap();
|
let result = DbApi::with_conn(&conn).as_player(1).claim(2);
|
||||||
assert_eq!(result.executed, false);
|
assert_eq!(result.is_ok(), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_player_unclaim_item() {
|
fn as_player_unclaim_item() {
|
||||||
let conn = test_connection();
|
let conn = test_connection();
|
||||||
DbApi::with_conn(&conn)
|
DbApi::with_conn(&conn)
|
||||||
.as_admin()
|
.as_admin()
|
||||||
.add_player("Player".to_string(), 0.0)
|
.add_player("Player", 0.0)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
DbApi::with_conn(&conn)
|
DbApi::with_conn(&conn)
|
||||||
.as_admin()
|
.as_admin()
|
||||||
.add_loot(vec![("Épée", 25)])
|
.add_loot(vec![("Épée", 25)])
|
||||||
.unwrap();
|
.unwrap();
|
||||||
// Claim an existing item
|
// Claim an existing item
|
||||||
let result = DbApi::with_conn(&conn).as_player(1).claim(1).unwrap();
|
let result = DbApi::with_conn(&conn).as_player(1).claim(1);
|
||||||
assert_eq!(result.executed, true);
|
assert_eq!(result.is_ok(), true);
|
||||||
let result = DbApi::with_conn(&conn).as_player(1).unclaim(1).unwrap();
|
// Claiming twice is an error
|
||||||
assert_eq!(result.executed, true);
|
let result = DbApi::with_conn(&conn).as_player(1).claim(1);
|
||||||
// Check that unclaimed items will not be unclaimed...
|
assert_eq!(result.is_ok(), false);
|
||||||
let result = DbApi::with_conn(&conn).as_player(1).unclaim(1).unwrap();
|
// Unclaiming and item
|
||||||
assert_eq!(result.executed, false);
|
let result = DbApi::with_conn(&conn).as_player(1).unclaim(1);
|
||||||
|
assert_eq!(result.is_ok(), true);
|
||||||
|
// Check that not claimed items will not be unclaimed...
|
||||||
|
let result = DbApi::with_conn(&conn).as_player(1).unclaim(1);
|
||||||
|
assert_eq!(result.is_ok(), false);
|
||||||
let claims = DbApi::with_conn(&conn).fetch_claims().unwrap();
|
let claims = DbApi::with_conn(&conn).fetch_claims().unwrap();
|
||||||
assert_eq!(claims.len(), 0);
|
assert_eq!(claims.len(), 0);
|
||||||
}
|
}
|
||||||
@@ -428,20 +457,19 @@ mod tests {
|
|||||||
///
|
///
|
||||||
/// Checks that player's chest and wealth are updated.
|
/// Checks that player's chest and wealth are updated.
|
||||||
/// Checks that items are sold at half their value.
|
/// Checks that items are sold at half their value.
|
||||||
|
/// Checks that a player cannot sell item he does not own.
|
||||||
#[test]
|
#[test]
|
||||||
fn test_buy_sell_simple() {
|
fn as_player_simple_buy_sell() {
|
||||||
let conn = test_connection();
|
let conn = test_connection();
|
||||||
DbApi::with_conn(&conn)
|
DbApi::with_conn(&conn)
|
||||||
.as_admin()
|
.as_admin()
|
||||||
.add_player("Player".to_string(), 1000.0)
|
.add_player("Player", 1000.0)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
// Buy an item
|
// Buy an item
|
||||||
let bought = DbApi::with_conn(&conn)
|
let bought = DbApi::with_conn(&conn)
|
||||||
.as_player(1)
|
.as_player(1)
|
||||||
.buy("Sword", 800)
|
.buy("Sword", 800);
|
||||||
.unwrap();
|
assert_eq!(bought.ok(), Some((0, 0, 0, -8))); // Returns diff of player wealth ?
|
||||||
assert_eq!(bought.executed, true); // Was updated ?
|
|
||||||
assert_eq!(bought.response, Some((0, 0, 0, -8))); // Returns diff of player wealth ?
|
|
||||||
let chest = DbApi::with_conn(&conn).as_player(1).loot().unwrap();
|
let chest = DbApi::with_conn(&conn).as_player(1).loot().unwrap();
|
||||||
assert_eq!(chest.len(), 1);
|
assert_eq!(chest.len(), 1);
|
||||||
let loot = chest.get(0).unwrap();
|
let loot = chest.get(0).unwrap();
|
||||||
@@ -450,13 +478,12 @@ mod tests {
|
|||||||
let players = DbApi::with_conn(&conn).fetch_players().unwrap();
|
let players = DbApi::with_conn(&conn).fetch_players().unwrap();
|
||||||
let player = players.get(1).unwrap();
|
let player = players.get(1).unwrap();
|
||||||
assert_eq!(player.pp, 2);
|
assert_eq!(player.pp, 2);
|
||||||
|
// A player cannot sell loot from an other's chest
|
||||||
|
let result = DbApi::with_conn(&conn).as_player(0).sell(loot.id, None);
|
||||||
|
assert_eq!(result.is_ok(), false);
|
||||||
// Sell back
|
// Sell back
|
||||||
let sold = DbApi::with_conn(&conn)
|
let sold = DbApi::with_conn(&conn).as_player(1).sell(loot.id, None);
|
||||||
.as_player(1)
|
assert_eq!(sold.ok(), Some((0, 0, 0, 4)));
|
||||||
.sell(loot.id, None)
|
|
||||||
.unwrap();
|
|
||||||
assert_eq!(sold.executed, true);
|
|
||||||
assert_eq!(sold.response, Some((0, 0, 0, 4)));
|
|
||||||
let chest = DbApi::with_conn(&conn).as_player(1).loot().unwrap();
|
let chest = DbApi::with_conn(&conn).as_player(1).loot().unwrap();
|
||||||
assert_eq!(chest.len(), 0);
|
assert_eq!(chest.len(), 0);
|
||||||
let players = DbApi::with_conn(&conn).fetch_players().unwrap();
|
let players = DbApi::with_conn(&conn).fetch_players().unwrap();
|
||||||
@@ -465,7 +492,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_admin_add_loot() {
|
fn as_admin_add_loot() {
|
||||||
let conn = test_connection();
|
let conn = test_connection();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
0,
|
0,
|
||||||
@@ -474,9 +501,8 @@ mod tests {
|
|||||||
let loot_to_add = vec![("Cape d'invisibilité", 8000), ("Arc long", 25)];
|
let loot_to_add = vec![("Cape d'invisibilité", 8000), ("Arc long", 25)];
|
||||||
let result = DbApi::with_conn(&conn)
|
let result = DbApi::with_conn(&conn)
|
||||||
.as_admin()
|
.as_admin()
|
||||||
.add_loot(loot_to_add.clone())
|
.add_loot(loot_to_add.clone());
|
||||||
.unwrap();
|
assert_eq!(result.is_ok(), true);
|
||||||
assert_eq!(result.executed, true);
|
|
||||||
let looted = DbApi::with_conn(&conn).as_player(0).loot().unwrap();
|
let looted = DbApi::with_conn(&conn).as_player(0).loot().unwrap();
|
||||||
assert_eq!(looted.len(), 2);
|
assert_eq!(looted.len(), 2);
|
||||||
// NB: Not a problem now, but this adds constraints of items being
|
// NB: Not a problem now, but this adds constraints of items being
|
||||||
|
|||||||
@@ -35,8 +35,8 @@ type OwnedLoot = Filter<looted::table, WithOwner>;
|
|||||||
pub(crate) struct Loot {
|
pub(crate) struct Loot {
|
||||||
id: i32,
|
id: i32,
|
||||||
name: String,
|
name: String,
|
||||||
base_price: i32,
|
pub(crate) base_price: i32,
|
||||||
owner: i32,
|
pub(crate) owner: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Loot {
|
impl Loot {
|
||||||
|
|||||||
@@ -1,295 +0,0 @@
|
|||||||
//! TODO:
|
|
||||||
//! Extract actions provided by API into their dedicated module.
|
|
||||||
//! Will allow more flexibilty to combinate them inside API methods.
|
|
||||||
//! Should make it easier to add a new feature : Reverting an action
|
|
||||||
//!
|
|
||||||
use crate::models;
|
|
||||||
use crate::schema;
|
|
||||||
use crate::DbConnection;
|
|
||||||
use diesel::prelude::*;
|
|
||||||
// TODO: revertable actions :
|
|
||||||
// - Buy
|
|
||||||
// - Sell
|
|
||||||
// - UpdateWealth
|
|
||||||
pub type TransactionResult<T> = Result<T, diesel::result::Error>;
|
|
||||||
|
|
||||||
|
|
||||||
pub trait DbTransaction {
|
|
||||||
type Params;
|
|
||||||
type Response: serde::Serialize;
|
|
||||||
|
|
||||||
fn execute<'q>(
|
|
||||||
self,
|
|
||||||
conn: &'q DbConnection,
|
|
||||||
params: Self::Params,
|
|
||||||
) -> TransactionResult<Self::Response>;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait Revertable : DbTransaction {
|
|
||||||
fn revert<'q>(
|
|
||||||
self,
|
|
||||||
conn: &'q DbConnection,
|
|
||||||
player_id: i32,
|
|
||||||
params: <Self as DbTransaction>::Response,
|
|
||||||
) -> TransactionResult<<Self as DbTransaction>::Response>;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Return status of an Action
|
|
||||||
#[derive(Serialize, Debug)]
|
|
||||||
pub struct ActionStatus<R: serde::Serialize> {
|
|
||||||
/// Has the action made changes ?
|
|
||||||
pub executed: bool,
|
|
||||||
/// Response payload
|
|
||||||
pub response: R,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ActionStatus<()> {
|
|
||||||
pub fn was_updated(updated_lines: usize) -> Self {
|
|
||||||
match updated_lines {
|
|
||||||
1 => Self::ok(),
|
|
||||||
_ => Self::nop(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn ok() -> ActionStatus<()> {
|
|
||||||
Self {
|
|
||||||
executed: true,
|
|
||||||
response: (),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: Default + serde::Serialize> ActionStatus<T> {
|
|
||||||
pub fn nop() -> ActionStatus<T> {
|
|
||||||
Self {
|
|
||||||
executed: false,
|
|
||||||
response: Default::default(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Or a module ?
|
|
||||||
pub(crate) mod player {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
pub struct AddLootParams {
|
|
||||||
pub player_id: i32,
|
|
||||||
pub loot_name: String,
|
|
||||||
pub loot_price: i32,
|
|
||||||
}
|
|
||||||
pub struct Buy;
|
|
||||||
|
|
||||||
enum LootTransactionKind {
|
|
||||||
Buy,
|
|
||||||
Sell,
|
|
||||||
}
|
|
||||||
#[derive(Serialize, Debug)]
|
|
||||||
pub struct LootTransaction {
|
|
||||||
player_id: i32,
|
|
||||||
loot_id: i32,
|
|
||||||
kind: LootTransactionKind,
|
|
||||||
pub loot_cost: (i32, i32, i32, i32),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl DbTransaction for Buy {
|
|
||||||
type Params = AddLootParams;
|
|
||||||
type Response = LootTransaction;
|
|
||||||
|
|
||||||
fn execute<'q>(
|
|
||||||
self,
|
|
||||||
conn: &'q DbConnection,
|
|
||||||
params: Self::Params,
|
|
||||||
) -> TransactionResult<Self::Response> {
|
|
||||||
let added_item = {
|
|
||||||
let new_item = models::item::NewLoot::to_player(
|
|
||||||
params.player_id,
|
|
||||||
(¶ms.loot_name, params.loot_price),
|
|
||||||
);
|
|
||||||
diesel::insert_into(schema::looted::table)
|
|
||||||
.values(&new_item)
|
|
||||||
.execute(conn)?
|
|
||||||
// TODO: return ID of inserted item
|
|
||||||
};
|
|
||||||
let updated_wealth = UpdateWealth.execute(
|
|
||||||
conn,
|
|
||||||
WealthParams {
|
|
||||||
player_id: params.player_id,
|
|
||||||
value_in_gp: -(params.loot_price as f32),
|
|
||||||
},
|
|
||||||
);
|
|
||||||
match (added_item, updated_wealth) {
|
|
||||||
(1, Ok(loot_cost)) => Ok(LootTransaction {
|
|
||||||
kind: LootTransactionKind::Buy,
|
|
||||||
player_id: params.player_id,
|
|
||||||
loot_id: 0, //TODO: find added item ID
|
|
||||||
loot_cost,
|
|
||||||
}),
|
|
||||||
// TODO: Handle other cases
|
|
||||||
_ => panic!()
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Revertable for Buy {
|
|
||||||
fn revert<'q>(self, conn: &'q DbConnection, player_id: i32, params: <Self as DbTransaction>::Response)
|
|
||||||
-> TransactionResult<<Self as DbTransaction>::Response> {
|
|
||||||
unimplemented!()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct LootParams {
|
|
||||||
pub player_id: i32,
|
|
||||||
pub loot_id: i32,
|
|
||||||
}
|
|
||||||
pub struct Sell;
|
|
||||||
impl DbTransaction for Sell {
|
|
||||||
type Params = LootParams;
|
|
||||||
type Response = LootTransaction;
|
|
||||||
fn execute<'q>(
|
|
||||||
self,
|
|
||||||
conn: &DbConnection,
|
|
||||||
params: Self::Params,
|
|
||||||
) -> TransactionResult<Self::Response> {
|
|
||||||
use schema::looted::dsl::*;
|
|
||||||
let loot_value = looted
|
|
||||||
.find(params.loot_id)
|
|
||||||
.select(base_price)
|
|
||||||
.first::<i32>(conn)?;
|
|
||||||
let sell_value = (loot_value / 2) as f32;
|
|
||||||
diesel::delete(looted.find(params.loot_id))
|
|
||||||
.execute(conn)
|
|
||||||
.and_then(|r| match r {
|
|
||||||
// On deletion, update this player wealth
|
|
||||||
1 => Ok(UpdateWealth
|
|
||||||
.execute(
|
|
||||||
conn,
|
|
||||||
WealthParams {
|
|
||||||
player_id: params.player_id,
|
|
||||||
value_in_gp: sell_value as f32,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.unwrap()),
|
|
||||||
_ => Ok(ActionStatus {
|
|
||||||
executed: false,
|
|
||||||
response: None,
|
|
||||||
}),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct PutClaim;
|
|
||||||
impl DbTransaction for PutClaim {
|
|
||||||
type Params = LootParams;
|
|
||||||
type Response = ();
|
|
||||||
fn execute<'q>(
|
|
||||||
self,
|
|
||||||
conn: &DbConnection,
|
|
||||||
params: Self::Params,
|
|
||||||
) -> TransactionResult<Self::Response> {
|
|
||||||
let claim = models::claim::NewClaim::new(params.player_id, params.loot_id);
|
|
||||||
diesel::insert_into(schema::claims::table)
|
|
||||||
.values(&claim)
|
|
||||||
.execute(conn)
|
|
||||||
.and_then(|_| Ok(()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct WithdrawClaim;
|
|
||||||
impl DbTransaction for WithdrawClaim {
|
|
||||||
type Params = LootParams;
|
|
||||||
type Response = ();
|
|
||||||
fn execute<'q>(
|
|
||||||
self,
|
|
||||||
conn: &DbConnection,
|
|
||||||
params: Self::Params,
|
|
||||||
) -> TransactionResult<Self::Response> {
|
|
||||||
use schema::claims::dsl::*;
|
|
||||||
diesel::delete(
|
|
||||||
claims
|
|
||||||
.filter(loot_id.eq(params.loot_id))
|
|
||||||
.filter(player_id.eq(params.player_id)),
|
|
||||||
)
|
|
||||||
.execute(conn)
|
|
||||||
.and_then(|_| Ok(()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct WealthParams {
|
|
||||||
pub player_id: i32,
|
|
||||||
pub value_in_gp: f32,
|
|
||||||
}
|
|
||||||
pub struct UpdateWealth;
|
|
||||||
|
|
||||||
impl DbTransaction for UpdateWealth {
|
|
||||||
type Params = WealthParams;
|
|
||||||
type Response = (i32, i32, i32, i32);
|
|
||||||
|
|
||||||
fn execute<'q>(
|
|
||||||
self,
|
|
||||||
conn: &'q DbConnection,
|
|
||||||
params: WealthParams,
|
|
||||||
) -> TransactionResult<Self::Response> {
|
|
||||||
use schema::players::dsl::*;
|
|
||||||
let current_wealth = players
|
|
||||||
.find(params.player_id)
|
|
||||||
.select((cp, sp, gp, pp))
|
|
||||||
.first::<models::Wealth>(conn)?;
|
|
||||||
// TODO: improve thisdiesel dependant transaction
|
|
||||||
// should be move inside a WealthUpdate method
|
|
||||||
let updated_wealth =
|
|
||||||
models::Wealth::from_gp(current_wealth.to_gp() + params.value_in_gp);
|
|
||||||
// Difference in coins that is sent back
|
|
||||||
let (old, new) = (current_wealth.as_tuple(), updated_wealth.as_tuple());
|
|
||||||
let diff = (new.0 - old.0, new.1 - old.1, new.2 - old.2, new.3 - old.3);
|
|
||||||
diesel::update(players)
|
|
||||||
.filter(id.eq(params.player_id))
|
|
||||||
.set(&updated_wealth)
|
|
||||||
.execute(conn)
|
|
||||||
.and_then(|r| match r {
|
|
||||||
1 => Ok(diff),
|
|
||||||
_ => panic!("UpdateWealth made no changes !"),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Revertable for UpdateWealth {
|
|
||||||
fn revert<'q>(
|
|
||||||
self,
|
|
||||||
conn: &'q DbConnection,
|
|
||||||
player_id: i32,
|
|
||||||
params: <Self as DbTransaction>::Response,
|
|
||||||
) -> TransactionResult<<Self as DbTransaction>::Response> {
|
|
||||||
use schema::players::dsl::*;
|
|
||||||
let cur_wealth = players
|
|
||||||
.find(player_id)
|
|
||||||
.select((cp, sp, gp, pp))
|
|
||||||
.first::<models::Wealth>(conn)?;
|
|
||||||
let reverted_wealth = models::player::Wealth {
|
|
||||||
cp: cur_wealth.cp - params.0,
|
|
||||||
sp: cur_wealth.cp - params.1,
|
|
||||||
gp: cur_wealth.cp - params.2,
|
|
||||||
pp: cur_wealth.cp - params.3,
|
|
||||||
};
|
|
||||||
// Difference in coins that is sent back
|
|
||||||
let diff = ( -params.0, -params.1, -params.2, -params.3);
|
|
||||||
diesel::update(players)
|
|
||||||
.filter(id.eq(params.0))
|
|
||||||
.set(&reverted_wealth)
|
|
||||||
.execute(conn)
|
|
||||||
.and_then(|r| match r {
|
|
||||||
1 => Ok(diff),
|
|
||||||
_ => panic!("RevertableWealthUpdate made no changes"),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
pub(crate) mod admin {
|
|
||||||
pub struct AddPlayer;
|
|
||||||
pub struct AddLoot;
|
|
||||||
pub struct SellLoot;
|
|
||||||
pub struct ResolveClaims;
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -2,7 +2,6 @@ module.exports = {
|
|||||||
presets: [
|
presets: [
|
||||||
'@vue/app'
|
'@vue/app'
|
||||||
],
|
],
|
||||||
"presets": [["env", { "modules": false }]],
|
|
||||||
"env": {
|
"env": {
|
||||||
"test": {
|
"test": {
|
||||||
"presets": [["env", { "targets": { "node": "current" } }]]
|
"presets": [["env", { "targets": { "node": "current" } }]]
|
||||||
|
|||||||
15047
lootalot_front/package-lock.json
generated
15047
lootalot_front/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -21,6 +21,7 @@
|
|||||||
"@vue/cli-plugin-eslint": "^3.8.0",
|
"@vue/cli-plugin-eslint": "^3.8.0",
|
||||||
"@vue/cli-service": "^3.8.0",
|
"@vue/cli-service": "^3.8.0",
|
||||||
"@vue/test-utils": "^1.0.0-beta.29",
|
"@vue/test-utils": "^1.0.0-beta.29",
|
||||||
|
"babel-core": "^6.26.3",
|
||||||
"babel-eslint": "^10.0.1",
|
"babel-eslint": "^10.0.1",
|
||||||
"babel-jest": "^24.8.0",
|
"babel-jest": "^24.8.0",
|
||||||
"babel-preset-env": "^1.7.0",
|
"babel-preset-env": "^1.7.0",
|
||||||
|
|||||||
@@ -1,11 +1,6 @@
|
|||||||
<template>
|
<template>
|
||||||
<main id="app" class="section">
|
<main id="app" class="container">
|
||||||
<section id="content" class="columns is-desktop">
|
|
||||||
<Player></Player>
|
<Player></Player>
|
||||||
<div class="column">
|
|
||||||
<Chest :player="0" v-if="state.initiated"></Chest>
|
|
||||||
</div>
|
|
||||||
</section>
|
|
||||||
</main>
|
</main>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
|||||||
@@ -116,17 +116,14 @@ export const AppStorage = {
|
|||||||
},
|
},
|
||||||
updatePlayerWealth (goldValue) {
|
updatePlayerWealth (goldValue) {
|
||||||
return Api.updateWealth(this.state.player_id, goldValue)
|
return Api.updateWealth(this.state.player_id, goldValue)
|
||||||
.then(done => {
|
.then(response => {
|
||||||
if (done.executed) {
|
|
||||||
// Update player wealth
|
// Update player wealth
|
||||||
var diff = done.response;
|
var diff = response;
|
||||||
if (this.debug) console.log('updatePlayerWealth', diff)
|
if (this.debug) console.log('updatePlayerWealth', diff)
|
||||||
this.state.player_list[this.state.player_id].cp += diff[0];
|
this.state.player_list[this.state.player_id].cp += diff[0];
|
||||||
this.state.player_list[this.state.player_id].sp += diff[1];
|
this.state.player_list[this.state.player_id].sp += diff[1];
|
||||||
this.state.player_list[this.state.player_id].gp += diff[2];
|
this.state.player_list[this.state.player_id].gp += diff[2];
|
||||||
this.state.player_list[this.state.player_id].pp += diff[3];
|
this.state.player_list[this.state.player_id].pp += diff[3];
|
||||||
}
|
|
||||||
return done.executed;
|
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
// Put a claim on an item from group chest.
|
// Put a claim on an item from group chest.
|
||||||
@@ -134,12 +131,8 @@ export const AppStorage = {
|
|||||||
const playerId = this.state.player_id
|
const playerId = this.state.player_id
|
||||||
Api.putClaim(playerId, itemId)
|
Api.putClaim(playerId, itemId)
|
||||||
.then(done => {
|
.then(done => {
|
||||||
if (done.executed) {
|
// Update cliend-side state
|
||||||
// Update cliend-side state
|
this.state.player_claims[playerId].push(itemId);
|
||||||
this.state.player_claims[playerId].push(itemId);
|
|
||||||
} else {
|
|
||||||
if (this.debug) console.log("API responded with 'false'")
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
// Withdraws a claim.
|
// Withdraws a claim.
|
||||||
@@ -147,16 +140,12 @@ export const AppStorage = {
|
|||||||
const playerId = this.state.player_id
|
const playerId = this.state.player_id
|
||||||
Api.unClaim(playerId, itemId)
|
Api.unClaim(playerId, itemId)
|
||||||
.then(done => {
|
.then(done => {
|
||||||
if (done.executed) {
|
|
||||||
var idx = this.state.player_claims[playerId].indexOf(itemId);
|
var idx = this.state.player_claims[playerId].indexOf(itemId);
|
||||||
if (idx > -1) {
|
if (idx > -1) {
|
||||||
this.state.player_claims[playerId].splice(idx, 1);
|
this.state.player_claims[playerId].splice(idx, 1);
|
||||||
} else {
|
} else {
|
||||||
if (this.debug) console.log("cancel a non-existent request")
|
if (this.debug) console.log("cancel a non-existent request")
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
if (this.debug) console.log("API responded with 'false'")
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,88 +1,50 @@
|
|||||||
<template>
|
<template>
|
||||||
<div class="container is-paddingless">
|
<table class="table is-fullwidth is-striped">
|
||||||
<div v-if="mainControlsDisplayed"
|
<thead>
|
||||||
class="columns is-mobile is-vcentered"
|
<tr>
|
||||||
>
|
<th>{{ player == 0 ? 'Coffre de groupe' : 'Objets'}}</th>
|
||||||
<div class="column is-narrow">
|
<th v-if="canGrab"></th>
|
||||||
<span class="icon is-large">
|
<th v-if="canSell">
|
||||||
<i class="fas fa-2x fa-dragon"></i>
|
<div class="buttons is-right">
|
||||||
</span>
|
<button class="button" :class="is_selling ? 'is-danger' : 'is-warning'"
|
||||||
</div>
|
@click="is_selling = !is_selling">
|
||||||
<div class="column has-text-left">
|
<span class="icon"><i class="fas fa-coins"></i></span>
|
||||||
<h1 class="title">Coffre de groupe</h1>
|
<p v-if="!is_selling">Vendre</p>
|
||||||
</div>
|
<p v-else>{{ totalSellValue ? totalSellValue : 'Annuler' }}</p>
|
||||||
<div class="column" v-show="canAdd">
|
|
||||||
<div v-show="mainControlsDisplayed" class="buttons is-right">
|
|
||||||
<button v-if="canAdd"
|
|
||||||
class="button is-inverted is-info"
|
|
||||||
@click="is_adding = true"
|
|
||||||
>
|
|
||||||
<span class="icon">
|
|
||||||
<i class="fas fa-box-open"></i>
|
|
||||||
</span>
|
|
||||||
<p>Nouveau loot</p>
|
|
||||||
</button>
|
|
||||||
<button class="button is-inverted is-primary">
|
|
||||||
<span class="icon">
|
|
||||||
<i class="fas fa-coins"></i>
|
|
||||||
</span>
|
|
||||||
<p>Tout vendre</p>
|
|
||||||
</button>
|
</button>
|
||||||
|
<PercentInput v-show="is_selling"></PercentInput>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</th>
|
||||||
</div>
|
</tr>
|
||||||
<Loot v-if="is_adding" @done="is_adding = false"></Loot>
|
</thead>
|
||||||
<table v-else class="table is-fullwidth is-striped" >
|
<tbody v-if="app_state.initiated">
|
||||||
<thead>
|
<template v-for="(item, idx) in content">
|
||||||
<tr>
|
<tr :key="`row-${idx}`">
|
||||||
<th>Objets de {{ player }}</th>
|
<td>
|
||||||
<th v-if="canGrab"></th>
|
<strong>{{item.name}}</strong>
|
||||||
<th v-if="canSell">
|
</td>
|
||||||
<div class="buttons is-right">
|
<td v-if="canGrab">
|
||||||
<button class="button"
|
<Request :item="item.id"></Request>
|
||||||
:class="is_selling ? 'is-danger' : 'is-warning'"
|
</td>
|
||||||
@click="is_selling = !is_selling"
|
<td v-if="canSell">
|
||||||
>
|
<div class="field is-grouped is-pulled-right" v-show="is_selling">
|
||||||
<span class="icon">
|
<div class="control">
|
||||||
<i class="fas fa-coins"></i>
|
<label class="label">
|
||||||
</span>
|
<input type="checkbox"
|
||||||
<p v-if="!is_selling">
|
id="`item-${idx}`"
|
||||||
Vendre</p>
|
:value="item.id"
|
||||||
<p v-else>
|
v-model="sell_selected">
|
||||||
{{ totalSellValue ? totalSellValue : 'Annuler' }}</p>
|
{{item.base_price / 2}} GP
|
||||||
</button>
|
</label>
|
||||||
<PercentInput v-show="is_selling">
|
|
||||||
</PercentInput>
|
|
||||||
</div>
|
</div>
|
||||||
</th>
|
<PercentInput></PercentInput>
|
||||||
</tr>
|
</div>
|
||||||
</thead>
|
</td>
|
||||||
<tbody>
|
</tr>
|
||||||
<template v-for="(item, idx) in content">
|
</template>
|
||||||
<tr :key="`row-${idx}`">
|
</tbody>
|
||||||
<td>{{item.name}}</td>
|
<p v-else>Loading...</p>
|
||||||
<td v-if="canGrab">
|
</table>
|
||||||
<Request :item="item.id"></Request>
|
|
||||||
</td>
|
|
||||||
<td v-if="canSell">
|
|
||||||
<div class="field is-grouped is-pulled-right" v-show="is_selling">
|
|
||||||
<div class="control">
|
|
||||||
<label class="label">
|
|
||||||
<input type="checkbox"
|
|
||||||
id="`item-${idx}`"
|
|
||||||
:value="item.id"
|
|
||||||
v-model="sell_selected">
|
|
||||||
{{item.base_price / 2}} GP
|
|
||||||
</label>
|
|
||||||
</div>
|
|
||||||
<PercentInput></PercentInput>
|
|
||||||
</div>
|
|
||||||
</td>
|
|
||||||
</tr>
|
|
||||||
</template>
|
|
||||||
</tbody>
|
|
||||||
</table>
|
|
||||||
</div>
|
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
@@ -154,11 +116,6 @@
|
|||||||
return (this.app_state.player_id == 0
|
return (this.app_state.player_id == 0
|
||||||
&& this.player == 0);
|
&& this.player == 0);
|
||||||
},
|
},
|
||||||
// The main controls are only displayed on group chest
|
|
||||||
mainControlsDisplayed () {
|
|
||||||
return (this.player == 0
|
|
||||||
&& !this.is_adding);
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
<div class="control is-expanded"
|
<div class="control is-expanded"
|
||||||
:class="{'is-loading': is_loading }">
|
:class="{'is-loading': is_loading }">
|
||||||
<input type="text"
|
<input type="text"
|
||||||
v-model="search"
|
v-model="item_name"
|
||||||
@input="autoCompletion"
|
@input="autoCompletion"
|
||||||
class="input"
|
class="input"
|
||||||
:class="{'is-danger': no_results,
|
:class="{'is-danger': no_results,
|
||||||
@@ -12,6 +12,11 @@
|
|||||||
autocomplete="on">
|
autocomplete="on">
|
||||||
</input>
|
</input>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="control">
|
||||||
|
<input type="text" class="input"
|
||||||
|
:class="{'is-danger': !item_price}"
|
||||||
|
v-model.number="item_price"></input>
|
||||||
|
</div>
|
||||||
<div class="control">
|
<div class="control">
|
||||||
<button class="button is-primary"
|
<button class="button is-primary"
|
||||||
:disabled="no_results"
|
:disabled="no_results"
|
||||||
@@ -23,7 +28,7 @@
|
|||||||
<div class="dropdown-menu">
|
<div class="dropdown-menu">
|
||||||
<div class="dropdown-content">
|
<div class="dropdown-content">
|
||||||
<a v-for="(result,i) in results" :key="i"
|
<a v-for="(result,i) in results" :key="i"
|
||||||
@click="setResult(result.name)"
|
@click="setResult(result)"
|
||||||
class="dropdown-item"
|
class="dropdown-item"
|
||||||
>
|
>
|
||||||
{{ result.name }}
|
{{ result.name }}
|
||||||
@@ -35,18 +40,15 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
// List of items for autocomplete
|
|
||||||
const MOCK_ITEMS = [
|
|
||||||
{id: 35, name: "Cape d'invisibilité", sell_value: 30000},
|
|
||||||
{id: 8, name: "Arc long", sell_value: 10},
|
|
||||||
];
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
|
props: ["source"],
|
||||||
data () {
|
data () {
|
||||||
return {
|
return {
|
||||||
is_loading: false,
|
is_loading: false,
|
||||||
no_results: false,
|
no_results: false,
|
||||||
search: '',
|
item_name: '',
|
||||||
|
item_price: '',
|
||||||
results: [],
|
results: [],
|
||||||
auto_open: false,
|
auto_open: false,
|
||||||
};
|
};
|
||||||
@@ -55,13 +57,13 @@
|
|||||||
autoCompletion (ev) {
|
autoCompletion (ev) {
|
||||||
// TODO: a lot happens here that
|
// TODO: a lot happens here that
|
||||||
// need to be clarified
|
// need to be clarified
|
||||||
if (this.search == '') {
|
if (this.item_name == '') {
|
||||||
this.auto_open = false;
|
this.auto_open = false;
|
||||||
this.results = [];
|
this.results = [];
|
||||||
this.no_results = false;
|
this.no_results = false;
|
||||||
} else {
|
} else {
|
||||||
this.results = MOCK_ITEMS.filter(item => {
|
this.results = this.source.filter(item => {
|
||||||
return item.name.includes(this.search);
|
return item.name.includes(this.item_name);
|
||||||
});
|
});
|
||||||
// Update status
|
// Update status
|
||||||
if (this.results.length == 0) {
|
if (this.results.length == 0) {
|
||||||
@@ -73,12 +75,17 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
setResult(result) {
|
setResult(result) {
|
||||||
this.search = result;
|
this.item_name = result.name;
|
||||||
|
this.item_price = result.sell_value;
|
||||||
this.auto_open = false;
|
this.auto_open = false;
|
||||||
},
|
},
|
||||||
addItem () {
|
addItem () {
|
||||||
this.$emit("addItem", this.search);
|
this.$emit("addItem", {
|
||||||
this.search = '';
|
name: this.item_name,
|
||||||
|
sell_value: this.item_price
|
||||||
|
});
|
||||||
|
this.item_name = '';
|
||||||
|
this.item_price = '';
|
||||||
this.results = [];
|
this.results = [];
|
||||||
this.no_results = false;
|
this.no_results = false;
|
||||||
this.auto_open = false;
|
this.auto_open = false;
|
||||||
|
|||||||
@@ -1,32 +1,29 @@
|
|||||||
<template>
|
<template>
|
||||||
<div class="card is-shadowless">
|
<div>
|
||||||
<div class="card-header">
|
<p class="heading has-text-left is-size-5">
|
||||||
<p class="card-header-title">
|
Nouveau loot - {{ looted.length }} objet(s)</p>
|
||||||
Nouveau loot - {{ looted.length }} objet(s)</p>
|
<ItemInput @addItem="onAddItem" :source="inventory"></ItemInput>
|
||||||
</div>
|
<p v-for="(item, idx) in looted" :key="idx"
|
||||||
<div class="card-content">
|
class="has-text-left is-size-5">
|
||||||
<ItemInput @addItem="onAddItem"></ItemInput>
|
{{ item.name }} ({{ item.sell_value }}po)
|
||||||
<p v-for="(item, idx) in looted" :key="idx"
|
</p>
|
||||||
class="has-text-left is-size-5">
|
</div>
|
||||||
{{ item }}
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
<div class="card-footer">
|
|
||||||
<div class="card-footer-item buttons is-center">
|
|
||||||
<a class="button is-primary">Confirmer</a>
|
|
||||||
<a @click="onClose" class="button is-danger">Annuler</a>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import ItemInput from './ItemInput.vue'
|
import ItemInput from './ItemInput.vue'
|
||||||
|
// List of items for autocomplete
|
||||||
|
const MOCK_ITEMS = [
|
||||||
|
{id: 35, name: "Cape d'invisibilité", sell_value: 30000},
|
||||||
|
{id: 8, name: "Arc long", sell_value: 10},
|
||||||
|
{id: 9, name: "Arc court", sell_value: 10},
|
||||||
|
];
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
components: { ItemInput },
|
components: { ItemInput },
|
||||||
data () { return {
|
data () { return {
|
||||||
looted: [],
|
looted: [],
|
||||||
|
inventory: MOCK_ITEMS,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
|
|||||||
@@ -1,27 +0,0 @@
|
|||||||
<template>
|
|
||||||
<input type="text"
|
|
||||||
class="input"
|
|
||||||
:class="{'is-danger': has_error}"
|
|
||||||
:value="value"
|
|
||||||
@input="checkError"
|
|
||||||
></input>
|
|
||||||
</template>
|
|
||||||
|
|
||||||
<script>
|
|
||||||
export default {
|
|
||||||
props: ["value"],
|
|
||||||
data () {
|
|
||||||
return { has_error: false};
|
|
||||||
},
|
|
||||||
methods: {
|
|
||||||
checkError (ev) {
|
|
||||||
const newValue = ev.target.value;
|
|
||||||
this.has_error = isNaN(newValue);
|
|
||||||
this.$emit(
|
|
||||||
'input',
|
|
||||||
this.has_error ? 0 : Number(newValue)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
@@ -1,20 +1,18 @@
|
|||||||
<template>
|
<template>
|
||||||
<div class="field has-addons">
|
<div class="field has-addons">
|
||||||
<div v-show="is_opened" class="control has-icons-left">
|
<div v-show="is_opened" class="control has-icons-left">
|
||||||
<input class="input is-small" type="number" size="3" min=-50 max=50 step=5>
|
<input class="input is-small" type="number" size="3" min=-50 max=50 step=5>
|
||||||
<span class="icon is-small is-left">
|
<span class="icon is-small is-left">
|
||||||
<i class="fas fa-percent"></i>
|
<i class="fas fa-percent"></i>
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
<div class="control">
|
<div class="control">
|
||||||
<button class="button is-small is-outlined"
|
<button class="button is-small is-outlined" @click="is_opened = !is_opened">
|
||||||
@click="is_opened = !is_opened"
|
<small v-if="!is_opened">Mod.</small>
|
||||||
>
|
<span v-else class="icon"><i class="fas fa-times-circle"></i></span>
|
||||||
<small v-if="!is_opened">Mod.</small>
|
</button>
|
||||||
<span v-else class="icon"><i class="fas fa-times-circle"></i></span>
|
</div>
|
||||||
</button>
|
</div>
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
|||||||
@@ -1,51 +1,53 @@
|
|||||||
<template>
|
<template>
|
||||||
<div class="column is-one-third-desktop">
|
<div class="">
|
||||||
<div id="sidebar" class="card">
|
<nav class="navbar is-info">
|
||||||
<header id="sidebar-heading" class="card-header">
|
<div class="navbar-brand">
|
||||||
<p class="card-header-title">
|
<p class="navbar-item is-size-4" @click="show_wealth = !show_wealth">{{ app_state.initiated ? player.name : "..." }}</p>
|
||||||
{{ app_state.initiated ? player.name : "..." }}</p>
|
<a role="button" class="navbar-burger" aria-label="menu" aria-expanded="false">
|
||||||
<div class="dropdown is-right"
|
<span aria-hidden="true"></span>
|
||||||
:class="{ 'is-active': show_dropdown }">
|
<span aria-hidden="true"></span>
|
||||||
<div class="dropdown-trigger" ref="dropdown_btn">
|
<span aria-hidden="true"></span>
|
||||||
<a id="change_player" class="card-header-icon"
|
</a>
|
||||||
@click="show_dropdown = !show_dropdown"
|
</div>
|
||||||
aria-haspopup="true" aria-controls="dropdown-menu">
|
<div id="menu" class="navbar-menu">
|
||||||
<span class="icon is-small">
|
<div class="navbar-start" v-if="!playerIsGroup">
|
||||||
<i class="fas fa-exchange-alt"></i>
|
<a class="navbar-item" @click="switchPlayerChestVisibility">
|
||||||
</span>
|
{{ app_state.show_player_chest ? 'Coffre de groupe' : 'Mon coffre' }}</a>
|
||||||
</a>
|
</div>
|
||||||
</div>
|
<div class="navbar-end">
|
||||||
<div class="dropdown-menu" id="dropdown-menu" role="menu"
|
<div class="navbar-item has-dropdown is-hoverable">
|
||||||
v-closable="{ exclude: ['dropdown_btn'], handler: 'closeDropdown', visible: show_dropdown }">
|
<a class="navbar-link">Autres</a>
|
||||||
<div class="dropdown-content" v-if="app_state.initiated">
|
<div class="navbar-dropdown is-right">
|
||||||
<a v-for="(p,i) in app_state.player_list" :key="i"
|
<a class="navbar-item">History of Loot</a>
|
||||||
@click="setActivePlayer(i)"
|
<hr class="navbar-divider">
|
||||||
href="#" class="dropdown-item">
|
<div class="navbar-item heading">Changer</div>
|
||||||
{{ p.name }}</a>
|
<a v-for="(p,i) in app_state.player_list" :key="i"
|
||||||
</div>
|
@click="setActivePlayer(i)"
|
||||||
</div>
|
href="#" class="navbar-item">
|
||||||
</div>
|
{{ p.name }}</a>
|
||||||
</header>
|
</div>
|
||||||
<div class="card-content">
|
|
||||||
<Wealth :wealth="wealth" :debt="player.debt"></Wealth>
|
|
||||||
<div class="box is-shadowless" v-show="!playerIsGroup">
|
|
||||||
<div class="columns is-vcentered" @click="switchPlayerChestVisibility">
|
|
||||||
<div class="column is-one-fifth">
|
|
||||||
<span class="icon is-large">
|
|
||||||
<i class="fas fa-2x fa-box"></i>
|
|
||||||
</span>
|
|
||||||
</div>
|
|
||||||
<div class="column if-four-fifth has-text-left">
|
|
||||||
<p class="is-size-3">Coffre</p>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
<Chest :player="app_state.player_id"
|
|
||||||
v-show="app_state.show_player_chest">
|
|
||||||
</Chest>
|
|
||||||
<a href="#" class="button is-link is-fullwidth is-hidden" disabled>Historique</a>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
</nav>
|
||||||
|
<section class="box is-shadowless" v-if="show_wealth">
|
||||||
|
<Wealth :wealth="wealth" :debt="player.debt"></Wealth>
|
||||||
|
</section>
|
||||||
|
<section class="card">
|
||||||
|
<div class="card-content">
|
||||||
|
<Loot v-if="is_adding"></Loot>
|
||||||
|
<Chest v-else :player="shownChest"></Chest>
|
||||||
|
</div>
|
||||||
|
<div class="card-footer" v-if="shownChest == player.id">
|
||||||
|
<template v-if="is_adding">
|
||||||
|
<a class="card-footer-item">Confirmer</a>
|
||||||
|
<a class="card-footer-item" @click="toggleAdding">Annuler</a>
|
||||||
|
</template>
|
||||||
|
<a v-else class="card-footer-item" @click="toggleAdding">
|
||||||
|
+ {{ playerIsGroup ? 'Nouveau loot' : 'Acheter' }}
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
@@ -53,31 +55,17 @@
|
|||||||
import { AppStorage } from '../AppStorage'
|
import { AppStorage } from '../AppStorage'
|
||||||
import Chest from './Chest.vue'
|
import Chest from './Chest.vue'
|
||||||
import Wealth from './Wealth.vue'
|
import Wealth from './Wealth.vue'
|
||||||
|
import Loot from './Loot.vue'
|
||||||
/*
|
/*
|
||||||
The Player control board.
|
The Player control board.
|
||||||
To test :
|
|
||||||
- Player name is displayed
|
|
||||||
- Player's wealth is displayed -> Inside Wealth component
|
|
||||||
- Dropdown:
|
|
||||||
- The first item is the group
|
|
||||||
- Opened by activator
|
|
||||||
- Closed when clicked outside
|
|
||||||
- Click on item does switch active player
|
|
||||||
- Switch player :
|
|
||||||
- Name is updated when player_id is updated
|
|
||||||
- Wealth is updated -> Inside Wealth component
|
|
||||||
- Chest button controls Chest visibility
|
|
||||||
|
|
||||||
*/
|
*/
|
||||||
let handleOutsideClick;
|
|
||||||
export default {
|
export default {
|
||||||
components: { Chest, Wealth },
|
components: { Chest, Wealth, Loot },
|
||||||
data () {
|
data () {
|
||||||
return {
|
return {
|
||||||
app_state: AppStorage.state,
|
app_state: AppStorage.state,
|
||||||
show_dropdown: false,
|
is_adding: false,
|
||||||
edit_wealth: false,
|
show_wealth: true,
|
||||||
handleOutsideClick: null,
|
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
computed: {
|
computed: {
|
||||||
@@ -90,19 +78,27 @@
|
|||||||
if (!this.app_state.initiated) {
|
if (!this.app_state.initiated) {
|
||||||
return ["-", "-", "-", "-"];
|
return ["-", "-", "-", "-"];
|
||||||
} else {
|
} else {
|
||||||
const cp = this.player.cp
|
const cp = this.player.cp
|
||||||
const sp = this.player.sp
|
const sp = this.player.sp
|
||||||
const gp = this.player.gp
|
const gp = this.player.gp
|
||||||
const pp = this.player.pp
|
const pp = this.player.pp
|
||||||
return [cp, sp, gp, pp];
|
return [cp, sp, gp, pp];
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
// Check if the active player is the special 'Group' player
|
// Check if the active player is the special 'Group' player
|
||||||
playerIsGroup () {
|
playerIsGroup () {
|
||||||
return this.app_state.player_id == 0;
|
return this.app_state.player_id == 0;
|
||||||
|
},
|
||||||
|
shownChest () {
|
||||||
|
return this.app_state.show_player_chest
|
||||||
|
? this.app_state.player_id
|
||||||
|
: 0;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
|
toggleAdding () {
|
||||||
|
this.is_adding = !this.is_adding;
|
||||||
|
},
|
||||||
switchPlayerChestVisibility () {
|
switchPlayerChestVisibility () {
|
||||||
AppStorage.switchPlayerChestVisibility();
|
AppStorage.switchPlayerChestVisibility();
|
||||||
},
|
},
|
||||||
@@ -120,43 +116,6 @@
|
|||||||
this.show_dropdown = false
|
this.show_dropdown = false
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
directives: {
|
|
||||||
'closable': {
|
|
||||||
bind: function(el, binding, vnode) {
|
|
||||||
handleOutsideClick = (e) => {
|
|
||||||
e.stopPropagation();
|
|
||||||
const { exclude, handler } = binding.value;
|
|
||||||
let excludedElClicked = false;
|
|
||||||
exclude.forEach(refName => {
|
|
||||||
if (!excludedElClicked) {
|
|
||||||
const elt = vnode.context.$refs[refName];
|
|
||||||
excludedElClicked = elt.contains(e.target);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!excludedElClicked) {
|
|
||||||
console.log('outsideCloseDropdown');
|
|
||||||
vnode.context[handler]()
|
|
||||||
}
|
|
||||||
};
|
|
||||||
},
|
|
||||||
// Bind custom handler only when dropdown is visible
|
|
||||||
update: function(el, binding, vnode, _) {
|
|
||||||
const { visible } = binding.value;
|
|
||||||
if (visible) {
|
|
||||||
document.addEventListener('click', handleOutsideClick);
|
|
||||||
document.addEventListener('touchstart', handleOutsideClick);
|
|
||||||
} else {
|
|
||||||
document.removeEventListener('click', handleOutsideClick);
|
|
||||||
document.removeEventListener('touchstart', handleOutsideClick);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
unbind: function() { console.log("unbind");
|
|
||||||
document.removeEventListener('click', handleOutsideClick);
|
|
||||||
document.removeEventListener('touchstart', handleOutsideClick);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
@@ -2,25 +2,22 @@
|
|||||||
<div class="buttons is-right" >
|
<div class="buttons is-right" >
|
||||||
<template v-if="isInConflict">
|
<template v-if="isInConflict">
|
||||||
<button class="button is-success"
|
<button class="button is-success"
|
||||||
@click="cancelRequest"
|
@click="cancelRequest">
|
||||||
>
|
<span class="icon is-small">
|
||||||
<span class="icon is-small">
|
<i class="fas fa-hand-peace"></i>
|
||||||
<i class="fas fa-hand-peace"></i>
|
</span>
|
||||||
</span>
|
</button>
|
||||||
</button>
|
<button class="button is-danger"
|
||||||
<button class="button is-danger"
|
@click="hardenRequest">
|
||||||
@click="hardenRequest"
|
<span class="icon is-small">
|
||||||
>
|
<i class="fas fa-hand-middle-finger"></i>
|
||||||
<span class="icon is-small">
|
</span>
|
||||||
<i class="fas fa-hand-middle-finger"></i>
|
</button>
|
||||||
</span>
|
|
||||||
</button>
|
|
||||||
</template>
|
</template>
|
||||||
<button class="button is-primary"
|
<button class="button is-primary"
|
||||||
@click="putRequest"
|
@click="putRequest"
|
||||||
:class="{'is-outlined': isRequested}"
|
:class="{'is-small': isRequested}"
|
||||||
:disabled="isRequested"
|
:disabled="isRequested">
|
||||||
>
|
|
||||||
<span class="icon is-small">
|
<span class="icon is-small">
|
||||||
<i class="fas fa-praying-hands"></i>
|
<i class="fas fa-praying-hands"></i>
|
||||||
</span>
|
</span>
|
||||||
@@ -33,20 +30,18 @@
|
|||||||
export default {
|
export default {
|
||||||
props: ["item"],
|
props: ["item"],
|
||||||
data () {
|
data () {
|
||||||
return {
|
return AppStorage.state;
|
||||||
state: AppStorage.state,
|
|
||||||
};
|
|
||||||
},
|
},
|
||||||
computed: {
|
computed: {
|
||||||
// Check if item is requested by active player
|
// Check if item is requested by active player
|
||||||
isRequested () {
|
isRequested () {
|
||||||
const reqs = this.state.player_claims[this.state.player_id];
|
const reqs = this.player_claims[this.player_id];
|
||||||
return reqs.includes(this.item);
|
return reqs.includes(this.item);
|
||||||
},
|
},
|
||||||
// Check if item is requested by multiple players including active one
|
// Check if item is requested by multiple players including active one
|
||||||
isInConflict () {
|
isInConflict () {
|
||||||
const reqs = this.state.player_claims;
|
const reqs = this.player_claims;
|
||||||
const playerId = this.state.player_id;
|
const playerId = this.player_id;
|
||||||
var reqByPlayer = false;
|
var reqByPlayer = false;
|
||||||
var reqByOther = false;
|
var reqByOther = false;
|
||||||
for (var key in reqs) {
|
for (var key in reqs) {
|
||||||
|
|||||||
@@ -1,57 +1,53 @@
|
|||||||
<template>
|
<template>
|
||||||
<div class="box is-shadowless">
|
<section class="level is-mobile">
|
||||||
<nav class="columns is-mobile is-multiline is-vcentered">
|
<div class="level-left">
|
||||||
<div class="column">
|
<div class="level-item">
|
||||||
<span class="icon is-large"
|
<span class="icon is-large"
|
||||||
@click="edit = !edit">
|
@click="edit = !edit">
|
||||||
<i class="fas fa-2x fa-piggy-bank"></i>
|
<i class="fas fa-2x fa-piggy-bank"></i>
|
||||||
</span>
|
</span>
|
||||||
<p v-if="debt" class="has-text-danger">-{{ debt }}gp </p>
|
</div>
|
||||||
</div>
|
<template v-if="edit">
|
||||||
<div class="column has-text-info">
|
<div class="level-item">
|
||||||
<p class="heading">PP</p>
|
<input class="input" type="number" step="0.01" v-model="edit_value"></input>
|
||||||
<p class="is-size-4">{{ wealth[3] }}</p>
|
</div>
|
||||||
</div>
|
<div class="level-item">
|
||||||
<div class="column has-text-warning">
|
<button class="button is-danger" @click="updateWealth()">
|
||||||
<p class="heading">PO</p>
|
Modifier
|
||||||
<p class="is-size-4">{{ wealth[2] }}</p>
|
|
||||||
</div>
|
|
||||||
<div class="column has-text-grey">
|
|
||||||
<p class="heading">PA</p>
|
|
||||||
<p class="is-size-4">{{ wealth[1] }}</p>
|
|
||||||
</div>
|
|
||||||
<div class="column has-text-grey">
|
|
||||||
<p class="heading">PC</p>
|
|
||||||
<p class="is-size-4">{{ wealth[0] }}</p>
|
|
||||||
</div>
|
|
||||||
</nav>
|
|
||||||
<div v-if="edit"> <!-- or v-show ? -->
|
|
||||||
<nav class="columns is-mobile">
|
|
||||||
<div class="column">
|
|
||||||
<NumberInput v-model="edit_value"></NumberInput>
|
|
||||||
</div>
|
|
||||||
<div class="column is-2">
|
|
||||||
<button class="button is-outlined is-fullwidth is-danger"
|
|
||||||
@click="updateWealth('minus')">
|
|
||||||
<span class="icon"><i class="fas fa-2x fa-minus"></i></span>
|
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
<div class="column is-2">
|
</template>
|
||||||
<button class="button is-outlined is-primary is-fullwidth"
|
<template v-else>
|
||||||
@click="updateWealth('plus')">
|
<div class="level-item ">
|
||||||
<span class="icon"><i class="fas fa-2x fa-plus"></i></span>
|
<p class="is-size-4">{{ wealth[3] }}</p>
|
||||||
</button>
|
<p class="heading">PP</p>
|
||||||
</div>
|
</div>
|
||||||
</nav>
|
<div class="level-item ">
|
||||||
|
<p class="is-size-4">{{ wealth[2] }}</p>
|
||||||
|
<p class="heading">PO</p>
|
||||||
|
</div>
|
||||||
|
<div class="level-item ">
|
||||||
|
<p class="is-size-4">{{ wealth[1] }}</p>
|
||||||
|
<p class="heading">PA</p>
|
||||||
|
</div>
|
||||||
|
<div class="level-item ">
|
||||||
|
<p class="is-size-4">{{ wealth[0] }}</p>
|
||||||
|
<p class="heading">PC</p>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
<div class="level-right" v-if="debt">
|
||||||
|
<div class="level-item">
|
||||||
|
<p class="heading is-size-4 has-text-danger">Dette: {{ debt }}gp </p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import { AppStorage } from '../AppStorage.js'
|
import { AppStorage } from '../AppStorage.js'
|
||||||
import NumberInput from './NumberInput.vue'
|
|
||||||
export default {
|
export default {
|
||||||
components: { NumberInput },
|
|
||||||
props: ["wealth", "debt"],
|
props: ["wealth", "debt"],
|
||||||
data () {
|
data () {
|
||||||
return {
|
return {
|
||||||
@@ -60,28 +56,9 @@
|
|||||||
};
|
};
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
updateWealth (op) {
|
updateWealth () {
|
||||||
var goldValue;
|
AppStorage.updatePlayerWealth(this.edit_value)
|
||||||
switch (op) {
|
.then(_ => this.resetValues());
|
||||||
case 'plus':
|
|
||||||
goldValue = this.edit_value;
|
|
||||||
break;
|
|
||||||
case 'minus':
|
|
||||||
goldValue = -this.edit_value;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
console.log("Error, bad operator !", op);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
AppStorage.updatePlayerWealth(goldValue)
|
|
||||||
.then(done => {
|
|
||||||
if (done) {
|
|
||||||
this.$emit('updated');
|
|
||||||
this.resetValues();
|
|
||||||
} else {
|
|
||||||
console.log('correct errors');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
resetValues () {
|
resetValues () {
|
||||||
this.edit = false;
|
this.edit = false;
|
||||||
|
|||||||
@@ -112,7 +112,7 @@ pub(crate) fn serve() -> std::io::Result<()> {
|
|||||||
web::get().to_async(
|
web::get().to_async(
|
||||||
move |pool: AppPool, data: web::Path<(String, f32)>| {
|
move |pool: AppPool, data: web::Path<(String, f32)>| {
|
||||||
db_call(pool, move |api| {
|
db_call(pool, move |api| {
|
||||||
api.as_admin().add_player(data.0.clone(), data.1)
|
api.as_admin().add_player(&data.0, data.1)
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
|||||||
Reference in New Issue
Block a user