-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbot.py
854 lines (749 loc) · 31.9 KB
/
bot.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
"""Discord Bot for ship analysis"""
import base64
import json
import os
import random
import traceback
from datetime import datetime as dt
from io import BytesIO
import logging
import discord
import requests
from discord import app_commands
from dotenv import load_dotenv
import data_analysis
import fight_db
import text_content
load_dotenv()
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("discord")
API_URL = "https://api.cosmoship.duckdns.org/"
API_NEW = "https://cosmo-api-six.vercel.app/"
try:
db = fight_db.FightDB(db_name="/home/astrod/Desktop/Bots/cosmoteer-com/test.db")
logger.info("Successfully initialized db")
except Exception as e:
logger.error("Error initializing the db:")
logger.error(traceback.format_exc())
intents = discord.Intents.default()
# client = discord.Client(intents=intents)
client = discord.AutoShardedClient(shard_count=3, intents=intents)
tree = app_commands.CommandTree(client)
VERSION_TEXT = text_content.VERSION_TEXT
HELP_TEXT = text_content.HELP_TEXT
DB_HELP_TEXT = text_content.DB_HELP_TEXT
@client.event
async def on_ready():
"""
Event handler that is called when the bot is ready to start receiving events.
"""
# Set the bot's presence to the specified game
await client.change_presence(activity=discord.Game(name="Cosmoteer (/help)"))
# Print a message indicating that the slash commands are being synced
print(dt.now(), "Syncing slash commands")
# Sync the slash commands with the Discord API
await tree.sync()
# Print a list of guilds that the bot is connected to
print("Guilds:")
for guild in client.guilds:
print("\t- " + guild.name, guild.id)
# Print a message indicating that the bot is ready
print(dt.now(), "Bot is ready")
@tree.command(name="com", description="Calculates the center of mass of a cosmoteer ship.png")
async def com(
interaction: discord.Interaction,
ship: discord.Attachment,
boost: bool = True,
flip_vectors: bool = False,
draw_all_cot: bool = True,
draw_all_com: bool = False,
cosmoship_api: bool = False,
):
"""
Calculates the center of mass of a cosmoteer ship.png.
Args:
interaction (discord.Interaction): The interaction object representing the command invocation.
ship (discord.Attachment): The attachment object representing the ship.png file.
boost (bool, optional): Flag indicating whether to enable boosters. Defaults to True.
flip_vectors (bool, optional): Flag indicating whether to flip the thrust vectors.
Defaults to False.
draw_all_cot (bool, optional): Flag indicating whether to draw the center of thrust vectors
on every direction. Defaults to True.
draw_all_com (bool, optional): Flag indicating whether to draw the center of mass of each part.
Defaults to False.
Returns:
None
"""
command = tree.get_command("full")
await command.callback(
interaction, ship, boost, flip_vectors, draw_all_cot, draw_all_com, cosmoship_api
)
@tree.command(name="cost", description="Calculates the cost analysis of a cosmoteer ship.png")
async def cost(
interaction: discord.Interaction,
ship: discord.Attachment,
boost: bool = True,
flip_vectors: bool = False,
draw_all_cot: bool = True,
draw_all_com: bool = False,
):
"""
Calculates the cost analysis of a cosmoteer ship.png.
Args:
interaction (discord.Interaction): The interaction object representing
the command invocation.
ship (discord.Attachment): The attachment object representing the ship.png file.
boost (bool, optional): Flag indicating whether to enable boosters. Defaults to True.
flip_vectors (bool, optional): Flag indicating whether to flip the thrust vectors.
Defaults to False.
draw_all_cot (bool, optional): Flag indicating whether to draw the center of thrust
vectors on every direction. Defaults to True.
draw_all_com (bool, optional): Flag indicating whether to draw the center of mass of
each part. Defaults to False.
Returns:
None
"""
command = tree.get_command("full")
await command.callback(interaction, ship, boost, flip_vectors, draw_all_cot, draw_all_com)
@tree.command(
name="full",
description="Calculates the center of mass and cost analysis of a cosmoteer ship.png",
)
async def full(
interaction: discord.Interaction,
ship: discord.Attachment,
boost: bool = True,
flip_vectors: bool = False,
draw_all_cot: bool = True,
draw_all_com: bool = False,
cosmoship_api: bool = False,
):
"""
Calculates the center of mass and cost analysis of a cosmoteer ship.png.
Args:
interaction (discord.Interaction): The interaction object representing
the command invocation.
ship (discord.Attachment): The attachment object representing the ship.png file.
boost (bool, optional): Flag indicating whether to enable boosters. Defaults to True.
flip_vectors (bool, optional): Flag indicating whether to flip the thrust vectors.
Defaults to False.
draw_all_cot (bool, optional): Flag indicating whether to draw the center of thrust
vectors on every direction. Defaults to True.
draw_all_com (bool, optional): Flag indicating whether to draw the center of mass of
each part. Defaults to False.
"""
# switch between API_NEW and API_URL
if cosmoship_api:
primary_api = API_URL
secondary_api = API_NEW
else:
primary_api = API_NEW
secondary_api = API_URL
print(dt.now(), "received command")
await interaction.response.defer()
print(dt.now(), "deferred")
# Read the image bytes and encode them to base64
image_bytes = await ship.read()
base64_string = base64.b64encode(image_bytes).decode("utf-8")
# Create a file object for the original image
ship = discord.File(BytesIO(image_bytes), filename="input_file.png")
try:
# Prepare the request data
args = {
"boost": boost,
"draw_all_cot": draw_all_cot,
"draw_all_com": draw_all_com,
"draw_cot": True,
"draw_com": True,
"draw": True,
"flip_vectors": flip_vectors,
"analyze": True,
}
json_data = json.dumps({"image": base64_string, "args": args})
# Send the request to the server
print(dt.now(), "requesting data")
# try API_NEW and if it fails, try API_URL
try:
url = primary_api + "analyze"
response = requests.post(url, json=json_data, timeout=30)
response.raise_for_status()
except Exception as e:
print(f"error fetching data, switching to old API {dt.now()} Exception: {e}")
url = secondary_api + "analyze"
response = requests.post(url, json=json_data, timeout=30)
response.raise_for_status()
print(dt.now(), "server responded")
# Get the response
data_returned = response.json()
# Get the URL of the center of mass image
url_com = data_returned["url_com"]
# prepare the data
center_of_mass_x = round(data_returned["center_of_mass_x"], 2)
center_of_mass_y = round(data_returned["center_of_mass_y"], 2)
total_mass = round(data_returned["total_mass"], 2)
top_speed = round(data_returned["top_speed"], 2)
crew = data_returned["crew"]
price = data_returned["price"]
made_by = data_returned["author"]
# convert the data
data_converted = {
"Center of mass": f"{center_of_mass_x}, {center_of_mass_y}",
"Total mass": total_mass,
"Max speed": f"{top_speed} m/s",
"Total crew": crew,
"Aprox cost": price,
"Made by": made_by,
}
text = "use the /help command for more info\n"
categoriescom = ["Center of mass", "Total mass", "Max speed", "Total crew", "Aprox cost"]
if data_returned["author"] != "":
categoriescom += ["Made by"]
embedcom = discord.Embed(title="Center of mass analysis", color=discord.Color.green())
# Create a formatted table header with consistent column widths
table_headercom = "Category | Data \n"
table_headercom += "----------------|--------------\n"
# Create a formatted table body with each category's data
table_bodycom = ""
for category in categoriescom:
data = data_converted[category]
# Format each column with padding to ensure consistent width
category_formatted = f"{category:<15}"
data_formatted = f"{data:>15}"
table_bodycom += f"{category_formatted} | {data_formatted}\n"
# Combine the header and body to form the table
tablecom = f"```\n{table_headercom}{table_bodycom}```"
# Add the table to the Discord embed
embedcom.add_field(
name="\u200b", value=tablecom, inline=False
) # "\u200b" is a zero-width space for better formatting
url_stats = data_returned["analysis"]["url_analysis"]
analysis = data_returned["analysis"]
categories = [
"total_price",
"price_crew",
"price_armor",
"price_weapons",
"price_mouvement",
"price_shield",
"price_storage",
"price_utility",
"price_power",
]
text_categories = {
"total_price": "total",
"price_crew": "crew",
"price_armor": "armor",
"price_weapons": "weapons",
"price_mouvement": "thrust",
"price_shield": "shield",
"price_storage": "storage",
"price_utility": "misc",
"price_power": "power",
}
embed = discord.Embed(title="Price analysis", color=discord.Color.green())
# Create a formatted table header with consistent column widths
table_header = "Category | Percent | Price\n"
table_header += "----------|---------|----------\n"
# Create a formatted table body with each category's data
table_body = ""
for category in categories:
percent = f"{analysis[category]['percent']*100:.2f}%"
price = analysis[category]["price"]
# Format each column with padding to ensure consistent width
category_formatted = f"{text_categories[category]:<9}"
percent_formatted = f"{percent:>7}"
price_formatted = f"{price:>8}"
table_body += f"{category_formatted} | {percent_formatted} | {price_formatted}\n"
# Combine the header and body to form the table
table = f"```\n{table_header}{table_body}```"
# Add the table to the Discord embed
embed.add_field(
name="\u200b", value=table, inline=False
) # "\u200b" is a zero-width space for better formatting
print(dt.now(), "sending to Discord")
# Create an Embed for the Center of Mass image
embedcom.set_image(url=url_com)
# Create an Embed for the Stats image
embed.set_image(url=url_stats)
embeds = [embedcom, embed]
await interaction.followup.send(text, embeds=embeds, files=[ship])
print(dt.now(), "sent to Discord")
except Exception as e:
print(dt.now(), "error", e)
if "data_returned" in locals(): # if the data was returned
text = (
"Error: could not process ship :\n\t"
+ type(e).__name__
+ ":"
+ str(e)
+ str(data_returned)
)
else:
text = (
"Error: could not process ship :\n\t"
+ type(e).__name__
+ ":"
+ str(e)
+ "server did not respond"
)
await interaction.followup.send(text, file=ship)
return "Error: could not process ship"
@tree.command(name="ping", description="responds with the bot's latency")
async def ping(interaction: discord.Interaction):
"""
Responds to a ping command and sends the bot's latency in milliseconds.
Parameters:
interaction (discord.Interaction): The interaction triggered by the user.
Returns:
None
"""
await interaction.response.send_message(f"Pong! {round(client.latency * 1000)}ms")
@tree.command(name="hmmm", description="responds with hmmm")
async def hmmm(interaction: discord.Interaction):
"""a little fun"""
if random.random() < 0.05:
await interaction.response.send_message(
"oh no, not again, please no, not the hmmm, anything but the hmmm, please"
)
else:
await interaction.response.send_message("hmmm")
@tree.command(name="help", description="shows the list of commands")
async def show_help(interaction: discord.Interaction, show_db_commands: bool = False):
"""
Responds to a help command and sends a list of commands.
Parameters:
interaction (discord.Interaction): The interaction triggered by the user.
Returns:
None
"""
if not show_db_commands:
print(dt.now(), "help command received")
# Defer the initial response to prevent timeouts
try:
await interaction.response.defer()
# Send the help text along with the legend image as a file
await interaction.followup.send(HELP_TEXT, file=discord.File("legend.png"))
except Exception as e:
print(dt.now(), "Error:", e)
await interaction.followup.send("Error:" + str(e))
else:
await interaction.response.defer()
await send_long_message(interaction, DB_HELP_TEXT)
@tree.command(
name="elim_rps", description="play rock-paper-scissors, but with elimination archtypes!"
)
async def rps(interaction: discord.Interaction, player_pick: str):
"""rock-paper-scissors"""
ships = {
"cannon wall": {"wins": ["avoider"]},
"avoider": {"wins": ["dc spinner"]},
"dc spinner": {"wins": ["cannon wall"]},
}
player_pick = player_pick.lower().strip()
computer_pick = random.choice(list(ships.keys()))
if player_pick not in ships:
await interaction.response.send_message(
f"Error:{player_pick} You need to pick between " + ", ".join(list(ships.keys()))
)
return
player_win = False
computer_win = False
if computer_pick in ships[player_pick]["wins"]:
player_win = True
elif player_pick in ships[computer_pick]["wins"]:
computer_win = True
if player_win: # Display results to user
message = (
f"{interaction.user.display_name} picked `{player_pick}` and "
f"Cosmoteer Design Tools picked `{computer_pick}`; "
f"{interaction.user.display_name} wins!"
)
await interaction.response.send_message(message)
elif computer_win:
message = (
f"{interaction.user.display_name} picked `{player_pick}` and "
f"Cosmoteer Design Tools picked `{computer_pick}`; "
f"Cosmoteer Design Tools wins!"
)
await interaction.response.send_message(message)
else:
message = (
f"{interaction.user.display_name} and Cosmoteer Design Tools picked `{player_pick}`; "
f"it is a draw!"
)
await interaction.response.send_message(message)
# Function to split and send long messages at newline characters
async def send_long_message(
interaction: discord.Interaction,
text: str,
chunk_size: int = 1800,
use_code_blocks: bool = False,
):
"""help to send long messages"""
start = 0
while start < len(text):
end = start + chunk_size
if end >= len(text):
chunk = text[start:]
else:
# Find the last newline character within the chunk
newline_index = text.rfind("\n", start, end)
if newline_index == -1:
newline_index = end
chunk = text[start:newline_index]
start = newline_index + 1
if use_code_blocks:
chunk = f"```\n{chunk}\n```"
if start == 0:
await interaction.followup.send(chunk)
else:
await interaction.followup.send(chunk)
if end >= len(text):
break
@tree.command(name="db_add_fight", description="adds a new fight to the database")
async def db_add_fight(
interaction: discord.Interaction, shipname1: str, shipname2: str, result: str
):
"""
Adds a new fight to the database.
Args:
interaction (discord.Interaction): The interaction object.
shipname1 (str): The name of the first ship.
shipname2 (str): The name of the second ship.
result (str): The result of the fight.
Returns:
None
"""
shipname1 = shipname1.lower().strip().replace("_", " ")
shipname2 = shipname2.lower().strip().replace("_", " ")
result = result.lower().strip().replace("_", " ")
switched_ship_names = False
not_a_draw = True
if result in {"draw", "d"}:
result = fight_db.FIGHT_RESULT.DRAW
not_a_draw = False
elif result in {"win", "w"}:
result = fight_db.FIGHT_RESULT.WIN
elif result in {"lose", "l"}:
result = fight_db.FIGHT_RESULT.WIN
shipname1, shipname2 = shipname2, shipname1 # switch ship names
switched_ship_names = True
else:
await interaction.response.send_message("Error: result must be 'win', 'lose' or 'draw'")
return
author = str(interaction.user.id)
author_name = interaction.user.display_name
try:
if not switched_ship_names:
for ship_name in shipname2.split(","):
if not_a_draw and ship_name == shipname1:
await interaction.response.send_message(
"Can not add a non draw result for 2 ships of the same type."
)
return
# else: no need for an else since there is a return
db.insert_fight(shipname1, ship_name, author, author_name, result)
else:
for ship_name in shipname1.split(","):
if not_a_draw and ship_name == shipname2:
await interaction.response.send_message(
"Can not add a non draw result for 2 ships of the same type."
)
return
# else: no need for an else since there is a return
db.insert_fight(ship_name, shipname2, author, author_name, result)
winner_text = ""
if result == fight_db.FIGHT_RESULT.WIN:
winner_text = "the winner is " + shipname1
elif result == fight_db.FIGHT_RESULT.DRAW:
winner_text = "it is a draw"
await interaction.response.send_message(
f"In a fight between {shipname1} and {shipname2}, "
f"{winner_text}, according to {author_name}"
)
except Exception as e:
await interaction.response.send_message(f"Error:{e}")
return
@tree.command(name="db_add_all_draws", description="sets all ships to draw in the database")
async def db_add_all_draws(interaction: discord.Interaction):
"""sets all ships to draw in the database"""
author = str(interaction.user.id)
author_name = interaction.user.display_name
try:
await interaction.response.defer() # Acknowledge the interaction to avoid timeout
for ship in db.get_ships():
db.insert_fight(ship, ship, author, author_name, 0)
await interaction.followup.send(
"All draws of the same ships fighting each other added to the database"
)
except Exception as e:
await interaction.followup.send(f"Error: {e}")
@tree.command(name="db_add_ship", description="adds a new ship to the database")
async def db_add_ship(
interaction: discord.Interaction, shipname: str, parentname: str = None, description: str = None
):
"""adds a new ship to the database"""
shipname = shipname.lower().strip()
if parentname is not None:
parentname = parentname.lower().strip()
try:
if parentname == shipname:
await interaction.response.send_message("Ship cant be its own parent")
if (not db.archetype_exists(parentname)) and parentname is not None:
await interaction.response.send_message("That parent does not exist")
backup = backup_file()
await interaction.response.send_message(
f"Ship {shipname} added to the database", file=backup
)
db.add_ship(shipname, parentname, description)
except Exception as e:
await interaction.response.send_message(f"Error:{e}")
return
@tree.command(name="db_remove_fight", description="removes a fight from the database")
async def db_remove_fight(interaction: discord.Interaction, shipname1: str, shipname2: str):
"""removes a fight from the database"""
shipname1 = shipname1.lower().replace("_", " ").strip()
shipname2 = shipname2.lower().replace("_", " ").strip()
author = str(interaction.user.id)
try:
db.remove_fight(shipname1, shipname2, author)
await interaction.response.send_message(
f"Fight between {shipname1} and {shipname2} removed from the database"
)
except Exception as e:
await interaction.response.send_message(f"Error:{e}")
return
@tree.command(name="db_get_matchups", description="gets the matchups of a ship from the database")
async def db_get_matchups(interaction: discord.Interaction, shipname: str, playername: str = None):
"""gets the matchups of a ship from the database"""
shipname = shipname.lower().replace("_", " ").strip()
try:
await interaction.response.defer()
wins, draws, losses = db.get_matchups(shipname, playername)
text_wins = "Wins:\n"
for ship, matches in wins.items():
text_wins += f"- **{ship}** : {', '.join(matches)}\n"
text_draws = "Draws:\n"
for ship, matches in draws.items():
text_draws += f"- **{ship}** : {', '.join(matches)}\n"
text_losses = "Losses:\n"
for ship, matches in losses.items():
text_losses += f"- **{ship}** : {', '.join(matches)}\n"
text = f"Matchups for **{shipname}**\n" + text_wins + "\n" + text_draws + "\n" + text_losses
await send_long_message(interaction, text)
except Exception as e:
await interaction.followup.send(
f"Error:{str(traceback.format_exception_only(type(e), e)[0])}"
)
return
@tree.command(name="db_ship_meta_analysis", description="Analyses a ships place in the meta.")
async def db_ship_meta_analysis(interaction: discord.Interaction, shipname: str):
"""Analyses a ships place in the meta."""
shipname = shipname.lower().replace("_", " ").strip()
try:
await interaction.response.defer()
text = "Name: " + shipname + "\n"
text += "Description: " + db.get_ship_description(shipname) + "\n"
text += "Page rank: " + str(data_analysis.page_rank_ship(shipname)) + "\n"
await send_long_message(interaction, text)
except Exception as e:
await interaction.followup.send(
f"Error:{str(traceback.format_exception_only(type(e), e)[0])}"
)
return
@tree.command(name="db_simulate_fight", description="simulates a fight between two ships")
async def db_simulate_fight(interaction: discord.Interaction, shipname1: str, shipname2: str):
"""simulates a fight between two ships"""
shipname1 = shipname1.lower().replace("_", " ").strip()
shipname2 = shipname2.lower().replace("_", " ").strip()
try:
result = db.simulate_fight(shipname1, shipname2)
people_win = result.get(fight_db.FIGHT_RESULT.WIN)
if people_win is None:
people_win = []
people_draw = result.get(fight_db.FIGHT_RESULT.DRAW)
if people_draw is None:
people_draw = []
people_lose = result.get(fight_db.FIGHT_RESULT.LOSE)
if people_lose is None:
people_lose = []
text = (
f"In a fight between {shipname1} and {shipname2}, "
f"the results are:\n {len(people_win)} people think {shipname1} would win\n "
f"{len(people_draw)} people think it would be a draw\n "
f"{len(people_lose)} people think {shipname2} would win"
)
await interaction.response.send_message(text)
except Exception as e:
await interaction.response.send_message(f"Error:{e}")
return
@tree.command(name="db_list_ships", description='lists all ships in the database',)
async def db_list_ships(interaction: discord.Interaction, filter_keyword: str=None):
if filter_keyword:
filter_keyword.lower().replace('_', ' ').strip()
try:
await interaction.response.defer()
ships = db.get_ships()
# sort the ships
ships.sort()
text="Ships in the database:\n"
for ship in ships:
if filter_keyword:
if filter_keyword in ship:
text+=f"- {ship}\n"
else:
text+=f"- {ship}\n"
await send_long_message(interaction, text)
except Exception as e:
await interaction.response.send_message(f"Error:{e}")
return
@tree.command(name="db_draw_archetype_tree", description="Draws a the archetype tree")
async def db_draw_archetype_tree(interaction: discord.Interaction):
"""Draws a the archetype tree"""
try:
await interaction.response.defer()
text = data_analysis.visualize_tree()
await send_long_message(interaction, text)
except Exception as e:
await interaction.response.send_message(f"Error:{e}")
return
@tree.command(
name="db_get_unknown_matchups",
description="gets the unknown matchups of a ship from the database",
)
async def db_get_unknown_matchups(
interaction: discord.Interaction, shipname: str, player_name: str = None
):
"""gets the unknown matchups of a ship from the database"""
shipname = shipname.lower().replace("_", " ").strip()
try:
await interaction.response.defer()
ships = db.get_unknown_matchups(shipname, player_name)
text = "Unknown matchups for " + shipname + ":\n"
for ship in ships:
text += f"- {ship}\n"
await send_long_message(interaction, text)
except Exception as e:
await interaction.response.send_message(f"Error:{e}")
return
@tree.command(name="db_export_csv", description="exports the database to a csv file")
async def db_export_csv(interaction: discord.Interaction):
"""exports the database to a csv file"""
try:
# Create a file object for the CSV file
csv_file = backup_file(is_csv=True)
# Send the CSV file to the user
await interaction.response.send_message("Database exported to CSV file", file=csv_file)
except Exception as e:
await interaction.response.send_message(f"Error:{e}")
return
@tree.command(name="db_export_db", description="exports the database to a db file")
async def db_export_db(interaction: discord.Interaction):
"""exports the database to a db file"""
try:
# db.export_db("fight_database.db")
# Create a file object for the DB file
db_file = backup_file()
# Send the DB file to the user
await interaction.response.send_message("Database exported to DB file", file=db_file)
except Exception as e:
await interaction.response.send_message(f"Error:{e}")
return
@tree.command(name="db_rename_ship", description="renames a ship in the database")
async def db_rename_ship(
interaction: discord.Interaction,
old_name: str,
new_name: str = None,
new_parent_name: str = None,
new_description: str = None,
):
"""renames a ship in the database"""
old_name = old_name.lower().replace("_", " ").strip()
if new_name is not None:
new_name = new_name.lower().replace("_", " ").strip()
try:
backup = backup_file()
author = str(interaction.user.id)
if author not in {"457210821773361152", "450347288301273108"}:
raise ValueError("Only LunastroD or Plaus can rename ships!")
await interaction.response.send_message("Backup file created.", file=backup)
message = db.rename_ship(old_name, new_name, new_parent_name, new_description)
await interaction.followup.send(message)
except Exception as e:
if not interaction.response.is_done():
await interaction.response.send_message(f"Error: {e}")
else:
await interaction.followup.send(f"Error: {e}")
# refactored scoreboard, slpit into functions
@tree.command(name="db_scoreboard", description="shows the scoreboard of the database")
async def db_scoreboard(
interaction: discord.Interaction, player_name: str = None, sort_by: str = "win"
):
"""Shows the scoreboard of the database."""
try:
await interaction.response.defer()
ships = db.get_ships()
page_rank_dic = data_analysis.page_rank(data_analysis.get_fights_graph())
sort_list = ["win", "draw", "loss", "matches", "page rank"]
if sort_by not in sort_list:
raise ValueError("Can only sort by: " + str(sort_list))
scoreboard = calculate_scoreboard(ships, page_rank_dic, player_name)
sorted_ships = sort_ships(ships, scoreboard, sort_by, sort_list)
leading_message = f"Scoreboard ({sort_by.capitalize()})"
table = format_scoreboard(sorted_ships, scoreboard, leading_message)
await send_long_message(interaction, table, use_code_blocks=True)
except Exception as e:
await interaction.followup.send(f"Error: {e}")
return
def calculate_scoreboard(ships, page_rank_dic, player_name):
"""Calculates the scoreboard for each ship."""
scoreboard = {s: [0, 0, 0, 0, round(page_rank_dic[s], 4)] for s in ships}
for s in ships:
for s2 in ships:
wins, draws, losses = db.get_matchups(s, player_name)
players_win = len(wins.get(s2, []))
players_draw = len(draws.get(s2, []))
players_lose = len(losses.get(s2, []))
players_matches = players_win + players_draw + players_lose
if players_matches == 0:
continue
scoreboard[s][0] += players_win / players_matches
scoreboard[s][1] += players_draw / players_matches
scoreboard[s][2] += players_lose / players_matches
scoreboard[s][3] += 1
return scoreboard
def sort_ships(ships, scoreboard, sort_by, sort_list):
"""Sorts the ships based on the specified criterion."""
return sorted(ships, key=lambda x: scoreboard[x][sort_list.index(sort_by)], reverse=True)
def format_scoreboard(sorted_ships, scoreboard, leading_message):
"""Formats the scoreboard into a string table."""
return f"{leading_message.ljust(22)} |Win|Draw|Lost|Total|Page Rank\n" + "\n".join(
[
f"{ship.ljust(23)}|{str(round(scoreboard[ship][0], 1)).rjust(4)}|"
f"{str(round(scoreboard[ship][1], 1)).rjust(4)}|"
f"{str(round(scoreboard[ship][2], 1)).rjust(4)}|"
f"{str(scoreboard[ship][3]).rjust(3)}|"
f"{str(scoreboard[ship][4])}\n"
for ship in sorted_ships
]
)
def backup_file(is_csv=False):
"""
Backup the database to a file.
Args:
is_csv (bool, optional): Whether to export the database as a CSV file. Defaults to False.
Returns:
discord.File: A file object representing the backup file.
If `is_csv` is False, the file is a DB file.
Otherwise, the file is a CSV file.
"""
if not is_csv:
db.export_db("fight_database.db")
# Create a file object for the DB file
db_file = discord.File("test.db", filename="fight_database.db")
return db_file
db.export_csv("fight_database.csv")
# Create a file object for the CSV file
return discord.File("fight_database.csv", filename="fight_database.csv")
client.run(os.getenv("TOKEN"))