File: actions.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 (282 lines) | stat: -rw-r--r-- 7,094 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
-- $Id: actions.lua 2491 2008-07-17 13:36:51Z det $
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
--  file:    actions
--  brief:   hooks for GotChatMsg() and RecvFromSynced() calls
--  author:  Dave Rodgers
--
--  Copyright (C) 2007.
--  Licensed under the terms of the GNU GPL, v2 or later.
--
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

if (actionHandler) then
  return actionHandler
end


--------------------------------------------------------------------------------

local chatActions = {}

local syncActions = {}


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

local isSyncedCode = (SendToUnsynced ~= nil)

local function MakeWords(line)
  local words = {}
  for w in line:gmatch("[^%s]+") do
    table.insert(words, w)
  end   
  return words
end

            
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
--  Insertions
--

local function InsertCallInfo(callInfoList, gadget, func, help)
  local layer = gadget.ghInfo.layer
  local index = 1
  for i,ci in ipairs(callInfoList) do
    local g = ci[2]
    if (g == gadget) then
      return false  --  already in the table
    end
    if (layer >= g.ghInfo.layer) then
      index = i + 1
    end
  end
  table.insert(callInfoList, index, { func, gadget, help = help })
  return true
end


local function InsertAction(map, gadget, cmd, func, help)
  local callInfoList = map[cmd]
  if (callInfoList == nil) then
    callInfoList = {}
    map[cmd] = callInfoList
  end
  return InsertCallInfo(callInfoList, gadget, func, help)
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
--  Removals
--

local function RemoveCallInfo(callInfoList, gadget)
  local count = 0
  for i,callInfo in ipairs(callInfoList) do
    local g = callInfo[2]
    if (g == gadget) then
      table.remove(callInfoList, i)
      count = count + 1
      -- break
    end
  end
  return count
end


local function RemoveAction(map, gadget, cmd)
  local callInfoList = map[cmd]
  if (callInfoList == nil) then
    return false
  end
  local count = RemoveCallInfo(callInfoList, gadget)
  if (#callInfoList <= 0) then
    map[cmd] = nil
  end
  return (count > 0)
end


local function RemoveGadgetActions(gadget)
  local function clearActionList(actionMap)
    for cmd, callInfoList in pairs(actionMap) do
      RemoveCallInfo(callInfoList, gadget)
    end
  end
  clearActionList(chatActions)
  clearActionList(syncActions)
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
--  Add / Remove Chat Action
--


local function AddChatAction(gadget, cmd, func, help)
  return InsertAction(chatActions, gadget, cmd, func, help)
end


local function RemoveChatAction(gadget, cmd)
  return RemoveAction(chatActions, gadget, cmd)
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
--  Add / Remove Sync Action
--

local function AddSyncAction(gadget, cmd, func, help)
  return InsertAction(syncActions, gadget, cmd, func, help)
end


local function RemoveSyncAction(gadget, cmd)
  return RemoveAction(syncActions, gadget, cmd)
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

local function EchoLines(msg)
  for line in msg:gmatch('([^\n]+)\n?') do
    Spring.Echo(line)
  end
end


local function Help(playerID, cmd)
  if (cmd == nil) then
    -- print the list of commands, alphabetically
    local sorted = {}
    for name in pairs(chatActions) do
      table.insert(sorted, name)
    end
    table.sort(sorted)
    local str = Script.GetName() .. ' commands: '
    for _,name in ipairs(sorted) do
      str = str .. '  ' .. name
    end
    Spring.Echo(str)
    if (isSyncedCode) then
      SendToUnsynced(playerID, "help")
    end
  else
    local callInfoList = chatActions[cmd]
    if (not callInfoList) then
      if (not isSyncedCode) then
        Spring.Echo('unknown command:  ' .. cmd)
      end
    else
      for i,callInfo in ipairs(callInfoList) do
        if (callInfo.help) then
          EchoLines(cmd .. callInfo.help)
          return
        end
      end
    end
    if (isSyncedCode) then
      SendToUnsynced(playerID, "help " .. cmd)
    end
  end
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

local function GotChatMsg(msg, playerID)
  local words = MakeWords(msg)
  local cmd = words[1]
  if (cmd == nil) then
    return false
  end

  local callInfoList = chatActions[cmd]
  if (callInfoList == nil) then
    if (cmd == 'help') then
      Help(playerID, words[2])
      return true
    end
    return false
  end

  -- remove the command from the words list and the raw line
  table.remove(words, 1)
  local _,_,msg = msg:find("[%s]*[^%s]+[%s]+(.*)")
  if (msg == nil) then
    msg = ""  -- no args
  end

  for i,callInfo in ipairs(callInfoList) do
    local func = callInfo[1]
    -- local gadget = callInfo[2]
    if (func(cmd, msg, words, playerID)) then
      return true
    end
  end

  return false
end


local function RecvFromSynced(arg1,arg2,...)
  if (type(arg1) == 'string') then
    -- a raw sync msg
    local callInfoList = syncActions[arg1]
    if (callInfoList == nil) then
      return false
    end
    
    for i,callInfo in ipairs(callInfoList) do
      local func = callInfo[1]
      -- local gadget = callInfo[2]
      if (func(arg1,arg2,...)) then
        return true
      end
    end
    return false
  end

  return false -- unknown type
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

local AH = {}

actionHandler = AH  -- set the global

AH.GotChatMsg     = GotChatMsg
AH.RecvFromSynced = RecvFromSynced

AH.AddChatAction    = AddChatAction
AH.AddSyncAction    = AddSyncAction
AH.RemoveChatAction = RemoveChatAction
AH.RemoveSyncAction = RemoveSyncAction

AH.RemoveGadgetActions = RemoveGadgetActions

AH.HaveChatAction = function() return (next(chatActions) ~= nil) end
AH.HaveSyncAction = function() return (next(syncActions) ~= nil) end

return AH


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------