forked from nedbrek/TrueAtlanteans
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dbtools.tcl
457 lines (397 loc) · 9.41 KB
/
dbtools.tcl
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
package require atlantis_utils
package require sqlite3
package provide atlantis_dbtools 1.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 {[lindex $il 2] eq [format {[%s]} $item]} {
return [lindex $il 0]
}
}
return 0
}
proc registerFunctions {} {
::db function curTax curTax
::db function curProduce curProduce
::db function countItem countItem
::db function countMen countMen
}
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, 1, 0, "", "", 0, 0, 0)
}
# 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));
}
# 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,
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,
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
)
}
# 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 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"
}
registerFunctions
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=$gui::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 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)
VALUES(
$regionId, $n, $uid, $desc, $fact, $detail, $orders, $items, $skills, $flags
);
}
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 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 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]
$db eval {
INSERT OR REPLACE INTO objects
(regionId, name, desc)
VALUES(
$regionId, $oname, $odesc
)
}
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
}
$db eval {END TRANSACTION}
}