Source

AdaCode / warpdb.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>
<!-- Saved on Monday, December 03, 2012, 12:00 PM -->
<!-- MuClient version 4.77 -->

<!-- Plugin "WarpDB" generated by Plugin Wizard -->

<muclient>
<plugin
   name="WarpDB"
   author="Ada"
   id="ae5f1227fd39d40fa64beadf"
   language="Python"
   purpose="Warp Database"
   date_written="2012-12-30 12:00:00"
   requires="4.62"
   version="1.0"
   >
<description trim="y">
<![CDATA[
I'm a serpent and I often wonder which warp to take to reach a given area from a given area. This plugin addresses the problem with a warp database which may be looked up for such assistance. Have a look at the available aliases to interact with it. I'll add the ability to work with ATCP/GMCP information, warps in the wilderness and open some possibility to work with a mapper in later versions.

This plugin is written in Python, please see this page to setup Python on your computer: http://adayoung.bitbucket.org/pythonsqlite.html

The following aliases are available:

    WarpDB:sources - Check for warps in your area and add them to database

    WarpDB:endpoints - Display a list of all areas you can reach from your location
    WarpDB:toarea <area name> - Check for warps leading to the specified area from your location

    WarpDB:endpoints <area name> - Display a list of all areas you can reach from the specified area

    WarpDB:export [<area name>] - Export all outgoing warps optionally specifying an area
    WarpDB:import - Import a list of warps exported by the above alias

    WarpDB:orphans - Display a list of outgoing warps from your location who's destination area is not in the database

The plugin will ask you for a file location on first load, just point it wherever you want your database file to be created. If you get "OperationalError: database is locked" error while trying to add an entry, please delete the .journal file wherever you have your database file to resolve the issue.
]]>
</description>

</plugin>

<!--  Get our standard constants -->

<include name="constants.pys"/>

<!--  Triggers  -->

<triggers>
  <trigger
   group="WarpDB"
   enabled="n"
   keep_evaluating="y"
   match="^You close your eyes and seek out all wormhole sources\.$"
   name="CollectWarps"
   regexp="y"
   send_to="12"
   sequence="900"
  >
  <send>
world.EnableTrigger("WarpLine", True)
world.EnableTrigger("PromptCaptureWarpDB", True)
warp_list = []
  </send>
  </trigger>
  <trigger
   group="WarpDB"
   enabled="n"
   keep_evaluating="y"
   match="^You sense a connection between (.+)\.$"
   name="WarpLine"
   regexp="y"
   send_to="12"
   sequence="900"
  >
  <send>
WarpLine("%1")
  </send>
  </trigger>
  <trigger
   group="WarpDB"
   enabled="n"
   keep_evaluating="y"
   match="^\d+h(, \d+m)?(, \d+e)?(, \d+w)? [@cexkdb]*(?: Vote)?-"
   name="PromptCaptureWarpDB"
   regexp="y"
   send_to="12"
   sequence="900"
  >
  <send>
areaname = ""
world.EnableTrigger("WarpLine", False)
world.EnableTrigger("CollectWarps", False)
world.EnableTrigger("PromptCaptureWarpDB", False)
world.EnableTrigger("SurveyLine", True)
world.EnableTrigger("SurveyEnd", True)
world.Send("SURVEY")
  </send>
  </trigger>
  <trigger
   group="WarpDB"
   enabled="n"
   keep_evaluating="y"
   match="^You discern that you are in (.+)\.$"
   name="SurveyLine"
   regexp="y"
   send_to="12"
   sequence="900"
  >
  <send>
areaname = "%1"
  </send>
  </trigger>
  <trigger
   group="WarpDB"
   enabled="n"
   keep_evaluating="y"
   match="^Your environment conforms to that of (.+)\.$"
   name="SurveyEnd"
   regexp="y"
   send_to="12"
   sequence="900"
  >
  <send>
world.EnableTrigger("SurveyLine", False)
world.EnableTrigger("SurveyEnd", False)
if len(areaname) > 0:
    ProcessWarps(areaname)
else:
    Note("WarpDB: Will not manage warps in the wilderness!")
  </send>
  </trigger>
  <trigger
   group="WarpDB"
   enabled="n"
   keep_evaluating="y"
   match="^Your environment conforms to that of (.+)\.$"
   name="SurveyEndPoint"
   regexp="y"
   send_to="12"
   sequence="900"
  >
  <send>
