gardening

This commit is contained in:
ntr 2019-06-01 14:59:01 +10:00
parent e1a26d66d5
commit a321805f54
9 changed files with 59 additions and 233 deletions

View File

@ -310,6 +310,10 @@ CONSTRUCT DAMAGE
opacity: 0;
}
.resolving .targeting {
opacity: 0;
}
/*
COMBAT ANIMATIONS
*/

View File

@ -778,25 +778,25 @@ impl Construct {
return result;
}
pub fn evade(&self, skill: Skill) -> Option<Event> {
if self.evasion.value == 0 {
return None;
}
// pub fn evade(&self, skill: Skill) -> Option<Event> {
// if self.evasion.value == 0 {
// return None;
// }
let mut rng = thread_rng();
let green_life_pct = (self.green_life.value * 100) / self.green_life.value;
let evasion_rating = (self.evasion.value * green_life_pct) / 100;
let roll = rng.gen_range(0, 100);
info!("{:} < {:?}", roll, evasion_rating);
// let mut rng = thread_rng();
// let green_life_pct = (self.green_life.value * 100) / self.green_life.value;
// let evasion_rating = (self.evasion.value * green_life_pct) / 100;
// let roll = rng.gen_range(0, 100);
// info!("{:} < {:?}", roll, evasion_rating);
match roll < evasion_rating {
true => Some(Event::Evasion {
skill,
evasion_rating: evasion_rating,
}),
false => None,
}
}
// match roll < evasion_rating {
// true => Some(Event::Evasion {
// skill,
// evasion_rating: evasion_rating,
// }),
// false => None,
// }
// }
}
pub fn construct_get(tx: &mut Transaction, id: Uuid, account_id: Uuid) -> Result<Construct, Error> {

View File

@ -16,7 +16,7 @@ use rpc::{GameStateParams, GameSkillParams};
use construct::{Construct};
use skill::{Skill, Effect, Cast, Resolution, Event, resolution_steps};
use player::{Player};
use instance::{instance_game_finished, global_game_finished};
use instance::{instance_game_finished};
#[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)]
pub enum Phase {
@ -73,10 +73,6 @@ impl Game {
self
}
pub fn joinable(&self) -> bool {
self.can_start()
}
pub fn player_add(&mut self, mut player: Player) -> Result<&mut Game, Error> {
if self.players.len() == self.player_num {
return Err(err_msg("maximum number of players"));
@ -115,17 +111,6 @@ impl Game {
}
}
pub fn construct_by_id_take(&mut self, id: Uuid) -> Construct {
match self.players.iter_mut().find(|t| t.constructs.iter().any(|c| c.id == id)) {
Some(player) => {
let i = player.constructs.iter().position(|c| c.id == id).unwrap();
player.constructs.remove(i)
}
None => panic!("id not in game {:}", id),
}
}
fn all_constructs(&self) -> Vec<Construct> {
self.players.clone()
.into_iter()
@ -495,7 +480,7 @@ impl Game {
}
fn log_resolution(&mut self, speed: u64, resolution: &Resolution) -> &mut Game {
let Resolution { source, target, event, stages } = resolution;
let Resolution { source, target, event, stages: _ } = resolution;
match event {
Event::Ko { skill: _ }=>
self.log.push(format!("{:} KO!", target.name)),
@ -973,11 +958,11 @@ mod tests {
let x_player_id = Uuid::new_v4();
x.account = x_player_id;
let mut x_player = Player::new(x_player_id, &"ntr".to_string(), vec![x]);
let x_player = Player::new(x_player_id, &"ntr".to_string(), vec![x]);
let y_player_id = Uuid::new_v4();
y.account = y_player_id;
let mut y_player = Player::new(y_player_id, &"mash".to_string(), vec![y]);
let y_player = Player::new(y_player_id, &"mash".to_string(), vec![y]);
game
.player_add(x_player).unwrap()

View File

@ -15,7 +15,7 @@ use chrono::Duration;
use rpc::{InstanceLobbyParams, InstanceJoinParams, InstanceReadyParams, InstanceStateParams};
use account::Account;
use player::{Player, player_create, player_get, player_global_update};
use player::{Player, player_create};
use construct::{Construct, construct_get};
use mob::{instance_mobs};
use game::{Game, Phase, game_get, game_write};
@ -688,27 +688,6 @@ pub fn instance_join(params: InstanceJoinParams, tx: &mut Transaction, account:
instance_update(tx, instance)
}
// pub fn instance_ready_global(tx: &mut Transaction, _account: &Account, player: Player) -> Result<Game, Error> {
// // get the game
// let game = match game_global_get(tx) {
// Ok(g) => {
// info!("received global game {:?}", g.id);
// // if there is one try to join
// match game_instance_join(tx, player.clone(), g.id) {
// Ok(g) => g,
// // if fails make a new one
// Err(_e) => game_instance_new(tx, vec![player], Uuid::new_v4(), Uuid::nil())?,
// }
// },
// // if not found make a new one
// Err(_) => game_instance_new(tx, vec![player], Uuid::new_v4(), Uuid::nil())?,
// };
// // set the current game
// game_global_set(tx, &game)?;
// Ok(game)
// }
pub fn instance_ready(params: InstanceReadyParams, tx: &mut Transaction, account: &Account) -> Result<Instance, Error> {
let mut instance = instance_get(tx, params.instance_id)?;
let player_id = instance.account_player(account.id)?.id;
@ -735,22 +714,6 @@ pub fn instance_state(params: InstanceStateParams, tx: &mut Transaction, account
Ok(RpcResult::InstanceState(instance))
}
pub fn global_game_finished(tx: &mut Transaction, game: &Game) -> Result<(), Error> {
let winner = game.winner().ok_or(err_msg("game not finished"))?;
for player in game.players.iter() {
let mut player = player_get(tx, player.id, Uuid::nil())?;
match player.id == winner.id {
true => player.add_win(),
false => player.add_loss(),
};
player.vbox.fill();
player_global_update(tx, player, true)?;
}
Ok(())
}
pub fn instance_game_finished(tx: &mut Transaction, game: &Game, instance_id: Uuid) -> Result<(), Error> {
let mut instance = instance_get(tx, instance_id)?;
instance.game_finished(game)?;

View File

@ -19,15 +19,8 @@ static DB_POOL_SIZE: u32 = 20;
pub type Db = PooledConnection<PostgresConnectionManager>;
use rpc::{Rpc};
use util::{startup};
use warden::{warden};
// struct Server {
// client: WebSocket<TcpStream>,
// rpc: Rpc,
// db: Pool<PostgresConnectionManager>,
// }
#[derive(Debug,Clone,Serialize,Deserialize)]
struct RpcErrorResponse {
err: String
@ -88,10 +81,10 @@ pub fn start() {
let pool = db_connection(database_url);
{
let startup_connection = pool.get().expect("unable to get db connection");
startup(startup_connection).unwrap();
}
// {
// let startup_connection = pool.get().expect("unable to get db connection");
// startup(startup_connection).unwrap();
// }
let server = TcpListener::bind("0.0.0.0:40000").unwrap();

View File

@ -1,19 +1,15 @@
use uuid::Uuid;
use rand::prelude::*;
use serde_cbor::{from_slice, to_vec};
use postgres::transaction::Transaction;
use failure::Error;
use failure::err_msg;
use account::Account;
use construct::{Construct, Colours, construct_get};
use construct::{Construct, Colours};
use vbox::{Vbox};
use item::{Item, ItemEffect};
use rpc::{PlayerConstructsSetParams};
use instance::{Instance};
use skill::{Effect};
const DISCARD_COST: u16 = 5;
@ -291,53 +287,21 @@ impl Player {
.find(|c| c.affected(Effect::Taunt))
}
pub fn set_constructs(&mut self, mut constructs: Vec<Construct>) -> &mut Player {
constructs.sort_unstable_by_key(|c| c.id);
self.constructs = constructs;
self
}
pub fn construct_by_id(&mut self, id: Uuid) -> Option<&mut Construct> {
self.constructs.iter_mut().find(|c| c.id == id)
}
}
pub fn player_get(tx: &mut Transaction, account_id: Uuid, instance_id: Uuid) -> Result<Player, Error> {
let query = "
SELECT *
FROM players
WHERE account = $1
AND instance = $2
FOR UPDATE;
";
let result = tx
.query(query, &[&account_id, &instance_id])?;
let returned = match result.iter().next() {
Some(row) => row,
None => return Err(err_msg("player not found")),
};
// tells from_slice to cast into a construct
let bytes: Vec<u8> = returned.get("data");
let data = from_slice::<Player>(&bytes)?;
return Ok(data);
}
pub fn player_create(tx: &mut Transaction, player: Player, instance: Uuid, account: &Account) -> Result<Player, Error> {
let player_bytes = to_vec(&player)?;
let query = "
INSERT INTO players (id, instance, account, data)
VALUES ($1, $2, $3, $4)
INSERT INTO players (id, instance, account)
VALUES ($1, $2, $3)
RETURNING id, account;
";
let result = tx
.query(query, &[&Uuid::new_v4(), &instance, &account.id, &player_bytes])?;
.query(query, &[&Uuid::new_v4(), &instance, &account.id])?;
let _returned = result.iter().next().expect("no row written");
@ -346,71 +310,6 @@ pub fn player_create(tx: &mut Transaction, player: Player, instance: Uuid, accou
return Ok(player);
}
pub fn player_global_update(tx: &mut Transaction, mut player: Player, ignore_phase: bool) -> Result<Player, Error> {
// sort vbox for niceness
player.vbox.bound.sort_unstable();
let bytes = to_vec(&player)?;
let query = "
UPDATE players
SET data = $1, updated_at = now()
WHERE id = $2
RETURNING id, data;
";
let result = tx
.query(query, &[&bytes, &player.id])?;
result.iter().next().ok_or(format_err!("player {:?} could not be written", player))?;
Ok(player)
}
pub fn player_delete(tx: &mut Transaction, id: Uuid) -> Result<(), Error> {
let query = "
DELETE
FROM players
WHERE id = $1;
";
let result = tx
.execute(query, &[&id])?;
if result != 1 {
return Err(format_err!("unable to delete player {:?}", id));
}
info!("player deleted {:?}", id);
return Ok(());
}
pub fn player_mm_constructs_set(params: PlayerConstructsSetParams, tx: &mut Transaction, account: &Account) -> Result<Instance, Error> {
if params.construct_ids.len() != 3 {
return Err(err_msg("player size is 3"));
}
let constructs = params.construct_ids
.iter()
.map(|id| construct_get(tx, *id, account.id))
.collect::<Result<Vec<Construct>, Error>>()?;
let player = match player_get(tx, account.id, Uuid::nil()) {
Ok(mut p) => {
p.constructs = constructs;
p.vbox = Vbox::new();
player_global_update(tx, p, false)?
},
Err(_) => {
player_create(tx, Player::new(account.id, &account.name, constructs), Uuid::nil(), &account)?
}
};
let instance = Instance::global(player);
Ok(instance)
}
#[cfg(test)]
mod tests {
use mob::instance_mobs;

View File

@ -17,7 +17,7 @@ use game::{Game, game_state, game_skill, game_ready};
use account::{Account, account_create, account_login, account_from_token, account_constructs, account_instances};
use skill::{Skill};
use spec::{Spec};
use player::{Score, player_mm_constructs_set};
use player::{Score};
use instance::{Instance, instance_state, instance_new, instance_ready, instance_join};
use vbox::{vbox_accept, vbox_apply, vbox_discard, vbox_combine, vbox_reclaim, vbox_unequip};
use item::{Item, ItemInfoCtr, item_info};
@ -53,6 +53,7 @@ impl Rpc {
match v.method.as_ref() {
"account_create" => (),
"account_login" => (),
"item_info" => (),
_ => match account {
Some(_) => (),
None => return Err(err_msg("auth required")),
@ -62,14 +63,14 @@ impl Rpc {
// now we have the method name
// match on that to determine what fn to call
let response = match v.method.as_ref() {
// no auth methods
// NO AUTH
"account_create" => Rpc::account_create(data, &mut tx, client),
"account_login" => Rpc::account_login(data, &mut tx, client),
"item_info" => Ok(RpcResponse { method: "item_info".to_string(), params: RpcResult::ItemInfo(item_info()) }),
// auth methods
// AUTH METHODS
"account_constructs" => Rpc::account_constructs(data, &mut tx, account.unwrap(), client),
"account_instances" => Rpc::account_instances(data, &mut tx, account.unwrap(), client),
// "account_zone" => Rpc::account_zone(data, &mut tx, account.unwrap(), client),
"construct_spawn" => Rpc::construct_spawn(data, &mut tx, account.unwrap(), client),
@ -82,7 +83,6 @@ impl Rpc {
"instance_new" => Rpc::instance_new(data, &mut tx, account.unwrap(), client),
"instance_state" => Rpc::instance_state(data, &mut tx, account.unwrap(), client),
"player_mm_constructs_set" => Rpc::player_mm_constructs_set(data, &mut tx, account.unwrap(), client),
"player_vbox_accept" => Rpc::player_vbox_accept(data, &mut tx, account.unwrap(), client),
"player_vbox_apply" => Rpc::player_vbox_apply(data, &mut tx, account.unwrap(), client),
"player_vbox_combine" => Rpc::player_vbox_combine(data, &mut tx, account.unwrap(), client),
@ -90,7 +90,6 @@ impl Rpc {
"player_vbox_reclaim" => Rpc::player_vbox_reclaim(data, &mut tx, account.unwrap(), client),
"player_vbox_unequip" => Rpc::player_vbox_unequip(data, &mut tx, account.unwrap(), client),
"item_info" => Ok(RpcResponse { method: "item_info".to_string(), params: RpcResult::ItemInfo(item_info()) }),
_ => Err(format_err!("unknown method - {:?}", v.method)),
};
@ -296,17 +295,6 @@ impl Rpc {
}
}
fn player_mm_constructs_set(data: Vec<u8>, tx: &mut Transaction, account: Account, _client: &mut WebSocket<TcpStream>) -> Result<RpcResponse, Error> {
let msg = from_slice::<PlayerConstructsSetMsg>(&data).or(Err(err_msg("invalid params")))?;
let response = RpcResponse {
method: "instance_state".to_string(),
params: RpcResult::InstanceState(player_mm_constructs_set(msg.params, tx, &account)?)
};
return Ok(response);
}
fn player_vbox_accept(data: Vec<u8>, tx: &mut Transaction, account: Account, _client: &mut WebSocket<TcpStream>) -> Result<RpcResponse, Error> {
let msg = from_slice::<VboxAcceptMsg>(&data).or(Err(err_msg("invalid params")))?;
@ -341,7 +329,7 @@ impl Rpc {
return Ok(response);
}
fn player_vbox_apply(data: Vec<u8>, tx: &mut Transaction, account: Account, client: &mut WebSocket<TcpStream>) -> Result<RpcResponse, Error> {
fn player_vbox_apply(data: Vec<u8>, tx: &mut Transaction, account: Account, _client: &mut WebSocket<TcpStream>) -> Result<RpcResponse, Error> {
let msg = from_slice::<VboxApplyMsg>(&data).or(Err(err_msg("invalid params")))?;
let response = RpcResponse {
@ -363,7 +351,7 @@ impl Rpc {
return Ok(response);
}
fn player_vbox_unequip(data: Vec<u8>, tx: &mut Transaction, account: Account, client: &mut WebSocket<TcpStream>) -> Result<RpcResponse, Error> {
fn player_vbox_unequip(data: Vec<u8>, tx: &mut Transaction, account: Account, _client: &mut WebSocket<TcpStream>) -> Result<RpcResponse, Error> {
let msg = from_slice::<VboxUnequipMsg>(&data).or(Err(err_msg("invalid params")))?;
let response = RpcResponse {

View File

@ -1372,9 +1372,7 @@ impl Skill {
Skill::ClutchIII |
Skill::ParryI |
Skill::ParryII |
Skill::ParryIII |
Skill::Block |
Skill::ParryI => true,
Skill::ParryIII => true,
_ => false,
}

View File

@ -1,24 +1,20 @@
use net::Db;
// use net::Db;
// Db Commons
use failure::Error;
// use failure::Error;
// use game::{game_global_startup};
// pub fn startup(db: Db) -> Result<(), Error> {
// let tx = db.transaction()?;
pub fn startup(db: Db) -> Result<(), Error> {
let mut tx = db.transaction()?;
// info!("running startup fns");
info!("running startup fns");
// game_global_startup(&mut tx)?;
match tx.commit() {
Ok(_) => {
info!("startup processes completed");
Ok(())
},
Err(e) => Err(format_err!("failed to commit startup tx {:?}", e)),
}
}
// match tx.commit() {
// Ok(_) => {
// info!("startup processes completed");
// Ok(())
// },
// Err(e) => Err(format_err!("failed to commit startup tx {:?}", e)),
// }
// }
pub trait IntPct {
fn pct(self, pct: u64) -> u64;