Skip to content

Commit

Permalink
Rework ArenaRun;
Browse files Browse the repository at this point in the history
Fix unit hover;
  • Loading branch information
makscee committed Apr 23, 2024
1 parent ce6f20b commit a043958
Show file tree
Hide file tree
Showing 22 changed files with 118 additions and 439 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ bevy_asset_loader = { version = "0.20.0", features = [
bevy_common_assets = { version = "0.10.0", features = ["ron"] }
bevy_mod_picking = { version = "0.18.2", default-features = false, features = [
"backend_raycast",
"backend_bevy_ui",
# "backend_bevy_ui",
"selection",
] }
bevy_egui = { version = "0.27.0", features = ["serde"] }
Expand Down
4 changes: 2 additions & 2 deletions assets/ron/unit.rep.ron
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
shape_type: Line(thickness: Float(0.7)),
padding: Float(0.2),
),
mapping: {Offset: Vec2(1.0, -1.0)},
mapping: {Offset: Vec2(0.9, -0.9)},
children: [
(
material: Text(
Expand All @@ -31,7 +31,7 @@
shape_type: Line(thickness: Float(0.7)),
padding: Float(0.2),
),
mapping: {Offset: Vec2(-1.0, -1.0)},
mapping: {Offset: Vec2(-0.9, -0.9)},
children: [
(
material: Text(text: StringInt(OwnerState(Atk)), size: Float(0.6), color: Hex("#FBC02D")),
Expand Down
2 changes: 1 addition & 1 deletion server/src/arena_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub struct ArenaPool {
#[autoinc]
pub id: u64,
pub owner: u64,
pub round: u8,
pub round: u32,
pub team: Vec<TableUnit>,
}

Expand Down
70 changes: 43 additions & 27 deletions server/src/arena_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@ pub struct ArenaRun {
#[autoinc]
id: u64,
user_id: u64,
wins: u8,
loses: u8,
enemies: Vec<u64>,
state: GameState,
battles: Vec<ArenaBattle>,
round: u32,
state: RunState,
active: bool,
last_updated: Timestamp,
}
#[derive(SpacetimeType)]
pub struct GameState {
pub struct ArenaBattle {
enemy: u64,
result: Option<bool>,
}
#[derive(SpacetimeType)]
pub struct RunState {
g: i64,
team: Vec<TeamUnit>,
case: Vec<ShopOffer>,
Expand All @@ -50,7 +54,10 @@ fn run_start(ctx: ReducerContext) -> Result<(), String> {
}
let mut run = ArenaRun::new(user.id);
if let Some(enemy) = ArenaPool::filter_by_round(&0).choose(&mut thread_rng()) {
run.enemies.push(enemy.id);
run.battles.push(ArenaBattle {
enemy: enemy.id,
result: None,
});
}
run.fill_case();
ArenaRun::insert(run).unwrap();
Expand All @@ -69,30 +76,26 @@ fn run_submit_result(ctx: ReducerContext, win: bool) -> Result<(), String> {
ArenaPool::insert(ArenaPool {
id: 0,
owner: user_id,
round: run.round(),
round: run.round,
team,
})?;
if win {
run.wins += 1;
if let Some(last_battle) = run.battles.get_mut(run.round as usize) {
last_battle.result = Some(win);
} else {
run.loses += 1;
run.active = false;
}
if run.loses > 2 {
run.round += 1;

if run.loses() > 2 {
run.active = false;
} else {
let round = run.round();
if (round as usize) > run.enemies.len() {
run.active = false;
} else if let Some(enemy) = ArenaPool::filter_by_round(&round)
// .filter(|e| e.owner != user_id)
.choose(&mut thread_rng())
{
run.enemies.push(enemy.id);
if let Some(enemy) = ArenaBattle::next(&run) {
run.battles.push(enemy);
}
}
if run.active {
let settings = GlobalSettings::get();
run.change_g((settings.g_per_round_min + run.round() as i64).min(settings.g_per_round_max));
run.change_g((settings.g_per_round_min + run.round as i64).min(settings.g_per_round_max));
run.state.case.clear();
run.fill_case();
}
Expand Down Expand Up @@ -217,12 +220,11 @@ impl ArenaRun {
Self {
user_id,
active: true,
wins: 0,
loses: 0,
last_updated: Timestamp::now(),
id: 0,
enemies: Vec::default(),
state: GameState {
battles: Vec::default(),
round: 0,
state: RunState {
g: GlobalSettings::get().g_per_round_min,
team: Vec::default(),
case: Vec::default(),
Expand All @@ -231,8 +233,11 @@ impl ArenaRun {
}
}

fn round(&self) -> u8 {
self.wins
fn loses(&self) -> u32 {
self.battles
.iter()
.filter(|b| b.result.is_some_and(|r| !r))
.count() as u32
}

fn get_by_identity(identity: &Identity) -> Result<(u64, Self), String> {
Expand Down Expand Up @@ -261,7 +266,7 @@ impl ArenaRun {
fn fill_case(&mut self) {
let settings = GlobalSettings::get();
let slots = (settings.shop_slots_min
+ (self.round() as f32 * settings.shop_slots_per_round) as u32)
+ (self.round as f32 * settings.shop_slots_per_round) as u32)
.min(settings.shop_slots_max);
let team_houses: HashSet<String> = HashSet::from_iter(
self.state
Expand Down Expand Up @@ -352,3 +357,14 @@ impl ArenaRun {
Self::update_by_id(&self.id.clone(), self);
}
}

impl ArenaBattle {
fn next(run: &ArenaRun) -> Option<Self> {
ArenaPool::filter_by_round(&run.round)
.choose(&mut thread_rng())
.map(|a| ArenaBattle {
enemy: a.id,
result: None,
})
}
}
19 changes: 19 additions & 0 deletions src/module_bindings/arena_battle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE
// WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD.

#[allow(unused)]
use spacetimedb_sdk::{
anyhow::{anyhow, Result},
identity::Identity,
reducer::{Reducer, ReducerCallbackId, Status},
sats::{de::Deserialize, ser::Serialize},
spacetimedb_lib,
table::{TableIter, TableType, TableWithPrimaryKey},
Address,
};

#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)]
pub struct ArenaBattle {
pub enemy: u64,
pub result: Option<bool>,
}
4 changes: 2 additions & 2 deletions src/module_bindings/arena_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use spacetimedb_sdk::{
pub struct ArenaPool {
pub id: u64,
pub owner: u64,
pub round: u8,
pub round: u32,
pub team: Vec<TableUnit>,
}

Expand All @@ -43,7 +43,7 @@ impl ArenaPool {
Self::filter(|row| row.owner == owner)
}
#[allow(unused)]
pub fn filter_by_round(round: u8) -> TableIter<Self> {
pub fn filter_by_round(round: u32) -> TableIter<Self> {
Self::filter(|row| row.round == round)
}
#[allow(unused)]
Expand Down
24 changes: 10 additions & 14 deletions src/module_bindings/arena_run.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE
// WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD.

use super::game_state::GameState;
use super::arena_battle::ArenaBattle;
use super::run_state::RunState;
#[allow(unused)]
use spacetimedb_sdk::{
anyhow::{anyhow, Result},
Expand All @@ -17,10 +18,9 @@ use spacetimedb_sdk::{
pub struct ArenaRun {
pub id: u64,
pub user_id: u64,
pub wins: u8,
pub loses: u8,
pub enemies: Vec<u64>,
pub state: GameState,
pub battles: Vec<ArenaBattle>,
pub round: u32,
pub state: RunState,
pub active: bool,
pub last_updated: u64,
}
Expand All @@ -47,19 +47,15 @@ impl ArenaRun {
Self::filter(|row| row.user_id == user_id)
}
#[allow(unused)]
pub fn filter_by_wins(wins: u8) -> TableIter<Self> {
Self::filter(|row| row.wins == wins)
pub fn filter_by_battles(battles: Vec<ArenaBattle>) -> TableIter<Self> {
Self::filter(|row| row.battles == battles)
}
#[allow(unused)]
pub fn filter_by_loses(loses: u8) -> TableIter<Self> {
Self::filter(|row| row.loses == loses)
pub fn filter_by_round(round: u32) -> TableIter<Self> {
Self::filter(|row| row.round == round)
}
#[allow(unused)]
pub fn filter_by_enemies(enemies: Vec<u64>) -> TableIter<Self> {
Self::filter(|row| row.enemies == enemies)
}
#[allow(unused)]
pub fn filter_by_state(state: GameState) -> TableIter<Self> {
pub fn filter_by_state(state: RunState) -> TableIter<Self> {
Self::filter(|row| row.state == state)
}
#[allow(unused)]
Expand Down
6 changes: 4 additions & 2 deletions src/module_bindings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ use spacetimedb_sdk::{
use std::sync::Arc;

pub mod ability;
pub mod arena_battle;
pub mod arena_pool;
pub mod arena_run;
pub mod game_state;
pub mod give_right_reducer;
pub mod global_data;
pub mod global_settings;
Expand All @@ -40,6 +40,7 @@ pub mod run_reroll_reducer;
pub mod run_sell_reducer;
pub mod run_stack_reducer;
pub mod run_start_reducer;
pub mod run_state;
pub mod run_submit_result_reducer;
pub mod run_team_reorder_reducer;
pub mod set_name_reducer;
Expand All @@ -59,9 +60,9 @@ pub mod user_right;
pub mod vfx;

pub use ability::*;
pub use arena_battle::*;
pub use arena_pool::*;
pub use arena_run::*;
pub use game_state::*;
pub use give_right_reducer::*;
pub use global_data::*;
pub use global_settings::*;
Expand All @@ -78,6 +79,7 @@ pub use run_reroll_reducer::*;
pub use run_sell_reducer::*;
pub use run_stack_reducer::*;
pub use run_start_reducer::*;
pub use run_state::*;
pub use run_submit_result_reducer::*;
pub use run_team_reorder_reducer::*;
pub use set_name_reducer::*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use spacetimedb_sdk::{
};

#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)]
pub struct GameState {
pub struct RunState {
pub g: i64,
pub team: Vec<TeamUnit>,
pub case: Vec<ShopOffer>,
Expand Down
53 changes: 0 additions & 53 deletions src/module_bindings/sync_abilities_reducer.rs

This file was deleted.

Loading

0 comments on commit a043958

Please sign in to comment.