Skip to content

Commit

Permalink
refactor: update command naming (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
tarrencev authored Apr 6, 2023
1 parent 5c663f1 commit 184eb7f
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 39 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ Systems are functions operating on the world state. They receive some input from
fn commands::uuid() -> felt252;

// Update an existing entity with the provided components.
fn commands::set(storage_key: StorageKey, components: T);
fn commands::set_entity(storage_key: StorageKey, components: T);

// Retreive a components for an entity.
fn commands::<T>::get(storage_key: StorageKey) -> T;
fn commands::<T>::entity(storage_key: StorageKey) -> T;

// Retreive all entity ids that match the component selector criteria.
fn commands::<T>::entities() -> Array<felt252>;
Expand All @@ -92,7 +92,7 @@ mod SpawnSystem {
#[system]
mod MoveSystem {
fn execute(player_id: usize) {
let player = commands<(Health, Name)>::get(player_id);
let player = commands<(Health, Name)>::entity(player_id);
let positions = commands<(Position, Health)>::entities();

// @NOTE: Loops are not available in Cairo 1.0 yet.
Expand Down
2 changes: 1 addition & 1 deletion crates/dojo-erc/src/erc20/erc20.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ mod ERC20 {
IWorldDispatcher { contract_address: world_address::read() }.execute('ERC20_TransferFrom', calldata.span());

let approval_sk: StorageKey = (token_id, (caller.into(), spender)).into();
let approval = commands::<Approval>::get(approval_sk);
let approval = commands::<Approval>::entity(approval_sk);

Transfer(spender, recipient, amount);
Approval(get_caller_address(),spender,approval.amount);
Expand Down
16 changes: 8 additions & 8 deletions crates/dojo-erc/src/erc20/systems.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ mod ERC20_Approve {
execute(token_id: felt252, spender: ContractAddress, amount: felt252) {
let caller = starknet::get_caller_address();
let approval_sk: StorageKey = (token_id, (caller.into(), spender)).into();
let approval = commands::<Approval>::get(approval_sk);
commands::set(approval_sk, (
let approval = commands::<Approval>::entity(approval_sk);
commands::set_entity(approval_sk, (
Approval { amount: amount }
))
}
Expand All @@ -28,21 +28,21 @@ mod ERC20_TransferFrom {
let spender_ownership_sk: StorageKey = (token_address, (spender)).into();
let recipient_ownership_sk: StorageKey = (token_address, (recipient)).into();

let spen_ownershipder = commands::<Ownership>::get(spender_ownership_sk);
commands::set(spender_ownership_sk, (
let spen_ownershipder = commands::<Ownership>::entity(spender_ownership_sk);
commands::set_entity(spender_ownership_sk, (
Ownership { balance : spen_ownershipder.balance - amount}
));

let recipient_ownership = commands::<Ownership>::get(recipient_ownership_sk);
commands::set(recipient_ownership_sk, (
let recipient_ownership = commands::<Ownership>::entity(recipient_ownership_sk);
commands::set_entity(recipient_ownership_sk, (
Ownership { balance : ownership.balance + amount}
));

//update allowance
let approval_sk: StorageKey = (token_address, (caller.into(), spender)).into();
let approval = commands::<Approval>::get(approval_sk);
let approval = commands::<Approval>::entity(approval_sk);

commands::set(approval_sk, (
commands::set_entity(approval_sk, (
Approval { amount: approval.amount - amount }
))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ use sanitizer::StringSanitizer;
use super::entities::find_components;
use super::{CommandData, CommandTrait};

pub struct GetCommand {
pub struct EntityCommand {
query_id: String,
query_pattern: String,
data: CommandData,
}

impl CommandTrait for GetCommand {
impl CommandTrait for EntityCommand {
fn from_ast(
db: &dyn SyntaxGroup,
let_pattern: Option<ast::Pattern>,
Expand All @@ -25,7 +25,7 @@ impl CommandTrait for GetCommand {
let var_name = let_pattern.unwrap();
let mut query_id = StringSanitizer::from(var_name.as_syntax_node().get_text(db));
query_id.to_snake_case();
let mut command = GetCommand {
let mut command = EntityCommand {
query_id: query_id.get(),
query_pattern: var_name.as_syntax_node().get_text(db),
data: CommandData::new(),
Expand Down
8 changes: 4 additions & 4 deletions crates/dojo-lang/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use cairo_lang_syntax::node::{ast, Terminal};
use smol_str::SmolStr;

pub mod entities;
pub mod entity;
pub mod execute;
pub mod get;
pub mod set;
pub mod uuid;

Expand Down Expand Up @@ -53,12 +53,12 @@ impl Command {
command.rewrite_nodes.extend(sc.rewrite_nodes());
command.diagnostics.extend(sc.diagnostics());
}
"get" => {
let sc = get::GetCommand::from_ast(db, let_pattern, command_ast);
"entity" => {
let sc = entity::EntityCommand::from_ast(db, let_pattern, command_ast);
command.rewrite_nodes.extend(sc.rewrite_nodes());
command.diagnostics.extend(sc.diagnostics());
}
"set" => {
"set_entity" => {
let sc = set::SetCommand::from_ast(db, let_pattern, command_ast);
command.rewrite_nodes.extend(sc.rewrite_nodes());
command.diagnostics.extend(sc.diagnostics());
Expand Down
34 changes: 17 additions & 17 deletions crates/dojo-lang/src/plugin_test_data/system
Original file line number Diff line number Diff line change
Expand Up @@ -33,33 +33,33 @@ mod Spawn {

fn execute(name: felt252) {
let uuid = commands::uuid();
commands::set((420, (69, uuid)).into(), (
commands::set_entity((420, (69, uuid)).into(), (
Player { name: name },
Position { x: 0, y: 0 },
));

commands::set(420.into(), (
commands::set_entity(420.into(), (
Player { name: name },
Position { x: 0, y: 0 },
));

let player_id = starknet::get_caller_address();
commands::set(player_id.into(), (
commands::set_entity(player_id.into(), (
Player { name: name },
Position { x: 0, y: 0 },
));

commands::set((
commands::set_entity((
Player { name: name },
Position { x: 0, y: 0 },
));

commands::set((0, 0, 0, 0, 0), (
commands::set_entity((0, 0, 0, 0, 0), (
Player { name: name },
Position { x: 0, y: 0 },
));

commands::set(1337.into(), (
commands::set_entity(1337.into(), (
Player { name: name },
Position { x: 0, y: 0 },
));
Expand All @@ -81,39 +81,39 @@ mod Move {
fn execute(player_id: felt252) {
let positions_query = commands::<(Position, Player)>::entities();
let players_query = commands::<Player>::entities();
let player = commands::<Player>::get(player_id.into());
let player_position = commands::<(Position, Player)>::get(player_id.into());
let player = commands::<Player>::entity(player_id.into());
let player_position = commands::<(Position, Player)>::entity(player_id.into());

let mut bar = 123;
let mut foo = ArrayTrait::<felt252>::new();
foo.append(bar);

if bar == 123 {
let player_position = commands::<(Position, Player)>::get(player_id.into());
let player_position = commands::<(Position, Player)>::entity(player_id.into());
if foo.len() > 0_u32 {
let positions_query = commands::<(Position, Player)>::entities();
}
} else if bar == 321 {
let player_position = commands::<(Position, Player)>::get(player_id.into());
let player_position = commands::<(Position, Player)>::entity(player_id.into());
} else {
let player_position = commands::<(Position, Player)>::get(player_id.into());
let player_position = commands::<(Position, Player)>::entity(player_id.into());
}

{
let player_position = commands::<(Position, Player)>::get(player_id.into());
let player_position = commands::<(Position, Player)>::entity(player_id.into());
}

let foo_bar = Option::Some(1);
match foo_bar {
Option::Some(x) => {
let bar = x;
let player_position = commands::<(Position, Player)>::get(player_id.into());
let player_position = commands::<(Position, Player)>::entity(player_id.into());
if bar == 123 {
let positions_query = commands::<(Position, Player)>::entities();
}
},
Option::None(()) => {
let player_position = commands::<(Position, Player)>::get(player_id.into());
let player_position = commands::<(Position, Player)>::entity(player_id.into());
}
}

Expand Down Expand Up @@ -633,9 +633,9 @@ mod MoveSystem {

//! > expected_diagnostics
error: Plugin diagnostic: Invalid arguments. Expected "(storage_key, (components,))"
--> lib.cairo:46:22
commands::set((
^^
--> lib.cairo:46:29
commands::set_entity((
^^

error: Plugin diagnostic: Unexpected argument type. Expected: "dojo_core::storage::key::StorageKey", found: "(core::felt252, core::felt252, core::felt252, core::felt252, core::felt252)".
--> Spawn:51:88
Expand Down
6 changes: 3 additions & 3 deletions examples/src/systems.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mod Spawn {

fn execute() {
let caller = starknet::get_caller_address();
let player = commands::set(caller.into(), (
let player = commands::set_entity(caller.into(), (
Moves { remaining: 10_u8 },
Position { x: 0_u32, y: 0_u32 },
));
Expand All @@ -39,9 +39,9 @@ mod Move {
// left: 0, right: 1, up: 2, down: 3
fn execute(direction: felt252) {
let caller = starknet::get_caller_address();
let (position, moves) = commands::<Position, Moves>::get(caller.into());
let (position, moves) = commands::<Position, Moves>::entity(caller.into());
let next = next_position(position.unwrap(), direction);
let uh = commands::set(caller.into(), (
let uh = commands::set_entity(caller.into(), (
Moves { remaining: moves.unwrap().remaining - 1_u8 },
Position { x: next.x, y: next.y },
));
Expand Down

0 comments on commit 184eb7f

Please sign in to comment.