Skip to content

Commit

Permalink
refac + add some Todo's (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
manlikeHB authored Feb 17, 2025
1 parent e4e86f5 commit 4b52bc8
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 36 deletions.
44 changes: 24 additions & 20 deletions onchain/src/contracts/lyricsflip.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -145,22 +145,6 @@ pub mod LyricsFlip {
self.round_players_count.entry(round_id).read()
}

fn is_round_player(
self: @ContractState, round_id: u64, player_address: ContractAddress,
) -> bool {
let round_players = self.get_round_players(round_id);
let mut is_player = false;
let mut idx = 0;
while (idx < round_players.len()) {
if *round_players.at(idx) == player_address {
is_player = true;
break;
}
idx += 1;
};
is_player
}

fn next_card(ref self: ContractState, round_id: u64) -> Card {
self._next_round_card(round_id)
}
Expand Down Expand Up @@ -212,12 +196,15 @@ pub mod LyricsFlip {
let caller_address = get_caller_address();
let round = self.rounds.entry(round_id);

//TODO: check if caller is an admin or round participant
assert(round.admin.read() == caller_address, Errors::NOT_ROUND_ADMIN);

let start_time = get_block_timestamp();
round.start_time.write(start_time);
round.is_started.write(true);

//TODO: call the next_card function to get the first QuestionCard

self
.emit(
Event::RoundStarted(
Expand All @@ -231,7 +218,7 @@ pub mod LyricsFlip {
fn join_round(ref self: ContractState, round_id: u64) {
let caller_address = get_caller_address();
assert(self.rounds.entry(round_id).round_id.read() != 0, Errors::NON_EXISTING_ROUND);
assert(!self.is_round_player(round_id, caller_address), Errors::ROUND_ALREADY_JOINED);
assert(!self._is_round_player(round_id, caller_address), Errors::ROUND_ALREADY_JOINED);

let round = self.rounds.entry(round_id);

Expand Down Expand Up @@ -307,7 +294,6 @@ pub mod LyricsFlip {
genre_cards.span()
}

//TODO
fn get_cards_of_artist(self: @ContractState, artist: felt252, seed: u64) -> Span<Card> {
assert(self.artist_cards.entry(artist.into()).len() > 0, Errors::ARTIST_CARDS_IS_ZERO);
let mut cards = array![];
Expand All @@ -324,7 +310,6 @@ pub mod LyricsFlip {
cards.span()
}


fn set_role(
ref self: ContractState, recipient: ContractAddress, role: felt252, is_enable: bool
) {
Expand All @@ -351,7 +336,6 @@ pub mod LyricsFlip {

#[generate_trait]
pub impl InternalFunctions of InternalFunctionsTrait {
//TODO
fn get_random_cards(self: @ContractState, seed: u64) -> Span<u64> {
let amount: u64 = self.cards_per_round.read().into();
let limit = self.cards_count.read();
Expand Down Expand Up @@ -384,6 +368,8 @@ pub mod LyricsFlip {
round.is_completed.write(true);
}

//TODO: Build and return QuestionCard

card
}

Expand Down Expand Up @@ -449,5 +435,23 @@ pub mod LyricsFlip {
self.accesscontrol._revoke_role(role, recipient);
}
}

fn _is_round_player(
self: @ContractState, round_id: u64, player_address: ContractAddress,
) -> bool {
let round_players = self.get_round_players(round_id);
let mut is_player = false;
let mut idx = 0;
while (idx < round_players.len()) {
if *round_players.at(idx) == player_address {
is_player = true;
break;
}
idx += 1;
};
is_player
}
//TODO
// fn _build_question_card(self: @ContractState, card: Card) -> QuestionCard<T> {}
}
}
21 changes: 8 additions & 13 deletions onchain/src/interfaces/lyricsflip.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,21 @@ pub trait ILyricsFlip<TContractState> {
fn get_round_cards(self: @TContractState, round_id: u64) -> Span<u64>;
fn get_round_players(self: @TContractState, round_id: u64) -> Span<ContractAddress>;
fn get_players_round_count(self: @TContractState, round_id: u64) -> u256;
fn is_round_player(
self: @TContractState, round_id: u64, player_address: ContractAddress,
) -> bool;
fn get_cards_per_round(self: @TContractState) -> u8;
fn get_card(self: @TContractState, card_id: u64) -> Card;
fn get_cards_of_genre(self: @TContractState, genre: Genre, seed: u64) -> Span<Card>;
fn get_cards_of_artist(self: @TContractState, artist: felt252, seed: u64) -> Span<Card>;
fn get_cards_of_a_year(self: @TContractState, year: u64, seed: u64) -> Span<Card>;

fn create_round(ref self: TContractState, genre: Option<Genre>, seed: u64) -> u64;
fn start_round(ref self: TContractState, round_id: u64);
fn join_round(ref self: TContractState, round_id: u64);
fn set_cards_per_round(ref self: TContractState, value: u8);
fn get_cards_per_round(self: @TContractState) -> u8;
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<Card>;
fn next_card(ref self: TContractState, round_id: u64) -> Card;
fn is_admin(self: @TContractState, role: felt252, address: ContractAddress) -> bool;

// TODO
fn get_cards_of_artist(self: @TContractState, artist: felt252, seed: u64) -> Span<Card>;
fn set_cards_per_round(ref self: TContractState, value: u8);
fn add_card(ref self: TContractState, card: Card);
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<Card>;
}
4 changes: 1 addition & 3 deletions onchain/src/tests/test_lyricsflip.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ fn test_set_role() {
}

#[test]
#[should_panic]
#[should_panic(expected: "role not enable")]
fn test_set_role_should_panic_when_invalid_role_is_passed() {
let lyricsflip = deploy();
let mut spy = spy_events();
Expand Down Expand Up @@ -192,7 +192,6 @@ fn test_start_round() {

assert(lyricsflip.get_players_round_count(round_id) == 1, 'wrong players count');
assert(*round_players.at(0) == PLAYER_1(), 'wrong player address');
assert(lyricsflip.is_round_player(round_id, PLAYER_1()), 'wrong is_player value');

spy
.assert_emitted(
Expand Down Expand Up @@ -275,7 +274,6 @@ fn test_join_round() {

assert(lyricsflip.get_players_round_count(round_id) == 2, 'wrong players count');
assert(*round_players.at(1) == PLAYER_2(), 'wrong player address');
assert(lyricsflip.is_round_player(round_id, PLAYER_2()), 'wrong is_player value');
}

#[test]
Expand Down
17 changes: 17 additions & 0 deletions onchain/src/utils/types.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ impl Felt252TryIntoGenre of TryInto<felt252, Genre> {
// pub enum Mode {
// }

#[derive(Drop, Copy, Serde, starknet::Store)]
pub enum Difficulty {
Easy,
Medium,
Hard,
}

#[derive(Drop, Copy, Serde, starknet::Store)]
pub struct Round {
pub round_id: u64,
Expand All @@ -107,3 +114,13 @@ pub struct Round {
pub end_time: u64,
pub next_card_index: u8,
}

#[derive(Drop, Clone, Serde)]
pub struct QuestionCard<T> {
pub card_id: u64,
pub lyric: ByteArray,
pub option_one: T,
pub option_two: T,
pub option_three: T,
pub option_four: T,
}

0 comments on commit 4b52bc8

Please sign in to comment.