-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoracle.py
More file actions
210 lines (176 loc) · 7.87 KB
/
Copy pathoracle.py
File metadata and controls
210 lines (176 loc) · 7.87 KB
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
from algosdk import account, encoding, mnemonic, transaction
from q_learning.model import BattleBot, Game, load_bot, delete_bot, Memory
from q_learning.connect4 import Connect4
from algosdk.v2client import algod
from pyteal import *
from q_learning.train import play_battle_bots
from algosdk.v2client import indexer
import algosdk
import time, base64, uuid, os, json
# Connect to Algorand node
algod_address = "https://testnet-algorand.api.purestake.io/ps2"
algod_token = "" # PUT TOKEN HERE
headers = {"X-API-KEY": algod_token}
my_client = algod.AlgodClient(algod_token, algod_address, headers)
# Define smart contract information
apid = 0 # PUT APP ID HERE
app_address = "" # PUT APP ADDRESS HERE
privateKey = "" #private key for sending winner
publicAdd = "" #public key for sending winner
pa = encoding.decode_address(publicAdd)
gameDone = True
# [run game]
def deleteBot(botID):
print(delete_bot(botID))
def runGame(player1ID, player2ID, botID1, botID2):
gameDone = False
memory = Memory()
# Load the bots for both players
player_1_bot_id = botID1
player_1_bot_name = 'BattleBotA'
player_1_bot = load_bot(player_1_bot_id)
# If the bot does not exist, create a new one
if player_1_bot is None:
print('Creating new bot for player 1...')
player_1_bot = BattleBot(player_1_bot_name, player_1_bot_id, f'./q_learning/models/{player_1_bot_id}.pt', None)
player_1_bot.save_bot()
player_2_bot_id = botID2
player_2_bot_name = 'BattleBotB'
player_2_bot = load_bot(player_2_bot_id)
# If the bot does not exist, create a new one
if player_2_bot is None:
print('Creating new bot for player 2...')
player_2_bot = BattleBot(player_2_bot_name, player_2_bot_id, f'./q_learning/models/{player_2_bot_id}.pt', None)
player_2_bot.save_bot()
# Create the Connect 4 board
board = Connect4()
# Have the two battle bots from the players compete against each other
player_1_bot, player_2_bot, actions, winner_id = play_battle_bots(board, memory, player_1_bot, player_2_bot)
# Save the game to the database
if winner_id == player_1_bot.bot_id:
winner_name = player_1_bot.name
player_1_bot.win_count += 1
callContract(player1ID, player1ID, player2ID, False)
elif winner_id == player_2_bot.bot_id:
winner_name = player_2_bot.name
player_2_bot.win_count += 1
callContract(player2ID, player1ID, player2ID, False)
elif winner_id == 'NotValidWinnerID':
winner_name = 'No Winner Found'
callContract(winner_id, player1ID, player2ID, True)
game_id = str(uuid.uuid4())
game = Game(game_id, player_1_bot.bot_id, player_2_bot.bot_id, winner_name, actions)
game.save_game()
# Print the winner of the Connect 4 Game
player_1_bot.total_games += 1
player_2_bot.total_games += 1
player_1_bot.games.append(game_id)
player_2_bot.games.append(game_id)
# Update the player models & battle bots associated with each player
player_1_bot.save_bot()
player_2_bot.save_bot()
print(f'Winner: {winner_name}!!!')
gameDone = True
# [update and send info]
# with the return from play_battle_bots update models in database, update
# website with moves, and call smart contract with winner (with word 'resolve'
# as first arguement), winner id must be base32 encoded.
# Connect to Algorand node
def wait_for_confirmation(client, txid):
last_round = client.status().get("last-round")
txinfo = client.pending_transaction_info(txid)
while not (txinfo.get("confirmed-round") and txinfo.get("confirmed-round") > 0):
print("Waiting for confirmation...")
last_round += 1
client.status_after_block(last_round)
txinfo = client.pending_transaction_info(txid)
print(
"Transaction {} confirmed in round {}.".format(
txid, txinfo.get("confirmed-round")
)
)
return txinfo
def call_app(client, private_key, pub,index, app_args, player1ID, player2ID):
# declare sender
sender = pub
print("Call from account:", sender)
# get node suggested parameters
params = client.suggested_params()
# comment out the next two (2) lines to use suggested fees
params.flat_fee = True
params.fee = 1000
# create unsigned transaction
txn = transaction.ApplicationNoOpTxn(sender, params, index, app_args,accounts=[player1ID,player2ID])
# sign transaction
signed_txn = txn.sign(private_key)
tx_id = signed_txn.transaction.get_txid()
# send transaction
client.send_transactions([signed_txn])
# await confirmation
wait_for_confirmation(client, tx_id)
def callContract(winner, player1ID, player2ID, tie):
if (tie):
args = [b'tie', encoding.decode_address(player1ID)]
else:
args = [b'resolve', encoding.decode_address(winner)]
call_app(my_client, privateKey, publicAdd ,apid, args, player1ID, player2ID)
# [recieve game information]
# check algorand blockchain every few seconds and if any users local state's game
# ready variable is set to 'true' then pull model id and opponent model id
# Define oracle logic
# Wait for events
def wait_for_events():
# Initialize the indexer client
indexer_address = "https://testnet-algorand.api.purestake.io/idx2"
idx_client = indexer.IndexerClient(algod_token, indexer_address, headers)
last_txn_id = 0
# Loop indefinitely
while True:
if gameDone:
print("Searching for Transactions...")
# Get the transactions of the smart contract with a higher ID than the last processed ID
txns = idx_client.search_transactions(
#address = app_address,
application_id = apid,
min_round=last_txn_id + 1
)
# Iterate over the new transactions and extract the user ID
opponents = {}
botid = {}
for txn in txns["transactions"]:
if (txn['tx-type'] == 'appl') and ('local-state-delta' in txn.keys()):
local_state = txn['local-state-delta'][0]['delta']
for pair in local_state:
if base64.b64decode(pair['key']).decode('utf-8') == 'bot':
try:
botid[txn['sender']] = str(int.from_bytes(base64.b64decode(pair['value']['bytes']), 'big'))
except:
pass
if txn['sender'] in opponents:
print("running game")
runGame(txn['sender'], opponents[txn['sender']], botid[txn['sender']], botid[opponents[txn['sender']]])
else:
for pair in local_state:
if base64.b64decode(pair['key']).decode('utf-8') == 'opponent':
try:
opponents[algosdk.encoding.encode_address(base64.b64decode(pair['value']['bytes']))] = txn['sender']
except:
pass
elif ('application-transaction' in txn.keys()):
if ('application-args' in txn['application-transaction'].keys()):
try:
if (base64.b64decode(txn['application-transaction']['application-args'][0])) == b'delete bot':
print("deleting bot")
deleteBot(str(int.from_bytes(base64.b64decode(txn['application-transaction']['application-args'][1]), 'big')))
except:
pass
# Update the last processed ID
last_txn_id = max(last_txn_id, txn["confirmed-round"])
# Wait for a few seconds before checking for new transactions again
time.sleep(10)
# Main function
def main():
# Wait for events
wait_for_events()
if __name__ == "__main__":
main()