Skip to content

Commit

Permalink
refactor(contracts): split player/drug name to component (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
broody authored Jul 10, 2023
1 parent 099d78f commit f75b545
Show file tree
Hide file tree
Showing 21 changed files with 200 additions and 77 deletions.
2 changes: 1 addition & 1 deletion Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ cairo-version = "2.0.0-rc4"
sierra-replace-ids = true

[dependencies]
dojo = { git = "https://github.com/dojoengine/dojo.git"}
dojo = { git = "https://github.com/dojoengine/dojo.git", rev="b842d0a2c137fe232d4b9d8a4282a4c78771e3d8"}

[[target.dojo]]

Expand Down
1 change: 1 addition & 0 deletions src/components.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ mod player;
mod drug;
mod location;
mod risks;
mod name;
3 changes: 1 addition & 2 deletions src/components/drug.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use traits::{Into, TryInto};

#[derive(Component, Copy, Drop, Serde)]
struct Drug {
name: felt252,
quantity: usize,
quantity: usize,
}

trait DrugTrait {
Expand Down
4 changes: 4 additions & 0 deletions src/components/name.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[derive(Component, Copy, Drop, Serde)]
struct Name {
short_string: felt252,
}
1 change: 0 additions & 1 deletion src/components/player.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#[derive(Component, Copy, Drop, Serde)]
struct Player {
name: felt252,
cash: u128,
health: u8,
arrested: bool,
Expand Down
10 changes: 8 additions & 2 deletions src/systems/create.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod create_game {
use dojo::world::Context;

use rollyourown::events::{emit, GameCreated, PlayerJoined};
use rollyourown::components::name::Name;
use rollyourown::components::game::Game;
use rollyourown::components::player::Player;
use rollyourown::components::risks::Risks;
Expand Down Expand Up @@ -48,7 +49,6 @@ mod create_game {
(game_id, player_id).into(),
(
Player {
name: 0, // set at end of game
cash: 100 * SCALING_FACTOR, // $100
health: 100,
arrested: false,
Expand Down Expand Up @@ -89,7 +89,13 @@ mod create_game {
set !(
ctx.world,
(game_id, *location_name, *drug_name).into(),
(Market { cash: MARKET_CASH, quantity: MARKET_QUANTITY })
(
Name {
short_string: *drug_name
}, Market {
cash: MARKET_CASH, quantity: MARKET_QUANTITY
}
)
);
},
Option::None(()) => {
Expand Down
1 change: 0 additions & 1 deletion src/systems/join.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ mod join_game {
(game_id, player_id).into(),
(
Player {
name: 0, // set at end of game
cash: 100 * SCALING_FACTOR, // $100
health: 100,
arrested: false,
Expand Down
15 changes: 2 additions & 13 deletions src/systems/player.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,12 @@ mod set_name {

use dojo::world::Context;
use rollyourown::components::game::Game;
use rollyourown::components::player::Player;
use rollyourown::components::name::Name;

fn execute(ctx: Context, game_id: u32, player_name: felt252) {
let player_id: felt252 = ctx.origin.into();
let player_sk: Query = (game_id, player_id).into();
let player = get !(ctx.world, player_sk, (Player));

set !(
ctx.world,
player_sk,
(Player {
name: player_name,
cash: player.cash,
health: player.health,
arrested: player.arrested,
turns_remaining: player.turns_remaining,
})
)
set !(ctx.world, player_sk, (Name { short_string: player_name, }))
}
}
8 changes: 2 additions & 6 deletions src/systems/trade.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ mod buy {
ctx.world,
(game_id, player_id).into(),
(Player {
name: player.name,
cash: player.cash - cost,
health: player.health,
arrested: player.arrested,
Expand All @@ -60,9 +59,7 @@ mod buy {
Option::None(_) => quantity,
};
set !(
ctx.world,
(game_id, player_id, drug_name).into(),
(Drug { name: drug_name, quantity: player_quantity })
ctx.world, (game_id, player_id, drug_name).into(), (Drug { quantity: player_quantity })
);

Bought(game_id, player_id, drug_name, quantity, cost);
Expand Down Expand Up @@ -121,7 +118,6 @@ mod sell {
ctx.world,
(game_id, player_id).into(),
(Player {
name: player.name,
cash: player.cash + payout,
health: player.health,
arrested: player.arrested,
Expand All @@ -131,7 +127,7 @@ mod sell {
set !(
ctx.world,
(game_id, player_id, drug_name).into(),
(Drug { name: drug_name, quantity: player_quantity - quantity })
(Drug { quantity: player_quantity - quantity })
);

Sold(game_id, player_id, drug_name, quantity, payout);
Expand Down
1 change: 0 additions & 1 deletion src/systems/travel.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ mod travel {
Location {
name: next_location_name
}, Player {
name: player.name,
cash: player.cash - result.money_loss,
health: updated_health,
arrested: result.arrested,
Expand Down
2 changes: 2 additions & 0 deletions src/tests/create.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use rollyourown::components::player::{player, Player};
use rollyourown::components::drug::{drug, Drug};
use rollyourown::components::location::{location, Location};
use rollyourown::components::risks::{risks, Risks};
use rollyourown::components::name::{name, Name};
use rollyourown::systems::travel::travel;
use rollyourown::systems::trade::{buy, sell};
use rollyourown::systems::join::join_game;
Expand All @@ -46,6 +47,7 @@ fn spawn_game() -> (ContractAddress, u32, felt252) {
components.append(risks::TEST_CLASS_HASH);
components.append(market::TEST_CLASS_HASH);
components.append(drug::TEST_CLASS_HASH);
components.append(name::TEST_CLASS_HASH);

let mut systems = array::ArrayTrait::new();
systems.append(create_game::TEST_CLASS_HASH);
Expand Down
8 changes: 4 additions & 4 deletions src/tests/player.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use starknet::{ContractAddress, syscalls::deploy_syscall};
use starknet::class_hash::{ClassHash, Felt252TryIntoClassHash};
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};

use rollyourown::components::player::{player, Player};
use rollyourown::components::name::{name, Name};
use rollyourown::tests::create::spawn_game;

#[test]
Expand All @@ -24,9 +24,9 @@ fn test_set_name() {

world.execute('set_name'.into(), set_name_calldata.span());

let mut res = world.entity('Player'.into(), (game_id, player_id).into(), 0, 0);
let mut res = world.entity('Name'.into(), (game_id, player_id).into(), 0, 0);
assert(res.len() > 0, 'no player');

let player = serde::Serde::<Player>::deserialize(ref res).expect('deserialization failed');
assert(player.name == 'Rambo'.into(), 'incorrect name');
let name = serde::Serde::<Name>::deserialize(ref res).expect('deserialization failed');
assert(name.short_string == 'Rambo'.into(), 'incorrect name');
}
8 changes: 0 additions & 8 deletions web/src/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,6 @@ const Header = ({ back }: HeaderProps) => {
{inventoryInfos.used}/{inventoryInfos.capacity}
</Text>
</HStack>
<Divider orientation="vertical" borderColor="neon.600" h="12px" />
<HStack>
<Clock />{" "}
<Text whiteSpace="nowrap">
{!IsMobile && "Day"} {game.maxTurns - player.turnsRemaining}/
{game.maxTurns}
</Text>
</HStack>
</HStack>
</HStack>
)}
Expand Down
2 changes: 1 addition & 1 deletion web/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const RYO_WORLD_ADDRESS =
"0x7d17bb24b59cb371c9ca36b79efca27fe53318e26340df3d8623dba5a7b9e5f";
"0x26065106fa319c3981618e7567480a50132f23932226a51c219ffb8e47daa84";
export const ETH_CONTRACT_ADDRESS =
"0x49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7";
Loading

1 comment on commit f75b545

@vercel
Copy link

@vercel vercel bot commented on f75b545 Jul 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

rollyourown – ./

rollyourown.preview.cartridge.gg
rollyourown-git-main.preview.cartridge.gg

Please sign in to comment.