learning how to test
This commit is contained in:
@@ -6,9 +6,10 @@ edition = "2018"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
dotenv = "*"
|
dotenv = "*"
|
||||||
|
diesel_migrations = "*"
|
||||||
serde = "*"
|
serde = "*"
|
||||||
serde_derive = "*"
|
serde_derive = "*"
|
||||||
|
|
||||||
[dependencies.diesel]
|
[dependencies.diesel]
|
||||||
version = "1.0"
|
version = "1.4"
|
||||||
features = ["sqlite", "r2d2"]
|
features = ["sqlite", "r2d2"]
|
||||||
|
|||||||
@@ -5,10 +5,10 @@ extern crate diesel;
|
|||||||
extern crate serde_derive;
|
extern crate serde_derive;
|
||||||
|
|
||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
use diesel::r2d2::{self, ConnectionManager};
|
|
||||||
use diesel::query_dsl::RunQueryDsl;
|
use diesel::query_dsl::RunQueryDsl;
|
||||||
|
use diesel::r2d2::{self, ConnectionManager};
|
||||||
|
|
||||||
mod models;
|
pub mod models;
|
||||||
mod schema;
|
mod schema;
|
||||||
|
|
||||||
/// The connection used
|
/// The connection used
|
||||||
@@ -49,7 +49,7 @@ impl ActionStatus<()> {
|
|||||||
/// It offers a convenient way to deal with connection
|
/// It offers a convenient way to deal with connection
|
||||||
///
|
///
|
||||||
/// # Todo list
|
/// # Todo list
|
||||||
/// ```
|
/// ```text
|
||||||
/// struct DbApi<'q>(&'q DbConnection);
|
/// struct DbApi<'q>(&'q DbConnection);
|
||||||
/// ::new() -> DbApi<'q> (Db finds a connection by itself, usefull for cli)
|
/// ::new() -> DbApi<'q> (Db finds a connection by itself, usefull for cli)
|
||||||
/// ::with_conn(conn) -> DbApi<'q> (uses a user-defined connection)
|
/// ::with_conn(conn) -> DbApi<'q> (uses a user-defined connection)
|
||||||
@@ -77,7 +77,9 @@ impl<'q> DbApi<'q> {
|
|||||||
///
|
///
|
||||||
/// # Usage
|
/// # Usage
|
||||||
/// ```
|
/// ```
|
||||||
/// let conn = DbConnection::establish();
|
/// use lootalot_db::{DbConnection, DbApi};
|
||||||
|
/// # use diesel::connection::Connection;
|
||||||
|
/// let conn = DbConnection::establish(":memory:").unwrap();
|
||||||
/// let api = DbApi::with_conn(&conn);
|
/// let api = DbApi::with_conn(&conn);
|
||||||
/// ```
|
/// ```
|
||||||
pub fn with_conn(conn: &'q DbConnection) -> Self {
|
pub fn with_conn(conn: &'q DbConnection) -> Self {
|
||||||
@@ -87,26 +89,17 @@ impl<'q> DbApi<'q> {
|
|||||||
///
|
///
|
||||||
/// This method consumes the DbApi object.
|
/// This method consumes the DbApi object.
|
||||||
pub fn fetch_players(self) -> QueryResult<Vec<models::Player>> {
|
pub fn fetch_players(self) -> QueryResult<Vec<models::Player>> {
|
||||||
Ok(
|
Ok(schema::players::table.load::<models::Player>(self.0)?)
|
||||||
schema::players::table
|
|
||||||
.load::<models::Player>(self.0)?
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
/// Fetch the inventory of items
|
/// Fetch the inventory of items
|
||||||
///
|
///
|
||||||
/// Consumes the DbApi instance
|
/// Consumes the DbApi instance
|
||||||
pub fn fetch_inventory(self) -> QueryResult<Vec<models::Item>> {
|
pub fn fetch_inventory(self) -> QueryResult<Vec<models::Item>> {
|
||||||
Ok(
|
Ok(schema::items::table.load::<models::Item>(self.0)?)
|
||||||
schema::items::table
|
|
||||||
.load::<models::Item>(self.0)?
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn fetch_claims(self) -> QueryResult<Vec<models::Claim>> {
|
pub fn fetch_claims(self) -> QueryResult<Vec<models::Claim>> {
|
||||||
Ok(
|
Ok(schema::claims::table.load::<models::Claim>(self.0)?)
|
||||||
schema::claims::table
|
|
||||||
.load::<models::Claim>(self.0)?
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
/// Wrapper for acting as a specific player
|
/// Wrapper for acting as a specific player
|
||||||
///
|
///
|
||||||
@@ -114,6 +107,10 @@ impl<'q> DbApi<'q> {
|
|||||||
///
|
///
|
||||||
/// # Usage
|
/// # Usage
|
||||||
/// ```
|
/// ```
|
||||||
|
/// # use lootalot_db::{DbConnection, DbApi};
|
||||||
|
/// # use diesel::connection::Connection;
|
||||||
|
/// # let conn = DbConnection::establish(":memory:").unwrap();
|
||||||
|
/// # let api = DbApi::with_conn(&conn);
|
||||||
/// let player_id: i32 = 1; // Id that references player in DB
|
/// let player_id: i32 = 1; // Id that references player in DB
|
||||||
/// let player = api.as_player(player_id);
|
/// let player = api.as_player(player_id);
|
||||||
/// ```
|
/// ```
|
||||||
@@ -136,36 +133,37 @@ pub struct AsPlayer<'q> {
|
|||||||
|
|
||||||
impl<'q> AsPlayer<'q> {
|
impl<'q> AsPlayer<'q> {
|
||||||
/// Fetch the content of a player's chest
|
/// Fetch the content of a player's chest
|
||||||
|
///
|
||||||
|
/// # Usage
|
||||||
|
/// ```
|
||||||
|
/// # extern crate diesel_migrations;
|
||||||
|
/// # use lootalot_db::{DbConnection, DbApi};
|
||||||
|
/// # use diesel::connection::Connection;
|
||||||
|
/// # let conn = DbConnection::establish(":memory:").unwrap();
|
||||||
|
/// # diesel_migrations::run_pending_migrations(&conn).unwrap();
|
||||||
|
/// # let api = DbApi::with_conn(&conn);
|
||||||
|
/// // Get loot of player with id of 1
|
||||||
|
/// let loot = api.as_player(1).loot().unwrap();
|
||||||
|
/// assert_eq!(format!("{:?}", loot), "[]".to_string());
|
||||||
|
/// ```
|
||||||
pub fn loot(self) -> QueryResult<Vec<models::Item>> {
|
pub fn loot(self) -> QueryResult<Vec<models::Item>> {
|
||||||
Ok(
|
Ok(models::Item::owned_by(self.id).load(self.conn)?)
|
||||||
models::Item::owned_by(self.id)
|
|
||||||
.load(self.conn)?
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
/// 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)
|
pub fn update_wealth(self, value_in_gp: f32) -> ActionResult<Option<(i32, i32, i32, i32)>> {
|
||||||
-> ActionResult<Option<(i32, i32, i32, i32)>> {
|
|
||||||
use schema::players::dsl::*;
|
use schema::players::dsl::*;
|
||||||
let current_wealth = players
|
let current_wealth = players
|
||||||
.find(self.id)
|
.find(self.id)
|
||||||
.select((cp, sp, gp, pp))
|
.select((cp, sp, gp, pp))
|
||||||
.first::<models::WealthUpdate>(self.conn)?;
|
.first::<models::WealthUpdate>(self.conn)?;
|
||||||
// TODO: improve this
|
// TODO: improve this
|
||||||
// should be move inside a WealthUpdate method
|
// should be move inside a WealthUpdate method
|
||||||
let update = models::WealthUpdate::from_gp(
|
let update = models::WealthUpdate::from_gp(current_wealth.to_gp() + value_in_gp);
|
||||||
current_wealth.to_gp() + value_in_gp
|
|
||||||
);
|
|
||||||
// Difference in coins that is sent back
|
// Difference in coins that is sent back
|
||||||
let (old, new) =
|
let (old, new) = (current_wealth.as_tuple(), update.as_tuple());
|
||||||
(current_wealth.as_tuple(), update.as_tuple());
|
let diff = (new.0 - old.0, new.1 - old.1, new.2 - old.2, new.3 - old.3);
|
||||||
let diff = (
|
|
||||||
new.0 - old.0,
|
|
||||||
new.1 - old.1,
|
|
||||||
new.2 - old.2,
|
|
||||||
new.3 - old.3
|
|
||||||
);
|
|
||||||
diesel::update(players)
|
diesel::update(players)
|
||||||
.filter(id.eq(self.id))
|
.filter(id.eq(self.id))
|
||||||
.set(&update)
|
.set(&update)
|
||||||
@@ -175,26 +173,31 @@ impl<'q> AsPlayer<'q> {
|
|||||||
1 => ActionStatus {
|
1 => ActionStatus {
|
||||||
executed: true,
|
executed: true,
|
||||||
response: Some(diff),
|
response: Some(diff),
|
||||||
},
|
},
|
||||||
_ => ActionStatus {
|
_ => ActionStatus {
|
||||||
executed: false,
|
executed: false,
|
||||||
response: None,
|
response: None,
|
||||||
}, })
|
},
|
||||||
|
})
|
||||||
}
|
}
|
||||||
/// 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<()> {
|
||||||
// TODO: check that looted item exists
|
// TODO: check that looted item exists
|
||||||
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 Ok(ActionStatus::nop());
|
||||||
|
};
|
||||||
|
let request = models::NewClaim {
|
||||||
|
player_id: self.id,
|
||||||
|
loot_id: item,
|
||||||
};
|
};
|
||||||
let request = models::NewClaim { player_id: self.id, loot_id: item };
|
|
||||||
diesel::insert_into(schema::claims::table)
|
diesel::insert_into(schema::claims::table)
|
||||||
.values(&request)
|
.values(&request)
|
||||||
.execute(self.conn)
|
.execute(self.conn)
|
||||||
.map(|r| match r { 1 => ActionStatus::ok(), _ => ActionStatus::nop() })
|
.map(|r| match r {
|
||||||
|
1 => ActionStatus::ok(),
|
||||||
|
_ => ActionStatus::nop(),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
/// Withdraw claim
|
/// Withdraw claim
|
||||||
pub fn unclaim(self, item: i32) -> ActionResult<()> {
|
pub fn unclaim(self, item: i32) -> ActionResult<()> {
|
||||||
@@ -202,9 +205,13 @@ impl<'q> AsPlayer<'q> {
|
|||||||
diesel::delete(
|
diesel::delete(
|
||||||
claims
|
claims
|
||||||
.filter(loot_id.eq(item))
|
.filter(loot_id.eq(item))
|
||||||
.filter(player_id.eq(self.id)))
|
.filter(player_id.eq(self.id)),
|
||||||
.execute(self.conn)
|
)
|
||||||
.map(|r| match r { 1 => ActionStatus::ok(), _ => ActionStatus::nop() })
|
.execute(self.conn)
|
||||||
|
.map(|r| match r {
|
||||||
|
1 => ActionStatus::ok(),
|
||||||
|
_ => ActionStatus::nop(),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -212,12 +219,14 @@ impl<'q> AsPlayer<'q> {
|
|||||||
pub struct AsAdmin<'q>(&'q DbConnection);
|
pub struct AsAdmin<'q>(&'q DbConnection);
|
||||||
|
|
||||||
impl<'q> AsAdmin<'q> {
|
impl<'q> AsAdmin<'q> {
|
||||||
|
|
||||||
pub fn add_player(self, name: String, start_wealth: f32) -> ActionResult<()> {
|
pub fn add_player(self, name: String, start_wealth: f32) -> ActionResult<()> {
|
||||||
diesel::insert_into(schema::players::table)
|
diesel::insert_into(schema::players::table)
|
||||||
.values(&models::NewPlayer::create(&name, start_wealth))
|
.values(&models::NewPlayer::create(&name, start_wealth))
|
||||||
.execute(self.0)
|
.execute(self.0)
|
||||||
.map(|r| match r { 1 => ActionStatus::ok(), _ => ActionStatus::nop() })
|
.map(|r| match r {
|
||||||
|
1 => ActionStatus::ok(),
|
||||||
|
_ => ActionStatus::nop(),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn resolve_claims(self) -> ActionResult<()> {
|
pub fn resolve_claims(self) -> ActionResult<()> {
|
||||||
@@ -239,7 +248,7 @@ impl<'q> AsAdmin<'q> {
|
|||||||
/// Uses the DATABASE_URL environment variable (must be set)
|
/// Uses the DATABASE_URL environment variable (must be set)
|
||||||
pub fn create_pool() -> Pool {
|
pub fn create_pool() -> Pool {
|
||||||
let connspec = std::env::var("DATABASE_URL").expect("DATABASE_URL");
|
let connspec = std::env::var("DATABASE_URL").expect("DATABASE_URL");
|
||||||
dbg!( &connspec );
|
dbg!(&connspec);
|
||||||
let manager = ConnectionManager::<DbConnection>::new(connspec);
|
let manager = ConnectionManager::<DbConnection>::new(connspec);
|
||||||
r2d2::Pool::builder()
|
r2d2::Pool::builder()
|
||||||
.build(manager)
|
.build(manager)
|
||||||
@@ -247,6 +256,4 @@ pub fn create_pool() -> Pool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {}
|
||||||
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use diesel::prelude::*;
|
|
||||||
use crate::schema::claims;
|
|
||||||
use crate::models::item::Loot;
|
use crate::models::item::Loot;
|
||||||
|
use crate::schema::claims;
|
||||||
|
use diesel::prelude::*;
|
||||||
|
|
||||||
#[derive(Identifiable, Queryable, Associations, Serialize, Debug)]
|
#[derive(Identifiable, Queryable, Associations, Serialize, Debug)]
|
||||||
#[belongs_to(Loot)]
|
#[belongs_to(Loot)]
|
||||||
@@ -12,7 +12,7 @@ pub struct Claim {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Insertable, Debug)]
|
#[derive(Insertable, Debug)]
|
||||||
#[table_name="claims"]
|
#[table_name = "claims"]
|
||||||
pub struct NewClaim {
|
pub struct NewClaim {
|
||||||
pub player_id: i32,
|
pub player_id: i32,
|
||||||
pub loot_id: i32,
|
pub loot_id: i32,
|
||||||
|
|||||||
@@ -1,13 +1,11 @@
|
|||||||
use diesel::prelude::*;
|
use crate::schema::{items, looted};
|
||||||
use diesel::dsl::{Eq, Filter, Select, exists, Find };
|
|
||||||
use diesel::expression::exists::Exists;
|
|
||||||
use crate::schema::{looted, items};
|
|
||||||
use crate::DbConnection;
|
use crate::DbConnection;
|
||||||
|
use diesel::dsl::{exists, Eq, Filter, Find, Select};
|
||||||
|
use diesel::expression::exists::Exists;
|
||||||
|
use diesel::prelude::*;
|
||||||
|
|
||||||
type ItemColumns =
|
type ItemColumns = (looted::id, looted::name, looted::base_price);
|
||||||
( looted::id, looted::name, looted::base_price, );
|
const ITEM_COLUMNS: ItemColumns = (looted::id, looted::name, looted::base_price);
|
||||||
const ITEM_COLUMNS: ItemColumns =
|
|
||||||
( looted::id, looted::name, looted::base_price, );
|
|
||||||
type OwnedBy = Select<OwnedLoot, ItemColumns>;
|
type OwnedBy = Select<OwnedLoot, ItemColumns>;
|
||||||
|
|
||||||
/// Represents a unique item in inventory
|
/// Represents a unique item in inventory
|
||||||
@@ -25,8 +23,7 @@ pub struct Item {
|
|||||||
impl Item {
|
impl Item {
|
||||||
/// Public proxy for Loot::owned_by that selects only Item fields
|
/// Public proxy for Loot::owned_by that selects only Item fields
|
||||||
pub fn owned_by(player: i32) -> OwnedBy {
|
pub fn owned_by(player: i32) -> OwnedBy {
|
||||||
Loot::owned_by(player)
|
Loot::owned_by(player).select(ITEM_COLUMNS)
|
||||||
.select(ITEM_COLUMNS)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,7 +32,7 @@ type OwnedLoot = Filter<looted::table, WithOwner>;
|
|||||||
|
|
||||||
/// Represents an item that has been looted
|
/// Represents an item that has been looted
|
||||||
#[derive(Identifiable, Debug, Queryable, Serialize)]
|
#[derive(Identifiable, Debug, Queryable, Serialize)]
|
||||||
#[table_name="looted"]
|
#[table_name = "looted"]
|
||||||
pub(crate) struct Loot {
|
pub(crate) struct Loot {
|
||||||
id: i32,
|
id: i32,
|
||||||
name: String,
|
name: String,
|
||||||
@@ -46,8 +43,7 @@ pub(crate) struct Loot {
|
|||||||
impl Loot {
|
impl Loot {
|
||||||
/// A filter on Loot that is owned by given player
|
/// A filter on Loot that is owned by given player
|
||||||
pub(crate) fn owned_by(id: i32) -> OwnedLoot {
|
pub(crate) fn owned_by(id: i32) -> OwnedLoot {
|
||||||
looted::table
|
looted::table.filter(looted::owner_id.eq(id))
|
||||||
.filter(looted::owner_id.eq(id))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn exists(id: i32) -> Exists<Find<looted::table, i32>> {
|
pub(crate) fn exists(id: i32) -> Exists<Find<looted::table, i32>> {
|
||||||
@@ -55,7 +51,9 @@ impl Loot {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Description of an item : (name, value in gold)
|
||||||
type ItemDesc<'a> = (&'a str, i32);
|
type ItemDesc<'a> = (&'a str, i32);
|
||||||
|
|
||||||
/// An item being looted or bought.
|
/// An item being looted or bought.
|
||||||
///
|
///
|
||||||
/// The owner is set to 0 in case of looting,
|
/// The owner is set to 0 in case of looting,
|
||||||
@@ -69,6 +67,7 @@ struct NewLoot<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> NewLoot<'a> {
|
impl<'a> NewLoot<'a> {
|
||||||
|
/// A new loot going to the group (loot procedure)
|
||||||
fn to_group(desc: ItemDesc<'a>) -> Self {
|
fn to_group(desc: ItemDesc<'a>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
name: desc.0,
|
name: desc.0,
|
||||||
@@ -77,6 +76,7 @@ impl<'a> NewLoot<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A new loot going to a specific player (buy procedure)
|
||||||
fn to_player(player: i32, desc: ItemDesc<'a>) -> Self {
|
fn to_player(player: i32, desc: ItemDesc<'a>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
name: desc.0,
|
name: desc.0,
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
|
mod claim;
|
||||||
mod item;
|
mod item;
|
||||||
mod player;
|
mod player;
|
||||||
mod claim;
|
|
||||||
|
|
||||||
|
pub use claim::{Claim, NewClaim};
|
||||||
pub use item::Item;
|
pub use item::Item;
|
||||||
pub(crate) use item::Loot;
|
pub(crate) use item::Loot;
|
||||||
pub use player::{Player, NewPlayer, WealthUpdate};
|
pub use player::{NewPlayer, Player, WealthUpdate};
|
||||||
pub use claim::{NewClaim, Claim};
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use diesel::prelude::*;
|
|
||||||
use crate::schema::players;
|
use crate::schema::players;
|
||||||
use crate::DbConnection;
|
use crate::DbConnection;
|
||||||
|
use diesel::prelude::*;
|
||||||
|
|
||||||
/// Representation of a player in database
|
/// Representation of a player in database
|
||||||
#[derive(Debug, Queryable, Serialize)]
|
#[derive(Debug, Queryable, Serialize)]
|
||||||
@@ -14,7 +14,6 @@ pub struct Player {
|
|||||||
pp: i32,
|
pp: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Unpack a floating value in gold pieces to integer
|
/// Unpack a floating value in gold pieces to integer
|
||||||
/// values of copper, silver, gold and platinum pieces
|
/// values of copper, silver, gold and platinum pieces
|
||||||
///
|
///
|
||||||
@@ -38,33 +37,39 @@ fn unpack_gold_value(gold: f32) -> (i32, i32, i32, i32) {
|
|||||||
/// The values held here are the amount of pieces to add or
|
/// The values held here are the amount of pieces to add or
|
||||||
/// substract to player wealth.
|
/// substract to player wealth.
|
||||||
#[derive(Queryable, AsChangeset, Debug)]
|
#[derive(Queryable, AsChangeset, Debug)]
|
||||||
#[table_name="players"]
|
#[table_name = "players"]
|
||||||
pub struct WealthUpdate {
|
pub struct WealthUpdate {
|
||||||
cp: i32,
|
cp: i32,
|
||||||
sp: i32,
|
sp: i32,
|
||||||
gp: i32,
|
gp: i32,
|
||||||
pp: i32
|
pp: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WealthUpdate{
|
impl WealthUpdate {
|
||||||
/// Unpack individual pieces counts from gold value
|
/// Unpack individual pieces counts from gold value
|
||||||
pub fn from_gp(gp: f32) -> Self {
|
pub fn from_gp(gp: f32) -> Self {
|
||||||
let (cp, sp, gp, pp) = unpack_gold_value(gp);
|
let (cp, sp, gp, pp) = unpack_gold_value(gp);
|
||||||
Self { cp, sp, gp, pp }
|
Self { cp, sp, gp, pp }
|
||||||
}
|
}
|
||||||
|
/// Convert total value to a floating value in gold pieces
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
/// ```
|
||||||
|
/// # use lootalot_db::models::WealthUpdate;
|
||||||
|
/// let wealth = WealthUpdate::from_gp(403.21);
|
||||||
|
/// assert_eq!(wealth.to_gp(), 403.21);
|
||||||
|
/// ```
|
||||||
pub fn to_gp(&self) -> f32 {
|
pub fn to_gp(&self) -> f32 {
|
||||||
let i = self.pp * 100 + self.gp;
|
let i = self.pp * 100 + self.gp;
|
||||||
let f = ( self.sp * 10 + self.cp ) as f32 / 100.0;
|
let f = (self.sp * 10 + self.cp) as f32 / 100.0;
|
||||||
i as f32 + f
|
i as f32 + f
|
||||||
}
|
}
|
||||||
|
/// Pack the counts inside a tuple, from lower to higher coin value.
|
||||||
pub fn as_tuple(&self) -> (i32, i32, i32, i32) {
|
pub fn as_tuple(&self) -> (i32, i32, i32, i32) {
|
||||||
(self.cp, self.sp, self.gp, self.pp)
|
(self.cp, self.sp, self.gp, self.pp)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Representation of a new player record
|
/// Representation of a new player record
|
||||||
#[derive(Insertable)]
|
#[derive(Insertable)]
|
||||||
#[table_name = "players"]
|
#[table_name = "players"]
|
||||||
@@ -98,13 +103,13 @@ mod tests {
|
|||||||
fn test_unpack_gold_values() {
|
fn test_unpack_gold_values() {
|
||||||
use super::unpack_gold_value;
|
use super::unpack_gold_value;
|
||||||
let test_values = [
|
let test_values = [
|
||||||
(1.0, (0,0,1,0)),
|
(1.0, (0, 0, 1, 0)),
|
||||||
(1.23, (3,2,1,0)),
|
(1.23, (3, 2, 1, 0)),
|
||||||
(1.03, (3,0,1,0)),
|
(1.03, (3, 0, 1, 0)),
|
||||||
(100.23, (3,2,0,1)),
|
(100.23, (3, 2, 0, 1)),
|
||||||
(-100.23, (-3,-2,-0,-1)),
|
(-100.23, (-3, -2, -0, -1)),
|
||||||
(10189.23, (3,2,89,101)),
|
(10189.23, (3, 2, 89, 101)),
|
||||||
(-8090.20, (0,-2,-90,-80))
|
(-8090.20, (0, -2, -90, -80)),
|
||||||
];
|
];
|
||||||
|
|
||||||
for (tested, expected) in test_values.into_iter() {
|
for (tested, expected) in test_values.into_iter() {
|
||||||
|
|||||||
@@ -40,9 +40,4 @@ joinable!(claims -> looted (loot_id));
|
|||||||
joinable!(claims -> players (player_id));
|
joinable!(claims -> players (player_id));
|
||||||
joinable!(looted -> players (owner_id));
|
joinable!(looted -> players (owner_id));
|
||||||
|
|
||||||
allow_tables_to_appear_in_same_query!(
|
allow_tables_to_appear_in_same_query!(claims, items, looted, players,);
|
||||||
claims,
|
|
||||||
items,
|
|
||||||
looted,
|
|
||||||
players,
|
|
||||||
);
|
|
||||||
|
|||||||
@@ -1,5 +1,11 @@
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
presets: [
|
presets: [
|
||||||
'@vue/app'
|
'@vue/app'
|
||||||
]
|
],
|
||||||
|
"presets": [["env", { "modules": false }]],
|
||||||
|
"env": {
|
||||||
|
"test": {
|
||||||
|
"presets": [["env", { "targets": { "node": "current" } }]]
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
2819
lootalot_front/package-lock.json
generated
2819
lootalot_front/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -7,7 +7,8 @@
|
|||||||
"css-watch": "npm run css-build -- --watch",
|
"css-watch": "npm run css-build -- --watch",
|
||||||
"serve": "vue-cli-service serve",
|
"serve": "vue-cli-service serve",
|
||||||
"build": "vue-cli-service build",
|
"build": "vue-cli-service build",
|
||||||
"lint": "vue-cli-service lint"
|
"lint": "vue-cli-service lint",
|
||||||
|
"test": "jest"
|
||||||
},
|
},
|
||||||
"main": "sass/scroll.scss",
|
"main": "sass/scroll.scss",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
@@ -19,10 +20,15 @@
|
|||||||
"@vue/cli-plugin-babel": "^3.8.0",
|
"@vue/cli-plugin-babel": "^3.8.0",
|
||||||
"@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",
|
||||||
"babel-eslint": "^10.0.1",
|
"babel-eslint": "^10.0.1",
|
||||||
|
"babel-jest": "^24.8.0",
|
||||||
|
"babel-preset-env": "^1.7.0",
|
||||||
"eslint": "^5.16.0",
|
"eslint": "^5.16.0",
|
||||||
"eslint-plugin-vue": "^5.0.0",
|
"eslint-plugin-vue": "^5.0.0",
|
||||||
|
"jest": "^24.8.0",
|
||||||
"node-sass": "^4.12.0",
|
"node-sass": "^4.12.0",
|
||||||
|
"vue-jest": "^3.0.4",
|
||||||
"vue-template-compiler": "^2.6.10"
|
"vue-template-compiler": "^2.6.10"
|
||||||
},
|
},
|
||||||
"eslintConfig": {
|
"eslintConfig": {
|
||||||
@@ -47,5 +53,16 @@
|
|||||||
"browserslist": [
|
"browserslist": [
|
||||||
"> 1%",
|
"> 1%",
|
||||||
"last 2 versions"
|
"last 2 versions"
|
||||||
]
|
],
|
||||||
|
"jest": {
|
||||||
|
"moduleFileExtensions": [
|
||||||
|
"js",
|
||||||
|
"json",
|
||||||
|
"vue"
|
||||||
|
],
|
||||||
|
"transform": {
|
||||||
|
".*\\.(vue)$": "vue-jest",
|
||||||
|
"^.+\\.js$": "<rootDir>/node_modules/babel-jest"
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,12 +17,10 @@
|
|||||||
checkError (ev) {
|
checkError (ev) {
|
||||||
const newValue = ev.target.value;
|
const newValue = ev.target.value;
|
||||||
this.has_error = isNaN(newValue);
|
this.has_error = isNaN(newValue);
|
||||||
if (!this.has_error) {
|
this.$emit(
|
||||||
this.$emit(
|
'input',
|
||||||
'input',
|
this.has_error ? 0 : Number(newValue)
|
||||||
Number(newValue)
|
);
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
|
extern crate actix_web;
|
||||||
extern crate dotenv;
|
extern crate dotenv;
|
||||||
extern crate env_logger;
|
extern crate env_logger;
|
||||||
extern crate actix_web;
|
|
||||||
extern crate lootalot_db;
|
extern crate lootalot_db;
|
||||||
|
|
||||||
mod server;
|
mod server;
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
use std::env;
|
|
||||||
use futures::Future;
|
|
||||||
use actix_files as fs;
|
|
||||||
use actix_web::{web, App, HttpServer, HttpResponse, Error};
|
|
||||||
use actix_cors::Cors;
|
use actix_cors::Cors;
|
||||||
use lootalot_db::{Pool, DbApi, QueryResult};
|
use actix_files as fs;
|
||||||
|
use actix_web::{web, App, Error, HttpResponse, HttpServer};
|
||||||
|
use futures::Future;
|
||||||
|
use lootalot_db::{DbApi, Pool, QueryResult};
|
||||||
|
use std::env;
|
||||||
|
|
||||||
type AppPool = web::Data<Pool>;
|
type AppPool = web::Data<Pool>;
|
||||||
|
|
||||||
@@ -61,17 +61,17 @@ pub(crate) fn serve() -> std::io::Result<()> {
|
|||||||
Cors::new()
|
Cors::new()
|
||||||
.allowed_origin("http://localhost:8080")
|
.allowed_origin("http://localhost:8080")
|
||||||
.allowed_methods(vec!["GET", "POST"])
|
.allowed_methods(vec!["GET", "POST"])
|
||||||
.max_age(3600)
|
.max_age(3600),
|
||||||
)
|
)
|
||||||
.route(
|
.route(
|
||||||
"/api/players",
|
"/api/players",
|
||||||
web::get().to_async(move |pool: AppPool| {
|
web::get()
|
||||||
db_call(pool, move |api| api.fetch_players())
|
.to_async(move |pool: AppPool| db_call(pool, move |api| api.fetch_players())),
|
||||||
}),
|
|
||||||
)
|
)
|
||||||
.route(
|
.route(
|
||||||
"/api/claims",
|
"/api/claims",
|
||||||
web::get().to_async(move |pool: AppPool| db_call(pool, move |api| api.fetch_claims())),
|
web::get()
|
||||||
|
.to_async(move |pool: AppPool| db_call(pool, move |api| api.fetch_claims())),
|
||||||
)
|
)
|
||||||
.route(
|
.route(
|
||||||
"/api/{player_id}/update-wealth/{amount}",
|
"/api/{player_id}/update-wealth/{amount}",
|
||||||
@@ -89,13 +89,13 @@ pub(crate) fn serve() -> std::io::Result<()> {
|
|||||||
"/api/{player_id}/claim/{item_id}",
|
"/api/{player_id}/claim/{item_id}",
|
||||||
web::get().to_async(move |pool: AppPool, data: web::Path<(i32, i32)>| {
|
web::get().to_async(move |pool: AppPool, data: web::Path<(i32, i32)>| {
|
||||||
db_call(pool, move |api| api.as_player(data.0).claim(data.1))
|
db_call(pool, move |api| api.as_player(data.0).claim(data.1))
|
||||||
})
|
}),
|
||||||
)
|
)
|
||||||
.route(
|
.route(
|
||||||
"/api/{player_id}/unclaim/{item_id}",
|
"/api/{player_id}/unclaim/{item_id}",
|
||||||
web::get().to_async(move |pool: AppPool, data: web::Path<(i32, i32)>| {
|
web::get().to_async(move |pool: AppPool, data: web::Path<(i32, i32)>| {
|
||||||
db_call(pool, move |api| api.as_player(data.0).unclaim(data.1))
|
db_call(pool, move |api| api.as_player(data.0).unclaim(data.1))
|
||||||
})
|
}),
|
||||||
)
|
)
|
||||||
.route(
|
.route(
|
||||||
"/api/admin/resolve-claims",
|
"/api/admin/resolve-claims",
|
||||||
@@ -106,7 +106,9 @@ pub(crate) fn serve() -> std::io::Result<()> {
|
|||||||
.route(
|
.route(
|
||||||
"/api/admin/add-player/{name}/{wealth}",
|
"/api/admin/add-player/{name}/{wealth}",
|
||||||
web::get().to_async(move |pool: AppPool, data: web::Path<(String, f32)>| {
|
web::get().to_async(move |pool: AppPool, data: web::Path<(String, f32)>| {
|
||||||
db_call(pool, move |api| api.as_admin().add_player(data.0.clone(), data.1))
|
db_call(pool, move |api| {
|
||||||
|
api.as_admin().add_player(data.0.clone(), data.1)
|
||||||
|
})
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
.service(fs::Files::new("/", www_root.clone()).index_file("index.html"))
|
.service(fs::Files::new("/", www_root.clone()).index_file("index.html"))
|
||||||
|
|||||||
Reference in New Issue
Block a user