Skip to content

Commit

Permalink
Add gameplayer logic
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandoDrRobot committed Sep 2, 2024
1 parent c84652f commit f970cdd
Show file tree
Hide file tree
Showing 15 changed files with 1,627 additions and 2 deletions.
459 changes: 459 additions & 0 deletions manifests/dev/base/abis/models/bytebeasts-Game-e91217d7.json

Large diffs are not rendered by default.

407 changes: 407 additions & 0 deletions manifests/dev/base/abis/models/bytebeasts-GameId-344511d3.json

Large diffs are not rendered by default.

439 changes: 439 additions & 0 deletions manifests/dev/base/abis/models/bytebeasts-GamePlayer-596ef4a1.json

Large diffs are not rendered by default.

41 changes: 41 additions & 0 deletions manifests/dev/base/models/bytebeasts-Game-e91217d7.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
kind = "DojoModel"
class_hash = "0x14996dadc46ccc2e22fa71e614ea4a158f91fd919429d00a0e36a4a4314bdd8"
original_class_hash = "0x14996dadc46ccc2e22fa71e614ea4a158f91fd919429d00a0e36a4a4314bdd8"
abi = "manifests/dev/base/abis/models/bytebeasts-Game-e91217d7.json"
tag = "bytebeasts-Game"
manifest_name = "bytebeasts-Game-e91217d7"

[[members]]
name = "game_id"
type = "u128"
key = true

[[members]]
name = "player_1"
type = "ContractAddress"
key = false

[[members]]
name = "player_2"
type = "ContractAddress"
key = false

[[members]]
name = "player_3"
type = "ContractAddress"
key = false

[[members]]
name = "player_4"
type = "ContractAddress"
key = false

[[members]]
name = "status"
type = "GameStatus"
key = false

[[members]]
name = "is_private"
type = "bool"
key = false
16 changes: 16 additions & 0 deletions manifests/dev/base/models/bytebeasts-GameId-344511d3.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
kind = "DojoModel"
class_hash = "0x6a3b296e3483766bd71a615fe01cf34ec0300356ed2971289fa54b236f47958"
original_class_hash = "0x6a3b296e3483766bd71a615fe01cf34ec0300356ed2971289fa54b236f47958"
abi = "manifests/dev/base/abis/models/bytebeasts-GameId-344511d3.json"
tag = "bytebeasts-GameId"
manifest_name = "bytebeasts-GameId-344511d3"

[[members]]
name = "id"
type = "u32"
key = true

[[members]]
name = "game_id"
type = "u128"
key = false
56 changes: 56 additions & 0 deletions manifests/dev/base/models/bytebeasts-GamePlayer-596ef4a1.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
kind = "DojoModel"
class_hash = "0x62792f6e33268b9969eb392c92b70ef6729e674fd204b324f22a2615c1d22a7"
original_class_hash = "0x62792f6e33268b9969eb392c92b70ef6729e674fd204b324f22a2615c1d22a7"
abi = "manifests/dev/base/abis/models/bytebeasts-GamePlayer-596ef4a1.json"
tag = "bytebeasts-GamePlayer"
manifest_name = "bytebeasts-GamePlayer-596ef4a1"

[[members]]
name = "address"
type = "ContractAddress"
key = true

[[members]]
name = "game_id"
type = "u128"
key = true

[[members]]
name = "beast_1"
type = "u8"
key = false

[[members]]
name = "beast_2"
type = "u8"
key = false

[[members]]
name = "beast_3"
type = "u8"
key = false

[[members]]
name = "beast_4"
type = "u8"
key = false

[[members]]
name = "bag_id"
type = "u8"
key = false

[[members]]
name = "active_mount"
type = "u8"
key = false

[[members]]
name = "mounts"
type = "Array<u8>"
key = false

[[members]]
name = "position"
type = "Array<u8>"
key = false
103 changes: 101 additions & 2 deletions src/models.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use starknet::ContractAddress;

use starknet::{ContractAddress, SyscallResultTrait};

#[derive(Serde, Copy, Drop, Introspect)]
pub enum WorldElements {
Expand Down Expand Up @@ -226,3 +225,103 @@ mod tests {
assert_eq!(potion.potion_effect, 50, "Potion effect should be 50");
}
}



// New Models

// Game Player
#[derive(Drop, Serde, Debug)]
#[dojo::model]
pub struct GamePlayer {
#[key]
pub address: ContractAddress,
#[key]
pub game_id: u128,
pub beast_1: u8,
pub beast_2: u8,
pub beast_3: u8,
pub beast_4: u8,
pub bag_id: u8,
pub active_mount: u8,
pub mounts: Array<u8>,
pub position: Array<u8>
}

pub trait GamePlayerTrait {
fn new(game_id: u128, address: ContractAddress) -> GamePlayer;
}

impl GamePlayerImpl of GamePlayerTrait {
// logic to create an instance of a game player
fn new(game_id: u128, address: ContractAddress) -> GamePlayer {
let game_player = GamePlayer {
address: address,
game_id: game_id,
beast_1: 0_u8,
beast_2: 0_u8,
beast_3: 0_u8,
beast_4: 0_u8,
bag_id: 0_u8,
active_mount: 0_u8,
mounts: ArrayTrait::new(),
position: ArrayTrait::new(),
};
game_player
}
}

// Realm (Game)
#[derive(Copy, Drop, Serde)]
#[dojo::model]
pub struct GameId {
#[key]
pub id: u32,
pub game_id: u128
}

