Skip to content

Commit

Permalink
minor improvemts for the aoc command (#119)
Browse files Browse the repository at this point in the history
* seperated different things into different files and made year automatically detected for the aoc command

* added year option to command

* fixed lint
  • Loading branch information
ProgramPhoenix authored Nov 28, 2023
1 parent 7c94dd0 commit 21c7c22
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 63 deletions.
22 changes: 18 additions & 4 deletions modules/aoc/aocCommand.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
import {ISlashCommand} from "../../types";
import {CommandInteraction} from "discord.js";
import {embedLeaderboard} from "./aocLeaderboardParser";
import {ApplicationCommandOptionType, CommandInteraction, CommandInteractionOptionResolver} from "discord.js";
import {embedLeaderboard} from "./aocEmbedGenerator";
import {get} from "../../lib/configmanager";
import {DateTime} from "luxon";

/**
* Slash command definition for /aoc, a command that sends the current leaderboard
*/
export default {
command: {
name: "aoc",
description: "Returns the Advent of Code Leaderboard"
description: "Returns the Advent of Code Leaderboard",
options:[
{
type: ApplicationCommandOptionType.Integer,
name: "year",
description: "the year of the Advent of Code",
min_value: 2022,
max_value: DateTime.now().setZone("Europe/Berlin").year,
required: false
}
]
},
handler: async function(interaction: CommandInteraction) {
const embed = await embedLeaderboard(get("id", "aoc") as number, get("year", "aoc") as number);
const options = interaction.options as CommandInteractionOptionResolver;
const requestedYear = options.getInteger("year", false);
const year = (requestedYear === undefined)? DateTime.now().setZone("Europe/Berlin").year : requestedYear;
const embed = await embedLeaderboard(get("id", "aoc") as number, year);
await interaction.reply({ embeds: [embed] });
}
} as ISlashCommand;
32 changes: 32 additions & 0 deletions modules/aoc/aocEmbedGenerator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {EmbedBuilder} from "discord.js";
import {requestLeaderboard} from "./aocLeaderboardParser";


/**
* Generates the embed for displaying a leaderboard
*
* @param id ID of the leaderboard
* @param year Year of this AoC competition
* @returns The Embed for showing the leaderboard
*/
export async function embedLeaderboard(id:number, year: number) : Promise<EmbedBuilder> {
const members = await requestLeaderboard(id, year);
const map = {} as {[key:string]: string};
for(const m of members) {
map[m.name] = "";
for (const k in m.completion_day_level) {
let emoji = ":eight_pointed_black_star:";
if (m.completion_day_level[k]["1"].get_star_ts != 0) {
if (m.completion_day_level[k]["2"].get_star_ts != 0) {
emoji = ":star2:";
} else {
emoji = ":star:";
}
}
map[m.name] = map[m.name] + emoji;
}
}
return new EmbedBuilder().setURL("https://adventofcode.com").setTitle("Advent of Code Leaderboard")
.addFields({name:"Ranking", value:members.map(e => { return e.name + ": " + e.local_score + map[e.name]; }).join("\n")});

}
61 changes: 3 additions & 58 deletions modules/aoc/aocLeaderboardParser.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,9 @@
import axios from "axios";
import {EmbedBuilder} from "discord.js";
import {aocData, aocMember} from "./aocTypes";

const url = "https://adventofcode.com/";
const path = "/leaderboard/private/view/";
interface aoc_data {
event: string
owner_id: number
members: {
[id:number]:aoc_member
}
}

interface aoc_member {
last_start_ts: number
id: number
local_score: number
stars: number
name: string
global_score: number
completion_day_level: {
[day:number]: {
1: {
get_star_ts: number
star_index: number
}
2: {
get_star_ts: number
star_index: number
}
}
}
}

/**
* Gets the current leaderboard data from the AoC servers
Expand All @@ -39,10 +12,10 @@ interface aoc_member {
* @param year Year of this AoC competition
* @returns Array of members of this leaderboard
*/
export async function requestLeaderboard(id: number, year: number) : Promise<aoc_member[]> {
export async function requestLeaderboard(id: number, year: number) : Promise<aocMember[]> {
const concaturl = url + year + path + id + ".json";
const answer = await axios.get(concaturl, {headers: {'Content-Type' : 'application/json', 'cookie':'session=' + process.env.AOC_SESSION}});
const data = answer.data as aoc_data;
const data = answer.data as aocData;
const members = Object.values(data.members);
members.sort((a, b)=> {
if (a.local_score < b.local_score) {
Expand All @@ -55,33 +28,5 @@ export async function requestLeaderboard(id: number, year: number) : Promise<aoc
return members;
}

/**
* Generates the embed for displaying a leaderboard
*
* @param id ID of the leaderboard
* @param year Year of this AoC competition
* @returns The Embed for showing the leaderboard
*/
export async function embedLeaderboard(id:number, year: number) : Promise<EmbedBuilder> {
const members = await requestLeaderboard(id, year);
const map = {} as {[key:string]: string};
for(const m of members) {
map[m.name] = "";
for (const k in m.completion_day_level) {
let a = ":eight_pointed_black_star:";
if (m.completion_day_level[k]["1"].get_star_ts != 0) {
if (m.completion_day_level[k]["2"].get_star_ts != 0) {
a = ":star2:";
} else {
a = ":star:";
}
}
map[m.name] = map[m.name] + a;
}
}
const embed = new EmbedBuilder().setURL("https://adventofcode.com").setTitle("Advent of Code Leaderboard")
.addFields({name:"Ranking", value:members.map(e => { return e.name + ": " + e.local_score + map[e.name]; }).join("\n")});
return embed;
}


2 changes: 1 addition & 1 deletion modules/aoc/aocTask.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Client, TextChannel} from "discord.js";
import {TaskExecutor} from "../../types";
import {embedLeaderboard} from "./aocLeaderboardParser";
import {embedLeaderboard} from "./aocEmbedGenerator";
import {get} from "../../lib/configmanager";
import {DateTime} from "luxon";

Expand Down
28 changes: 28 additions & 0 deletions modules/aoc/aocTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export interface aocData {
event: string
owner_id: number
members: {
[id:number]:aocMember
}
}

export interface aocMember {
last_start_ts: number
id: number
local_score: number
stars: number
name: string
global_score: number
completion_day_level: {
[day:number]: {
1: {
get_star_ts: number
star_index: number
}
2: {
get_star_ts: number
star_index: number
}
}
}
}

0 comments on commit 21c7c22

Please sign in to comment.