-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathCreate_ATCP_Map_Database.xml
373 lines (292 loc) · 9.58 KB
/
Create_ATCP_Map_Database.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>
<muclient>
<plugin
name="Create_ATCP_Map_Database"
author="Nick Gammon"
id="d91aff8af0ad44c3910394ce"
language="Lua"
purpose="Creates the ATCP Map database from an XML file"
save_state="y"
date_written="2010-03-04 09:10:51"
requires="4.50"
version="1.0"
>
<description trim="y">
<![CDATA[
Type "create map database" to run.
]]>
</description>
</plugin>
<aliases>
<alias
script="create_map_database"
match="create map database"
enabled="y"
sequence="100"
>
</alias>
</aliases>
<!-- Script -->
<script>
<![CDATA[
count = 0
roomcount = 0
endroomcount = 0
areacount = 0
coordcount = 0
directioncount = 0
environmentcount = 0
entities = { ['&'] = '&';
['"'] = '"';
['<'] = '<';
['>'] = '>';
}
directions = {}
convert_direction = {
north = "n",
south = "s",
east = "e",
west = "w",
up = "u",
down = "d",
northeast = "ne",
northwest = "nw",
southeast = "se",
southwest = "sw",
['in'] = "in",
out = "out",
}
function process_area (args)
if not args.id and args.name then
return "Need id and name"
end -- if
areacount = areacount + 1
dbcheck (db:execute (string.format (
"INSERT INTO areas (uid, name, date_added) VALUES (%s, %s, DATETIME('NOW'));",
fixsql (args.id),
fixsql (args.name)
)))
end -- process_area
function process_environment (args)
if not args.id and args.name and args.color then
return "Need id, name, color"
end -- if
environmentcount = environmentcount + 1
dbcheck (db:execute (string.format (
"INSERT INTO environments (uid, name, color, date_added) VALUES (%s, %s, %i, DATETIME('NOW'));",
fixsql (args.id),
fixsql (args.name),
args.color
)))
end -- process_environment
function process_info (args)
if not args.flags then
return "Need flags"
end -- if
info = args.flags
end -- process_environment
function process_start_room (args)
if not args.id and args.area and args.title and args.environment then
return "Need id, area, title, environment"
end -- if
roomcount = roomcount + 1
SetStatus ("Processing room " .. args.id .. " (" .. roomcount .. ") - " .. args.title )
room = { id = args.id, area = args.area, title = args.title, terrain = args.environment }
exits = {}
coords = {}
info = nil
end -- process_start_room
function process_end_room ()
endroomcount = endroomcount + 1
local building = coords.building
local x, y, z = coords.x or 0, coords.y or 0, coords.z or 0
dbcheck (db:execute (string.format (
"INSERT INTO rooms (uid, name, area, terrain, building, info, x, y, z, date_added) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, DATETIME('NOW'));",
fixsql (room.id),
fixsql (room.title),
fixsql (room.area),
fixsql (room.terrain),
fixsql (building),
fixsql (info),
fixsql (coords.x),
fixsql (coords.y),
fixsql (coords.z)
)))
for dir, exit in pairs (exits) do
dbcheck (db:execute (string.format ([[
INSERT INTO exits (dir, fromuid, touid, date_added)
VALUES (%s, %s, %s, DATETIME('NOW'));
]], fixsql (dir), -- direction (eg. "n")
fixsql (room.id), -- from current room
fixsql (exit) -- destination room
)))
end -- for each exit
dbcheck (db:execute (string.format ([[
INSERT INTO rooms_lookup (uid, name) VALUES (%s, %s);
]], fixsql (room.id),
fixsql (room.title)
)))
end -- process_end_room
function process_coord (args)
coordcount = coordcount + 1
coords = { x = args.x, y = args.y, z = args.z, building = args.building, area = args.area }
end -- process_coord
function process_exit (args)
if not args.direction and args.target then
return "Need direction and target"
end -- if
directioncount = directioncount + 1
if not convert_direction [args.direction] then
print ("Don't know of direction", args.direction)
end -- if
directions [args.direction] = (directions [args.direction] or 0) + 1
exits [(convert_direction [args.direction]) or args.direction] = args.target
end -- process_exit
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 ignore (args)
-- ignore this tag
end -- ignore
handlers = {
areas = ignore,
map = ignore,
rooms = ignore,
environments = ignore,
["/areas"] = ignore,
["/rooms"] = ignore,
["/map"] = ignore,
["/environments"] = ignore,
area = process_area,
room = process_start_room,
coord = process_coord,
environment = process_environment,
exit = process_exit,
info = process_info,
["/room"] = process_end_room,
}
function get_params (s)
local t = {}
for name, contents in string.gmatch (s, '(%a+)="([^"]*)"') do
t [name] = string.gsub (contents, '&%a-;', entities)
end -- for
return t
end -- get_params
function create_map_database (name, line, wildcards)
local filename = utils.filepicker ("Mapper database", "", "xml", { xml = "XML files", ["*"] = "All files" }, false)
if not filename then
return -- they cancelled
end
-- open database on disk
db = assert (sqlite3.open(GetInfo (66) .. Trim (WorldAddress ()) .. "_" .. WorldPort () .. ".db"))
-- create rooms table
dbcheck (db:execute[[
PRAGMA foreign_keys = ON;
DROP TABLE IF EXISTS exits;
DROP TABLE IF EXISTS rooms;
DROP TABLE IF EXISTS areas;
DROP TABLE IF EXISTS environments;
CREATE TABLE areas (
areaid INTEGER PRIMARY KEY AUTOINCREMENT,
uid TEXT NOT NULL, -- vnum or how the MUD identifies the area
name TEXT, -- name of area
date_added DATE, -- date added to database
UNIQUE (uid)
);
CREATE TABLE environments (
environmentid INTEGER PRIMARY KEY AUTOINCREMENT,
uid TEXT NOT NULL, -- code for the environment
name TEXT, -- name of environment
color INTEGER, -- ANSI colour code
date_added DATE, -- date added to database
UNIQUE (uid)
);
CREATE INDEX IF NOT EXISTS name_index ON environments (name);
CREATE TABLE rooms (
roomid INTEGER PRIMARY KEY AUTOINCREMENT,
uid TEXT NOT NULL, -- vnum or how the MUD identifies the room
name TEXT, -- name of room
area TEXT, -- which area
building TEXT, -- which building it is in
terrain TEXT, -- eg. road OR water
info TEXT, -- eg. shop,postoffice
notes TEXT, -- player notes
x INTEGER,
y INTEGER,
z INTEGER,
date_added DATE, -- date added to database
UNIQUE (uid)
);
CREATE INDEX IF NOT EXISTS info_index ON rooms (info);
CREATE INDEX IF NOT EXISTS terrain_index ON rooms (terrain);
CREATE INDEX IF NOT EXISTS area_index ON rooms (area);
CREATE TABLE 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)
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);
DROP TABLE IF EXISTS rooms_lookup;
CREATE VIRTUAL TABLE rooms_lookup USING FTS3(uid, name);
]])
db:exec ("BEGIN TRANSACTION;")
for line in io.lines (filename) do
local xml = string.match (line, "^%s*<(.-)/?>%s*$")
if xml then
local tag, things = string.match (xml, "^([%a/]+)%s*(.*)$")
if tag then
local f = handlers [tag]
if f then
local t = get_params (things)
local problem = f (t)
if problem then
print ("Error (" .. problem .. ") processing XML:", xml)
end -- if
else
print ("unprocessed line:", xml)
end -- if tag not known
count = count + 1
else
print ("No tag found on line:", xml)
end -- if tag
else
print ("Line not processed:", line)
end -- if xml
end -- for each line
db:exec ("COMMIT;")
print ("Found", count, "xml lines")
print ("Found", areacount, "areas")
print ("Found", roomcount, "rooms")
print ("Found", endroomcount, "/rooms")
print ("Found", coordcount, "coordinates")
print ("Found", directioncount, "directions")
print ("Found", environmentcount, "environments")
end -- function create_map_database
]]>
</script>
</muclient>