diff --git a/src/achievements/achievement.cairo b/src/achievements/achievement.cairo index 62a1b6b..71ba546 100644 --- a/src/achievements/achievement.cairo +++ b/src/achievements/achievement.cairo @@ -1,6 +1,5 @@ // Mock Task module for demonstration (replace with actual import if available) - // Import the Task and TaskTrait (adjust path based on your project structure) use dojo_starter::types::task::{Task, TaskTrait}; @@ -115,11 +114,26 @@ pub impl SnookerAchievementImpl of SnookerAchievementTrait { fn tasks(self: SnookerAchievement) -> Span { match self { SnookerAchievement::None => array![].span(), // Explicitly empty array - SnookerAchievement::FirstPot => array![TaskTrait::new('First Pot', 1, "Pot a ball in a game")].span(), - SnookerAchievement::BreakMaster => array![TaskTrait::new('Break Master', 1, "Score a break of 50+")].span(), - SnookerAchievement::CenturyMaker => array![TaskTrait::new('Century Maker', 1, "Score a century break")].span(), - SnookerAchievement::MaximumBreak => array![TaskTrait::new('Maximum Break', 1, "Score a 147 break")].span(), - SnookerAchievement::LegendShot => array![TaskTrait::new('Legend Shot', 10, "Win 10 frames with a century")].span(), + SnookerAchievement::FirstPot => array![ + TaskTrait::new('First Pot', 1, "Pot a ball in a game"), + ] + .span(), + SnookerAchievement::BreakMaster => array![ + TaskTrait::new('Break Master', 1, "Score a break of 50+"), + ] + .span(), + SnookerAchievement::CenturyMaker => array![ + TaskTrait::new('Century Maker', 1, "Score a century break"), + ] + .span(), + SnookerAchievement::MaximumBreak => array![ + TaskTrait::new('Maximum Break', 1, "Score a 147 break"), + ] + .span(), + SnookerAchievement::LegendShot => array![ + TaskTrait::new('Legend Shot', 10, "Win 10 frames with a century"), + ] + .span(), } } @@ -166,4 +180,4 @@ pub impl IntoU8SnookerAchievement of Into { _ => SnookerAchievement::None, } } -} \ No newline at end of file +} diff --git a/src/errors/errors.cairo b/src/errors/errors.cairo index 5a58513..d333248 100644 --- a/src/errors/errors.cairo +++ b/src/errors/errors.cairo @@ -57,7 +57,6 @@ impl SnooknetErrorIntoFelt252 of Into { SnooknetError::MatchNotPending => 'Match Not Pending', SnooknetError::MatchNotInProgress => 'Match Not In Progress', SnooknetError::MatchNotInProgressOrPaused => 'Match Not In Progress Or Paused', - } } } diff --git a/src/interfaces/ISnooknet.cairo b/src/interfaces/ISnooknet.cairo index da1dc13..15972a7 100644 --- a/src/interfaces/ISnooknet.cairo +++ b/src/interfaces/ISnooknet.cairo @@ -13,14 +13,14 @@ pub trait ISnooknet { fn end_match(ref self: T, game_id: u256, winner: ContractAddress); fn retrieve_game(ref self: T, game_id: u256) -> Game; - + fn join_tournament(ref self: T, tournament_id: u256); fn start_match(ref self: T, game_id: u256) -> bool; // Pause an ongoing match, updating its status fn pause_match(ref self: T, game_id: u256) -> bool; - // fn pot_ball(ref self: T, game_id: u256, ball_type: felt252, is_cue_foul: bool); - // End a match, setting the winner and finalizing the game + // fn pot_ball(ref self: T, game_id: u256, ball_type: felt252, is_cue_foul: bool); +// End a match, setting the winner and finalizing the game // fn mint_nft(ref self: T, asset_type: AssetType, rarity: Rarity) -> u256; // fn lease_nft(ref self: T, asset_id: u256, leasee: ContractAddress); // fn submit_proposal(ref self: T, proposal_id: u256); diff --git a/src/interfaces/ITournaments.cairo b/src/interfaces/ITournaments.cairo index a10bfe0..f297fef 100644 --- a/src/interfaces/ITournaments.cairo +++ b/src/interfaces/ITournaments.cairo @@ -11,14 +11,14 @@ pub trait ITournaments { max_players: u8, start_date: u64, end_date: u64, - rewards: Array + rewards: Array, ) -> u256; fn register_player(ref self: T, tournament_id: u256); fn start_tournament(ref self: T, tournament_id: u256, end_date: u64); fn end_tournament(ref self: T, tournament_id: u256); fn cancel_tournament(ref self: T, tournament_id: u256); fn is_player_registered(self: @T, tournament_id: u256, player: ContractAddress) -> bool; - fn update_player_stats( + fn update_player_stats( ref self: T, tournament_id: u256, player: ContractAddress, @@ -28,7 +28,7 @@ pub trait ITournaments { lost: bool, nft_coins_delta: u256, level_delta: u32, - tournament_won: bool + tournament_won: bool, ); fn update_match_stats( ref self: T, @@ -36,6 +36,6 @@ pub trait ITournaments { player: ContractAddress, won: bool, xp_delta: u256, - elo_delta: u256 + elo_delta: u256, ); -} \ No newline at end of file +} diff --git a/src/lib.cairo b/src/lib.cairo index 4f5105f..cbfe018 100644 --- a/src/lib.cairo +++ b/src/lib.cairo @@ -36,7 +36,8 @@ pub mod types { pub mod achievements { pub mod achievement; } - // pub mod tokens { // pub mod mock_erc20; // } + + diff --git a/src/model/game_model.cairo b/src/model/game_model.cairo index 8d476c1..3ec7166 100644 --- a/src/model/game_model.cairo +++ b/src/model/game_model.cairo @@ -83,13 +83,21 @@ pub struct Game { pub trait GameTrait { // Create and return a new game with default snooker setup fn new( - id: u256,tournament_id: u256, player1: ContractAddress, player2: ContractAddress, stake_amount: u256, + id: u256, + tournament_id: u256, + player1: ContractAddress, + player2: ContractAddress, + stake_amount: u256, ) -> Game; } impl GameImpl of GameTrait { fn new( - id: u256, tournament_id: u256, player1: ContractAddress, player2: ContractAddress, stake_amount: u256, + id: u256, + tournament_id: u256, + player1: ContractAddress, + player2: ContractAddress, + stake_amount: u256, ) -> Game { let zero_address = contract_address_const::<0x0>(); let current_time = get_block_timestamp(); diff --git a/src/model/new_tourn_models.cairo b/src/model/new_tourn_models.cairo index 7baceef..0369ceb 100644 --- a/src/model/new_tourn_models.cairo +++ b/src/model/new_tourn_models.cairo @@ -75,7 +75,7 @@ impl TournamentImpl of TournamentTrait { end_date, status: TournamentStatus::Pending, rewards, - players: ArrayTrait::new() + players: ArrayTrait::new(), } } } diff --git a/src/model/player_model.cairo b/src/model/player_model.cairo index 23dd97d..fe48faa 100644 --- a/src/model/player_model.cairo +++ b/src/model/player_model.cairo @@ -1,6 +1,6 @@ use starknet::ContractAddress; -#[derive(Copy, Drop, Serde, Debug)] +#[derive(Copy, Drop, Serde, Debug, Introspect, PartialEq)] #[dojo::model] pub struct Player { #[key] @@ -25,7 +25,7 @@ pub trait PlayerTrait { games_lost: u64, nft_coins_available: u256, level: u32, - tournaments_won: u64, // Added parameter + tournaments_won: u64 // Added parameter ) -> Player; } @@ -39,7 +39,7 @@ impl PlayerImpl of PlayerTrait { games_lost: u64, nft_coins_available: u256, level: u32, - tournaments_won: u64, // Added parameter + tournaments_won: u64 // Added parameter ) -> Player { Player { contract_address, @@ -50,7 +50,7 @@ impl PlayerImpl of PlayerTrait { games_lost, nft_coins_available, level, - tournaments_won, // Initialize new field + tournaments_won // Initialize new field } } } @@ -86,7 +86,9 @@ mod tests { ); assert(player.contract_address == contract_address, 'Contract address mismatch'); - assert(player.leaderboard_position == leaderboard_position, 'Leaderboard position mismatch'); + assert( + player.leaderboard_position == leaderboard_position, 'Leaderboard position mismatch', + ); assert(player.xp == xp, 'XP mismatch'); assert(player.elo_rating == elo_rating, 'Elo rating mismatch'); assert(player.games_won == games_won, 'Games won mismatch'); @@ -124,7 +126,15 @@ mod tests { let max_u32 = 4294967295_u32; let player = PlayerImpl::new( - contract_address, max_u64, max_u256, max_u256, max_u64, max_u64, max_u256, max_u32, max_u64, + contract_address, + max_u64, + max_u256, + max_u256, + max_u64, + max_u64, + max_u256, + max_u32, + max_u64, ); assert(player.contract_address == contract_address, 'Contract address mismatch'); @@ -137,4 +147,4 @@ mod tests { assert(player.level == max_u32, 'Level should be max'); assert(player.tournaments_won == max_u64, 'Tournaments won should be max'); // Added } -} \ No newline at end of file +} diff --git a/src/model/skiil.cairo b/src/model/skiil.cairo index e69de29..8b13789 100644 --- a/src/model/skiil.cairo +++ b/src/model/skiil.cairo @@ -0,0 +1 @@ + diff --git a/src/systems/Snooknet.cairo b/src/systems/Snooknet.cairo index d079153..eaf2782 100644 --- a/src/systems/Snooknet.cairo +++ b/src/systems/Snooknet.cairo @@ -6,7 +6,7 @@ use dojo::event::EventStorage; use dojo_starter::interfaces::ISnooknet::ISnooknet; use dojo_starter::model::game_model::{ - Game, GameTrait, GameState, GameCounter, MatchStatus, BallState, GameIndex,GameScore + Game, GameTrait, GameState, GameCounter, MatchStatus, BallState, GameIndex, GameScore, }; use dojo_starter::model::new_tourn_models::{ Tournament as TournamentModel, TournamentTrait, TournamentStatus, TournamentReward, @@ -81,16 +81,17 @@ pub mod Snooknet { // world.write_model(@new_player); - // world.emit_event(@PlayerCreated { player: caller, timestamp: get_block_timestamp() }); + // world.emit_event(@PlayerCreated { player: caller, timestamp: get_block_timestamp() + // }); // } -fn create_player(ref self: ContractState) { - let mut world = self.world_default(); - let caller: ContractAddress = get_caller_address(); - let new_player: Player = PlayerTrait::new(caller, 0, 0, 0, 0, 0, 0, 1, 0); - world.write_model(@new_player); - world.emit_event(@PlayerCreated { player: caller, timestamp: get_block_timestamp() }); - } + fn create_player(ref self: ContractState) { + let mut world = self.world_default(); + let caller: ContractAddress = get_caller_address(); + let new_player: Player = PlayerTrait::new(caller, 0, 0, 0, 0, 0, 0, 1, 0); + world.write_model(@new_player); + world.emit_event(@PlayerCreated { player: caller, timestamp: get_block_timestamp() }); + } fn create_new_game_id(ref self: ContractState) -> u256 { let mut world = self.world_default(); @@ -133,35 +134,35 @@ fn create_player(ref self: ContractState) { // game_id // } -fn create_match( - ref self: ContractState, opponent: ContractAddress, stake_amount: u256, - ) -> u256 { - let mut world = self.world_default(); - let game_id = self.create_new_game_id(); - let timestamp = get_block_timestamp(); - let player_1 = get_caller_address(); + fn create_match( + ref self: ContractState, opponent: ContractAddress, stake_amount: u256, + ) -> u256 { + let mut world = self.world_default(); + let game_id = self.create_new_game_id(); + let timestamp = get_block_timestamp(); + let player_1 = get_caller_address(); - assert(player_1 != opponent, SnooknetError::PlayersCannotBeSame.into()); - assert(stake_amount > 0, SnooknetError::StakeMustBePositive.into()); + assert(player_1 != opponent, SnooknetError::PlayersCannotBeSame.into()); + assert(stake_amount > 0, SnooknetError::StakeMustBePositive.into()); - let mut new_game: Game = GameTrait::new(game_id, 0, player_1, opponent, stake_amount); + let mut new_game: Game = GameTrait::new(game_id, 0, player_1, opponent, stake_amount); - new_game.current_turn = player_1; - new_game.red_balls_remaining = 15; - new_game.state = GameState::NotStarted; - new_game.winner = contract_address_const::<0x0>(); + new_game.current_turn = player_1; + new_game.red_balls_remaining = 15; + new_game.state = GameState::NotStarted; + new_game.winner = contract_address_const::<0x0>(); - world.write_model(@new_game); + world.write_model(@new_game); - let index1 = GameIndex { player: player_1, game_id, created_at: timestamp }; - world.write_model(@index1); + let index1 = GameIndex { player: player_1, game_id, created_at: timestamp }; + world.write_model(@index1); - let index2 = GameIndex { player: opponent, game_id, created_at: timestamp }; - world.write_model(@index2); + let index2 = GameIndex { player: opponent, game_id, created_at: timestamp }; + world.write_model(@index2); - world.emit_event(@GameCreated { game_id, timestamp }); - game_id - } + world.emit_event(@GameCreated { game_id, timestamp }); + game_id + } fn retrieve_game(ref self: ContractState, game_id: u256) -> Game { // Get default world @@ -172,7 +173,6 @@ fn create_match( } - fn join_tournament(ref self: ContractState, tournament_id: u256) { // Get the caller's address let caller = get_caller_address(); @@ -264,90 +264,94 @@ fn create_match( world.emit_event(@Winner { game_id, winner }); world.emit_event(@GameEnded { game_id, timestamp }); } - - // fn pot_ball(ref self: ContractState, game_id: u256, ball_type: felt252, is_cue_foul: bool) { - // let mut world = self.world_default(); - // let mut game: Game = world.read_model(game_id); - // let mut score: GameScore = world.read_model(game_id); - // let caller = get_caller_address(); + // fn pot_ball(ref self: ContractState, game_id: u256, ball_type: felt252, is_cue_foul: + // bool) { + // let mut world = self.world_default(); + // let mut game: Game = world.read_model(game_id); + // let mut score: GameScore = world.read_model(game_id); + // let caller = get_caller_address(); // assert(caller == game.current_turn, SnooknetError::NotAPlayer.into()); - // assert(game.state == GameState::InProgress, SnooknetError::GameNotInProgress.into()); + // assert(game.state == GameState::InProgress, SnooknetError::GameNotInProgress.into()); // let timestamp = get_block_timestamp(); - // let mut points = 0; + // let mut points = 0; // if is_cue_foul { - // game.cue_ball_fouled = true; - // let opponent = if game.current_turn == game.player1 { game.player2 } else { game.player1 }; - // if opponent == game.player1 { - // score.player1_score += 4; // Minimum foul penalty - // } else { - // score.player2_score += 4; - // } - // score.last_foul = timestamp; - // } else if ball_type == "red" && game.red_balls_remaining > 0 { - // game.red_balls_remaining -= 1; - // points = 1; - // } else if game.red_balls_remaining == 0 { - // let yellow_state = world.read_model::((game_id, "yellow")); - // let green_state = world.read_model::((game_id, "green")); - // let brown_state = world.read_model::((game_id, "brown")); - // let blue_state = world.read_model::((game_id, "blue")); - // let pink_state = world.read_model::((game_id, "pink")); - // let black_state = world.read_model::((game_id, "black")); + // game.cue_ball_fouled = true; + // let opponent = if game.current_turn == game.player1 { game.player2 } else { + // game.player1 }; + // if opponent == game.player1 { + // score.player1_score += 4; // Minimum foul penalty + // } else { + // score.player2_score += 4; + // } + // score.last_foul = timestamp; + // } else if ball_type == "red" && game.red_balls_remaining > 0 { + // game.red_balls_remaining -= 1; + // points = 1; + // } else if game.red_balls_remaining == 0 { + // let yellow_state = world.read_model::((game_id, "yellow")); + // let green_state = world.read_model::((game_id, "green")); + // let brown_state = world.read_model::((game_id, "brown")); + // let blue_state = world.read_model::((game_id, "blue")); + // let pink_state = world.read_model::((game_id, "pink")); + // let black_state = world.read_model::((game_id, "black")); // if ball_type == "yellow" && !yellow_state.is_potted { - // points = 2; - // } else if ball_type == "green" && !green_state.is_potted { - // points = 3; - // } else if ball_type == "brown" && !brown_state.is_potted { - // points = 4; - // } else if ball_type == "blue" && !blue_state.is_potted { - // points = 5; - // } else if ball_type == "pink" && !pink_state.is_potted { - // points = 6; - // } else if ball_type == "black" && !black_state.is_potted { - // points = 7; - // } else { - // assert(false, SnooknetError::InvalidBallType.into()); - // } - // let mut ball = BallState { game_id, ball_type, is_potted: true, last_updated: timestamp }; - // world.write_model(@ball); - // } else { - // assert(false, SnooknetError::InvalidBallType.into()); - // } + // points = 2; + // } else if ball_type == "green" && !green_state.is_potted { + // points = 3; + // } else if ball_type == "brown" && !brown_state.is_potted { + // points = 4; + // } else if ball_type == "blue" && !blue_state.is_potted { + // points = 5; + // } else if ball_type == "pink" && !pink_state.is_potted { + // points = 6; + // } else if ball_type == "black" && !black_state.is_potted { + // points = 7; + // } else { + // assert(false, SnooknetError::InvalidBallType.into()); + // } + // let mut ball = BallState { game_id, ball_type, is_potted: true, last_updated: + // timestamp }; + // world.write_model(@ball); + // } else { + // assert(false, SnooknetError::InvalidBallType.into()); + // } // if points > 0 { - // if game.current_turn == game.player1 { - // score.player1_score += points; - // } else { - // score.player2_score += points; - // } - // } + // if game.current_turn == game.player1 { + // score.player1_score += points; + // } else { + // score.player2_score += points; + // } + // } // game.updated_at = timestamp; - // world.write_model(@game); - // world.write_model(@score); + // world.write_model(@game); + // world.write_model(@score); // if points == 0 || is_cue_foul { - // game.current_turn = if game.current_turn == game.player1 { game.player2 } else { game.player1 }; - // world.write_model(@game); - // } + // game.current_turn = if game.current_turn == game.player1 { game.player2 } else { + // game.player1 }; + // world.write_model(@game); + // } // if game.red_balls_remaining == 0 && - // yellow_state.is_potted && - // green_state.is_potted && - // brown_state.is_potted && - // blue_state.is_potted && - // pink_state.is_potted && - // black_state.is_potted { - // game.state = GameState::Finished; - // game.winner = if score.player1_score > score.player2_score { game.player1 } else { game.player2 }; - // world.write_model(@game); - // world.emit_event(@GameEnded { game_id, timestamp }); - // } - // } + // yellow_state.is_potted && + // green_state.is_potted && + // brown_state.is_potted && + // blue_state.is_potted && + // pink_state.is_potted && + // black_state.is_potted { + // game.state = GameState::Finished; + // game.winner = if score.player1_score > score.player2_score { game.player1 } else + // { game.player2 }; + // world.write_model(@game); + // world.emit_event(@GameEnded { game_id, timestamp }); + // } + // } } @@ -370,5 +374,3 @@ fn create_match( } } - - diff --git a/src/systems/player.cairo b/src/systems/player.cairo index 991abde..90a9e20 100644 --- a/src/systems/player.cairo +++ b/src/systems/player.cairo @@ -17,10 +17,12 @@ // } // #[external(v0)] -// fn create_player(ref self: ContractState, leaderboard_position: u64, xp: u256, elo_rating: u256, games_won: u64, games_lost: u64, nft_coins_available: u256, level: u32) { +// fn create_player(ref self: ContractState, leaderboard_position: u64, xp: u256, elo_rating: +// u256, games_won: u64, games_lost: u64, nft_coins_available: u256, level: u32) { // let world = self.world.read(); // let player_addr = get_caller_address(); -// let player = PlayerImpl::new(player_addr, leaderboard_position, xp, elo_rating, games_won, games_lost, nft_coins_available, level); +// let player = PlayerImpl::new(player_addr, leaderboard_position, xp, elo_rating, +// games_won, games_lost, nft_coins_available, level); // set!(world, (player)); // } @@ -30,7 +32,8 @@ // let player_addr = get_caller_address(); // let mut player = get!(world, player_addr, (Player)); // assert(player.contract_address == player_addr, 'Invalid player'); -// set!(world, (Player { contract_address: player_addr, leaderboard_position: 0, xp: 0, elo_rating: 0, games_won: 0, games_lost: 0, nft_coins_available: 0, level: 0 })); +// set!(world, (Player { contract_address: player_addr, leaderboard_position: 0, xp: 0, +// elo_rating: 0, games_won: 0, games_lost: 0, nft_coins_available: 0, level: 0 })); // } // } @@ -45,7 +48,8 @@ // #[available_gas(2000000)] // fn test_create_player() { // let world = spawn_test_world(array![get_model_selector::()].span()); -// let contract_address = deploy_contract(Players::TEST_CLASS_HASH, array![world.contract_address.into()].span()); +// let contract_address = deploy_contract(Players::TEST_CLASS_HASH, +// array![world.contract_address.into()].span()); // let caller = contract_address_const::<'player_1'>(); // set_caller_address(caller); @@ -67,7 +71,8 @@ // #[available_gas(2000000)] // fn test_delete_player() { // let world = spawn_test_world(array![get_model_selector::()].span()); -// let contract_address = deploy_contract(Players::TEST_CLASS_HASH, array![world.contract_address.into()].span()); +// let contract_address = deploy_contract(Players::TEST_CLASS_HASH, +// array![world.contract_address.into()].span()); // let caller = contract_address_const::<'player_1'>(); // set_caller_address(caller); @@ -84,4 +89,4 @@ // assert(player.nft_coins_available == 0_u256, 'NFT coins should be 0'); // assert(player.level == 0_u32, 'Level should be 0'); // } -// } \ No newline at end of file +// } diff --git a/src/systems/snooker.cairo b/src/systems/snooker.cairo index f33b056..a56569f 100644 --- a/src/systems/snooker.cairo +++ b/src/systems/snooker.cairo @@ -7,7 +7,8 @@ // use dojo_starter::types::tournament_types::SnookerTournament; // use dojo_starter::errors::errors::SnooknetError; // use dojo_starter::model::player_model::{Player, PlayerTrait}; -// use dojo_starter::model::game_model::{Game, GameTrait, GameState, MatchStatus, GameCounter, GameIndex, GameScore, BallState}; +// use dojo_starter::model::game_model::{Game, GameTrait, GameState, MatchStatus, GameCounter, +// GameIndex, GameScore, BallState}; // use dojo_starter::interfaces::ITournaments::ITournaments; // use dojo_starter::interfaces::ISnooknet::ISnooknet; // use dojo_starter::types::task::{Task, TaskTrait}; @@ -110,14 +111,16 @@ // world.has_model(player) // } -// fn validate_match_participant(self: @ContractState, game: @Game, caller: ContractAddress) { +// fn validate_match_participant(self: @ContractState, game: @Game, caller: ContractAddress) +// { // assert( // *game.player1 == caller || *game.player2 == caller, // SnooknetError::NotMatchParticipant.into() // ); // } -// fn check_achievements(ref self: ContractState, player: ContractAddress, game_id: u256, break_score: u32) { +// fn check_achievements(ref self: ContractState, player: ContractAddress, game_id: u256, +// break_score: u32) { // let mut world = self.world_default(); // let mut player_data: Player = world.read_model(player); // let timestamp = get_block_timestamp(); @@ -163,10 +166,12 @@ // let player = PlayerTrait::new(caller, 0, 0, 1500, 0, 0, 0, 1, 0); // world.write_model(@player); -// world.emit_event(@PlayerCreated { player: caller, timestamp: get_block_timestamp() }); +// world.emit_event(@PlayerCreated { player: caller, timestamp: get_block_timestamp() +// }); // } -// fn create_match(ref self: ContractState, opponent: ContractAddress, stake_amount: u256) -> u256 { +// fn create_match(ref self: ContractState, opponent: ContractAddress, stake_amount: u256) +// -> u256 { // let mut world = self.world_default(); // let caller = get_caller_address(); // let timestamp = get_block_timestamp(); @@ -183,7 +188,8 @@ // // Initialize game indices and score // let game_index1 = GameIndex { player: caller, game_id, created_at: timestamp }; // let game_index2 = GameIndex { player: opponent, game_id, created_at: timestamp }; -// let game_score = GameScore { game_id, player1_score: 0, player2_score: 0, last_foul: 0 }; +// let game_score = GameScore { game_id, player1_score: 0, player2_score: 0, last_foul: +// 0 }; // // Initialize ball states // let ball_types = array!["red", "yellow", "green", "brown", "blue", "pink", "black"]; @@ -232,7 +238,8 @@ // let caller = get_caller_address(); // self.validate_match_participant(@game, caller); -// assert(game.match_status == MatchStatus::Pending, SnooknetError::MatchNotPending.into()); +// assert(game.match_status == MatchStatus::Pending, +// SnooknetError::MatchNotPending.into()); // game.current_turn = game.player1; // game.match_status = MatchStatus::Ongoing; @@ -252,7 +259,8 @@ // let caller = get_caller_address(); // self.validate_match_participant(@game, caller); -// assert(game.match_status == MatchStatus::Ongoing, SnooknetError::MatchNotInProgress.into()); +// assert(game.match_status == MatchStatus::Ongoing, +// SnooknetError::MatchNotInProgress.into()); // game.match_status = MatchStatus::Paused; // game.updated_at = get_block_timestamp(); @@ -271,8 +279,8 @@ // self.validate_match_participant(@game, caller); // assert( -// game.match_status == MatchStatus::Ongoing || game.match_status == MatchStatus::Paused, -// SnooknetError::MatchNotInProgressOrPaused.into() +// game.match_status == MatchStatus::Ongoing || game.match_status == +// MatchStatus::Paused, SnooknetError::MatchNotInProgressOrPaused.into() // ); // assert( // winner == game.player1 || winner == game.player2, @@ -307,13 +315,15 @@ // // Check achievements based on game score // let game_score: GameScore = world.read_model(game_id); -// let break_score = if winner == game.player1 { game_score.player1_score } else { game_score.player2_score }; +// let break_score = if winner == game.player1 { game_score.player1_score } else { +// game_score.player2_score }; // self.check_achievements(winner, game_id, break_score); // // Update tournament stats // if game.tournament_id != 0 { // let tournament: Tournament = world.read_model(game.tournament_id); -// assert(tournament.status == TournamentStatus::Active, SnooknetError::TournamentNotInProgress.into()); +// assert(tournament.status == TournamentStatus::Active, +// SnooknetError::TournamentNotInProgress.into()); // let mut tournament_contract = self.world_default(); // tournament_contract.execute_selector( // @TournamentContract::update_match_stats, @@ -343,8 +353,10 @@ // assert(self.validate_player(caller), SnooknetError::PlayerNotRegistered.into()); // let tournament: Tournament = world.read_model(tournament_id); -// assert(tournament.status == TournamentStatus::Pending, SnooknetError::TournamentNotOpen.into()); -// assert(tournament.current_players < tournament.max_players, SnooknetError::TournamentIsFull.into()); +// assert(tournament.status == TournamentStatus::Pending, +// SnooknetError::TournamentNotOpen.into()); +// assert(tournament.current_players < tournament.max_players, +// SnooknetError::TournamentIsFull.into()); // let mut tournament_contract = self.world_default(); // tournament_contract.execute_selector( @@ -354,7 +366,8 @@ // }} // } -// // fn pot_ball(ref self: ContractState, game_id: u256, ball_type: felt252, is_cue_foul: bool) { +// // fn pot_ball(ref self: ContractState, game_id: u256, ball_type: felt252, is_cue_foul: +// bool) { // // let mut world = self.world_default(); // // let mut game: Game = world.read_model(game_id); // // let mut score: GameScore = world.read_model(game_id); @@ -363,7 +376,8 @@ // // self.validate_match_participant(@game, caller); // // assert(caller == game.current_turn, SnooknetError::NotYourTurn.into()); -// // assert(game.match_status == MatchStatus::Ongoing, SnooknetError::MatchNotInProgress.into()); +// // assert(game.match_status == MatchStatus::Ongoing, +// SnooknetError::MatchNotInProgress.into()); // // let mut points = 0_u32; // // let mut ball_state: BallState = world.read_model((game_id, ball_type)); @@ -371,7 +385,8 @@ // // if is_cue_foul { // // game.cue_ball_fouled = true; // // score.last_foul = timestamp; -// // let opponent = if caller == game.player1 { game.player2 } else { game.player1 }; +// // let opponent = if caller == game.player1 { game.player2 } else { game.player1 +// }; // // if opponent == game.player1 { // // score.player1_score += 4; // Minimum foul penalty // // } else { @@ -430,15 +445,17 @@ // // score.player2_score // // }); // // } else if points == 0 || is_cue_foul { -// // game.current_turn = if caller == game.player1 { game.player2 } else { game.player1 }; +// // game.current_turn = if caller == game.player1 { game.player2 } else { +// game.player1 }; // // } // // world.write_model(@game); // // world.write_model(@score); // // if !is_cue_foul { // // world.write_model(@ball_state); -// // world.emit_event(@BallPotted { game_id, player: caller, ball_type, points, timestamp }); +// // world.emit_event(@BallPotted { game_id, player: caller, ball_type, points, +// timestamp }); // // } // // } // // } -// // } \ No newline at end of file +// // } diff --git a/src/systems/tournament.cairo b/src/systems/tournament.cairo index dcd46f7..986fe74 100644 --- a/src/systems/tournament.cairo +++ b/src/systems/tournament.cairo @@ -2,7 +2,7 @@ use starknet::{ContractAddress, get_caller_address, get_block_timestamp}; use dojo::model::{ModelStorage}; use dojo::event::EventStorage; use dojo_starter::model::new_tourn_models::{ - Tournament, TournamentTrait, TournamentStatus, TournamentReward, TournamentCounter + Tournament, TournamentTrait, TournamentStatus, TournamentReward, TournamentCounter, }; use dojo_starter::types::tournament_types::SnookerTournament; use dojo_starter::errors::errors::SnooknetError; @@ -93,7 +93,7 @@ pub mod TournamentContract { max_players: u8, start_date: u64, end_date: u64, - rewards: Array + rewards: Array, ) -> u256 { let mut world = self.world_default(); let tournament_id = self.create_new_tournament_id(); @@ -103,7 +103,8 @@ pub mod TournamentContract { assert(start_date < end_date, SnooknetError::InvalidStartDate.into()); let organizer = get_caller_address(); - let tournament_type = SnookerTournament::Default; // Default type since not provided in interface + let tournament_type = + SnookerTournament::Default; // Default type since not provided in interface // Create new tournament let new_tournament: Tournament = TournamentTrait::new( @@ -119,15 +120,10 @@ pub mod TournamentContract { world.write_model(@new_tournament); - world.emit_event( - @TournamentCreated { - tournament_id, - name, - organizer, - start_date, - end_date, - } - ); + world + .emit_event( + @TournamentCreated { tournament_id, name, organizer, start_date, end_date }, + ); tournament_id } @@ -140,19 +136,19 @@ pub mod TournamentContract { // Check if tournament is open for joining assert( tournament.status == TournamentStatus::Pending, - SnooknetError::TournamentNotOpen.into() + SnooknetError::TournamentNotOpen.into(), ); // Check if tournament is full assert( tournament.current_players < tournament.max_players, - SnooknetError::TournamentIsFull.into() + SnooknetError::TournamentIsFull.into(), ); // Check if player is already registered assert( !self.is_player_registered(tournament_id, caller), - SnooknetError::PlayerAlreadyRegistered.into() + SnooknetError::PlayerAlreadyRegistered.into(), ); // Update tournament with new player @@ -170,22 +166,16 @@ pub mod TournamentContract { let caller = get_caller_address(); // Check if caller is the organizer - assert( - caller == tournament.organizer, - SnooknetError::NotTournamentOrganizer.into() - ); + assert(caller == tournament.organizer, SnooknetError::NotTournamentOrganizer.into()); // Check if tournament is pending assert( tournament.status == TournamentStatus::Pending, - SnooknetError::TournamentNotPending.into() + SnooknetError::TournamentNotPending.into(), ); // Check if end_date is valid - assert( - end_date > get_block_timestamp(), - SnooknetError::InvalidEndDate.into() - ); + assert(end_date > get_block_timestamp(), SnooknetError::InvalidEndDate.into()); // Update tournament status and timestamps tournament.status = TournamentStatus::Active; @@ -194,9 +184,10 @@ pub mod TournamentContract { world.write_model(@tournament); - world.emit_event( - @TournamentStarted { tournament_id, start_date: tournament.start_date } - ); + world + .emit_event( + @TournamentStarted { tournament_id, start_date: tournament.start_date }, + ); } fn end_tournament(ref self: ContractState, tournament_id: u256) { @@ -205,15 +196,12 @@ pub mod TournamentContract { let caller = get_caller_address(); // Check if caller is the organizer - assert( - caller == tournament.organizer, - SnooknetError::NotTournamentOrganizer.into() - ); + assert(caller == tournament.organizer, SnooknetError::NotTournamentOrganizer.into()); // Check if tournament is in progress assert( tournament.status == TournamentStatus::Active, - SnooknetError::TournamentNotInProgress.into() + SnooknetError::TournamentNotInProgress.into(), ); // Update tournament status and timestamp @@ -222,9 +210,7 @@ pub mod TournamentContract { world.write_model(@tournament); - world.emit_event( - @TournamentEnded { tournament_id, end_date: tournament.end_date } - ); + world.emit_event(@TournamentEnded { tournament_id, end_date: tournament.end_date }); } fn cancel_tournament(ref self: ContractState, tournament_id: u256) { @@ -233,15 +219,12 @@ pub mod TournamentContract { let caller = get_caller_address(); // Check if caller is the organizer - assert( - caller == tournament.organizer, - SnooknetError::NotTournamentOrganizer.into() - ); + assert(caller == tournament.organizer, SnooknetError::NotTournamentOrganizer.into()); // Check if tournament is pending assert( tournament.status == TournamentStatus::Pending, - SnooknetError::TournamentNotPending.into() + SnooknetError::TournamentNotPending.into(), ); // Update tournament status @@ -249,12 +232,15 @@ pub mod TournamentContract { world.write_model(@tournament); - world.emit_event( - @TournamentCanceled { tournament_id, timestamp: get_block_timestamp() } - ); + world + .emit_event( + @TournamentCanceled { tournament_id, timestamp: get_block_timestamp() }, + ); } - fn is_player_registered(self: @ContractState, tournament_id: u256, player: ContractAddress) -> bool { + fn is_player_registered( + self: @ContractState, tournament_id: u256, player: ContractAddress, + ) -> bool { let world = self.world_default(); let tournament: Tournament = world.read_model(tournament_id); let mut i = 0; @@ -277,28 +263,26 @@ pub mod TournamentContract { lost: bool, nft_coins_delta: u256, level_delta: u32, - tournament_won: bool + tournament_won: bool, ) { let mut world = self.world_default(); let tournament: Tournament = world.read_model(tournament_id); let caller = get_caller_address(); // Validate caller is the tournament organizer - assert( - caller == tournament.organizer, - SnooknetError::NotTournamentOrganizer.into() - ); + assert(caller == tournament.organizer, SnooknetError::NotTournamentOrganizer.into()); // Validate tournament is active or ended assert( - tournament.status == TournamentStatus::Active || tournament.status == TournamentStatus::Ended, - SnooknetError::TournamentNotInProgressOrEnded.into() + tournament.status == TournamentStatus::Active + || tournament.status == TournamentStatus::Ended, + SnooknetError::TournamentNotInProgressOrEnded.into(), ); // Validate player is registered in the tournament assert( self.is_player_registered(tournament_id, player), - SnooknetError::PlayerNotRegistered.into() + SnooknetError::PlayerNotRegistered.into(), ); // Read player model @@ -326,20 +310,21 @@ pub mod TournamentContract { world.write_model(@player_data); // Emit event - world.emit_event( - @PlayerStatsUpdated { - player, - tournament_id, - xp: player_data.xp, - elo_rating: player_data.elo_rating, - games_won: player_data.games_won, - games_lost: player_data.games_lost, - nft_coins_available: player_data.nft_coins_available, - level: player_data.level, - tournaments_won: player_data.tournaments_won, - timestamp: get_block_timestamp(), - } - ); + world + .emit_event( + @PlayerStatsUpdated { + player, + tournament_id, + xp: player_data.xp, + elo_rating: player_data.elo_rating, + games_won: player_data.games_won, + games_lost: player_data.games_lost, + nft_coins_available: player_data.nft_coins_available, + level: player_data.level, + tournaments_won: player_data.tournaments_won, + timestamp: get_block_timestamp(), + }, + ); } fn update_match_stats( @@ -348,28 +333,25 @@ pub mod TournamentContract { player: ContractAddress, won: bool, xp_delta: u256, - elo_delta: u256 + elo_delta: u256, ) { let mut world = self.world_default(); let tournament: Tournament = world.read_model(tournament_id); let caller = get_caller_address(); // Validate caller is the tournament organizer - assert( - caller == tournament.organizer, - SnooknetError::NotTournamentOrganizer.into() - ); + assert(caller == tournament.organizer, SnooknetError::NotTournamentOrganizer.into()); // Validate tournament is active assert( tournament.status == TournamentStatus::Active, - SnooknetError::TournamentNotInProgress.into() + SnooknetError::TournamentNotInProgress.into(), ); // Validate player is registered in the tournament assert( self.is_player_registered(tournament_id, player), - SnooknetError::PlayerNotRegistered.into() + SnooknetError::PlayerNotRegistered.into(), ); // Read player model @@ -391,19 +373,19 @@ pub mod TournamentContract { world.write_model(@player_data); // Emit event - world.emit_event( - @MatchStatsUpdated { - player, - tournament_id, - xp: player_data.xp, - elo_rating: player_data.elo_rating, - games_won: player_data.games_won, - games_lost: player_data.games_lost, - timestamp: get_block_timestamp(), - } - ); + world + .emit_event( + @MatchStatsUpdated { + player, + tournament_id, + xp: player_data.xp, + elo_rating: player_data.elo_rating, + games_won: player_data.games_won, + games_lost: player_data.games_lost, + timestamp: get_block_timestamp(), + }, + ); } - } #[generate_trait] @@ -421,4 +403,4 @@ pub mod TournamentContract { new_val } } -} \ No newline at end of file +} diff --git a/src/tokens/mock_erc20.cairo b/src/tokens/mock_erc20.cairo index f26647b..9f12770 100644 --- a/src/tokens/mock_erc20.cairo +++ b/src/tokens/mock_erc20.cairo @@ -1,4 +1,3 @@ - // use starknet::{ContractAddress, get_caller_address}; // #[starknet::interface] @@ -10,7 +9,8 @@ // fn balanceOf(self: @T, account: ContractAddress) -> u256; // fn allowance(self: @T, owner: ContractAddress, spender: ContractAddress) -> u256; // fn transfer(ref self: T, recipient: ContractAddress, amount: u256) -> bool; -// fn transferFrom(ref self: T, sender: ContractAddress, recipient: ContractAddress, amount: u256) -> bool; +// fn transferFrom(ref self: T, sender: ContractAddress, recipient: ContractAddress, amount: +// u256) -> bool; // fn approve(ref self: T, spender: ContractAddress, amount: u256) -> bool; // } @@ -59,7 +59,8 @@ // let caller = get_caller_address(); // self.total_supply.write(initial_supply); // self.balances.write(caller, initial_supply); -// self.emit(Transfer { from: starknet::contract_address_const::<0>(), to: caller, value: initial_supply }); +// self.emit(Transfer { from: starknet::contract_address_const::<0>(), to: caller, value: +// initial_supply }); // } // #[abi(embed_v0)] @@ -84,7 +85,8 @@ // self.balances.read(account) // } -// fn allowance(self: @ContractState, owner: ContractAddress, spender: ContractAddress) -> u256 { +// fn allowance(self: @ContractState, owner: ContractAddress, spender: ContractAddress) -> +// u256 { // self.allowances.read((owner, spender)) // } @@ -94,7 +96,8 @@ // true // } -// fn transferFrom(ref self: ContractState, sender: ContractAddress, recipient: ContractAddress, amount: u256) -> bool { +// fn transferFrom(ref self: ContractState, sender: ContractAddress, recipient: +// ContractAddress, amount: u256) -> bool { // let caller = get_caller_address(); // let allowance = self.allowances.read((sender, caller)); // assert(allowance >= amount, 'Insufficient allowance'); @@ -114,9 +117,12 @@ // #[generate_trait] // impl InternalImpl of InternalTrait { -// fn _transfer(ref self: ContractState, sender: ContractAddress, recipient: ContractAddress, amount: u256) { -// assert(sender != starknet::contract_address_const::<0>(), 'Transfer from zero address'); -// assert(recipient != starknet::contract_address_const::<0>(), 'Transfer to zero address'); +// fn _transfer(ref self: ContractState, sender: ContractAddress, recipient: +// ContractAddress, amount: u256) { +// assert(sender != starknet::contract_address_const::<0>(), 'Transfer from zero +// address'); +// assert(recipient != starknet::contract_address_const::<0>(), 'Transfer to zero +// address'); // let sender_balance = self.balances.read(sender); // assert(sender_balance >= amount, 'Insufficient balance'); // self.balances.write(sender, sender_balance - amount); @@ -124,4 +130,4 @@ // self.emit(Transfer { from: sender, to: recipient, value: amount }); // } // } -// } \ No newline at end of file +// } diff --git a/src/types/ball_type.cairo b/src/types/ball_type.cairo index 9571c49..3a9f427 100644 --- a/src/types/ball_type.cairo +++ b/src/types/ball_type.cairo @@ -54,4 +54,4 @@ impl IntoU8SnookerBall of Into { _ => SnookerBall::Undefined, } } -} \ No newline at end of file +} diff --git a/src/types/game_and_player_types.cairo b/src/types/game_and_player_types.cairo index 9714d5d..b2a7d08 100644 --- a/src/types/game_and_player_types.cairo +++ b/src/types/game_and_player_types.cairo @@ -104,7 +104,9 @@ pub impl IntoU8SnookerPlayerStatus of Into { } pub impl SnookerPlayerStatusDisplay of core::fmt::Display { - fn fmt(self: @SnookerPlayerStatus, ref f: core::fmt::Formatter) -> Result<(), core::fmt::Error> { + fn fmt( + self: @SnookerPlayerStatus, ref f: core::fmt::Formatter, + ) -> Result<(), core::fmt::Error> { let s = match self { SnookerPlayerStatus::Ready => "Ready", SnookerPlayerStatus::Playing => "Playing", @@ -315,4 +317,4 @@ mod tests { let player_status: SnookerPlayerStatus = player_status_u8.into(); assert_eq!(player_status, SnookerPlayerStatus::Inactive); } -} \ No newline at end of file +} diff --git a/src/types/rarity.cairo b/src/types/rarity.cairo index dbe0cfa..d0e0ad1 100644 --- a/src/types/rarity.cairo +++ b/src/types/rarity.cairo @@ -15,7 +15,8 @@ pub enum SnookerRarity { pub impl SnookerRarityImpl of SnookerRarityTrait { fn is_rare(self: @SnookerRarity) -> bool { match self { - SnookerRarity::Rare | SnookerRarity::VeryRare | SnookerRarity::Epic | SnookerRarity::Unique => true, + SnookerRarity::Rare | SnookerRarity::VeryRare | SnookerRarity::Epic | + SnookerRarity::Unique => true, _ => false, } } @@ -35,4 +36,4 @@ pub impl SnookerRarityDisplay of core::fmt::Display { f.buffer.append(@s); Result::Ok(()) } -} \ No newline at end of file +} diff --git a/src/types/skill_type.cairo b/src/types/skill_type.cairo index c98775b..f1a27ec 100644 --- a/src/types/skill_type.cairo +++ b/src/types/skill_type.cairo @@ -27,4 +27,4 @@ pub impl SnookerShotDisplay of core::fmt::Display { f.buffer.append(@s); Result::Ok(()) } -} \ No newline at end of file +} diff --git a/src/types/task.cairo b/src/types/task.cairo index 5456333..f2d2770 100644 --- a/src/types/task.cairo +++ b/src/types/task.cairo @@ -1,17 +1,16 @@ +use starknet::ContractAddress; - use starknet::ContractAddress; +#[derive(Drop, Serde, Debug, Introspect, PartialEq)] +pub struct Task { + pub name: felt252, + pub target: u32, + pub description: ByteArray, +} - #[derive( Drop, Serde, Debug, Introspect, PartialEq)] - pub struct Task { - pub name: felt252, - pub target: u32, - pub description: ByteArray, - } - - #[generate_trait] - pub impl TaskImpl of TaskTrait { - #[inline] - fn new(name: felt252, target: u32, description: ByteArray) -> Task { - Task { name, target, description } - } - } +#[generate_trait] +pub impl TaskImpl of TaskTrait { + #[inline] + fn new(name: felt252, target: u32, description: ByteArray) -> Task { + Task { name, target, description } + } +} diff --git a/src/types/tournament_types.cairo b/src/types/tournament_types.cairo index ac86c3b..c8a88fb 100644 --- a/src/types/tournament_types.cairo +++ b/src/types/tournament_types.cairo @@ -16,7 +16,8 @@ pub enum SnookerTournament { pub impl SnookerTournamentImpl of SnookerTournamentTrait { fn is_prestigious(self: @SnookerTournament) -> bool { match self { - SnookerTournament::Ranking | SnookerTournament::Masters | SnookerTournament::WorldChampionship => true, + SnookerTournament::Ranking | SnookerTournament::Masters | + SnookerTournament::WorldChampionship => true, _ => false, } } @@ -37,4 +38,4 @@ pub impl SnookerTournamentDisplay of core::fmt::Display { f.buffer.append(@s); Result::Ok(()) } -} \ No newline at end of file +}