Skip to content

Commit

Permalink
Update name
Browse files Browse the repository at this point in the history
  • Loading branch information
web3olalala committed Sep 24, 2023
1 parent 5532c39 commit b908b30
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 56 deletions.
26 changes: 13 additions & 13 deletions examples/sources/codegen/components/multi_column.move
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ module examples::multi_column_comp {
// Systems
friend examples::example_system;

const COMPONENT_NAME: vector<u8> = b"multi_column";
const NAME: vector<u8> = b"multi_column";

public fun id(): address {
entity_key::from_bytes(COMPONENT_NAME)
entity_key::from_bytes(NAME)
}

// state
Expand All @@ -28,35 +28,35 @@ module examples::multi_column_comp {
}

public fun register(world: &mut World, ctx: &mut TxContext) {
world::add_component<Table<address,Field>>(
world::add_comp<Table<address,Field>>(
world,
COMPONENT_NAME,
NAME,
table::new<address,Field>(ctx)
);
}

public(friend) fun add(world: &mut World, key: address, state: vector<u8>, last_update_time: u64) {
let component = world::get_mut_component<Table<address,Field>>(world, id());
let component = world::get_mut_comp<Table<address,Field>>(world, id());
let data = encode(state, last_update_time);
table::add(component, key, Field { data });
world::emit_add_event(id(), key, data)
}

public(friend) fun remove(world: &mut World, key: address) {
let component = world::get_mut_component<Table<address,Field>>(world, id());
let component = world::get_mut_comp<Table<address,Field>>(world, id());
table::remove(component, key);
world::emit_remove_event(id(), key)
}

public(friend) fun update(world: &mut World, key: address, state: vector<u8>, last_update_time: u64) {
let component = world::get_mut_component<Table<address, Field>>(world, id());
let component = world::get_mut_comp<Table<address, Field>>(world, id());
let field = table::borrow_mut<address, Field>(component, key);
let data = encode(state, last_update_time);
field.data = data;
world::emit_update_event(id(), some(key), data)
}
public(friend) fun update_state(world: &mut World, key: address, state: vector<u8>) {
let component = world::get_mut_component<Table<address,Field>>(world, id());
let component = world::get_mut_comp<Table<address,Field>>(world, id());
let field = table::borrow_mut<address, Field>(component, key);
let (_, last_update_time) = decode(field.data);
let data = encode(state, last_update_time);
Expand All @@ -65,7 +65,7 @@ module examples::multi_column_comp {
}

public(friend) fun update_last_update_time(world: &mut World, key: address, last_update_time: u64) {
let component = world::get_mut_component<Table<address,Field>>(world, id());
let component = world::get_mut_comp<Table<address,Field>>(world, id());
let field = table::borrow_mut<address, Field>(component, key);
let (state, _) = decode(field.data);
let data = encode(state, last_update_time);
Expand All @@ -74,27 +74,27 @@ module examples::multi_column_comp {
}

public fun get(world: &World, key: address): (vector<u8>,u64) {
let component = world::get_component<Table<address,Field>>(world, id());
let component = world::get_comp<Table<address,Field>>(world, id());
let field = table::borrow<address, Field>(component, key);
decode(field.data)
}

public fun get_state(world: &World, key: address): vector<u8> {
let component = world::get_component<Table<address,Field>>(world, id());
let component = world::get_comp<Table<address,Field>>(world, id());
let field = table::borrow<address, Field>(component, key);
let (state, _) = decode(field.data);
state
}

public fun get_last_update_time(world: &World, key: address): u64 {
let component = world::get_component<Table<address,Field>>(world, id());
let component = world::get_comp<Table<address,Field>>(world, id());
let field = table::borrow<address, Field>(component, key);
let (_, last_update_time) = decode(field.data);
last_update_time
}

public fun contains(world: &World, key: address): bool {
let component = world::get_component<Table<address,Field>>(world, id());
let component = world::get_comp<Table<address,Field>>(world, id());
table::contains<address, Field>(component, key)
}

Expand Down
18 changes: 9 additions & 9 deletions examples/sources/codegen/components/single_column.move
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ module examples::single_column_comp {
// Systems
friend examples::example_system;

const COMPONENT_NAME: vector<u8> = b"single_column";
const NAME: vector<u8> = b"single_column";

public fun id(): address {
entity_key::from_bytes(COMPONENT_NAME)
entity_key::from_bytes(NAME)
}

// value
Expand All @@ -27,42 +27,42 @@ module examples::single_column_comp {
}

public fun register(world: &mut World, ctx: &mut TxContext) {
world::add_component<Table<address,Field>>(
world::add_comp<Table<address,Field>>(
world,
COMPONENT_NAME,
NAME,
table::new<address,Field>(ctx)
);
}

public(friend) fun add(world: &mut World, key: address, value: u64) {
let component = world::get_mut_component<Table<address,Field>>(world, id());
let component = world::get_mut_comp<Table<address,Field>>(world, id());
let data = encode(value);
table::add(component, key, Field { data });
world::emit_add_event(id(), key, data)
}

public(friend) fun remove(world: &mut World, key: address) {
let component = world::get_mut_component<Table<address,Field>>(world, id());
let component = world::get_mut_comp<Table<address,Field>>(world, id());
table::remove(component, key);
world::emit_remove_event(id(), key)
}

public(friend) fun update(world: &mut World, key: address, value: u64) {
let component = world::get_mut_component<Table<address, Field>>(world, id());
let component = world::get_mut_comp<Table<address, Field>>(world, id());
let field = table::borrow_mut<address, Field>(component, key);
let data = encode(value);
field.data = data;
world::emit_update_event(id(), some(key), data)
}

public fun get(world: &World, key: address): u64 {
let component = world::get_component<Table<address,Field>>(world, id());
let component = world::get_comp<Table<address,Field>>(world, id());
let field = table::borrow<address, Field>(component, key);
decode(field.data)
}

public fun contains(world: &World, key: address): bool {
let component = world::get_component<Table<address,Field>>(world, id());
let component = world::get_comp<Table<address,Field>>(world, id());
table::contains<address, Field>(component, key)
}

Expand Down
20 changes: 10 additions & 10 deletions examples/sources/codegen/components/single_struct.move
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ module examples::single_struct_comp {
// Systems
friend examples::example_system;

const COMPONENT_NAME: vector<u8> = b"single_struct";
const NAME: vector<u8> = b"single_struct";

public fun id(): address {
entity_key::from_bytes(COMPONENT_NAME)
entity_key::from_bytes(NAME)
}

// admin
Expand All @@ -26,45 +26,45 @@ module examples::single_struct_comp {
}

public fun register(world: &mut World) {
world::add_component<Field>(
world::add_comp<Field>(
world,
COMPONENT_NAME,
NAME,
Field { data: encode(@0x1, 100) }
);
}

public(friend) fun update(world: &mut World, admin: address, fee: u64) {
let data = encode(admin, fee);
world::get_mut_component<Field>(world, id()).data = data;
world::get_mut_comp<Field>(world, id()).data = data;
world::emit_update_event(id(), none(), data)
}
public(friend) fun update_admin(world: &mut World, admin: address) {
let field = world::get_mut_component<Field>(world, id());
let field = world::get_mut_comp<Field>(world, id());
let (_, fee) = decode(field.data);
field.data = encode(admin, fee);
world::emit_update_event(id(), none(), field.data)
}

public(friend) fun update_fee(world: &mut World, fee: u64) {
let field = world::get_mut_component<Field>(world, id());
let field = world::get_mut_comp<Field>(world, id());
let (admin, _) = decode(field.data);
field.data = encode(admin, fee);
world::emit_update_event(id(), none(), field.data)
}

public fun get(world: &World): (address,u64) {
let data = world::get_component<Field>(world, id()).data;
let data = world::get_comp<Field>(world, id()).data;
decode(data)
}

public fun get_admin(world: &World): address {
let data = world::get_component<Field>(world, id()).data;
let data = world::get_comp<Field>(world, id()).data;
let (admin, _) = decode(data);
admin
}

public fun get_fee(world: &World): u64 {
let data = world::get_component<Field>(world, id()).data;
let data = world::get_comp<Field>(world, id()).data;
let (_, fee) = decode(data);
fee
}
Expand Down
12 changes: 6 additions & 6 deletions examples/sources/codegen/components/single_value.move
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ module examples::single_value_comp {
// Systems
friend examples::example_system;

const COMPONENT_NAME: vector<u8> = b"single_value";
const NAME: vector<u8> = b"single_value";

public fun id(): address {
entity_key::from_bytes(COMPONENT_NAME)
entity_key::from_bytes(NAME)
}

// value
Expand All @@ -25,21 +25,21 @@ module examples::single_value_comp {
}

public fun register(world: &mut World) {
world::add_component<Field>(
world::add_comp<Field>(
world,
COMPONENT_NAME,
NAME,
Field { data: encode(1000) }
);
}

public(friend) fun update(world: &mut World, value: u64) {
let data = encode(value);
world::get_mut_component<Field>(world, id()).data = data;
world::get_mut_comp<Field>(world, id()).data = data;
world::emit_update_event(id(), none(), data)
}

public fun get(world: &World): u64 {
let data = world::get_component<Field>(world, id()).data;
let data = world::get_comp<Field>(world, id()).data;
decode(data)
}

Expand Down
34 changes: 17 additions & 17 deletions examples/sources/codegen/eps/world.move
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ module examples::world {
/// Description of the world
description: String,
/// Components of the world
components: Bag,
comps: Bag,
/// Component names of the world
component_names: vector<String>,
compnames: vector<String>,
/// admin of the world
admin: ID,
/// Components of the world
Expand Down Expand Up @@ -68,8 +68,8 @@ module examples::world {
id: object::new(ctx),
name,
description,
components: bag::new(ctx),
component_names: vector::empty(),
comps: bag::new(ctx),
compnames: vector::empty(),
admin: object::id(&admin),
version: VERSION
};
Expand All @@ -81,33 +81,33 @@ module examples::world {
(world.name, world.description, world.version)
}

public fun component_names(world: &World): vector<String> {
world.component_names
public fun compnames(world: &World): vector<String> {
world.compnames
}

public fun get_component<T : store>(world: &World, id: address): &T {
public fun get_comp<T : store>(world: &World, id: address): &T {
assert!(world.version == VERSION, EWrongVersion);
assert!(bag::contains(&world.components, id), ECompDoesNotExist);
bag::borrow<address, T>(&world.components, id)
assert!(bag::contains(&world.comps, id), ECompDoesNotExist);
bag::borrow<address, T>(&world.comps, id)
}

public fun get_mut_component<T : store>(world: &mut World, id: address): &mut T {
public fun get_mut_comp<T : store>(world: &mut World, id: address): &mut T {
assert!(world.version == VERSION, EWrongVersion);
assert!(bag::contains(&world.components, id), ECompDoesNotExist);
bag::borrow_mut<address, T>(&mut world.components, id)
assert!(bag::contains(&world.comps, id), ECompDoesNotExist);
bag::borrow_mut<address, T>(&mut world.comps, id)
}

public fun add_component<T : store>(world: &mut World, component_name: vector<u8>, component: T){
public fun add_comp<T : store>(world: &mut World, component_name: vector<u8>, component: T){
assert!(world.version == VERSION, EWrongVersion);
let id = entity_key::from_bytes(component_name);
assert!(!bag::contains(&world.components, id), ECompAlreadyExists);
vector::push_back(&mut world.component_names, string(component_name));
bag::add<address,T>(&mut world.components, id, component);
assert!(!bag::contains(&world.comps, id), ECompAlreadyExists);
vector::push_back(&mut world.compnames, string(component_name));
bag::add<address,T>(&mut world.comps, id, component);
}

public fun contains(world: &mut World, id: address): bool {
assert!(world.version == VERSION, EWrongVersion);
bag::contains(&mut world.components, id)
bag::contains(&mut world.comps, id)
}

public fun emit_remove_event(comp: address, key: address) {
Expand Down
2 changes: 1 addition & 1 deletion examples/sources/system/example_system.move
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ module examples::example_system {
assert!(description == string(b"Examples description"), 0);
assert!(version == 1, 0);

let names = world::component_names(&world);
let names = world::compnames(&world);
assert!(names == vector[
string(b"single_column"),
string(b"multi_column"),
Expand Down

0 comments on commit b908b30

Please sign in to comment.