Skip to content

Commit

Permalink
reputation
Browse files Browse the repository at this point in the history
  • Loading branch information
notV4l committed Apr 1, 2024
1 parent a0b0070 commit 9e51294
Show file tree
Hide file tree
Showing 29 changed files with 653 additions and 445 deletions.
14 changes: 11 additions & 3 deletions src/config/config.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use rollyourown::config::hustlers::{HustlerConfig, HustlerImpl};
use rollyourown::config::{
hustlers::{HustlerConfig, HustlerImpl},
game::{GameConfig}
};


#[starknet::interface]
Expand All @@ -14,6 +17,7 @@ trait IConfig<T> {
struct Config {
layouts: LayoutsConfig,
hustlers: Array<HustlerConfig>,
game_config: GameConfig,
}

#[derive(Drop, Serde)]
Expand All @@ -34,8 +38,8 @@ struct LayoutItem {
#[dojo::contract]
mod config {
use rollyourown::config::{
game::{initialize_game_config, GameConfig, GameConfigImpl},
drugs::initialize_drug_config, locations::initialize_location_config,
game::initialize_game_config,
hustlers::{
HustlerConfig, HustlerImpl, initialize_weapons_config, initialize_clothes_config,
initialize_feet_config, initialize_transport_config, initialize_weapons_tiers_config,
Expand Down Expand Up @@ -138,7 +142,11 @@ mod config {
};

//
Config { layouts: LayoutsConfig { game_store, player }, hustlers }

let game_config = GameConfigImpl::get(world);

//
Config { game_config, layouts: LayoutsConfig { game_store, player }, hustlers }
}
}
}
34 changes: 27 additions & 7 deletions src/config/game.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,21 @@ const GAME_CONFIG_KEY: u8 = 0;
struct GameConfig {
#[key]
key: u8,
cash: u32,
health: u8,
max_turns: u8,
max_wanted_shopping: u8,
max_rounds: u8,
cash: u32, // initial cash
health: u8, // initial health 0-100
max_turns: u8, // max game turn u6 : max 63
max_wanted_shopping: u8, // limit to enter pawnshop
max_rounds: u8, // max loop when running
rep_drug_step: u8, // reputation requiered to level up drug
rep_buy_item: u8, // reputation earn when buying item
rep_carry_drugs: u8, // reputation earn when traveling with >5 drug.quantity
rep_pay_cops: u8, // reputation modifier for paying NEGATIVE
rep_pay_gang: u8, // reputation modifier for paying NEGATIVE
rep_run_cops: u8, // reputation modifier for running
rep_run_gang: u8, // reputation modifier for running
rep_fight_cops: u8, // reputation modifier for fighting
rep_fight_gang: u8, // reputation modifier for fighting

}

#[generate_trait]
Expand All @@ -26,11 +36,21 @@ fn initialize_game_config(world: IWorldDispatcher) {
world,
GameConfig {
key: GAME_CONFIG_KEY,
cash: 1000, // 690
health: 100, // 100,
cash: 1000,
health: 100,
max_turns: 30,
max_wanted_shopping: 5,
max_rounds: 3,
rep_drug_step: 20,
rep_buy_item: 1,
rep_carry_drugs: 2,
rep_pay_cops: 6, // NEGATIVE
rep_pay_gang: 3, // NEGATIVE
rep_run_cops: 3,
rep_run_gang: 1,
rep_fight_cops: 4,
rep_fight_gang: 3,

}
);
}
Expand Down
11 changes: 0 additions & 11 deletions src/packing/game_store.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ struct GameStore {
items: ItemsPacked,
drugs: DrugsPacked,
wanted: WantedPacked,
encounters: EncountersPacked,
player: Player,
}

Expand All @@ -43,7 +42,6 @@ impl GameStoreImpl of GameStoreTrait {
items: ItemsPackedImpl::new(world, game),
drugs: DrugsPackedImpl::new(world, game),
wanted: WantedPackedImpl::new(world, game),
encounters: EncountersPackedImpl::new(world, game),
player: PlayerImpl::new(world, game),
}
}
Expand Down Expand Up @@ -77,12 +75,6 @@ impl GameStorePackerImpl of Packer<GameStore, GameStorePacked> {
GameStoreLayout::Wanted => {
bits.replace::<felt252>(item.idx(), item.bits(), self.wanted.packed);
},
GameStoreLayout::Encounters => {
bits
.replace::<
felt252
>(item.idx(), item.bits(), self.encounters.packed);
},
GameStoreLayout::Player => {
let player_packed: felt252 = self.player.pack();
bits.replace::<felt252>(item.idx(), item.bits(), player_packed);
Expand Down Expand Up @@ -124,9 +116,6 @@ impl GameStoreUnpackerImpl of Unpacker<GameStorePacked, GameStore> {
GameStoreLayout::Wanted => {
game_store.wanted = WantedPacked { world, game, packed };
},
GameStoreLayout::Encounters => {
game_store.encounters = EncountersPacked { world, game, packed };
},
GameStoreLayout::Player => {
// unpack packed into Player
game_store.player = packed.unpack(world, game);
Expand Down
6 changes: 1 addition & 5 deletions src/packing/game_store_layout.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ enum GameStoreLayout {
Items,
Drugs,
Wanted,
Encounters,
Player,
}

Expand All @@ -17,7 +16,6 @@ impl GameStoreLayoutIntoBytes31Impl of Into<GameStoreLayout, bytes31> {
GameStoreLayout::Items => 'Items',
GameStoreLayout::Drugs => 'Drugs',
GameStoreLayout::Wanted => 'Wanted',
GameStoreLayout::Encounters => 'Encounters',
GameStoreLayout::Player => 'Player',
};
value.try_into().unwrap()
Expand All @@ -34,7 +32,6 @@ impl GameStoreLayoutEnumerableImpl of Enumerable<GameStoreLayout> {
GameStoreLayout::Items,
GameStoreLayout::Drugs,
GameStoreLayout::Wanted,
GameStoreLayout::Encounters,
GameStoreLayout::Player,
];
items.span()
Expand All @@ -50,8 +47,7 @@ impl GameStoreLayoutPackableImpl of Packable<GameStoreLayout> {
GameStoreLayout::Items => 8,
GameStoreLayout::Drugs => 16,
GameStoreLayout::Wanted => 18,
GameStoreLayout::Encounters => 6,
GameStoreLayout::Player => 57,
GameStoreLayout::Player => 64,
}
}

Expand Down
6 changes: 2 additions & 4 deletions src/packing/markets_packed.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -168,16 +168,14 @@ impl MarketsPackedImpl of MarketsPackedTrait {
//
//

fn shuffle_drug_prices(ref self: MarketsPacked, ref randomizer: Random, drug_level: u8) {
fn shuffle_drug_prices(ref self: MarketsPacked, ref randomizer: Random, drug_slot: u8) {
let mut locations = LocationsEnumerableImpl::all();

let drug = drug_level.sub_capped(1,0);

loop {
match locations.pop_front() {
Option::Some(location) => {
let rand_tick = randomizer.between::<usize>(0, 63).into();
self.set_tick(*location, drug.into(), rand_tick);
self.set_tick(*location, drug_slot.into(), rand_tick);
},
Option::None(_) => { break; }
};
Expand Down
58 changes: 41 additions & 17 deletions src/packing/player.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ struct Player {
location: Locations,
next_location: Locations,
drug_level: u8,
reputation: u8,
}


Expand All @@ -96,6 +97,7 @@ impl PlayerImpl of PlayerTrait {
location: Locations::Home,
next_location: Locations::Home,
drug_level: 0,
reputation: 0,
}
}

Expand All @@ -113,6 +115,7 @@ impl PlayerImpl of PlayerTrait {
location: Locations::Home,
next_location: Locations::Home,
drug_level: 0,
reputation: 0,
}
}

Expand All @@ -124,13 +127,15 @@ impl PlayerImpl of PlayerTrait {
if self.health == 0 {
return false;
}

if self.status != PlayerStatus::Normal {
return false;
}

if self.turn == self.game.max_turns {
return false;
}

if self.game.game_over {
return false;
}
Expand All @@ -142,6 +147,7 @@ impl PlayerImpl of PlayerTrait {
if self.health == 0 {
return false;
}

if self.status != PlayerStatus::Normal {
return false;
}
Expand All @@ -163,38 +169,47 @@ impl PlayerImpl of PlayerTrait {
self.status == PlayerStatus::BeingArrested || self.status == PlayerStatus::BeingMugged
}

fn level_up_drug(
ref self: Player, ref game_store: GameStore, ref randomizer: Random
//drugs_packed: DrugsPacked, encounters_packed: EncountersPacked
) {
// check if already max drug_level
if self.drug_level == 4 {
return;
};
fn level_up_drug(ref self: Player, ref game_store: GameStore, ref randomizer: Random) {
// // check if already max drug_level
// if self.drug_level == 4 {
// return;
// };

let cops_level = game_store.encounters.get_encounter_level(Encounters::Cops);
let gang_level = game_store.encounters.get_encounter_level(Encounters::Gang);
let level = cops_level + gang_level;
let game_config = GameConfigImpl::get(self.world);

// level up each 2 encounters capped to 4
let drug_level = 0_u8.add_capped(level / 2, 4);
// level up each rep_drug_step capped to 4
let mut drug_level: u8 = MathImpl::min(self.reputation / game_config.rep_drug_step, 4);

// check if already the right level
if self.drug_level == drug_level {
return;
}

// check if not carrying drug to be disabled
// get drugs
let drugs = game_store.drugs.get();
if drugs.quantity > 0 && drugs.drug.into() < drug_level {
return;

// level up
if self.drug_level < drug_level {
// check if not carrying drug to be disabled
if drugs.quantity > 0 && drugs.drug.into() < drug_level {
return;
}
}

// level down
if self.drug_level > drug_level {
// check if not carrying drug to be disabled
if drugs.quantity > 0 && drugs.drug.into() > drug_level + 4 {
return;
}
}

// update drug level
game_store.player.drug_level = drug_level;

// randomize price for new drug
game_store.markets.shuffle_drug_prices(ref randomizer, drug_level.into());
let drug_slot = drug_level.sub_capped(1,0);
game_store.markets.shuffle_drug_prices(ref randomizer, drug_slot);
}

fn hustle(self: Player, ref game_store: GameStore, ref randomizer: Random) {
Expand Down Expand Up @@ -255,6 +270,9 @@ impl PlayerPackerImpl of Packer<Player, felt252> {
PlayerLayout::DrugLevel => {
bits.replace::<u8>(item.idx(), item.bits(), self.drug_level.into());
},
PlayerLayout::Reputation => {
bits.replace::<u8>(item.idx(), item.bits(), self.reputation.into());
},
};
},
Option::None => { break; },
Expand Down Expand Up @@ -312,6 +330,12 @@ impl PlayerUnpackerImpl of Unpacker<felt252, Player> {
.extract_into::<u8>(item.idx(), item.bits())
.into();
},
PlayerLayout::Reputation => {
player
.reputation = bits
.extract_into::<u8>(item.idx(), item.bits())
.into();
},
};
},
Option::None => { break; },
Expand Down
6 changes: 5 additions & 1 deletion src/packing/player_layout.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ enum PlayerLayout {
PrevLocation,
Location,
NextLocation,
DrugLevel
DrugLevel,
Reputation,
}

impl PlayerLayoutIntoBytes31Impl of Into<PlayerLayout, bytes31> {
Expand All @@ -23,6 +24,7 @@ impl PlayerLayoutIntoBytes31Impl of Into<PlayerLayout, bytes31> {
PlayerLayout::Location => 'Location',
PlayerLayout::NextLocation => 'NextLocation',
PlayerLayout::DrugLevel => 'DrugLevel',
PlayerLayout::Reputation => 'Reputation',
};
value.try_into().unwrap()
}
Expand All @@ -42,6 +44,7 @@ impl PlayerLayoutEnumerableImpl of Enumerable<PlayerLayout> {
PlayerLayout::Location,
PlayerLayout::NextLocation,
PlayerLayout::DrugLevel,
PlayerLayout::Reputation,
];
items.span()
}
Expand All @@ -60,6 +63,7 @@ impl PlayerLayoutPackableImpl of Packable<PlayerLayout> {
PlayerLayout::Location => 3,
PlayerLayout::NextLocation => 3,
PlayerLayout::DrugLevel => 3,
PlayerLayout::Reputation => 7,
}
}

Expand Down
1 change: 1 addition & 0 deletions src/systems/devtools.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ mod devtools {
Into::<u8, felt252>::into(rand_10),
Into::<u32, felt252>::into(rand),
Into::<u8, felt252>::into(rand_100),
Into::<u8, felt252>::into(rand_100),
]
);

Expand Down
11 changes: 7 additions & 4 deletions src/systems/game.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ mod game {
drug_loss: Array<u32>,
turn_loss: u8,
escaped_with_item: bool,
rep_pos:u8,
rep_neg:u8,
}

#[derive(Drop, Serde, starknet::Event)]
Expand All @@ -182,6 +184,7 @@ mod game {
turn: u8,
cash: u32,
health: u8,
reputation: u8,
}


Expand All @@ -207,10 +210,10 @@ mod game {
player_name: Bytes16Impl::from(player_name),
hustler_id,
leaderboard_version,
game_mode,
max_turns: game_config.max_turns,
max_wanted_shopping: game_config.max_wanted_shopping,
max_rounds: game_config.max_rounds,
game_mode,// TODO: remove
max_turns: game_config.max_turns, // TODO: remove?
max_wanted_shopping: game_config.max_wanted_shopping, // TODO: remove?
max_rounds: game_config.max_rounds, // TODO: remove?
game_over: false
};

Expand Down
Loading

0 comments on commit 9e51294

Please sign in to comment.