From 68f9039940f17ab387a29cae9eac109f8aab8d1e Mon Sep 17 00:00:00 2001 From: dubzn Date: Sun, 17 Dec 2023 22:34:11 -0300 Subject: [PATCH] add match tests + scarb fmt --- src/constants.cairo | 652 +++++++++++++++++++++++- src/lib.cairo | 8 +- src/models/entities/map.cairo | 19 +- src/models/entities/skill.cairo | 2 +- src/models/states/character_state.cairo | 3 +- src/systems/action_system.cairo | 27 +- src/systems/character_system.cairo | 14 +- src/systems/map_system.cairo | 2 +- src/systems/match_system.cairo | 28 +- src/systems/move_system.cairo | 15 +- src/systems/skill_system.cairo | 19 +- src/tests/setup.cairo | 30 +- src/tests/test_action_system.cairo | 80 +++ src/tests/test_character_system.cairo | 2 +- src/tests/test_map_system.cairo | 6 +- src/tests/test_match_system.cairo | 85 +++ src/tests/test_skill_system.cairo | 11 +- 17 files changed, 922 insertions(+), 81 deletions(-) create mode 100644 src/tests/test_action_system.cairo create mode 100644 src/tests/test_match_system.cairo diff --git a/src/constants.cairo b/src/constants.cairo index e001edc..c0ced97 100644 --- a/src/constants.cairo +++ b/src/constants.cairo @@ -1,30 +1,630 @@ fn MAP_1() -> Array { let mut map = array![ - 'G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G', - 'G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G', - 'G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G', - 'G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G', - 'G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G', - 'G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G', - 'G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G', - 'G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G', - 'G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G', - 'G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G', - 'G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G', - 'G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G', - 'G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G', - 'G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G', - 'G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G', - 'G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G', - 'G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G', - 'G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G', - 'G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G', - 'G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G', - 'G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G', - 'G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G', - 'G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G', - 'G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G', - 'G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G','G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', + 'G', ]; map -} \ No newline at end of file +} diff --git a/src/lib.cairo b/src/lib.cairo index d11d5c3..0aed5e9 100644 --- a/src/lib.cairo +++ b/src/lib.cairo @@ -25,11 +25,13 @@ mod systems { mod map_system; } + #[cfg(test)] mod tests { mod setup; - mod test_character_system; - mod test_skill_system; - mod test_map_system; + // mod test_character_system; + // mod test_skill_system; + // mod test_map_system; + mod test_match_system; } diff --git a/src/models/entities/map.cairo b/src/models/entities/map.cairo index d8a6676..a6d3e6d 100644 --- a/src/models/entities/map.cairo +++ b/src/models/entities/map.cairo @@ -64,7 +64,15 @@ fn create_tiles(map: Map) -> Array { } let index = (y * map.width) + x; // TODO: for now, all tiles are walkeables - tiles.append(Tile { map_id: map.id, id: index.try_into().unwrap(), walkable: true, terrain_type: TerrainType::Grass.into() }); + tiles + .append( + Tile { + map_id: map.id, + id: index.try_into().unwrap(), + walkable: true, + terrain_type: TerrainType::Grass.into() + } + ); x += 1; if x == map.width { y += 1; @@ -87,7 +95,12 @@ fn create_tiles_by_array(map: Map, array: Array) -> Array { // TODO: for now, all tiles are walkeables let walkable = true; - tiles.append(Tile { map_id: map.id, id: index.into(), walkable, terrain_type: terrain_type.into() }); + tiles + .append( + Tile { + map_id: map.id, id: index.into(), walkable, terrain_type: terrain_type.into() + } + ); index += 1; }; tiles @@ -120,4 +133,4 @@ impl Felt252TryIntoTerrainType of TryInto { Option::None(()) } } -} \ No newline at end of file +} diff --git a/src/models/entities/skill.cairo b/src/models/entities/skill.cairo index 02d8701..ca1453a 100644 --- a/src/models/entities/skill.cairo +++ b/src/models/entities/skill.cairo @@ -27,7 +27,7 @@ trait SkillTrait { } impl SkillImpl of SkillTrait { - fn new(skill_type: SkillType, character_id: u32, level: u8) -> Skill { + fn new(skill_type: SkillType, character_id: u32, level: u8) -> Skill { match skill_type { SkillType::MeeleAttack => create_meele(character_id, skill_type.into(), level), SkillType::RangeAttack => create_range(character_id, skill_type.into(), level), diff --git a/src/models/states/character_state.cairo b/src/models/states/character_state.cairo index 64a7486..abb4f68 100644 --- a/src/models/states/character_state.cairo +++ b/src/models/states/character_state.cairo @@ -4,6 +4,8 @@ struct ActionState { match_id: u32, #[key] character_id: u32, + #[key] + player: felt252, action: bool, movement: bool, } @@ -17,7 +19,6 @@ struct CharacterState { #[key] player: felt252, turn: u32, - action_state: ActionState, remain_hp: u128, remain_mp: u128, x: u128, diff --git a/src/systems/action_system.cairo b/src/systems/action_system.cairo index 60f10e5..490bdc6 100644 --- a/src/systems/action_system.cairo +++ b/src/systems/action_system.cairo @@ -24,11 +24,13 @@ mod action_system { Skill, SkillType, SkillTypeIntoU8, U8TryIntoSkillType }; use starkane::models::states::match_state::MatchState; - use starkane::models::states::character_state::CharacterState; + use starkane::models::states::character_state::{CharacterState, ActionState}; use starkane::store::{Store, StoreTrait}; use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait}; + use debug::PrintTrait; + #[storage] struct Storage {} @@ -51,6 +53,10 @@ mod action_system { let match_state = store.get_match_state(match_id); + let action_key = (match_id, player_character_id, player); + let last_action_state = get!(world, action_key, (ActionState)); + assert(!last_action_state.action, 'already do action in this turn'); + let player_character = store.get_character(player_character_id); let mut player_character_state = store .get_character_state(match_state, player_character_id, player); @@ -63,7 +69,10 @@ mod action_system { let skill = store.get_skill(player_character_id, skill_id, level); // fijarse que tenga el skill el que ataca + 'antes skilltpye'.print(); + skill.skill_type.print(); let skill_type: SkillType = skill.skill_type.try_into().unwrap(); + 'me re pego'.print(); match skill_type { SkillType::MeeleAttack => attack( @@ -101,8 +110,15 @@ mod action_system { } // character can do the action, so we have to save that - player_character_state.action_state.action = true; - store.set_character_state(player_character_state); + + let action_state = ActionState { + match_id, + character_id: player_character_id, + player, + action: true, + movement: last_action_state.movement + }; + set!(world, (action_state)); } } @@ -142,13 +158,14 @@ mod action_system { skill: Skill, receiver: Character, receiver_state: CharacterState - ) { + ) { assert(player_character_state.player == receiver_state.player, 'cannot heal an enemy'); assert(player_character_state.remain_mp >= skill.mp_cost, 'out of mana to cast that skill'); assert(receiver_state.remain_hp > 0, 'character already dead'); let distance_to = distance( - (player_character_state.x, player_character_state.y), (receiver_state.x, receiver_state.y) + (player_character_state.x, player_character_state.y), + (receiver_state.x, receiver_state.y) ); assert(distance_to <= skill.range, 'character cannot heal that far'); diff --git a/src/systems/character_system.cairo b/src/systems/character_system.cairo index 3b38564..f043290 100644 --- a/src/systems/character_system.cairo +++ b/src/systems/character_system.cairo @@ -47,14 +47,22 @@ mod character_system { let mut store: Store = StoreTrait::new(world); assert(owner.is_non_zero(), 'owner cannot be zero'); - assert(store.get_character(character_type.into()).character_type != 0, 'character id doesnt exists'); + assert( + store.get_character(character_type.into()).character_type != 0, + 'character id doesnt exists' + ); // TODO: sending character_type as character_id - let character_progress = store.get_character_player_progress(owner, character_type.into()); + let character_progress = store + .get_character_player_progress(owner, character_type.into()); assert(!character_progress.owned, 'ERR: character already owned'); let character_player_progress = CharacterPlayerProgress { - owner: owner, character_id: character_type.into(), skin_id: skin_id, owned: true, level: 1 + owner: owner, + character_id: character_type.into(), + skin_id: skin_id, + owned: true, + level: 1 }; store.set_character_player_progress(character_player_progress); } diff --git a/src/systems/map_system.cairo b/src/systems/map_system.cairo index 68f796f..0491a01 100644 --- a/src/systems/map_system.cairo +++ b/src/systems/map_system.cairo @@ -25,7 +25,7 @@ mod map_system { let (map, tiles) = MapTrait::new(map_id); store.set_map(map); - + let mut idx = 0; loop { if idx == tiles.len() { diff --git a/src/systems/match_system.cairo b/src/systems/match_system.cairo index c9948ac..b61dbae 100644 --- a/src/systems/match_system.cairo +++ b/src/systems/match_system.cairo @@ -15,7 +15,7 @@ trait IMatchSystem { } #[starknet::contract] -mod actions { +mod match_system { use super::{IMatchSystem, PlayerCharacter}; use starkane::models::data::starkane::{MatchIndex, MATCH_IDX_KEY}; use starkane::models::entities::map::{Map, MapTrait}; @@ -67,31 +67,35 @@ mod actions { let p: PlayerCharacter = *players_characters[i]; let (x, y) = obtain_position(player_index(p.player, players), players_len, i); let character = store.get_character(p.character_id); + set!( world, CharacterState { match_id: match_index, character_id: character.character_id, - turn: 0, player: p.player, - action_state: ActionState { - match_id: match_index, - character_id: character.character_id, - action: false, - movement: false - }, + turn: 0, remain_hp: character.hp, remain_mp: character.mp, x: x, y: y } ); + set!( + world, + ActionState { + match_id: match_index, + player: p.player, + character_id: character.character_id, + action: false, + movement: false + } + ); i += 1; }; - - // TODO: agregar mapa y asignar id - set!(world, (new_match)); - set!(world, MatchIndex { id: MATCH_IDX_KEY, index: match_index + 1 }); + // TODO: agregar mapa y asignar id + // set!(world, (new_match)); + // set!(world, MatchIndex { id: MATCH_IDX_KEY, index: match_index + 1 }); } } diff --git a/src/systems/move_system.cairo b/src/systems/move_system.cairo index 5bc3f63..dc4e47f 100644 --- a/src/systems/move_system.cairo +++ b/src/systems/move_system.cairo @@ -16,7 +16,7 @@ trait IMoveSystem { mod move_system { use super::IMoveSystem; use starkane::models::states::match_state::MatchState; - use starkane::models::states::character_state::CharacterState; + use starkane::models::states::character_state::{CharacterState, ActionState}; use starkane::models::entities::map::{Map, Tile, MapTrait, DEFAULT_MAP_WIDTH}; use starkane::models::entities::character::Character; @@ -47,11 +47,13 @@ mod move_system { let character_progress = store.get_character_player_progress(player, character_id); assert(character_progress.owned, 'ERR: player wrong character_id'); - let mut character_state = store.get_character_state(match_state, character_id, player); - assert(!character_state.action_state.movement, 'already move in this turn'); + let action_key = (match_id, character_id, player); + let last_action_state = get!(world, action_key, (ActionState)); + assert(!last_action_state.movement, 'already move in this turn'); let (to_x, to_y) = position; assert(MapTrait::is_inside((to_x, to_y)), 'position is outside of map'); + let mut character_state = store.get_character_state(match_state, character_id, player); assert( character_state.x != to_x && character_state.y != to_y, 'already in that position' ); @@ -67,8 +69,11 @@ mod move_system { character_state.x = to_x; character_state.y = to_y; - character_state.action_state.movement = true; - store.set_character_state(character_state); + + let action_state = ActionState { + match_id, character_id, player, action: last_action_state.action, movement: true + }; + set!(world, (action_state)); } } diff --git a/src/systems/skill_system.cairo b/src/systems/skill_system.cairo index 2a24d58..eefa2d9 100644 --- a/src/systems/skill_system.cairo +++ b/src/systems/skill_system.cairo @@ -38,19 +38,28 @@ mod skill_system { let mut store: Store = StoreTrait::new(world); // [Skill] MeeleAttack - store.set_skill(SkillTrait::new(SkillType::MeeleAttack, CharacterType::Warrior.into(), 1)); - store.set_skill(SkillTrait::new(SkillType::MeeleAttack, CharacterType::Cleric.into(), 1)); + store + .set_skill( + SkillTrait::new(SkillType::MeeleAttack, CharacterType::Warrior.into(), 1) + ); + store + .set_skill( + SkillTrait::new(SkillType::MeeleAttack, CharacterType::Cleric.into(), 1) + ); store.set_skill(SkillTrait::new(SkillType::MeeleAttack, CharacterType::Pig.into(), 1)); // [Skill] RangeAttack - store.set_skill(SkillTrait::new(SkillType::RangeAttack, CharacterType::Archer.into(), 1)); - + store + .set_skill( + SkillTrait::new(SkillType::RangeAttack, CharacterType::Archer.into(), 1) + ); + // Currently we dont have a Sorcerer // [Skill] Fireball // store.set_skill(SkillTrait::new(SkillType::Fireball, CharacterType::Sorcerer.into(), 1)); // store.set_skill(SkillTrait::new(SkillType::Fireball, CharacterType::Sorcerer.into(), 2)); // store.set_skill(SkillTrait::new(SkillType::Fireball, CharacterType::Sorcerer.into(), 3)); - + // [Skill] Heal store.set_skill(SkillTrait::new(SkillType::Heal, CharacterType::Cleric.into(), 1)); store.set_skill(SkillTrait::new(SkillType::Heal, CharacterType::Cleric.into(), 2)); diff --git a/src/tests/setup.cairo b/src/tests/setup.cairo index 8836d1d..f917f2e 100644 --- a/src/tests/setup.cairo +++ b/src/tests/setup.cairo @@ -16,11 +16,16 @@ mod setup { use starkane::models::entities::character::{character, Character}; use starkane::models::entities::skill::{skill, Skill}; use starkane::models::entities::map::{tile, Tile}; - use starkane::models::data::starkane::{character_player_progress, CharacterPlayerProgress, match_index, MatchIndex}; + use starkane::models::data::starkane::{ + character_player_progress, CharacterPlayerProgress, match_index, MatchIndex + }; use starkane::systems::character_system::{character_system, ICharacterSystemDispatcher}; use starkane::systems::skill_system::{skill_system, ISkillSystemDispatcher}; use starkane::systems::map_system::{map_system, IMapSystemDispatcher}; + use starkane::systems::match_system::{match_system, IMatchSystemDispatcher}; + use starkane::systems::action_system::{action_system, IActionSystemDispatcher}; + use starkane::systems::move_system::{move_system, IMoveSystemDispatcher}; // Constants @@ -33,6 +38,9 @@ mod setup { character_system: ICharacterSystemDispatcher, skill_system: ISkillSystemDispatcher, map_system: IMapSystemDispatcher, + match_system: IMatchSystemDispatcher, + action_system: IActionSystemDispatcher, + move_system: IMoveSystemDispatcher, } fn spawn_game() -> (IWorldDispatcher, Systems) { @@ -52,21 +60,25 @@ mod setup { let character_system_address = deploy_contract( character_system::TEST_CLASS_HASH, array![].span() ); - let skill_system_address = deploy_contract( - skill_system::TEST_CLASS_HASH, array![].span() - ); - let map_system_address = deploy_contract( - map_system::TEST_CLASS_HASH, array![].span() - ); + let skill_system_address = deploy_contract(skill_system::TEST_CLASS_HASH, array![].span()); + let map_system_address = deploy_contract(map_system::TEST_CLASS_HASH, array![].span()); + let match_system_address = deploy_contract(match_system::TEST_CLASS_HASH, array![].span()); + let action_system_address = deploy_contract(action_system::TEST_CLASS_HASH, array![].span()); + let move_system_address = deploy_contract(move_system::TEST_CLASS_HASH, array![].span()); let systems = Systems { - character_system: ICharacterSystemDispatcher { contract_address: character_system_address }, + character_system: ICharacterSystemDispatcher { + contract_address: character_system_address + }, skill_system: ISkillSystemDispatcher { contract_address: skill_system_address }, map_system: IMapSystemDispatcher { contract_address: map_system_address }, + match_system: IMatchSystemDispatcher { contract_address: match_system_address }, + action_system: IActionSystemDispatcher { contract_address: action_system_address }, + move_system: IMoveSystemDispatcher { contract_address: move_system_address }, }; // [Return] set_contract_address(PLAYER()); (world, systems) } -} \ No newline at end of file +} diff --git a/src/tests/test_action_system.cairo b/src/tests/test_action_system.cairo new file mode 100644 index 0000000..eb1cbd9 --- /dev/null +++ b/src/tests/test_action_system.cairo @@ -0,0 +1,80 @@ +// Core imports +use debug::PrintTrait; + +// Dojo imports +use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait}; + +// Internal imports +use starkane::store::{Store, StoreTrait}; +use starkane::systems::character_system::ICharacterSystemDispatcherTrait; +use starkane::systems::action_system::IActionSystemDispatcherTrait; +use starkane::models::entities::character::{Character, CharacterType}; + +use starkane::tests::setup::{setup, setup::Systems, setup::PLAYER}; + +// Constants +const ACCOUNT: felt252 = 'ACCOUNT'; +const SEED: felt252 = 'SEED'; +const NAME: felt252 = 'NAME'; + +#[test] +#[available_gas(1_000_000_000)] +fn test_attack() { + // [Setup] + let (world, systems) = setup::spawn_game(); + let mut store = StoreTrait::new(world); + + // [Create] + systems.character_system.init(world); + systems.skill_system.init(world); + systems.map_system.init(world); + + system.match_system(); + // [Assert] Archer + let archer = store.get_character(1); + assert(archer.character_type == 1, 'archer wrong id'); + assert(archer.hp == 250, 'archer wrong initial hp'); + assert(archer.mp == 100, 'archer wrong initial mp'); + assert(archer.attack == 15, 'archer wrong initial attack'); + assert(archer.defense == 10, 'archer wrong initial defense'); + assert(archer.evasion == 15, 'archer wrong initial evasion'); + assert(archer.crit_chance == 20, 'archer wrong crit_chance'); + assert(archer.crit_rate == 2, 'archer wrong initial crit_rate'); + assert(archer.movement_range == 6, 'archer wrong initial movement'); + + // [Assert] Cleric + let cleric = store.get_character(2); + assert(cleric.character_type == 2, 'cleric wrong id'); + assert(cleric.hp == 200, 'cleric wrong initial hp'); + assert(cleric.mp == 350, 'cleric wrong initial mp'); + assert(cleric.attack == 5, 'cleric wrong initial attack'); + assert(cleric.defense == 20, 'cleric wrong initial defense'); + assert(cleric.evasion == 15, 'cleric wrong initial evasion'); + assert(cleric.crit_chance == 0, 'cleric wrong crit_chance'); + assert(cleric.crit_rate == 0, 'cleric wrong initial crit_rate'); + assert(cleric.movement_range == 4, 'cleric wrong initial movement'); + + // [Assert] Warrior + let warrior = store.get_character(3); + assert(warrior.character_type == 3, 'warrior wrong id'); + assert(warrior.hp == 300, 'warrior wrong initial hp'); + assert(warrior.mp == 100, 'warrior wrong initial mp'); + assert(warrior.attack == 20, 'warrior wrong initial attack'); + assert(warrior.defense == 15, 'warrior wrong initial defense'); + assert(warrior.evasion == 5, 'warrior wrong initial evasion'); + assert(warrior.crit_chance == 10, 'warrior wrong crit_chance'); + assert(warrior.crit_rate == 2, 'warrior wrong initial crit_rate'); + assert(warrior.movement_range == 5, 'warrior wrong initial movement'); + + // [Assert] Pig + let pig = store.get_character(4); + assert(pig.character_type == 4, 'pig wrong id'); + assert(pig.hp == 150, 'pig wrong initial hp'); + assert(pig.mp == 0, 'pig wrong initial mp'); + assert(pig.attack == 25, 'pig wrong initial attack'); + assert(pig.defense == 10, 'pig wrong initial defense'); + assert(pig.evasion == 5, 'pig wrong initial evasion'); + assert(pig.crit_chance == 15, 'pig wrong crit_chance'); + assert(pig.crit_rate == 2, 'pig wrong initial crit_rate'); + assert(pig.movement_range == 4, 'pig wrong initial movement'); +} diff --git a/src/tests/test_character_system.cairo b/src/tests/test_character_system.cairo index ce9dbf8..7d1bd35 100644 --- a/src/tests/test_character_system.cairo +++ b/src/tests/test_character_system.cairo @@ -73,4 +73,4 @@ fn test_initialize_characters() { assert(pig.crit_chance == 15, 'pig wrong crit_chance'); assert(pig.crit_rate == 2, 'pig wrong initial crit_rate'); assert(pig.movement_range == 4, 'pig wrong initial movement'); -} \ No newline at end of file +} diff --git a/src/tests/test_map_system.cairo b/src/tests/test_map_system.cairo index 8e14a7e..bab7e7f 100644 --- a/src/tests/test_map_system.cairo +++ b/src/tests/test_map_system.cairo @@ -7,7 +7,9 @@ use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait}; // Internal imports use starkane::store::{Store, StoreTrait}; -use starkane::models::entities::map::{Map, Tile, TerrainType, DEFAULT_MAP_WIDTH, DEFAULT_MAP_HEIGHT}; +use starkane::models::entities::map::{ + Map, Tile, TerrainType, DEFAULT_MAP_WIDTH, DEFAULT_MAP_HEIGHT +}; use starkane::systems::map_system::IMapSystemDispatcherTrait; use starkane::tests::setup::{setup, setup::Systems, setup::PLAYER}; @@ -46,4 +48,4 @@ fn test_initialize_map() { index += 1; }; -} \ No newline at end of file +} diff --git a/src/tests/test_match_system.cairo b/src/tests/test_match_system.cairo new file mode 100644 index 0000000..3f702a2 --- /dev/null +++ b/src/tests/test_match_system.cairo @@ -0,0 +1,85 @@ +// Core imports +use debug::PrintTrait; + +// Dojo imports +use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait}; + +// Internal imports +use starkane::store::{Store, StoreTrait}; +use starkane::systems::character_system::ICharacterSystemDispatcherTrait; +use starkane::systems::map_system::IMapSystemDispatcherTrait; +use starkane::systems::skill_system::ISkillSystemDispatcherTrait; +use starkane::systems::match_system::{IMatchSystemDispatcherTrait, PlayerCharacter}; +use starkane::systems::action_system::IActionSystemDispatcherTrait; +use starkane::models::entities::character::{Character, CharacterType}; +use starkane::models::entities::skill::{Skill, SkillType}; +use starkane::models::data::starkane::{MatchIndex, MATCH_IDX_KEY}; + + +use starkane::tests::setup::{setup, setup::Systems, setup::PLAYER}; + +// Constants +const ACCOUNT: felt252 = 'ACCOUNT'; +const SEED: felt252 = 'SEED'; +const NAME: felt252 = 'NAME'; + +#[test] +#[available_gas(1_000_000_000)] +fn test_attack() { + // [Setup] + let (world, systems) = setup::spawn_game(); + let mut store = StoreTrait::new(world); + + // [Create] + systems.character_system.init(world); + systems.skill_system.init(world); + systems.map_system.init(world); + + // [Mint] + systems.character_system.mint(world, CharacterType::Warrior, '0x1', 1); + systems.character_system.mint(world, CharacterType::Warrior, '0x2', 1); + + let player_characters = array![ + PlayerCharacter { player: '0x1', character_id: CharacterType::Warrior.into() }, + PlayerCharacter { player: '0x2', character_id: CharacterType::Warrior.into() }, + ]; + + systems.match_system.init(world, player_characters); + let match_idx = store.get_match_index(MATCH_IDX_KEY); + let match_state = store.get_match_state(match_idx.index); + + let mut cs_player_1 = store + .get_character_state(match_state, CharacterType::Warrior.into(), '0x1'); + let mut cs_player_2 = store + .get_character_state(match_state, CharacterType::Warrior.into(), '0x2'); + + cs_player_1.x = 1; + cs_player_1.y = 1; + + cs_player_2.x = 2; + cs_player_2.y = 1; + + store.set_character_state(cs_player_1); + store.set_character_state(cs_player_2); + + // [Attack] + // match_id: u32, + // player: felt252, + // player_character_id: u32, + // skill_id: u32, + // level: u32, + // receiver: felt252, + // receiver_character_id: u32 + systems + .action_system + .action( + world, + match_idx.index, + '0x1', + CharacterType::Warrior.into(), + SkillType::MeeleAttack.into(), + 1, + '0x2', + CharacterType::Warrior.into() + ); +} diff --git a/src/tests/test_skill_system.cairo b/src/tests/test_skill_system.cairo index 14547e5..6d8e52e 100644 --- a/src/tests/test_skill_system.cairo +++ b/src/tests/test_skill_system.cairo @@ -28,7 +28,8 @@ fn test_initialize_skills() { systems.skill_system.init(world); // [Assert] MeeleAttack - let meele_warrior = store.get_skill(SkillType::MeeleAttack.into(), CharacterType::Warrior.into(), 1); + let meele_warrior = store + .get_skill(SkillType::MeeleAttack.into(), CharacterType::Warrior.into(), 1); assert(meele_warrior.character_id == 3, 'meele warrior wrong char_id'); assert(meele_warrior.skill_type == 1, 'meele warrior wrong skill_type'); assert(meele_warrior.level == 1, 'meele warrior wrong level'); @@ -36,7 +37,8 @@ fn test_initialize_skills() { assert(meele_warrior.mp_cost == 0, 'meele warrior wrong mp_cost'); assert(meele_warrior.range == 2, 'meele warrior wrong range'); - let meele_cleric = store.get_skill(SkillType::MeeleAttack.into(), CharacterType::Cleric.into(), 1); + let meele_cleric = store + .get_skill(SkillType::MeeleAttack.into(), CharacterType::Cleric.into(), 1); assert(meele_cleric.character_id == 2, 'meele cleric wrong char_id'); assert(meele_cleric.skill_type == 1, 'meele cleric wrong skill_type'); assert(meele_cleric.level == 1, 'meele cleric wrong level'); @@ -53,7 +55,8 @@ fn test_initialize_skills() { assert(meele_pig.range == 2, 'meele pig wrong range'); // [Assert] RangeAttack - let range_archer = store.get_skill(SkillType::RangeAttack.into(), CharacterType::Archer.into(), 1); + let range_archer = store + .get_skill(SkillType::RangeAttack.into(), CharacterType::Archer.into(), 1); assert(range_archer.character_id == 1, 'range archer wrong char_id'); assert(range_archer.skill_type == 2, 'range archer wrong skill_type'); assert(range_archer.level == 1, 'range archer wrong level'); @@ -91,4 +94,4 @@ fn test_initialize_skills() { assert(cleric_heal.power == 120, 'cleric heal wrong power'); assert(cleric_heal.mp_cost == 100, 'cleric heal wrong mp_cost'); assert(cleric_heal.range == 5, 'cleric heal wrong range'); -} \ No newline at end of file +}