From 656259589a16e3219fbd0a355bec7837f4ccf14b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damian=20Pi=C3=B1ones?= Date: Thu, 31 Oct 2024 22:57:30 -0300 Subject: [PATCH] add event in game --- src/models/data/beast.cairo | 1 + src/models/data/challenge.cairo | 1 + src/models/data/events.cairo | 18 ++++++++++++++++++ src/models/status/round/beast.cairo | 13 +++++++------ src/models/status/round/challenge.cairo | 2 ++ src/tests/test_game_play.cairo | 20 ++++++++++---------- src/tests/test_game_play_beast.cairo | 18 ++++++------------ 7 files changed, 45 insertions(+), 28 deletions(-) diff --git a/src/models/data/beast.cairo b/src/models/data/beast.cairo index 9c8c6fa..4b4a4d9 100644 --- a/src/models/data/beast.cairo +++ b/src/models/data/beast.cairo @@ -21,6 +21,7 @@ struct Beast { } #[derive(Copy, Drop, IntrospectPacked, Serde)] +#[dojo::event] #[dojo::model] struct PlayerBeast { #[key] diff --git a/src/models/data/challenge.cairo b/src/models/data/challenge.cairo index 3de614d..d61fd00 100644 --- a/src/models/data/challenge.cairo +++ b/src/models/data/challenge.cairo @@ -8,6 +8,7 @@ struct Challenge { } #[derive(Copy, Drop, IntrospectPacked, Serde)] +#[dojo::event] #[dojo::model] struct ChallengePlayer { #[key] diff --git a/src/models/data/events.cairo b/src/models/data/events.cairo index 28172bc..7ab96a2 100644 --- a/src/models/data/events.cairo +++ b/src/models/data/events.cairo @@ -254,3 +254,21 @@ struct ChallengeCompleted { player_name: felt252, game_id: u32 } + +#[derive(Copy, Drop, Serde)] +#[dojo::event] +#[dojo::model] +struct BeastAttack { + #[key] + player: ContractAddress, + attack: u32 +} + +#[derive(Copy, Drop, Serde)] +#[dojo::event] +#[dojo::model] +struct PlayerAttack { + #[key] + player: ContractAddress, + attack: u32 +} diff --git a/src/models/status/round/beast.cairo b/src/models/status/round/beast.cairo index 17c06ab..9f189a7 100644 --- a/src/models/status/round/beast.cairo +++ b/src/models/status/round/beast.cairo @@ -5,7 +5,7 @@ use jokers_of_neon::constants::card::INVALID_CARD; use jokers_of_neon::models::data::beast::{ GameModeBeast, GameModeBeastStore, Beast, BeastStore, PlayerBeast, PlayerBeastStore }; -use jokers_of_neon::models::data::events::{PlayWinGameEvent, PlayGameOverEvent}; +use jokers_of_neon::models::data::events::{PlayWinGameEvent, PlayGameOverEvent, BeastAttack, PlayerAttack}; use jokers_of_neon::models::data::game_deck::{GameDeckImpl, GameDeck, GameDeckStore}; use jokers_of_neon::models::status::game::game::{Game, GameState, GameSubState}; use jokers_of_neon::models::status::game::rage::{RageRound, RageRoundStore}; @@ -73,13 +73,13 @@ impl BeastImpl of BeastTrait { let rage_round = RageRoundStore::get(world, game_id); - let score = play(world, ref game, @cards_index, @modifiers_index); + let attack = play(world, ref game, @cards_index, @modifiers_index); - let player_attack = score; // TODO: + emit!(world, (PlayerAttack { player: get_caller_address(), attack })); let mut beast = BeastStore::get(world, game.id); - beast.health = if player_attack < beast.health { - beast.health - player_attack + beast.health = if attack < beast.health { + beast.health - attack } else { 0 }; @@ -109,7 +109,7 @@ impl BeastImpl of BeastTrait { _ => Option::None }.unwrap(); IRageSystemDispatcher { contract_address: rage_system_address.try_into().unwrap() }.calculate(game.id); - // create_level(world, ref store, game); TODO: + // create_level(world, ref store, game); TODO: } else if player_beast.energy.is_zero() { _attack_beast(world, ref store, ref game, ref player_beast, ref beast, ref game_mode_beast); } else { @@ -242,6 +242,7 @@ fn _attack_beast( ref beast: Beast, ref game_mode_beast: GameModeBeast ) { + emit!(world, (BeastAttack { player: get_caller_address(), attack: beast.attack })); player_beast.health = if beast.attack > player_beast.health { 0 } else { diff --git a/src/models/status/round/challenge.cairo b/src/models/status/round/challenge.cairo index 6a0ef4f..848677f 100644 --- a/src/models/status/round/challenge.cairo +++ b/src/models/status/round/challenge.cairo @@ -69,6 +69,7 @@ impl ChallengeImpl of ChallengeTrait { } else { let mut challenge_player = ChallengePlayerStore::get(world, game_id); challenge_player.plays -= 1; + emit!(world, (challenge_player)); ChallengePlayerStore::set(@challenge_player, world); } @@ -117,6 +118,7 @@ impl ChallengeImpl of ChallengeTrait { let mut challenge_player = ChallengePlayerStore::get(world, game_id); challenge_player.discards -= 1; + emit!(world, (challenge_player)); ChallengePlayerStore::set(@challenge_player, world); let game_deck = GameDeckStore::get(world, game_id); diff --git a/src/tests/test_game_play.cairo b/src/tests/test_game_play.cairo index eb0e0d6..2eb2e5d 100644 --- a/src/tests/test_game_play.cairo +++ b/src/tests/test_game_play.cairo @@ -649,7 +649,7 @@ mod test_rage_cards { let round_after = store.get_round(game.id); assert(round_after.player_score == 40, 'wrong round player_score'); } -// #[test] + // #[test] // #[available_gas(30000000000000000)] // fn test_play_rage_card_diminished_hold() { // let (world, systems) = setup::spawn_game(); @@ -657,22 +657,22 @@ mod test_rage_cards { // let mut game = mock_game(ref store, PLAYER()); // mock_round(ref store, @game, 300); -// // Mock RageRound + // // Mock RageRound // let len_hand_before = game.len_hand; // mock_rage_round(world, game.id, array![RAGE_CARD_DIMINISHED_HOLD]); -// // Set game state in shop + // // Set game state in shop // game.state = GameState::AT_SHOP; // store.set_game(game); -// set_contract_address(PLAYER()); + // set_contract_address(PLAYER()); // systems.shop_system.skip_shop(game.id); -// let game_after = store.get_game(game.id); + // let game_after = store.get_game(game.id); // assert(game_after.len_hand == len_hand_before - 2, 'wrong game_after len_hand'); // } -// #[test] + // #[test] // #[available_gas(30000000000000000)] // fn test_play_rage_card_zero_waste() { // let (world, systems) = setup::spawn_game(); @@ -680,17 +680,17 @@ mod test_rage_cards { // let mut game = mock_game(ref store, PLAYER()); // mock_round(ref store, @game, 300); -// // Mock RageRound + // // Mock RageRound // mock_rage_round(world, game.id, array![RAGE_CARD_ZERO_WASTE]); -// // Set game state in shop + // // Set game state in shop // game.state = GameState::AT_SHOP; // store.set_game(game); -// set_contract_address(PLAYER()); + // set_contract_address(PLAYER()); // systems.shop_system.skip_shop(game.id); -// let round = store.get_round(game.id); + // let round = store.get_round(game.id); // assert(round.discard == 0, 'wrong round discard'); // } } diff --git a/src/tests/test_game_play_beast.cairo b/src/tests/test_game_play_beast.cairo index a90b193..e197a00 100644 --- a/src/tests/test_game_play_beast.cairo +++ b/src/tests/test_game_play_beast.cairo @@ -93,12 +93,7 @@ mod test_play_beast_special_cards { // Mock hand let hand_cards_ids = array![ - SIX_CLUBS_ID, - QUEEN_CLUBS_ID, - FOUR_CLUBS_ID, - JACK_HEARTS_ID, - KING_DIAMONDS_ID, - MULTI_MODIFIER_1_ID + SIX_CLUBS_ID, QUEEN_CLUBS_ID, FOUR_CLUBS_ID, JACK_HEARTS_ID, KING_DIAMONDS_ID, MULTI_MODIFIER_1_ID ]; mock_current_hand_cards_ids(ref store, game.id, hand_cards_ids); @@ -285,7 +280,7 @@ mod test_play_beast_special_cards { player_beast_after.energy == player_beast_before.energy - game_mode_beast.cost_play, 'wrong player energy' ); } -// #[test] + // #[test] // #[available_gas(30000000000000000)] // fn test_play_special_initial_advantage() { // let (world, systems) = setup::spawn_game(); @@ -293,15 +288,15 @@ mod test_play_beast_special_cards { // let mut game = mock_game(ref store, PLAYER()); // mock_round(ref store, @game, 300); -// // Mock special card + // // Mock special card // let special_cards_ids = array![SPECIAL_INITIAL_ADVANTAGE_ID]; // mock_special_cards(ref store, ref game, special_cards_ids); -// // Mock hand + // // Mock hand // let hand_cards_ids = array![ACE_CLUBS_ID, ACE_HEARTS_ID, ACE_DIAMONDS_ID, ACE_SPADES_ID]; // mock_current_hand_cards_ids(ref store, game.id, hand_cards_ids); -// set_contract_address(PLAYER()); + // set_contract_address(PLAYER()); // systems.game_system.play(game.id, array![0, 1, 2, 3], array![100, 100, 100, 100]); // // Four of a Kind - points: 60, multi: 7 // // points: 11 + 11 + 11 + 11 + 100 @@ -318,8 +313,7 @@ mod test_play_beast_modifier_cards { KING_SPADES_ID, TWO_SPADES_ID, TWO_DIAMONDS_ID, TWO_CLUBS_ID, FOUR_DIAMONDS_ID, FOUR_HEARTS_ID }; use jokers_of_neon::constants::modifiers::{ - MULTI_MODIFIER_1_ID, POINTS_MODIFIER_4_ID, MULTI_MODIFIER_4_ID, POINTS_MODIFIER_2_ID, - MULTI_MODIFIER_3_ID, + MULTI_MODIFIER_1_ID, POINTS_MODIFIER_4_ID, MULTI_MODIFIER_4_ID, POINTS_MODIFIER_2_ID, MULTI_MODIFIER_3_ID, }; use jokers_of_neon::models::data::beast::{ GameModeBeast, GameModeBeastStore, Beast, BeastStore, PlayerBeast, PlayerBeastStore