This repository has been archived by the owner on Mar 14, 2021. It is now read-only.
forked from nickgammon/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Room_Info_Helper.xml
285 lines (210 loc) · 7.59 KB
/
Room_Info_Helper.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
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
283
284
285
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>
<!-- Saved on Sunday, February 28, 2010, 10:44 AM -->
<!-- MuClient version 4.50 -->
<!-- Plugin "Room_Info_Helper" generated by Plugin Wizard -->
<muclient>
<plugin
name="Room_Info_Helper"
author="Nick Gammon"
id="8a78c14d185c36109f479926"
language="Lua"
purpose="Manages room information for various plugins"
date_written="2010-02-28 10:43:27"
requires="4.50"
version="1.0"
>
<description trim="y">
<![CDATA[
Install required for Mapper_Telnet and Room_Contents_Telnet plugins.
]]>
</description>
</plugin>
<!-- Script -->
<script>
<![CDATA[
local PPI = require("ppi")
room_not_in_database = {}
room_in_database = {}
requested_rooms = {}
function dbcheck (code)
if code ~= sqlite3.OK and -- no error
code ~= sqlite3.ROW and -- completed OK with another row of data
code ~= sqlite3.DONE then -- completed OK, no more rows
local err = db:errmsg () -- the rollback will change the error message
db:exec ("ROLLBACK") -- rollback any transaction to unlock the database
error (err, 2) -- show error in caller's context
end -- if
end -- dbcheck
function fixsql (s)
if s then
return "'" .. (string.gsub (s, "'", "''")) .. "'" -- replace single quotes with two lots of single quotes
else
return "NULL"
end -- if
end -- fixsql
function fixbool (b)
if b then
return 1
else
return 0
end -- if
end -- fixbool
function load_room_from_database (uid)
local room
-- if not in database, don't look again
if room_not_in_database [uid] then
return nil
end -- no point looking
for row in db:nrows(string.format ("SELECT * FROM rooms WHERE uid = %s", fixsql (uid))) do
room = {
hash = row.hash,
exits = {} }
for exitrow in db:nrows(string.format ("SELECT * FROM exits WHERE fromuid = %s", fixsql (uid))) do
room.exits [exitrow.dir] = {
uid = tostring (exitrow.touid),
closed = exitrow.closed == 1,
locked = exitrow.locked == 1,
door = exitrow.door == 1,
}
end -- for each exit
end -- finding room
if room then
return room
end -- if found
room_not_in_database [uid] = true
return nil
end -- load_room_from_database
local IAC, SB, SE = 0xFF, 0xFA, 0xF0
-- check if single item cached
function get_room (uid, ask_server)
-- see if in database
local room = load_room_from_database (uid)
-- yes? just return it then
if room then
return room
end -- if found
if requested_rooms [uid] or not ask_server then
return nil
end -- if already asked for it
SendPkt (string.char (IAC, SB, 102) .. "room_info = \"" .. uid .. "\"" .. string.char (IAC, SE))
Note ("Requested cache ID: ", guid)
requested_rooms [uid] = GetInfo (232) -- don't request twice
return nil
end -- get_room
function save_room_to_database (uid, hash, contents)
for row in db:nrows(string.format ("SELECT uid FROM rooms WHERE uid = %s", fixsql (uid))) do
room_in_database [uid] = true
end -- finding room
if room_in_database [uid] then
return
end -- already there
dbcheck (db:execute (string.format ([[
INSERT INTO rooms (uid, hash, date_added)
VALUES (%s, %s, DATETIME('NOW'));
]], fixsql (uid), fixsql (hash)
)))
--[=[
-- for full-text searching
dbcheck (db:execute (string.format ([[
INSERT INTO rooms_lookup (uid, name, desc) VALUES (%s, %s, %s);
]], fixsql (uid),
fixsql (room.name),
fixsql (room.desc)
)))
--]=]
for exit, details in pairs (contents) do
if string.match (exit, "^r%.") then
dbcheck (db:execute (string.format ([[
INSERT INTO exits (dir, fromuid, touid, closed, locked, door, date_added)
VALUES (%s, %s, %s, %i, %i, %i, DATETIME('NOW'));
]], fixsql (details.dir), -- direction (eg. "n")
fixsql (uid), -- from current room
fixsql (exit), -- destination room
fixbool (details.closed), -- exit closed
fixbool (details.locked), -- exit locked
fixbool (details.door) -- is a door
)))
end -- if an exit
end -- for each exit
room_not_in_database [uid] = false
end -- function
function find_rooms (match_text)
local guids = {}
for row in db:nrows(string.format ("SELECT guid, name FROM rooms_lookup WHERE rooms_lookup MATCH %s", fixsql (match_text))) do
table.insert (guids, { guid = row.guid, name = row.name } )
end -- finding room
return guids
end -- find_rooms
function OnPluginInstall ()
-- open database on disk (uses world address, that is which MUD we are playing)
db = assert (sqlite3.open( GetInfo (66) .. Trim (WorldAddress ()) .. "_" .. WorldPort () .. ".db"))
-- create rooms table
dbcheck (db:execute[[
PRAGMA foreign_keys = ON;
CREATE TABLE IF NOT EXISTS
rooms(
roomid INTEGER PRIMARY KEY AUTOINCREMENT,
uid STRING NOT NULL, -- vnum or how the MUD identifies the room
hash TEXT, -- name of room
date_added DATE, -- date added to database
UNIQUE (uid)
);
CREATE TABLE IF NOT EXISTS
exits(
exitid INTEGER PRIMARY KEY AUTOINCREMENT,
dir TEXT NOT NULL, -- direction, eg. "n", "s"
fromuid STRING NOT NULL, -- exit from which room (in rooms table)
touid STRING NOT NULL, -- exit to which room (in rooms table)
closed INTEGER, -- true if closed
locked INTEGER, -- true if locked
door INTEGER, -- true if a door
date_added DATE, -- date added to database
FOREIGN KEY(fromuid) REFERENCES rooms(uid)
);
CREATE INDEX IF NOT EXISTS fromuid_index ON exits (fromuid);
CREATE INDEX IF NOT EXISTS touid_index ON exits (touid);
]])
--[=[
-- check if rooms_lookup table exists
local table_exists
for a in db:nrows "SELECT * FROM sqlite_master WHERE name = 'rooms_lookup' AND type = 'table'" do
table_exists = true
end -- for
if not table_exists then
dbcheck (db:execute "CREATE VIRTUAL TABLE rooms_lookup USING FTS3(guid, name, desc, notes);")
end -- if
--]=]
PPI.Expose "get_room"
-- PPI.Expose "find_rooms"
end -- function OnPluginInstall
function OnPluginClose ()
-- close database
db:close()
end -- OnPluginClose
function OnPluginTelnetOption (option)
local t = {} -- incoming server variables will go into table t
setfenv (assert (loadstring (option)), t) () -- compile and load into t
--for k in pairs (t) do
-- print ("Received telnet item:", k, "in plugin", GetPluginName ())
--end -- for
-- cache incoming object information
if t.inroom then
if not room_in_database [t.inroom.uid] and next (t.inroom.contents) then
dbcheck (db:execute "BEGIN TRANSACTION;")
requested_rooms [t.inroom.uid] = nil
local time_elapsed = GetInfo (232) - (requested_rooms [k] or GetInfo (232))
Note (string.format ("RECEIVED room ID: %s in %.2f seconds.", t.inroom.uid, time_elapsed))
save_room_to_database (t.inroom.uid, t.inroom.hash, t.inroom.contents)
dbcheck (db:execute "COMMIT;")
end -- not already in database
end -- if t.room_info
end -- function OnPluginTelnetOption
function OnPluginTelnetRequest (type, data)
if type == 102 and data == "WILL" then
return true
end -- if
end -- function OnPluginTelnetRequest
]]>
</script>
</muclient>