1
1
// Find all our documentation at https://docs.near.org
2
- use near_sdk:: borsh:: { BorshSerialize , BorshDeserialize } ;
3
- use near_sdk:: env:: { self , log_str} ;
4
- use near_sdk:: { near_bindgen, AccountId , BorshStorageKey } ;
5
2
use near_sdk:: collections:: UnorderedMap ;
3
+ use near_sdk:: env:: { self , log_str} ;
4
+ use near_sdk:: { near, AccountId , BorshStorageKey } ;
6
5
7
- #[ derive ( BorshSerialize , BorshStorageKey ) ]
8
- #[ borsh ( crate = "near_sdk::borsh" ) ]
6
+ #[ near ( serializers = [ borsh ] ) ]
7
+ #[ derive ( BorshStorageKey ) ]
9
8
enum StorageKey {
10
9
Points ,
11
10
}
12
11
13
12
pub ( crate ) fn simulate_coin_flip ( ) -> String {
14
- // Here we get a first byte of a random seed
15
- let random_seed = * env:: random_seed ( ) . get ( 0 ) . unwrap ( ) as i8 ;
13
+ // Here we get a first byte of a random seed
14
+ let random_seed = * env:: random_seed ( ) . get ( 0 ) . unwrap ( ) as i8 ;
16
15
17
- // If a first byte is EVEN we choose heads, otherwise tails
18
- if let 0 = random_seed % 2 {
19
- return "heads" . to_string ( )
20
- } else {
21
- return "tails" . to_string ( )
22
- } ;
16
+ // If a first byte is EVEN we choose heads, otherwise tails
17
+ if let 0 = random_seed % 2 {
18
+ return "heads" . to_string ( ) ;
19
+ } else {
20
+ return "tails" . to_string ( ) ;
21
+ } ;
23
22
}
24
23
25
24
// Define the contract structure
26
- #[ near_bindgen]
27
- #[ derive( BorshDeserialize , BorshSerialize ) ]
28
- #[ borsh( crate = "near_sdk::borsh" ) ]
25
+ #[ near( contract_state) ]
29
26
pub struct Contract {
30
27
points : UnorderedMap < AccountId , u8 > ,
31
28
}
@@ -34,42 +31,42 @@ pub struct Contract {
34
31
impl Default for Contract {
35
32
fn default ( ) -> Self {
36
33
Self {
37
- points : UnorderedMap :: new ( StorageKey :: Points )
34
+ points : UnorderedMap :: new ( StorageKey :: Points ) ,
38
35
}
39
36
}
40
37
}
41
38
42
39
// Implement the contract structure
43
- #[ near_bindgen ]
40
+ #[ near ]
44
41
impl Contract {
45
42
/*
46
43
Flip a coin. Pass in the side (heads or tails) and a random number will be chosen
47
44
indicating whether the flip was heads or tails. If you got it right, you get a point.
48
45
*/
49
46
pub fn flip_coin ( & mut self , player_guess : String ) -> String {
50
- // Check who called the method
51
- let player: AccountId = env:: predecessor_account_id ( ) ;
52
- log_str ( & format ! ( "{player} chose {player_guess}" ) ) ;
47
+ // Check who called the method
48
+ let player: AccountId = env:: predecessor_account_id ( ) ;
49
+ log_str ( & format ! ( "{player} chose {player_guess}" ) ) ;
50
+
51
+ // Simulate a Coin Flip
52
+ let outcome = simulate_coin_flip ( ) ;
53
53
54
- // Simulate a Coin Flip
55
- let outcome = simulate_coin_flip ( ) ;
56
-
57
- // Get the current player points
58
- let mut player_points = self . points . get ( & player) . unwrap_or ( 0 ) ;
54
+ // Get the current player points
55
+ let mut player_points = self . points . get ( & player) . unwrap_or ( 0 ) ;
59
56
60
- // Check if their guess was right and modify the points accordingly
61
- if outcome. eq ( & player_guess) {
62
- player_points = player_points + 1 ;
63
- } else {
64
- player_points = player_points. saturating_sub ( 1 ) ;
65
- } ;
57
+ // Check if their guess was right and modify the points accordingly
58
+ if outcome. eq ( & player_guess) {
59
+ player_points = player_points + 1 ;
60
+ } else {
61
+ player_points = player_points. saturating_sub ( 1 ) ;
62
+ } ;
66
63
67
- log_str ( & format ! ( "player_points: {player_points}" ) ) ;
64
+ log_str ( & format ! ( "player_points: {player_points}" ) ) ;
68
65
69
- // Store the new points
70
- self . points . insert ( & player, & player_points) ;
66
+ // Store the new points
67
+ self . points . insert ( & player, & player_points) ;
71
68
72
- return outcome;
69
+ return outcome;
73
70
}
74
71
75
72
// View how many points a specific player has
0 commit comments