/
main.py
315 lines (271 loc) · 10.7 KB
/
main.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
from User import User
from Battle import Battle
from Page import Page
from dotenv import load_dotenv
from time import sleep
from random import randint, uniform
from datetime import datetime, timedelta
from playwright.sync_api import sync_playwright
from rich.traceback import install
from rich.console import Console
import os
import sys
import traceback
import json
import requests
# setup default rich traceback (pretty print)
install()
console = Console()
# green = success
# red = failure
# white = main
# magenta = page
# blue = user
# yellow = battle
# load .env variables
load_dotenv(override=True)
# load card details
CARDS_DETAILS = []
try:
with console.status(
"[bold blue]Obtaining all card information...", spinner="toggle10"
) as status:
sleep(randint(2, 4))
CARDS_DETAILS = json.loads(
requests.get("https://api.splinterlands.io/cards/get_details").content
)
console.log("[bold white]Data Status: [bold green]Card information loaded")
except:
console.print_exception()
console.log("[bold white]Data Status: [bold red]Card information unable to loaded")
sys.exit()
sleep(randint(2, 4))
# load battlebase (previously logged battles database)
BATTLEBASE = []
try:
with console.status(
"[bold blue]Booting up the battlebase connection...", spinner="betaWave"
) as status:
sleep(randint(2, 4))
with open("collection.json") as f:
BATTLEBASE = json.load(f)
console.log("[bold white]Battlebase Status: [bold green]Battlebase loaded")
except:
console.print_exception()
console.log("[bold white]Battlebase Status: [bold red]Battlebase unable to load")
sys.exit()
sleep(randint(2, 4))
# gets global user details
USER = {
"username": os.getenv("USERNAME").lower(),
"email": os.getenv("EMAIL"),
"password": os.getenv("PASSWORD"),
"claim_reward_quest": os.getenv("CLAIM_REWARD_QUEST").lower() == "true" or False,
"claim_reward_season": os.getenv("CLAIM_REWARD_SEASON").lower() == "true" or False,
}
# get global browser details
play = sync_playwright().start()
creation = datetime.now()
BROWSER = {
"base_url": "https://splinterlands.com",
"headless": os.getenv("HEADLESS").lower() == "true",
"args": [
"--no-sandbox",
"--disable-setuid-sandbox",
"--single-process",
"--hide-scrollbars",
"--mute-audio",
"--disable-web-security",
],
"creation": creation,
"lifespan": randint(15, 20), # minutes
}
p_browser = play.chromium.launch_persistent_context(
"./", headless=BROWSER["headless"], args=BROWSER["args"]
)
BROWSER["browser"] = p_browser
# get global battle details
BATTLE = {
"card_details": CARDS_DETAILS,
"battle_interval": int(os.getenv("BATTLE_INTERVAL")),
"ecr_min": float(int(os.getenv("ECR_MIN")) / 100),
"ecr_max": float(int(os.getenv("ECR_MAX")) / 100),
"prioritize_quest": os.getenv("PRIORITIZE_QUEST").lower() == "true",
}
def main():
try:
# Step 0: Build Page
page = Page(console, BROWSER)
sleep(uniform(1, 2))
browser_status = True
# Step 1: Setup Player
player = User(console, USER)
sleep(uniform(1, 2))
while True:
try:
# Step 2: Checks if player is logged in; if not, logs them in
with console.status(
"[bold blue]Checking if player is logged in"
) as status:
player_login_status = page.is_logged_in(player.username)
sleep(randint(1, 3))
if not player_login_status:
console.log(
"[bold white]Login Status: [bold red]Currently logged out"
)
page.logout_account()
# Step 2.1: Attempts to login player
player_login = page.login(player)
sleep(1)
if player_login == None:
console.log("[bold red]Login attempt failed :x:")
sleep(randint(3, 5))
break
else:
console.log(
"[bold white] Login Status: [bold green]Login attempt successful :white_check_mark:"
)
else:
console.log(
"[bold white]Login Status: [bold yellow]Currently logged in"
)
sleep(1)
# Step 3: Check if player is already in battle
if not page.is_mid_battle():
# Step 3.1: Calculate how long we should wait for ECR to reset
ecr_wait = 0
with console.status("[bold blue]Calculating Current ECR") as status:
ecr_wait = page.calculate_ecr_wait(
BATTLE["ecr_min"], BATTLE["ecr_max"]
)
if ecr_wait > 0:
# Step 3.2: If have to wait, close browser and user data
BROWSER["browser"].close()
with console.status(
"[bold blue]Waiting "
+ str((ecr_wait / 60) / 60)
+ " Hours",
spinner="pong",
) as status:
sleep(ecr_wait)
p_browser = play.chromium.launch_persistent_context(
"./", headless=BROWSER["headless"], args=BROWSER["args"]
)
BROWSER["browser"] = p_browser
page = Page(console, BROWSER)
sleep(uniform(1, 2))
continue
else:
for _ in range(2):
page.close_modal()
# Step 4: Initiate battle
with console.status(
"[bold blue]Initiating battle", spinner="shark"
) as status:
battle_initiation = page.initiate_battle()
if not battle_initiation:
console.log("[bold red]Something occurred during battle initiation")
break
sleep(randint(1, 3))
# Step 5 Create battle instance with battle details
with console.status("[bold blue]Getting battle details") as status:
battle_details = page.get_battle_details()
sleep(1)
battle = Battle(
console, player, CARDS_DETAILS, battle_details, BATTLEBASE
)
# Step 6: Choose deck from battle deck logs
with console.status(
"[bold blue]Picking deck", spinner="growVertical"
) as status:
deck = battle.get_deck(BATTLE["prioritize_quest"])
sleep(uniform(1, 2))
# Step 7: Click cards on the page
page.click_cards(deck)
# Step 8: Start the battle on the page
page.start_battle()
# Step 9: Check who won the battle
winner = battle.check_winner()
if winner == USER["username"]:
player.battle_streak += 1
console.log("[bold white]Winner: [bold green]" + winner)
player.battles_won += 1
elif winner == "DRAW":
console.log("[bold white]Winner: [bold yellow]" + winner)
player.battles_drawn += 1
else:
player.battle_streak = 0
player.battles_lost += 1
console.log("[bold white]Winner: [bold red]" + winner)
player.battles_played += 1
sleep(uniform(1, 2))
# Step 9: Attempts to claim rewards
# if (
# CLAIM_REWARD_QUEST
# and player.quest["claimed"] == None
# and player.quest["quest_total"] == player.quest["completed_total"]
# ):
# page.claim_reward("quest")
# if CLAIM_REWARD_SEASON:
# page.claim_reward("season")
console.log(
"[bold white]Battle Stats: [bold green]"
+ str(player.battles_won)
+ "[white]/[bold yellow]"
+ str(player.battles_drawn)
+ "[white]/[bold red]"
+ str(player.battles_lost)
+ " [white]([bold blue]"
+ str(player.battles_played)
+ "[white])"
)
if player.battle_streak > 1:
sleep(uniform(1, 2))
console.log(
"[bold white]Battle Win Streak: [bold green]"
+ str(player.battle_streak)
)
sleep(uniform(1, 2))
except Exception:
console.print_exception(show_locals=True)
console.log(
"[bold white]Browser Status: [bold red]Error Occurred During Battle Process"
)
del page
BROWSER["browser"].close()
browser_status = False
# Step 10: Sleep for set interval
console.log(
"[bold white]Rest Period: [bold hot_pink]"
+ str(BATTLE["battle_interval"])
+ " [bold white]Seconds"
)
print()
# Step 10.1: Close browser if older than browser_life_allowed minutes or was closed
if (
BROWSER["creation"]
< datetime.now() - timedelta(minutes=BROWSER["lifespan"])
and browser_status != False
):
del page
BROWSER["browser"].close()
browser_status = False
sleep(BATTLE["battle_interval"])
# Step 10.2: Open browser if old one was closed
if browser_status == False:
p_browser = play.chromium.launch_persistent_context(
"./", headless=BROWSER["headless"], args=BROWSER["args"]
)
browser_status = True
BROWSER["browser"] = p_browser
BROWSER["creation"] = datetime.now()
page = Page(console, BROWSER)
sleep(uniform(1, 2))
except KeyboardInterrupt:
console.log("[bold red]Shutdown requested...")
sleep(3)
except:
console.print_exception(show_locals=True)
sys.exit()
if __name__ == "__main__":
main()