Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions src/interfaces/IWorld.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use dojo_starter::model::game_model::{GameMode, Game};
use dojo_starter::model::player_model::{PlayerSymbol, Player};
use dojo_starter::model::property_model::{Property};

use starknet::{ContractAddress};

Expand All @@ -11,7 +12,36 @@ pub trait IWorld<T> {
fn retrieve_player(ref self: T, addr: ContractAddress) -> Player;
fn create_new_game(
ref self: T, game_mode: GameMode, player_symbol: PlayerSymbol, number_of_players: u8,
) -> u64;
fn create_new_game_id(ref self: T) -> u64;
fn retrieve_game(ref self: T, game_id: u64) -> Game;
) -> u256;
fn create_new_game_id(ref self: T) -> u256;
fn retrieve_game(ref self: T, game_id: u256) -> Game;
fn generate_properties(
ref self: T,
id: u8,
game_id: u256,
name: felt252,
cost_of_property: u256,
rent_site_only: u256,
rent_one_house: u256,
rent_two_houses: u256,
rent_three_houses: u256,
rent_four_houses: u256,
cost_of_house: u256,
rent_hotel: u256,
is_mortgaged: bool,
group_id: u8,
);
fn get_property(ref self: T, id: u8, game_id: u256) -> Property;
fn buy_property(ref self: T, property_id: u8, game_id: u256) -> bool;
fn sell_property(ref self: T, property_id: u8, game_id: u256) -> bool;
fn mortgage_property(ref self: T, property_id: u8, game_id: u256) -> bool;
fn unmortgage_property(ref self: T, property_id: u8, game_id: u256) -> bool;
fn collect_rent(ref self: T, property_id: u8, game_id: u256) -> bool;
fn transfer_from(
ref self: T, from: ContractAddress, to: ContractAddress, game_id: u256, amount: u256,
);
fn buy_house_or_hotel(ref self: T, property_id: u8, game_id: u256) -> bool;
fn sell_house_or_hotel(ref self: T, property_id: u8, game_id: u256) -> bool;
fn mint(ref self: T, recepient: ContractAddress, game_id: u256, amount: u256);
fn get_players_balance(ref self: T, player: ContractAddress, game_id: u256) -> u256;
}
1 change: 1 addition & 0 deletions src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod interfaces {
pub mod model {
pub mod game_model;
pub mod player_model;
pub mod property_model;
}

pub mod tests {
Expand Down
16 changes: 13 additions & 3 deletions src/model/game_model.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,21 @@ pub struct GameCounter {
}


#[derive(Serde, Copy, Drop, Introspect, PartialEq)]
#[dojo::model]
pub struct GameBalance {
#[key]
pub playe_address: ContractAddress,
#[key]
pub game_id: u256,
pub balance: u256,
}

#[derive(Drop, Serde)]
#[dojo::model]
pub struct Game {
#[key]
pub id: u64, // Unique id of the game
pub id: u256, // Unique id of the game
pub created_by: felt252, // Address of the game creator
pub is_initialised: bool, // Indicate whether game with given Id has been created/initialised
pub status: GameStatus, // Status of the game
Expand Down Expand Up @@ -50,7 +60,7 @@ pub struct Game {
pub trait GameTrait {
// Create and return a new game
fn new(
id: u64,
id: u256,
created_by: felt252,
game_mode: GameMode,
player_hat: felt252,
Expand Down Expand Up @@ -88,7 +98,7 @@ pub enum GameMode {

impl GameImpl of GameTrait {
fn new(
id: u64,
id: u256,
created_by: felt252,
game_mode: GameMode,
player_hat: felt252,
Expand Down
128 changes: 128 additions & 0 deletions src/model/property_model.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
use starknet::{ContractAddress, contract_address_const};

#[derive(Copy, Drop, Serde)]
#[dojo::model]
pub struct Property {
#[key]
pub id: u8,
#[key]
game_id: u256,
pub name: felt252,
pub owner: ContractAddress,
pub cost_of_property: u256,
pub rent_site_only: u256,
pub rent_one_house: u256,
pub rent_two_houses: u256,
pub rent_three_houses: u256,
pub rent_four_houses: u256,
pub cost_of_house: u256,
pub rent_hotel: u256,
pub is_mortgaged: bool,
pub group_id: u8,
pub for_sale: bool,
pub development: u8,
}

#[derive(Drop, Copy, Serde)]
#[dojo::model]
pub struct PropertyToId {
#[key]
pub name: felt252,
pub id: u8,
}

#[derive(Drop, Copy, Serde)]
#[dojo::model]
pub struct IdToProperty {
#[key]
pub id: u8,
pub name: felt252,
}

pub trait PropertyTrait {
fn new(
id: u8,
game_id: u256,
name: felt252,
cost: u256,
rent_site_only: u256,
rent_one_house: u256,
rent_two_houses: u256,
rent_three_houses: u256,
rent_four_houses: u256,
cost_of_house: u256,
rent_hotel: u256,
group_id: u8,
) -> Property;
fn set_owner(property: Property, new_owner: ContractAddress);
fn get_rent_amount(property: Property, houses: u8, hotel: bool) -> u256;
fn mortgage(property: Property);
fn lift_mortgage(property: Property);
}

impl PropertyImpl of PropertyTrait {
fn new(
id: u8,
game_id: u256,
name: felt252,
cost: u256,
rent_site_only: u256,
rent_one_house: u256,
rent_two_houses: u256,
rent_three_houses: u256,
rent_four_houses: u256,
cost_of_house: u256,
rent_hotel: u256,
group_id: u8,
) -> Property {
let zero_address: ContractAddress = contract_address_const::<0>();
Property {
id,
game_id,
name,
owner: zero_address,
cost_of_property: cost,
rent_site_only: rent_site_only,
rent_one_house: rent_one_house,
rent_two_houses: rent_two_houses,
rent_three_houses: rent_three_houses,
rent_four_houses: rent_four_houses,
rent_hotel: rent_hotel,
cost_of_house,
is_mortgaged: false,
group_id,
for_sale: true,
development: 0,
}
}


fn set_owner(mut property: Property, new_owner: ContractAddress) {
property.owner = new_owner;
}

fn get_rent_amount(mut property: Property, houses: u8, hotel: bool) -> u256 {
if property.is_mortgaged {
return 0;
}
if hotel {
return property.rent_hotel;
}
match houses {
0 => property.rent_site_only,
1 => property.rent_one_house,
2 => property.rent_two_houses,
3 => property.rent_three_houses,
4 => property.rent_four_houses,
_ => property.rent_site_only // default fallback
}
}

fn mortgage(mut property: Property) {
property.is_mortgaged = true;
}

fn lift_mortgage(mut property: Property) {
property.is_mortgaged = false;
}
}
Loading
Loading