File: game_messages.lua

package info (click to toggle)
spring 103.0%2Bdfsg2-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 43,720 kB
  • ctags: 63,685
  • sloc: cpp: 368,283; ansic: 33,988; python: 12,417; java: 12,203; awk: 5,879; sh: 1,846; xml: 655; perl: 405; php: 211; objc: 194; makefile: 77; sed: 2
file content (77 lines) | stat: -rw-r--r-- 2,300 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
function widget:GetInfo()
  return {
    name      = "TeamDiedMessages",
    desc      = "Prints a message when a team die",
    author    = "abma & zwzsg",
    date      = "Sep, 2013",
    license   = "GNU GPL, v2 or later",
    layer     = 5,
    enabled   = true
  }
end

local sec = "game_messages.lua" -- log section
local msgsfile = "gamedata/messages.tdf"

local luaMsgs = {}

function widget:Initialize()
	local TDF = VFS.Include('gamedata/parse_tdf.lua')
	if (not VFS.FileExists(msgsfile)) then
		Spring.Log(sec, LOG.INFO, "gamedata/messages.tdf doesn't exist.")
		return
	end

	local tdfMsgs, err = TDF.Parse(msgsfile)
	if (tdfMsgs == nil) then
		Spring.Log(sec, LOG.ERROR, 'Error parsing '.. msgsfile  .. err)
	end
	if (type(tdfMsgs.messages) ~= 'table') then
		Spring.Log(sec, LOG.ERROR, 'Missing "messages" section in ' .. msgsfile )
	end

	for label, msgs in pairs(tdfMsgs.messages) do
		if ((type(label) == 'string') and (type(msgs)  == 'table')) then
			local msgArray = {}
			for _, msg in pairs(msgs) do
				if (type(msg) == 'string') then
					table.insert(msgArray, msg)
				end
			end
			luaMsgs[label:lower()] = msgArray -- lower case keys
		end
	end
end

local function Translate(msg)
	msg = msg:lower()
	if luaMsgs and type(luaMsgs[msg]) == "table" then
		local msgs = luaMsgs[msg]
		return msgs[ math.random( #msgs ) ]
	end
	Spring.Log(sec, LOG.DEBUG, string.format("Missing translation for %s", msg))
	return msg
end

function widget:TeamDied(teamID)
	local playerNames
	local _, leaderPlayerId, isDead, isAiTeam = Spring.GetTeamInfo(teamID)
	if isAiTeam then
		local skirmishAIID, aiName, hostingPlayerID, shortName, version, options = Spring.GetAIInfo(teamID)
		if aiName~="UNKNOWN" and aiName:lower():sub(1,3)~="bot" and aiName:len()>1 then
			playerNames=aiName
		elseif shortName~="UNKNOWN" then
			playerNames=shortName
		elseif hostingPlayerID~=Spring.GetMyPlayerID() then
			local ownerName=Spring.GetPlayerInfo(hostingPlayerID)
			if ownerName then
				playerNames=ownerName.."'s bot"
			end
		end
	end
	for _,playerId in ipairs(Spring.GetPlayerList(teamID,true)) do
		playerNames=(playerNames and playerNames..", " or "")..Spring.GetPlayerInfo(playerId)
	end
	msg = string.format(Translate("Team%i(%s) is no more"), teamID, playerNames or "")
	Spring.Echo(msg)
end