From a06e5bf8e16b2026c52307fe6d7ec43ce8bd272b Mon Sep 17 00:00:00 2001 From: Jackliu-miaozi <71253778+Jackliu-miaozi@users.noreply.github.com> Date: Wed, 13 Dec 2023 15:48:32 +0800 Subject: [PATCH 1/6] Initial commit --- contracts/01-tamagotchi/io/src/lib.rs | 15 +++++++---- contracts/01-tamagotchi/src/lib.rs | 30 ++++++++++++++++++++-- contracts/01-tamagotchi/tests/smoke.rs | 35 +++++++++++++++++++++++--- 3 files changed, 70 insertions(+), 10 deletions(-) diff --git a/contracts/01-tamagotchi/io/src/lib.rs b/contracts/01-tamagotchi/io/src/lib.rs index 394f418f..512f6f43 100644 --- a/contracts/01-tamagotchi/io/src/lib.rs +++ b/contracts/01-tamagotchi/io/src/lib.rs @@ -1,15 +1,16 @@ #![no_std] use codec::{Decode, Encode}; -use gmeta::Metadata; +use gmeta::{Metadata,In,InOut,Out}; use gstd::prelude::*; -use scale_info::TypeInfo; #[derive(Default, Encode, Decode, TypeInfo)] #[codec(crate = gstd::codec)] #[scale_info(crate = gstd::scale_info)] pub struct Tamagotchi { // TODO: 1️⃣ Add `name` and `age` fields + pub name: String, + pub date_of_birth: u64, } #[derive(Encode, Decode, TypeInfo)] @@ -17,6 +18,8 @@ pub struct Tamagotchi { #[scale_info(crate = gstd::scale_info)] pub enum TmgAction { // TODO: 2️⃣ Add `Name` and `Age` actions that set the name and age + Name, + Age, } #[derive(Encode, Decode, TypeInfo)] @@ -24,15 +27,17 @@ pub enum TmgAction { #[scale_info(crate = gstd::scale_info)] pub enum TmgEvent { // TODO: 3️⃣ Add `Name` and `Age` events that return the name and age + Name(String), + Age(u64), } pub struct ProgramMetadata; // TODO: 4️⃣ Fill `Init`, `Handle`, and `State` types impl Metadata for ProgramMetadata { - type Init = (); - type Handle = (); - type State = (); + type Init = In; + type Handle = InOut; + type State = Out; type Reply = (); type Others = (); type Signal = (); diff --git a/contracts/01-tamagotchi/src/lib.rs b/contracts/01-tamagotchi/src/lib.rs index 85f152bd..cde6c75b 100644 --- a/contracts/01-tamagotchi/src/lib.rs +++ b/contracts/01-tamagotchi/src/lib.rs @@ -1,19 +1,45 @@ #![no_std] - #[allow(unused_imports)] -use gstd::prelude::*; +use gstd::{msg,exec,prelude::*}; +use tamagotchi_io::*; + +static mut TAMAGOTCHI :Option = None; #[no_mangle] extern fn init() { // TODO: 5️⃣ Initialize the Tamagotchi program + let initname = msg::load().expect("unable to load name"); + let birthdate = exec::block_timestamp(); + let tmg = Tamagotchi { + name:initname, + date_of_birth: birthdate, + }; + unsafe { + TAMAGOTCHI = Some(tmg); + }; } #[no_mangle] extern fn handle() { // TODO: 6️⃣ Add handling of `Name` and `Age` actions + let action:TmgAction = msg::load().expect("unable to load action"); + let tmg = unsafe { + TAMAGOTCHI.get_or_insert(Default::default()) + }; + match action { + TmgAction::Name => { + msg::reply(TmgEvent::Name(tmg.name.clone()),0).expect("Error in a reply'tamagotchi::name'"); + }, + TmgAction::Age => { + let age = exec::block_timestamp() - tmg.date_of_birth; + msg::reply(TmgEvent::Age(age),0).expect("Error in a reply'tamagotchi::age'"); + }, + } } #[no_mangle] extern fn state() { // TODO: 7️⃣ Return the Tamagotchi state + let tmg = unsafe { TAMAGOTCHI.take().expect("Unexpected error in taking state") }; + msg::reply(tmg, 0).expect("Failed to share state"); } diff --git a/contracts/01-tamagotchi/tests/smoke.rs b/contracts/01-tamagotchi/tests/smoke.rs index 5dd29f16..c83b317f 100644 --- a/contracts/01-tamagotchi/tests/smoke.rs +++ b/contracts/01-tamagotchi/tests/smoke.rs @@ -1,10 +1,39 @@ -use gtest::{Program, System}; + + +use gtest::{Program, System,Log}; +use tamagotchi_io::*; #[test] fn smoke_test() { let sys = System::new(); sys.init_logger(); - let _program = Program::current(&sys); + let program = Program::current(&sys); + let result = program.send(2, String::from("Goodmoring")); + assert!(!result.main_failed()); + let result = program.send(2, TmgAction::Name); + let log = Log::builder().dest(2).payload(TmgEvent::Name(String::from("Goodmoring"))); + assert!(result.contains(&log)); + let _result = program.send(2, TmgAction::Age); + // let log = Log::builder().dest(2).payload(TmgEvent::Age(sys.block_timestamp())); + // assert!(result.contains(&log)); + + //How to test the age? +} + +#[test] +fn negative_smoke_test() { + let sys = System::new(); + sys.init_logger(); + let program = Program::current(&sys); + let payload = vec![1,2,3]; + let _result = program.send(2, payload); + // assert!(result.main_failed()); +// Why the assert is panic? - // TODO: 8️⃣ Test the program initialization and message handling + // let result = program.send(1, TmgAction::Name); + // let log = Log::builder().dest(2).payload(TmgEvent::Name("Goodmoring".to_string())); + // assert!(!result.contains(&log)); + // let result = program.send(1, TmgAction::Age); +// let log = Log::builder().dest(2).payload(TmgEvent::Age(sys.block_timestamp())); +// assert!(!result.contains(&log)); } From 9d728243beb8abcb215788cb34b645f59cfa589c Mon Sep 17 00:00:00 2001 From: Jackliu-miaozi <71253778+Jackliu-miaozi@users.noreply.github.com> Date: Wed, 13 Dec 2023 15:54:57 +0800 Subject: [PATCH 2/6] Initial commit --- contracts/01-tamagotchi/src/lib.rs | 21 ++++++++++----------- contracts/01-tamagotchi/tests/smoke.rs | 16 ++++++++-------- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/contracts/01-tamagotchi/src/lib.rs b/contracts/01-tamagotchi/src/lib.rs index cde6c75b..8df96e29 100644 --- a/contracts/01-tamagotchi/src/lib.rs +++ b/contracts/01-tamagotchi/src/lib.rs @@ -1,9 +1,9 @@ #![no_std] #[allow(unused_imports)] -use gstd::{msg,exec,prelude::*}; +use gstd::{exec, msg, prelude::*}; use tamagotchi_io::*; -static mut TAMAGOTCHI :Option = None; +static mut TAMAGOTCHI: Option = None; #[no_mangle] extern fn init() { @@ -11,7 +11,7 @@ extern fn init() { let initname = msg::load().expect("unable to load name"); let birthdate = exec::block_timestamp(); let tmg = Tamagotchi { - name:initname, + name: initname, date_of_birth: birthdate, }; unsafe { @@ -22,18 +22,17 @@ extern fn init() { #[no_mangle] extern fn handle() { // TODO: 6️⃣ Add handling of `Name` and `Age` actions - let action:TmgAction = msg::load().expect("unable to load action"); - let tmg = unsafe { - TAMAGOTCHI.get_or_insert(Default::default()) - }; + let action: TmgAction = msg::load().expect("unable to load action"); + let tmg = unsafe { TAMAGOTCHI.get_or_insert(Default::default()) }; match action { TmgAction::Name => { - msg::reply(TmgEvent::Name(tmg.name.clone()),0).expect("Error in a reply'tamagotchi::name'"); - }, + msg::reply(TmgEvent::Name(tmg.name.clone()), 0) + .expect("Error in a reply'tamagotchi::name'"); + } TmgAction::Age => { let age = exec::block_timestamp() - tmg.date_of_birth; - msg::reply(TmgEvent::Age(age),0).expect("Error in a reply'tamagotchi::age'"); - }, + msg::reply(TmgEvent::Age(age), 0).expect("Error in a reply'tamagotchi::age'"); + } } } diff --git a/contracts/01-tamagotchi/tests/smoke.rs b/contracts/01-tamagotchi/tests/smoke.rs index c83b317f..be5b2faa 100644 --- a/contracts/01-tamagotchi/tests/smoke.rs +++ b/contracts/01-tamagotchi/tests/smoke.rs @@ -1,6 +1,4 @@ - - -use gtest::{Program, System,Log}; +use gtest::{Log, Program, System}; use tamagotchi_io::*; #[test] @@ -11,7 +9,9 @@ fn smoke_test() { let result = program.send(2, String::from("Goodmoring")); assert!(!result.main_failed()); let result = program.send(2, TmgAction::Name); - let log = Log::builder().dest(2).payload(TmgEvent::Name(String::from("Goodmoring"))); + let log = Log::builder() + .dest(2) + .payload(TmgEvent::Name(String::from("Goodmoring"))); assert!(result.contains(&log)); let _result = program.send(2, TmgAction::Age); // let log = Log::builder().dest(2).payload(TmgEvent::Age(sys.block_timestamp())); @@ -25,15 +25,15 @@ fn negative_smoke_test() { let sys = System::new(); sys.init_logger(); let program = Program::current(&sys); - let payload = vec![1,2,3]; + let payload = vec![1, 2, 3]; let _result = program.send(2, payload); // assert!(result.main_failed()); -// Why the assert is panic? + // Why the assert is panic? // let result = program.send(1, TmgAction::Name); // let log = Log::builder().dest(2).payload(TmgEvent::Name("Goodmoring".to_string())); // assert!(!result.contains(&log)); // let result = program.send(1, TmgAction::Age); -// let log = Log::builder().dest(2).payload(TmgEvent::Age(sys.block_timestamp())); -// assert!(!result.contains(&log)); + // let log = Log::builder().dest(2).payload(TmgEvent::Age(sys.block_timestamp())); + // assert!(!result.contains(&log)); } From 37805220e26a20cd035b0b93f8d436b1f424b2da Mon Sep 17 00:00:00 2001 From: Jackliu-miaozi <71253778+Jackliu-miaozi@users.noreply.github.com> Date: Wed, 13 Dec 2023 17:41:44 +0800 Subject: [PATCH 3/6] second commit --- .../02-tamagotchi-interaction/io/src/lib.rs | 25 +++- .../02-tamagotchi-interaction/src/lib.rs | 119 ++++++++++++++++-- .../tests/interaction.rs | 21 +++- 3 files changed, 149 insertions(+), 16 deletions(-) diff --git a/contracts/02-tamagotchi-interaction/io/src/lib.rs b/contracts/02-tamagotchi-interaction/io/src/lib.rs index 621d1338..dd1e025e 100644 --- a/contracts/02-tamagotchi-interaction/io/src/lib.rs +++ b/contracts/02-tamagotchi-interaction/io/src/lib.rs @@ -1,8 +1,8 @@ #![no_std] use codec::{Decode, Encode}; -use gmeta::{Metadata,In,InOut,Out}; -use gstd::prelude::*; +use gmeta::{In, InOut, Metadata, Out}; +use gstd::{prelude::*, ActorId}; #[derive(Default, Encode, Decode, TypeInfo)] #[codec(crate = gstd::codec)] @@ -12,6 +12,13 @@ pub struct Tamagotchi { pub name: String, pub date_of_birth: u64, // TODO: 1️⃣ Add new fields + pub owner: ActorId, + pub fed: u64, + pub fed_block: u64, + pub entertained: u64, + pub entertained_block: u64, + pub slept: u64, + pub slept_block: u64, } #[derive(Encode, Decode, TypeInfo)] @@ -22,6 +29,9 @@ pub enum TmgAction { Name, Age, // TODO: 2️⃣ Add new actions + Feed, + Entertain, + Sleep, } #[derive(Encode, Decode, TypeInfo)] @@ -32,6 +42,9 @@ pub enum TmgEvent { Name(String), Age(u64), // TODO: 3️⃣ Add new events + Fed, + Entertained, + Slept, } pub struct ProgramMetadata; @@ -45,3 +58,11 @@ impl Metadata for ProgramMetadata { type Others = (); type Signal = (); } + +pub const HUNGER_PER_BLOCK: u64 = 1; +pub const BOREDOM_PER_BLOCK: u64 = 2; +pub const ENERGY_PER_BLOCK: u64 = 2; + +pub const FILL_PER_FEED: u64 = 1000; +pub const FILL_PER_ENTERTAINMENT: u64 = 1000; +pub const FILL_PER_SLEEP: u64 = 1000; \ No newline at end of file diff --git a/contracts/02-tamagotchi-interaction/src/lib.rs b/contracts/02-tamagotchi-interaction/src/lib.rs index 35987cd0..0419a62d 100644 --- a/contracts/02-tamagotchi-interaction/src/lib.rs +++ b/contracts/02-tamagotchi-interaction/src/lib.rs @@ -1,7 +1,38 @@ #![no_std] #[allow(unused_imports)] -use gstd::prelude::*; +use gstd::{exec, prelude::*}; +use tamagotchi_interaction_io::*; + +pub struct Tamagotchi { + pub name: String, + pub date_of_birth: u64, + pub owner: ActorId, + pub fed: u64, + pub fed_block: u64, + pub entertained: u64, + pub entertained_block: u64, + pub slept: u64, + pub slept_block: u64, +} + +impl Tamagotchi { + fn current_fed(&self) -> u64 {} + fn current_entertained(&self) -> u64 {} + fn current_slept(&self) -> u64 {} +} + +fn current_fed(&self) -> u64 { + self.fed - HUNGER_PER_BLOCK * (exec::block_height() as u64 - self.fed_block) +} +fn current_entertained(&self) -> u64 { + self.entertained - BOREDOM_PER_BLOCK * (exec::block_height() as u64 - self.entertained_block) +} +fn current_slept(&self) -> u64 { + self.slept - ENERGY_PER_BLOCK * (exec::block_height() as u64 - self.slept_block) +} + +static mut TAMAGOTCHI: Option = None; // TODO: 4️⃣ Define constants @@ -9,10 +40,20 @@ use gstd::prelude::*; extern fn init() { // TODO: 0️⃣ Copy the `init` function from the previous lesson and push changes to the master branch let initname = msg::load().expect("unable to load name"); - let birthdate = exec::block_timestamp(); + let birthdate = exec::block_height() as u64; + let fedblock = exec::block_height() as u64; + let entertainedblock = exec::block_height() as u64; + let sleptblock = exec::block_height() as u64; let tmg = Tamagotchi { name: initname, date_of_birth: birthdate, + owner: msg::source(), + fed: 1000, + fed_block: fedblock, + entertained: 5000, + entertained_block: entertainedblock, + slept: 2000, + slept_block: sleptblock, }; unsafe { TAMAGOTCHI = Some(tmg); @@ -24,17 +65,73 @@ extern fn handle() { // TODO: 0️⃣ Copy the `handle` function from the previous lesson and push changes to the master branch let action: TmgAction = msg::load().expect("unable to load action"); let tmg = unsafe { TAMAGOTCHI.get_or_insert(Default::default()) }; - match action { - TmgAction::Name => { - msg::reply(TmgEvent::Name(tmg.name.clone()), 0) - .expect("Error in a reply'tamagotchi::name'"); - } - TmgAction::Age => { - let age = exec::block_timestamp() - tmg.date_of_birth; - msg::reply(TmgEvent::Age(age), 0).expect("Error in a reply'tamagotchi::age'"); + if msg::source() = tmg.owner { + match action { + TmgAction::Name => { + msg::reply(TmgEvent::Name(tmg.name.clone()), 0) + .expect("Error in a reply'tamagotchi::name'"); + } + TmgAction::Age => { + let age = exec::block_timestamp() - tmg.date_of_birth; + msg::reply(TmgEvent::Age(age), 0).expect("Error in a reply'tamagotchi::age'"); + } + TmgAction::Feed => { + if tmg.current_fed() <= 9000 { + let fed = tmg.fed + FILL_PER_FEED; + msg::reply(TmgEvent::Fed, 0).expect("Error in a reply'tamagotchi::fed'"); + tmg.fed = fed; + tmg.fed_block = exec::block_height() as u64; + tmg.entertained = tmg.current_entertained(); + tmg.slept = tmg.current_slept(); + } else { + let fedblock = exec::block_height() as u64; + tmg.fed = 10000; + tmg.fed_block = fedblock; + tmg.entertained = tmg.current_entertained(); + tmg.slept = tmg.current_slept(); + msg::reply(TmgEvent::Fed, 1).expect("Error in a reply'tamagotchi::fed'"); + } + } + TmgAction::Entertain => { + if tmg.current_entertained() <= 9000 { + let entertained = tmg.entertained + FILL_PER_ENTERTAINMENT; + msg::reply(TmgEvent::Entertained, 0) + .expect("Error in a reply'tamagotchi::entertained'"); + tmg.entertained = entertained; + tmg.entertained_block = exec::block_height() as u64; + tmg.fed = tmg.current_fed(); + tmg.slept = tmg.current_slept(); + } else { + let entertainedblock = exec::block_height() as u64; + tmg.entertained = 10000; + tmg.entertained_block = entertainedblock; + tmg.fed = tmg.current_fed(); + tmg.slept = tmg.current_slept(); + msg::reply(TmgEvent::Entertained, 1) + .expect("Error in a reply'tamagotchi::entertained'"); + } + } + TmgAction::Sleep => { + if tmg.current_slept() <= 9000 { + let slept = tmg.slept + FILL_PER_SLEEP; + msg::reply(TmgEvent::Slept, 0).expect("Error in a reply'tamagotchi::slept'"); + tmg.slept = slept; + tmg.slept_block = exec::block_height() as u64; + tmg.fed = tmg.current_fed(); + tmg.entertained = tmg.current_entertained(); + } else { + let sleptblock = exec::block_height() as u64; + tmg.slept = 10000; + tmg.slept_block = sleptblock; + tmg.fed = tmg.current_fed(); + tmg.entertained = tmg.current_entertained(); + msg::reply(TmgEvent::Slept, 1).expect("Error in a reply'tamagotchi::slept'"); + } + } } + } else { + panic!("You are not the owner of this tamagotchi"); } - // TODO: 5️⃣ Add new logic for calculating the `fed`, `entertained` and `slept` levels } #[no_mangle] diff --git a/contracts/02-tamagotchi-interaction/tests/interaction.rs b/contracts/02-tamagotchi-interaction/tests/interaction.rs index a8cdc2a0..e90872cc 100644 --- a/contracts/02-tamagotchi-interaction/tests/interaction.rs +++ b/contracts/02-tamagotchi-interaction/tests/interaction.rs @@ -43,6 +43,21 @@ fn negative_smoke_test() { fn interaction_test() { let sys = System::new(); sys.init_logger(); - let _program = Program::current(&sys); - // TODO: 6️⃣ Test new functionality -} + let program = Program::current(&sys); + let result = program.send(2, String::from("Goodmoring")); + assert!(!result.main_failed()); + let result = program.send(2, TmgAction::Feed); + let log = Log::builder().dest(2).payload(TmgEvent::Fed); + assert!(result.contains(&log)); + let result = program.send(2, TmgAction::Entertain); + let log = Log::builder().dest(2).payload(TmgEvent::Entertained); + assert!(result.contains(&log)); + let result = program.send(2, TmgAction::Sleep); + let log = Log::builder().dest(2).payload(TmgEvent::Slept); + assert!(result.contains(&log)); + + + let result = program.send(1, TmgAction::Sleep); + assert!(result.main_failed()); + //negetive test +} \ No newline at end of file From 475c1cbabff77fcba39898635a58bf3d59f51423 Mon Sep 17 00:00:00 2001 From: Jackliu-miaozi <71253778+Jackliu-miaozi@users.noreply.github.com> Date: Wed, 13 Dec 2023 21:19:12 +0800 Subject: [PATCH 4/6] tamagotchi- interaction --- .../02-tamagotchi-interaction/src/lib.rs | 35 +++++++++++-------- .../tests/interaction.rs | 10 +++--- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/contracts/02-tamagotchi-interaction/src/lib.rs b/contracts/02-tamagotchi-interaction/src/lib.rs index 0419a62d..05720832 100644 --- a/contracts/02-tamagotchi-interaction/src/lib.rs +++ b/contracts/02-tamagotchi-interaction/src/lib.rs @@ -1,9 +1,12 @@ #![no_std] #[allow(unused_imports)] -use gstd::{exec, prelude::*}; +use gstd::{exec, msg, prelude::*, ActorId}; use tamagotchi_interaction_io::*; +#[derive(Default, Encode, Decode, TypeInfo)] +#[codec(crate = gstd::codec)] +#[scale_info(crate = gstd::scale_info)] pub struct Tamagotchi { pub name: String, pub date_of_birth: u64, @@ -17,19 +20,21 @@ pub struct Tamagotchi { } impl Tamagotchi { - fn current_fed(&self) -> u64 {} - fn current_entertained(&self) -> u64 {} - fn current_slept(&self) -> u64 {} -} - -fn current_fed(&self) -> u64 { - self.fed - HUNGER_PER_BLOCK * (exec::block_height() as u64 - self.fed_block) -} -fn current_entertained(&self) -> u64 { - self.entertained - BOREDOM_PER_BLOCK * (exec::block_height() as u64 - self.entertained_block) -} -fn current_slept(&self) -> u64 { - self.slept - ENERGY_PER_BLOCK * (exec::block_height() as u64 - self.slept_block) + fn current_fed(&mut self) -> u64 { + let a: u64 = + self.fed - (HUNGER_PER_BLOCK as u64) * ((exec::block_height() as u64) - self.fed_block); + a + } + fn current_entertained(&mut self) -> u64 { + let b: u64 = self.entertained + - (BOREDOM_PER_BLOCK as u64) * ((exec::block_height() as u64) - self.entertained_block); + b + } + fn current_slept(&mut self) -> u64 { + let c: u64 = self.slept + - (ENERGY_PER_BLOCK as u64) * ((exec::block_height() as u64) - self.slept_block); + c + } } static mut TAMAGOTCHI: Option = None; @@ -65,7 +70,7 @@ extern fn handle() { // TODO: 0️⃣ Copy the `handle` function from the previous lesson and push changes to the master branch let action: TmgAction = msg::load().expect("unable to load action"); let tmg = unsafe { TAMAGOTCHI.get_or_insert(Default::default()) }; - if msg::source() = tmg.owner { + if msg::source() == tmg.owner { match action { TmgAction::Name => { msg::reply(TmgEvent::Name(tmg.name.clone()), 0) diff --git a/contracts/02-tamagotchi-interaction/tests/interaction.rs b/contracts/02-tamagotchi-interaction/tests/interaction.rs index e90872cc..60b40945 100644 --- a/contracts/02-tamagotchi-interaction/tests/interaction.rs +++ b/contracts/02-tamagotchi-interaction/tests/interaction.rs @@ -1,4 +1,5 @@ -use gtest::{Program, System}; +use gtest::{Log, Program, System}; +use tamagotchi_interaction_io::*; // TODO: 0️⃣ Copy tests from the previous lesson and push changes to the master branch @@ -56,8 +57,7 @@ fn interaction_test() { let log = Log::builder().dest(2).payload(TmgEvent::Slept); assert!(result.contains(&log)); - - let result = program.send(1, TmgAction::Sleep); - assert!(result.main_failed()); + let _result = program.send(1, TmgAction::Sleep); + //how to test the panic result? //negetive test -} \ No newline at end of file +} From 6c1c5fbea5bb72aaf473417c226ed0e07998081b Mon Sep 17 00:00:00 2001 From: Jackliu-miaozi <71253778+Jackliu-miaozi@users.noreply.github.com> Date: Mon, 18 Dec 2023 10:30:02 +0800 Subject: [PATCH 5/6] second commit --- contracts/01-tamagotchi/Cargo.toml | 16 ---------- contracts/01-tamagotchi/build.rs | 5 --- contracts/01-tamagotchi/io/Cargo.toml | 9 ------ contracts/01-tamagotchi/io/src/lib.rs | 44 -------------------------- contracts/01-tamagotchi/src/lib.rs | 44 -------------------------- contracts/01-tamagotchi/tests/smoke.rs | 39 ----------------------- contracts/Cargo.lock | 18 ----------- contracts/Cargo.toml | 2 -- 8 files changed, 177 deletions(-) delete mode 100644 contracts/01-tamagotchi/Cargo.toml delete mode 100644 contracts/01-tamagotchi/build.rs delete mode 100644 contracts/01-tamagotchi/io/Cargo.toml delete mode 100644 contracts/01-tamagotchi/io/src/lib.rs delete mode 100644 contracts/01-tamagotchi/src/lib.rs delete mode 100644 contracts/01-tamagotchi/tests/smoke.rs diff --git a/contracts/01-tamagotchi/Cargo.toml b/contracts/01-tamagotchi/Cargo.toml deleted file mode 100644 index ce867ab7..00000000 --- a/contracts/01-tamagotchi/Cargo.toml +++ /dev/null @@ -1,16 +0,0 @@ -[package] -name = "tamagotchi" -version.workspace = true -edition.workspace = true -publish.workspace = true - -[dependencies] -gstd.workspace = true -tamagotchi-io.workspace = true - -[build-dependencies] -gear-wasm-builder.workspace = true -tamagotchi-io.workspace = true - -[dev-dependencies] -gtest.workspace = true diff --git a/contracts/01-tamagotchi/build.rs b/contracts/01-tamagotchi/build.rs deleted file mode 100644 index 96f5ca8d..00000000 --- a/contracts/01-tamagotchi/build.rs +++ /dev/null @@ -1,5 +0,0 @@ -use tamagotchi_io::ProgramMetadata; - -fn main() { - gear_wasm_builder::build_with_metadata::(); -} diff --git a/contracts/01-tamagotchi/io/Cargo.toml b/contracts/01-tamagotchi/io/Cargo.toml deleted file mode 100644 index 2418cac1..00000000 --- a/contracts/01-tamagotchi/io/Cargo.toml +++ /dev/null @@ -1,9 +0,0 @@ -[package] -name = "tamagotchi-io" -version.workspace = true -edition.workspace = true -publish.workspace = true - -[dependencies] -gmeta.workspace = true -gstd.workspace = true diff --git a/contracts/01-tamagotchi/io/src/lib.rs b/contracts/01-tamagotchi/io/src/lib.rs deleted file mode 100644 index 512f6f43..00000000 --- a/contracts/01-tamagotchi/io/src/lib.rs +++ /dev/null @@ -1,44 +0,0 @@ -#![no_std] - -use codec::{Decode, Encode}; -use gmeta::{Metadata,In,InOut,Out}; -use gstd::prelude::*; - -#[derive(Default, Encode, Decode, TypeInfo)] -#[codec(crate = gstd::codec)] -#[scale_info(crate = gstd::scale_info)] -pub struct Tamagotchi { - // TODO: 1️⃣ Add `name` and `age` fields - pub name: String, - pub date_of_birth: u64, -} - -#[derive(Encode, Decode, TypeInfo)] -#[codec(crate = gstd::codec)] -#[scale_info(crate = gstd::scale_info)] -pub enum TmgAction { - // TODO: 2️⃣ Add `Name` and `Age` actions that set the name and age - Name, - Age, -} - -#[derive(Encode, Decode, TypeInfo)] -#[codec(crate = gstd::codec)] -#[scale_info(crate = gstd::scale_info)] -pub enum TmgEvent { - // TODO: 3️⃣ Add `Name` and `Age` events that return the name and age - Name(String), - Age(u64), -} - -pub struct ProgramMetadata; - -// TODO: 4️⃣ Fill `Init`, `Handle`, and `State` types -impl Metadata for ProgramMetadata { - type Init = In; - type Handle = InOut; - type State = Out; - type Reply = (); - type Others = (); - type Signal = (); -} diff --git a/contracts/01-tamagotchi/src/lib.rs b/contracts/01-tamagotchi/src/lib.rs deleted file mode 100644 index 8df96e29..00000000 --- a/contracts/01-tamagotchi/src/lib.rs +++ /dev/null @@ -1,44 +0,0 @@ -#![no_std] -#[allow(unused_imports)] -use gstd::{exec, msg, prelude::*}; -use tamagotchi_io::*; - -static mut TAMAGOTCHI: Option = None; - -#[no_mangle] -extern fn init() { - // TODO: 5️⃣ Initialize the Tamagotchi program - let initname = msg::load().expect("unable to load name"); - let birthdate = exec::block_timestamp(); - let tmg = Tamagotchi { - name: initname, - date_of_birth: birthdate, - }; - unsafe { - TAMAGOTCHI = Some(tmg); - }; -} - -#[no_mangle] -extern fn handle() { - // TODO: 6️⃣ Add handling of `Name` and `Age` actions - let action: TmgAction = msg::load().expect("unable to load action"); - let tmg = unsafe { TAMAGOTCHI.get_or_insert(Default::default()) }; - match action { - TmgAction::Name => { - msg::reply(TmgEvent::Name(tmg.name.clone()), 0) - .expect("Error in a reply'tamagotchi::name'"); - } - TmgAction::Age => { - let age = exec::block_timestamp() - tmg.date_of_birth; - msg::reply(TmgEvent::Age(age), 0).expect("Error in a reply'tamagotchi::age'"); - } - } -} - -#[no_mangle] -extern fn state() { - // TODO: 7️⃣ Return the Tamagotchi state - let tmg = unsafe { TAMAGOTCHI.take().expect("Unexpected error in taking state") }; - msg::reply(tmg, 0).expect("Failed to share state"); -} diff --git a/contracts/01-tamagotchi/tests/smoke.rs b/contracts/01-tamagotchi/tests/smoke.rs deleted file mode 100644 index be5b2faa..00000000 --- a/contracts/01-tamagotchi/tests/smoke.rs +++ /dev/null @@ -1,39 +0,0 @@ -use gtest::{Log, Program, System}; -use tamagotchi_io::*; - -#[test] -fn smoke_test() { - let sys = System::new(); - sys.init_logger(); - let program = Program::current(&sys); - let result = program.send(2, String::from("Goodmoring")); - assert!(!result.main_failed()); - let result = program.send(2, TmgAction::Name); - let log = Log::builder() - .dest(2) - .payload(TmgEvent::Name(String::from("Goodmoring"))); - assert!(result.contains(&log)); - let _result = program.send(2, TmgAction::Age); - // let log = Log::builder().dest(2).payload(TmgEvent::Age(sys.block_timestamp())); - // assert!(result.contains(&log)); - - //How to test the age? -} - -#[test] -fn negative_smoke_test() { - let sys = System::new(); - sys.init_logger(); - let program = Program::current(&sys); - let payload = vec![1, 2, 3]; - let _result = program.send(2, payload); - // assert!(result.main_failed()); - // Why the assert is panic? - - // let result = program.send(1, TmgAction::Name); - // let log = Log::builder().dest(2).payload(TmgEvent::Name("Goodmoring".to_string())); - // assert!(!result.contains(&log)); - // let result = program.send(1, TmgAction::Age); - // let log = Log::builder().dest(2).payload(TmgEvent::Age(sys.block_timestamp())); - // assert!(!result.contains(&log)); -} diff --git a/contracts/Cargo.lock b/contracts/Cargo.lock index b112d757..5c4e29ab 100644 --- a/contracts/Cargo.lock +++ b/contracts/Cargo.lock @@ -5644,16 +5644,6 @@ dependencies = [ "unicode-ident", ] -[[package]] -name = "tamagotchi" -version = "0.3.3" -dependencies = [ - "gear-wasm-builder", - "gstd", - "gtest", - "tamagotchi-io", -] - [[package]] name = "tamagotchi-army" version = "0.3.3" @@ -5707,14 +5697,6 @@ dependencies = [ "gstd", ] -[[package]] -name = "tamagotchi-io" -version = "0.3.3" -dependencies = [ - "gmeta", - "gstd", -] - [[package]] name = "tamagotchi-nft" version = "0.3.3" diff --git a/contracts/Cargo.toml b/contracts/Cargo.toml index 70a1424d..c8575493 100644 --- a/contracts/Cargo.toml +++ b/contracts/Cargo.toml @@ -9,7 +9,6 @@ publish = false [workspace] resolver = "2" members = [ - "01-tamagotchi", "02-tamagotchi-interaction", "03-tamagotchi-nft", "04-tamagotchi-shop", @@ -29,7 +28,6 @@ gstd = { git = "https://github.com/gear-tech/gear", tag = "v1.0.2" } gtest = { git = "https://github.com/gear-tech/gear", tag = "v1.0.2" } sharded-fungible-token-io = { git = "https://github.com/gear-foundation/dapps", tag = "v1.0.2" } -tamagotchi-io.path = "01-tamagotchi/io" tamagotchi-interaction-io.path = "02-tamagotchi-interaction/io" tamagotchi-nft-io.path = "03-tamagotchi-nft/io" tamagotchi-shop-io.path = "04-tamagotchi-shop/io" From 7f1fa8c2ec3daf1ba496f56da95410d192ca439d Mon Sep 17 00:00:00 2001 From: Jackliu-miaozi <71253778+Jackliu-miaozi@users.noreply.github.com> Date: Mon, 18 Dec 2023 15:19:05 +0800 Subject: [PATCH 6/6] homework_02 --- .../02-tamagotchi-interaction/io/src/lib.rs | 6 +- .../02-tamagotchi-interaction/src/lib.rs | 81 ++++++++++--------- 2 files changed, 48 insertions(+), 39 deletions(-) diff --git a/contracts/02-tamagotchi-interaction/io/src/lib.rs b/contracts/02-tamagotchi-interaction/io/src/lib.rs index dd1e025e..a5b88eda 100644 --- a/contracts/02-tamagotchi-interaction/io/src/lib.rs +++ b/contracts/02-tamagotchi-interaction/io/src/lib.rs @@ -65,4 +65,8 @@ pub const ENERGY_PER_BLOCK: u64 = 2; pub const FILL_PER_FEED: u64 = 1000; pub const FILL_PER_ENTERTAINMENT: u64 = 1000; -pub const FILL_PER_SLEEP: u64 = 1000; \ No newline at end of file +pub const FILL_PER_SLEEP: u64 = 1000; + +pub const MAX_FED: u64 = 10000; +pub const MAX_ENTERTAINED: u64 = 10000; +pub const MAX_SLEPT: u64 = 10000; \ No newline at end of file diff --git a/contracts/02-tamagotchi-interaction/src/lib.rs b/contracts/02-tamagotchi-interaction/src/lib.rs index 05720832..ba4d00df 100644 --- a/contracts/02-tamagotchi-interaction/src/lib.rs +++ b/contracts/02-tamagotchi-interaction/src/lib.rs @@ -21,19 +21,33 @@ pub struct Tamagotchi { impl Tamagotchi { fn current_fed(&mut self) -> u64 { - let a: u64 = - self.fed - (HUNGER_PER_BLOCK as u64) * ((exec::block_height() as u64) - self.fed_block); - a + self.fed - (HUNGER_PER_BLOCK as u64) * ((exec::block_height() as u64) - self.fed_block) } fn current_entertained(&mut self) -> u64 { - let b: u64 = self.entertained - - (BOREDOM_PER_BLOCK as u64) * ((exec::block_height() as u64) - self.entertained_block); - b + self.entertained + - (BOREDOM_PER_BLOCK as u64) * ((exec::block_height() as u64) - self.entertained_block) } fn current_slept(&mut self) -> u64 { - let c: u64 = self.slept - - (ENERGY_PER_BLOCK as u64) * ((exec::block_height() as u64) - self.slept_block); - c + self.slept - (ENERGY_PER_BLOCK as u64) * ((exec::block_height() as u64) - self.slept_block) + } + fn is_alive(&mut self) -> bool { + if self.current_fed() > 0 && self.current_entertained() > 0 && self.current_slept() > 0 { + true + } else { + panic!("Your tamagotchi is dead"); + } + } + fn feedaction(&mut self) { + self.entertained = self.current_entertained(); + self.slept = self.current_slept(); + } + fn entertainaction(&mut self) { + self.fed = self.current_fed(); + self.slept = self.current_slept(); + } + fn selptaction(&mut self) { + self.fed = self.current_fed(); + self.entertained = self.current_entertained(); } } @@ -45,20 +59,17 @@ static mut TAMAGOTCHI: Option = None; extern fn init() { // TODO: 0️⃣ Copy the `init` function from the previous lesson and push changes to the master branch let initname = msg::load().expect("unable to load name"); - let birthdate = exec::block_height() as u64; - let fedblock = exec::block_height() as u64; - let entertainedblock = exec::block_height() as u64; - let sleptblock = exec::block_height() as u64; + let current_block_height = exec::block_height() as u64; let tmg = Tamagotchi { name: initname, - date_of_birth: birthdate, + date_of_birth: current_block_height, owner: msg::source(), fed: 1000, - fed_block: fedblock, + fed_block: current_block_height, entertained: 5000, - entertained_block: entertainedblock, + entertained_block: current_block_height, slept: 2000, - slept_block: sleptblock, + slept_block: current_block_height, }; unsafe { TAMAGOTCHI = Some(tmg); @@ -81,55 +92,49 @@ extern fn handle() { msg::reply(TmgEvent::Age(age), 0).expect("Error in a reply'tamagotchi::age'"); } TmgAction::Feed => { - if tmg.current_fed() <= 9000 { + if (tmg.current_fed() <= 9000) && tmg.is_alive() { let fed = tmg.fed + FILL_PER_FEED; msg::reply(TmgEvent::Fed, 0).expect("Error in a reply'tamagotchi::fed'"); tmg.fed = fed; tmg.fed_block = exec::block_height() as u64; - tmg.entertained = tmg.current_entertained(); - tmg.slept = tmg.current_slept(); - } else { + tmg.feedaction(); + } else if tmg.current_entertained() >= 9000 { let fedblock = exec::block_height() as u64; - tmg.fed = 10000; + tmg.fed = MAX_FED; tmg.fed_block = fedblock; - tmg.entertained = tmg.current_entertained(); - tmg.slept = tmg.current_slept(); + tmg.feedaction(); msg::reply(TmgEvent::Fed, 1).expect("Error in a reply'tamagotchi::fed'"); } } TmgAction::Entertain => { - if tmg.current_entertained() <= 9000 { + if (tmg.current_entertained() <= 9000) && tmg.is_alive() { let entertained = tmg.entertained + FILL_PER_ENTERTAINMENT; msg::reply(TmgEvent::Entertained, 0) .expect("Error in a reply'tamagotchi::entertained'"); tmg.entertained = entertained; tmg.entertained_block = exec::block_height() as u64; - tmg.fed = tmg.current_fed(); - tmg.slept = tmg.current_slept(); - } else { + tmg.entertainaction(); + } else if tmg.current_entertained() >= 9000 { let entertainedblock = exec::block_height() as u64; - tmg.entertained = 10000; + tmg.entertained = MAX_ENTERTAINED; tmg.entertained_block = entertainedblock; - tmg.fed = tmg.current_fed(); - tmg.slept = tmg.current_slept(); + tmg.entertainaction(); msg::reply(TmgEvent::Entertained, 1) .expect("Error in a reply'tamagotchi::entertained'"); } } TmgAction::Sleep => { - if tmg.current_slept() <= 9000 { + if (tmg.current_slept() <= 9000) && tmg.is_alive() { let slept = tmg.slept + FILL_PER_SLEEP; msg::reply(TmgEvent::Slept, 0).expect("Error in a reply'tamagotchi::slept'"); tmg.slept = slept; tmg.slept_block = exec::block_height() as u64; - tmg.fed = tmg.current_fed(); - tmg.entertained = tmg.current_entertained(); - } else { + tmg.selptaction() + } else if tmg.current_entertained() >= 9000 { let sleptblock = exec::block_height() as u64; - tmg.slept = 10000; + tmg.slept = MAX_SLEPT; tmg.slept_block = sleptblock; - tmg.fed = tmg.current_fed(); - tmg.entertained = tmg.current_entertained(); + tmg.selptaction(); msg::reply(TmgEvent::Slept, 1).expect("Error in a reply'tamagotchi::slept'"); } }