araeyn commited on
Commit
6cb8604
·
1 Parent(s): 5d3827d

client and update .gitignore

Browse files
Files changed (2) hide show
  1. .gitignore +4 -0
  2. client.py +362 -0
.gitignore ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ .venv/
2
+ __pycache__/
3
+ glares.json
4
+ users.json
client.py ADDED
@@ -0,0 +1,362 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import asyncio
2
+ import json
3
+ import random
4
+
5
+ import aiohttp
6
+ import websockets
7
+
8
+ # base = "araeynn-glorified.hf.space" # For production
9
+ base = "localhost:7860" # For local testing
10
+ API_URL = f"https://{base}"
11
+ if base.startswith("localhost"):
12
+ API_URL = f"http://{base}"
13
+ WS_URL_ROULETTE = f"ws://{base}/ws/roulette"
14
+ WS_URL_SPINNER = f"ws://{base}/ws/spinner"
15
+ WS_URL_CRASH = f"ws://{base}/ws/crash"
16
+ WS_URL_MINES = f"ws://{base}/ws/mines"
17
+ WS_URL_TOWER = f"ws://{base}/ws/tower"
18
+ else:
19
+ WS_URL_ROULETTE = f"wss://{base}/ws/roulette"
20
+ WS_URL_SPINNER = f"wss://{base}/ws/spinner"
21
+ WS_URL_CRASH = f"wss://{base}/ws/crash"
22
+ WS_URL_MINES = f"wss://{base}/ws/mines"
23
+ WS_URL_TOWER = f"wss://{base}/ws/tower"
24
+
25
+ USERNAME = "testuser"
26
+ PASSWORD = "testpass"
27
+
28
+
29
+ async def register_user():
30
+ async with aiohttp.ClientSession() as session:
31
+ data = {"username": USERNAME, "password": PASSWORD}
32
+ async with session.post(f"{API_URL}/register", data=data) as resp:
33
+ # If already registered, the endpoint will return an error.
34
+ if resp.status != 200:
35
+ text = await resp.text()
36
+ print(f"Register failed (maybe already registered): {text}")
37
+ else:
38
+ res = await resp.json()
39
+ print(f"Register response: {res}")
40
+
41
+
42
+ async def play_roulette():
43
+ async with websockets.connect(WS_URL_ROULETTE) as websocket:
44
+ # Send authentication
45
+ auth_msg = {"username": USERNAME, "password": PASSWORD}
46
+ await websocket.send(json.dumps(auth_msg))
47
+ # Receive welcome message with balance
48
+ welcome = await websocket.recv()
49
+ data = json.loads(welcome)
50
+ print(f"Initial connection: {data}")
51
+
52
+ # Wait 2 seconds
53
+ await asyncio.sleep(1)
54
+
55
+ # Bet 1 on red
56
+ bet_msg = {"bet": {"red": 1}}
57
+ print("Placing bet of 1 on red...")
58
+ await websocket.send(json.dumps(bet_msg))
59
+ response = await websocket.recv()
60
+ print(f"Bet response: {json.loads(response)}")
61
+
62
+ # Wait 2 seconds again
63
+ await asyncio.sleep(2)
64
+
65
+ # Bet 1 on red again
66
+ bet_msg = {"bet": {"red": 1}}
67
+ print("Placing another bet of 1 on red...")
68
+ await websocket.send(json.dumps(bet_msg))
69
+ response = await websocket.recv()
70
+ print(f"Bet response: {json.loads(response)}")
71
+
72
+
73
+ async def play_spinner():
74
+ # First get current balance to bet all-in
75
+ async with aiohttp.ClientSession() as session:
76
+ params = {"username": USERNAME, "password": PASSWORD}
77
+ async with session.get(f"{API_URL}/glares", params=params) as resp:
78
+ balance_data = await resp.json()
79
+ balance = balance_data.get("glares", 0)
80
+ print(f"Current balance: {balance}")
81
+
82
+ # Now connect to spinner and bet all-in
83
+ async with websockets.connect(WS_URL_SPINNER) as websocket:
84
+ # Send authentication
85
+ auth_msg = {"username": USERNAME, "password": PASSWORD}
86
+ await websocket.send(json.dumps(auth_msg))
87
+
88
+ # Receive welcome message
89
+ welcome = await websocket.recv()
90
+ data = json.loads(welcome)
91
+ print(f"Spinner connection: {data}")
92
+
93
+ # Get current balance for all-in bet
94
+ current_balance = data.get("glares", balance)
95
+
96
+ # Wait 2 seconds
97
+ await asyncio.sleep(2)
98
+
99
+ # All-in bet
100
+ bet_msg = {"bet": int(current_balance)}
101
+ print(f"Placing ALL-IN bet of {int(current_balance)} on spinner...")
102
+ await websocket.send(json.dumps(bet_msg))
103
+
104
+ # Get result
105
+ response = await websocket.recv()
106
+ result = json.loads(response)
107
+ print(f"Spinner result: {result}")
108
+
109
+ if "error" in result:
110
+ print(f"Error in spinner: {result['error']}")
111
+ else:
112
+ multiplier = result.get("multiplier", 0)
113
+ earnings = result.get("earnings", 0)
114
+ print(f"Multiplier: {multiplier}x")
115
+ print(f"Earnings: {earnings}")
116
+
117
+
118
+ async def play_crash():
119
+ """Play the crash game and cash out after 2 seconds"""
120
+ # Get balance first
121
+ async with aiohttp.ClientSession() as session:
122
+ params = {"username": USERNAME, "password": PASSWORD}
123
+ async with session.get(f"{API_URL}/glares", params=params) as resp:
124
+ balance_data = await resp.json()
125
+ balance = balance_data.get("glares", 0)
126
+ print(f"Current balance before crash: {balance}")
127
+
128
+ # Connect to crash game
129
+ async with websockets.connect(WS_URL_CRASH) as websocket:
130
+ # Send authentication
131
+ auth_msg = {"username": USERNAME, "password": PASSWORD}
132
+ await websocket.send(json.dumps(auth_msg))
133
+
134
+ # Receive welcome message
135
+ welcome = await websocket.recv()
136
+ data = json.loads(welcome)
137
+ print(f"Crash game connection: {data}")
138
+
139
+ # Wait 1 second
140
+ await asyncio.sleep(1)
141
+
142
+ # Place bet (half of balance)
143
+ bet_amount = max(int(balance / 2), 1) # At least 1
144
+ bet_msg = {"bet": bet_amount}
145
+ print(f"Placing bet of {bet_amount} on crash game...")
146
+ await websocket.send(json.dumps(bet_msg))
147
+
148
+ # Set up a task to cash out after 2 seconds
149
+ async def cashout_after_delay():
150
+ await asyncio.sleep(2)
151
+ print("Cashing out after 2 seconds...")
152
+ await websocket.send(json.dumps({"action": "cashout"}))
153
+
154
+ cashout_task = asyncio.create_task(cashout_after_delay())
155
+
156
+ # Listen for messages
157
+ last_multiplier = 1.0
158
+ try:
159
+ while True:
160
+ response = await websocket.recv()
161
+ data = json.loads(response)
162
+
163
+ # Track multipliers
164
+ if "multiplier" in data:
165
+ last_multiplier = data["multiplier"]
166
+ print(f"Current multiplier: {last_multiplier}x", end="\r")
167
+
168
+ # Check for crash
169
+ if "crash" in data:
170
+ print(f"\nGame crashed at {data['crash']}x!")
171
+ break
172
+
173
+ # Check for cash out
174
+ if "cashed_out" in data:
175
+ print(f"\nCashed out at {data['cashed_out']}x!")
176
+
177
+ # Check for final message
178
+ if "message" in data and "earnings" in data:
179
+ print(f"Result: {data['message']}")
180
+ print(f"Earnings: {data['earnings']}")
181
+ break
182
+ except Exception as e:
183
+ print(f"Error in crash game: {e}")
184
+ finally:
185
+ if not cashout_task.done():
186
+ cashout_task.cancel()
187
+
188
+
189
+ async def play_mines():
190
+ """Play the mines game with random placements and cash out after 5 reveals"""
191
+ # Get balance first
192
+ async with aiohttp.ClientSession() as session:
193
+ params = {"username": USERNAME, "password": PASSWORD}
194
+ async with session.get(f"{API_URL}/glares", params=params) as resp:
195
+ balance_data = await resp.json()
196
+ balance = balance_data.get("glares", 0)
197
+ print(f"Current balance before mines: {balance}")
198
+
199
+ # Connect to mines game
200
+ async with websockets.connect(WS_URL_MINES) as websocket:
201
+ # Send authentication
202
+ auth_msg = {"username": USERNAME, "password": PASSWORD}
203
+ await websocket.send(json.dumps(auth_msg))
204
+
205
+ # Receive welcome message
206
+ welcome = await websocket.recv()
207
+ data = json.loads(welcome)
208
+ print(f"Mines game connection: {data}")
209
+
210
+ # Wait 1 second
211
+ await asyncio.sleep(1)
212
+
213
+ # Place bet (half of balance)
214
+ bet_amount = max(int(balance / 2), 1) # At least 1
215
+ mines_count = 3 # Using 5 mines (medium difficulty)
216
+ bet_msg = {"bet": bet_amount, "mines": mines_count}
217
+ print(f"Placing bet of {bet_amount} on mines game with {mines_count} mines...")
218
+ await websocket.send(json.dumps(bet_msg))
219
+
220
+ # Keep track of revealed positions
221
+ revealed_positions = set()
222
+ available_positions = set(range(25))
223
+ num_reveals = 0
224
+ max_reveals = 5 # Cash out after 5 reveals
225
+
226
+ try:
227
+ while True:
228
+ # Choose a random position that hasn't been revealed yet
229
+ available = list(available_positions - revealed_positions)
230
+ if not available or num_reveals >= max_reveals:
231
+ # Cash out after revealing max_reveals positions
232
+ print(f"Cashing out after {num_reveals} reveals...")
233
+ await websocket.send(json.dumps({"action": "cashout"}))
234
+ break
235
+
236
+ position = random.choice(available)
237
+ print(f"Revealing position {position}...")
238
+ await websocket.send(
239
+ json.dumps({"action": "reveal", "position": position})
240
+ )
241
+
242
+ # Get result
243
+ response = await websocket.recv()
244
+ data = json.loads(response)
245
+
246
+ if "error" in data:
247
+ print(f"Error in mines game: {data['error']}")
248
+ break
249
+
250
+ if "multiplier" in data and "revealed" in data:
251
+ revealed_positions = set(data["revealed"])
252
+ multiplier = data["multiplier"]
253
+ print(f"Current multiplier: {multiplier}x")
254
+ num_reveals += 1
255
+ continue
256
+
257
+ # If we get here with other data, the game might be over
258
+ if "multiplier" in data and "earnings" in data:
259
+ print(f"Game finished with multiplier: {data['multiplier']}x")
260
+ print(f"Earnings: {data['earnings']}")
261
+ if "safe_positions" in data:
262
+ print(f"Safe positions were: {data['safe_positions']}")
263
+ break
264
+
265
+ except Exception as e:
266
+ print(f"Error in mines game: {e}")
267
+
268
+
269
+ async def play_tower():
270
+ """Play the tower game with random tile selections and cash out after a random number of levels"""
271
+ # Get balance first
272
+ async with aiohttp.ClientSession() as session:
273
+ params = {"username": USERNAME, "password": PASSWORD}
274
+ async with session.get(f"{API_URL}/glares", params=params) as resp:
275
+ balance_data = await resp.json()
276
+ balance = balance_data.get("glares", 0)
277
+ print(f"Current balance before tower: {balance}")
278
+
279
+ # Connect to tower game
280
+ async with websockets.connect(WS_URL_TOWER) as websocket:
281
+ # Send authentication
282
+ auth_msg = {"username": USERNAME, "password": PASSWORD}
283
+ await websocket.send(json.dumps(auth_msg))
284
+
285
+ # Receive welcome message
286
+ welcome = await websocket.recv()
287
+ data = json.loads(welcome)
288
+ print(f"Tower game connection: {data}")
289
+
290
+ # Wait 1 second
291
+ await asyncio.sleep(1)
292
+
293
+ # Place bet (half of balance)
294
+ bet_amount = max(int(balance / 2), 1) # At least 1
295
+ bet_msg = {"bet": bet_amount}
296
+ print(f"Placing bet of {bet_amount} on tower game...")
297
+ await websocket.send(json.dumps(bet_msg))
298
+
299
+ # Choose a random number of levels to attempt (1-8)
300
+ target_levels = random.randint(1, 8)
301
+ print(f"Will attempt {target_levels} levels before cashing out")
302
+
303
+ current_level = 0
304
+
305
+ try:
306
+ while True:
307
+ # Check if we've reached our target number of levels
308
+ if current_level >= target_levels:
309
+ print(f"Reached target of {target_levels} levels. Cashing out...")
310
+ await websocket.send(json.dumps({"action": "cashout"}))
311
+ break
312
+
313
+ # Choose a random position (0-3) for the next level
314
+ position = random.randint(0, 3)
315
+ print(f"Selecting position {position} for level {current_level + 1}...")
316
+ await websocket.send(
317
+ json.dumps({"action": "reveal", "position": position})
318
+ )
319
+
320
+ # Get result
321
+ response = await websocket.recv()
322
+ data = json.loads(response)
323
+
324
+ if "error" in data:
325
+ print(f"Error in tower game: {data['error']}")
326
+ break
327
+
328
+ if "level" in data and "multiplier" in data:
329
+ current_level = data["level"]
330
+ multiplier = data["multiplier"]
331
+ print(
332
+ f"Advanced to level {current_level} with multiplier: {multiplier}x"
333
+ )
334
+ continue
335
+
336
+ # If we get here with other data, the game might be over
337
+ if "multiplier" in data and "earnings" in data:
338
+ print(f"Game finished with multiplier: {data['multiplier']}x")
339
+ print(f"Earnings: {data['earnings']}")
340
+ if "safe_positions" in data:
341
+ print(f"Safe positions were: {data['safe_positions']}")
342
+ break
343
+
344
+ except Exception as e:
345
+ print(f"Error in tower game: {e}")
346
+
347
+
348
+ async def main():
349
+ await register_user()
350
+ await play_roulette()
351
+ # Play spinner all-in
352
+ # await play_spinner()
353
+ # Play crash game with cashout after 2 seconds
354
+ await play_crash()
355
+ # Play mines game with random placements
356
+ await play_mines()
357
+ # Play tower game with random levels
358
+ await play_tower()
359
+
360
+
361
+ if __name__ == "__main__":
362
+ asyncio.run(main())