updates table schema

This commit is contained in:
2019-06-21 13:57:36 +02:00
parent 472e24a62c
commit 0f77a16a8c
10 changed files with 50 additions and 26 deletions

Binary file not shown.

View File

@@ -1,2 +1,3 @@
DROP TABLE items; DROP TABLE items;
DROP TABLE looted;

View File

@@ -1,5 +1,15 @@
-- The global inventory of items
CREATE TABLE items ( CREATE TABLE items (
id INTEGER PRIMARY KEY NOT NULL, id INTEGER PRIMARY KEY NOT NULL,
name VARCHAR NOT NULL, name VARCHAR NOT NULL,
base_price INTEGER 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)
);

View File

@@ -7,3 +7,5 @@ CREATE TABLE players (
gp INTEGER DEFAULT 0 NOT NULL, gp INTEGER DEFAULT 0 NOT NULL,
pp INTEGER DEFAULT 0 NOT NULL pp INTEGER DEFAULT 0 NOT NULL
); );
INSERT INTO players (id, name) VALUES (0, 'Groupe');

View File

@@ -1 +0,0 @@
DROP TABLE looted;

View File

@@ -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)
);

View File

@@ -0,0 +1 @@
DROP TABLE claims;

View File

@@ -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)
);

View File

@@ -23,18 +23,14 @@ impl Player {
Ok(players.load::<Self>(conn)?) Ok(players.load::<Self>(conn)?)
} }
pub fn fetch_chest(player: i32, conn: &SqliteConnection) -> QueryResult<Vec<Item>> { pub fn fetch_chest(player_id: i32, conn: &SqliteConnection) -> QueryResult<Vec<Item>> {
let owned = {
use schema::looted::dsl::*; use schema::looted::dsl::*;
looted.filter(player_id.eq(player)).select(item_id).load::<i32>(conn)? let owned = looted
}; .filter(owner_id.eq(player_id))
.select((id, name, base_price))
.load::<Item>(conn)?;
dbg!(&owned); dbg!(&owned);
let chest = { Ok(owned)
use schema::items::dsl::*;
items.filter(id.eq_any(owned)).load::<Item>(conn)?
};
dbg!(&chest);
Ok(chest)
} }
} }

View File

@@ -1,3 +1,12 @@
table! {
claims (id) {
id -> Integer,
player_id -> Integer,
loot_id -> Integer,
resolve -> Integer,
}
}
table! { table! {
items (id) { items (id) {
id -> Integer, id -> Integer,
@@ -9,9 +18,9 @@ table! {
table! { table! {
looted (id) { looted (id) {
id -> Integer, id -> Integer,
player_id -> Integer, name -> Text,
item_id -> Integer, base_price -> Integer,
acquired_date -> Date, owner_id -> Integer,
} }
} }
@@ -27,7 +36,13 @@ table! {
} }
} }
joinable!(looted -> items (item_id)); joinable!(claims -> looted (loot_id));
joinable!(looted -> players (player_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,
);