-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/BibliothecaDAO/loot-survivor
- Loading branch information
Showing
8 changed files
with
411 additions
and
323 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,77 @@ | ||
use starknet::{StorePacking}; | ||
use traits::{TryInto, Into}; | ||
use super::stats::{Stats, StatsPacking}; | ||
|
||
#[derive(Drop, Copy, Serde)] | ||
struct AdventurerMetadata { | ||
name: u128, | ||
entropy: u128, | ||
start_block: u64, // 64 bits | ||
starting_stats: Stats, // 24 bits | ||
name: u128, // 128 bits | ||
} | ||
|
||
impl PackingAdventurerMetadata of StorePacking<AdventurerMetadata, felt252> { | ||
fn pack(value: AdventurerMetadata) -> felt252 { | ||
(value.entropy.into() + value.name.into() * TWO_POW_128).try_into().unwrap() | ||
(value.start_block.into() | ||
+ StatsPacking::pack(value.starting_stats).into() * TWO_POW_64 | ||
+ value.name.into() * TWO_POW_88) | ||
.try_into() | ||
.unwrap() | ||
} | ||
fn unpack(value: felt252) -> AdventurerMetadata { | ||
let packed = value.into(); | ||
let (packed, entropy) = integer::U256DivRem::div_rem( | ||
packed, TWO_POW_128.try_into().unwrap() | ||
let (packed, start_block) = integer::U256DivRem::div_rem( | ||
packed, TWO_POW_64.try_into().unwrap() | ||
); | ||
let (packed, starting_stats) = integer::U256DivRem::div_rem( | ||
packed, TWO_POW_24.try_into().unwrap() | ||
); | ||
let (_, name) = integer::U256DivRem::div_rem(packed, TWO_POW_128.try_into().unwrap()); | ||
AdventurerMetadata { name: name.try_into().unwrap(), entropy: entropy.try_into().unwrap() } | ||
AdventurerMetadata { | ||
start_block: start_block.try_into().unwrap(), | ||
starting_stats: StatsPacking::unpack(starting_stats.try_into().unwrap()), | ||
name: name.try_into().unwrap() | ||
} | ||
} | ||
} | ||
|
||
const TWO_POW_24: u256 = 0x1000000; | ||
const TWO_POW_64: u256 = 0x10000000000000000; | ||
const TWO_POW_88: u256 = 0x10000000000000000000000; | ||
const TWO_POW_128: u256 = 0x100000000000000000000000000000000; | ||
|
||
#[cfg(test)] | ||
#[test] | ||
#[available_gas(116600)] | ||
#[available_gas(1187400)] | ||
fn test_adventurer_metadata_packing() { | ||
// max value case | ||
let max_u128 = 340282366920938463463374607431768211455; | ||
let name_length = 'abcdefghijklmno'; | ||
let max_u64 = 0xffffffffffffffff; | ||
let max_name_length = 'abcdefghijklmnop'; | ||
let max_starting_stats = Stats { | ||
strength: 15, | ||
dexterity: 15, | ||
vitality: 15, | ||
intelligence: 15, | ||
wisdom: 15, | ||
charisma: 15, | ||
luck: 15 | ||
}; | ||
|
||
let meta = AdventurerMetadata { | ||
start_block: max_u64, starting_stats: max_starting_stats, name: max_name_length | ||
}; | ||
|
||
let meta = AdventurerMetadata { name: name_length, entropy: max_u128 }; | ||
|
||
let packed = PackingAdventurerMetadata::pack(meta); | ||
let unpacked: AdventurerMetadata = PackingAdventurerMetadata::unpack(packed); | ||
assert(meta.name == unpacked.name, 'name should be max'); | ||
assert(meta.entropy == unpacked.entropy, 'entropy should be max u128'); | ||
assert(meta.start_block == unpacked.start_block, 'sblock should be max u64'); | ||
|
||
// zero case | ||
let meta = AdventurerMetadata { name: 0, entropy: 0 }; | ||
let zero_starting_stats = Stats { | ||
strength: 0, dexterity: 0, vitality: 0, intelligence: 0, wisdom: 0, charisma: 0, luck: 0 | ||
}; | ||
let meta = AdventurerMetadata { start_block: 0, starting_stats: zero_starting_stats, name: 0 }; | ||
let packed = PackingAdventurerMetadata::pack(meta); | ||
let unpacked: AdventurerMetadata = PackingAdventurerMetadata::unpack(packed); | ||
assert(unpacked.name == 0, 'name should be 0'); | ||
assert(unpacked.entropy == 0, 'entropy should be 0'); | ||
} | ||
assert(unpacked.start_block == 0, 'entropy should be 0'); | ||
} |
Oops, something went wrong.