-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanage_database.py
365 lines (320 loc) · 14.7 KB
/
manage_database.py
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
#!/usr/bin/env python
"""
Database management for Carcassonne score keeping system.
Copyright 2018 George C. Privon
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import sys
import os
import argparse
import configparser
import re
import sqlite3
def loadConfig(cfile):
"""
Load configuration file
"""
if not os.path.isfile(cfile):
sys.stderr.write("Error: could not find configuration file '" + cfile + "'\n")
if os.path.isfile('CarcassonneScore.conf.example'):
sys.stderr.write("Copying 'CarcassonneScore.conf.example' to 'CarcassonneScore.conf'.\n")
import shutil
shutil.copyfile('CarcassonneScore.conf.example',
'CarcassonneScore.conf')
else:
sys.stderr.write("Error: cannot find default configuration file 'CarcassonneScore.conf.example' to copy.\n")
config = configparser.RawConfigParser()
config.read(cfile)
return config
def parseArgs():
"""
Command line arguments
"""
parser = argparse.ArgumentParser(description="Update the Carcassonne \
scoring database.")
parser.add_argument('-c', '--config', default='CarcassonneScore.conf',
help='Location of the configuration file.')
cmds = parser.add_mutually_exclusive_group(required=True)
cmds.add_argument('--init', default=False, action='store_true',
help='Create a fresh database.')
cmds.add_argument('-n', '--newplayer', type=str, default=None,
nargs='+',
help='Add a new player.')
cmds.add_argument('-e', '--enableexpansion', action='store_true',
default=False,
help='Enable an expansion.')
cmds.add_argument('-d', '--disableexpansion', action='store_true',
default=False,
help='Disable an expansion.')
return parser.parse_args()
def initializeDB(c, DBVER):
"""
Initialize Database
"""
# player table
c.execute('''CREATE TABLE players (playerID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
name TEXT NOT NULL DEFAULT "")''')
# player names
c.execute('''INSERT INTO players values (0,
"John Smith")''')
c.execute('''INSERT INTO players values (1,
"Jane Doe")''')
# games table
# gameID - unique ID
# location - free text
# starttime - timecode
# endtime - timecode
# expansions - comma-separated list of expansions used (by expansionID)
c.execute('''CREATE TABLE games (gameID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
location TEXT NOT NULL DEFAULT "",
starttime TEXT NOT NULL DEFAULT "",
endtime TEXT NOT NULL DEFAULT "",
expansions TEXT NOT NULL DEFAULT "")''')
# turns table
# gameID - corresponds to games table
# turnNum - incremented turn number (corresponds to a tile placement)
# time - time at which the placement was made
# builder - 0 if tile was placed as part of normal turn, 1 if placed
# as a result of a builder token
# playerID - player who made the turn
c.execute('''CREATE TABLE turns (gameID INTEGER NOT NULL,
turnNum INTEGER NOT NULL,
time TEXT NOT NULL,
builder INTEGER NOT NULL,
playerID INTEGER NOT NULL)''')
# individual scores
# gameID - unique per game
# playerID - corresponds to entry from players table
# turnNum - turn number (corresponds to number in turns table)
# scoreID - ID number for the score (unique to game?)
# ingame - 1 if scored during regular play, 0 if scored after tiles are gone
# points - number of points awarded
# scoretype - road, city, meadow, etc.
# sharedscore - was this score shared with another player?
# token - token(s) used for score (e.g., meeple, wagon, pig)
# extras - any other items (e.g., trade goods)
# comments - text annotation
c.execute('''CREATE TABLE scores (gameID INTEGER NOT NULL,
playerID INTEGER NOT NULL,
turnNum INTEGER NOT NULL,
scoreID INTEGER NOT NULL,
ingame INTEGER NOT NULL,
points INTEGER NOT NULL,
scoretype TEXT NOT NULL,
sharedscore INTEGER NOT NULL,
token TEXT NOT NULL,
extras NOT NULL,
comments TEXT)''')
# list of expansions
# mini - 1 if a mini expanion, otherwise 0
# active - 1 if it can be played, otherwise 0
# tokens - additional tokens provided beyond the base game
# scoretypes - additional classes of scoring enabled
# Ntiles - number of tiles added
# tiletypes - special scorable tiles added
c.execute('''CREATE TABLE expansions (expansionID INTEGER PRIMARY KEY,
name TEXT NOT NULL,
mini INTEGER,
active INTEGER,
tokens TEXT,
scoretypes TEXT,
Ntiles INTEGER,
tiletypes TEXT)''')
# large expansions
# taken from http://carcassonne.wikia.com/wiki/Official_expansions
# Only 1, 2, and 5 are currently populated
c.execute('''INSERT INTO expansions values (1,
"Inns & Cathedrals",
0,
1,
"BigMeeple",
"",
18,
"Cathedral,InnonLake")''')
c.execute('''INSERT INTO expansions values (2,
"Traders & Builders",
0,
1,
"Pig,Builder",
"",
24,
"TradeGoods")''')
c.execute('''INSERT INTO expansions values (3,
"Princess & Dragon",
0,
0,
"Dragon,Fairy",
"",
30,
"")''')
c.execute('''INSERT INTO expansions values (4,
"The Tower",
0,
0,
"",
"",
0,
"")''')
c.execute('''INSERT INTO expansions values (5,
"Abbey & Mayor",
0,
1,
"Mayor,Wagon,Barn",
"Abbey",
12,
"Abbey")''')
c.execute('''INSERT INTO expansions values (6,
"Count, King & Robber",
0,
0,
"",
"",
0,
"")''')
c.execute('''INSERT INTO expansions values (7,
"The Catapult",
0,
0,
"",
"",
0,
"")''')
c.execute('''INSERT INTO expansions values (8,
"Bridges, Castles & Bazaars",
0,
0,
"",
"",
0,
"")''')
c.execute('''INSERT INTO expansions values (9,
"Hills & Sheep",
0,
0,
"Shepherd",
"Flock",
18,
"Hill,Vineyard")''')
c.execute('''INSERT INTO expansions values (10,
"Under the Big Top",
0,
0,
"",
"",
0,
"")''')
#mini expansions
c.execute('''INSERT INTO expansions values(101,
"The River",
1,
1,
"",
"",
12,
"")''')
c.execute('''INSERT INTO expansions values(102,
"The Abbott",
1,
1,
"Abbott",
"Garden",
0,
"")''')
c.execute('PRAGMA user_version={0:1.0f}'.format(DBVER))
def getExpans(cur, active=True):
"""
Get a list of (in)active expansions
"""
command = 'SELECT expansionID,name FROM expansions WHERE active='
if active:
command = command + '1'
else:
command = command + '0'
return cur.execute(command).fetchall()
def toggleExpan(cur, ID, activate=True):
"""
Change the active status for an expansion
"""
command = 'UPDATE expansions SET active='
if activate:
command = command + '1'
elif not activate:
command = command + '0'
else:
sys.stderr.write("What have you done?\n")
sys.exit(-1)
command = command + ' where expansionID={0:1.0f}'.format(ID)
cur.execute(command)
def main():
"""
main routine
"""
args = parseArgs()
copts = loadConfig(args.config)
DBNAME = 'CarcassonneScore.db'
DBVER = 0
if args.init and os.path.isfile(DBNAME):
sys.stderr.write("Error: '" + DBNAME + "' already exists. Exiting.\n")
sys.exit()
conn = sqlite3.connect(DBNAME)
cur = conn.cursor()
VALID = False
if args.init:
initializeDB(cur, DBVER)
elif args.newplayer:
pname = ' '.join(args.newplayer)
sys.stdout.write("Adding new player: " + pname)
sys.stdout.write(" to the database.\n")
while not VALID:
ans = input("Is this correct (y/n)? ")
if re.match('y', ans, re.IGNORECASE):
cur.execute('INSERT INTO players (name) VALUES ("' + \
pname + '")')
sys.stdout.write(pname + ' added to the database.\n')
VALID = True
elif re.match('n', ans, re.IGNORECASE):
sys.stdout.write('Canceling.\n')
VALID = True
elif args.enableexpansion:
expans = getExpans(cur, active=False)
kwds = ("enable", "inactive")
activate = True
elif args.disableexpansion:
expans = getExpans(cur, active=True)
kwds = ("disable", "active")
activate = False
if args.enableexpansion or args.disableexpansion:
expnumlist = []
for expan in expans:
expnumlist.append(int(expan[0]))
sys.stdout.write("{0:1.0f}) ".format(expan[0]) + expan[1] + "\n")
try:
toggleexp = input("Please enter the " + kwds[1] + ' expansion to ' + kwds[0] + ': ')
if not list:
sys.stderr.write("No expansions specified. Exiting.\n")
except (EOFError, KeyboardInterrupt):
conn.close()
sys.exit()
#TODO actually update the expansions
try:
toggleexp = int(toggleexp)
except:
sys.stderr.write("Error: invalid input. Please enter a number from the list next time.\n")
sys.exit()
if toggleexp not in expnumlist:
sys.stderr.write("Error: invalid input. Please enter a number from the list next time.\n")
sys.exit()
toggleExpan(cur, toggleexp, activate=activate)
conn.commit()
conn.close()
if __name__ == "__main__":
main()