-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Homework 1 #4
base: master
Are you sure you want to change the base?
Homework 1 #4
Conversation
let a: u64 = | ||
self.fed - (HUNGER_PER_BLOCK as u64) * ((exec::block_height() as u64) - self.fed_block); | ||
a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to create a var:
let a: u64 = | |
self.fed - (HUNGER_PER_BLOCK as u64) * ((exec::block_height() as u64) - self.fed_block); | |
a | |
self.fed - HUNGER_PER_BLOCK * ((exec::block_height() as u64) - self.fed_block) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If self.fed
is less than HUNGER_PER_BLOCK * (exec::block_height() - self.fed_block)
, then subtraction results in the underflow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no need to call exec::block_height
multiple times as it is quite expensive. Better is to get block height one time and copy it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tmg.slept = tmg.current_slept(); | ||
} else { | ||
let fedblock = exec::block_height() as u64; | ||
tmg.fed = 10000; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is better to use named constants for numbers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tmg.entertained = tmg.current_entertained(); | ||
tmg.slept = tmg.current_slept(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same code is in both if
branches.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
} | ||
fn current_entertained(&mut self) -> u64 { | ||
let b: u64 = self.entertained | ||
- (BOREDOM_PER_BLOCK as u64) * ((exec::block_height() as u64) - self.entertained_block); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using self.entertained_block
is not correct here. We need to save the last processed block height here.
- (BOREDOM_PER_BLOCK as u64) * ((exec::block_height() as u64) - self.entertained_block); | |
- (BOREDOM_PER_BLOCK as u64) * ((exec::block_height() as u64) - last_processed_block); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have fixed it.
tmg.fed = tmg.current_fed(); | ||
tmg.entertained = tmg.current_entertained(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Anyway, you can move the fed
/ entertained
/ slept
decreasing outside this match
.
|
||
let _result = program.send(1, TmgAction::Sleep); | ||
//how to test the panic result? | ||
//negetive test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be great to check the Tamagotchi's vital signs after spending various counts of blocks.
No description provided.