-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdbtools.tcl
More file actions
651 lines (567 loc) · 13.4 KB
/
Copy pathdbtools.tcl
File metadata and controls
651 lines (567 loc) · 13.4 KB
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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
package require atlantis_utils
package require sqlite3
package provide atlantis_dbtools 1.0
# men will be auto-populated from db
set ::men {
}
set ::boats {
}
proc parseShips {} {
db eval {
SELECT desc
FROM object_defs
} {
set col0 [lindex $desc 0]
if {[regexp {([^:]*): This is a ship} $col0 -> name]} {
lappendU ::boats $name
}
}
}
proc getMaint {item_list} {
set maint 0
foreach i $item_list {
set abbr [string trim [lindex $i 2] {[]}]
set item_desc [dGet [db onecolumn {SELECT desc FROM items WHERE abbr=$abbr}] Desc]
if {[regexp {This race takes (.*) hits to kill} $item_desc -> hits]} {
set ct [lindex $i 0]
incr maint [expr {$ct * $hits * 5}]
}
}
return $maint
}
set ::currentTurn 0
# (database available function)
# return amount of tax revenue in hex given by 'rid'
# (capped by maxTax extracted from detail table)
proc curTax {rid maxTax} {
# ocean hexes have null maxTax
if {$maxTax eq ""} { return 0 }
# pull all the units in the region
set res [::db eval {
SELECT items,orders
FROM units
WHERE regionId=$rid
}]
# count number of men taxing
set taxers 0
foreach {il ol} $res {
if {[ordersMatch $ol "tax"] != -1} {
incr taxers [countMen $il]
}
}
# can't tax more than maxTax
return [expr min($taxers*50, $maxTax)]
}
# return a list of producers in hex given by 'rid'
# (capped by the maximums in maxProducts)
proc curProduce {rid maxProducts} {
# pull all the units in the region
set res [::db eval {
SELECT items,orders,skills
FROM units
WHERE regionId=$rid
}]
set maxProdDict [buildProductDict $maxProducts]
set ret ""
# foreach result
foreach {il ol sl} $res {
set idx [ordersMatch $ol "produce"]
if {$idx != -1} {
set o [lindex $ol $idx]
set product [string toupper [lindex $o 1]]
if {![dict exists $::production $product]} {
puts "No product $product"
}
set numMen [countMen $il]
dict set ret $product $numMen
}
}
return $maxProdDict
}
proc countItem {ils item} {
foreach il $ils {
if {[string compare -nocase [lindex $il 2] [format {[%s]} $item]] == 0} {
return [lindex $il 0]
}
if {[string compare -nocase [lindex $il 1] $item] == 0} {
return [lindex $il 0]
}
}
return 0
}
proc skillLevel {skill_list skill_name} {
set i [lsearch $skill_list [format {*%s*} $skill_name]]
if {$i == -1} { return 0 }
if {[lindex $skill_list $i 0] ne $skill_name && [lindex $skill_list $i 1] ne $skill_name} {
puts "Mismatch in skillLevel '$skill_list' '$skill_name'"
exit
}
return [lindex $skill_list $i 2]
}
proc registerFunctions {} {
::db function curTax curTax
::db function curProduce curProduce
::db function countItem countItem
::db function countMen countMen
::db function dGet dGet
::db function skillLevel skillLevel
}
proc createObjDef {} {
::db eval {
CREATE TABLE object_defs(
id INTEGER PRIMARY KEY AUTOINCREMENT,
desc TEXT not null unique on conflict replace
)
}
}
proc createDb {filename} {
if {[info exists ::db]} {
::db close
}
sqlite3 ::db $filename
# settings
::db eval {
CREATE TABLE settings(
id INTEGER PRIMARY KEY,
version INTEGER,
player_id INTEGER,
player_pass TEXT not null,
geom_top TEXT not null,
zoom_level INTEGER,
view_level INTEGER,
forSale_open INTEGER
);
}
::db eval {
INSERT INTO settings
(id, version, player_id, player_pass, geom_top, zoom_level, view_level, forSale_open)
VALUES(1, 3, 0, "", "", 0, 0, 0)
}
::db eval {
CREATE TABLE notes(
key TEXT PRIMARY KEY,
val TEXT not null
);
}
# terrain table: (x, y, z) -> terrain type, city, region name
::db eval {
CREATE TABLE terrain(
x TEXT not null,
y TEXT not null,
z TEXT not null,
type not null,
city not null,
region not null,
unique(x,y,z));
}
::db eval {
CREATE TABLE gates(
x TEXT not null,
y TEXT not null,
z TEXT not null,
desc TEXT not null,
unique(x,y,z));
}
# detailed table: (x, y, z) -> turn info gathered, wants?, sells?, weather(cur,
# next) wage(per, max)
::db eval {
CREATE TABLE detail (
id INTEGER PRIMARY KEY AUTOINCREMENT,
x TEXT not null,
y TEXT not null,
z TEXT not null,
turn INTEGER not null,
weather not null,
wages not null,
pop not null,
race not null,
tax not null,
entertainment not null,
wants not null,
sells not null,
products not null,
exitDirs not null,
unique(x,y,z,turn)
);
}
::db eval {
CREATE TABLE nexus_exits (
id INTEGER PRIMARY KEY AUTOINCREMENT,
dir not null,
dest not null,
unique(dir)
);
}
# unit table: (regionId) -> name, description, detail (own or foreign), orders
::db eval {
CREATE TABLE units (
id INTEGER PRIMARY KEY AUTOINCREMENT,
regionId INTEGER not null,
name not null,
uid INTEGER not null,
desc not null,
faction not null,
detail not null,
orders not null,
items not null,
skills not null,
flags not null,
other,
FOREIGN KEY (regionId) REFERENCES detail(id)
ON DELETE CASCADE
ON UPDATE CASCADE
);
}
# object table
::db eval {
CREATE TABLE objects (
id INTEGER PRIMARY KEY AUTOINCREMENT,
regionId INTEGER not null,
name not null,
desc not null,
flags not null,
FOREIGN KEY (regionId) REFERENCES detail(id)
ON DELETE CASCADE
ON UPDATE CASCADE
);
}
# object to unit mappings (what units are in which objects)
::db eval {
CREATE TABLE object_unit_map (
id INTEGER PRIMARY KEY AUTOINCREMENT,
objectId INTEGER not null,
unitId INTEGER not null,
FOREIGN KEY (objectId) REFERENCES objects(id)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY (unitId) REFERENCES units(id)
ON DELETE CASCADE
ON UPDATE CASCADE
);
}
# item descriptions (desc is a dict)
::db eval {
CREATE TABLE items(
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT not null,
abbr TEXT not null unique on conflict replace,
type TEXT not null,
desc TEXT not null
)
}
# skill descriptions (desc is a list)
::db eval {
CREATE TABLE skills(
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT not null,
abbr TEXT not null,
level TEXT not null,
cost TEXT not null,
desc TEXT not null
)
}
createObjDef
# active markers
::db eval {
CREATE TABLE active_markers(
x TEXT not null,
y TEXT not null,
z TEXT not null,
done not null,
unique(x,y,z)
)
}
registerFunctions
}
proc setMaxXY {} {
set maxx [::db onecolumn {SELECT val FROM notes WHERE key = "max_x"}]
if {$maxx eq ""} {
set maxx [::db onecolumn { SELECT max(cast(x as integer)) FROM terrain WHERE z=1}]
if {$maxx ne ""} {
set ::max_x [expr {$maxx + 1}]
}
} else {
set ::max_x $maxx
}
set maxy [::db onecolumn {SELECT val FROM notes WHERE key = "max_y"}]
if {$maxy eq ""} {
set maxy [::db onecolumn { SELECT max(cast(y as integer)) FROM terrain WHERE z=1}]
if {$maxy ne ""} {
set ::max_y [expr {$maxy + 1}]
}
} else {
set ::max_y $maxy
}
}
proc openDb {ofile} {
if {[info exists ::db]} {
::db close
}
sqlite3 ::db $ofile
set res [db eval {SELECT name from sqlite_master}]
if {[lsearch $res terrain] == -1 ||
[lsearch $res detail] == -1} {
::db close
unset ::db
return "Error file $ofile is invalid"
}
set version [db onecolumn {SELECT version FROM settings}]
if {$version == 1} {
createObjDef
db eval {
ALTER TABLE units
ADD COLUMN other
}
db eval {
UPDATE settings SET version = 2
}
}
if {$version == 2} {
::db eval {
CREATE TABLE gates(
x TEXT not null,
y TEXT not null,
z TEXT not null,
desc TEXT not null,
unique(x,y,z));
UPDATE settings SET version = 3
}
}
registerFunctions
set ::men [db eval {select abbr from items where type="race"}]
set ::currentTurn [db eval {select max(turn) from detail}]
parseShips
setMaxXY
return ""
}
proc insertItem {i} {
set nd [dict get $i "Name"]
regexp { *([^[]*) *\[(.*)\]} $nd -> name abbr
set type [dict get $i "Type"]
set desc [dict remove $i "Name" "Type"]
::db eval {
INSERT INTO items
(name, abbr, type, desc)
VALUES($name, $abbr, $type, $desc)
}
}
proc taxProgressDetailed {} {
return [::db eval {
SELECT x, y, z, turn, curTax(id, tax)
FROM detail
ORDER BY turn
}]
}
proc taxProgress {} {
return [::db eval {
SELECT turn, sum(curTax(id, tax))
FROM detail
GROUP BY turn
}]
}
proc getUnits {name} {
return [::db eval {
SELECT detail.x, detail.y, detail.z, units.name, units.uid
FROM detail JOIN units
ON detail.id=units.regionId
WHERE detail.turn=$::currentTurn and units.detail='own' and units.name LIKE $name
}]
}
# helper for updateDb
# process the exits field
# returns a list of all exit directions (for wall processing)
proc doExits {db exits rz} {
set dirs ""
#foreach direction and exit info
foreach {d e} $exits {
lappend dirs $d ;# save the exit direction
# pull the terrain info from the exit info
set loc [dGet $e Location]
set x [lindex $loc 0]
set y [lindex $loc 1]
set z [lindex $loc 2]
set ttype [dGet $e Terrain]
set city [dGet $e Town]
set region [dGet $e Region]
$db eval {
INSERT OR REPLACE INTO terrain VALUES
($x, $y, $z, $ttype, $city, $region);
}
if {$rz == 0} {
$db eval {
INSERT OR REPLACE INTO nexus_exits (dir, dest)
VALUES ($d, $loc);
}
}
}
return $dirs
}
proc dbInsertUnit {db regionId u} {
set name [dGet $u Name]
set desc [dGet $u Desc]
set fact [dGet $u Faction]
set detail [dGet $u Report]
set orders [dGet $u Orders]
set items [dGet $u Items]
set skills [dGet $u Skills]
set flags [dGet $u Flags]
set can_study [dGet $u CanStudy]
set combat_spell [dGet $u CombatSpell]
set other [dict create CanStudy $can_study CombatSpell $combat_spell]
set r [extractUnitNameNum $name]
set n [lindex $r 0]
set uid [lindex $r 1]
$db eval {
INSERT INTO units
(regionId, name, uid, desc, faction, detail, orders, items, skills, flags, other)
VALUES(
$regionId, $n, $uid, $desc, $fact, $detail, $orders, $items, $skills, $flags, $other
);
}
return [$db last_insert_rowid]
}
proc insertSkill {s} {
set name [dGet $s "Name"]
set abbr [dGet $s "Abbr"]
set level [dGet $s "Level"]
set cost [dGet $s "Cost"]
set desc [dGet $s "Desc"]
::db eval {
INSERT INTO skills
(name, abbr, level, cost, desc)
VALUES($name, $abbr, $level, $cost, $desc)
}
}
proc updateDb {db tdata} {
set pid [dGet $tdata PlayerNum]
set ppass [dGet $tdata PlayerPass]
db eval {
UPDATE settings SET
player_id = $pid,
player_pass = $ppass
}
set turnNo [calcTurnNo [dGet $tdata Month] [dGet $tdata Year]]
$db eval {BEGIN TRANSACTION}
set alignment [dGet $tdata Alignment]
$db eval {INSERT OR REPLACE INTO notes VALUES("alignment", $alignment)}
set regions [dGet $tdata Regions]
foreach r $regions {
set loc [dGet $r Location]
set x [lindex $loc 0]
set y [lindex $loc 1]
set z [lindex $loc 2]
set dirs [doExits $db [dGet $r Exits] $z]
set ttype [dGet $r Terrain]
set city [dGet $r Town]
set region [dGet $r Region]
$db eval {
INSERT OR REPLACE INTO terrain VALUES
($x, $y, $z, $ttype, $city, $region);
}
set gate [dGet $r Gate]
$db eval {
INSERT OR REPLACE INTO gates VALUES
($x, $y, $z, $gate);
}
set weather [list [dGet $r WeatherOld] [dGet $r WeatherNew]]
set wages [list [dGet $r Wage] [dGet $r MaxWage]]
set pop [dGet $r Population]
set race [dGet $r Race]
set tax [dGet $r MaxTax]
set ente [dGet $r Entertainment]
set wants [dGet $r Wants]
set sells [dGet $r Sells]
set prod [dGet $r Products]
$db eval {
INSERT OR REPLACE INTO detail
(x, y, z, turn, weather, wages, pop, race, tax, entertainment, wants,
sells, products, exitDirs)
VALUES(
$x, $y, $z, $turnNo, $weather, $wages, $pop, $race, $tax, $ente,
$wants, $sells, $prod, $dirs
);
}
set regionId [$db last_insert_rowid]
set units [dGet $r Units]
foreach u $units {
dbInsertUnit $db $regionId $u
}
set objects [dGet $r Objects]
foreach o $objects {
set oname [dGet $o Name]
set odesc [dGet $o ObjectName]
set oflags [dGet $o Flags]
$db eval {
INSERT OR REPLACE INTO objects
(regionId, name, desc, flags)
VALUES(
$regionId, $oname, $odesc, $oflags
)
}
set objectId [$db last_insert_rowid]
foreach u [dGet $o Units] {
set unitRow [dbInsertUnit $db $regionId $u]
$db eval {
INSERT OR REPLACE INTO object_unit_map
(objectId, unitId)
VALUES($objectId, $unitRow)
}
}
}
}
# items are a list of dict
set items [dGet $tdata Items]
foreach item $items {
insertItem $item
}
set skills [dGet $tdata Skills]
foreach skill $skills {
insertSkill $skill
}
foreach obj_def [dGet $tdata Objects] {
$db eval {
INSERT OR REPLACE INTO object_defs
(desc)
VALUES($obj_def)
}
}
$db eval {END TRANSACTION}
set unclaimed [dGet $tdata Unclaimed]
$db eval {
INSERT OR REPLACE INTO notes
VALUES("unclaimed", $unclaimed)
}
set ::men [db eval {select abbr from items where type="race"}]
set ::currentTurn [db eval {select max(turn) from detail}]
parseShips
# only store events for latest turn
if {$turnNo == $::currentTurn} {
$db eval {DROP TABLE IF EXISTS events}
$db eval {
CREATE TABLE events(
id INTEGER PRIMARY KEY,
type TEXT not null,
val TEXT not null
)
}
$db eval {BEGIN TRANSACTION}
foreach e [dGet $tdata Events] {
set t [dict get $e TYPE]
set val [dict remove $e TYPE]
$db eval {
INSERT INTO events
(type, val)
VALUES($t, $val)
}
}
foreach b [dGet $tdata Battles] {
$db eval {
INSERT INTO events
(type, val)
VALUES("BATTLE", $b)
}
}
$db eval {END TRANSACTION}
}
setMaxXY
}