Skip to content
Snippets Groups Projects
Commit c286ce67 authored by magicfelix's avatar magicfelix
Browse files

Add basic functionality

parents
No related branches found
No related tags found
No related merge requests found
local version = "0.1.0"
local modpath = minetest.get_modpath("magic_stats")
local srcpath = modpath .. "/src"
magic_stats = {}
dofile(srcpath .. "/game.lua")
dofile(srcpath .. "/utils.lua")
dofile(srcpath .. "/commands.lua")
name = magic_stats
description = Library to handle player statistics in minigames
depends = lib_chatcmdbuilder
local stats_cmd = chatcmdbuilder.register("stats", {
description = "Show game statistics for yourself or another player. /stats <game> [player]",
})
stats_cmd:sub(":game", function(name, gamename)
local game = magic_stats.get_game_by_name(gamename)
if not game then
return false, "A game with the name " .. gamename .. " does not exist."
end
return game:get_stats(name)
end)
stats_cmd:sub(":game :target", function(name, gamename, target)
local game = magic_stats.get_game_by_name(gamename)
if not game then
return false, "A game with the name " .. gamename .. " does not exist."
end
return game:get_stats(target)
end)
local storage = minetest.get_mod_storage()
local games = {}
local Game = {}
function Game.get_stat(self, player, category)
return storage:get_int("STATS." .. self.mod.. "." .. player .. "." .. category)
end
function Game.set_stat(self, player, category, value)
storage:set_int("STATS." .. self.mod .. "." .. player .. "." .. category, value)
end
function Game.increment_stat(self, player, category, difference)
local current = self:get_stat(player, category)
if current == nil then
current = 0
end
self:set_stat(player, category, current + difference)
end
function Game.get_stats(self, target)
local output = self.display_name .. " statistics of " .. target
for i, category in ipairs(self.categories) do
output = output .. "\n" .. category .. ": " .. self:get_stat(target, category)
end
return true, output
end
function Game.register(mod, name, display_name, categories)
local game = {
mod = mod,
name = name,
display_name = display_name,
categories = categories,
get_stat = Game.get_stat,
set_stat = Game.set_stat,
increment_stat = Game.increment_stat,
get_stats = Game.get_stats,
}
games[mod] = game
return game
end
magic_stats.games = games
magic_stats.Game = Game
function magic_stats.get_game_by_mod(mod)
return magic_stats.games[mod]
end
function magic_stats.get_game_by_name(name)
for mod, game in pairs(magic_stats.games) do
if game.name == name then
return game
end
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment