From 922aec35c150f2c448e3a1795c3697f7ea2c0701 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Sat, 29 Nov 2025 15:12:27 +0100 Subject: [PATCH] fix: simplify loadStats --- src/calendar.lua | 1 + src/lib/aoc.lua | 22 ++++++---------------- src/lib/days.lua | 2 +- 3 files changed, 8 insertions(+), 17 deletions(-) diff --git a/src/calendar.lua b/src/calendar.lua index ab5f7f5..03ca1ab 100644 --- a/src/calendar.lua +++ b/src/calendar.lua @@ -19,6 +19,7 @@ local function insertInReadme() end local totalStars = 0 +---@diagnostic disable-next-line: param-type-mismatch for _, s in pairs(stats) do if s.puzzle1 then totalStars = totalStars + 1 end if s.puzzle2 then totalStars = totalStars + 1 end diff --git a/src/lib/aoc.lua b/src/lib/aoc.lua index 5c1b7d5..cba558b 100644 --- a/src/lib/aoc.lua +++ b/src/lib/aoc.lua @@ -11,24 +11,14 @@ END_DATE = {day=12, month=12, year=2025} local json = require("json") local dates = require("dates") local days = require("days") +local utils = require("utils") local progress = require "progress" local today = os.date("*t") local aoc = {} -function aoc.loadStats(path) - path = shell.resolve(path) - if not fs.exists(path) then - printError("Stats file not found (" .. path .. ")") - return - end - local file, err = fs.open(path, "r") - if not file then - printError("Cannot open stats file") - return - end - local data = json.loads(file.readAll()) - file.close() - return data +function aoc.loadStats() + local data = utils.readFile(RES_PATH .. "/stats.json") or "{}" + return json.loads(data) end function aoc.printDateInfo() @@ -111,7 +101,7 @@ function aoc.printBanner() end function aoc.main() - local stats = aoc.loadStats("aoc/res/stats.json") or {} + local stats = aoc.loadStats() or {} local selectedDay = math.max(1, math.min(END_DATE.day, today.day)) if not dates.isInDateRange(START_DATE, today, END_DATE) then selectedDay = 1 @@ -139,7 +129,7 @@ function aoc.main() dayStats.puzzle2 ) day:show() - stats = aoc.loadStats("aoc/res/stats.json") or {} + stats = aoc.loadStats() or {} end end end diff --git a/src/lib/days.lua b/src/lib/days.lua index 7437821..5ec2439 100644 --- a/src/lib/days.lua +++ b/src/lib/days.lua @@ -65,7 +65,7 @@ function Day:getTitle() end end fs.makeDir(self:cacheDir()) - local res = http.get("https://adventofcode.com/2024/day/" .. self.day) + local res = http.get("https://adventofcode.com/2025/day/" .. self.day) local title = "Day " .. self.day if res then local body = res.readAll() or ""