diff --git a/lua/vim-be-good/game-runner.lua b/lua/vim-be-good/game-runner.lua index f618e9d..ed5d455 100644 --- a/lua/vim-be-good/game-runner.lua +++ b/lua/vim-be-good/game-runner.lua @@ -264,7 +264,9 @@ function GameRunner:renderEndGame() self.ended = true + Stats:logHighscore(sum/self.config.roundCount , self.results.games[1]) -- TODO: Make this a bit better especially with random. + -- print to screen within table table.insert(lines, string.format("%d / %d completed successfully", self.results.successes, self.config.roundCount)) table.insert(lines, string.format("Average %.2f", sum / self.config.roundCount)) table.insert(lines, string.format("Game Type %s", self.results.games[1])) diff --git a/lua/vim-be-good/menu.lua b/lua/vim-be-good/menu.lua index afbb899..90c6051 100644 --- a/lua/vim-be-good/menu.lua +++ b/lua/vim-be-good/menu.lua @@ -2,6 +2,9 @@ local log = require("vim-be-good.log") local bind = require("vim-be-good.bind") local types = require("vim-be-good.types") local createEmpty = require("vim-be-good.game-utils").createEmpty +local statistics = require("vim-be-good.statistics") + + local Menu = {} @@ -37,7 +40,7 @@ local credits = { "https://twitch.tv/ThePrimeagen", } -function Menu:new(window, onResults) +function Menu:new(window, onResults, highscore) local menuObj = { window = window, buffer = window.buffer, @@ -48,6 +51,8 @@ function Menu:new(window, onResults) -- relative game = types.games[1], + + highscore = highscore, } window.buffer:clear() @@ -138,13 +143,24 @@ end function Menu:render() self.window.buffer:clearGameLines() + local highscoreTab = Stats:loadHighscore() local lines = { } + for idx = 1, #gameHeader do table.insert(lines, gameHeader[idx]) end for idx = 1, #types.games do - table.insert(lines, createMenuItem(types.games[idx], self.game)) + log.info("save highscore ?", vim.g["vim_be_good_save_highscore"]) + if vim.g["vim_be_good_save_highscore"] or false then + if types.games[idx] == "random" then + table.insert(lines, createMenuItem(types.games[idx], self.game)) + else + table.insert(lines, createMenuItem(types.games[idx], self.game).." \t\t\t | " .. tostring(highscoreTab[types.games[idx]]) .. " sec") + end + else + table.insert(lines, createMenuItem(types.games[idx], self.game)) + end end for idx = 1, #difficultyHeader do diff --git a/lua/vim-be-good/statistics.lua b/lua/vim-be-good/statistics.lua index 40673ec..bf28af6 100644 --- a/lua/vim-be-good/statistics.lua +++ b/lua/vim-be-good/statistics.lua @@ -1,9 +1,10 @@ local log = require("vim-be-good.log") +local types = require("vim-be-good.types") + local default_config = { plugin = 'VimBeGoodStats', - save_statistics = vim.g["vim_be_good_save_statistics"] or false, - + save_highscore = vim.g["vim_be_good_save_highscore"] or false, } local statistics = {} @@ -14,12 +15,71 @@ function statistics:new(config) local stats = { file = string.format('%s/%s.log', vim.api.nvim_call_function('stdpath', {'data'}), config.plugin), - saveStats = config.save_statistics + saveStats = config.save_statistics, + saveHighscore = config.save_highscore } self.__index = self return setmetatable(stats, self) end +function statistics:loadHighscore() + if self.saveHighscore then + local out = io.open(self.file, 'r') + + local fLines = {} + for l in out:lines() do + table.insert(fLines, l) + end + + out:close() + + local highscoreTable = {} + highscoreTable= {} + for k, v in string.gmatch(fLines[1], "(%w+{*):(%d+%.%d+)") do + highscoreTable[k] = v + end + return highscoreTable + end +end + +function statistics:logHighscore(average,gameType) + if self.saveHighscore then + local out = io.open(self.file, 'r') + + local fLines = {} + for l in out:lines() do + table.insert(fLines, l) + end + + out:close() + + -- this init-line if its not present add init line in statistics tab + if string.find(fLines[1],":") == nil then + local highscoreLine = "" + for idx = 1, #types.games - 1 do + highscoreLine = highscoreLine .. types.games[idx] .. ":10.00," + end + table.insert(fLines,1,highscoreLine) + end + + local currHighscore = (string.match(fLines[1],gameType..":".."(%d+%.%d%d)")) + -- if game gets new gametypes then they are not in highscore line this could case a crash + if currHighscore == nil then + fLines[1] = fLines[1] .. "," .. gameType .. ":" .. string.format("%.2f",average) + else + if tonumber(currHighscore) > average then + fLines[1] = string.gsub(fLines[1],gameType..":"..currHighscore,gameType..":"..string.format("%.2f",average)) + end + end + + local out = io.open(self.file, 'w') + for _, l in ipairs(fLines) do + out:write(l.."\n") + end + out:close() + end +end + function statistics:logResult(result) if self.saveStats then local fp = io.open(self.file, "a")