This repository has been archived by the owner on Oct 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EU4Bot.py
1638 lines (1528 loc) · 80.3 KB
/
EU4Bot.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
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import asyncio
import os
from abc import ABC, abstractmethod
from io import StringIO
from random import shuffle
from typing import Any, Dict, List, Optional, Tuple, Union
import aiohttp
import EU4cpplib
import discord
from discord.errors import DiscordException
from dotenv import load_dotenv
from PIL import Image, ImageDraw, ImageFont, ImageOps
import EU4Lib
import EU4Reserve
import discordLib as dLib
import baseLib
import GuildManager
import Skanderbeg
# Load Discord Client
load_dotenv()
token: str = os.getenv("DISCORD_TOKEN")
intents: discord.Intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)
dLib.client = client
session: aiohttp.ClientSession = None
# Load Skanderbeg key if it exists
SKANDERBEGKEY = os.getenv("SKANDERBEG_KEY")
if SKANDERBEGKEY == "" or SKANDERBEGKEY.isspace():
SKANDERBEGKEY = None
async def checkResAdmin(server: Union[str, int, discord.Guild], user: Union[str, int, dLib.DiscUser]) -> bool:
"""
Returns whether or not a user has bot admin control roles on a server.
"""
# Get server object
server = await dLib.findGuild(server)
# Get member object
user = await dLib.findMember(user, server)
# OK now check
try:
role = server.get_role(GuildManager.getAdmin(server))
except TypeError:
role = None
return (role is not None and role <= user.top_role) or user.top_role.id == server.roles[-1].id or user._user.id == 249680375280959489
class AbstractChannel(ABC):
@abstractmethod
def __init__(self, user: dLib.DiscUser, initChannel: dLib.DiscTextChannels):
self.user = user
self.interactChannel = initChannel
self.displayChannel = initChannel
@abstractmethod
async def responsive(self, message: discord.Message) -> bool:
pass
@abstractmethod
async def process(self, message: discord.Message):
pass
@abstractmethod
async def msgdel(self, msgID: Union[str, int]):
pass
@abstractmethod
async def userdel(self, user: dLib.DiscUser):
pass
class ReserveChannel(AbstractChannel):
def __init__(self, user: dLib.DiscUser, initChannel: dLib.DiscTextChannels, textID: Optional[int] = None, imgID: Optional[int] = None):
self.user = None
self.interactChannel = initChannel
self.displayChannel = initChannel
self.reserve: EU4Reserve.Reserve = EU4Reserve.Reserve(initChannel.id)
self.textID = textID
self.imgID = imgID
async def responsive(self, message: discord.Message) -> bool:
return message.channel == self.interactChannel
@property
def textID(self) -> Optional[int]:
if hasattr(self, "_textID"):
return self._textID
return self.reserve.textID
@property
def imgID(self) -> Optional[int]:
if hasattr(self, "_imgID"):
return self._imgID
return self.reserve.imgID
@textID.setter
def textID(self, id: int):
self._textID = id
self.reserve.textID = id
@imgID.setter
def imgID(self, id: int):
self._imgID = id
self.reserve.imgID = id
async def process(self, message: discord.Message):
text: str = message.content.strip()
if text.upper() == "HELP": # HELP
stringHelp = f"__**Command help for {message.channel.mention}:**__"
stringHelp += "\n**HELP**\nGets you this information!"
stringHelp += "\n**RESERVE [nation]**\nReserves a nation or overwrites your previous reservation. Don't include the brackets."
stringHelp += "\n**DELRESERVE**\nCancels your reservation."
# Here we send info about commands only for admins
if await checkResAdmin(message.guild, message.author):
stringHelp += "\n**END**\nStops allowing reservations and stops the bot's channel management.\nThis should be done by the time the game starts."
stringHelp += "\n**ADMRES [nation] [@user]**\nReserves a nation on behalf of a player on the server.\nMake sure to actually @ the player. This ignores the ban list."
stringHelp += "\n**ADMDELRES [@user]**\nDeletes a player's reservation.\nMake sure to actually @ the player."
stringHelp += "\n**UPDATE**\nUpdates the reservations list. Should usually not be necessary unless in debug or something went wrong."
stringHelp += "\n**ADDBAN [nation], [nation], ... **\nAdds countries to the ban list. Add commas between each entry if there are more than one."
stringHelp += "\n**DELBAN [nation], [nation], ... **\nRemoves countries from the ban list. Add commas between each entry if there are more than one."
await message.delete()
await dLib.sendDM(message.author, stringHelp)
elif text.upper() == "UPDATE" and await checkResAdmin(message.guild, message.author): # UPDATE
await message.delete()
await self.updateText()
await self.updateImg()
elif text.upper() == "END" and await checkResAdmin(message.guild, message.author): # END
await message.delete()
# Load the reserve
string = "**Final players list:**"
# Text String - Players
if self.reserve.countPlayers() == 0:
string += "\n*It's so empty here...*"
else:
players = self.reserve.getPlayers()
players.sort(key=lambda x: x.time)
for x in players:
string += f"\n{x.userID}: {EU4Lib.tagToName(x.tag)} | {x.timeStr()}"
# Update the message or send a new one if nonexistant
try:
await (await self.displayChannel.fetch_message(self.textID())).edit(content=string)
except:
pass
await self.displayChannel.send("*Reservations are now ended. Good Luck.*")
self.reserve.delete()
controlledChannels.remove(self)
del(self)
# RESERVE [nation]
elif text.upper().startswith("RESERVE "):
res = text[text.index(" ")+1:].strip()
tag = EU4Lib.country(res)
if tag is not None:
if self.reserve.isBan(tag):
await dLib.sendDM(message.author, f"You may not reserve {EU4Lib.tagToName(tag)} in {self.displayChannel.mention} because it is banned. If you still want to play it, please have an admin override.")
else:
await self.add(EU4Reserve.reservePick(message.author.id, tag))
else:
await dLib.sendDM(message.author, f"Your country reservation in {self.displayChannel.mention} was not recorded, as \"{res}\" was not recognized.")
await message.delete()
# ADMRES [nation] @[player]
elif text.upper().startswith("ADMRES") and await checkResAdmin(message.guild, message.author):
if len(message.mentions) == 1:
res = text[text.index(" ")+1:].strip("\n\t <@!1234567890>")
tag = EU4Lib.country(res)
if tag is not None:
await self.add(EU4Reserve.reservePick(message.mentions[0].id, tag.upper()))
else:
await dLib.sendDM(message.author, f"Your reservation for {message.mentions[0].mention} in {self.displayChannel.mention} was not recorded, as \"{res}\" was not recognized.")
else:
await dLib.sendDM(message.author, f"Your reservation in {self.displayChannel.mention} needs to @ a player.")
await message.delete()
# DELRESERVE
elif text.upper() == "DELRESERVE" or text.upper() == "DELETERESERVE":
await self.removePlayer(message.author.id)
await message.delete()
# ADMDELRES @[player]
elif text.upper().startswith("ADMDELRES") and await checkResAdmin(message.guild, message.author):
if len(message.mentions) == 1:
await self.removePlayer(message.mentions[0].id)
else:
await dLib.sendDM(message.author, f"Your deletion of a reservation in {self.displayChannel.mention} needs to @ a player.")
await message.delete()
# ADDBAN [nation], [nation], ...
elif text.upper().startswith("ADDBAN") and await checkResAdmin(message.guild, message.author):
# This is implemented by having lists of recognized and unrecognized bans, doing the recognized ones, and informing about the result.
bannations = text[text.index(" ")+1:].strip("\n\t ,").split(",")
bantags: List[str] = []
fails: List[str] = []
for bannat in bannations:
tag = EU4Lib.country(bannat.strip("\n\t ,"))
if tag is not None:
bantags.append(tag)
else:
fails.append(bannat)
string = ""
if len(bantags) > 0:
for tag in bantags:
self.reserve.addBan(tag)
string += f"Added the following nations to the ban list in {self.displayChannel.mention}: "
for tag in bantags:
string += EU4Lib.tagToName(tag) + \
("" if tag is bantags[-1] else ",")
if len(fails) > 0:
string += "\nDid not recognize the following nations: "
for tag in fails:
string += EU4Lib.tagToName(tag) + \
("" if tag is fails[-1] else ", ")
string += "\n The unrecognized nations were not added to the ban list."
if string != "":
await dLib.sendDM(message.author, string)
await message.delete()
await self.updateText()
# DELBAN [nation], [nation], ...
elif text.upper().startswith("DELBAN") and await checkResAdmin(message.guild, message.author):
# This is implemented by having lists of recognized and unrecognized bans, doing the recognized ones, and informing about the result.
bannations = text[text.index(" ")+1:].strip("\n\t ,").split(",")
bantags: List[str] = []
fails: List[str] = []
for bannat in bannations:
tag = EU4Lib.country(bannat.strip("\n\t ,"))
if tag is not None:
bantags.append(tag)
else:
fails.append(bannat)
string = ""
if len(bantags) > 0:
for tag in bantags:
self.reserve.delBan(tag)
string += f"Removed the following nations from the ban list in {self.displayChannel.mention}: "
for tag in bantags:
string += EU4Lib.tagToName(tag) + \
("" if tag is bantags[-1] else ",")
if len(fails) > 0:
string += "\nDid not recognize the following nations: "
for tag in fails:
string += EU4Lib.tagToName(tag) + \
("" if tag is fails[-1] else ", ")
string += "\n The unrecognized nations were not removed from the ban list."
if string != "":
await dLib.sendDM(message.author, string)
await message.delete()
await self.updateText()
else:
await message.delete()
async def updateText(self):
# Text String - Header
string = f"How to reserve: `reserve [nation]`\nTo unreserve: `delreserve`\n"
# Text String - Banned Nations
string += "Banned nations: "
banlist = self.reserve.allBans()
if len(banlist) == 0:
string += "*none or unspecified*"
for tag in banlist:
name = EU4Lib.tagToName(tag)
string += (tag if name is None else name) + \
("" if tag is banlist[-1] else ", ")
playerCount = self.reserve.countPlayers()
string += f"\n**Current players list: ** *({playerCount} players)*"
# Text String - Players
if playerCount == 0:
string += "\n*It's so empty here...*"
else:
players = self.reserve.getPlayers()
players.sort(key=lambda x: x.time)
for x in players:
string += f"\n<@{x.userID}>: {EU4Lib.tagToName(x.tag)} | {x.timeStr()}"
# Update the message or send a new one if nonexistant
try:
await (await self.displayChannel.fetch_message(self.textID)).edit(content=string)
except (discord.NotFound, discord.HTTPException):
self.textID = (await self.displayChannel.send(content=string)).id
async def updateImg(self):
# Try to delete the previous image to trigger the upload of a new one.
try:
await (await self.interactChannel.fetch_message(self.imgID)).delete()
except (discord.NotFound, discord.HTTPException):
# Normally when deleted it'll create the new one in the delete event, but in case there's an issue this will fix it.
# This issue is usually that the image message doesn't exist or has already been deleted.
self.imgID = (await self.displayChannel.send(file=dLib.imageToFile(EU4Reserve.createMap(self.reserve)))).id
async def add(self, nation: EU4Reserve.reservePick) -> int:
"""
Adds a reservation for a player.
Codes:
0 = Failed; Reserve not found
1 = Success; New Reservation
2 = Success; Replaced old reservation
3 = Failed; Nation taken by other
4 = Nothing happens; Nation taken by self
"""
addInt = self.reserve.add(nation)
# Notifications based off the code returned by the backend.
if addInt == 1 or addInt == 2: # Success!
await self.updateText()
await self.updateImg()
elif addInt == 0: # This is not a reserve channel. How did this happen?
await dLib.sendDM(client.get_user(int(nation.userID.strip("\n\t <!@>"))), f"You can't reserve nations in {self.displayChannel.mention}.")
elif addInt == 3: # This nation is already taken
await dLib.sendDM(client.get_user(int(nation.userID.strip("\n\t <!@>"))), f"The nation {EU4Lib.tagToName(nation.tag)} is already reserved in {self.displayChannel.mention}.")
return addInt
async def remove(self, tag: str):
pass
async def removePlayer(self, id: str):
"""
Deletes a player's reservation.
"""
# If it did anything
if self.reserve.removePlayer(id):
await self.updateText()
await self.updateImg()
async def msgdel(self, msgID: Union[str, int]):
"""
Method called whenever a message is deleted.
"""
if msgID == self.textID:
self.textID = None
await self.updateText()
# This will call msgdel again to update the image
await (await self.interactChannel.fetch_message(self.imgID)).delete()
elif msgID == self.imgID:
self.imgID = (await self.displayChannel.send(file=dLib.imageToFile(EU4Reserve.createMap(self.reserve)))).id
async def userdel(self, user: dLib.DiscUser):
"""
Method called whenever a user leaves the guild.
"""
if (hasattr(self.displayChannel, "guild") and self.displayChannel.guild == user.guild) or (hasattr(self.interactChannel, "guild") and self.interactChannel.guild == user.guild):
await self.removePlayer(user.id)
class Nation:
def __init__(self, tag: str):
self.tag: str = tag.upper()
self.othertags: List[str] = []
self.development: int = 0
self.prestige: int = None
self.stability: int = None
# self.manpower = None
# self.maxManpower = None
self.army: float = 0.0
self.navy: int = 0
self.debt: int = 0
self.treasury: float = 0.0
self.totalIncome: float = 0.0
self.totalExpense: float = 0.0
self.scorePlace = None
self.capitalID: int = 0
self.overlord: Optional[str] = None
self.allies: List[str] = []
self.subjects: List[str] = []
# self.revColors: color = None
self.mapColor: Tuple[int, int, int] = None
self.natColor: Tuple[int, int, int] = None
def fullDataStr(self) -> str:
"""
Returns a brief multi-line human-readable text representation of the data contained in this Nation object.
"""
string = f"Tag: {self.tag} Previous Tags: {self.othertags}\n"
string += f"Dev: {self.development} Prestige: {self.prestige} Stability: {self.stability}\n"
string += f"Army: {self.army} Navy: {self.navy}\n"
string += f"Treasury: {self.treasury} Debt: {self.debt}\n"
string += f"Income: {self.totalIncome} Expenses: {self.totalExpense}\n"
string += f"Capital: {self.capitalID}\n"
string += f"Allies: {self.allies}\n"
string += (f"Overlord: {self.overlord}" if self.overlord is not None else "") + (
f"Subjects: {self.subjects}" if len(self.subjects) != 0 else "")
return string
class war():
WHITEPEACE: int = 1
ATTACKERWIN: int = 2
DEFENDERWIN: int = 3
def __init__(self, name: str):
self.name = name
self.attackers: List[str] = []
self.defenders: List[str] = []
self.attackerLosses: int = 0
self.defenderLosses: int = 0
self.startDate: EU4cpplib.EU4Date = None
self.endDate: EU4cpplib.eu4Date = None
self.result: int = 0
def isPlayerWar(self, playertags: List[str]) -> bool:
"""
Returns True if there are players on both sides of a war.
"""
attacker = False
defender = False
for tag in self.attackers:
if tag in playertags:
attacker = True
break
for tag in self.defenders:
if tag in playertags:
defender = True
break
return attacker and defender
def playerAttackers(self, playertags: List[str]) -> List[str]:
"""
Returns a list of tags for nations on the attacking side played by players.
"""
return list(filter(lambda x: x in self.attackers, playertags))
def playerDefenders(self, playertags: List[str]) -> List[str]:
"""
Returns a list of tags for nations on the defending side played by players.
"""
return list(filter(lambda x: x in self.defenders, playertags))
def warScale(self, playertags: List[str] = []) -> int:
"""
Calculates a score for how important the war is for deciding which to display. May be somewhat arbitrary or subjective.
"""
if playertags is None or playertags == []: # Ignore player involvement
# Base off casualties
return self.attackerLosses + self.defenderLosses
else: # Include player involvement
# Scale by number of players
return (self.attackerLosses + self.defenderLosses) * max(min(len(self.playerAttackers(playertags)) * 0.7, len(self.playerDefenders(playertags)) * 0.7), 1)
class saveGame():
def __init__(self):
self.allNations: Dict[str, Nation] = {}
self.playertags: Dict[str, str] = {}
self.provinces: Dict[int, str] = {}
self.dlc: List[str] = []
self.GP: List[str] = []
self.date: Optional[EU4cpplib.EU4Date] = None
self.mp: bool = True
self.age: Optional[str] = None
self.HRE: str = None
self.china: str = None
self.crusade: str = None
self.playerWars: List[war] = []
self.mod: str = "vanilla"
def allPlayerTags(self) -> List[str]:
if hasattr(self, "allplayertags"):
return self.allplayertags
else:
# Player tags plus all previous tags those countries have had
self.allplayertags: List[str] = []
for tag in self.playertags:
if tag not in self.allplayertags:
self.allplayertags.append(tag)
for prevtag in self.allNations[tag].othertags:
if prevtag not in self.allplayertags:
self.allplayertags.append(prevtag)
return self.allplayertags
class statsChannel(AbstractChannel):
def __init__(self, user: dLib.DiscUser, initChannel: dLib.DiscTextChannels):
self.user = user
self.interactChannel: dLib.DiscTextChannels = None
self.displayChannel: dLib.DiscTextChannels = initChannel
self.hasReadFile = False
self.game = saveGame()
self.modMsg: discord.Message = None
self.doneMod = False
self.skanderbeg = True
self.skanderbegURL: Optional[asyncio.Task] = None
async def asyncInit(self):
"""
Does all the init stuff that needs to happen async. Returns self.
"""
if self.user.dm_channel is None:
await self.user.create_dm()
self.interactChannel = self.user.dm_channel
await self.interactChannel.send("**Send EITHER an uncompressed .eu4 save file\nor a direct link to an uncompressed .eu4 save file:**\nYou can do this by uploading to https://www.file.io/\nthen clicking **Copy Link** and entering it here:")
return self
def modPromptStr(self) -> str:
"""
Makes and returns a string giving information and instructions for player list modification.
"""
prompt = "**Current players list:**```"
for tag in self.game.playertags:
natName = EU4Lib.tagToName(tag, self.game.mod)
prompt += f"\n{tag if natName is None else natName}: {self.game.playertags[tag]}"
prompt += "```\n**Do you want to make any changes?\nType `done` to finish. Commands:\n`remove [nation]`\n`add [player], [nation]`**"
return prompt
async def readFile(self, file: iter):
"""
Gets all data from file and saves it to the self.game.
"""
brackets: List[str] = []
currentReadWar: war = None
currentReadWarParticTag: str = None
currentWarLastLeave: EU4cpplib.EU4Date = None
lastPlayerInList: str = None
# Reading save file...
linenum = 0
for line in file:
linenum += 1
if "{" in line:
if line.count("}") == 0 and line.count("{") == 1:
brackets.append(line.strip("\t ={\n"))
elif line.count("{") == line.count("}"):
continue
elif line.count("}") == 0 and line.count("{") > 1:
for x in range(line.count("{")):
brackets.append("{") # TODO: fix this so it has more
else: # Unexpected Brackets
pass
elif "}" in line:
try:
brackets.pop()
except IndexError: # No brackets to close
pass
else:
if "=" in line and not ('"' in line and line.index('"') < line.index("=")):
linekey, lineval = line.split("=", maxsplit=1)
lineval = lineval.strip()
linekey = linekey.strip()
else:
# This probably means it's the interior of a list or something like that.
linekey = ""
lineval = line.strip()
# This is where we do stuff
if len(brackets) == 0:
# Get current gamedate
if linekey == "date":
self.game.date = EU4cpplib.EU4Date(lineval)
# Get save DLC (not sure if we use this...)
elif brackets == ["dlc_enabled"]:
self.game.dlc.append(lineval.strip('"'))
# Check if game is mp
elif linekey == "multi_player":
if "yes" == lineval:
self.game.mp = True
else:
self.game.mp = False
# Get current age
elif linekey == "current_age" and brackets == []:
self.game.age = lineval.strip('"')
# Get player names and country tags
elif brackets == ["players_countries"]:
# In the file, the format is like this:
# players_countries={
# "playername"
# "SWE"
#
# Where " " is a tab \t
# Prepare to assign the player by recording it for the next line
if lastPlayerInList is None:
lastPlayerInList = lineval.strip('"')
# Add to playertags based on what was saved in the previous line as the player name
else:
self.game.playertags[lineval.strip(
'"')] = lastPlayerInList
lastPlayerInList = None
elif len(brackets) == 2 and brackets[0] == "mods_enabled_names" and linekey == "name":
if lineval == '"Anbennar: A Fantasy Total Conversion Mod"':
self.game.mod = "anbennar"
# Get top 8
elif linekey == "country" and brackets == ["great_powers", "original"]:
if len(self.game.GP) < 8: # Make sure to not include leaving GPs
self.game.GP.append(lineval.strip('"'))
# Get HRE emperor tag
elif linekey == "emperor" and brackets == ["empire"]:
self.game.HRE = lineval.strip('"')
# Get Celestial emperor tag
elif linekey == "emperor" and brackets == ["celestial_empire"]:
self.game.china = lineval.strip('"')
# Get target of crusade ('---' if none)
elif linekey == "crusade_target" and brackets == ["religion_instance_data", "catholic", "papacy"]:
self.game.crusade = lineval.strip('"')
# Get papal controller
elif linekey == "previous_controller" and brackets == ["religion_instance_data", "catholic", "papacy"]:
continue
elif len(brackets) > 1 and brackets[0] == "provinces":
provinceID = int(brackets[1][1:])
if len(brackets) == 2:
if linekey == "owner":
self.game.provinces[provinceID] = line[line.index(
'"')+1:line.rindex('"')]
# Country-specific data (for players)
elif len(brackets) > 1 and brackets[0] == "countries":
try:
bracketNation = self.game.allNations[brackets[1]]
except:
# This should be the government_rank= line where we create the Nation object.
pass
if len(brackets) == 2:
if linekey == "government_rank":
# This one is assignment so should NOT be referring to bracketNation
self.game.allNations[brackets[1]] = Nation(
brackets[1])
elif linekey == "previous_country_tags":
bracketNation.othertags.append(
line[line.index('"')+1:line.rindex('"')])
elif linekey == "raw_development":
bracketNation.development = round(
float(lineval))
elif linekey == "capital":
bracketNation.capitalID = int(lineval)
elif linekey == "score_place":
bracketNation.scorePlace = round(
float(lineval))
elif linekey == "prestige":
bracketNation.prestige = round(float(lineval))
elif linekey == "stability":
bracketNation.stability = round(float(lineval))
elif linekey == "treasury":
bracketNation.treasury = round(float(lineval))
# elif linekey == "manpower":
# bracketNation.manpower = round(float(lineval))
# elif linekey == "max_manpower":
# bracketNation.maxManpower = round(float(lineval))
elif linekey == "overlord":
bracketNation.overlord = lineval.strip('"')
elif len(brackets) == 3:
# Get each loan and add its amount to debt
if brackets[2] == "loan" and linekey == "amount":
bracketNation.debt += round(float(lineval))
# Get Income from the previous month
elif brackets[2] == "ledger" and linekey == "lastmonthincome":
bracketNation.totalIncome = round(
float(lineval), 2)
# Get Expense from the previous month
elif brackets[2] == "ledger" and linekey == "lastmonthexpense":
bracketNation.totalExpense = round(
float(lineval), 2)
elif brackets[2] == "subjects":
for subject in line.split():
bracketNation.subjects.append(subject)
elif brackets[2] == "allies":
for ally in line.split():
bracketNation.allies.append(ally)
elif len(brackets) == 4:
# Add 1 to army size for each regiment
if brackets[2:] == ["army", "regiment"] and linekey == "morale":
bracketNation.army += 1000
# Subtract damage done to units from army size
elif brackets[2:] == ["army", "regiment"] and linekey == "strength":
try:
bracketNation.army = round(
bracketNation.army - 1000 + 1000 * float(lineval))
except ValueError:
# Full unit
continue
# Add 1 for each ship
elif brackets[2:] == ["navy", "ship"] and linekey == "home":
bracketNation.navy += 1
elif brackets[2:] == ["colors", "map_color"]:
bracketNation.mapColor = tuple(
map(lambda x: int(x), line.split()))
elif brackets[2:] == ["colors", "country_color"]:
bracketNation.natColor = tuple(
map(lambda x: int(x), line.split()))
# Read wars
elif len(brackets) > 0 and brackets[0] == "previous_war":
if len(brackets) == 1 and linekey == "name":
if currentReadWar is not None and currentReadWar.isPlayerWar(self.game.playertags):
currentReadWar.endDate = currentWarLastLeave
self.game.playerWars.append(currentReadWar)
currentReadWar = war(lineval.strip('"'))
elif len(brackets) == 3 and brackets[1] == "history":
if linekey == "add_attacker":
currentReadWar.attackers.append(
lineval.strip('"'))
if currentReadWar.startDate is None:
currentReadWar.startDate = EU4cpplib.EU4Date(
brackets[2])
elif linekey == "add_defender":
currentReadWar.defenders.append(
lineval.strip('"'))
elif linekey == "rem_attacker" or linekey == "rem_defender":
currentWarLastLeave = EU4cpplib.EU4Date(
brackets[2])
elif len(brackets) >= 2 and brackets[1] == "participants":
if len(brackets) == 2 and linekey == "tag":
currentReadWarParticTag = lineval.strip('"')
elif len(brackets) == 4 and brackets[2:] == ["losses", "members"]:
if currentReadWarParticTag in currentReadWar.attackers:
for x in line.split():
currentReadWar.attackerLosses += int(x)
elif currentReadWarParticTag in currentReadWar.defenders:
for x in line.split():
currentReadWar.defenderLosses += int(x)
else:
print(
"Something went wrong with the attacker/defender list.")
elif len(brackets) == 1 and linekey == "outcome":
currentReadWar.result = int(lineval)
if currentReadWar.isPlayerWar(self.game.playertags):
currentReadWar.endDate = currentWarLastLeave
self.game.playerWars.append(currentReadWar)
currentReadWar = None
# Finalize data
# These signify that it's probably not a valid save file.
if self.game.GP == [] or self.game.date is None or self.game.age is None:
raise ValueError(
f"This probably isn't a valid .eu4 uncompressed save file from {self.user.mention}")
for nat in self.game.allNations.copy().keys():
if self.game.allNations[nat].development == 0:
try:
del(self.game.allNations[nat])
except:
pass
try:
del(self.game.playertags[nat])
except:
pass
# Sort Data:
self.game.playerWars.sort(key=lambda nat: nat.warScale(
self.game.playertags), reverse=True)
async def generateImage(self, sendUpdates=True) -> Image.Image:
"""
Returns a stats Image based off the self.game data.
If sendUpdates is True, a message will be sent and edited in the interactChannel for this control channel.
"""
progressMessage: discord.Message = None
if sendUpdates and self.interactChannel is not None:
progressMessage = await self.interactChannel.send("**Generating Image...**")
async def updateProgress(text: str, num: int, maxnum: int):
if progressMessage is not None:
await progressMessage.edit(content=f"**Generating Image...**\n{text} ({num}/{maxnum})")
await updateProgress("Finding colors...", 1, 9)
# Formatting: (map color) = (player contrast color)
playerColors: Dict[Tuple[int, int, int], Tuple[int, int, int]] = {}
for natTag in self.game.allNations:
try:
nat: Nation = self.game.allNations[natTag]
playerNatTag: str = None # This is what nation actually is said to own the land
# Check if this nation is a player
if natTag in self.game.playertags:
playerNatTag = natTag
# Check if any overlord(s) are players and if so, go with the highest one.
while nat.overlord is not None:
if nat.overlord in self.game.playertags:
playerNatTag = nat.overlord
nat = self.game.allNations[nat.overlord]
if playerNatTag is not None:
playerColors[self.game.allNations[natTag].mapColor] = baseLib.invertColor(
self.game.allNations[playerNatTag].mapColor)
except:
pass
# All tags' colors
tagColors: Dict[str, Tuple[int, int, int]] = {}
for natTag in self.game.allNations:
tagColors[natTag] = self.game.allNations[natTag].mapColor
# Modify the image
await updateProgress("Drawing map...", 2, 9)
img: Image.Image = Image.frombytes(
"RGB", (5632, 2048), EU4cpplib.drawMap(tagColors, self.game.provinces, self.game.mod))
img = ImageOps.flip(img)
await updateProgress("Calculating player borders...", 3, 9)
# Formatting: (draw color) = [(x, y), (x, y), ...]
drawColors: Dict[Tuple[int, int, int], List[Tuple[int, int]]] = EU4cpplib.drawBorders(
playerColors, img.tobytes(), img.width, img.height)
try:
del(drawColors[(0, 0, 0)])
except:
pass
await updateProgress("Drawing player borders...", 4, 9)
mapDraw = ImageDraw.Draw(img)
for drawColor in drawColors:
mapDraw.point(drawColors[drawColor], drawColor)
del(drawColors)
del(playerColors)
# Start Final Img Creation
# Copy map into bottom of final image
await updateProgress("Finalizing map section...", 5, 9)
imgFinal: Image.Image = Image.open("resources/finalTemplate.png")
imgFinal.paste(img, (0, imgFinal.size[1]-img.size[1]))
del(img)
# The top has 5632x1119
# Getting fonts
fontmini = ImageFont.truetype("resources/GARA.TTF", 36)
fontsmall = ImageFont.truetype("resources/GARA.TTF", 50)
font = ImageFont.truetype("resources/GARA.TTF", 100)
imgDraw = ImageDraw.Draw(imgFinal)
#================MULTIPLAYER================#
if True: # mp == True:
# Players section from (20,30) to (4710, 1100) half way is x=2345
# So start with yborder = 38, yheight = 128 for each player row. x just make it half or maybe thirds depending on how it goes
# Get the list of player Nations
# TODO: make this more integrated with the new savefile data system rather than doing this conversion
await updateProgress("Drawing player list...", 6, 9)
playerNationList: List[Nation] = []
for x in self.game.playertags:
playerNationList.append(self.game.allNations[x])
playerNationList.sort(key=lambda x: x.development, reverse=True)
# Put stuff on the board
for nat in playerNationList:
natnum = playerNationList.index(nat)
# We have 2335 pixels to work with maximum for each player column
x = 38 + 2335*int(natnum/8)
y = 38 + 128*(natnum % 8)
if (natnum < 16):
# x: Country flag
flag: Image.Image = None
if (nat.tag.startswith("C") and nat.tag[1:].isdigit() and nat.overlord is not None):
# This is a colonial nation, so make that flag instead.
flag = EU4Lib.colonialFlag(nat.overlord, EU4Lib.colonialRegion(
nat.capitalID, self.game.mod), self.game.mod)
else:
flag = EU4Lib.flag(nat.tag, self.game.mod)
imgFinal.paste(flag, (x, y))
# x+128: Player
playerName = self.game.playertags[nat.tag]
while (imgDraw.textsize(playerName, font)[0] > 760 - 128):
# Make sure the name isn't too long
playerName = playerName[:-1]
imgDraw.text((x+128, y), playerName, (255, 255, 255), font)
# x+760: Army size
imgFinal.paste(Image.open(
"resources/army.png"), (x+760, y))
imgDraw.text(
(x+760+128, y), baseLib.armyDisplay(nat.army), (255, 255, 255), font)
# x+1100: Navy size
imgFinal.paste(Image.open(
"resources/navy.png"), (x+1100, y))
imgDraw.text((x+1100+128, y), str(nat.navy),
(255, 255, 255), font)
# x+1440: Development
imgFinal.paste(Image.open(
"resources/development.png"), (x+1440, y))
imgDraw.text((x+1440+128, y),
str(nat.development), (255, 255, 255), font)
# x+1780: Income/Expense
monthlyProfit = nat.totalIncome-nat.totalExpense
imgIncome = Image.open("resources/income.png")
if monthlyProfit < 0:
imgIncome = imgIncome.crop((128, 0, 255, 127))
imgFinal.paste(imgIncome, (x+1780, y))
imgDraw.text(
(x+1780+128, y), str(round(nat.totalIncome - nat.totalExpense)), (247, 16, 16), font)
else:
imgIncome = imgIncome.crop((0, 0, 127, 127))
imgFinal.paste(imgIncome, (x+1780, y))
imgDraw.text(
(x+1780+128, y), str(round(nat.totalIncome - nat.totalExpense)), (49, 190, 66), font)
imgDraw.text(
(x+2130, y), "+" + str(round(nat.totalIncome, 2)), (49, 190, 66), fontsmall)
imgDraw.text(
(x+2130, y+64), "-" + str(round(nat.totalExpense, 2)), (247, 16, 16), fontsmall)
# Possible TODO:
# navy_strength
# manpower
# max_manpower
# max_sailors
else:
pass
await updateProgress("Drawing player wars...", 7, 9)
for playerWar in self.game.playerWars:
warnum = self.game.playerWars.index(playerWar)
if warnum < 4:
x = 4742
y = 230 + 218 * warnum
# Draw Attacker Flags
for nat in playerWar.playerAttackers(self.game.allPlayerTags()):
natnum = playerWar.playerAttackers(
self.game.allPlayerTags()).index(nat)
if natnum < 8:
flag: Image.Image = None
if (nat.startswith("C") and nat[1:].isdigit() and self.game.allNations[nat].overlord is not None):
# This is a colonial nation, so make that flag instead.
try:
flag = EU4Lib.colonialFlag(self.game.allNations[nat].overlord, EU4Lib.colonialRegion(
self.game.allNations[nat].capitalID, self.game.mod), self.game.mod)
except:
raise RuntimeWarning(
f"Something went wrong in creating a colonial flag. Details:\n{nat.fullDataStr()}")
else:
flag = EU4Lib.flag(nat, self.game.mod)
imgFinal.paste(flag.resize((64, 64)), (round(x + 3 * (12 + 64) - (
natnum % 4) * (64 + 12)), round(y + (natnum - natnum % 4) / 4 * (64 + 12) + 12)))
# Draw Attacker Casualties
attackerIcon = Image.open(
"resources/bodycount_attacker_button.png")
imgFinal.paste(
attackerIcon, (x + 290 - 12 - 32, y + 156), attackerIcon)
lossesStr = f"Losses: {baseLib.armyDisplay(playerWar.attackerLosses)}"
imgDraw.text((x + 290 - 12 - 32 - imgDraw.textsize(lossesStr,
fontmini)[0], y + 152), lossesStr, (255, 255, 255), fontmini)
# Draw Defender Flags
for nat in playerWar.playerDefenders(self.game.allPlayerTags()):
natnum = playerWar.playerDefenders(
self.game.allPlayerTags()).index(nat)
if natnum < 8:
flag: Image.Image = None
if (nat.startswith("C") and nat[1:].isdigit() and self.game.allNations[nat].overlord is not None):
# This is a colonial nation, so make that flag instead.
try:
flag = EU4Lib.colonialFlag(self.game.allNations[nat].overlord, EU4Lib.colonialRegion(
self.game.allNations[nat].capitalID, self.game.mod), self.game.mod)
except:
raise RuntimeWarning(
f"Something went wrong in creating a colonial flag. Details:\n{nat.fullDataStr()}")
else:
flag = EU4Lib.flag(nat, self.game.mod)
imgFinal.paste(flag.resize((64, 64)), (round(
x + (natnum % 4) * (64 + 12) + 585), round(y + (natnum - natnum % 4) / 4 * (64 + 12) + 12)))
# Draw Defender Casualties
defenderIcon = Image.open(
"resources/bodycount_defender_button.png")
imgFinal.paste(
defenderIcon, (x + 12 + 585, y + 156), defenderIcon)
imgDraw.text((x + 12 + 32 + 585, y + 152),
f"Losses: {baseLib.armyDisplay(playerWar.defenderLosses)}", (255, 255, 255), fontmini)
# Draw war details
remainingWords = playerWar.name.split()
lineLimit = 290 # pix/ln
nameStr = ""
for word in remainingWords:
if nameStr == "" or nameStr.endswith("\n"):
if imgDraw.textsize(word, fontmini)[0] >= lineLimit:
nameStr += f"{word}\n"
else:
nameStr += word
else:
if imgDraw.textsize(word, fontmini)[0] >= lineLimit:
nameStr += f"\n{word}\n"
elif imgDraw.textsize(nameStr.split('\n')[-1] + word, fontmini)[0] >= lineLimit:
nameStr += f"\n{word}"
else:
nameStr += f" {word}"
imgDraw.text((round(x + 437.5 - imgDraw.textsize(nameStr, fontmini)
[0]/2), y + 12), nameStr, (255, 255, 255), fontmini, align="center")
dateStr = str(playerWar.startDate.year) + \
"-" + str(playerWar.endDate.year)
imgDraw.text((round(x + 437.5 - imgDraw.textsize(dateStr, fontmini)
[0]/2), y + 115), dateStr, (255, 255, 255), fontmini, align="center")
# Draw result
if playerWar.result == war.WHITEPEACE:
WPIcon = Image.open("resources/icon_peace.png")
imgFinal.paste(WPIcon, (x + 437 - 32, y + 140), WPIcon)
elif playerWar.result == war.ATTACKERWIN:
WinnerIcon = Image.open("resources/star.png")
imgFinal.paste(
WinnerIcon, (x + 290, y + 148), WinnerIcon)
elif playerWar.result == war.DEFENDERWIN:
WinnerIcon = Image.open("resources/star.png")
imgFinal.paste(
WinnerIcon, (x + 12 + 585 - 48, y + 148), WinnerIcon)
#================SINGLEPLAYER================#
else:
pass
#================END SECTION================#
# Date
await updateProgress("Drawing date...", 8, 9)
imgDraw.text((round(5177 - imgDraw.textsize(self.game.date.fancyStr(),
font)[0] / 2), 60), self.game.date.fancyStr(), (255, 255, 255), font)
await updateProgress("**Image generation complete.** Uploading...", 9, 9)
return imgFinal
async def responsive(self, message: discord.Message) -> bool:
return message.channel == self.interactChannel and not message.author.bot
async def process(self, message: discord.Message):
if message.content.upper() == "CANCEL":
await self.interactChannel.send("**Cancelling the stats operation.**")
controlledChannels.remove(self)
if self.skanderbegURL is not None:
self.skanderbegURL.cancel()
del(self)
elif not self.hasReadFile: # First step - get .eu4 file
saveFile: Optional[bytes] = None
if len(message.attachments) > 0 and message.attachments[0].filename.endswith(".eu4"):
saveFile = await message.attachments[0].read()
else: # str
saveURL: str = message.content.strip()
try:
response = await session.get(saveURL, allow_redirects=False)