so fresh, so clean
This commit is contained in:
parent
7bf4d9954a
commit
e5b2eb48c9
@ -28,14 +28,11 @@
|
||||
## NOW
|
||||
|
||||
*$$$*
|
||||
|
||||
* rerolls cost 1cr
|
||||
* rename costs 1cr
|
||||
* invader set
|
||||
|
||||
* new construct costs 5cr
|
||||
|
||||
|
||||
* eth adapter
|
||||
|
||||
|
||||
|
||||
@ -140,7 +140,7 @@ function createSocket(events) {
|
||||
}
|
||||
|
||||
function sendMtxApply(constructId, mtx) {
|
||||
send({ method: 'mtx_apply', params: { construct_id: constructId, mtx } });
|
||||
send(['MtxConstructApply', { construct_id: constructId, mtx }]);
|
||||
// events.clearMtxActive();
|
||||
}
|
||||
|
||||
@ -155,7 +155,7 @@ function createSocket(events) {
|
||||
|
||||
function onAccountInstances(list) {
|
||||
events.setAccountInstances(list);
|
||||
setTimeout(sendAccountInstances, 5000);
|
||||
// setTimeout(sendAccountInstances, 5000);
|
||||
}
|
||||
|
||||
function onAccountConstructs(constructs) {
|
||||
@ -205,7 +205,7 @@ function createSocket(events) {
|
||||
let pongTimeout;
|
||||
function onPong() {
|
||||
events.setPing(Date.now() - ping);
|
||||
pongTimeout = setTimeout(sendPing, 1000);
|
||||
// pongTimeout = setTimeout(sendPing, 1000);
|
||||
}
|
||||
|
||||
// -------------
|
||||
|
||||
@ -9,7 +9,6 @@ use serde_cbor::{from_slice};
|
||||
use postgres::transaction::Transaction;
|
||||
|
||||
use names::{name as generate_name};
|
||||
use rpc::{ConstructSpawnParams};
|
||||
use construct::{Construct, construct_recover, construct_spawn};
|
||||
use instance::{Instance, instance_delete};
|
||||
use mtx::{Mtx, FREE_MTX};
|
||||
@ -253,7 +252,7 @@ pub fn create(name: &String, password: &String, code: &String, tx: &mut Transact
|
||||
|
||||
// 3 constructs for a team and 1 to swap
|
||||
for _i in 0..4 {
|
||||
construct_spawn(tx, ConstructSpawnParams { name: generate_name() }, id)?;
|
||||
construct_spawn(tx, id, generate_name())?;
|
||||
}
|
||||
|
||||
for mtx in FREE_MTX.iter() {
|
||||
|
||||
@ -7,7 +7,6 @@ use postgres::transaction::Transaction;
|
||||
use failure::Error;
|
||||
use failure::err_msg;
|
||||
|
||||
use rpc::{ConstructSpawnParams};
|
||||
use skill::{Skill, Cast, Immunity, Disable, Event};
|
||||
use effect::{Cooldown, Effect, Colour};
|
||||
use spec::{Spec};
|
||||
@ -866,9 +865,9 @@ pub fn construct_select(tx: &mut Transaction, id: Uuid, account_id: Uuid) -> Res
|
||||
return Ok(construct);
|
||||
}
|
||||
|
||||
pub fn construct_spawn(tx: &mut Transaction, params: ConstructSpawnParams, account: Uuid) -> Result<Construct, Error> {
|
||||
pub fn construct_spawn(tx: &mut Transaction, account: Uuid, name: String) -> Result<Construct, Error> {
|
||||
let construct = Construct::new()
|
||||
.named(¶ms.name)
|
||||
.named(&name)
|
||||
.set_account(account);
|
||||
|
||||
let construct_bytes = to_vec(&construct)?;
|
||||
|
||||
@ -12,7 +12,7 @@ use failure::Error;
|
||||
use failure::err_msg;
|
||||
|
||||
use account::Account;
|
||||
use rpc::{GameStateParams, GameSkillParams};
|
||||
|
||||
use construct::{Construct};
|
||||
use skill::{Skill, Cast, Resolution, Event, resolution_steps};
|
||||
use effect::{Effect};
|
||||
@ -628,8 +628,8 @@ pub fn game_write(tx: &mut Transaction, game: &Game) -> Result<(), Error> {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
pub fn game_state(params: GameStateParams, tx: &mut Transaction, _account: &Account) -> Result<Game, Error> {
|
||||
return game_get(tx, params.id)
|
||||
pub fn game_state(tx: &mut Transaction, _account: &Account, id: Uuid) -> Result<Game, Error> {
|
||||
return game_get(tx, id)
|
||||
}
|
||||
|
||||
pub fn game_get(tx: &mut Transaction, id: Uuid) -> Result<Game, Error> {
|
||||
@ -813,10 +813,10 @@ pub fn game_update(tx: &mut Transaction, game: &Game) -> Result<(), Error> {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
pub fn game_skill(params: GameSkillParams, tx: &mut Transaction, account: &Account) -> Result<Game, Error> {
|
||||
let mut game = game_get(tx, params.game_id)?;
|
||||
pub fn game_skill(tx: &mut Transaction, account: &Account, game_id: Uuid, construct_id: Uuid, target_construct_id: Option<Uuid>, skill: Skill) -> Result<Game, Error> {
|
||||
let mut game = game_get(tx, game_id)?;
|
||||
|
||||
game.add_skill(account.id, params.construct_id, params.target_construct_id, params.skill)?;
|
||||
game.add_skill(account.id, construct_id, target_construct_id, skill)?;
|
||||
|
||||
if game.skill_phase_finished() {
|
||||
game = game.resolve_phase_start();
|
||||
@ -827,8 +827,8 @@ pub fn game_skill(params: GameSkillParams, tx: &mut Transaction, account: &Accou
|
||||
Ok(game)
|
||||
}
|
||||
|
||||
pub fn game_ready(params: GameStateParams, tx: &mut Transaction, account: &Account) -> Result<Game, Error> {
|
||||
let mut game = game_get(tx, params.id)?;
|
||||
pub fn game_ready(tx: &mut Transaction, account: &Account, id: Uuid) -> Result<Game, Error> {
|
||||
let mut game = game_get(tx, id)?;
|
||||
|
||||
game.player_ready(account.id)?;
|
||||
|
||||
|
||||
@ -11,11 +11,11 @@ pub fn img_molecular_write(id: Uuid) -> Result<Uuid, Error> {
|
||||
for _i in 0..100 {
|
||||
let mol: u32 = rng.gen_range(0, 10000);
|
||||
let src = format!("/var/lib/mnml/data/molecules/{}.svg", mol);
|
||||
println!("{:?}", src);
|
||||
let dest = format!("/var/lib/mnml/public/imgs/{}.svg", id);
|
||||
println!("{:?}", dest);
|
||||
debug!("molecule src={:?}", src);
|
||||
debug!("molecule dest={:?}", dest);
|
||||
if let Ok(_bytes) = copy(&src, &dest) {
|
||||
info!("new molecule avatar generated src={:?} dest={:?}", src, dest);
|
||||
info!("new molecule img generated src={:?} dest={:?}", src, dest);
|
||||
return Ok(id);
|
||||
}
|
||||
}
|
||||
|
||||
@ -7,13 +7,10 @@ use postgres::transaction::Transaction;
|
||||
use failure::Error;
|
||||
use failure::err_msg;
|
||||
|
||||
use std::iter;
|
||||
|
||||
// timekeeping
|
||||
use chrono::prelude::*;
|
||||
use chrono::Duration;
|
||||
|
||||
use rpc::{InstanceLobbyParams, InstanceJoinParams, InstanceReadyParams, InstanceStateParams};
|
||||
use account::Account;
|
||||
use player::{Player, player_create};
|
||||
use construct::{Construct, construct_get};
|
||||
@ -21,7 +18,7 @@ use mob::{bot_player, instance_mobs};
|
||||
use game::{Game, Phase, game_get, game_write};
|
||||
use item::{Item};
|
||||
use rpc::{RpcResult};
|
||||
use names::{name};
|
||||
|
||||
use img::{img_molecular_write};
|
||||
|
||||
#[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)]
|
||||
@ -701,8 +698,8 @@ pub fn instances_idle(tx: &mut Transaction) -> Result<Vec<Instance>, Error> {
|
||||
}
|
||||
|
||||
|
||||
pub fn instance_new(params: InstanceLobbyParams, tx: &mut Transaction, account: &Account) -> Result<Instance, Error> {
|
||||
let mut instance = match params.pve {
|
||||
pub fn instance_new(tx: &mut Transaction, account: &Account, construct_ids: Vec<Uuid>, name: String, pve: bool, password: Option<String>) -> Result<Instance, Error> {
|
||||
let mut instance = match pve {
|
||||
true => {
|
||||
let bot = bot_player();
|
||||
|
||||
@ -714,26 +711,24 @@ pub fn instance_new(params: InstanceLobbyParams, tx: &mut Transaction, account:
|
||||
let mut instance = Instance::new()
|
||||
.set_time_control(TimeControl::Practice)
|
||||
.set_max_rounds(10)
|
||||
.set_name(params.name)?;
|
||||
.set_name(name)?;
|
||||
|
||||
instance.add_player(bot)?;
|
||||
|
||||
instance
|
||||
},
|
||||
false => Instance::new()
|
||||
.set_name(params.name)?
|
||||
.set_name(name)?
|
||||
};
|
||||
|
||||
instance = instance_create(tx, instance)?;
|
||||
let join_params = InstanceJoinParams { instance_id: instance.id, construct_ids: params.construct_ids };
|
||||
|
||||
instance_join(join_params, tx, account)
|
||||
instance_join(tx, account, instance.id, construct_ids)
|
||||
}
|
||||
|
||||
pub fn instance_join(params: InstanceJoinParams, tx: &mut Transaction, account: &Account) -> Result<Instance, Error> {
|
||||
let mut instance = instance_get(tx, params.instance_id)?;
|
||||
pub fn instance_join(tx: &mut Transaction, account: &Account, instance_id: Uuid, construct_ids: Vec<Uuid>) -> Result<Instance, Error> {
|
||||
let mut instance = instance_get(tx, instance_id)?;
|
||||
|
||||
let constructs = params.construct_ids
|
||||
let constructs = construct_ids
|
||||
.iter()
|
||||
.map(|id| construct_get(tx, *id, account.id))
|
||||
.collect::<Result<Vec<Construct>, Error>>()?;
|
||||
@ -749,8 +744,8 @@ pub fn instance_join(params: InstanceJoinParams, tx: &mut Transaction, account:
|
||||
instance_update(tx, instance)
|
||||
}
|
||||
|
||||
pub fn instance_ready(params: InstanceReadyParams, tx: &mut Transaction, account: &Account) -> Result<Instance, Error> {
|
||||
let mut instance = instance_get(tx, params.instance_id)?;
|
||||
pub fn instance_ready(tx: &mut Transaction, account: &Account, instance_id: Uuid) -> Result<Instance, Error> {
|
||||
let mut instance = instance_get(tx, instance_id)?;
|
||||
let player_id = instance.account_player(account.id)?.id;
|
||||
|
||||
if let Some(game) = instance.player_ready(player_id)? {
|
||||
@ -760,8 +755,8 @@ pub fn instance_ready(params: InstanceReadyParams, tx: &mut Transaction, account
|
||||
instance_update(tx, instance)
|
||||
}
|
||||
|
||||
pub fn instance_state(params: InstanceStateParams, tx: &mut Transaction, _account: &Account) -> Result<RpcResult, Error> {
|
||||
let instance = instance_get(tx, params.instance_id)?;
|
||||
pub fn instance_state(tx: &mut Transaction, _account: &Account, instance_id: Uuid) -> Result<RpcResult, Error> {
|
||||
let instance = instance_get(tx, instance_id)?;
|
||||
|
||||
if let Some(game_id) = instance.current_game_id() {
|
||||
let game = game_get(tx, game_id)?;
|
||||
|
||||
@ -33,6 +33,8 @@ impl MtxVariant {
|
||||
}
|
||||
}
|
||||
|
||||
// could use postgres-derive
|
||||
// but enums in pg are a bit of a pain
|
||||
impl TryFrom<String> for MtxVariant {
|
||||
type Error = Error;
|
||||
fn try_from(v: String) -> Result<MtxVariant, Error> {
|
||||
|
||||
@ -12,7 +12,6 @@ use r2d2::{Pool};
|
||||
use r2d2::{PooledConnection};
|
||||
use r2d2_postgres::{TlsMode, PostgresConnectionManager};
|
||||
|
||||
use rpc::{AccountLoginParams, AccountCreateParams};
|
||||
use warden::{warden};
|
||||
use pubsub::{pg_listen};
|
||||
use ws::{connect};
|
||||
@ -98,6 +97,12 @@ fn logout_res() -> HttpResponse {
|
||||
.finish()
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
struct AccountLoginParams {
|
||||
name: String,
|
||||
password: String,
|
||||
}
|
||||
|
||||
fn login(state: web::Data<State>, params: web::Json::<AccountLoginParams>) -> Result<HttpResponse, MnmlHttpError> {
|
||||
let db = state.pool.get().or(Err(MnmlHttpError::ServerError))?;
|
||||
let mut tx = db.transaction().or(Err(MnmlHttpError::ServerError))?;
|
||||
@ -133,7 +138,14 @@ fn logout(r: HttpRequest, state: web::Data<State>) -> Result<HttpResponse, MnmlH
|
||||
}
|
||||
}
|
||||
|
||||
fn register(state: web::Data<State>, params: web::Json::<AccountCreateParams>) -> Result<HttpResponse, MnmlHttpError> {
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
struct AccountRegisterParams {
|
||||
name: String,
|
||||
password: String,
|
||||
code: String,
|
||||
}
|
||||
|
||||
fn register(state: web::Data<State>, params: web::Json::<AccountRegisterParams>) -> Result<HttpResponse, MnmlHttpError> {
|
||||
let db = state.pool.get().or(Err(MnmlHttpError::ServerError))?;
|
||||
let mut tx = db.transaction().or(Err(MnmlHttpError::ServerError))?;
|
||||
|
||||
|
||||
@ -2,7 +2,6 @@ use std::time::{Instant};
|
||||
|
||||
use actix_web_actors::ws;
|
||||
|
||||
use postgres::transaction::Transaction;
|
||||
use serde_cbor::{from_slice};
|
||||
use uuid::Uuid;
|
||||
use failure::Error;
|
||||
@ -10,7 +9,7 @@ use failure::err_msg;
|
||||
|
||||
use net::{Db};
|
||||
use ws::{MnmlSocket};
|
||||
use construct::{Construct, construct_spawn, construct_delete};
|
||||
use construct::{Construct};
|
||||
use game::{Game, game_state, game_skill, game_ready};
|
||||
use account::{Account, account_constructs, account_instances};
|
||||
use skill::{Skill, dev_resolve, Resolutions};
|
||||
@ -22,208 +21,6 @@ use mtx;
|
||||
|
||||
type MnmlWs = ws::WebsocketContext<MnmlSocket>;
|
||||
|
||||
pub fn receive(data: Vec<u8>, db: &Db, client: &mut MnmlWs, begin: Instant, account: Option<&Account>) -> Result<RpcResult, Error> {
|
||||
// cast the msg to this type to receive method name
|
||||
match from_slice::<RpcMessage>(&data) {
|
||||
Ok(v) => {
|
||||
if v.method == "ping" {
|
||||
return Ok(RpcResult::Pong(()));
|
||||
}
|
||||
|
||||
let mut tx = db.transaction()?;
|
||||
|
||||
let account_name = match account {
|
||||
Some(a) => a.name.clone(),
|
||||
None => "none".to_string(),
|
||||
};
|
||||
|
||||
// check the method
|
||||
// if no auth required
|
||||
match v.method.as_ref() {
|
||||
"item_info" => return Ok(RpcResult::ItemInfo(item_info())),
|
||||
"dev_game_resolve" => return handle_dev_resolve(data),
|
||||
_ => match account {
|
||||
Some(_) => (),
|
||||
None => return Err(err_msg("auth required")),
|
||||
},
|
||||
};
|
||||
|
||||
let account = account.unwrap();
|
||||
|
||||
// now we have the method name
|
||||
// match on that to determine what fn to call
|
||||
let response = match v.method.as_ref() {
|
||||
"account_state" => return Ok(RpcResult::AccountState(account.clone())),
|
||||
"account_constructs" => handle_account_constructs(data, &mut tx, account),
|
||||
"account_instances" => handle_account_instances(data, &mut tx, account),
|
||||
|
||||
"construct_spawn" => handle_construct_spawn(data, &mut tx, account),
|
||||
"construct_delete" => handle_construct_delete(data, &mut tx, account),
|
||||
|
||||
"game_state" => handle_game_state(data, &mut tx, account),
|
||||
"game_skill" => handle_game_skill(data, &mut tx, account),
|
||||
"game_ready" => handle_game_ready(data, &mut tx, account),
|
||||
|
||||
"instance_list" => handle_instance_list(data, &mut tx, account),
|
||||
"instance_join" => handle_instance_join(data, &mut tx, account),
|
||||
"instance_ready" => handle_instance_ready(data, &mut tx, account),
|
||||
"instance_new" => handle_instance_new(data, &mut tx, account),
|
||||
"instance_state" => handle_instance_state(data, &mut tx, account),
|
||||
|
||||
"mtx_apply" => handle_mtx_apply(data, &mut tx, client, account),
|
||||
|
||||
"vbox_accept" => handle_vbox_accept(data, &mut tx, account),
|
||||
"vbox_apply" => handle_vbox_apply(data, &mut tx, account),
|
||||
"vbox_combine" => handle_vbox_combine(data, &mut tx, account),
|
||||
"vbox_discard" => handle_vbox_discard(data, &mut tx, account),
|
||||
"vbox_reclaim" => handle_vbox_reclaim(data, &mut tx, account),
|
||||
"vbox_unequip" => handle_vbox_unequip(data, &mut tx, account),
|
||||
|
||||
_ => Err(format_err!("unknown method - {:?}", v.method)),
|
||||
};
|
||||
|
||||
tx.commit()?;
|
||||
|
||||
info!("method={:?} account={:?} duration={:?}", v.method, account_name, begin.elapsed());
|
||||
|
||||
return response;
|
||||
},
|
||||
Err(e) => {
|
||||
info!("{:?}", e);
|
||||
Err(err_msg("unknown error"))
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_game_state(data: Vec<u8>, tx: &mut Transaction, account: &Account) -> Result<RpcResult, Error> {
|
||||
let msg = from_slice::<GameStateMsg>(&data).or(Err(err_msg("invalid params")))?;
|
||||
return Ok(RpcResult::GameState(game_state(msg.params, tx, &account)?));
|
||||
}
|
||||
|
||||
// fn handle_game_pve(data: Vec<u8>, tx: &mut Transaction, account: &Account) -> Result<RpcResult, Error> {
|
||||
// let msg = from_slice::<GamePveMsg>(&data).or(Err(err_msg("invalid params")))?;
|
||||
|
||||
// let game_response = RpcResponse {
|
||||
// method: "game_state".to_string(),
|
||||
// params: RpcResult::GameState(game_pve(msg.params, tx, &account)?)
|
||||
// };
|
||||
|
||||
// return Ok(game_response);
|
||||
// }
|
||||
|
||||
fn handle_game_skill(data: Vec<u8>, tx: &mut Transaction, account: &Account) -> Result<RpcResult, Error> {
|
||||
let msg = from_slice::<GameSkillMsg>(&data).or(Err(err_msg("invalid params")))?;
|
||||
Ok(RpcResult::GameState(game_skill(msg.params, tx, &account)?))
|
||||
}
|
||||
|
||||
fn handle_game_ready(data: Vec<u8>, tx: &mut Transaction, account: &Account) -> Result<RpcResult, Error> {
|
||||
let msg = from_slice::<GameStateMsg>(&data).or(Err(err_msg("invalid params")))?;
|
||||
Ok(RpcResult::GameState(game_ready(msg.params, tx, &account)?))
|
||||
}
|
||||
|
||||
fn handle_construct_spawn(data: Vec<u8>, tx: &mut Transaction, account: &Account) -> Result<RpcResult, Error> {
|
||||
let msg = from_slice::<ConstructSpawnMsg>(&data).or(Err(err_msg("invalid params")))?;
|
||||
construct_spawn(tx, msg.params, account.id)?;
|
||||
Ok(RpcResult::AccountConstructs(account_constructs(tx, account)?))
|
||||
}
|
||||
|
||||
fn handle_construct_delete(data: Vec<u8>, tx: &mut Transaction, account: &Account) -> Result<RpcResult, Error> {
|
||||
let msg = from_slice::<ConstructDeleteMsg>(&data).or(Err(err_msg("invalid params")))?;
|
||||
construct_delete(tx, msg.params.id, account.id)?;
|
||||
Ok(RpcResult::AccountConstructs(account_constructs(tx, &account)?))
|
||||
}
|
||||
|
||||
// fn handle_account::create(data: Vec<u8>, tx: &mut Transaction) -> Result<RpcResult, Error> {
|
||||
// let msg = from_slice::<AccountCreateMsg>(&data).or(Err(err_msg("invalid params")))?;
|
||||
// let account = account::create(msg.params, tx)?;
|
||||
// Ok(RpcResult::Account(account))
|
||||
// }
|
||||
|
||||
// fn handle_account_login(data: Vec<u8>, tx: &mut Transaction) -> Result<RpcResult, Error> {
|
||||
// let msg = from_slice::<AccountLoginMsg>(&data).or(Err(err_msg("invalid params")))?;
|
||||
// Ok(RpcResult::Account(account_login(msg.params, tx)?))
|
||||
// }
|
||||
|
||||
fn handle_account_constructs(_data: Vec<u8>, tx: &mut Transaction, account: &Account) -> Result<RpcResult, Error> {
|
||||
Ok(RpcResult::AccountConstructs(account_constructs(tx, &account)?))
|
||||
}
|
||||
|
||||
fn handle_account_instances(_data: Vec<u8>, tx: &mut Transaction, account: &Account) -> Result<RpcResult, Error> {
|
||||
Ok(RpcResult::AccountInstances(account_instances(tx, &account)?))
|
||||
}
|
||||
|
||||
fn handle_instance_new(data: Vec<u8>, tx: &mut Transaction, account: &Account) -> Result<RpcResult, Error> {
|
||||
let msg = from_slice::<InstanceLobbyMsg>(&data).or(Err(err_msg("invalid params")))?;
|
||||
Ok(RpcResult::InstanceState(instance_new(msg.params, tx, &account)?))
|
||||
}
|
||||
|
||||
fn handle_instance_ready(data: Vec<u8>, tx: &mut Transaction, account: &Account) -> Result<RpcResult, Error> {
|
||||
let msg = from_slice::<InstanceReadyMsg>(&data).or(Err(err_msg("invalid params")))?;
|
||||
Ok(RpcResult::InstanceState(instance_ready(msg.params, tx, &account)?))
|
||||
}
|
||||
|
||||
fn handle_instance_join(data: Vec<u8>, tx: &mut Transaction, account: &Account) -> Result<RpcResult, Error> {
|
||||
let msg = from_slice::<InstanceJoinMsg>(&data).or(Err(err_msg("invalid params")))?;
|
||||
Ok(RpcResult::InstanceState(instance_join(msg.params, tx, &account)?))
|
||||
}
|
||||
|
||||
fn handle_instance_list(_data: Vec<u8>, tx: &mut Transaction, _account: &Account) -> Result<RpcResult, Error> {
|
||||
Ok(RpcResult::OpenInstances(instance_list(tx)?))
|
||||
}
|
||||
|
||||
|
||||
fn handle_instance_state(data: Vec<u8>, tx: &mut Transaction, account: &Account) -> Result<RpcResult, Error> {
|
||||
let msg = from_slice::<InstanceStateMsg>(&data).or(Err(err_msg("invalid params")))?;
|
||||
match instance_state(msg.params, tx, &account)? {
|
||||
RpcResult::GameState(p) => Ok(RpcResult::GameState(p)),
|
||||
RpcResult::InstanceState(p) => Ok(RpcResult::InstanceState(p)),
|
||||
_ => Err(err_msg("unhandled instance state"))
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_mtx_apply(data: Vec<u8>, tx: &mut Transaction, client: &mut MnmlWs, account: &Account) -> Result<RpcResult, Error> {
|
||||
let msg = from_slice::<MtxApplyMsg>(&data).or(Err(err_msg("invalid params")))?;
|
||||
mtx::apply(tx, msg.params.mtx, msg.params.construct_id, account.id)?;
|
||||
|
||||
Ok(RpcResult::AccountConstructs(account_constructs(tx, &account)?))
|
||||
}
|
||||
|
||||
fn handle_vbox_accept(data: Vec<u8>, tx: &mut Transaction, account: &Account) -> Result<RpcResult, Error> {
|
||||
let msg = from_slice::<VboxAcceptMsg>(&data).or(Err(err_msg("invalid params")))?;
|
||||
Ok(RpcResult::InstanceState(vbox_accept(msg.params, tx, &account)?))
|
||||
}
|
||||
|
||||
fn handle_vbox_discard(data: Vec<u8>, tx: &mut Transaction, account: &Account) -> Result<RpcResult, Error> {
|
||||
let msg = from_slice::<VboxDiscardMsg>(&data).or(Err(err_msg("invalid params")))?;
|
||||
Ok(RpcResult::InstanceState(vbox_discard(msg.params, tx, &account)?))
|
||||
}
|
||||
|
||||
|
||||
fn handle_vbox_combine(data: Vec<u8>, tx: &mut Transaction, account: &Account) -> Result<RpcResult, Error> {
|
||||
let msg = from_slice::<VboxCombineMsg>(&data).or(Err(err_msg("invalid params")))?;
|
||||
Ok(RpcResult::InstanceState(vbox_combine(msg.params, tx, &account)?))
|
||||
}
|
||||
|
||||
fn handle_vbox_apply(data: Vec<u8>, tx: &mut Transaction, account: &Account) -> Result<RpcResult, Error> {
|
||||
let msg = from_slice::<VboxApplyMsg>(&data).or(Err(err_msg("invalid params")))?;
|
||||
Ok(RpcResult::InstanceState(vbox_apply(msg.params, tx, &account)?))
|
||||
}
|
||||
|
||||
fn handle_vbox_reclaim(data: Vec<u8>, tx: &mut Transaction, account: &Account) -> Result<RpcResult, Error> {
|
||||
let msg = from_slice::<VboxReclaimMsg>(&data).or(Err(err_msg("invalid params")))?;
|
||||
Ok(RpcResult::InstanceState(vbox_reclaim(msg.params, tx, &account)?))
|
||||
}
|
||||
|
||||
fn handle_vbox_unequip(data: Vec<u8>, tx: &mut Transaction, account: &Account) -> Result<RpcResult, Error> {
|
||||
let msg = from_slice::<VboxUnequipMsg>(&data).or(Err(err_msg("invalid params")))?;
|
||||
Ok(RpcResult::InstanceState(vbox_unequip(msg.params, tx, &account)?))
|
||||
}
|
||||
|
||||
fn handle_dev_resolve(data: Vec<u8>) -> Result<RpcResult, Error> {
|
||||
let msg = from_slice::<DevResolveMsg>(&data).or(Err(err_msg("invalid params")))?;
|
||||
Ok(RpcResult::DevResolutions(dev_resolve(msg.params.a, msg.params.b, msg.params.skill)))
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
pub enum RpcResult {
|
||||
AccountState(Account),
|
||||
@ -241,245 +38,136 @@ pub enum RpcResult {
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
pub struct RpcMessage {
|
||||
method: String,
|
||||
token: Option<String>,
|
||||
enum RpcRequest {
|
||||
Ping {},
|
||||
ItemInfo {},
|
||||
DevResolve { a: Uuid, b: Uuid, skill: Skill },
|
||||
|
||||
MtxConstructApply { mtx: mtx::MtxVariant, construct_id: Uuid },
|
||||
MtxAccountApply { mtx: mtx::MtxVariant },
|
||||
|
||||
GameState { id: Uuid },
|
||||
GameReady { id: Uuid },
|
||||
GameSkill { game_id: Uuid, construct_id: Uuid, target_construct_id: Option<Uuid>, skill: Skill },
|
||||
|
||||
AccountState {},
|
||||
AccountConstructs {},
|
||||
AccountInstances {},
|
||||
|
||||
InstanceList {},
|
||||
InstanceLobby { construct_ids: Vec<Uuid>, name: String, pve: bool, password: Option<String> },
|
||||
InstanceJoin { instance_id: Uuid, construct_ids: Vec<Uuid> },
|
||||
InstanceReady { instance_id: Uuid },
|
||||
InstanceState { instance_id: Uuid },
|
||||
|
||||
VboxAccept { instance_id: Uuid, group: usize, index: usize },
|
||||
VboxDiscard { instance_id: Uuid },
|
||||
VboxCombine { instance_id: Uuid, indices: Vec<usize> },
|
||||
VboxApply { instance_id: Uuid, construct_id: Uuid, index: usize },
|
||||
VboxUnequip { instance_id: Uuid, construct_id: Uuid, target: Item },
|
||||
VboxReclaim { instance_id: Uuid, index: usize },
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
struct ConstructSpawnMsg {
|
||||
method: String,
|
||||
params: ConstructSpawnParams,
|
||||
pub fn receive(data: Vec<u8>, db: &Db, _client: &mut MnmlWs, begin: Instant, account: Option<&Account>) -> Result<RpcResult, Error> {
|
||||
// cast the msg to this type to receive method name
|
||||
match from_slice::<RpcRequest>(&data) {
|
||||
Ok(v) => {
|
||||
|
||||
// non authenticated
|
||||
// non transactional reqs
|
||||
match v {
|
||||
RpcRequest::Ping {} => return Ok(RpcResult::Pong(())),
|
||||
RpcRequest::ItemInfo {} => return Ok(RpcResult::ItemInfo(item_info())),
|
||||
RpcRequest::DevResolve {a, b, skill } =>
|
||||
return Ok(RpcResult::DevResolutions(dev_resolve(a, b, skill))),
|
||||
_ => (),
|
||||
};
|
||||
|
||||
// check for authorization now
|
||||
let account = match account {
|
||||
Some(account) => account,
|
||||
None => return Err(err_msg("auth required")),
|
||||
};
|
||||
|
||||
// all good, let's make a tx and process
|
||||
let mut tx = db.transaction()?;
|
||||
|
||||
let request = v.clone();
|
||||
|
||||
let response = match v {
|
||||
RpcRequest::AccountState {} =>
|
||||
return Ok(RpcResult::AccountState(account.clone())),
|
||||
RpcRequest::AccountConstructs {} =>
|
||||
Ok(RpcResult::AccountConstructs(account_constructs(&mut tx, &account)?)),
|
||||
RpcRequest::AccountInstances {} =>
|
||||
Ok(RpcResult::AccountInstances(account_instances(&mut tx, &account)?)),
|
||||
|
||||
|
||||
// RpcRequest::ConstructDelete" => handle_construct_delete(data, &mut tx, account),
|
||||
|
||||
RpcRequest::GameState { id } =>
|
||||
Ok(RpcResult::GameState(game_state(&mut tx, account, id)?)),
|
||||
|
||||
RpcRequest::GameSkill { game_id, construct_id, target_construct_id, skill } =>
|
||||
Ok(RpcResult::GameState(game_skill(&mut tx, account, game_id, construct_id, target_construct_id, skill)?)),
|
||||
|
||||
RpcRequest::GameReady { id } =>
|
||||
Ok(RpcResult::GameState(game_ready(&mut tx, account, id)?)),
|
||||
|
||||
RpcRequest::InstanceList {} =>
|
||||
Ok(RpcResult::OpenInstances(instance_list(&mut tx)?)),
|
||||
RpcRequest::InstanceLobby { construct_ids, name, pve, password } =>
|
||||
Ok(RpcResult::InstanceState(instance_new(&mut tx, account, construct_ids, name, pve, password)?)),
|
||||
RpcRequest::InstanceJoin { instance_id, construct_ids } =>
|
||||
Ok(RpcResult::InstanceState(instance_join(&mut tx, account, instance_id, construct_ids)?)),
|
||||
RpcRequest::InstanceReady { instance_id } =>
|
||||
Ok(RpcResult::InstanceState(instance_ready(&mut tx, account, instance_id)?)),
|
||||
RpcRequest::InstanceState { instance_id } =>
|
||||
Ok(instance_state(&mut tx, account, instance_id)?),
|
||||
|
||||
RpcRequest::VboxAccept { instance_id, group, index } =>
|
||||
Ok(RpcResult::InstanceState(vbox_accept(&mut tx, account, instance_id, group, index)?)),
|
||||
|
||||
RpcRequest::VboxApply { instance_id, construct_id, index } =>
|
||||
Ok(RpcResult::InstanceState(vbox_apply(&mut tx, account, instance_id, construct_id, index)?)),
|
||||
|
||||
RpcRequest::VboxCombine { instance_id, indices } =>
|
||||
Ok(RpcResult::InstanceState(vbox_combine(&mut tx, account, instance_id, indices)?)),
|
||||
|
||||
RpcRequest::VboxDiscard { instance_id } =>
|
||||
Ok(RpcResult::InstanceState(vbox_discard(&mut tx, account, instance_id)?)),
|
||||
|
||||
RpcRequest::VboxReclaim { instance_id, index } =>
|
||||
Ok(RpcResult::InstanceState(vbox_reclaim(&mut tx, account, instance_id, index)?)),
|
||||
|
||||
RpcRequest::VboxUnequip { instance_id, construct_id, target } =>
|
||||
Ok(RpcResult::InstanceState(vbox_unequip(&mut tx, account, instance_id, construct_id, target)?)),
|
||||
|
||||
// "mtx_apply" => handle_mtx_apply(data, &mut tx, account),
|
||||
|
||||
_ => Err(format_err!("unknown request request={:?}", request)),
|
||||
};
|
||||
|
||||
tx.commit()?;
|
||||
|
||||
info!("request={:?} account={:?} duration={:?}", request, account.name, begin.elapsed());
|
||||
|
||||
return response;
|
||||
},
|
||||
Err(e) => {
|
||||
info!("{:?}", e);
|
||||
Err(err_msg("invalid message"))
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
pub struct ConstructSpawnParams {
|
||||
pub name: String,
|
||||
}
|
||||
// fn handle_mtx_apply(data: Vec<u8>, tx: &mut Transaction, account: &Account) -> Result<RpcResult, Error> {
|
||||
// let msg = from_slice::<MtxApplyMsg>(&data).or(Err(err_msg("invalid params")))?;
|
||||
// match msg.params {
|
||||
// MtxApplyParams::ConstructApply { construct_id, mtx } =>
|
||||
// mtx::apply(tx, mtx, construct_id, account.id)?,
|
||||
// _ => unimplemented!(),
|
||||
// };
|
||||
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
struct ConstructDeleteMsg {
|
||||
method: String,
|
||||
params: ConstructDeleteParams,
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
pub struct ConstructDeleteParams {
|
||||
pub id: Uuid,
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
struct GameStateMsg {
|
||||
method: String,
|
||||
params: GameStateParams,
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
pub struct GameStateParams {
|
||||
pub id: Uuid,
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
struct GameSkillMsg {
|
||||
method: String,
|
||||
params: GameSkillParams,
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
pub struct GameSkillParams {
|
||||
pub game_id: Uuid,
|
||||
pub construct_id: Uuid,
|
||||
pub target_construct_id: Option<Uuid>,
|
||||
pub skill: Skill,
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
struct AccountCreateMsg {
|
||||
method: String,
|
||||
params: AccountCreateParams,
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
pub struct AccountCreateParams {
|
||||
pub name: String,
|
||||
pub password: String,
|
||||
pub code: String,
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
struct AccountLoginMsg {
|
||||
method: String,
|
||||
params: AccountLoginParams,
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
pub struct AccountLoginParams {
|
||||
pub name: String,
|
||||
pub password: String,
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
struct AccountConstructsMsg {
|
||||
method: String,
|
||||
params: (),
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
struct InstanceLobbyMsg {
|
||||
method: String,
|
||||
params: InstanceLobbyParams,
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
pub struct InstanceLobbyParams {
|
||||
pub construct_ids: Vec<Uuid>,
|
||||
pub name: String,
|
||||
pub pve: bool,
|
||||
pub password: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
struct InstanceJoinMsg {
|
||||
method: String,
|
||||
params: InstanceJoinParams,
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
pub struct InstanceJoinParams {
|
||||
pub instance_id: Uuid,
|
||||
pub construct_ids: Vec<Uuid>,
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
struct InstanceReadyMsg {
|
||||
method: String,
|
||||
params: InstanceReadyParams,
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
pub struct InstanceReadyParams {
|
||||
pub instance_id: Uuid,
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
struct InstanceStateMsg {
|
||||
method: String,
|
||||
params: InstanceStateParams,
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
pub struct InstanceStateParams {
|
||||
pub instance_id: Uuid,
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
struct MtxApplyMsg {
|
||||
method: String,
|
||||
params: MtxApplyParams,
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
pub struct MtxApplyParams {
|
||||
pub construct_id: Uuid,
|
||||
pub mtx: mtx::MtxVariant,
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
struct VboxAcceptMsg {
|
||||
method: String,
|
||||
params: VboxAcceptParams,
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
pub struct VboxAcceptParams {
|
||||
pub instance_id: Uuid,
|
||||
pub group: usize,
|
||||
pub index: usize,
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
struct VboxDiscardMsg {
|
||||
method: String,
|
||||
params: VboxDiscardParams,
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
pub struct VboxDiscardParams {
|
||||
pub instance_id: Uuid,
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
struct VboxCombineMsg {
|
||||
method: String,
|
||||
params: VboxCombineParams,
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
pub struct VboxCombineParams {
|
||||
pub instance_id: Uuid,
|
||||
pub indices: Vec<usize>,
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
struct VboxApplyMsg {
|
||||
method: String,
|
||||
params: VboxApplyParams,
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
pub struct VboxApplyParams {
|
||||
pub instance_id: Uuid,
|
||||
pub construct_id: Uuid,
|
||||
pub index: usize,
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
struct VboxUnequipMsg {
|
||||
method: String,
|
||||
params: VboxUnequipParams,
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
pub struct VboxUnequipParams {
|
||||
pub instance_id: Uuid,
|
||||
pub construct_id: Uuid,
|
||||
pub target: Item,
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
struct VboxReclaimMsg {
|
||||
method: String,
|
||||
params: VboxReclaimParams,
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
pub struct VboxReclaimParams {
|
||||
pub instance_id: Uuid,
|
||||
pub index: usize,
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
struct DevResolveMsg {
|
||||
method: String,
|
||||
params: DevResolveParams,
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
pub struct DevResolveParams {
|
||||
pub a: Uuid,
|
||||
pub b: Uuid,
|
||||
pub skill: Skill,
|
||||
}
|
||||
|
||||
// #[cfg(test)]
|
||||
// mod tests {
|
||||
// use super::*;
|
||||
// use serde_cbor::to_vec;
|
||||
// #[test]
|
||||
// fn rpc_parse() {
|
||||
// let rpc = Rpc {};
|
||||
// let msg = GenerateMsg { method: "construct_generate".to_string(), params: GenerateParams { level: 64 } };
|
||||
// let v = to_vec(&msg).unwrap();
|
||||
// let received = rpc.receive(Message::Binary(v));
|
||||
// }
|
||||
// }
|
||||
// Ok(RpcResult::AccountConstructs(account_constructs(tx, &account)?))
|
||||
// }
|
||||
|
||||
@ -1,3 +1,5 @@
|
||||
use uuid::Uuid;
|
||||
|
||||
use std::iter;
|
||||
|
||||
// reclaims
|
||||
@ -11,7 +13,7 @@ use failure::Error;
|
||||
use failure::err_msg;
|
||||
|
||||
use account::Account;
|
||||
use rpc::{VboxAcceptParams, VboxDiscardParams, VboxCombineParams, VboxApplyParams, VboxReclaimParams, VboxUnequipParams};
|
||||
|
||||
use instance::{Instance, instance_get, instance_update};
|
||||
use construct::{Colours};
|
||||
|
||||
@ -155,39 +157,39 @@ impl Vbox {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn vbox_discard(params: VboxDiscardParams, tx: &mut Transaction, account: &Account) -> Result<Instance, Error> {
|
||||
let instance = instance_get(tx, params.instance_id)?
|
||||
pub fn vbox_discard(tx: &mut Transaction, account: &Account, instance_id: Uuid) -> Result<Instance, Error> {
|
||||
let instance = instance_get(tx, instance_id)?
|
||||
.vbox_discard(account.id)?;
|
||||
return instance_update(tx, instance);
|
||||
}
|
||||
|
||||
pub fn vbox_accept(params: VboxAcceptParams, tx: &mut Transaction, account: &Account) -> Result<Instance, Error> {
|
||||
let instance = instance_get(tx, params.instance_id)?
|
||||
.vbox_accept(account.id, params.group, params.index)?;
|
||||
pub fn vbox_accept(tx: &mut Transaction, account: &Account, instance_id: Uuid, group: usize, index: usize) -> Result<Instance, Error> {
|
||||
let instance = instance_get(tx, instance_id)?
|
||||
.vbox_accept(account.id, group, index)?;
|
||||
return instance_update(tx, instance);
|
||||
}
|
||||
|
||||
pub fn vbox_combine(params: VboxCombineParams, tx: &mut Transaction, account: &Account) -> Result<Instance, Error> {
|
||||
let instance = instance_get(tx, params.instance_id)?
|
||||
.vbox_combine(account.id, params.indices)?;
|
||||
pub fn vbox_combine(tx: &mut Transaction, account: &Account, instance_id: Uuid, indices: Vec<usize>) -> Result<Instance, Error> {
|
||||
let instance = instance_get(tx, instance_id)?
|
||||
.vbox_combine(account.id, indices)?;
|
||||
return instance_update(tx, instance);
|
||||
}
|
||||
|
||||
pub fn vbox_reclaim(params: VboxReclaimParams, tx: &mut Transaction, account: &Account) -> Result<Instance, Error> {
|
||||
let instance = instance_get(tx, params.instance_id)?
|
||||
.vbox_reclaim(account.id, params.index)?;
|
||||
pub fn vbox_reclaim(tx: &mut Transaction, account: &Account, instance_id: Uuid, index: usize) -> Result<Instance, Error> {
|
||||
let instance = instance_get(tx, instance_id)?
|
||||
.vbox_reclaim(account.id, index)?;
|
||||
return instance_update(tx, instance);
|
||||
}
|
||||
|
||||
pub fn vbox_apply(params: VboxApplyParams, tx: &mut Transaction, account: &Account) -> Result<Instance, Error> {
|
||||
let instance = instance_get(tx, params.instance_id)?
|
||||
.vbox_apply(account.id, params.index, params.construct_id)?;
|
||||
pub fn vbox_apply(tx: &mut Transaction, account: &Account, instance_id: Uuid, construct_id: Uuid, index: usize) -> Result<Instance, Error> {
|
||||
let instance = instance_get(tx, instance_id)?
|
||||
.vbox_apply(account.id, index, construct_id)?;
|
||||
return instance_update(tx, instance);
|
||||
}
|
||||
|
||||
pub fn vbox_unequip(params: VboxUnequipParams, tx: &mut Transaction, account: &Account) -> Result<Instance, Error> {
|
||||
let instance = instance_get(tx, params.instance_id)?
|
||||
.vbox_unequip(account.id, params.target, params.construct_id)?;
|
||||
pub fn vbox_unequip(tx: &mut Transaction, account: &Account, instance_id: Uuid, construct_id: Uuid, target: Item) -> Result<Instance, Error> {
|
||||
let instance = instance_get(tx, instance_id)?
|
||||
.vbox_unequip(account.id, target, construct_id)?;
|
||||
return instance_update(tx, instance);
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user