Created
October 12, 2021 12:39
-
-
Save EdoWahdana/8c04ed31d205b419b1c62935facdcfb7 to your computer and use it in GitHub Desktop.
Neargochi Smart Contract
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize}; | |
use near_sdk::{env, near_bindgen, setup_alloc}; | |
use rand::Rng; // 0.8.0 | |
setup_alloc!(); | |
#[near_bindgen] | |
#[derive(Default, BorshDeserialize, BorshSerialize)] | |
pub struct Status { | |
weight: i16, | |
hunger: i16, | |
happines: i16, | |
is_sick: bool, | |
} | |
#[near_bindgen] | |
impl Status { | |
#[init] | |
pub fn new() -> Self { | |
assert!(env::state_read::<Self>().is_none(), "Already initialized"); | |
Self { | |
weight: 1, | |
hunger: 1, | |
happines: 1, | |
is_sick: false, | |
} | |
} | |
pub fn health_check(&self) -> bool { | |
if !self.is_sick { return true; } | |
else { return false; } | |
} | |
pub fn get_weight(&self) -> i16 { | |
self.weight | |
} | |
pub fn get_hunger(&self) -> i16 { | |
self.hunger | |
} | |
pub fn get_happines(&self) -> i16 { | |
self.happines | |
} | |
pub fn medicine(&mut self) { | |
if self.is_sick { self.is_sick = false; } | |
} | |
pub fn meal(&mut self) { | |
if self.health_check() { | |
self.weight = i16::wrapping_add(self.weight, 1); | |
self.hunger = 4; | |
} | |
} | |
pub fn play(&mut self, value: i16) -> bool { | |
self.weight = i16::wrapping_sub(self.weight, 1); | |
self.hunger = i16::wrapping_sub(self.weight, 1); | |
let random_value = rand::thread_rng().gen_range(0..2); | |
if random_value == value { | |
self.happines = i16::wrapping_add(self.happines, 1); | |
true | |
} else { false } | |
} | |
pub fn snack(&mut self) { | |
if self.health_check() { | |
if self.hunger > 4 { self.is_sick = true; } | |
self.weight = i16::wrapping_add(self.weight, 2); | |
self.hunger = i16::wrapping_add(self.hunger, 1); | |
} | |
} | |
} | |
#[cfg(test)] | |
mod tests { | |
use super::*; | |
use near_sdk::MockedBlockchain; | |
use near_sdk::{testing_env, VMContext}; | |
// mock the context for testing, notice "signer_account_id" that was accessed above from env:: | |
fn get_context(input: Vec<u8>, is_view: bool) -> VMContext { | |
VMContext { | |
current_account_id: "alice_near".to_string(), | |
signer_account_id: "bob_near".to_string(), | |
signer_account_pk: vec![0, 1, 2], | |
predecessor_account_id: "carol_near".to_string(), | |
input, | |
block_index: 0, | |
block_timestamp: 0, | |
account_balance: 0, | |
account_locked_balance: 0, | |
storage_usage: 0, | |
attached_deposit: 0, | |
prepaid_gas: 10u64.pow(18), | |
random_seed: vec![0, 1, 2], | |
is_view, | |
output_data_receivers: vec![], | |
epoch_height: 19, | |
} | |
} | |
#[test] | |
fn make_sick() { | |
let context = get_context(vec![], true); | |
testing_env!(context); | |
let mut contract = Status::default(); | |
contract.meal(); | |
contract.snack(); | |
contract.snack(); | |
contract.snack(); | |
assert_eq!(false, contract.health_check()); | |
} | |
#[test] | |
fn medicine() { | |
let context = get_context(vec![], true); | |
testing_env!(context); | |
let mut contract = Status::default(); | |
contract.meal(); | |
contract.snack(); | |
contract.snack(); | |
contract.snack(); | |
contract.medicine(); | |
assert_eq!(true, contract.health_check()); | |
} | |
#[test] | |
fn play() { | |
let context = get_context(vec![], true); | |
testing_env!(context); | |
let mut contract = Status::default(); | |
assert_eq!(true, contract.play(1)); | |
assert_eq!(5, contract.get_happines()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment