Skip to content

Commit

Permalink
reintroduce turn cap & test mode
Browse files Browse the repository at this point in the history
  • Loading branch information
notV4l committed Nov 24, 2023
1 parent 4e3a7d5 commit bf5ed71
Show file tree
Hide file tree
Showing 19 changed files with 116 additions and 48 deletions.
4 changes: 0 additions & 4 deletions src/models/game.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ struct Game {
max_players: usize,
num_players: usize,
max_turns: usize,
is_finished: bool,
creator: ContractAddress,
}

Expand All @@ -29,9 +28,6 @@ impl GameImpl of GameTrait {
if info.block_timestamp < self.start_time {
return false;
}
if self.is_finished {
return false;
}

true
}
Expand Down
3 changes: 2 additions & 1 deletion src/models/player.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ struct Player {
transport: usize,
speed: usize,
wanted: u8,
leaderboard_version: u32
leaderboard_version: u32,
game_over: bool,
}

#[generate_trait]
Expand Down
1 change: 1 addition & 0 deletions src/systems/decide.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ mod decide {

if health_loss >= player.health {
player.health = 0;
player.game_over = true;
outcome = Outcome::Died;

let leaderboard_manager = LeaderboardManagerTrait::new(self.world());
Expand Down
1 change: 1 addition & 0 deletions src/systems/devtools.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ mod devtools {
transport: 42,
speed: 42,
leaderboard_version,
game_over: true,
};

set!(self.world(), (player));
Expand Down
7 changes: 4 additions & 3 deletions src/systems/lobby.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ mod lobby {
defense: player_settings.defense,
transport: player_settings.transport,
speed: player_settings.speed,
leaderboard_version
leaderboard_version,
game_over: false,
};

let game = Game {
Expand All @@ -113,7 +114,6 @@ mod lobby {
max_players: game_settings.max_players,
num_players: 1, // caller auto joins
max_turns: game_settings.max_turns,
is_finished: false,
creator: caller,
};

Expand Down Expand Up @@ -153,7 +153,8 @@ mod lobby {
}

fn assert_valid_chain(game_mode: GameMode) {
assert(game_mode == GameMode::Unlimited, 'invalid game_mode');
// assert(game_mode == GameMode::Unlimited, 'invalid game_mode');

//if game_mode == GameMode::Test {
// let chain_id = get_tx_info().unbox().chain_id;
// assert(chain_id != 'KATANA', 'wrong chain_id');
Expand Down
17 changes: 17 additions & 0 deletions src/systems/travel.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use rollyourown::models::location::LocationEnum;
#[starknet::interface]
trait ITravel<TContractState> {
fn travel(self: @TContractState, game_id: u32, next_location_id: LocationEnum) -> bool;
fn end_game(self: @TContractState, game_id: u32);
}

#[dojo::contract]
Expand All @@ -28,6 +29,7 @@ mod travel {
use rollyourown::utils::risk::{RiskTrait, RiskImpl};
use rollyourown::utils::math::{MathTrait, MathImplU8};
use rollyourown::utils::random::{Random, RandomImpl};
use rollyourown::utils::leaderboard::{LeaderboardManager, LeaderboardManagerTrait};

use super::ITravel;
use super::on_turn_end;
Expand Down Expand Up @@ -152,6 +154,21 @@ mod travel {

false
}

fn end_game(self: @ContractState, game_id: u32) {
let world = self.world();
let player_id = get_caller_address();

let mut player: Player = get!(world, (game_id, player_id).into(), Player);
assert(player.game_over == false, 'already game_over');

player.game_over = true;
set!(world, (player));

let leaderboard_manager = LeaderboardManagerTrait::new(self.world());
leaderboard_manager.on_game_end(player.cash);
}

}
}

Expand Down
1 change: 0 additions & 1 deletion src/tests/create.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ fn spawn_game() -> (ContractAddress, u32, felt252) {
assert(game.start_time == START_TIME, 'start time mismatch');
assert(game.max_players == MAX_PLAYERS, 'max players mismatch');
assert(game.max_turns == MAX_TURNS, 'max turns mismatch');
assert(game.is_finished == false, 'game is finished mismatch');

(world.contract_address, game_id, player_id)
}
Expand Down
7 changes: 6 additions & 1 deletion src/utils/settings.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,12 @@ trait EncounterSettingsTrait<T> {

impl GameSettingsImpl of SettingsTrait<GameSettings> {
fn get(game_mode: GameMode) -> GameSettings {
let game_settings = GameSettings { max_players: 1, max_turns: 0 };
let mut game_settings = GameSettings { max_players: 1, max_turns: 42 };

if game_mode == GameMode::Test {
game_settings.max_turns = 5;
}

game_settings
}
}
Expand Down
38 changes: 25 additions & 13 deletions web/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@
{
"name": "decide",
"address": "0x59fed3332e583eb4ff6c0ff52a45c08b823025f2bc2d1b148e14a3e089ce271",
"class_hash": "0xbc1cd02cccc564f4a34c8ee1d3dd20829b6b9e6a9db44fdb0b691ecc71976f",
"class_hash": "0x4f0237ac51e1fbf4e312ad9034fc7f2efe9d100233894cf336b8e62f8f86025",
"abi": [
{
"type": "impl",
Expand Down Expand Up @@ -1247,7 +1247,7 @@
{
"name": "devtools",
"address": "0x3e76834b68cd7aa2ec3da8ad4022bace3d9a0ff9f5568e5c4e5f8435474f2b5",
"class_hash": "0x4a5d1613cc8cfb562d3fc0d0552f464f8313bbe1aff617a4147573962bbc9da",
"class_hash": "0x31ea04e15d7b101247898b533edbcd9c224e42118ed01b920874e0b0dfb7d8e",
"abi": [
{
"type": "impl",
Expand Down Expand Up @@ -1389,7 +1389,7 @@
{
"name": "lobby",
"address": "0x4c03b718b8cda385d9ba4cf9eedec6588fffe053fbc9a5de3840226c981f00d",
"class_hash": "0x284167a7b42a1a01b580f04b8e067cc69d3643fdbd38f05844933f9ba9b3f86",
"class_hash": "0x4225ba28a49bf788d2eba0757c1a12dacefd2b0ea1c3e4ac26e8e9e634f205b",
"abi": [
{
"type": "impl",
Expand Down Expand Up @@ -1762,7 +1762,7 @@
{
"name": "shop",
"address": "0x49666a158f5d130c9efebd24aaa99b0c52796439fab687cef3d089a35d97e31",
"class_hash": "0xd93571e6279c44d980a7bb751e89519ae54903122e8eded0e4def7f728010e",
"class_hash": "0xeff9452fe41f5dddf8b9e81b999fad03bac852352797ec94aaf34c09b8549d",
"abi": [
{
"type": "impl",
Expand Down Expand Up @@ -2099,7 +2099,7 @@
{
"name": "trade",
"address": "0x5decbe746e8199f7162d9cfbf3cb84fff20712e4fec76c729a4e0a8725fbc4",
"class_hash": "0x55c5b19899758fd0385948cf61728c77bb7511fdf1f88346219dbf68e1309a7",
"class_hash": "0x26e9a1222efc369cf91f0026c81f39fb2b2ad80f72a3eaf8728d0f9efd4a7bb",
"abi": [
{
"type": "impl",
Expand Down Expand Up @@ -2417,7 +2417,7 @@
{
"name": "travel",
"address": "0x2ec3dd5ae820ac90b7a95af30b9d62c2b13d3f26c948750d9d3f38a1a7a9fbd",
"class_hash": "0x350787165207d8dc9acb4e96726bca3b30f830fa4812dcc21427e13802033b3",
"class_hash": "0x6518aa200bbb25603ef9e047fb131f2fde40411e379c10f34ab501f5e8bca41",
"abi": [
{
"type": "impl",
Expand Down Expand Up @@ -2527,6 +2527,18 @@
}
],
"state_mutability": "view"
},
{
"type": "function",
"name": "end_game",
"inputs": [
{
"name": "game_id",
"type": "core::integer::u32"
}
],
"outputs": [],
"state_mutability": "view"
}
]
},
Expand Down Expand Up @@ -3214,18 +3226,13 @@
"type": "usize",
"key": false
},
{
"name": "is_finished",
"type": "bool",
"key": false
},
{
"name": "creator",
"type": "ContractAddress",
"key": false
}
],
"class_hash": "0x23b1befabb6193697bfeef78eae927b62224e1924ee405f9b8e60052a7bfe3a",
"class_hash": "0x5a281cfdf88fa6fd701345c663480c71f9bbe7462902b54a7245e09f25ea2fa",
"abi": [
{
"type": "function",
Expand Down Expand Up @@ -4056,9 +4063,14 @@
"name": "leaderboard_version",
"type": "u32",
"key": false
},
{
"name": "game_over",
"type": "bool",
"key": false
}
],
"class_hash": "0x7f44c9585e56584edbb490fbc7a7cc6636f078d66086b1f776358eb518baa98",
"class_hash": "0x2ebd2ef65f9a3a8f9da19a1353a1904d846a5213ee63d7340402f955efaa84d",
"abi": [
{
"type": "function",
Expand Down
2 changes: 1 addition & 1 deletion web/src/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const Header = ({ back }: HeaderProps) => {
return (
<HStack w="full" px="10px" spacing="10px" zIndex="overlay" align="flex-start" py={["0", "20px"]} fontSize={["14px", "16px"]}>
<HStack flex="1" justify={["left", "right"]}></HStack>
{playerEntity && playerEntity.health > 0 && (
{playerEntity && playerEntity.health > 0 && !playerEntity.gameOver && (
<HStack flex={["auto", 1]} justify="center" width={["100%", "auto"]}>
<HStack
h="48px"
Expand Down
5 changes: 3 additions & 2 deletions web/src/dojo/generated/contractModels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,12 @@ export function defineContractComponents(world: World) {
max_players: RecsType.Number,
num_players: RecsType.Number,
max_turns: RecsType.Number,
is_finished: RecsType.Boolean,
creator: RecsType.String,
},
{
metadata: {
name: name,
types: ["GameMode", "u64", "usize", "usize", "usize", "bool", "ContractAddress"],
types: ["GameMode", "u64", "usize", "usize", "usize", "ContractAddress"],
},
},
);
Expand Down Expand Up @@ -129,6 +128,7 @@ export function defineContractComponents(world: World) {
speed: RecsType.Number,
wanted: RecsType.Number,
leaderboard_version: RecsType.Number,
game_over: RecsType.Boolean,
},
{
metadata: {
Expand All @@ -151,6 +151,7 @@ export function defineContractComponents(world: World) {
"usize",
"u8",
"u32",
"bool",
],
},
},
Expand Down
21 changes: 21 additions & 0 deletions web/src/dojo/hooks/useSystems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface SystemsInterface {
avatarId: number
) => Promise<SystemExecuteResult>;
travel: (gameId: string, locationId: Location) => Promise<SystemExecuteResult>;
endGame: (gameId: string) => Promise<SystemExecuteResult>;
// join: (gameId: string) => Promise<SystemExecuteResult>;
buy: (
gameId: string,
Expand Down Expand Up @@ -203,6 +204,25 @@ export const useSystems = (): SystemsInterface => {
[executeAndReceipt],
);

const endGame = useCallback(
async (gameId: string) => {
const { hash, events, parsedEvents } = await executeAndReceipt(
"travel",
"end_game",
[gameId],
);

return {
hash,
event: [],
events: [],
};
},
[executeAndReceipt],
);



const buy = useCallback(
async (
gameId: string,
Expand Down Expand Up @@ -359,6 +379,7 @@ export const useSystems = (): SystemsInterface => {
createGame,
// join,
travel,
endGame,
buy,
sell,
//setName,
Expand Down
5 changes: 5 additions & 0 deletions web/src/dojo/queries/usePlayerEntity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export class PlayerEntity {
speed: number;

wanted: number;
gameOver: boolean;

constructor(player: Player, drugs: Drug[], items: ShopItem[], encounters: Encounter[]) {
this.name = shortString.decodeShortString(player.name);
Expand All @@ -61,10 +62,13 @@ export class PlayerEntity {
this.speed = player.speed;

this.wanted = player.wanted;
this.gameOver = player.game_over;


this.drugs = drugs;
this.items = items;
this.encounters = encounters;

}

update(player: Player) {
Expand All @@ -75,6 +79,7 @@ export class PlayerEntity {
this.locationId = player.location_id === "Home" ? undefined : player.location_id;
this.status = player.status;
this.wanted = player.wanted;
this.gameOver = player.game_over;
return this;
}

Expand Down
Loading

0 comments on commit bf5ed71

Please sign in to comment.