-
Notifications
You must be signed in to change notification settings - Fork 3
/
algonim.py
executable file
·327 lines (288 loc) · 14.6 KB
/
algonim.py
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
"""AlgoNim, the first crypto-mini-game on Algorand! (by cusma)
Usage:
algonim.py setup <dealer_mnemonic> <opponent_address> <hours_duration>
[--bet-amount=<ba>] [--pieces=<ps>] [--max-removal=<mr>]
algonim.py join <opponent_mnemonic>
algonim.py play <player_mnemonic> <asa_pieces_amount>
algonim.py status <player_address>
algonim.py close <player_address>
algonim.py [--help]
Commands:
setup Dealer sets up a new AlgoNim match.
join Opponent joins the match.
play Play your turn.
status Display current match status.
close Close expired AlgoNim Bet Escrows.
Options:
-b <ba> --bet-amount=<ba> Set the bet amount in microAlgos
[default: 0].
-p <ps> --pieces=<ps> Set the total amount of pieces on game table
[default: 21].
-m <mr> --max-removal=<mr> Set maximum amount of pieces removal
[default: 4].
-h --help
"""
import sys
from docopt import docopt
from algonim_moves import *
def main():
if len(sys.argv) == 1:
# Display help if no arguments, see:
# https://github.com/docopt/docopt/issues/420#issuecomment-405018014
sys.argv.append('--help')
args = docopt(__doc__)
algod_client = create_algod_client()
try:
algod_client.compile(
compileTeal(Global.group_size() == Int(1), Mode.Signature))
except Exception:
raise ErrAlgodClientCompile
if args['setup']:
dealer_passphrase = args['<dealer_mnemonic>']
addr_opponent = args['<opponent_address>']
match_hours_timeout = float(args['<hours_duration>'])
microalgo_bet_amount = int(args['--bet-amount'])
asa_pieces_total = int(args['--pieces'])
asa_pieces_max_remove = int(args['--max-removal'])
assert microalgo_bet_amount >= 0
assert asa_pieces_total > asa_pieces_max_remove + 1
match_setup(algod_client, dealer_passphrase, addr_opponent,
match_hours_timeout, microalgo_bet_amount,
asa_pieces_total, asa_pieces_max_remove)
elif args['join']:
with open("algonim.match", "rb") as f:
match_data_bytes = f.read()
f.close()
match_data = msgpack.unpackb(match_data_bytes)
opponent_private_key = mnemonic.to_private_key(args['<opponent_mnemonic>'])
opponent = {'pk': address_from_private_key(opponent_private_key),
'sk': opponent_private_key}
microalgo_bet_amount = match_data['microalgo_bet_amount']
print(
r"""
_ __ ____ _____ _
/ \ [ | |_ \|_ _| (_)
/ _ \ | | .--./) .--. | \ | | __ _ .--..--.
/ ___ \ | | / /'`\;/ .'`\ \ | |\ \| | [ | [ `.-. .-. |
_/ / \ \_ | | \ \._//| \__. |_| |_\ |_ | | | | | | | |
|____| |____|[___].',__` '.__.'|_____|\____|[___][___||__||__]
( ( __))
by cusma
Welcome to AlgoNim, the first crypto-mini-game on Algorand!
"""
)
print("")
print("The Dealer wants to bet", microalgo_bet_amount * 10 ** -6,
"ALGO.")
join = input("Do you want to join the match? [y/N]")
if join == 'y':
# AlgoNim ASAs Opt-In
asa_pieces_opt_in_txn = unsigned_asset_send(
algod_client, opponent['pk'], opponent['pk'],
match_data['asa_pieces_id'], 0)
asa_pieces_opt_in_stxn = asa_pieces_opt_in_txn.sign(opponent['sk'])
txid = algod_client.send_transactions([asa_pieces_opt_in_stxn])
print("\nAlgoNim ASA Piece Opt-In...")
print("Transaction ID:", txid)
wait_for_tx_confirmation(algod_client, txid)
asa_turn_opt_in_txn = unsigned_asset_send(
algod_client, opponent['pk'], opponent['pk'],
match_data['asa_turn_id'], 0)
asa_turn_opt_in_stxn = asa_turn_opt_in_txn.sign(opponent['sk'])
txid = algod_client.send_transactions([asa_turn_opt_in_stxn])
print("\nAlgoNim ASA Turn Opt-In...")
print("Transaction ID:", txid)
wait_for_tx_confirmation(algod_client, txid)
# AlgoNim Players' Bet Atomic Transfer
dealer_bet_stxn = encoding.msgpack_decode(
match_data['dealer_bet_stxn'])
opponent_bet_txn = encoding.msgpack_decode(
match_data['opponent_bet_txn'])
opponent_bet_stxn = opponent_bet_txn.sign(opponent['sk'])
bet_sgtxn = [dealer_bet_stxn, opponent_bet_stxn]
txid = algod_client.send_transactions(bet_sgtxn)
print("\nPlayers betting...")
print("Transaction ID: ", txid)
wait_for_tx_confirmation(algod_client, txid)
dealer_info = algod_client.account_info(match_data['dealer'])
asa_pieces_total = next(
(asa['params']['total'] for asa in
dealer_info['created-assets'] if
asa['index'] == match_data['asa_pieces_id']), None)
print(
r"""
_ _
/\/\ __ _| |_ ___| |__ _
/ \ / _` | __/ __| '_ \ (_)
/ /\/\ \ (_| | || (__| | | | _
\/ \/\__,_|\__\___|_| |_| (_)
"""
)
print("MATCH DURATION:\t\t",
match_data['match_hours_timeout'] * 60, "min")
print("PIECES ON GAME TABLE:\t", asa_pieces_total, "\n")
print("RULES:")
print("1. Players on each turn must remove at least 1 ASA Piece")
print("2. Players on each turn must remove at most",
match_data['asa_pieces_max_remove'], "ASA Piece")
print("3. Who removes the last ASA Piece form the Game Table wins "
+ "the match!\n")
print("Player 1 - Dealer:\t" + match_data['dealer'])
print("Player 2 - Opponent:\t" + match_data['opponent'], "\n")
print("AlgoNim ASA Pieces ID:\t", match_data['asa_pieces_id'])
print("AlgoNim ASA Turn ID:\t", match_data['asa_turn_id'], "\n")
print("AlgoNim Sink Account:\t\t\t" + match_data['sink'])
print("AlgoNim Game Table Account:\t\t" + match_data['game_table'])
print("AlgoNim Bet Escrow Player 1 Account:\t"
+ match_data['dealer_bet_escrow'])
print("AlgoNim Bet Escrow Player 2 Account:\t"
+ match_data['opponent_bet_escrow'])
print("\nMay the best win!\n")
else:
print("See you for the next AlgoNim match! Arrivederci!")
elif args['play']:
with open("algonim.match", "rb") as f:
match_data_bytes = f.read()
f.close()
match_data = msgpack.unpackb(match_data_bytes)
player_private_key = mnemonic.to_private_key(args['<player_mnemonic>'])
player = {'pk': address_from_private_key(player_private_key),
'sk': player_private_key}
asa_pieces_amount = int(args['<asa_pieces_amount>'])
asa_pieces_id = match_data['asa_pieces_id']
asa_pieces_max_remove = match_data['asa_pieces_max_remove']
asa_turn_id = match_data['asa_turn_id']
addr_sink = match_data['sink']
sink_lsig = encoding.msgpack_decode(match_data['sink_lsig'])
addr_game_table = match_data['game_table']
game_table_lsig = encoding.msgpack_decode(
match_data['game_table_lsig'])
addr_dealer_bet_escrow = match_data['dealer_bet_escrow']
dealer_bet_escrow_lsig = encoding.msgpack_decode(
match_data['dealer_bet_escrow_lsig'])
dealer_info = algod_client.account_info(match_data['dealer'])
asa_pieces_total = next(
(asa['params']['total'] for asa in dealer_info['created-assets'] if
asa['index'] == match_data['asa_pieces_id']), None)
if player['pk'] == match_data['dealer']:
addr_adversary = match_data['opponent']
addr_adversary_bet_escrow = match_data['opponent_bet_escrow']
adversary_bet_escrow_lsig = encoding.msgpack_decode(
match_data['opponent_bet_escrow_lsig'])
play_turn(algod_client, player, addr_adversary,
addr_game_table, game_table_lsig,
addr_sink, sink_lsig,
addr_adversary_bet_escrow, adversary_bet_escrow_lsig,
asa_turn_id, asa_pieces_id,
asa_pieces_max_remove, asa_pieces_amount,
asa_pieces_total)
else:
addr_adversary = match_data['dealer']
addr_adversary_bet_escrow = match_data['dealer_bet_escrow']
adversary_bet_escrow_lsig = encoding.msgpack_decode(
match_data['dealer_bet_escrow_lsig'])
play_turn(algod_client, player, addr_adversary,
addr_game_table, game_table_lsig,
addr_sink, sink_lsig,
addr_adversary_bet_escrow, adversary_bet_escrow_lsig,
asa_turn_id, asa_pieces_id,
asa_pieces_max_remove, asa_pieces_amount,
asa_pieces_total)
elif args['status']:
with open("algonim.match", "rb") as f:
match_data_bytes = f.read()
f.close()
match_data = msgpack.unpackb(match_data_bytes)
player_info = algod_client.account_info(args['<player_address>'])
game_table_info = algod_client.account_info(match_data['game_table'])
dealer_info = algod_client.account_info(match_data['dealer'])
asa_pieces_total = next(
(asa['params']['total'] for asa in dealer_info['created-assets'] if
asa['index'] == match_data['asa_pieces_id']), None)
pieces_on_table = next(
(asa['amount'] for asa in game_table_info['assets'] if
asa['asset-id'] == match_data['asa_pieces_id']), None)
dealer_bet_escrow_info = algod_client.account_info(
match_data['dealer_bet_escrow'])
opponent_bet_escrow_info = algod_client.account_info(
match_data['opponent_bet_escrow'])
print("\nMATCH TOTAL PIECES:\t\t" + str(asa_pieces_total))
print("PIECES ON THE GAME TABLE:\t" + str(pieces_on_table))
if pieces_on_table != 0:
if next((asa['amount'] for asa in player_info['assets'] if
asa['asset-id'] == match_data['asa_turn_id']), None) == 1:
print("It's your turn! Play your best move!")
else:
print("Your opponent is playing the turn...")
else:
print("The match is over!")
dealer_bet_escrow_amount = dealer_bet_escrow_info['amount']
opponent_bet_escrow_amount = opponent_bet_escrow_info['amount']
if args['<player_address>'] == match_data['dealer']:
print("\nOPPONENT BET ESCROW AMOUNT:\t",
opponent_bet_escrow_amount)
print("YOUR BET ESCROW AMOUNT:\t\t",
dealer_bet_escrow_amount)
if dealer_bet_escrow_amount != 0:
blockchain_params = algod_client.suggested_params()
last_round = blockchain_params.first
bet_escrow_expiry = match_data[
'dealer_bet_escrow_expiry'] - last_round
if bet_escrow_expiry > 0:
print("Your Bet Escrow is still locked.",
bet_escrow_expiry, "blocks left!\n")
else:
print("Your Bet Escrow expired! " +
"You can claim your bet back.")
elif args['<player_address>'] == match_data['opponent']:
print("\nOPPONENT BET ESCROW AMOUNT:\t",
dealer_bet_escrow_amount)
print("YOUR BET ESCROW AMOUNT:\t\t",
opponent_bet_escrow_amount)
if opponent_bet_escrow_amount != 0:
blockchain_params = algod_client.suggested_params()
last_round = blockchain_params.first
bet_escrow_expiry = match_data[
'opponent_bet_escrow_expiry'] - last_round
if bet_escrow_expiry > 0 and opponent_bet_escrow_amount != 0:
print("Your Bet Escrow is still locked.",
bet_escrow_expiry, "blocks left!\n")
else:
print("Your Bet Escrow expired! " +
"You can claim your bet back.")
else:
print("Invalid player address for this match!")
elif args['close']:
with open("algonim.match", "rb") as f:
match_data_bytes = f.read()
f.close()
match_data = msgpack.unpackb(match_data_bytes)
if args['<player_address>'] == match_data['dealer']:
dealer_bet_escrow_lsig = encoding.msgpack_decode(
match_data['dealer_bet_escrow_lsig'])
close_bet_escrow(algod_client,
match_data['dealer_bet_escrow'],
match_data['dealer'],
dealer_bet_escrow_lsig)
elif args['<player_address>'] == match_data['opponent']:
opponent_bet_escrow_lsig = encoding.msgpack_decode(
match_data['opponent_bet_escrow_lsig'])
close_bet_escrow(algod_client,
match_data['opponent_bet_escrow'],
match_data['opponent'],
opponent_bet_escrow_lsig)
else:
print("Invalid player address for this match!")
else:
print("\nError: read AlgoNim '--help'!\n")
class ErrAlgodClientCompile(Exception):
def __init__(self):
Exception.__init__(
self, "\n\nAlgoNim failed to compile TEAL source code. Please:\n\n"
+ "\t1. Go to your Algorand node data folder\n"
+ "\t2. Copy 'config.json.example' as 'config.json'\n"
+ "\t3. Open and edit 'config.json'\n"
+ "\t4. Set \"EnableDeveloperAPI\" to true and save\n"
+ "\t5. Restart your Algorand node: goal node restart\n")
if __name__ == "__main__":
main()