forked from spoekles/aoc-discord-bot-p2p
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadventofcode.py
208 lines (183 loc) · 9.76 KB
/
adventofcode.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
import discord
from discord.ext import commands
from aocapi import AoCAPI
import utils
from random import randint
import datetime
import random
class AdventOfCodeCommands(commands.Cog):
"""Advent of Code"""
def __init__(self, year):
self.aoc_api = AoCAPI()
self.year = year
@commands.command(brief="Shows the top 20 players",
description="Shows the list of the top 20 players, with their local score and obtained stars.")
async def leaderboard(self, ctx):
leaderboard_users = self.aoc_api.get_leaderboard()
names = ""
score = ""
stars = ""
embed = discord.Embed(title="🎄 Peer2Peer Advent of Code leaderboard 🎄",
url=f"https://adventofcode.com/{self.year}/leaderboard/private/view/959961", color=0xC03221)
for i in range(20):
user = leaderboard_users[i]
if i == 0:
names += f"**🌟 {user.leaderboard_position}: {user.name}**\n"
score += f"**{user.localScore}**\n"
stars += f"**{user.stars_amount}**\n"
elif i == 1:
names += f"**⭐ {user.leaderboard_position}: {user.name}**\n"
score += f"**{user.localScore}**\n"
stars += f"**{user.stars_amount}**\n"
elif i == 2:
names += f"**💫 {user.leaderboard_position}: {user.name}**\n"
score += f"**{user.localScore}**\n"
stars += f"**{user.stars_amount}**\n"
else:
emote = random.choice(["🎄", "🎁", "🎅"])
names += f"{emote} **{user.leaderboard_position}:** {user.name}\n"
score += f"{user.localScore}\n"
stars += f"{user.stars_amount}\n"
embed.add_field(name="Name", value=names, inline=True)
embed.add_field(name="Score", value=score, inline=True)
embed.add_field(name="Stars", value=stars, inline=True)
embed.set_footer(text="Updated at: {}\n🎄🎁🎄🎁🎄🎁🎄🎁🎄🎁🎄🎁🎄🎁🎄🎁🎄🎁🎄🎁".format(
datetime.datetime.fromtimestamp(self.aoc_api.lastUpdate).strftime('%Y-%m-%d %H:%M:%S')))
await ctx.send(embed=embed)
@commands.command(brief="Shows overview of a specific player",
description="By adding an optional day argument it will also show the time taken for finished parts.")
async def user(self, ctx, name: str = "", day: int | None = None):
leaderboard_users = self.aoc_api.get_leaderboard()
user = utils.getUserFromLeaderboard(leaderboard_users, name)
if not user:
embed = discord.Embed(
title=f"User {name} was not found."
)
ctx.send(embed=embed)
return
if day == None:
embed = discord.Embed(
title="{}".format(user.name), color=0x13A10E)
embed.add_field(name="Position",
value=user.leaderboard_position, inline=True)
embed.add_field(name="Local Score",
value=user.localScore, inline=True)
if user.globalScore > 0:
embed.add_field(name="Global Score",
value=user.globalScore, inline=True)
stars = "\u200b" # With an additional normal character the star emoji gets smaller on mobile devices, thus fitting better
for i in range(1, 26):
if user.days[i].get_star_2_ts != None:
stars += "⭐"
elif user.days[i].get_star_1_ts != None:
stars += "<:silver_star:918552091553857536>"
else:
stars += "<:no_star:918553772739932221>"
if i % 5 == 0 and i != 0:
stars += "\n"
embed.add_field(name="Days completed",
value=stars, inline=False)
embed.set_footer(text="Updated at: {}\n🎄🎁🎄🎁🎄🎁🎄🎁🎄🎁🎄🎁🎄🎁🎄🎁🎄🎁🎄🎁".format(
datetime.datetime.fromtimestamp(self.aoc_api.lastUpdate).strftime('%Y-%m-%d %H:%M:%S')))
await ctx.send(embed=embed)
elif day > 0 and day <= 26:
stars = ""
if user.days[day].get_star_2_ts != None:
stars = "⭐"
elif user.days[day].get_star_1_ts != None:
stars = "<:silver_star:918552091553857536>"
else:
stars = "<:no_star:918553772739932221>"
embed = discord.Embed(title=user.name, description=f"**Day:** {day} {stars}",
color=0x13A10E)
if user.days[day].get_star_1_ts != None:
val = utils.timeTakenFormatted(
datetime.datetime(
self.year, 12, int(day), 6).timestamp(),
user.days[day].get_star_1_ts)
embed.add_field(name="Part 1", value=val, inline=False)
if user.days[day].get_star_2_ts != None:
val = utils.timeTakenFormatted(user.days[day].get_star_1_ts,
user.days[day].get_star_2_ts)
embed.add_field(name="Part 2", value=val, inline=False)
embed.set_footer(text="Updated at: {}\n🎄🎁🎄🎁🎄🎁🎄🎁🎄🎁🎄🎁🎄🎁🎄🎁🎄🎁🎄🎁".format(
datetime.datetime.fromtimestamp(self.aoc_api.lastUpdate).strftime('%Y-%m-%d %H:%M:%S')))
await ctx.send(embed=embed)
else:
embed = discord.Embed(
title=f"The command could not be completed."
)
ctx.send(embed=embed)
return
@commands.command(brief="Compare time taken for two players on a given day",
description="Shows the faster of the 2 players, time is relative to each other.")
async def userCmp(self, ctx, name1="", name2="", day: int | None = None):
leaderboard_users = self.aoc_api.get_leaderboard()
if not day:
embed = discord.Embed(
title=f"You must specify a day."
)
await ctx.send(embed=embed)
return
user1 = utils.getUserFromLeaderboard(leaderboard_users, name1)
user2 = utils.getUserFromLeaderboard(leaderboard_users, name2)
if not user1 or not user2:
embed = discord.Embed(
title=f"The users specified could not be found."
)
ctx.send(embed=embed)
return
embed = discord.Embed(title=f"{user1.name} vs {user2.name}", description=f"**Day:** {day}",
color=0xFFB900)
user1_star1_ts = user1.days[day].get_star_1_ts
user2_star1_ts = user2.days[day].get_star_1_ts
if user1_star1_ts == None or user2_star1_ts == None:
embed.add_field(name="Part 1",
value="Not all users have completed this part.",
inline=False)
else:
if user1_star1_ts < user2_star1_ts:
embed.add_field(name="Part 1",
value=f"**{user1.name}** was {utils.timeTakenFormatted(user1_star1_ts, user2_star1_ts)} faster",
inline=False)
else:
embed.add_field(name="Part 1",
value=f"**{user2.name}** was {utils.timeTakenFormatted(user2_star1_ts, user1_star1_ts)} faster",
inline=False)
user1_star2_ts = user1.days[day].get_star_2_ts
user2_star2_ts = user2.days[day].get_star_2_ts
if user1_star2_ts == None or user2_star2_ts == None:
embed.add_field(name="Part 2",
value="Not all users have completed this part.",
inline=False)
else:
if user1_star2_ts < user2_star2_ts:
embed.add_field(name="Part 2",
value=f"**{user1.name}** was {utils.timeTakenFormatted(user1_star2_ts, user2_star2_ts)} faster",
inline=False)
else:
embed.add_field(name="Part 2",
value=f"**{user2.name}** was {utils.timeTakenFormatted(user2_star2_ts, user1_star2_ts)} faster",
inline=False)
embed.set_footer(text="Updated at: {}\n🎄🎁🎄🎁🎄🎁🎄🎁🎄🎁🎄🎁🎄🎁🎄🎁🎄🎁🎄🎁".format(
datetime.datetime.fromtimestamp(self.aoc_api.lastUpdate).strftime('%Y-%m-%d %H:%M:%S')))
await ctx.send(embed=embed)
# @commands.command(brief="Compare 2 players their time taken for each day", description="Shows the faster of the 2 players, time is relative to each other.")
# async def fastest(self, ctx, day=None):
# data = self.leaderboard.get()
# part1 = {}
# part2 = {}
# for i, user in data.items():
# if user.days[day] != "":
# if user.days[day][str(1)]['get_star_ts'] != None and part1 == None:
# part1 = user
# if user.days[day][str(2)]['get_star_ts'] != None and part2 == None:
# part2 = user
# else:
# if user.days[day][str(1)]['get_star_ts'] != None:
# if int(user.days[day][str(1)]['get_star_ts']) < int(part1.days[day][str(1)]['get_star_ts']):
# part1 = user
# if user.days[day][str(2)]['get_star_ts'] != None:
# if int(user.days[day][str(2)]['get_star_ts']) < int(part2.days[day][str(2)]['get_star_ts']):
# part2 = user
# await ctx.send("{} was fast so was {}".format(part1.name, part2.name))