Skip to content

Commit

Permalink
add stadistics check into tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dubzn committed Dec 24, 2023
1 parent c16af42 commit 3ac70d8
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/systems/character_system.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ mod character_system {
owned: true,
level: 1
};

let mut player_stadistics = store.get_player_stadistics(owner);
player_stadistics.characters_owned += 1;

store.set_player_stadistics(player_stadistics);
store.set_character_player_progress(character_player_progress);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/systems/stadistics_system.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ mod stadistics_system {
let match_player: MatchPlayer = (*match_players[i]);
let mut player_stadistics = store.get_player_stadistics(match_player.player);
if match_player.player == match_state.winner {
player_stadistics
.total_score += calculate_score(match_state, @winner_characters_states);
player_stadistics.total_score += 100
+ calculate_score(match_state, @winner_characters_states);
player_stadistics.matchs_won += 1;
} else {
// give 50 points to player for participate
Expand Down
57 changes: 57 additions & 0 deletions src/tests/test_character_system.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,61 @@ 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');

// [Assert] Peasent
let peasent = store.get_character(5);
assert(peasent.character_type == 5, 'peasent wrong id');
assert(peasent.hp == 300, 'peasent wrong initial hp');
assert(peasent.mp == 100, 'peasent wrong initial mp');
assert(peasent.attack == 20, 'peasent wrong initial attack');
assert(peasent.defense == 15, 'peasent wrong initial defense');
assert(peasent.evasion == 0, 'peasent wrong initial evasion');
assert(peasent.crit_chance == 0, 'peasent wrong crit_chance');
assert(peasent.crit_rate == 2, 'peasent wrong initial crit_rate');
assert(peasent.movement_range == 5, 'peasent wrong initial movement');
}

#[test]
#[available_gas(1_000_000_000)]
fn test_mint_character() {
// [Setup]
let (world, systems) = setup::spawn_game();
let mut store = StoreTrait::new(world);
let PLAYER_1 = '0x1';

// [Create]
systems.character_system.init();

// [Create]
systems.character_system.init();

// [Mint]
systems.character_system.mint(CharacterType::Warrior, PLAYER_1, 1);
systems.character_system.mint(CharacterType::Archer, PLAYER_1, 1);

let player_stadistics = store.get_player_stadistics(PLAYER_1);
assert(player_stadistics.characters_owned == 2, 'wrong characters owned number');
}

#[test]
#[available_gas(1_000_000_000)]
#[should_panic(expected: ('ERR: character already owned', 'ENTRYPOINT_FAILED'))]
fn test_mint_same_character_twice() {
// [Setup]
let (world, systems) = setup::spawn_game();
let mut store = StoreTrait::new(world);

// [Create]
systems.character_system.init();

// [Mint] Warrior
let PLAYER_1 = '0x1';

// [Create]
systems.character_system.init();

// [Mint]
systems.character_system.mint(CharacterType::Warrior, PLAYER_1, 1);
systems.character_system.mint(CharacterType::Warrior, PLAYER_1, 1);
}

25 changes: 25 additions & 0 deletions src/tests/test_match_system.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ 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::{MatchCount, MATCH_COUNT_KEY};
use starkane::models::states::character_state::CharacterState;

use starkane::tests::setup::{setup, setup::Systems, setup::PLAYER};

Expand Down Expand Up @@ -372,6 +373,30 @@ fn test_end_match_set_correct_winner() {
.get_match_player_characters_len(MATCH_ID, PLAYER_2)
.remain_characters;
assert(remain_characters_player_2 == 0, 'it should be remain 0 character');

// Check match stadistics update
let player_1_stadistics = store.get_player_stadistics(PLAYER_1);
assert(player_1_stadistics.matchs_lost == 0, 'wrong 0x1 match lost');
assert(player_1_stadistics.matchs_won == 1, 'wrong 0x1 match won');

//
let match_characters_player_1 = store.get_match_player_characters_states(MATCH_ID, PLAYER_1);
let mut total_remain_hp = 0;
let mut i = 0;
loop {
if match_characters_player_1.len() == i {
break;
}
let character_state: CharacterState = *match_characters_player_1[i];
total_remain_hp += character_state.remain_hp;
i += 1;
};
assert(player_1_stadistics.total_score == 100 + total_remain_hp, 'wrong 0x1 wrong score');

let player_2_stadistics = store.get_player_stadistics(PLAYER_2);
assert(player_2_stadistics.matchs_lost == 1, 'wrong 0x2 match lost');
assert(player_2_stadistics.matchs_won == 0, 'wrong 0x2 match won');
assert(player_2_stadistics.total_score == 50, 'wrong 0x2 wrong score');
}

#[test]
Expand Down

0 comments on commit 3ac70d8

Please sign in to comment.