diff --git a/onchain/src/contracts/lyricsflip.cairo b/onchain/src/contracts/lyricsflip.cairo index eb07bcb7..65fb63f7 100644 --- a/onchain/src/contracts/lyricsflip.cairo +++ b/onchain/src/contracts/lyricsflip.cairo @@ -5,32 +5,12 @@ pub mod LyricsFlip { use lyricsflip::interfaces::lyricsflip::{ILyricsFlip}; use lyricsflip::utils::errors::Errors; use lyricsflip::utils::types::{Card, Entropy, Genre, Round}; - use openzeppelin::introspection::src5::SRC5Component; - use openzeppelin_access::accesscontrol::{AccessControlComponent}; - use openzeppelin_access::ownable::OwnableComponent; use starknet::storage::{ Map, MutableVecTrait, StoragePathEntry, StoragePointerReadAccess, StoragePointerWriteAccess, Vec, VecTrait, }; use starknet::{ContractAddress, get_block_number, get_block_timestamp, get_caller_address}; - component!(path: OwnableComponent, storage: ownable, event: OwnableEvent); - component!(path: AccessControlComponent, storage: accesscontrol, event: AccessControlEvent); - component!(path: SRC5Component, storage: src5, event: SRC5Event); - - #[abi(embed_v0)] - impl OwnableImpl = OwnableComponent::OwnableImpl; - - impl OwnableInternalImpl = OwnableComponent::InternalImpl; - - #[abi(embed_v0)] - impl SRC5Impl = SRC5Component::SRC5Impl; - - #[abi(embed_v0)] - impl AccessControlImpl = - AccessControlComponent::AccessControlImpl; - - impl AccessControlInternalImpl = AccessControlComponent::InternalImpl; #[storage] struct Storage { @@ -46,13 +26,7 @@ pub mod LyricsFlip { u64, Map, >, // round_id -> player_index -> player_address round_players_count: Map, - round_cards: Map>, // round_id -> vec - #[substorage(v0)] - ownable: OwnableComponent::Storage, - #[substorage(v0)] - accesscontrol: AccessControlComponent::Storage, - #[substorage(v0)] - src5: SRC5Component::Storage, + round_cards: Map> // round_id -> vec } @@ -63,12 +37,6 @@ pub mod LyricsFlip { RoundStarted: RoundStarted, RoundJoined: RoundJoined, SetCardPerRound: SetCardPerRound, - #[flat] - OwnableEvent: OwnableComponent::Event, - #[flat] - AccessControlEvent: AccessControlComponent::Event, - #[flat] - SRC5Event: SRC5Component::Event, } #[derive(Drop, starknet::Event)] @@ -105,14 +73,8 @@ pub mod LyricsFlip { pub new_value: u8, } - const ADMIN_ROLE: felt252 = selector!("ADMIN_ROLE"); - #[constructor] - fn constructor(ref self: ContractState, owner: ContractAddress) { - self.ownable.initializer(owner); - self.accesscontrol.initializer(); - self.accesscontrol._grant_role(ADMIN_ROLE, owner); - } + fn constructor(ref self: ContractState) {} #[abi(embed_v0)] pub impl LyricsFlipImpl of ILyricsFlip { @@ -249,7 +211,6 @@ pub mod LyricsFlip { fn set_cards_per_round(ref self: ContractState, value: u8) { - self.accesscontrol.assert_only_role(ADMIN_ROLE); assert(value > 0, Errors::INVALID_CARDS_PER_ROUND); let old_value = self.cards_per_round.read(); @@ -270,7 +231,6 @@ pub mod LyricsFlip { fn add_card(ref self: ContractState, card: Card) { - self.accesscontrol.assert_only_role(ADMIN_ROLE); let card_id = self.cards_count.read() + 1; self.artist_cards.entry(card.artist).append().write(card_id); @@ -302,10 +262,27 @@ pub mod LyricsFlip { }; genre_cards.span() } - // // TODO - // fn next_card(ref self: ContractState, round_id: u64) -> Card { - // self._next_round_card() - // } + + fn next_card(ref self: ContractState, round_id: u64) -> Card { + let round: Round = self.rounds.entry(round_id).read(); + assert(round.is_started, 'Round has not started'); + assert(!round.is_completed, 'Round is already completed'); + + // Retrieve next card + let next_card_id = self.round_cards.entry(round_id).at(round.next_card_index.into()).read(); + let next_card = self.cards.entry(next_card_id).read(); + + // Increment the next_card_index + let mut round = self.rounds.entry(round_id).read(); + round.next_card_index += 1; + if round.next_card_index >= self.cards_per_round.read() { + round.is_completed = true; + } + self.rounds.entry(round_id).write(round); + + next_card + } + // // TODO // fn get_cards_of_genre(self: @ContractState, genre: Genre, amount: u64) -> Span {} @@ -326,30 +303,8 @@ pub mod LyricsFlip { }; cards.span() } - - - fn set_role( - ref self: ContractState, recipient: ContractAddress, role: felt252, is_enable: bool - ) { - self._set_role(recipient, role, is_enable); - } - fn is_admin(self: @ContractState, role: felt252, address: ContractAddress) -> bool { - self.accesscontrol.has_role(role, address) - } - - fn get_cards_of_a_year(self: @ContractState, year: u64, seed: u64) -> Span { - let year_cards = self.year_cards.entry(year).len(); - assert(year_cards > 0, Errors::EMPTY_YEAR_CARDS); - let amount = self.cards_per_round.read(); - let mut cards = ArrayTrait::new(); - let random_indices = self._get_random_numbers(seed, amount.into(), year_cards, true); - for i in random_indices { - let card_id = self.year_cards.entry(year).at(*i).read(); - let card = self.cards.entry(card_id).read(); - cards.append(card); - }; - cards.span() - } + // //TODO + // fn get_cards_of_a_year(self: @ContractState, year: u64, amount: u64) -> Span {} } #[generate_trait] @@ -361,10 +316,10 @@ pub mod LyricsFlip { self._get_random_numbers(seed, amount, limit, false) } - // // TODO - // fn _next_round_card(ref self: ContractState, round_id: u64) -> Card { - // // check round is started and is_completed is false - // } + + fn _next_round_card(ref self: ContractState, round_id: u64) -> Card { + self.next_card(round_id) + } /// Generates unique random numbers within a specified range. /// Uses a seed and entropy (block data, timestamp, index) to create randomness, @@ -415,18 +370,5 @@ pub mod LyricsFlip { }; unique_numbers.span() } - - fn _set_role( - ref self: ContractState, recipient: ContractAddress, role: felt252, is_enable: bool - ) { - self.ownable.assert_only_owner(); - self.accesscontrol.assert_only_role(ADMIN_ROLE); - assert!(role == ADMIN_ROLE, "role not enable"); - if is_enable { - self.accesscontrol._grant_role(role, recipient); - } else { - self.accesscontrol._revoke_role(role, recipient); - } - } } } diff --git a/onchain/src/interfaces/lyricsflip.cairo b/onchain/src/interfaces/lyricsflip.cairo index c0c05169..76fd9338 100644 --- a/onchain/src/interfaces/lyricsflip.cairo +++ b/onchain/src/interfaces/lyricsflip.cairo @@ -19,18 +19,16 @@ pub trait ILyricsFlip { fn add_card(ref self: TContractState, card: Card); fn get_card(self: @TContractState, card_id: u64) -> Card; fn get_cards_of_genre(self: @TContractState, genre: Genre, seed: u64) -> Span; - // // TODO - // fn next_card(ref self: TContractState, round_id: u64) -> Card; + + + fn next_card(ref self: TContractState, round_id: u64) -> Card; // // TODO // fn get_cards_of_genre(self: @TContractState, genre: Genre) -> Span; // TODO fn get_cards_of_artist(self: @TContractState, artist: felt252, seed: u64) -> Span; - fn set_role( - ref self: TContractState, recipient: ContractAddress, role: felt252, is_enable: bool - ); - fn is_admin(self: @TContractState, role: felt252, address: ContractAddress) -> bool; + // //TODO - fn get_cards_of_a_year(self: @TContractState, year: u64, seed: u64) -> Span; +// fn get_cards_of_a_year(self: @TContractState, year: u64) -> Span; } diff --git a/onchain/src/tests/test_lyricsflip.cairo b/onchain/src/tests/test_lyricsflip.cairo index 2d3a518b..dab523f3 100644 --- a/onchain/src/tests/test_lyricsflip.cairo +++ b/onchain/src/tests/test_lyricsflip.cairo @@ -9,14 +9,6 @@ use snforge_std::{ }; use starknet::{ContractAddress, get_block_timestamp}; -fn OWNER() -> ContractAddress { - 'OWNER'.try_into().unwrap() -} - -fn ADMIN_ADDRESS() -> ContractAddress { - 'ADMIN_ADDRESS'.try_into().unwrap() -} - fn PLAYER_1() -> ContractAddress { 'PLAYER_1'.try_into().unwrap() } @@ -25,15 +17,10 @@ fn PLAYER_2() -> ContractAddress { 'PLAYER_2'.try_into().unwrap() } -const ADMIN_ROLE: felt252 = selector!("ADMIN_ROLE"); -const INVALID_ROLE: felt252 = selector!("INVALID_ROLE"); fn deploy() -> ILyricsFlipDispatcher { let contract = declare("LyricsFlip").unwrap().contract_class(); - let mut constructor_calldata = array![]; - let owner: ContractAddress = OWNER().try_into().unwrap(); - owner.serialize(ref constructor_calldata); - let (contract_address, _) = contract.deploy(@constructor_calldata).unwrap(); + let (contract_address, _) = contract.deploy(@array![]).unwrap(); ILyricsFlipDispatcher { contract_address } } @@ -42,11 +29,7 @@ fn deploy() -> ILyricsFlipDispatcher { fn test_create_round() { let lyricsflip = deploy(); let mut spy = spy_events(); - start_cheat_caller_address(lyricsflip.contract_address, OWNER()); - lyricsflip.set_role(ADMIN_ADDRESS(), ADMIN_ROLE, true); - stop_cheat_caller_address(lyricsflip.contract_address); - start_cheat_caller_address(lyricsflip.contract_address, ADMIN_ADDRESS()); for i in 0 ..10_u64 { let card = Card { @@ -60,11 +43,11 @@ fn test_create_round() { lyricsflip.add_card(card); }; + start_cheat_caller_address(lyricsflip.contract_address, PLAYER_1()); + let valid_cards_per_round = 5; lyricsflip.set_cards_per_round(valid_cards_per_round); - stop_cheat_caller_address(lyricsflip.contract_address); - start_cheat_caller_address(lyricsflip.contract_address, PLAYER_1()); let seed = 1; let round_id = lyricsflip.create_round(Option::Some(Genre::HipHop), seed); stop_cheat_caller_address(lyricsflip.contract_address); @@ -108,45 +91,6 @@ fn test_create_round() { } } -#[test] -fn test_set_role() { - let lyricsflip = deploy(); - let mut spy = spy_events(); - - start_cheat_caller_address(lyricsflip.contract_address, OWNER()); - lyricsflip.set_role(ADMIN_ADDRESS(), ADMIN_ROLE, true); - stop_cheat_caller_address(lyricsflip.contract_address); - - let is_admin = lyricsflip.is_admin(ADMIN_ROLE, ADMIN_ADDRESS()); - assert(is_admin == true, 'wrong is_admin value'); -} - -#[test] -#[should_panic] -fn test_set_role_should_panic_when_invalid_role_is_passed() { - let lyricsflip = deploy(); - let mut spy = spy_events(); - - start_cheat_caller_address(lyricsflip.contract_address, OWNER()); - lyricsflip.set_role(ADMIN_ADDRESS(), INVALID_ROLE, true); - stop_cheat_caller_address(lyricsflip.contract_address); -} - -#[test] -#[should_panic(expected: ('Caller is not the owner',))] -fn test_set_role_should_panic_when_called_by_non_owner() { - let lyricsflip = deploy(); - - start_cheat_caller_address(lyricsflip.contract_address, OWNER()); - lyricsflip.set_role(ADMIN_ADDRESS(), ADMIN_ROLE, true); - stop_cheat_caller_address(lyricsflip.contract_address); - - start_cheat_caller_address(lyricsflip.contract_address, ADMIN_ADDRESS()); - lyricsflip.set_role(ADMIN_ADDRESS(), ADMIN_ROLE, true); - stop_cheat_caller_address(lyricsflip.contract_address); -} - - #[test] fn test_start_round() { let lyricsflip = deploy(); @@ -154,11 +98,6 @@ fn test_start_round() { start_cheat_block_timestamp_global(1736593692); - start_cheat_caller_address(lyricsflip.contract_address, OWNER()); - lyricsflip.set_role(ADMIN_ADDRESS(), ADMIN_ROLE, true); - stop_cheat_caller_address(lyricsflip.contract_address); - - start_cheat_caller_address(lyricsflip.contract_address, ADMIN_ADDRESS()); for i in 0 ..10_u64 { let card = Card { @@ -172,11 +111,11 @@ fn test_start_round() { lyricsflip.add_card(card); }; + start_cheat_caller_address(lyricsflip.contract_address, PLAYER_1()); + let valid_cards_per_round = 5; lyricsflip.set_cards_per_round(valid_cards_per_round); - stop_cheat_caller_address(lyricsflip.contract_address); - start_cheat_caller_address(lyricsflip.contract_address, PLAYER_1()); let seed = 1; let round_id = lyricsflip.create_round(Option::Some(Genre::HipHop), seed); lyricsflip.start_round(round_id); @@ -220,11 +159,6 @@ fn test_join_round() { start_cheat_block_timestamp_global(1736593692); - start_cheat_caller_address(lyricsflip.contract_address, OWNER()); - lyricsflip.set_role(ADMIN_ADDRESS(), ADMIN_ROLE, true); - stop_cheat_caller_address(lyricsflip.contract_address); - - start_cheat_caller_address(lyricsflip.contract_address, ADMIN_ADDRESS()); for i in 0 ..10_u64 { let card = Card { @@ -238,11 +172,11 @@ fn test_join_round() { lyricsflip.add_card(card); }; + start_cheat_caller_address(lyricsflip.contract_address, PLAYER_1()); + let valid_cards_per_round = 5; lyricsflip.set_cards_per_round(valid_cards_per_round); - stop_cheat_caller_address(lyricsflip.contract_address); - start_cheat_caller_address(lyricsflip.contract_address, PLAYER_1()); let seed = 1; let round_id = lyricsflip.create_round(Option::Some(Genre::HipHop), seed); @@ -283,11 +217,6 @@ fn test_join_round() { fn test_create_round_should_panic_with_unknown_genre() { let lyricsflip = deploy(); - start_cheat_caller_address(lyricsflip.contract_address, OWNER()); - lyricsflip.set_role(ADMIN_ADDRESS(), ADMIN_ROLE, true); - stop_cheat_caller_address(lyricsflip.contract_address); - - start_cheat_caller_address(lyricsflip.contract_address, ADMIN_ADDRESS()); for i in 0 ..10_u64 { let card = Card { @@ -301,11 +230,12 @@ fn test_create_round_should_panic_with_unknown_genre() { lyricsflip.add_card(card); }; + start_cheat_caller_address(lyricsflip.contract_address, PLAYER_1()); + let valid_cards_per_round = 5; lyricsflip.set_cards_per_round(valid_cards_per_round); - stop_cheat_caller_address(lyricsflip.contract_address); + let seed = 1; - start_cheat_caller_address(lyricsflip.contract_address, PLAYER_1()); lyricsflip.create_round(Option::None, seed); stop_cheat_caller_address(lyricsflip.contract_address); @@ -316,11 +246,6 @@ fn test_create_round_should_panic_with_unknown_genre() { fn test_start_round_should_panic_with_only_admin() { let lyricsflip = deploy(); - start_cheat_caller_address(lyricsflip.contract_address, OWNER()); - lyricsflip.set_role(ADMIN_ADDRESS(), ADMIN_ROLE, true); - stop_cheat_caller_address(lyricsflip.contract_address); - - start_cheat_caller_address(lyricsflip.contract_address, ADMIN_ADDRESS()); for i in 0 ..10_u64 { let card = Card { @@ -334,10 +259,11 @@ fn test_start_round_should_panic_with_only_admin() { lyricsflip.add_card(card); }; + start_cheat_caller_address(lyricsflip.contract_address, PLAYER_1()); + let valid_cards_per_round = 5; lyricsflip.set_cards_per_round(valid_cards_per_round); - stop_cheat_caller_address(lyricsflip.contract_address); - start_cheat_caller_address(lyricsflip.contract_address, PLAYER_1()); + let seed = 1; let round_id = lyricsflip.create_round(Option::Some(Genre::HipHop), seed); @@ -367,11 +293,6 @@ fn test_start_round_should_panic_with_non_existing_round() { fn test_join_round_should_panic_with_round_already_started() { let lyricsflip = deploy(); - start_cheat_caller_address(lyricsflip.contract_address, OWNER()); - lyricsflip.set_role(ADMIN_ADDRESS(), ADMIN_ROLE, true); - stop_cheat_caller_address(lyricsflip.contract_address); - - start_cheat_caller_address(lyricsflip.contract_address, ADMIN_ADDRESS()); for i in 0 ..10_u64 { let card = Card { @@ -385,11 +306,11 @@ fn test_join_round_should_panic_with_round_already_started() { lyricsflip.add_card(card); }; + start_cheat_caller_address(lyricsflip.contract_address, PLAYER_1()); + let valid_cards_per_round = 5; lyricsflip.set_cards_per_round(valid_cards_per_round); - stop_cheat_caller_address(lyricsflip.contract_address); - start_cheat_caller_address(lyricsflip.contract_address, PLAYER_1()); let seed = 1; let round_id = lyricsflip.create_round(Option::Some(Genre::HipHop), seed); lyricsflip.start_round(round_id); @@ -407,11 +328,6 @@ fn test_join_round_should_panic_with_round_already_started() { fn test_join_round_should_panic_with_already_joined() { let lyricsflip = deploy(); - start_cheat_caller_address(lyricsflip.contract_address, OWNER()); - lyricsflip.set_role(ADMIN_ADDRESS(), ADMIN_ROLE, true); - stop_cheat_caller_address(lyricsflip.contract_address); - - start_cheat_caller_address(lyricsflip.contract_address, ADMIN_ADDRESS()); for i in 0 ..10_u64 { let card = Card { @@ -425,10 +341,11 @@ fn test_join_round_should_panic_with_already_joined() { lyricsflip.add_card(card); }; + start_cheat_caller_address(lyricsflip.contract_address, PLAYER_1()); + let valid_cards_per_round = 5; lyricsflip.set_cards_per_round(valid_cards_per_round); - stop_cheat_caller_address(lyricsflip.contract_address); - start_cheat_caller_address(lyricsflip.contract_address, PLAYER_1()); + let seed = 1; let round_id = lyricsflip.create_round(Option::Some(Genre::HipHop), seed); @@ -454,19 +371,18 @@ fn test_join_round_should_panic_with_non_existing_round() { stop_cheat_caller_address(lyricsflip.contract_address); } + #[test] fn test_set_cards_per_round() { let lyricsflip = deploy(); let mut spy = spy_events(); - start_cheat_caller_address(lyricsflip.contract_address, OWNER()); - lyricsflip.set_role(ADMIN_ADDRESS(), ADMIN_ROLE, true); - stop_cheat_caller_address(lyricsflip.contract_address); + start_cheat_caller_address(lyricsflip.contract_address, PLAYER_1()); - start_cheat_caller_address(lyricsflip.contract_address, ADMIN_ADDRESS()); let valid_cards_per_round = 3; let old_value = lyricsflip.get_cards_per_round(); lyricsflip.set_cards_per_round(valid_cards_per_round); + stop_cheat_caller_address(lyricsflip.contract_address); let cards_per_round = lyricsflip.get_cards_per_round(); @@ -492,14 +408,9 @@ fn test_set_cards_per_round() { fn test_get_cards_per_round() { let lyricsflip = deploy(); - start_cheat_caller_address(lyricsflip.contract_address, OWNER()); - lyricsflip.set_role(ADMIN_ADDRESS(), ADMIN_ROLE, true); - stop_cheat_caller_address(lyricsflip.contract_address); - - start_cheat_caller_address(lyricsflip.contract_address, ADMIN_ADDRESS()); let initial_cards_per_round = 3; lyricsflip.set_cards_per_round(initial_cards_per_round); - stop_cheat_caller_address(lyricsflip.contract_address); + let retrieved_cards_per_round = lyricsflip.get_cards_per_round(); assert(retrieved_cards_per_round == initial_cards_per_round, 'wrong cards_per_round value'); } @@ -509,25 +420,14 @@ fn test_get_cards_per_round() { fn test_set_cards_per_round_should_panic_with_invalid_value() { let lyricsflip = deploy(); - start_cheat_caller_address(lyricsflip.contract_address, OWNER()); - lyricsflip.set_role(ADMIN_ADDRESS(), ADMIN_ROLE, true); - stop_cheat_caller_address(lyricsflip.contract_address); - - start_cheat_caller_address(lyricsflip.contract_address, ADMIN_ADDRESS()); let invalid_cards_per_round = 0; lyricsflip.set_cards_per_round(invalid_cards_per_round); - stop_cheat_caller_address(lyricsflip.contract_address); } #[test] fn test_add_card() { let lyricsflip = deploy(); - start_cheat_caller_address(lyricsflip.contract_address, OWNER()); - lyricsflip.set_role(ADMIN_ADDRESS(), ADMIN_ROLE, true); - stop_cheat_caller_address(lyricsflip.contract_address); - - start_cheat_caller_address(lyricsflip.contract_address, ADMIN_ADDRESS()); let genre: Genre = Genre::Reggae; let card = Card { @@ -540,7 +440,7 @@ fn test_add_card() { }; lyricsflip.add_card(card); - stop_cheat_caller_address(lyricsflip.contract_address); + let card_stored = lyricsflip.get_card(1); assert(card_stored.card_id == 1, 'Wrong card_id'); assert(card_stored.year == 2000, 'Wrong card_id'); @@ -593,11 +493,6 @@ fn test_generate_random_numbers_should_panic_with_invalid_amount() { fn test_get_cards_of_genre() { let lyricsflip = deploy(); - start_cheat_caller_address(lyricsflip.contract_address, OWNER()); - lyricsflip.set_role(ADMIN_ADDRESS(), ADMIN_ROLE, true); - stop_cheat_caller_address(lyricsflip.contract_address); - - start_cheat_caller_address(lyricsflip.contract_address, ADMIN_ADDRESS()); for i in 0 ..5_u64 { let card = Card { @@ -606,15 +501,16 @@ fn test_get_cards_of_genre() { artist: 'Bob Marley', title: "", year: 2000, - lyrics: "Lorem Ipsum", + lyrics: "Lorem Ipsum" }; lyricsflip.add_card(card); }; + start_cheat_caller_address(lyricsflip.contract_address, PLAYER_1()); + let valid_cards_per_round = 5; lyricsflip.set_cards_per_round(valid_cards_per_round); - stop_cheat_caller_address(lyricsflip.contract_address); - start_cheat_caller_address(lyricsflip.contract_address, PLAYER_1()); + let seed = 1; stop_cheat_caller_address(lyricsflip.contract_address); let genre_cards = lyricsflip.get_cards_of_genre(Genre::HipHop, seed); @@ -630,15 +526,11 @@ fn test_get_cards_of_genre() { fn test_get_cards_of_genre_should_panic_with_not_enough_cards_of_this_genre() { let lyricsflip = deploy(); - start_cheat_caller_address(lyricsflip.contract_address, OWNER()); - lyricsflip.set_role(ADMIN_ADDRESS(), ADMIN_ROLE, true); - stop_cheat_caller_address(lyricsflip.contract_address); + start_cheat_caller_address(lyricsflip.contract_address, PLAYER_1()); - start_cheat_caller_address(lyricsflip.contract_address, ADMIN_ADDRESS()); let valid_cards_per_round = 5; lyricsflip.set_cards_per_round(valid_cards_per_round); - stop_cheat_caller_address(lyricsflip.contract_address); - start_cheat_caller_address(lyricsflip.contract_address, PLAYER_1()); + let seed = 1; stop_cheat_caller_address(lyricsflip.contract_address); let genre_cards = lyricsflip.get_cards_of_genre(Genre::HipHop, seed); @@ -653,11 +545,6 @@ fn test_get_cards_of_genre_should_panic_with_not_enough_cards_of_this_genre() { fn test_get_cards_of_artist() { let lyricsflip = deploy(); - start_cheat_caller_address(lyricsflip.contract_address, OWNER()); - lyricsflip.set_role(ADMIN_ADDRESS(), ADMIN_ROLE, true); - stop_cheat_caller_address(lyricsflip.contract_address); - - start_cheat_caller_address(lyricsflip.contract_address, ADMIN_ADDRESS()); for i in 0 ..5_u64 { let card = Card { @@ -671,10 +558,11 @@ fn test_get_cards_of_artist() { lyricsflip.add_card(card); }; + start_cheat_caller_address(lyricsflip.contract_address, PLAYER_1()); + let valid_cards_per_round = 5; lyricsflip.set_cards_per_round(valid_cards_per_round); - stop_cheat_caller_address(lyricsflip.contract_address); - start_cheat_caller_address(lyricsflip.contract_address, PLAYER_1()); + let seed = 1; stop_cheat_caller_address(lyricsflip.contract_address); let artist_cards = lyricsflip.get_cards_of_artist('Tupac', seed); @@ -690,17 +578,13 @@ fn test_get_cards_of_artist() { fn test_get_cards_of_artist_should_panic_with_zero_cards() { let lyricsflip = deploy(); - start_cheat_caller_address(lyricsflip.contract_address, OWNER()); - lyricsflip.set_role(ADMIN_ADDRESS(), ADMIN_ROLE, true); - stop_cheat_caller_address(lyricsflip.contract_address); + start_cheat_caller_address(lyricsflip.contract_address, PLAYER_1()); - start_cheat_caller_address(lyricsflip.contract_address, ADMIN_ADDRESS()); let valid_cards_per_round = 5; lyricsflip.set_cards_per_round(valid_cards_per_round); - stop_cheat_caller_address(lyricsflip.contract_address); - start_cheat_caller_address(lyricsflip.contract_address, PLAYER_1()); let seed = 1; + stop_cheat_caller_address(lyricsflip.contract_address); let artist_cards = lyricsflip.get_cards_of_artist('Tupac', seed); assert(artist_cards.len() == valid_cards_per_round.into(), 'wrong cards count'); for i in 0 @@ -709,102 +593,53 @@ fn test_get_cards_of_artist_should_panic_with_zero_cards() { } } - #[test] -fn test_get_cards_of_a_year() { - // Deploy contract +fn test_next_card() { let lyricsflip = deploy(); + let mut spy = spy_events(); - start_cheat_caller_address(lyricsflip.contract_address, OWNER()); - lyricsflip.set_role(ADMIN_ADDRESS(), ADMIN_ROLE, true); - stop_cheat_caller_address(lyricsflip.contract_address); + start_cheat_caller_address(lyricsflip.contract_address, PLAYER_1()); - start_cheat_caller_address(lyricsflip.contract_address, ADMIN_ADDRESS()); - let target_year = 2000; - for i in 0 - ..5_u64 { - let card = Card { - card_id: i.into(), - genre: Genre::HipHop, - artist: 'Paris Paloma', - title: "Labour", - year: target_year, - lyrics: "For somebody", - }; - lyricsflip.add_card(card); + // Add cards to the contract + for i in 0..10_u64 { + let card = Card { + card_id: i.into(), + genre: Genre::HipHop, + artist: 'Bob Marley', + title: "", + year: 2000, + lyrics: "Lorem Ipsum", }; + lyricsflip.add_card(card); + }; let valid_cards_per_round = 5; lyricsflip.set_cards_per_round(valid_cards_per_round); - let seed = 1; - stop_cheat_caller_address(lyricsflip.contract_address); - - let year_cards = lyricsflip.get_cards_of_a_year(target_year, seed); - - assert(year_cards.len() == valid_cards_per_round.into(), 'wrong cards count'); - - for i in 0..year_cards.len() { - assert(*year_cards.at(i).year == target_year, 'wrong year'); - } -} - -#[test] -#[should_panic(expected: 'Year cards is zero')] -fn test_get_cards_of_a_year_should_panic_with_empty_year() { - let lyricsflip = deploy(); - - start_cheat_caller_address(lyricsflip.contract_address, OWNER()); - lyricsflip.set_role(ADMIN_ADDRESS(), ADMIN_ROLE, true); - stop_cheat_caller_address(lyricsflip.contract_address); - start_cheat_caller_address(lyricsflip.contract_address, ADMIN_ADDRESS()); - let valid_cards_per_round = 5; - lyricsflip.set_cards_per_round(valid_cards_per_round); let seed = 1; - stop_cheat_caller_address(lyricsflip.contract_address); - - let non_existent_year = 1999; - let _year_cards = lyricsflip.get_cards_of_a_year(non_existent_year, seed); -} + let round_id = lyricsflip.create_round(Option::Some(Genre::HipHop), seed); + lyricsflip.start_round(round_id); -#[test] -fn test_get_cards_of_a_year_random_distribution() { - let lyricsflip = deploy(); - start_cheat_caller_address(lyricsflip.contract_address, OWNER()); - lyricsflip.set_role(ADMIN_ADDRESS(), ADMIN_ROLE, true); - stop_cheat_caller_address(lyricsflip.contract_address); + // Retrieve cards using next_card + for i in 0..valid_cards_per_round { + let card = lyricsflip.next_card(round_id); + assert(card.card_id == i.into(), 'wrong card id'); + }; - start_cheat_caller_address(lyricsflip.contract_address, ADMIN_ADDRESS()); - let target_year = 2000; - let total_cards = 10_u64; - for i in 0 - ..total_cards { - let card = Card { - card_id: i.into(), - genre: Genre::HipHop, - artist: 'Paris Paloma', - title: "Labour", - year: target_year, - lyrics: "For somebody", - }; - lyricsflip.add_card(card); - }; + // Check if round is completed + let round = lyricsflip.get_round(round_id); + assert(round.is_completed == true, 'round should be completed'); - let cards_per_round = 5; - lyricsflip.set_cards_per_round(cards_per_round); stop_cheat_caller_address(lyricsflip.contract_address); - let cards_set1 = lyricsflip.get_cards_of_a_year(target_year, 1); - let cards_set2 = lyricsflip.get_cards_of_a_year(target_year, 2); - - let mut all_same = true; - for i in 0 - ..cards_set1 - .len() { - if *cards_set1.at(i).card_id != *cards_set2.at(i).card_id { - all_same = false; - break; - } - }; - assert(!all_same, 'Different seeds give same cards'); + spy.assert_emitted(@array![ + ( + lyricsflip.contract_address, + LyricsFlip::Event::Round_Completed( + LyricsFlip::RoundCompleted { + round_id: round_id, + }, + ), + ), + ]); } diff --git a/onchain/src/utils/errors.cairo b/onchain/src/utils/errors.cairo index 49ba9c97..2c274aff 100644 --- a/onchain/src/utils/errors.cairo +++ b/onchain/src/utils/errors.cairo @@ -6,5 +6,6 @@ pub mod Errors { pub const ROUND_ALREADY_JOINED: felt252 = 'You are already a player'; pub const INVALID_CARDS_PER_ROUND: felt252 = 'Invalid cards per round'; pub const ARTIST_CARDS_IS_ZERO: felt252 = 'Artist cards is zero'; - pub const EMPTY_YEAR_CARDS: felt252 = 'Year cards is zero'; + pub const ROUND_NOT_STARTED: felt252 = 'Round not started'; + pub const ROUND_COMPLETED: felt252 = 'Round completed'; }