2018-11-18 18:12:00 +02:00
|
|
|
-- dependencies
|
|
|
|
local Global = require 'utils.global'
|
|
|
|
|
|
|
|
-- this
|
|
|
|
local ScoreTable = {}
|
|
|
|
|
|
|
|
local scores = {}
|
|
|
|
|
|
|
|
Global.register({
|
|
|
|
scores = scores,
|
|
|
|
}, function (tbl)
|
|
|
|
scores = tbl.scores
|
|
|
|
end)
|
|
|
|
|
2018-11-23 23:04:55 +02:00
|
|
|
---Resets the score 0 for the given name
|
|
|
|
---@param name string
|
2018-11-18 18:12:00 +02:00
|
|
|
function ScoreTable.reset(name)
|
|
|
|
scores[name] = 0
|
|
|
|
end
|
|
|
|
|
2018-11-23 23:04:55 +02:00
|
|
|
---Adds score.
|
|
|
|
---@param name string
|
|
|
|
---@param value number the sum for the score added by name
|
|
|
|
---@return number the sum for the score added by the name
|
2018-11-18 18:12:00 +02:00
|
|
|
function ScoreTable.add(name, value)
|
|
|
|
local new = (scores[name] or 0) + value
|
|
|
|
scores[name] = new
|
|
|
|
return new
|
|
|
|
end
|
|
|
|
|
2018-11-23 23:04:55 +02:00
|
|
|
---Sets score.
|
|
|
|
---@param name string
|
|
|
|
---@param value number the sum for the score added by name
|
|
|
|
function ScoreTable.set(name, value)
|
|
|
|
scores[name] = value
|
|
|
|
end
|
2018-11-18 18:12:00 +02:00
|
|
|
|
2018-11-23 23:04:55 +02:00
|
|
|
---Increments the score by 1 for name.
|
|
|
|
---@param name string
|
|
|
|
---@return number the sum for the score incremented by name
|
2018-11-18 18:12:00 +02:00
|
|
|
function ScoreTable.increment(name)
|
|
|
|
return ScoreTable.add(name, 1)
|
|
|
|
end
|
|
|
|
|
2018-11-23 23:04:55 +02:00
|
|
|
---Returns the score for a single key.
|
|
|
|
---@param name string
|
|
|
|
---@return number the sum for the score by name
|
2018-11-18 18:12:00 +02:00
|
|
|
function ScoreTable.get(name)
|
|
|
|
return scores[name] or 0
|
|
|
|
end
|
|
|
|
|
2018-11-23 23:04:55 +02:00
|
|
|
---Returns all scores.
|
|
|
|
---@return table {[string] = int}
|
2018-11-18 18:12:00 +02:00
|
|
|
function ScoreTable.all()
|
|
|
|
return scores
|
|
|
|
end
|
|
|
|
|
|
|
|
return ScoreTable
|