world.EnableTrigger("SurveyLine", False)
world.EnableTrigger("SurveyEndPoint", False)
DisplayEndPoints(EndPoints(areaname))
  </send>
  </trigger>
  <trigger
   group="WarpDB"
   enabled="n"
   keep_evaluating="y"
   match="^Your environment conforms to that of (.+)\.$"
   name="SurveyEndToArea"
   regexp="y"
   send_to="12"
   sequence="900"
  >
  <send>
world.EnableTrigger("SurveyLine", False)
world.EnableTrigger("SurveyEndToArea", False)
WarpsTo(EndPoints(areaname), toarea)
  </send>
  </trigger>
  <trigger
   group="WarpDB"
   enabled="n"
   keep_evaluating="y"
   match="^Your environment conforms to that of (.+)\.$"
   name="SurveyEndOrphans"
   regexp="y"
   send_to="12"
   sequence="900"
  >
  <send>
world.EnableTrigger("SurveyLine", False)
world.EnableTrigger("SurveyEndOrphans", False)
DisplayOrphans(areaname)
  </send>
  </trigger>
</triggers>

<!--  Aliases  -->

<aliases>
  <alias
   match="^WarpDB:sources$"
   enabled="y"
   group="WarpDB"
   regexp="y"
   send_to="12"
   ignore_case="y"
   sequence="100"
  >
  <send>
world.EnableTrigger("CollectWarps", True)
world.Send("WORM SOURCES")
  </send>
  </alias>
  <alias
   match="^WarpDB:endpoints$"
   enabled="y"
   group="WarpDB"
   regexp="y"
   send_to="12"
   ignore_case="y"
   sequence="100"
  >
  <send>
world.EnableTrigger("SurveyLine", True)
world.EnableTrigger("SurveyEndPoint", True)
world.Send("SURVEY")
  </send>
  </alias>
  <alias
   match="^WarpDB:toarea (.+)$"
   enabled="y"
   group="WarpDB"
   regexp="y"
   send_to="12"
   ignore_case="y"
   sequence="100"
  >
  <send>
toarea = "%1"
world.EnableTrigger("SurveyLine", True)
world.EnableTrigger("SurveyEndToArea", True)
world.Send("SURVEY")
  </send>
  </alias>
  <alias
   match="^WarpDB:orphans$"
   enabled="y"
   group="WarpDB"
   regexp="y"
   send_to="12"
   ignore_case="y"
   sequence="100"
  >
  <send>
world.EnableTrigger("SurveyLine", True)
world.EnableTrigger("SurveyEndOrphans", True)
world.Send("SURVEY")
  </send>
  </alias>
  <alias
   match="^WarpDB:endpoints (.+)$"
   enabled="y"
   group="WarpDB"
   regexp="y"
   send_to="12"
   ignore_case="y"
   sequence="100"
  >
  <send>
OtherEndPoints("%1")
  </send>
  </alias>
  <alias
   match="^WarpDB:export (.+)$"
   enabled="y"
   group="WarpDB"
   regexp="y"
   send_to="12"
   ignore_case="y"
   sequence="100"
  >
  <send>
DumpData("%1")
  </send>
  </alias>
  <alias
   match="^WarpDB:export$"
   enabled="y"
   group="WarpDB"
   regexp="y"
   send_to="12"
   ignore_case="y"
   sequence="100"
  >
  <send>
DumpDataFull()
  </send>
  </alias>
  <alias
   match="^WarpDB:import$"
   enabled="y"
   group="WarpDB"
   regexp="y"
   send_to="12"
   ignore_case="y"
   sequence="100"
  >
  <send>
LoadData()
  </send>
  </alias>
  <alias
   match="^WarpDB:test (.*?)$"
   enabled="y"
   group="WarpDB"
   regexp="y"
   send_to="12"
   ignore_case="y"
   sequence="100"
  >
  <send>exec("%1")</send>
  </alias>
</aliases>

<!--  Script  -->

