Merge branch 'develop' into svt

This commit is contained in:
ntr
2019-09-12 12:47:19 +10:00
66 changed files with 533 additions and 191 deletions
+1 -5
View File
@@ -288,15 +288,11 @@ pub fn set_subscribed(tx: &mut Transaction, id: Uuid, subscribed: bool) -> Resul
Ok(name)
}
pub fn create(name: &String, password: &String, code: &String, tx: &mut Transaction) -> Result<String, MnmlHttpError> {
pub fn create(name: &String, password: &String, tx: &mut Transaction) -> Result<String, MnmlHttpError> {
if password.len() < PASSWORD_MIN_LEN {
return Err(MnmlHttpError::PasswordUnacceptable);
}
if code.to_lowercase() != "grep842" {
return Err(MnmlHttpError::InvalidCode);
}
if name.len() == 0 {
return Err(MnmlHttpError::AccountNameNotProvided);
}
+1 -5
View File
@@ -52,8 +52,6 @@ pub enum MnmlHttpError {
PasswordUnacceptable,
#[fail(display="incorrect token. refresh or logout of existing sessions")]
TokenDoesNotMatch,
#[fail(display="invalid code. https://discord.gg/YJJgurM")]
InvalidCode,
}
impl From<bcrypt::BcryptError> for MnmlHttpError {
@@ -129,7 +127,6 @@ impl From<MnmlHttpError> for IronError {
MnmlHttpError::PasswordUnacceptable => (m_err.compat(), status::BadRequest),
MnmlHttpError::PasswordNotMatch |
MnmlHttpError::InvalidCode |
MnmlHttpError::TokenDoesNotMatch |
MnmlHttpError::Unauthorized => (m_err.compat(), status::Unauthorized),
@@ -211,7 +208,6 @@ fn token_res(token: String) -> Response {
struct RegisterBody {
name: String,
password: String,
code: String,
}
fn register(req: &mut Request) -> IronResult<Response> {
@@ -224,7 +220,7 @@ fn register(req: &mut Request) -> IronResult<Response> {
let db = state.pool.get().or(Err(MnmlHttpError::DbError))?;
let mut tx = db.transaction().or(Err(MnmlHttpError::DbError))?;
match account::create(&params.name, &params.password, &params.code, &mut tx) {
match account::create(&params.name, &params.password, &mut tx) {
Ok(token) => {
tx.commit().or(Err(MnmlHttpError::ServerError))?;
Ok(token_res(token))
+38 -4
View File
@@ -316,10 +316,6 @@ impl Instance {
}
fn finish_condition(&mut self) -> bool {
if self.rounds.len() < 4 {
return false;
}
// tennis
for player in self.players.iter() {
if player.score == Score::Win {
@@ -327,6 +323,10 @@ impl Instance {
return true;
}
}
// Game defaults to lose otherwise
if self.rounds.len() < 4 {
return false;
}
// both players afk
if self.players.iter().all(|p| p.score == Score::Zero) {
@@ -458,6 +458,13 @@ impl Instance {
.ok_or(err_msg("account not in instance"))
}
fn account_opponent(&mut self, account: Uuid) -> Result<&mut Player, Error> {
self.players
.iter_mut()
.find(|p| p.id != account)
.ok_or(err_msg("opponent not in instance"))
}
pub fn vbox_action_allowed(&self, account: Uuid) -> Result<(), Error> {
if self.players.iter().find(|p| p.id == account).is_none() {
return Err(err_msg("player not in this instance"));
@@ -739,6 +746,14 @@ pub fn pvp(tx: &mut Transaction, a: &Account, b: &Account) -> Result<Instance, E
instance_update(tx, instance)
}
pub fn instance_abandon(tx: &mut Transaction, account: &Account, instance_id: Uuid) -> Result<RpcMessage, Error> {
let mut instance = instance_get(tx, instance_id)?;
instance.account_player(account.id)?.set_lose();
instance.account_opponent(account.id)?.set_win();
instance.next_round();
Ok(RpcMessage::InstanceState(instance_update(tx, instance)?))
}
pub fn instance_ready(tx: &mut Transaction, account: &Account, instance_id: Uuid) -> Result<RpcMessage, Error> {
let mut instance = instance_get(tx, instance_id)?;
@@ -800,6 +815,25 @@ pub fn bot_instance() -> Instance {
return instance;
}
pub fn demo() -> Result<Vec<Player>, Error> {
let bot = bot_player();
// generate bot imgs for the client to see
for c in bot.constructs.iter() {
img::molecular_write(c.img)?;
};
let bot2 = bot_player();
// generate bot imgs for the client to see
for c in bot2.constructs.iter() {
img::molecular_write(c.img)?;
};
Ok(vec![bot, bot2])
}
#[cfg(test)]
mod tests {
use super::*;
+1
View File
@@ -27,3 +27,4 @@ pub fn bot_player() -> Player {
let constructs = instance_mobs(bot_id);
Player::new(bot_id, &name(), constructs).set_bot(true)
}
+10
View File
@@ -99,6 +99,16 @@ impl Player {
self
}
pub fn set_win(&mut self) -> &mut Player {
self.score = Score::Win;
self
}
pub fn set_lose(&mut self) -> &mut Player {
self.score = Score::Lose;
self
}
pub fn construct_get(&mut self, id: Uuid) -> Result<&mut Construct, Error> {
self.constructs.iter_mut().find(|c| c.id == id).ok_or(err_msg("construct not found"))
}
+10 -1
View File
@@ -21,10 +21,12 @@ use account;
use construct::{Construct};
use events::{Event};
use game::{Game, game_state, game_skill, game_skill_clear, game_ready};
use instance::{Instance, instance_state, instance_practice, instance_ready};
use instance::{Instance, instance_state, instance_practice, instance_ready, instance_abandon, demo};
use item::{Item, ItemInfoCtr, item_info};
use mtx;
use mail;
use player::{Player};
use payments;
use mail::Email;
use pg::{Db};
@@ -41,6 +43,8 @@ pub enum RpcMessage {
AccountInstances(Vec<Instance>),
AccountShop(mtx::Shop),
Demo(Vec<Player>),
ConstructSpawn(Construct),
GameState(Game),
ItemInfo(ItemInfoCtr),
@@ -88,6 +92,7 @@ enum RpcRequest {
InstanceQueue {},
InstancePractice {},
InstanceAbandon { instance_id: Uuid },
InstanceReady { instance_id: Uuid },
InstanceState { instance_id: Uuid },
@@ -184,6 +189,8 @@ impl Connection {
Ok(instance_ready(&mut tx, account, instance_id)?),
RpcRequest::InstanceState { instance_id } =>
Ok(instance_state(&mut tx, instance_id)?),
RpcRequest::InstanceAbandon { instance_id } =>
Ok(instance_abandon(&mut tx, account, instance_id)?),
RpcRequest::VboxAccept { instance_id, group, index } =>
Ok(RpcMessage::InstanceState(vbox_accept(&mut tx, account, instance_id, group, index)?)),
@@ -268,6 +275,8 @@ impl Handler for Connection {
// tx should do nothing
tx.commit().unwrap();
} else {
self.ws.send(RpcMessage::Demo(demo().unwrap())).unwrap();
}
Ok(())