diff --git a/manifests/dev/base/contracts/flippyflop-actions-4407e5f5.toml b/manifests/dev/base/contracts/flippyflop-actions-4407e5f5.toml index 98a4840..5a119b1 100644 --- a/manifests/dev/base/contracts/flippyflop-actions-4407e5f5.toml +++ b/manifests/dev/base/contracts/flippyflop-actions-4407e5f5.toml @@ -1,6 +1,6 @@ kind = "DojoContract" -class_hash = "0x2792b58a3d449e7ac8967c2b7e09b53d01759f728c91dbff0f2c3def28ebfcf" -original_class_hash = "0x2792b58a3d449e7ac8967c2b7e09b53d01759f728c91dbff0f2c3def28ebfcf" +class_hash = "0x7cd25945ea14b7d237c2f350e9f786a85e75e9798182bf62fa5c65c54f683a0" +original_class_hash = "0x7cd25945ea14b7d237c2f350e9f786a85e75e9798182bf62fa5c65c54f683a0" base_class_hash = "0x0" abi = "manifests/dev/base/abis/contracts/flippyflop-actions-4407e5f5.json" reads = [] diff --git a/src/lib.cairo b/src/lib.cairo index 44530bc..adf87fb 100644 --- a/src/lib.cairo +++ b/src/lib.cairo @@ -11,4 +11,6 @@ mod tests { mod constants; mod tokens { mod flip; -} \ No newline at end of file +} + +mod packing; \ No newline at end of file diff --git a/src/packing.cairo b/src/packing.cairo new file mode 100644 index 0000000..d762efc --- /dev/null +++ b/src/packing.cairo @@ -0,0 +1,35 @@ +use starknet::ContractAddress; +use flippyflop::models::PowerUp; +use flippyflop::constants::{ADDRESS_MASK, POWERUP_MASK, POWERUP_DATA_MASK}; + +fn pack_flipped_data(address: felt252, powerup: PowerUp) -> felt252 { + let address_bits: u256 = address.into(); + let (powerup_type, powerup_data) = match powerup { + PowerUp::None => (0_u256, 0_u256), + PowerUp::Empty => (1_u256, 0_u256), + PowerUp::Multiplier(multiplier) => (2_u256, multiplier.into()), + }; + + let mut packed: u256 = 0_u256; + packed = packed | (address_bits & ADDRESS_MASK); + packed = packed | ((powerup_type * 256_u256) & POWERUP_MASK); + packed = packed | (powerup_data & POWERUP_DATA_MASK); + + packed.try_into().unwrap() +} + +fn unpack_flipped_data(flipped: felt252) -> (ContractAddress, PowerUp) { + let flipped_u256: u256 = flipped.into(); + let address: felt252 = (flipped_u256 & ADDRESS_MASK).try_into().unwrap(); + let powerup_type: felt252 = ((flipped_u256 & POWERUP_MASK) / 256_u256).try_into().unwrap(); + let powerup_data = flipped_u256 & POWERUP_DATA_MASK; + + let powerup = match powerup_type { + 0 => PowerUp::None, + 1 => PowerUp::Empty, + 2 => PowerUp::Multiplier(powerup_data.try_into().unwrap()), + _ => PowerUp::None, + }; + + (address.try_into().unwrap(), powerup) +} \ No newline at end of file diff --git a/src/systems/actions.cairo b/src/systems/actions.cairo index bb8caca..13237b3 100644 --- a/src/systems/actions.cairo +++ b/src/systems/actions.cairo @@ -16,6 +16,7 @@ mod actions { use dojo::model::{FieldLayout, Layout}; use flippyflop::tokens::flip::{IFlip, IFlipDispatcher, IFlipDispatcherTrait}; use flippyflop::constants::{GAME_ID, ADDRESS_MASK, POWERUP_MASK, POWERUP_DATA_MASK, X_BOUND, Y_BOUND, TILE_MODEL_SELECTOR}; + use flippyflop::packing::{pack_flipped_data, unpack_flipped_data}; fn get_random_powerup(seed: felt252) -> PowerUp { let tx_hash = get_tx_info().transaction_hash; @@ -39,38 +40,6 @@ mod actions { } } - fn pack_flipped_data(address: felt252, powerup: PowerUp) -> felt252 { - let address_bits: u256 = address.into(); - let (powerup_type, powerup_data) = match powerup { - PowerUp::None => (0_u256, 0_u256), - PowerUp::Empty => (1_u256, 0_u256), - PowerUp::Multiplier(multiplier) => (2_u256, multiplier.into()), - }; - - let mut packed: u256 = 0_u256; - packed = packed | (address_bits & ADDRESS_MASK); - packed = packed | ((powerup_type * 256_u256) & POWERUP_MASK); - packed = packed | (powerup_data & POWERUP_DATA_MASK); - - packed.try_into().unwrap() - } - - fn unpack_flipped_data(flipped: felt252) -> (ContractAddress, PowerUp) { - let flipped_u256: u256 = flipped.into(); - let address: felt252 = (flipped_u256 & ADDRESS_MASK).try_into().unwrap(); - let powerup_type: felt252 = ((flipped_u256 & POWERUP_MASK) / 256_u256).try_into().unwrap(); - let powerup_data = flipped_u256 & POWERUP_DATA_MASK; - - let powerup = match powerup_type { - 0 => PowerUp::None, - 1 => PowerUp::Empty, - 2 => PowerUp::Multiplier(powerup_data.try_into().unwrap()), - _ => PowerUp::None, - }; - - (address.try_into().unwrap(), powerup) - } - fn flip_token(world: IWorldDispatcher) -> IFlipDispatcher { let (class_hash, contract_address) = match world.resource(selector_from_tag!("flippyflop-FLIP")) {