write game abilities to db
This commit is contained in:
+33
-5
@@ -363,12 +363,12 @@ pub fn game_new(game: Game, tx: &mut Transaction) -> Result<Game, Error> {
|
||||
|
||||
let query = "
|
||||
INSERT INTO games (id, data)
|
||||
VALUES ($1, $2, $3)
|
||||
RETURNING id, account;
|
||||
VALUES ($1, $2)
|
||||
RETURNING id;
|
||||
";
|
||||
|
||||
let result = tx
|
||||
.query(query, &[&game_bytes, &game.id])?;
|
||||
.query(query, &[&game.id, &game_bytes])?;
|
||||
|
||||
let _returned = result.iter().next().expect("no row returned");
|
||||
|
||||
@@ -377,6 +377,34 @@ pub fn game_new(game: Game, tx: &mut Transaction) -> Result<Game, Error> {
|
||||
return Ok(game);
|
||||
}
|
||||
|
||||
pub fn players_write(game: Game, tx: &mut Transaction) -> Result<Game, Error> {
|
||||
let game_bytes = to_vec(&game)?;
|
||||
|
||||
for team in &game.teams {
|
||||
// pve
|
||||
if !team.id.is_nil() {
|
||||
for cryp in &team.cryps {
|
||||
let id = Uuid::new_v4();
|
||||
|
||||
let query = "
|
||||
INSERT INTO players (id, game, cryp, account)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING id, account;
|
||||
";
|
||||
|
||||
let result = tx
|
||||
.query(query, &[&id, &game.id, &cryp.id, &team.id])?;
|
||||
|
||||
let _returned = result.iter().next().expect("no row written");
|
||||
|
||||
println!("wrote player entry game:{:?} cryp:{:?} account:{:?}", game.id, cryp.id, team.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Ok(game);
|
||||
}
|
||||
|
||||
pub fn game_write(game: Game, tx: &mut Transaction) -> Result<Game, Error> {
|
||||
let game_bytes = to_vec(&game)?;
|
||||
|
||||
@@ -390,7 +418,7 @@ pub fn game_write(game: Game, tx: &mut Transaction) -> Result<Game, Error> {
|
||||
let result = tx
|
||||
.query(query, &[&game_bytes, &game.id])?;
|
||||
|
||||
let _returned = result.iter().next().expect("no row returned");
|
||||
let returned = result.iter().next().expect("no row returned");
|
||||
|
||||
println!("{:?} wrote game", game.id);
|
||||
|
||||
@@ -461,7 +489,7 @@ pub fn game_pve(params: GamePveParams, tx: &mut Transaction, account: &Account)
|
||||
|
||||
game.start();
|
||||
|
||||
Ok(game)
|
||||
return game_new(game, tx);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
Reference in New Issue
Block a user