Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new highscore feature #134

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions lua/vim-be-good/game-runner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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]))
Expand Down
20 changes: 18 additions & 2 deletions lua/vim-be-good/menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}

Expand Down Expand Up @@ -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,
Expand All @@ -48,6 +51,8 @@ function Menu:new(window, onResults)

-- relative
game = types.games[1],

highscore = highscore,
}

window.buffer:clear()
Expand Down Expand Up @@ -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
Expand Down
66 changes: 63 additions & 3 deletions lua/vim-be-good/statistics.lua
Original file line number Diff line number Diff line change
@@ -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 = {}
Expand All @@ -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")
Expand Down