#[derive(Serde, Copy, Drop, Introspect, PartialEq)]
pub enum GameStatus {
Pending: (),
InProgress: (),
Finished: (),
}

#[derive(Copy, Drop, Serde)]
#[dojo::model]
pub struct Game {
#[key]
pub game_id: u128,
pub player_1: ContractAddress,
pub player_2: ContractAddress,
pub player_3: ContractAddress,
pub player_4: ContractAddress,
pub status: GameStatus,
pub is_private: bool,
}

pub trait GameTrait {
fn new(game_id: u128, player_1: ContractAddress) -> Game;
fn join_game(ref self: Game, player_2: GamePlayer);
}

pub impl GameImpl of GameTrait {
// create the game
fn new(game_id: u128, player_1: ContractAddress) -> Game {
let game: Game = Game {
game_id: game_id,
player_1: player_1,
player_2: core::num::traits::Zero::<ContractAddress>::zero(),
player_3: core::num::traits::Zero::<ContractAddress>::zero(),
player_4: core::num::traits::Zero::<ContractAddress>::zero(),
status: GameStatus::InProgress,
is_private: false,
};
game
}

// player two can join the game
fn join_game(ref self: Game, player_2: GamePlayer) {
self.player_2 = player_2.address;
}
}
Empty file added src/models/Bag.cairo
Empty file.
Empty file added src/models/Game.cairo
Empty file.
42 changes: 42 additions & 0 deletions src/models/GamePlayer.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use starknet::ContractAddress;
use dojo::model::Model;

#[derive(Drop, Serde, Debug)]
#[dojo::model]
pub struct GamePlayer {
#[key]
pub address: ContractAddress,
#[key]
pub game_id: u128,
pub beast_1: u8,
pub beast_2: u8,
pub beast_3: u8,
pub beast_3: u8,
pub bag_id: u8,
pub active_mount: u8,
pub mounts: Array<u8>,
pub position: Array<u8>
}

pub trait GamePlayerTrait {
fn new(game_id: u128, address: ContractAddress) -> GamePlayer;
}

impl GamePlayerImpl of GamePlayerTrait {
// logic to create an instance of a game player
fn new(game_id: u128, address: ContractAddress) -> GamePlayer {
let game_player = GamePlayer {
address: address,
game_id: game_id,
beast_1: 0_u8,
beast_2: 0_u8,
beast_3: 0_u8,
beast_4: 0_u8,
bag_id: 0_u8,
active_mount: 0_u8,
mounts: ArrayTrait::new(),
position: ArrayTrait::new(),
};
game_player
}
}
Empty file added src/models/Map.cairo
Empty file.
Empty file added src/models/Mount.cairo
Empty file.
Empty file added src/models/NPCs.cairo
Empty file.
Empty file added src/models/Trainer.cairo
Empty file.
66 changes: 66 additions & 0 deletions src/systems/realms.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use starknet::ContractAddress;
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};

use bytebeasts::models::GameId
use bytebeasts::models::GameStatus
use bytebeasts::models::Game
use bytebeasts::models::GamePlayer

#[dojo::interface]
trait IActions {
fn create_initial_game_id(ref world: IWorldDispatcher);
fn create_game(ref world: IWorldDispatcher) -> Game;
fn join_game(ref world: IWorldDispatcher, game_id: u128, player_two_address: ContractAddress);
}

#[dojo::contract]
mod actions {
use starknet::{ContractAddress, get_caller_address, SyscallResultTrait};
use starknet::{get_tx_info, get_block_number};
use bytebeasts::models::{GameId, GameStatus, Game, GamePlayer};

use super::IActions;

#[abi(embed_v0)]
impl ActionsImpl of IActions<ContractState> {
fn create_initial_game_id(ref world: IWorldDispatcher) {
let existing_game_id = get!(world, 1, (GameId));
if (existing_game_id.game_id > 0) {
panic!("error global game id already created");
}
let game_id: GameId = GameId { id: 1, game_id: 1 };
set!(world, (game_id));
}

fn create_game(ref world: IWorldDispatcher) -> Game {
let player_1_address = get_caller_address();
let mut game_id: GameId = get!(world, 1, (GameId));
let player_1 = GamePlayerTrait::new(game_id.game_id, player_1_address);
let game: Game = GameTrait::new(game_id.game_id, player_1_address);
game_id.game_id += 1;
set!(world, (player_1, game, game_id));
game
}

fn join_game(
ref world: IWorldDispatcher, game_id: u128, player_2_address: ContractAddress
) {
let mut game = get!(world, game_id, (Game));
assert!(
game.player_2 == core::num::traits::Zero::<ContractAddress>::zero(),
"player_2 already set"
);
let player_2 = GamePlayerTrait::new(game.game_id, player_2_address);
game.join_game(player_2);
let player_2 = GamePlayerTrait::new(game.game_id, player_2_address);
set!(world, (player_2, game));
}

fn is_game_finished(ref world: IWorldDispatcher, game_id: u128) -> bool {
let mut game: Game = get!(world, game_id, (Game));
let player_1: GamePlayer = get!(world, (game.player_1, game.game_id), (GamePlayer));
let player_2: GamePlayer = get!(world, (game.player_2, game.game_id), (GamePlayer));
game.is_game_finished(player_1, player_2)
}
}
}

0 comments on commit f970cdd

Please sign in to comment.