Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

removes concept of item metadata #11

Merged
merged 2 commits into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ jobs:
market,
obstacles,
combat,
game_snapshot,
]
steps:
- uses: actions/checkout@v3
Expand Down
1,097 changes: 178 additions & 919 deletions contracts/adventurer/src/adventurer.cairo

Large diffs are not rendered by default.

31 changes: 20 additions & 11 deletions contracts/adventurer/src/adventurer_meta.cairo
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use core::integer::u256_try_as_non_zero;
use starknet::{StorePacking};
use traits::{TryInto, Into};
use super::stats::{Stats, StatsPacking, StatUtils};

#[derive(Drop, Copy, Serde)]
struct AdventurerMetadata {
start_block: u64, // 64 bits in storage
start_entropy: u64, // 64 bits in storage
starting_stats: Stats, // 24 bits in storage
name: u128, // 128 bits in storage
interface_camel: bool, // 1 bit bool in storage
Expand All @@ -18,7 +19,7 @@ impl PackingAdventurerMetadata of StorePacking<AdventurerMetadata, felt252> {
0
};

(value.start_block.into()
(value.start_entropy.into()
+ StatsPacking::pack(value.starting_stats).into() * TWO_POW_64
+ interface_camel_u256 * TWO_POW_91
+ value.name.into() * TWO_POW_92)
Expand All @@ -27,7 +28,7 @@ impl PackingAdventurerMetadata of StorePacking<AdventurerMetadata, felt252> {
}
fn unpack(value: felt252) -> AdventurerMetadata {
let packed = value.into();
let (packed, start_block) = integer::U256DivRem::div_rem(
let (packed, start_entropy) = integer::U256DivRem::div_rem(
packed, TWO_POW_64.try_into().unwrap()
);
let (packed, starting_stats) = integer::U256DivRem::div_rem(
Expand All @@ -41,7 +42,7 @@ impl PackingAdventurerMetadata of StorePacking<AdventurerMetadata, felt252> {
let interface_camel = interface_camel_u256 == 1;

AdventurerMetadata {
start_block: start_block.try_into().unwrap(),
start_entropy: start_entropy.try_into().unwrap(),
starting_stats: StatsPacking::unpack(starting_stats.try_into().unwrap()),
name: name.try_into().unwrap(),
interface_camel
Expand All @@ -54,11 +55,17 @@ 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
// @param interface_camel: Whether the players account is using a camelcase interface
// @return: The newly created AdventurerMetadata struct
fn new(name: u128, start_block: u64, interface_camel: bool) -> AdventurerMetadata {
AdventurerMetadata { name, start_block, starting_stats: StatUtils::new(), interface_camel }
fn new(name: u128, interface_camel: bool) -> AdventurerMetadata {
AdventurerMetadata { name, start_entropy: 0, starting_stats: StatUtils::new(), interface_camel }
}

fn generate_start_entropy(adventurer_entropy: felt252) -> u64 {
let (_, r) = integer::U256DivRem::div_rem(
adventurer_entropy.into(), u256_try_as_non_zero(U64_MAX.into()).unwrap()
);
r.try_into().unwrap()
}
}

Expand All @@ -71,6 +78,8 @@ const TWO_POW_128: u256 = 0x100000000000000000000000000000000;
const TWO_POW_91: u256 = 0x80000000000000000000000; // 2^91
const TWO_POW_92: u256 = 0x100000000000000000000000; // 2^92
const TWO_POW_27: u256 = 0x8000000; // 2^27
const U64_MAX: u64 = 18446744073709551615;


#[cfg(test)]
#[test]
Expand All @@ -91,7 +100,7 @@ fn test_adventurer_metadata_packing() {
let interface_camel = true;

let meta = AdventurerMetadata {
start_block: max_u64,
start_entropy: max_u64,
starting_stats: max_starting_stats,
name: max_name_length,
interface_camel
Expand All @@ -100,7 +109,7 @@ fn test_adventurer_metadata_packing() {
let packed = PackingAdventurerMetadata::pack(meta);
let unpacked: AdventurerMetadata = PackingAdventurerMetadata::unpack(packed);
assert(meta.name == unpacked.name, 'name should be max');
assert(meta.start_block == unpacked.start_block, 'sblock should be max u64');
assert(meta.start_entropy == unpacked.start_entropy, 'sblock should be max u64');
assert(
meta.starting_stats.strength == unpacked.starting_stats.strength, 'strength should be max'
);
Expand All @@ -127,12 +136,12 @@ fn test_adventurer_metadata_packing() {
};
let interface_camel = false;
let meta = AdventurerMetadata {
start_block: 0, starting_stats: zero_starting_stats, name: 0, interface_camel
start_entropy: 0, starting_stats: zero_starting_stats, name: 0, interface_camel
};
let packed = PackingAdventurerMetadata::pack(meta);
let unpacked: AdventurerMetadata = PackingAdventurerMetadata::unpack(packed);
assert(unpacked.name == 0, 'name should be 0');
assert(unpacked.start_block == 0, 'entropy should be 0');
assert(unpacked.start_entropy == 0, 'entropy should be 0');
assert(
meta.starting_stats.strength == unpacked.starting_stats.strength, 'strength should be 0'
);
Expand Down
Loading
Loading