Skip to content

Commit

Permalink
txs from felt252 array
Browse files Browse the repository at this point in the history
  • Loading branch information
chudkowsky committed Aug 22, 2024
1 parent 6cf9b49 commit b339ec3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 20 deletions.
68 changes: 48 additions & 20 deletions dojo_bin/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,63 @@ use starknet::ContractAddress;
use models::Direction;
use systems::actions::actions::ActionsImpl;
use systems::actions::actions::WorldState;
use core::dict::Felt252Dict;

#[derive(Drop)]
#[derive(Drop,Serde,Clone,Copy,Debug)]
enum TransactionType {
Spawn: ContractAddress,
Move: (ContractAddress, models::Direction)
}

fn main(input:Array<felt252>) -> Array<felt252> {
fn main(input: Array<felt252>) -> Array<felt252> {
//todo: implement deserialize input for player, world and array of transactions
let player = contract_address_const::<0>();
let mut world = WorldState { positions: Default::default(), moves: Default::default(), };
let txns = array![
TransactionType::Spawn(player),
TransactionType::Move((player, models::Direction::Up)),
TransactionType::Move((player, models::Direction::Up)),
];
let mut input = input.span();
let mut txns:Array::<TransactionType> = Serde::deserialize(ref input).unwrap();
let mut world = WorldState { positions: Default::default(), moves: Default::default(),};
let mut players: Array<ContractAddress> = ArrayTrait::new();
let mut output: Array<felt252> = ArrayTrait::new();
for txn in txns {
match txn {
TransactionType::Spawn(player) => { ActionsImpl::spawn(ref world, player) },
TransactionType::Spawn(player) => {
ActionsImpl::spawn(ref world, player);
let mut player_exists = false;
for p in players.clone() {
if p == player {
player_exists = true;
}
};

if !player_exists {
players.append(player);
}
},
TransactionType::Move((
player, direction
)) => { ActionsImpl::move(ref world, player, direction) },
}
)) => {
ActionsImpl::move(ref world, player, direction);
let mut player_exists = false;
for p in players.clone() {
if p == player {
player_exists = true;
}
};
if !player_exists {
players.append(player);
}
},
}
};
let mut output: Array<felt252> = ArrayTrait::new();
player.serialize(ref output);
let position = world.positions.get(player.into()).deref();
position.serialize(ref output);
let moves = world.moves.get(player.into()).deref();
moves.serialize(ref output);
output}
//[0, 0, 10, 10, 0, 98, 3, 1]

for player in players {
let position = world.positions.get(player.into()).deref();
let moves = world.moves.get(player.into()).deref();
// println!("player: {:?}, position: {:?}, moves: {:?}", player,position,moves);
player.serialize(ref output);
position.serialize(ref output);
moves.serialize(ref output);
};
println!("{:?}", output);
output
}
//[0, 0, 10, 10, 0, 98, 3, 1]

1 change: 1 addition & 0 deletions dojo_bin/src/systems/actions.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub mod actions {
world.moves.insert(player.into(), box_moves);
return;
} else {
let position = next_position(position, direction);
let box_position = NullableTrait::new(position);
world.positions.insert(player.into(), box_position);
let moves = Moves {
Expand Down

0 comments on commit b339ec3

Please sign in to comment.