41 lines
991 B
Luau
41 lines
991 B
Luau
--!strict
|
|
export type Row = {userId: number, stats: {[string]: number}}
|
|
local Stats = {}
|
|
local byPlayer: {[Player]: {[string]: number}} = {}
|
|
|
|
function Stats.reset()
|
|
table.clear(byPlayer)
|
|
end
|
|
|
|
function Stats.add(player: Player, key: string, amount: number)
|
|
local record = byPlayer[player]
|
|
if not record then
|
|
record = {}
|
|
byPlayer[player] = record
|
|
end
|
|
record[key] = (record[key] or 0) + amount
|
|
end
|
|
|
|
function Stats.get(player: Player): {[string]: number}
|
|
return table.clone(byPlayer[player] or {})
|
|
end
|
|
|
|
function Stats.remove(player: Player)
|
|
byPlayer[player] = nil
|
|
end
|
|
|
|
function Stats.mvp(rows: {Row}, weights: {[string]: number}): number?
|
|
local best: number? = nil
|
|
local bestScore = -math.huge
|
|
for _, row in rows do
|
|
local score = 0
|
|
for key, weight in weights do score += (row.stats[key] or 0) * weight end
|
|
if score > bestScore or (score == bestScore and best ~= nil and row.userId < best) then
|
|
best, bestScore = row.userId, score
|
|
end
|
|
end
|
|
return best
|
|
end
|
|
|
|
return Stats
|