Skip to content

Commit

Permalink
Merge pull request #1641 from multiversx/renames-crypto-zombies
Browse files Browse the repository at this point in the history
Crypto Zombies renames & small fixes
  • Loading branch information
andrei-marinica authored May 23, 2024
2 parents dbee715 + fa76fc9 commit a4eab37
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 23 deletions.
14 changes: 8 additions & 6 deletions contracts/examples/crypto-zombies/src/kitty_obj.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use multiversx_sc::derive_imports::*;

#[derive(NestedEncode, NestedDecode, TopEncode, TopDecode, TypeAbi)]
#[type_abi]
#[derive(NestedEncode, NestedDecode, TopEncode, TopDecode)]
pub struct Kitty {
pub genes: KittyGenes,
pub birth_time: u64, // timestamp
Expand All @@ -12,14 +12,16 @@ pub struct Kitty {
pub generation: u16, // max(sire_gen, matron_gen) + 1. Generation also influences cooldown.
}

#[derive(NestedEncode, NestedDecode, TopEncode, TopDecode, TypeAbi)]
#[type_abi]
#[derive(NestedEncode, NestedDecode, TopEncode, TopDecode)]
pub struct KittyGenes {
pub fur_color: Color,
pub eye_color: Color,
pub meow_power: u8, // the higher the value, the louder the cat
}

#[derive(NestedEncode, NestedDecode, TopEncode, TopDecode, TypeAbi)]
#[type_abi]
#[derive(NestedEncode, NestedDecode, TopEncode, TopDecode)]
pub struct Color {
pub r: u8,
pub g: u8,
Expand All @@ -28,13 +30,13 @@ pub struct Color {

impl KittyGenes {
pub fn get_as_u64(&self) -> u64 {
(self.fur_color.as_u64() << 12 | self.eye_color.as_u64()) << 4
(self.fur_color.as_u64() << 24 | self.eye_color.as_u64()) << 8
| self.meow_power.to_be() as u64
}
}

impl Color {
pub fn as_u64(&self) -> u64 {
((self.r.to_be() as u64) << 4 | self.r.to_be() as u64) << 4 | self.r.to_be() as u64
((self.r.to_be() as u64) << 8 | self.r.to_be() as u64) << 8 | self.r.to_be() as u64
}
}
4 changes: 2 additions & 2 deletions contracts/examples/crypto-zombies/src/proxy_crypto_zombies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,12 @@ where
.original_result()
}

pub fn zombies_count(
pub fn zombie_last_index(
self,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, usize> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("zombies_count")
.raw_call("zombie_last_index")
.original_result()
}

Expand Down
20 changes: 10 additions & 10 deletions contracts/examples/crypto-zombies/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,39 @@ use crate::zombie::Zombie;
#[multiversx_sc::module]
pub trait Storage {
#[view]
#[storage_mapper("dna_digits")]
#[storage_mapper("dnaDigits")]
fn dna_digits(&self) -> SingleValueMapper<u8>;

#[view]
#[storage_mapper("zombies_count")]
fn zombies_count(&self) -> SingleValueMapper<usize>;
#[storage_mapper("zombieLastIndex")]
fn zombie_last_index(&self) -> SingleValueMapper<usize>;

#[view]
#[storage_mapper("zombies")]
fn zombies(&self, id: &usize) -> SingleValueMapper<Zombie<Self::Api>>;

#[view]
#[storage_mapper("zombie_owner")]
#[storage_mapper("zombieOwner")]
fn zombie_owner(&self, id: &usize) -> SingleValueMapper<ManagedAddress>;

#[view]
#[storage_mapper("crypto_kitties_sc_address")]
#[storage_mapper("cryptoKittiesScAddress")]
fn crypto_kitties_sc_address(&self) -> SingleValueMapper<ManagedAddress>;

#[view]
#[storage_mapper("cooldown_time")]
#[storage_mapper("cooldownTime")]
fn cooldown_time(&self) -> SingleValueMapper<u64>;

#[view]
#[storage_mapper("owned_zombies")]
#[storage_mapper("ownedZombies")]
fn owned_zombies(&self, owner: &ManagedAddress) -> UnorderedSetMapper<usize>;

#[storage_mapper("attack_victory_probability")]
#[storage_mapper("attackVictoryProbability")]
fn attack_victory_probability(&self) -> SingleValueMapper<u8>;

#[storage_mapper("level_up_fee")]
#[storage_mapper("levelUpFee")]
fn level_up_fee(&self) -> SingleValueMapper<BigUint>;

#[storage_mapper("collected_fees")]
#[storage_mapper("collectedFees")]
fn collected_fees(&self) -> SingleValueMapper<BigUint>;
}
4 changes: 2 additions & 2 deletions contracts/examples/crypto-zombies/src/zombie_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{storage, zombie::Zombie};
#[multiversx_sc::module]
pub trait ZombieFactory: storage::Storage {
fn create_zombie(&self, owner: ManagedAddress, name: ManagedBuffer, dna: u64) {
self.zombies_count().update(|id| {
self.zombie_last_index().update(|id| {
self.new_zombie_event(*id, &name, dna);
self.zombies(id).set(Zombie {
name,
Expand Down Expand Up @@ -40,7 +40,7 @@ pub trait ZombieFactory: storage::Storage {
self.create_zombie(caller, name, rand_dna);
}

#[event("new_zombie_event")]
#[event("newZombieEvent")]
fn new_zombie_event(
&self,
#[indexed] zombie_id: usize,
Expand Down
4 changes: 2 additions & 2 deletions contracts/examples/crypto-zombies/src/zombie_feeding.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use multiversx_sc::imports::*;

use crate::{kitty_ownership_proxy, storage, zombie_factory, zombie_helper};
use crate::{kitty_obj::Kitty, kitty_ownership_proxy, storage, zombie_factory, zombie_helper};

#[multiversx_sc::module]
pub trait ZombieFeeding:
Expand Down Expand Up @@ -38,7 +38,7 @@ pub trait ZombieFeeding:
#[callback]
fn get_kitty_callback(
&self,
#[call_result] result: ManagedAsyncCallResult<crate::kitty_obj::Kitty>,
#[call_result] result: ManagedAsyncCallResult<Kitty>,
zombie_id: usize,
) {
match result {
Expand Down
2 changes: 1 addition & 1 deletion contracts/examples/crypto-zombies/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ multiversx_sc_wasm_adapter::endpoints! {
is_ready => is_ready
feed_on_kitty => feed_on_kitty
dna_digits => dna_digits
zombies_count => zombies_count
zombie_last_index => zombie_last_index
zombies => zombies
zombie_owner => zombie_owner
crypto_kitties_sc_address => crypto_kitties_sc_address
Expand Down

0 comments on commit a4eab37

Please sign in to comment.