Skip to content

Instantly share code, notes, and snippets.

@abma
Created March 1, 2012 18:26
Show Gist options
  • Save abma/1951961 to your computer and use it in GitHub Desktop.
Save abma/1951961 to your computer and use it in GitHub Desktop.
function gadget:GetInfo()
return {
name = "BA Chicken Scores",
desc = "Shows Scores at the end of the game",
author = "TheFatController/quantum/abma",
date = "26. Feb. 2012",
license = "GNU GPL, v2 or later",
layer = 0,
enabled = true -- loaded by default?
}
end
if (gadgetHandler:IsSyncedCode()) then
local function dump(o)
if type(o) == 'table' then
local s = '{ '
for k,v in pairs(o) do
if type(k) ~= 'number' then k = '"'..k..'"' end
s = s .. '['..k..'] = ' .. dump(v) .. ',\n'
end
return s .. '} '
else
return tostring(o)
end
end
local function getTeams()
local teams = {}
local teamlist = Spring.GetTeamList()
for _, teamID in ipairs(teamlist) do
teams[teamID] = true
end
return teams
end
--local bonusPoints = 1
--local burrowDef = UnitDefNames["roost"] --unitdef for burrow, used for bonus points
--function gadget:UnitDestroyed(unitID, unitDefID, unitTeam)
-- if (unitDefID == burrowDef) then
-- bonusPoints = (bonusPoints + 2500)
-- end
--end
local function CalculateScores(humanTeams)
local totalDamage = 0
local teamScores = {}
local totalScore = 0
for teamID in pairs(humanTeams) do
local max = Spring.GetTeamStatsHistory(teamID)
stats = Spring.GetTeamStatsHistory(teamID, 0, max)
-- Spring.Echo(dump(stats))
teamScores[teamID] = stats[max].damageDealt
totalDamage = totalDamage + teamScores[teamID]
-- Spring.Echo("TotalDamage: "..tostring(totalDamage))
local teamUnits = Spring.GetTeamUnits(teamID)
for _, unitID in pairs(teamUnits) do
local defID = Spring.GetUnitDefID(unitID)
if defID then
totalScore = totalScore + UnitDefs[defID].metalCost
totalScore = totalScore + (UnitDefs[defID].energyCost / 50)
end
end
end
for teamID in pairs(humanTeams) do
teamScores[teamID] = math.ceil(teamScores[teamID])
end
-- Spring.Echo(dump(teamScores))
return teamScores
end
local function comma_value(amount)
local formatted = amount
while true do
formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
if (k==0) then
break
end
end
return formatted
end
local function DisplayScores(scoreList)
Spring.Echo(" ")
Spring.Echo("Team Scores")
Spring.Echo("-----------")
-- Spring.Echo(dump(scoreList))
for teamID, teamScore in pairs(scoreList) do
local info = Spring.GetPlayerInfo(teamID)
local name = "Unknown"
Spring.Echo(dump(info))
if info then
name = info
end
info = Spring.GetAIInfo(teamID)
if info then
Spring.Echo(dump(info))
name = "AI"
end
Spring.Echo("Team" .. teamID .. ": " ..name .. " : " .. comma_value(teamScore) .. " Points")
--info = Spring.GetAIInfo(teamID)
--Spring.Echo(dump(info))
end
end
local function showScore()
local humanTeams = getTeams()
local endScores = CalculateScores(humanTeams)
DisplayScores(endScores)
_G.chickenEventArgs = endScores
SendToUnsynced("ChickenEvent")
_G.chickenEventArgs = nil
end
function gadget:GameFrame(n)
if n % 100 == 0 then
showScore()
end
end
function gadget:GameOver()
if not showScoresOnce then
showScore()
showScoresOnce = true
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment