diff --git a/lootalot_db/db.sqlite3 b/lootalot_db/db.sqlite3 index 62ac4b2..41341aa 100644 Binary files a/lootalot_db/db.sqlite3 and b/lootalot_db/db.sqlite3 differ diff --git a/lootalot_db/migrations/2019-06-10-123922_create_items/down.sql b/lootalot_db/migrations/2019-06-10-123922_create_items/down.sql index dfed2b1..d962a59 100644 --- a/lootalot_db/migrations/2019-06-10-123922_create_items/down.sql +++ b/lootalot_db/migrations/2019-06-10-123922_create_items/down.sql @@ -1,2 +1,3 @@ DROP TABLE items; +DROP TABLE looted; diff --git a/lootalot_db/migrations/2019-06-10-123922_create_items/up.sql b/lootalot_db/migrations/2019-06-10-123922_create_items/up.sql index f39d73b..4893561 100644 --- a/lootalot_db/migrations/2019-06-10-123922_create_items/up.sql +++ b/lootalot_db/migrations/2019-06-10-123922_create_items/up.sql @@ -1,5 +1,15 @@ +-- The global inventory of items CREATE TABLE items ( id INTEGER PRIMARY KEY NOT NULL, name VARCHAR NOT NULL, base_price INTEGER NOT NULL ); + +-- The items that have been looted +CREATE TABLE looted ( + id INTEGER PRIMARY KEY NOT NULL, + name VARCHAR NOT NULL, + base_price INTEGER NOT NULL, + owner_id INTEGER NOT NULL, + FOREIGN KEY (owner_id) REFERENCES players(id) +); diff --git a/lootalot_db/migrations/2019-06-10-123940_create_players/up.sql b/lootalot_db/migrations/2019-06-10-123940_create_players/up.sql index 669ddf9..1ee1f3d 100644 --- a/lootalot_db/migrations/2019-06-10-123940_create_players/up.sql +++ b/lootalot_db/migrations/2019-06-10-123940_create_players/up.sql @@ -7,3 +7,5 @@ CREATE TABLE players ( gp INTEGER DEFAULT 0 NOT NULL, pp INTEGER DEFAULT 0 NOT NULL ); + +INSERT INTO players (id, name) VALUES (0, 'Groupe'); diff --git a/lootalot_db/migrations/2019-06-10-124013_create_looted/down.sql b/lootalot_db/migrations/2019-06-10-124013_create_looted/down.sql deleted file mode 100644 index f01e49c..0000000 --- a/lootalot_db/migrations/2019-06-10-124013_create_looted/down.sql +++ /dev/null @@ -1 +0,0 @@ -DROP TABLE looted; diff --git a/lootalot_db/migrations/2019-06-10-124013_create_looted/up.sql b/lootalot_db/migrations/2019-06-10-124013_create_looted/up.sql deleted file mode 100644 index 46f2053..0000000 --- a/lootalot_db/migrations/2019-06-10-124013_create_looted/up.sql +++ /dev/null @@ -1,8 +0,0 @@ -CREATE TABLE looted ( - id INTEGER PRIMARY KEY NOT NULL, - player_id INTEGER NOT NULL, - item_id INTEGER NOT NULL, - acquired_date DATE NOT NULL, - FOREIGN KEY (player_id) REFERENCES players(id), - FOREIGN KEY (item_id) REFERENCES items(id) -); diff --git a/lootalot_db/migrations/2019-06-21-113950_claims/down.sql b/lootalot_db/migrations/2019-06-21-113950_claims/down.sql new file mode 100644 index 0000000..67e69d8 --- /dev/null +++ b/lootalot_db/migrations/2019-06-21-113950_claims/down.sql @@ -0,0 +1 @@ +DROP TABLE claims; diff --git a/lootalot_db/migrations/2019-06-21-113950_claims/up.sql b/lootalot_db/migrations/2019-06-21-113950_claims/up.sql new file mode 100644 index 0000000..8ae547c --- /dev/null +++ b/lootalot_db/migrations/2019-06-21-113950_claims/up.sql @@ -0,0 +1,8 @@ +CREATE TABLE claims ( + id INTEGER PRIMARY KEY NOT NULL, + player_id INTEGER NOT NULL, + loot_id INTEGER NOT NULL, + resolve INTEGER NOT NULL DEFAULT 0, + FOREIGN KEY (player_id) REFERENCES players(id), + FOREIGN KEY (loot_id) REFERENCES looted(id) +); diff --git a/lootalot_db/src/lib.rs b/lootalot_db/src/lib.rs index 75c65cb..f243542 100644 --- a/lootalot_db/src/lib.rs +++ b/lootalot_db/src/lib.rs @@ -23,18 +23,14 @@ impl Player { Ok(players.load::(conn)?) } - pub fn fetch_chest(player: i32, conn: &SqliteConnection) -> QueryResult> { - let owned = { - use schema::looted::dsl::*; - looted.filter(player_id.eq(player)).select(item_id).load::(conn)? - }; + pub fn fetch_chest(player_id: i32, conn: &SqliteConnection) -> QueryResult> { + use schema::looted::dsl::*; + let owned = looted + .filter(owner_id.eq(player_id)) + .select((id, name, base_price)) + .load::(conn)?; dbg!(&owned); - let chest = { - use schema::items::dsl::*; - items.filter(id.eq_any(owned)).load::(conn)? - }; - dbg!(&chest); - Ok(chest) + Ok(owned) } } diff --git a/lootalot_db/src/schema.rs b/lootalot_db/src/schema.rs index b7c9058..5e7b23e 100644 --- a/lootalot_db/src/schema.rs +++ b/lootalot_db/src/schema.rs @@ -1,3 +1,12 @@ +table! { + claims (id) { + id -> Integer, + player_id -> Integer, + loot_id -> Integer, + resolve -> Integer, + } +} + table! { items (id) { id -> Integer, @@ -9,9 +18,9 @@ table! { table! { looted (id) { id -> Integer, - player_id -> Integer, - item_id -> Integer, - acquired_date -> Date, + name -> Text, + base_price -> Integer, + owner_id -> Integer, } } @@ -27,7 +36,13 @@ table! { } } -joinable!(looted -> items (item_id)); -joinable!(looted -> players (player_id)); +joinable!(claims -> looted (loot_id)); +joinable!(claims -> players (player_id)); +joinable!(looted -> players (owner_id)); -allow_tables_to_appear_in_same_query!(items, looted, players,); +allow_tables_to_appear_in_same_query!( + claims, + items, + looted, + players, +);