Skip to content

Commit

Permalink
handle midflight stat boost changes (#408)
Browse files Browse the repository at this point in the history
* fixes events including outdated stat boosts
* this was most common when items first reached g15
* the impact to the player was client showing wrong stats which is confusin and in the case of charisma, impacts market price calculation
* the two scenarios that would cause this behavior are:
** when items first reach G15
** during equipment changes involving G15 items
  • Loading branch information
loothero authored Oct 13, 2023
1 parent c632275 commit d99116c
Show file tree
Hide file tree
Showing 5 changed files with 367 additions and 340 deletions.
101 changes: 51 additions & 50 deletions contracts/adventurer/src/adventurer.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use super::{
};
use lootitems::{
loot::{Loot, ILoot, ImplLoot},
constants::{ItemSuffix, ItemId, NamePrefixLength, NameSuffixLength}
constants::{ItemSuffix, ItemId, NamePrefixLength, NameSuffixLength, SUFFIX_UNLOCK_GREANTESS}
};
use combat::{
combat::{ImplCombat, CombatSpec, SpecialPowers, CombatResult},
Expand Down Expand Up @@ -838,7 +838,8 @@ impl ImplAdventurer of IAdventurer {
if (previous_level < 15 && new_level >= 19) {
// if previous level was below G15 and new level is G19+, sufix and prefixes were unlocked
return (true, true);
} else if (previous_level < 15 && new_level >= 15) {
} else if (previous_level < SUFFIX_UNLOCK_GREANTESS
&& new_level >= SUFFIX_UNLOCK_GREANTESS) {
// if previous level was below G15 and new level is G15+, suffix was unlocked
return (true, false);
} else if (previous_level < 19 && new_level >= 19) {
Expand Down Expand Up @@ -1048,74 +1049,74 @@ impl ImplAdventurer of IAdventurer {
// @notice Removes a specified suffix boost from an adventurer's stats.
// @param self The instance of the Stats struct which contains the adventurer's stats.
// @param suffix The suffix to be removed from the adventurer's stats.
fn remove_suffix_boost(ref self: Adventurer, suffix: u8) {
fn remove_suffix_boost(ref self: Stats, suffix: u8) {
if (suffix == ItemSuffix::of_Power) {
self.stats.decrease_strength(3);
self.decrease_strength(3);
} else if (suffix == ItemSuffix::of_Giant) {
self.stats.decrease_vitality(3);
self.decrease_vitality(3);
} else if (suffix == ItemSuffix::of_Titans) {
self.stats.decrease_strength(2);
self.stats.decrease_vitality(1);
self.decrease_strength(2);
self.decrease_vitality(1);
} else if (suffix == ItemSuffix::of_Skill) {
self.stats.decrease_dexterity(3);
self.decrease_dexterity(3);
} else if (suffix == ItemSuffix::of_Perfection) {
self.stats.decrease_strength(1);
self.stats.decrease_dexterity(1);
self.stats.decrease_vitality(1);
self.decrease_strength(1);
self.decrease_dexterity(1);
self.decrease_vitality(1);
} else if (suffix == ItemSuffix::of_Brilliance) {
self.stats.decrease_vitality(3);
self.decrease_vitality(3);
} else if (suffix == ItemSuffix::of_Enlightenment) {
self.stats.decrease_vitality(3);
self.decrease_vitality(3);
} else if (suffix == ItemSuffix::of_Protection) {
self.stats.decrease_vitality(2);
self.stats.decrease_dexterity(1);
self.decrease_vitality(2);
self.decrease_dexterity(1);
} else if (suffix == ItemSuffix::of_Anger) {
self.stats.decrease_strength(2);
self.stats.decrease_dexterity(1);
self.decrease_strength(2);
self.decrease_dexterity(1);
} else if (suffix == ItemSuffix::of_Rage) {
self.stats.decrease_strength(1);
self.stats.decrease_vitality(1);
self.stats.decrease_vitality(1);
self.decrease_strength(1);
self.decrease_vitality(1);
self.decrease_vitality(1);
} else if (suffix == ItemSuffix::of_Fury) {
self.stats.decrease_vitality(1);
self.stats.decrease_vitality(1);
self.stats.decrease_vitality(1);
self.decrease_vitality(1);
self.decrease_vitality(1);
self.decrease_vitality(1);
} else if (suffix == ItemSuffix::of_Vitriol) {
self.stats.decrease_vitality(2);
self.stats.decrease_vitality(1);
self.decrease_vitality(2);
self.decrease_vitality(1);
} else if (suffix == ItemSuffix::of_the_Fox) {
self.stats.decrease_dexterity(2);
self.stats.decrease_vitality(1);
self.decrease_dexterity(2);
self.decrease_vitality(1);
} else if (suffix == ItemSuffix::of_Detection) {
self.stats.decrease_vitality(2);
self.stats.decrease_dexterity(1);
self.decrease_vitality(2);
self.decrease_dexterity(1);
} else if (suffix == ItemSuffix::of_Reflection) {
self.stats.decrease_vitality(1);
self.stats.decrease_vitality(2);
self.decrease_vitality(1);
self.decrease_vitality(2);
} else if (suffix == ItemSuffix::of_the_Twins) {
self.stats.decrease_vitality(3);
self.decrease_vitality(3);
}
}

// @notice checks if the adventurer has any items with special names.
// @param self The Adventurer to check for item specials.
// @return Returns true if adventurer has item specials, false otherwise.
fn has_item_specials(self: Adventurer) -> bool {
if (self.weapon.get_greatness() >= 15) {
if (self.weapon.get_greatness() >= SUFFIX_UNLOCK_GREANTESS) {
true
} else if (self.chest.get_greatness() >= 15) {
} else if (self.chest.get_greatness() >= SUFFIX_UNLOCK_GREANTESS) {
true
} else if (self.head.get_greatness() >= 15) {
} else if (self.head.get_greatness() >= SUFFIX_UNLOCK_GREANTESS) {
true
} else if (self.waist.get_greatness() >= 15) {
} else if (self.waist.get_greatness() >= SUFFIX_UNLOCK_GREANTESS) {
true
} else if (self.foot.get_greatness() >= 15) {
} else if (self.foot.get_greatness() >= SUFFIX_UNLOCK_GREANTESS) {
true
} else if (self.hand.get_greatness() >= 15) {
} else if (self.hand.get_greatness() >= SUFFIX_UNLOCK_GREANTESS) {
true
} else if (self.neck.get_greatness() >= 15) {
} else if (self.neck.get_greatness() >= SUFFIX_UNLOCK_GREANTESS) {
true
} else if (self.ring.get_greatness() >= 15) {
} else if (self.ring.get_greatness() >= SUFFIX_UNLOCK_GREANTESS) {
true
} else {
false
Expand Down Expand Up @@ -1162,60 +1163,60 @@ impl ImplAdventurer of IAdventurer {
strength: 0, dexterity: 0, vitality: 0, charisma: 0, intelligence: 0, wisdom: 0, luck: 0
};

if (self.weapon.get_greatness() >= 15) {
if (self.weapon.get_greatness() >= SUFFIX_UNLOCK_GREANTESS) {
stats
.apply_suffix_boost(
ImplItemSpecials::get_specials_full(name_storage1, name_storage2, self.weapon)
.special1
);
}
if (self.chest.get_greatness() >= 15) {
if (self.chest.get_greatness() >= SUFFIX_UNLOCK_GREANTESS) {
stats
.apply_suffix_boost(
ImplItemSpecials::get_specials_full(name_storage1, name_storage2, self.chest)
.special1
);
}
if (self.head.get_greatness() >= 15) {
if (self.head.get_greatness() >= SUFFIX_UNLOCK_GREANTESS) {
stats
.apply_suffix_boost(
ImplItemSpecials::get_specials_full(name_storage1, name_storage2, self.head)
.special1
);
}
if (self.waist.get_greatness() >= 15) {
if (self.waist.get_greatness() >= SUFFIX_UNLOCK_GREANTESS) {
stats
.apply_suffix_boost(
ImplItemSpecials::get_specials_full(name_storage1, name_storage2, self.waist)
.special1
);
}

if (self.foot.get_greatness() >= 15) {
if (self.foot.get_greatness() >= SUFFIX_UNLOCK_GREANTESS) {
stats
.apply_suffix_boost(
ImplItemSpecials::get_specials_full(name_storage1, name_storage2, self.foot)
.special1
);
}

if (self.hand.get_greatness() >= 15) {
if (self.hand.get_greatness() >= SUFFIX_UNLOCK_GREANTESS) {
stats
.apply_suffix_boost(
ImplItemSpecials::get_specials_full(name_storage1, name_storage2, self.hand)
.special1
);
}

if (self.neck.get_greatness() >= 15) {
if (self.neck.get_greatness() >= SUFFIX_UNLOCK_GREANTESS) {
stats
.apply_suffix_boost(
ImplItemSpecials::get_specials_full(name_storage1, name_storage2, self.neck)
.special1
);
}

if (self.ring.get_greatness() >= 15) {
if (self.ring.get_greatness() >= SUFFIX_UNLOCK_GREANTESS) {
stats
.apply_suffix_boost(
ImplItemSpecials::get_specials_full(name_storage1, name_storage2, self.ring)
Expand Down Expand Up @@ -4069,11 +4070,11 @@ mod tests {

// test base case
adventurer.stats.strength = 4;
adventurer.remove_suffix_boost(1);
adventurer.stats.remove_suffix_boost(1);
assert(adventurer.stats.strength == 1, 'strength should be 1');

// underflow check
adventurer.remove_suffix_boost(1);
adventurer.stats.remove_suffix_boost(1);
assert(adventurer.stats.strength == 0, 'strength should still be 0');
}

Expand Down
14 changes: 13 additions & 1 deletion contracts/adventurer/src/adventurer_meta.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use starknet::{StorePacking};
use traits::{TryInto, Into};
use super::stats::{Stats, StatsPacking};
use super::stats::{Stats, StatsPacking, StatUtils};

#[derive(Drop, Copy, Serde)]
struct AdventurerMetadata {
Expand Down Expand Up @@ -34,6 +34,18 @@ impl PackingAdventurerMetadata of StorePacking<AdventurerMetadata, felt252> {
}
}

#[generate_trait]
impl ImplAdventurerMetadata of IAdventurerMetadata {
// @notice: Creates a new AdventurerMetadata struct
// @dev: AdventurerMetadata is initialized without any starting stats
// @param name: The name of the adventurer
// @param start_block: The block number at which the adventurer was created
// @return: The newly created AdventurerMetadata struct
fn new(name: u128, start_block: u64) -> AdventurerMetadata {
AdventurerMetadata { name, start_block, starting_stats: StatUtils::new() }
}
}

const TWO_POW_24: u256 = 0x1000000;
const TWO_POW_64: u256 = 0x10000000000000000;
const TWO_POW_88: u256 = 0x10000000000000000000000;
Expand Down
2 changes: 1 addition & 1 deletion contracts/adventurer/src/bag.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ mod tests {

#[test]
#[available_gas(2383150)]
fn test_pack_bag() {
fn test_save_bag() {
let mut bag = Bag {
item_1: ItemPrimitive { id: 127, xp: 511, metadata: 31 },
item_2: ItemPrimitive { id: 127, xp: 511, metadata: 31 },
Expand Down
Loading

1 comment on commit d99116c

@vercel
Copy link

@vercel vercel bot commented on d99116c Oct 13, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.