Skip to content

Commit

Permalink
add match tests + scarb fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
dubzn committed Dec 18, 2023
1 parent 562e572 commit 68f9039
Show file tree
Hide file tree
Showing 17 changed files with 922 additions and 81 deletions.
652 changes: 626 additions & 26 deletions src/constants.cairo

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

19 changes: 16 additions & 3 deletions src/models/entities/map.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,15 @@ fn create_tiles(map: Map) -> Array<Tile> {
}
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;
Expand All @@ -87,7 +95,12 @@ fn create_tiles_by_array(map: Map, array: Array<felt252>) -> Array<Tile> {
// 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
Expand Down Expand Up @@ -120,4 +133,4 @@ impl Felt252TryIntoTerrainType of TryInto<felt252, TerrainType> {
Option::None(())
}
}
}
}
2 changes: 1 addition & 1 deletion src/models/entities/skill.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
3 changes: 2 additions & 1 deletion src/models/states/character_state.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ struct ActionState {
match_id: u32,
#[key]
character_id: u32,
#[key]
player: felt252,
action: bool,
movement: bool,
}
Expand All @@ -17,7 +19,6 @@ struct CharacterState {
#[key]
player: felt252,
turn: u32,
action_state: ActionState,
remain_hp: u128,
remain_mp: u128,
x: u128,
Expand Down
27 changes: 22 additions & 5 deletions src/systems/action_system.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}

Expand All @@ -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);
Expand All @@ -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(
Expand Down Expand Up @@ -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));
}
}

Expand Down Expand Up @@ -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');

Expand Down
14 changes: 11 additions & 3 deletions src/systems/character_system.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/systems/map_system.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
28 changes: 16 additions & 12 deletions src/systems/match_system.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ trait IMatchSystem<TContractState> {
}

#[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};
Expand Down Expand Up @@ -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 });
}
}

Expand Down
15 changes: 10 additions & 5 deletions src/systems/move_system.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ trait IMoveSystem<TContractState> {
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;
Expand Down Expand Up @@ -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'
);
Expand All @@ -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));
}
}

Expand Down
19 changes: 14 additions & 5 deletions src/systems/skill_system.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
30 changes: 21 additions & 9 deletions src/tests/setup.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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) {
Expand All @@ -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)
}
}
}
Loading

0 comments on commit 68f9039

Please sign in to comment.