Skip to content

Commit

Permalink
packing
Browse files Browse the repository at this point in the history
  • Loading branch information
Larkooo committed Sep 4, 2024
1 parent f1521be commit a98f39f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 35 deletions.
4 changes: 2 additions & 2 deletions manifests/dev/base/contracts/flippyflop-actions-4407e5f5.toml
Original file line number Diff line number Diff line change
@@ -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 = []
Expand Down
4 changes: 3 additions & 1 deletion src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ mod tests {
mod constants;
mod tokens {
mod flip;
}
}

mod packing;
35 changes: 35 additions & 0 deletions src/packing.cairo
Original file line number Diff line number Diff line change
@@ -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)
}
33 changes: 1 addition & 32 deletions src/systems/actions.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")) {
Expand Down

0 comments on commit a98f39f

Please sign in to comment.