-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathGMCP_message_receiver_test.xml
158 lines (120 loc) · 3.79 KB
/
GMCP_message_receiver_test.xml
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
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>
<muclient>
<plugin
name="GMCP_message_receiver_test"
author="Nick Gammon"
id="b35ff32bccfac9d02023ad3b"
language="Lua"
purpose="Test OnPluginBroadcast for GMCP"
date_written="2015-05-06 08:00:31"
requires="4.90"
version="1.0"
>
</plugin>
<!-- Script -->
<script>
<![CDATA[
require "tprint"
require "json"
function getStats (what)
result, value = CallPlugin("3e7dedbe37e44942dd46d264", "gmcpval", what)
if result ~= 0 then
ColourNote ("orange", "", "Warning: Could not get stats for " .. what)
return nil
end -- if
return value
end -- getStats
function gotCharacterVitals (vitals)
-- tprint (vitals)
-- example:
print ("HP is now:", tonumber (vitals.hp))
end -- gotCharacterVitals
function gotCharacterStatus (status)
-- tprint (status)
end -- gotCharacterStatus
function gotRoomInfo (info)
-- tprint (info)
end -- gotRoomInfo
function gotRoomPlayers (players)
-- tprint (players)
end -- gotRoomPlayers
function gotWrongDirection (dir)
-- print ("Could not head: ", dir [1])
Sound ("ding.wav")
end -- gotWrongDirection
function gotAddPlayer (which)
tprint (which)
end -- gotAddPlayer
function gotRemovePlayer (which)
tprint (which)
end -- gotRemovePlayer
function gotCharacterName (name)
tprint (name)
end -- gotCharacterName
function gotCharacterStatusVars (status)
-- tprint (status)
end -- gotCharacterStatusVars
function gotCharacterAfflictionsList (affs)
tprint (affs)
end -- gotCharacterAfflictionsList
function gotCharacterAfflictionsAdd (what)
tprint (what)
end -- gotCharacterAfflictionsAdd
function gotCharacterDefencesList (defs)
tprint (defs)
end -- gotCharacterDefencesList
function gotCharacterDefencesAdd (add)
tprint (add)
end -- gotCharacterDefencesAdd
function gotCharacterAfflictionsRemove (what)
tprint (what)
end -- gotCharacterAfflictionsRemove
-- WARNING: Put handlers table AFTER the handler functions
-- Make names lower case, as we do a lower-case conversion for the lookup
handlers = {
["char.vitals"] = gotCharacterVitals,
["char.status"] = gotCharacterStatus,
["char.name"] = gotCharacterName,
["char.statusvars"] = gotCharacterStatusVars,
["char.afflictions.list"] = gotCharacterAfflictionsList,
["char.afflictions.add"] = gotCharacterAfflictionsAdd,
["char.afflictions.remove"] = gotCharacterAfflictionsRemove,
["char.defences.list"] = gotCharacterDefencesList,
["char.defences.add"] = gotCharacterDefencesAdd,
["room.info"] = gotRoomInfo,
["room.players"] = gotRoomPlayers,
["room.wrongdir"] = gotWrongDirection,
["room.removeplayer"] = gotRemovePlayer,
["room.addplayer"] = gotAddPlayer,
} -- end of handlers
function OnPluginBroadcast (msg, id, name, text)
if id == "74f8c420df7d59ad5aa66246" then -- GMCP_handler_NJG
-- pull out GMCP message, plus the data belonging to it
message, params = string.match (text, "([%a.]+)%s+(.*)")
-- no match? oops!
if not message then
return
end -- if
-- ensure we have an array or object
if not string.match (params, "^[%[{]") then
params = "[" .. params .. "]" -- JSON hack, make msg first element of an array.
end -- if
-- decode it
result = assert (json.decode (params))
-- debugging
ColourNote ("yellow", "", "GMCP: " .. message)
-- find a handler for this message type
local handler = handlers [message:lower ()]
-- warn if not found
if not handler then
ColourNote ("red", "", "Warning: No handler for: " .. message)
return
end -- no handler
-- call the handler, pass in whatever we got
handler (result)
end -- if GMCP message
end -- OnPluginBroadcast
]]>
</script>
</muclient>