team setting and postgres constraint

This commit is contained in:
ntr
2019-07-29 00:11:46 +10:00
parent 9fdda7c52c
commit 8cd0f1af36
10 changed files with 121 additions and 75 deletions
+18 -26
View File
@@ -175,12 +175,12 @@ pub fn debit(tx: &mut Transaction, id: Uuid, debit: i64) -> Result<Account, Erro
";
let result = tx
.query(query, &[&debit, &id])?;
.query(query, &[&debit, &id])
.or(Err(err_msg("insufficient balance")))?;
let row = result.iter().next()
.ok_or(format_err!("account not found {:?}", id))?;
let name: String = row.get(1);
let db_balance: i64 = row.get(2);
let balance = u32::try_from(db_balance)
@@ -301,8 +301,7 @@ pub fn account_team(tx: &mut Transaction, account: &Account) -> Result<Vec<Const
SELECT data
FROM constructs
WHERE account = $1
AND team = true
LIMIT 3;
AND team = true;
";
let result = tx
@@ -325,39 +324,32 @@ pub fn account_team(tx: &mut Transaction, account: &Account) -> Result<Vec<Const
}
let mut constructs = constructs.unwrap();
if constructs.len() != 3 {
return Err(format_err!("team not size 3 account={:?}", account));
}
constructs.sort_by_key(|c| c.id);
return Ok(constructs);
}
pub fn account_set_team(tx: &mut Transaction, account: &Account, ids: Vec<Uuid>) -> Result<Vec<Construct>, Error> {
// there is a trigger constraint on the table that enforces
// exactly 3 constructs in a team
pub fn set_team(tx: &mut Transaction, account: &Account, ids: Vec<Uuid>) -> Result<Vec<Construct>, Error> {
let query = "
UPDATE constructs
SET team = false
SET team =
CASE
WHEN id = ANY($2) THEN true
ELSE false
END
WHERE account = $1;
";
let updated = tx
let _updated = tx
.execute(query, &[&account.id, &ids])?;
if updated > 3 {
warn!("team members >3 account={:?} count={:?}", account, updated);
}
let query = "
UPDATE constructs
SET team = true
WHERE account = $1
AND id in $2
RETURNING data;
";
let updated = tx
.execute(query, &[&account.id, &ids])?;
if updated != 3 {
return Err(format_err!("could not create team of 3 account={:?} updated={:?}", account, updated));
}
account_team(tx, account)
}
+5 -2
View File
@@ -6,6 +6,7 @@ use uuid::Uuid;
use failure::Error;
use failure::err_msg;
use account;
use pg::{Db};
use construct::{Construct};
use game::{Game, game_state, game_skill, game_ready};
@@ -54,6 +55,7 @@ enum RpcRequest {
AccountState {},
AccountShop {},
AccountConstructs {},
AccountSetTeam { ids: Vec<Uuid> },
InstancePvp {},
InstancePractice {},
@@ -100,11 +102,12 @@ pub fn receive(data: Vec<u8>, db: &Db, begin: Instant, account: &Option<Account>
RpcRequest::AccountConstructs {} =>
Ok(RpcMessage::AccountConstructs(account_constructs(&mut tx, &account)?)),
RpcRequest::AccountSetTeam { ids } =>
Ok(RpcMessage::AccountConstructs(account::set_team(&mut tx, &account, ids)?)),
// RpcRequest::AccountShop {} =>
// Ok(RpcMessage::AccountShop(mtx::account_shop(&mut tx, &account)?)),
// RpcRequest::ConstructDelete" => handle_construct_delete(data, &mut tx, account),
RpcRequest::GameState { id } =>
+2
View File
@@ -83,6 +83,8 @@ impl Warden {
let db = self.pool.get()?;
let tx = db.transaction()?;
info!("received pair={:?}", pair);
Ok(())
}