Compare commits

2 Commits

Author SHA1 Message Date
441b7f5ad6 little fixes 2019-07-15 16:07:25 +02:00
4b55964786 adds comments and new tests 2019-07-15 15:55:13 +02:00
2 changed files with 32 additions and 7 deletions

View File

@@ -26,7 +26,6 @@ pub type QueryResult<T> = Result<T, diesel::result::Error>;
pub type ActionResult<R> = QueryResult<ActionStatus<R>>; pub type ActionResult<R> = QueryResult<ActionStatus<R>>;
/// Return status of an API Action /// Return status of an API Action
///
#[derive(Serialize, Debug)] #[derive(Serialize, Debug)]
pub struct ActionStatus<R: serde::Serialize> { pub struct ActionStatus<R: serde::Serialize> {
/// Has the action made changes ? /// Has the action made changes ?
@@ -165,13 +164,13 @@ impl<'q> AsPlayer<'q> {
.first::<models::Wealth>(self.conn)?; .first::<models::Wealth>(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::Wealth::from_gp(current_wealth.to_gp() + value_in_gp); let updated_wealth = models::Wealth::from_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) = (current_wealth.as_tuple(), update.as_tuple()); 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); 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(&updated_wealth)
.execute(self.conn) .execute(self.conn)
.map(|r| match r { .map(|r| match r {
1 => ActionStatus { 1 => ActionStatus {
@@ -213,6 +212,9 @@ impl<'q> AsPlayer<'q> {
pub struct AsAdmin<'q>(&'q DbConnection); pub struct AsAdmin<'q>(&'q DbConnection);
impl<'q> AsAdmin<'q> { impl<'q> AsAdmin<'q> {
/// Adds a player to the database
///
/// 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: String, 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))
@@ -220,6 +222,9 @@ impl<'q> AsAdmin<'q> {
.map(ActionStatus::was_updated) .map(ActionStatus::was_updated)
} }
/// Resolve all pending claims and dispatch claimed items.
///
/// When a player gets an item, it's debt is increased by this item sell value
pub fn resolve_claims(self) -> ActionResult<()> { pub fn resolve_claims(self) -> ActionResult<()> {
// Fetch all claims, grouped by items. // Fetch all claims, grouped by items.
let loot = models::Loot::owned_by(0).load(self.0)?; let loot = models::Loot::owned_by(0).load(self.0)?;
@@ -235,7 +240,7 @@ impl<'q> AsAdmin<'q> {
} }
} }
/// Create a connection pool and returns it. /// Sets up a connection pool and returns it.
/// 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");
@@ -251,12 +256,15 @@ mod tests {
use super::*; use super::*;
type TestConnection = DbConnection; type TestConnection = DbConnection;
/// Return a connection to a fresh database (stored in memory)
fn test_connection() -> TestConnection { fn test_connection() -> TestConnection {
let test_conn = DbConnection::establish(":memory:").unwrap(); let test_conn = DbConnection::establish(":memory:").unwrap();
diesel_migrations::run_pending_migrations(&test_conn).unwrap(); diesel_migrations::run_pending_migrations(&test_conn).unwrap();
test_conn test_conn
} }
/// When migrations are run, a special player with id 0 and name "Groupe"
/// must be created.
#[test] #[test]
fn test_group_is_autocreated() { fn test_group_is_autocreated() {
let conn = test_connection(); let conn = test_connection();
@@ -267,6 +275,8 @@ mod tests {
assert_eq!(group.name, "Groupe".to_string()); assert_eq!(group.name, "Groupe".to_string());
} }
/// When a player updates wealth, a difference is returned by API.
/// Added to the previous amount of coins, it should equal the updated weath.
#[test] #[test]
fn test_player_updates_wealth() { fn test_player_updates_wealth() {
let conn = test_connection(); let conn = test_connection();
@@ -288,7 +298,7 @@ mod tests {
} }
#[test] #[test]
fn test_add_player() { fn test_admin_add_player() {
let conn = test_connection(); let conn = test_connection();
let result = DbApi::with_conn(&conn).as_admin() let result = DbApi::with_conn(&conn).as_admin()
.add_player("PlayerName".to_string(), 403.21) .add_player("PlayerName".to_string(), 403.21)
@@ -300,6 +310,21 @@ mod tests {
assert_eq!(new_player.name, "PlayerName"); assert_eq!(new_player.name, "PlayerName");
assert_eq!((new_player.cp, new_player.sp, new_player.gp, new_player.pp), (1, 2, 3, 4)); assert_eq!((new_player.cp, new_player.sp, new_player.gp, new_player.pp), (1, 2, 3, 4));
} }
#[test]
fn test_admin_resolve_claims() {
}
#[test]
fn test_player_claim_item() {
}
#[test]
fn test_player_unclaim_item() {
}
} }

View File

@@ -13,5 +13,5 @@ fn main() {
// Server is started with 'serve' subcommand // Server is started with 'serve' subcommand
// $ lootalot serve // $ lootalot serve
// Start the server. // Start the server.
server::serve(); server::serve().ok();
} }