-
Notifications
You must be signed in to change notification settings - Fork 0
/
afall.py
1399 lines (1237 loc) · 45.4 KB
/
afall.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
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
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
import sys
import mysql.connector
import random
import re
import afal_config
import os
############################## begin startup code here
conn = mysql.connector.connect(**afal_config.config)
conn.get_warnings = True
cur = conn.cursor()
auction = 'Auction'
sell = 'Sell'
destroyed = 'destroyed'
outside = 'Someone'
party = 'AFAL'
special = (auction, sell, destroyed, outside)
######## Cleanup and misc code
def commit():
conn.commit()
def fini():
"""finish things up and close the database"""
conn.commit()
cur.close()
conn.close()
# if may is None, don't check for extra keys at all
def check_dict(d, must = None, may = None):
""" dict must contain every element in 'must', and may contain elements in 'may'"""
if must is None:
must = {}
if may is not None:
for i in d:
if i not in must and i not in may:
sys.stderr.write("\nUnexpected key '{i}' found in dict {d} only expected must={must}, may={may}\n".format(i=i,d=d,must=must,may=may))
raise NameError("Unexpected key " + i)
for i in must:
if not i in d:
sys.stderr.write("\nMissing key '{i}' in dict {d} expected must={must}\n".format(i=i,d=d,must=must))
raise NameError("Missing key " + i)
def _ins_if_not(d, l):
for k in l:
if k not in d:
d[k] = None
def _check_warnings():
warnings = cur.fetchwarnings()
if warnings:
sys.stderr.write("\nWarnings!: {warnings}\n".format(warnings=warnings))
######## Formatted output
def pw(*args, **kwargs):
"""print-wrapped, which is like print, except linewrapps as appropriate"""
eol = kwargs.get('end', '\n')
asep = kwargs.get('sep', ' ')
m = kwargs.get('max',None)
indent = kwargs.get('indent',0) * ' '
prefix = kwargs.get('prefix',0) * ' '
pad = kwargs.get('pad')
ret = ''
sep = ''
for i in args:
ret += sep + str(i)
sep = asep
while m is not None and len(ret) > m:
n = ret[0:m].rfind(' ')
if n < m / 2:
n = m
sys.stdout.write(prefix)
sys.stdout.write(ret[0:n])
sys.stdout.write(eol)
ret = ret[n+1:]
if indent != prefix:
m -= len(indent)
prefix = indent
sys.stdout.write(prefix)
sys.stdout.write(ret)
if pad and ret.rfind('\n') < 0:
if len(ret) < pad:
sys.stdout.write((pad - len(ret) ) * ' ')
else:
sys.stdout.write(' ')
sys.stdout.write(eol)
######## File IO and parsing
def _nextline(f):
if _nextline.line:
ret = _nextline.line
_nextline.line = None
return ret
ret = ''
while True:
tmp = f.readline()
if len(tmp) < 1:
break
if tmp[0] == '#':
continue
if tmp[-1] == '\n':
tmp = tmp[:-1]
if len(tmp) < 1:
break
if tmp[-1] != '\\':
ret += tmp
break
ret += tmp[:-1]
return ret
_nextline.line = None
_indent = ' ,'
def _peekline(file):
line = _nextline(file)
if _nextline.line:
raise NameError("Line already pushed")
_nextline.line = line
if line == '':
return '\n'
return line
def _add_entries(d, line):
while line[0] in _indent:
line = line[1:]
h1 = ''
while len(line) > 0:
if ';' in line:
sep = line.index(';')
if sep > 0 and line[sep-1] == '\\':
h1 += line[0:sep-1] + ';'
line = line[sep+1:]
continue
hdr = h1 + line[0:sep]
line = line[sep+1:]
h1 = ''
else:
hdr = h1+line
line = ''
if '=' in hdr:
e = hdr.index('=')
name = hdr[0:e]
value = hdr[e+1:]
else:
name = hdr
value = True
if name in d:
sys.stderr.write("\nKey {name} already in {d}\n".format(name=name,d=d))
raise NameError("Key '"+name+"' is already in dict")
d[name] = value
return d
def _compare_indent(l1, l2):
len1 = 0
while l1[len1] in _indent:
len1 += 1
len2 = 0
while l2[len2] in _indent:
len2 += 1
return len2 - len1
# aaa:
# We read a line
# bbb:
# we create a hash
# populate it with the entries we inherited
# fill it with the entries for our current line
# look at the indentation on the next line
# If it's deeper or the same
# If it's deeper
# recurse, passing the hash
# goto bbb
# else
# Create an object using the hash
# If it's the same
# goto aaa
# return
def parse_file(file, doit, inherited):
while True:
line = _nextline(file)
if line == '':
break
us = _add_entries(dict(inherited), line)
hadsub = False
while True:
peek = _peekline(file)
i = _compare_indent(line, peek)
if i > 0:
parse_file(file, doit, dict(us))
hadsub = True
continue
if i == 0:
if not hadsub:
doit(dict(us))
break
if i < 0:
if not hadsub:
doit(dict(us))
return
######## Date functions
month_names = (
'Hammer', 'Alturiak', 'Ches', 'Tarsakh', 'Mirtul', 'Kythorn',
'Flamerule', 'Eleasias', 'Eleint', 'Marpenoth', 'Uktar', 'Nightal')
festival_names = ('Midwinter', 'Greengrass', 'Midsummer', 'Higharvestide', 'Moon')
month_offset = {
'Hammer':0, 'Midwinter Festival':30, 'Alturiak':31, 'Ches':61,
'Tarsakh':91, 'Greengrass Festival':121, 'Mirtul':122, 'Kythorn':152,
'Flamerule':182, 'Midsummer Festival':212, 'Eleasias':213, 'Eleint':243,
'Higharvestide Festival':273, 'Marpenoth':274, 'Uktar':304,
'Moon Festival':334, 'Nightal':335}
_day_names = (
'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th', 'th',
'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th',
'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th')
def _make_date(year, month, day):
year = int(year)
day = int(day)
if year < 1369 or year > 1370 or day < 1 or day > 30 or month not in month_offset:
raise ValueError("Invalid date {y} {m} {d}".format(y=year, m=month, d=day))
return 1000 + 365 * (year - 1368) + month_offset[month] + day - 1
def str_to_date(date):
""" turn a human readable date (day month year) into a date_id"""
# 365 * (year-1368) + month_offset[month] + day
if date is None or type(date) is int:
return date
r = re.match(r'(\d+)\s+(\w+),?\s+(\d+)$', date)
if r:
return _make_date(r.group(3), r.group(2), r.group(1))
r = re.match(r'(\w+)\s+Festival,?\s+(\d+)$', date)
if r:
return _make_date(r.group(2), r.group(1)+' Festival', 1)
r = re.match(r'\((\d+),\s+(\w+),\s+(\d+)\)',date)
if r:
return _make_date(r.group(1), r.group(2), r.group(3))
r = re.match(r'(\d+)\s+([a-zA-Z]+),?\s+(\d+)', date)
if r:
return _make_date(r.group(3), r.group(2), r.group(1))
r = re.match(r'([a-zA-Z]+)\s+(\d+),?\s+(\d+)', date)
if r:
return _make_date(r.group(3), r.group(1), r.group(2))
r = re.match(r'([a-zA-Z]+)\s+(\d+)([a-zA-Z]+?),?\s+(\d+)', date)
if r:
day = int(r.group(2))
if _day_names[day-1] == r.group(3):
return _make_date(r.group(4), r.group(1), day)
raise NameError("Unknown date "+date)
#day_names = {
# 1:'First', 2:'Second', 3:'Third', 4:'Fourth', 5:'Fifth', 6:'Sixth',
# 7:'Seventh', 8:'Eighth', 9:'Ninth', 10:'Tenth', 11:'Eleventh', 12:'Twelth',
#13:'Thirteenth', 14:'Fourteenth', 15:'Fifteenth', 16:'Sixteenth', 17:'Seventeenth',
#18:'Eighteenth', 19:'Ninteenth', 20:'Twentieth', 21:'Twenty-First', 22:'Twenty-second',
#23:'Twenty-third', 24:'Twenty-fourth', 25:'Twenty-fifth', 26:'Twenty-sixth',
#27:'Twenty-seventh', 28:'Twenty-eighth', 29:'Twenty-ninth', 30:'Thirtyith'}
off_monthset = ((0, 29, 'Hammer'), (30, 30, 'Midwinter Festival'),
(31, 60, 'Alturiak'), (61, 90, 'Ches'), (91, 120, 'Tarsakh'),
(121, 121, 'Greengrass Festival'), (122, 151, 'Mirtul'), (152, 181, 'Kythorn'),
(182, 211, 'Flamerule'), (212, 212, 'Midsummer Festival'), (213, 242, 'Eleasias'),
(243, 272, 'Eleint'), (273, 273, 'Higharvestide Festival'), (274, 303, 'Marpenoth'),
(304, 333, 'Uktar'), (334, 334, 'Moon Festival'), (335, 364, 'Nightal'))
def date_to_str(date, is_html = True):
"""return a human-readable form of a date-id"""
try:
date = int(date)
except:
sys.stderr.write("\ndate {d} already a string\n".format(d=date))
return date
if date < 1000:
raise NameError("Invalid date " + str(date))
date -= 1000
year = 1368 + int(date/365)
mo = date % 365
for i in off_monthset:
if mo >= i[0] and mo <= i[1]:
month = i[2]
if i[0] == i[1]:
return month + ' ' + str(year)
day = 1 + mo - i[0]
if is_html:
return month+' '+str(day)+'<font size="-1"><sup>'+_day_names[day-1]+'</sup></font>, '+str(year)
return month+' '+str(day)+_day_names[day-1]+', '+str(year)
raise NameError("Couldn't find date '" + repr(date))+"'"
def party_to_date(d):
return str_to_date(re.sub(r'^([^-]*-)?([^-]+)-(Festival|[0-9]+)[A-Za-z]?-([^-]+)$', r'\2 \3 \4', d))
######## Copper and Gold piece functions
def _find_coin(m):
q = m.group(2)
if q is None:
q = 1
else:
q = int(q)
c = m.group(3)
if c in str_to_coins.coins:
str_to_coins.coins[c] += q
else:
str_to_coins.coins[c] = q
return ''
def coins_add(c, coin, n):
if coin not in coins_byname:
raise NameError("Unknown coin '"+coin+"' in money")
if n == 0:
return c
c['value_cp'] += n * coins_byname[coin]['copper_equiv']
if 'abbrev' in coins_byname[coin]:
name = coins_byname[coin]['abbrev']
else:
name = coins_byname[coin]['coin']
if name in c:
if c[name] == n:
del c[name]
else:
c[name] += n
else:
c[name] = n
return c
def coins_by_cpe(s):
tmp = {}
for i in s:
if i == 'value_cp':
continue
if not i in coins_byname:
pw(coins_byname,max=80)
raise ValueError("Unknown coin '"+i+"' in coins")
n = int(s[i])
if n == 0:
continue
cpe = coins_byname[i]['copper_equiv']
if cpe not in tmp:
tmp[cpe] = {}
tmp[cpe][i] = n
ret = []
l = tmp.keys()
l.sort(reverse = True)
for i in l:
tmp_ret = []
m = tmp[i].keys()
m.sort()
for j in m:
tmp_ret.append([tmp[i][j], j])
ret.append(tmp_ret)
return ret
def str_to_coins(s):
global coins_byname
if s is None:
return None
str_to_coins.coins = {}
tmp = s.lower()
while len(tmp)>0:
rest = re.sub(r'^\s*((\d+)[\*x]?)?\s*([^0-9,]+[^,]*)(,|$)', _find_coin, tmp)
if rest == tmp:
raise NameError ("invalid text '"+rest+"' in money")
tmp = rest
myret = {'value_cp': 0}
for i in str_to_coins.coins:
myret = coins_add(myret, i, int(str_to_coins.coins[i]))
return myret
def coins_to_str(s):
if s is None:
return ''
ret = ''
sep = ''
nsep = ', '
for i in coins_by_cpe(s):
for j in i:
n = j[0]
if n == 1:
coin = j[1].format(s='', es='', y='y')
else:
coin = j[1].format(s='s', es='es', y='ies')
ret += '{sep}{n} {coin}'.format(sep = sep, n = n, coin = coin)
sep = nsep
return ret
def str_to_cp(s):
global coins_byname
if s is None:
return None
return str_to_coins(s)['value_cp']
def cp_to_str(cp):
""" pretty-print a raw cp value"""
if cp is None:
return ''
if cp == 0:
return "nothing"
if cp < 0:
return "NEGATIVE MONEY " + str(int(-cp/200)) + "gp, " + str(-cp%200) + "cp"
gp = int(cp/200)
cp = cp % 200
if gp > 0 and cp > 0:
return str(gp) + "gp, " + str(cp) + "cp"
elif gp > 0:
return str(gp) + "gp"
else:
return str(cp) + "cp"
def divide_cp(share, total, shares):
"""Perform an integer division, rounding up"""
whole = int((share * total)/shares)
fract = (float(share * total)/float(shares)) - whole
if fract >= 0.5000:
whole += 1
return whole
# if float(int(top/bottom)) != float(top)/float(bottom):
# return 1 + int(top/bottom)
# return int(top/bottom)
def insert_coin(coin, abbrev, copper_equiv, common=True, note = None):
global coins_byname,coins_bycpe,coins_byid
cur.execute(
' insert into fr_money_type (coin, abbrev, copper_equiv, priority, note) '
' values ( %(coin)s, %(abbrev)s, %(copper_equiv)s, %(common)s, %(note)s)',
{ 'coin': coin, 'abbrev': abbrev, 'copper_equiv': copper_equiv, 'common': common, 'note': note})
_check_warnings()
ret = cur.lastrowid
(coins_byname, coins_bycpe, coins_byid) = get_coins()
return ret
def get_coins():
cur.execute('select money_id, coin, copper_equiv, priority, abbrev, note from fr_money_type order by copper_equiv desc')
resp = cur.fetchall()
ret_byname = {}
ret_bycpe = {}
ret_byid = {}
for i in resp:
tmp = {}
tmp['money_id'] = i[0]
tmp['coin'] = i[1]
tmp['copper_equiv'] = i[2]
tmp['priority'] = i[3]
if i[4] is not None:
tmp['abbrev'] = i[4]
ret_byname[i[4]] = tmp
ret_byname[i[4].lower()] = tmp
if i[5] is not None:
tmp['note'] = i[5]
if i[2] in ret_bycpe:
ret_bycpe[i[2]].append(tmp)
else:
ret_bycpe[i[2]] = [tmp]
ret_byname[i[1]] = tmp
ret_byname[i[1].lower()] = tmp
ret_byname[i[1].format(s='', es='', y='y').lower()] = tmp
ret_byname[i[1].format(s='s', es='es', y='ies').lower()] = tmp
ret_byid[i[0]] = tmp
return (ret_byname, ret_bycpe, ret_byid)
def get_char_money(char):
cur.execute('select m.money_id, m.quantity from '
' fr_char_money as m natural join fr_money_type as t where m.owner ='
' %(char)s order by t.priority, t.copper_equiv desc, t.coin', {'char':char})
resp = cur.fetchall()
_check_warnings()
ret = []
for i in resp:
id = i[0]
t = coins_byid[id]
tmp = {'money_id': id, 'coin': t['coin'], 'copper_equiv': t['copper_equiv'], 'priority': t['priority'], 'quantity': int(i[1])}
if 'abbrev' in t:
tmp['abbrev'] = t['abbrev']
if 'note' in t:
tmp['note'] = t['note']
ret.append(tmp)
return ret
def char_find_money(char, amount_cp):
ret = {'value_cp': amount_cp}
x = get_char_money(char)
for i in x:
if i['priority'] == 0:
continue
cpe = i['copper_equiv']
if cpe > amount_cp:
continue
q = i['quantity']
if cpe * q > amount_cp:
n = int(amount_cp/cpe)
else:
n = q
if n == 0:
continue
if 'abbrev' in i:
name = i['abbrev']
else:
name = i['coin']
ret[name] = n
amount_cp -= n * cpe
if amount_cp == 0:
break
if amount_cp:
# pw("Error: getting {a} ({l} left) from {x} got {r}".format(a = ret['value_cp'], l = amount_cp, x = x, r = ret), max = 130)
raise ValueError("Char {char} needs {amount_cp} change".\
format(char = char, amount_cp = amount_cp))
return ret
def invent_money(amount_cp):
if amount_cp is None or amount_cp == 0:
return None
ret = {'value_cp': amount_cp}
l = coins_bycpe.keys()
l.sort(reverse = True)
print "invent money list",l
for i in l:
if i > amount_cp:
continue
for j in coins_bycpe[i]:
# print "invent money",j
if j['priority'] == 1 or j['priority'] == 0:
continue
n = int(amount_cp / i)
# print "inventing",n,j['coin']
if n == 0:
continue
if 'abbrev' in j:
ret[j['abbrev']] = n
else:
ret[j['coin']] = n
amount_cp -= n * i
if amount_cp == 0:
break
if amount_cp == 0:
break
if amount_cp > 0:
raise ValueError("Can't Happen")
return ret
def char_change_money(char, dir, d):
global coins_byname
if dir == 'give':
sign = -1
elif dir == 'get':
sign = 1
else:
raise ValueError("Dir isn't 'give' or 'get': '"+dir+"'")
total = 0
coins = get_char_money(char)
for coin in d:
if coin == 'value_cp':
continue
if coin not in coins_byname:
raise NameError("{char} invalid coin {coin}".format(char=char, coin=coin))
amount = int(sign * int(d[coin]))
if amount == 0:
continue
coin = coins_byname[coin]
cc = None
for i in coins:
if coin['coin'] == i['coin']:
cc = i['quantity']
break
if cc is None:
if amount < 0:
raise ValueError("{char} has no {coin}, needs {n}".format(char = char, coin = coin['coin'], n = -amount))
cur.execute("insert into fr_char_money (owner, money_id, quantity)"
" values (%(char)s, %(id)s, %(amount)s)",
{'char': char, 'id': coin['money_id'], 'amount': amount})
elif cc + amount < 0:
raise ValueError("{char} only has {cc} {coin}, needs {n}".format(char=char, cc = cc, coin = coin['coin'], n = -amount))
elif cc + amount == 0:
cur.execute("delete from fr_char_money where owner=%(char)s and money_id = %(id)s",
{'char': char, 'id': coin['money_id']})
else:
cur.execute("update fr_char_money set quantity = %(amount)s where owner=%(char)s and money_id = %(id)s",
{'char': char, 'id': coin['money_id'], 'amount': cc + amount})
_check_warnings()
total += amount * coin['copper_equiv']
cur.execute("update fr_character set cash_cp = cash_cp + %(total)s where char_name = %(char)s",
{'char': char, 'total': total})
_check_warnings()
def chars_move_cash(i):
"""move cash from one character to another, journaling it"""
check_dict(i, must=('date', 'by', 'to'), may=('cash', 'amount_cp', 'journal', 'item', 'for', 'note', 'part_of', 'journ_by', 'journ_to'))
by = i['by']
to = i['to']
if get_party_data(by)['type'] == 'AFAL':
by = party
if get_party_data(to)['type'] == 'AFAL':
to = party
if 'cash' in i:
cash = i['cash']
if not 'value_cp' in cash:
raise ValueError("invalid cash")
amount_cp = cash['value_cp']
else:
cash = None
amount_cp = i['amount_cp']
if by not in special:
if cash is None:
cash = char_find_money(by, amount_cp)
char_change_money(by, 'give', cash)
if to not in special:
if cash is None:
cash = invent_money(amount_cp)
char_change_money(to, 'get', cash)
if i.get('journal', True):
text = 'gave {cs}'+coins_to_str(cash)+'{ce} {each}to {to}'
if i.get('item'):
text += " for " + i['item']
if i.get('for'):
text += " for " + i['for']
if i.get('note'):
text += ' ' + i['note']
if 'journ_by' in i and text in i['journ_by']:
ret = i['journ_by'][text]
journal_add_by(ret, i['by'])
elif 'journ_to' in i and text in i['journ_to']:
ret = i['journ_to'][text]
journal_add_to(ret, i['to'])
else:
ret = journal(str_to_date(i['date']), i['by'], i['to'], amount_cp, None, text, i.get('part_of'))
if 'journ_by' in i:
i['journ_by'][text] = ret
if 'journ_to' in i:
i['journ_to'][text] = ret
return ret
return i.get('part_of')
######## Shares
def share_to_str(s):
if float(s) == 1.0:
return '1'
if float(s) == 0.0:
return 'no'
if float(s) == 0.5:
return '½'
if float(s) == 0.25:
return '¼'
# Shouldn't get here, but who knows?
if s == int(float(s)):
return str(int(s))
s = str(s)
if s[0:2] == '0.':
s = s[1:]
return s
######## Journal entries
def journal(date, by, to, cash_cp, virtual_cp, text, part_of = None):
"""add an entry to the transaction journal, returning an id that may be used for subsequent calls"""
if date is None or by is None or text is None:
raise NameError("Missing required field in journal")
date = str_to_date(date)
cur.execute (
"insert into fr_journal ( date, cash_cp, virtual_cp, description, part_of ) "
" values ( %(date)s, %(cash)s, %(virtual)s, %(text)s, %(part_of)s )",
{'date':date, 'cash':cash_cp, 'virtual': virtual_cp,
'text':text + ".", 'part_of':part_of})
_check_warnings()
id = cur.lastrowid
if type(by) is list:
for i in by:
cur.execute("insert into fr_journ_by ( journal_id, made_by ) value ( %(id)s, %(i)s )", { 'id': id, 'i': i })
_check_warnings()
else:
cur.execute("insert into fr_journ_by ( journal_id, made_by ) value ( %(id)s, %(i)s )", { 'id': id, 'i': by })
_check_warnings()
if type(to) is list:
for i in to:
cur.execute("insert into fr_journ_to ( journal_id, made_to ) value ( %(id)s, %(i)s )", { 'id': id, 'i': i })
_check_warnings()
elif to:
cur.execute("insert into fr_journ_to ( journal_id, made_to ) value ( %(id)s, %(i)s )", { 'id': id, 'i': to })
_check_warnings()
return id
def journal_add_by(id, by):
cur.execute("insert into fr_journ_by(journal_id, made_by) values(%(id)s, %(by)s)", {'id': id, 'by': by})
_check_warnings()
def journal_add_to(id, to):
cur.execute("insert into fr_journ_to(journal_id, made_to) values(%(id)s, %(to)s)", {'id': id, 'to': to})
_check_warnings()
def get_journal_dates(char = None):
"""return a list of dates (in text form) of entries in the journal"""
if char:
clause = "natural join fr_journ_by natural join fr_journ_to where fr_journ_by.made_by = %(char)s or fr_journ_to.made_to = %(char)s"
else:
clause = ""
cur.execute('select distinct fr_journal.date from fr_journal {clause} order by date'.format(clause = clause), {'char': char})
resp = cur.fetchall()
_check_warnings()
if resp == None or len(resp)==0:
return resp
ret=['latest']
for i in resp:
ret.append(date_to_str(i[0], False))
return ret
def get_journal(cond = None):
"""return a list of dicts of entries in the journal"""
if cond is None:
cond = {}
check_dict(cond, must=None, may=('char', 'char2', 'primary', 'on', 'starting_on', 'up_to', 'cash'))
clause = ''
pre = ' where '
npre = ' and '
date1 = None
date2 = None
char = []
char_clause = ''
args = {}
if cond.get('char','').lower() == 'all':
del cond['char']
if cond.get('char2','').lower() == 'all':
del cond['char2']
if cond.get('on','').lower() == 'all':
del cond['on']
if cond.get('starting_on','').lower() == 'all':
del cond['starting_on']
if cond.get('up_to','').lower() == 'all':
del cond['up_to']
if 'char' in cond:
char.append(cond['char'])
if 'char2' in cond:
char.append(cond['char2'])
if len(char) > 0:
if len(char) > 1:
tmp = "(journal_id in (select journal_id from fr_journ_by where made_by in (%(ch1)s, %(ch2)s)) and journal_id in (select journal_id from fr_journ_to where made_to in (%(ch1)s, %(ch2)s)))"
args['ch1'] = char[0]
args['ch2'] = char[1]
else:
tmp = "(journal_id in (select journal_id from fr_journ_by where made_by = %(ch)s) or journal_id in (select journal_id from fr_journ_to where made_to = %(ch)s))"
args['ch'] = char[0]
clause += pre + tmp
char_clause = ' where ' + tmp
pre = npre
if 'primary' in cond:
clause += pre + ' part_of is NULL '
pre = npre
if 'on' in cond:
if cond['on'].lower() == 'latest':
clause += pre + ' date = (select max(date) from fr_journal {char_clause}) '.format(char_clause = char_clause)
else:
clause += pre + ' date = %(on)s '
args['on'] = str_to_date(cond['on'])
pre = npre
if 'starting_on' in cond:
if 'on' in cond:
raise KeyError("Can't have both 'starting_on' and 'on' in condition")
if cond['starting_on'].lower() == 'latest':
clause += pre + ' date = (select max(date) from fr_journal {char_clause}) '.format(char_clause = char_clause)
else:
clause += pre + ' date >= %(s_o)s '
args['s_o'] = str_to_date(cond['starting_on'])
pre = npre
if 'up_to' in cond:
if 'on' in cond:
raise KeyError("Can't have both 'up_to' and 'on' in condition")
if cond['up_to'].lower() == 'latest':
pass
else:
clause += pre + ' date <= %(u_t)s '
args['u_t'] = str_to_date(cond['up_to'])
pre = npre
if 'cash' in cond:
clause += pre + ' cash_cp > 0 '
pre = npre
cmd = 'select '\
' journal_id, part_of, ourtime, date, cash_cp, virtual_cp, description'\
' from fr_journal {clause} order by date, journal_id'.format(clause = clause)
cur.execute(cmd, args)
resp = cur.fetchall()
_check_warnings()
if resp == None or len(resp) == 0:
return resp
ids = []
for i in resp:
ids.append(i[0])
if len(ids) == 1:
ids = '({id})'.format(id=ids[0])
elif len(ids) > 1:
ids = str(tuple(ids))
by_map = {}
to_map = {}
if len(ids) > 0:
text = 'select journal_id, made_by from fr_journ_by where journal_id in '+ids
cur.execute(text)
by_tmp = cur.fetchall()
_check_warnings()
for i in by_tmp:
if i[0] in by_map:
by_map[i[0]].append(i[1])
else:
by_map[i[0]] = [i[1]]
text = 'select journal_id, made_to from fr_journ_to where journal_id in '+ids
cur.execute(text)
to_tmp = cur.fetchall()
_check_warnings()
for i in to_tmp:
if i[0] in to_map:
to_map[i[0]].append(i[1])
else:
to_map[i[0]] = [i[1]]
ret=[]
for i in resp:
id = i[0]
to_list = to_map.get(id)
t = {}
t['journal_id'] = id
t['by'] = by_map[i[0]]
t['to'] = to_list
t['part_of'] = i[1]
t['ourtime'] = i[2]
t['date'] = i[3]
t['cash_cp'] = i[4]
t['virtual_cp'] = i[5]
t['description'] = i[6]
ret.append(t)
return ret
########## Characters
def insert_character(d):
""" insert a character into the database"""
global coins_byname
check_dict(d, must=('name', 'status', 'association', 'date'), may=(
'alignment', 'characteristics', 'class', 'hidden_note', 'equipment',
'fullname', 'gender', 'note', 'player', 'race', 'cash', 'picture',
'large_picture'))
_ins_if_not(d, ('alignment', 'association', 'characteristics', 'class',
'equipment', 'fullname', 'hidden_note', 'large_picture', 'note', 'picture',
'player', 'race'))
if 'gender' not in d:
d['gender'] = 'Unknown Gender'
if 'cash' in d:
d['cash_cp'] = 0
else:
d['cash_cp'] = None
cur.execute(
"insert into fr_character ( "
" char_name, status, alignment, association, "
" char_acteristics, class, hidden_note, equipment, "
" fullname, gender, note, player, "
" race, cash_cp, picture_url, large_picture_url ) "
" values ( "
" %(name)s, %(status)s, %(alignment)s, %(association)s, "
" %(characteristics)s, %(class)s, %(hidden_note)s, %(equipment)s, "
" %(fullname)s, %(gender)s, %(note)s, %(player)s, "
" %(race)s, %(cash_cp)s, %(picture)s, %(large_picture)s )", d )
_check_warnings()
# Every character is in a party of just themselves
# This is primarily to prevent someone accidentally creating a party and a
# character with the same names
insert_party({'name': d['name'], 'type': 'Character', 'date': d['date'], 'members': {d['name']: 1}})
if 'cash' in d:
char_change_money(d['name'], 'get', str_to_coins(d['cash']))
def get_characters(kind, match = None):
"""return a list of name entries for characters in the database"""
clause = ''
if kind == 'All':
clause = 'status != "dummy"'
elif kind == 'Current':
clause = 'status = "active" and association = "AFAL"'
elif kind == 'Former':
clause = 'status = "inactive" and association = "AFAL"'
elif kind == 'Dead':
clause = 'status = "dead" and association = "AFAL"'
elif kind == 'ActiveNPCs':
clause = 'status = "active" and ( association is NULL or association != "AFAL")'
elif kind == 'InactiveNPCs':
clause = 'status = "inactive" and ( association is NULL or association != "AFAL")'
elif kind == 'DeadNPCs':
clause = 'status = "dead" and (association is NULL or association != "AFAL")'
else:
raise NameError("Unknown kind " + str(kind))
cur.execute("select char_name from fr_character "
" where " + clause + " order by char_name")
resp = cur.fetchall()
_check_warnings()
ret = []
if len(resp) < 1 or len(resp[0]) != 1:
return ret
for i in resp:
ret.append(i[0])
return ret
def get_char_data(char):
""" given a char, return its data """
cur.execute("select fullname, player, gender, "
" race, class, alignment, picture_url, equipment, "
" char_acteristics, note, association, status, cash_cp, "
" large_picture_url "
" from fr_character where char_name = %(char)s",
{'char':char})
resp = cur.fetchall()
_check_warnings()
if len(resp) != 1:
raise NameError("Couldn't find char '" + char + "'")
resp = resp[0]
ret = {}
ret['name'] = char
ret['fullname'] = resp[0]
ret['player'] = resp[1]
ret['gender'] = resp[2]
ret['race'] = resp[3]
ret['class'] = resp[4]
ret['alignment'] = resp[5]
ret['picture_url'] = resp[6]
ret['equipment'] = resp[7]
ret['characteristics'] = resp[8]
ret['note'] = resp[9]
ret['association'] = resp[10]
ret['status'] = resp[11]
ret['cash_cp'] = resp[12]
ret['large_picture_url'] = resp[13]
ret['coins'] = get_char_money(char)
return ret
########## Parties
def get_party_data(party):
"""Given a party, return a list [[[name, share]...], shares] of its members"""
cur.execute("select date, type, note from fr_party where party_name = %(party)s",
{'party':party})
resp = cur.fetchall()
_check_warnings()
if len(resp) != 1 or len(resp[0]) != 3:
raise NameError("party not found " + party)
ret = {'name': party, 'date': resp[0][0], 'type': resp[0][1], 'note': resp[0][2], 'members': {}}
cur.execute("select char_name, share from fr_char_party where party_name = %(party)s",
{'party':party})
resp = cur.fetchall()
_check_warnings()
if len(resp) < 1 or len(resp[0]) < 1:
raise NameError("Members not found " + party)
for i in resp:
ret['members'][i[0]] = i[1]
return ret