/
contract.ts
57 lines (45 loc) · 1.8 KB
/
contract.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { NearBindgen, near, call, view, UnorderedMap } from 'near-sdk-js';
import { AccountId } from 'near-sdk-js/lib/types';
type Side = 'heads' | 'tails'
function simulateCoinFlip(): Side {
// randomSeed creates a random string, learn more about it in the README
const randomString: string = near.randomSeed().toString();
// If the last charCode is even we choose heads, otherwise tails
return randomString.charCodeAt(0) % 2 ? 'heads' : 'tails';
}
@NearBindgen({})
class CoinFlip {
points: UnorderedMap<number> = new UnorderedMap<number>("points");
/*
Flip a coin. Pass in the side (heads or tails) and a random number will be chosen
indicating whether the flip was heads or tails. If you got it right, you get a point.
*/
@call({})
flip_coin({ player_guess }: { player_guess: Side }): Side {
// Check who called the method
const player: AccountId = near.predecessorAccountId();
near.log(`${player} chose ${player_guess}`);
// Simulate a Coin Flip
const outcome = simulateCoinFlip();
// Get the current player points
let player_points: number = this.points.get(player, { defaultValue: 0 })
// Check if their guess was right and modify the points accordingly
if (player_guess == outcome) {
near.log(`The result was ${outcome}, you get a point!`);
player_points += 1;
} else {
near.log(`The result was ${outcome}, you lost a point`);
player_points = player_points ? player_points - 1 : 0;
}
// Store the new points
this.points.set(player, player_points)
return outcome
}
// View how many points a specific player has
@view({})
points_of({ player }: { player: AccountId }): number {
const points = this.points.get(player, {defaultValue: 0})
near.log(`Points for ${player}: ${points}`)
return points
}
}