<script>
<![CDATA[
import re
import sqlite3
from datetime import datetime, timedelta

import Tkinter
root = Tkinter.Tk()
root.withdraw() # to hide the Tk GUI window as per http://stackoverflow.com/a/1796726

from tkFileDialog import asksaveasfilename, askopenfilename

def Note(message): # not writing world.Note everywhere ^_^
    world.Note(message)

DBFile = world.GetVariable("dbpath")
if not DBFile:
    while not (DBFile and len(DBFile) > 0):
        DBFile = asksaveasfilename(defaultextension=".db", filetypes=[("SQLite3 database file", "*.db")], title="Specify the location of your new database.")
    world.SetVariable("dbpath", DBFile)
    world.SaveState

db = sqlite3.connect(DBFile)
cursor = db.cursor()

def createdb():
    cursor.execute("PRAGMA foreign_keys=ON")
    cursor.execute("""
    CREATE TABLE IF NOT EXISTS arealist (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        areaname TEXT UNIQUE
    )""")
    cursor.execute("""
    CREATE INDEX IF NOT EXISTS area_index ON arealist (areaname)
    """)
    cursor.execute("""
    CREATE TABLE IF NOT EXISTS warplist (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        source TEXT,
        destination TEXT,
        source_areaid INTEGER REFERENCES arealist(id) ON DELETE CASCADE
    )""")
    cursor.execute("""
    CREATE INDEX IF NOT EXISTS warp_index ON warplist (source, destination, source_areaid)
    """)
    db.commit()

createdb()

areaname = ""
warp_list = []

def WarpLine(warpline):
    warp_list_ambiguous = []
    warpline = warpline.replace("(house)", "")
    room_names = warpline.split('and')
    if not len(room_names) > 2:
        warp_list.append((room_names[0].strip(), room_names[1].strip()))
    else:
        warp_list_ambiguous.append(warpline) # do something about this

def ProcessWarps(areaname):
    cursor.execute("DELETE FROM arealist WHERE areaname=?", (areaname, ))
    cursor.execute("INSERT INTO arealist (areaname) VALUES (?)", (areaname, ))
    cursor.execute("SELECT last_insert_rowid()")
    areaid = cursor.fetchone()[0]
    for source, destination in warp_list:
        cursor.execute("INSERT INTO warplist (source, destination, source_areaid) VALUES (?, ?, ?)", (source, destination, areaid))
    db.commit()

def EndPoints(areaname):
    """
    Display endpoints from areaname
    """
    cursor.execute("SELECT id FROM arealist WHERE areaname=?", (areaname, ))
    results = cursor.fetchall()
    if len(results) > 0:
        areaid = results[0][0]
    else:
        Note("WarpDB: No warps have been recorded for this area, try WarpDB:sources here.")
        return
    cursor.execute("SELECT DISTINCT b1.source, b1.destination, b2.source_areaid FROM warplist as b1 JOIN warplist as b2 ON b1.source=b2.destination WHERE b1.source_areaid=?", (areaid, ))
    return cursor.fetchall()

def DisplayEndPoints(results):
    if len(results) > 0:
        Note("The following recorded areas are reachable from [%s]:" % areaname)
        line_len = len("The following recorded areas are reachable from [%s]:" % areaname)
        Note("-" * line_len)
        for source, destination, areaid in results:
            cursor.execute("SELECT areaname FROM arealist WHERE id=?", (areaid, ))
            dest_area = cursor.fetchone()[0]
            Note("%s -> %s [%s]" % (source, destination, dest_area))
        Note("-" * line_len)
    else:
        Note("WarpDB: No areas are reachable from this area, try WarpDB:sources here and in the destination area to link the two.")

def ValidateArea(areaname):
    cursor.execute("SELECT areaname FROM arealist WHERE areaname LIKE ?", ("%" + areaname + "%", ))
    areanames = cursor.fetchall()
    if len(areanames) > 1:
        Note("Oops, multiple areas match the supplied input:")
        for i in areanames:
            Note(i[0])
    elif len(areanames) < 1:
        Note("No matching areas found :(")
    else:
        areaname = areanames[0][0]
        return areaname
    return None

def WarpsTo(resultset, areaname):
    areaname = ValidateArea(areaname)
    if areaname is None: # bail out
        return

    warp_list = []
    if len(resultset) > 0:
        for source, destination, areaid in resultset:
            cursor.execute("SELECT areaname FROM arealist WHERE id=?", (areaid, ))
            dest_area = cursor.fetchone()[0]
            if dest_area == areaname:
                warp_list.append((source, destination))

    if len(warp_list) > 0:
        Note("The following warps lead from here to [%s]:" % areaname)
        line_len = len("The following warps lead to [%s]:" % areaname)
        Note("-" * (line_len + 10)) # O_o
        for source, destination in warp_list:
            Note("%s -> %s" % (source, destination))
        Note("-" * (line_len + 10)) # O_o
    else:
        Note("WarpDB: No recorded warps lead from here to [%s], try WarpDB:sources here and in the destination area to link the two." % areaname)

def OtherEndPoints(o_areaname):
    o_areaname = ValidateArea(o_areaname)
    if o_areaname is None: # bail out
        return

    global areaname
    areaname = o_areaname
    DisplayEndPoints(EndPoints(o_areaname))

def DisplayOrphans(areaname):
    cursor.execute("SELECT id FROM arealist WHERE areaname=?", (areaname, ))
    results = cursor.fetchall()
    if not len(results) > 0:
        Note("WarpDB: No warps have been recorded in this area, try WarpDB:sources to begin.")
        return
    else:
        areaid = results[0][0]

    warp_pairs = []
    orphan_pairs = {}
    endpoints = EndPoints(areaname)
    for source, destination, source_areaid in endpoints:
        warp_pairs.append((source, destination))
    cursor.execute("SELECT source, destination FROM warplist WHERE source_areaid=?", (areaid, ))
    results = cursor.fetchall()
    for i in results:
        if not i in warp_pairs:
            orphan_pairs[i] = ""
    orphan_pairs = orphan_pairs.keys()
    if len(orphan_pairs) > 0:
        Note("The following warps lead to places which are not part of the database yet:")
        Note("--------------------------------------------------------------------------")
        for source, destination in orphan_pairs:
            Note("%s -> %s" % (source, destination))
        Note("--------------------------------------------------------------------------")
    else:
        Note("WarpDB: No orphan warps present in the area, use WarpDB:sources and WarpDB:endpoints to verify.")

def DumpData(areaname):
    areaname = ValidateArea(areaname)
    if areaname is None: # bail out
        return

    filename = asksaveasfilename(defaultextension=".pickle", filetypes=[("WarpDB Pickle", "*.pickle")])
    if filename is '': # bail out
        Note("WarpDB: Export cancelled by user.")
        return

    dataset = {areaname: []}
    cursor.execute("SELECT source, destination FROM warplist w JOIN arealist a ON w.source_areaid=a.id WHERE areaname=?", (areaname, ))
    results = cursor.fetchall()
    for source, destination in results:
        dataset[areaname].append((source, destination))

    import cPickle
    with open(filename, 'w') as dump_file:
        cPickle.dump(dataset, dump_file)
    Note("Exported a total of %d objects." % len(dataset[areaname]))

def DumpDataFull():
    filename = asksaveasfilename(defaultextension=".pickle", filetypes=[("WarpDB Pickle", "*.pickle")])
    if filename is '': # bail out
        Note("WarpDB: Export cancelled by user.")
        return

    dataset = {}
    cursor.execute("SELECT areaname FROM arealist")
    results = cursor.fetchall()
    for areaname in results:
        dataset[areaname[0]] = []
    cursor.execute("SELECT source, destination, areaname FROM warplist w JOIN arealist a ON w.source_areaid=a.id")
    results = cursor.fetchall()
    for source, destination, areaname in results:
        dataset[areaname].append((source, destination))

    import cPickle
    with open(filename, 'w') as dump_file:
        cPickle.dump(dataset, dump_file)
    Note("Exported a total of %d compound objects." % len(dataset))

def LoadData():
    filename = askopenfilename(defaultextension=".pickle", filetypes=[("WarpDB Pickle", "*.pickle")])
    if filename is '': # bail out
        Note("WarpDB: Import cancelled by user.")
        return

    import cPickle
    with open(filename, 'r') as load_file:
        try:
            dataset = cPickle.load(load_file)
        except:
            import sys
            Note(str(sys.exc_info()))
            Note("WarpDB: Invalid file or the file does not contain a WarpDB pickle object.")
            return

    try:
        for areaname in dataset.keys():
            cursor.execute("SELECT id FROM arealist WHERE areaname=?", (areaname, ))
            results = cursor.fetchall()
            if len(results) > 0:
                areaid = results[0][0]
            else:
                cursor.execute("INSERT INTO arealist (areaname) VALUES (?)", (areaname, ))
                cursor.execute("SELECT last_insert_rowid()")
                areaid = cursor.fetchone()[0]
            import_set = []
            for source, destination in dataset[areaname]:
                import_set.append((source, destination, areaid))
            cursor.executemany("""
            INSERT INTO warplist (source, destination, source_areaid) VALUES (?, ?, ?)
            """, import_set)
        db.commit()
        Note("WarpDB: Imported a total of %d compound objects." % len(dataset))
    except sqlite3.Error as e:
        Note("SQLite error: {0}".format(e.message))
    except:
        import sys
        Note(str(sys.exc_info()))
        Note("WarpDB: Invalid file or the file does not contain a WarpDB pickle object.")
]]>
</script>

<!--  Plugin help  -->

<aliases>
  <alias
   script="OnHelp"
   match="WarpDB:help"
   ignore_case="y"
   enabled="y"
  >
  </alias>
</aliases>

<script>
<![CDATA[
def OnHelp (a, b, c):
    world.note (world.GetPluginInfo (world.GetPluginID, 3))
]]>
</script>

</muclient>