-
Notifications
You must be signed in to change notification settings - Fork 0
/
bulk
executable file
·1110 lines (1034 loc) · 42.8 KB
/
bulk
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 argparse
import afall
import os
import sys
from cStringIO import StringIO
def do_meet(d):
afall.check_dict(d, must = ('name', 'status', 'date'), may = ('alignment', 'association',
'characteristics', 'class', 'hidden_note', 'equipment', 'fullname', 'note',
'player', 'race', 'picture', 'large_picture', 'cash', 'gender'))
e = {}
for i in ('name', 'status', 'date', 'alignment', 'association', 'characteristics',
'class', 'hidden_note', 'equipment', 'fullname', 'note', 'player',
'race', 'picture', 'large_picture', 'gender'):
e[i] = d.get(i)
if 'cash' in d:
e['cash'] = d['cash']
if args.verbose:
afall.pw("Creating character", e, max = 130)
afall.insert_character(e)
def do_party(d):
afall.check_dict(d, must = ('name', 'type', 'date'))
e = {}
e['name'] = d.pop('name')
e['type'] = d.pop('type')
e['date'] = d.pop('date')
if 'note' in d:
e['note'] = d.pop('note')
e['members'] = {}
for c in d:
e['members'][c] = d[c]
if args.verbose:
afall.pw("creating party", e, max = 130)
afall.insert_party(e)
def do_give(d):
# Two possibilities from=, amount= to move cash
# or item= to give an item
if 'item' in d:
afall.check_dict(d, must = ('date', 'to', 'item'), may = ('holder', 'journal', 'note', 'part_of'))
if args.verbose:
afall.pw("giving item", d, max = 130)
afall.change_item(d['date'], d['item'], owner = d['to'], holder = d.get('holder',d['to']), part_of = d.get('part_of'))
return
if 'cash' in d:
afall.check_dict(d, must = ('date', 'to', 'by', 'cash'), may = ('for', 'note', 'part_of'))
d['cash'] = afall.str_to_coins(d['cash'])
else:
afall.check_dict(d, must = ('date', 'to', 'by', 'amount'), may = ('for', 'note', 'part_of'))
d['amount_cp'] = afall.str_to_cp(d.pop('amount'))
afall.chars_move_cash(d)
def do_lend(d):
date = d['date']
to = d['to']
part_of = d.get('part_of')
if 'item' in d:
afall.check_dict(d, must = ('date', 'to', 'item'), may = ('note', 'part_of'))
if args.verbose:
afall.pw("lending item", d, max = 130)
afall.change_item(date, d['item'], holder = to, part_of = part_of)
return
afall.check_dict(d, must = ('date', 'to', 'by', 'amount'), may = ('virtual', 'for', 'note', 'part_of', 'journal'))
if args.verbose:
afall.pw("lending cash", d, max = 130)
by = d['by']
amount_cp = afall.str_to_cp(d['amount'])
party_data = afall.get_party_data(by)
lenders = party_data['members']
shares = sum(lenders.values())
if shares == 1:
even_cp = amount_cp
else:
share_mult = 1
for share in lenders.values():
if share > 0 and 1/share > share_mult:
share_mult = int(1/share)
even_cp = shares * share_mult * int(amount_cp/(shares*share_mult))
even_cp = int(even_cp)
if even_cp != amount_cp:
sys.stdout.write("Note: Even shares would be lending {even} not {amount} for shares of {even_shares} with {leftover} unaccounted for in {d}\n".format(
even = afall.cp_to_str(even_cp), amount = afall.cp_to_str(amount_cp), even_shares = afall.cp_to_str(int(even_cp/shares)), leftover = afall.cp_to_str(amount_cp-even_cp), d = d))
if len(lenders) > 1 and d.get('journal', True):
text = "{each}lent {virtual} to {to}"
if d.get('note'):
text += ' ' + d['note']
tmp = afall.journal(date, by, to, None, amount_cp, text, part_of )
if part_of is None:
part_of = tmp
journaled = {}
journ_by = {}
for by in lenders:
share = float(lenders[by])
if share == 0.0:
continue
share_cp = afall.divide_cp(share, amount_cp, shares)
amount_cp -= share_cp
shares -= share
if by == to:
text = "noted a self-debt of {virtual}"
totmp = None
else:
text = "{each}lent {virtual} to {to}"
totmp = to
fer = " for "
if share != 1.0:
text += fer + afall.share_to_str(share) + " share"
fer = " of "
if d.get('for'):
text += fer + d['for']
if d.get('note'):
text += ' ' + d['note']
if text in journaled:
afall.journal_add_by(journaled[text], by)
else:
journaled[text] = afall.journal(date, by, totmp, None, share_cp, text, part_of)
if totmp is not None:
afall.insert_debt({'date': date, 'by': by, 'to': to,
'amount_cp': share_cp, 'share': share, 'item': d.get('for')})
if 'virtual' not in d:
afall.chars_move_cash({'date': date, 'by': by, 'to': to, 'amount_cp': share_cp, 'part_of': part_of, 'journal': d.get('journal', True), 'journ_by': journ_by})
def do_sell(d):
afall.check_dict(d, must = ('date', 'to', 'item'), may = ('cash', 'debt', 'note', 'part_of', 'virtual'))
date = d['date']
to = d['to']
cash_cp = afall.str_to_cp(d.get('cash', '0cp'))
debt_cp = afall.str_to_cp(d.get('debt', '0cp'))
item = d['item']
item_data = afall.get_item_data(item)
note = d.get('note', '')
part_of = d.get('part_of')
if item_data is None or len(item_data) < 2 or (cash_cp == 0 and debt_cp == 0):
sys.stderr.write("\nIgnoring invalid sell command {d}\n".format(d = d))
return
seller = item_data['owner']
if cash_cp > 0:
t_for = '{cash} cash'
t_and = ' and '
else:
t_for = ''
t_and = ''
if debt_cp > 0:
t_for += t_and + '{virtual} debt'
text = 'sold {item} to {{to}} for {fer}{note}'.format(item = item, fer = t_for, note = note)
tmp = afall.journal(date, seller, to, cash_cp, debt_cp, text, part_of)
if part_of is None:
part_of = tmp
afall.change_item(date, item, owner = to, holder = to, value_cp = cash_cp + debt_cp, part_of = part_of )
if debt_cp > 0:
do_lend({'by': seller, 'to': to, 'amount': afall.cp_to_str(debt_cp), 'date': date, 'for': item, 'part_of': part_of, 'note': note, 'virtual': True, 'journal': False})
if 'virtual' not in d and cash_cp > 0:
do_give({'date': date, 'by': to, 'to': seller, 'cash': d.get('cash'), 'for': item, 'part_of': part_of, 'note': note})
def divide_coins(coins, payout):
# sys.stdout.write("paying {payout} with {coins}\n".format(payout = payout, coins = repr(coins)))
ret = {'value_cp': 0}
if payout > coins['value_cp']:
raise ValueError("payout {p} larger than coins value {v}".format(p=payout, v=coins['value_cp']))
tmp = {}
for i in coins:
if i == 'value_cp':
continue
cpe = afall.coins_byname[i]['copper_equiv']
if cpe in tmp:
tmp[cpe].append(i)
else:
tmp[cpe] = [i]
l = tmp.keys()
l.sort(reverse = True)
for j in l:
for i in tmp[j]:
n = int(payout/j)
if n == 0:
continue
coins[i] -= n
ret[i] = n
cpe = n * j
coins['value_cp'] -= cpe
ret['value_cp'] += cpe
payout -= cpe
if ret['value_cp'] == 0:
# Nothing got moved. Move at least one coin
sys.stdout.write("Nothing got moved\n")
if payout > 0:
sys.stdout.write("paying {payout} not zero in {coins}\n".format(payout = payout, coins = repr(coins)))
# raise ValueError("payout should be zero not {p}".format(p = payout))
return ret
def do_pay(d):
inexact = False
item = None
coins = None
if 'item' in d:
afall.check_dict(d, must = ('date', 'to', 'item'), may = ('note', 'part_of'))
item = d['item']
item_data = afall.get_item_data(item)
by = item_data['owner']
amount_cp = item_data['value_cp']
elif 'cash' in d:
afall.check_dict(d, must = ('date', 'by', 'cash'), may = ('to', 'note', 'part_of'))
by = d['by']
coins = afall.str_to_coins(d['cash'])
amount_cp = coins['value_cp']
if 'to' not in d:
inexact = True
elif 'by' in d:
afall.check_dict(d, must = ('date', 'by'), may = ('amount_cp', 'amount', 'percent', 'virtual', 'to', 'note', 'part_of'))
by = d['by']
if d.get('amount_cp'):
amount_cp = d['amount_cp']
elif d.get('amount'):
amount_cp = afall.str_to_cp(d['amount'])
elif 'percent' in d:
amount_cp = int(float(afall.get_char_data(by)['cash_cp']) * float(d['percent'])/100.0)
inexact = True
else:
amount_cp = afall.get_char_data(by)['cash_cp']
inexact = True
else:
sys.stderr.write("\nIgnoring invalid pay command {d}\n".format(d = d))
return
payable = afall.get_char_debts(by, 'Payable')
if len(payable) == 0:
sys.stderr.write("\n{date} {by} has not debts to pay\n".format(date = d['date'], by = by))
return
to_multiple = False
if 'to' in d:
to = d['to']
ptmp = []
for i in payable:
if i['to'] == to:
ptmp.append(i)
payable = ptmp
else:
to = payable[0]['to']
for debt in payable[1:]:
if debt['to'] != to:
to = 'debts'
to_multiple = True
break
date = d['date']
shares = 0.0
owed = 0
for i in payable:
shares += i['share']
owed += i['amount_cp']
# print by + ' paying ' + repr(amount_cp) + ' on ' + repr(owed) + ' in ' + repr(payable)
if amount_cp > owed:
if not inexact:
if amount_cp - owed < 3:
sys.stderr.write("\nWARNING: {date} {by} paying {to} overpaid {lost} out of {owed} (should go to party)\n".format( date = date, by = by, to = to, lost = amount_cp - owed, owed = afall.cp_to_str(owed)))
else:
sys.stderr.write("\nWARNING: {date} {by} only owes {owed} not {amount} to {to}\n".format(date = date, by = by, owed = afall.cp_to_str(owed), amount = afall.cp_to_str(amount_cp), to = to))
amount_cp = owed
if item:
cash = None
virtual = amount_cp
text = 'paid {virtual} {each}to {to} with ' + item
elif coins is not None:
cash = amount_cp
virtual = None
text = 'paid {cash} {each}to {to} with {cs}' + afall.coins_to_str(coins)+'{ce}'
else:
cash = amount_cp
virtual = None
text = 'paid {cash} {each}to {to}'
note = d.get('note')
if note:
text += ' ' + note
part_of = d.get('part_of')
tmp = afall.journal(date, by, to, cash, virtual, text, part_of)
if part_of is None:
part_of = tmp
topay = {}
todel = {}
tolower = {}
journ_to = {}
for i in payable:
this_owed = i['amount_cp']
this_share = i['share']
to = i['to']
this_cp = afall.divide_cp(this_owed, amount_cp, owed)
if abs(this_cp - this_owed) < 3:
if this_cp != this_owed:
sys.stderr.write("\nWARNING: {date} {by} paying {to} underpaid {cp} {lost} (should come from party)\n".format(date = date, by = i['by'], to = i['to'], cp = afall.cp_to_str(this_cp), lost = this_cp - this_owed))
tmp = {'date': date, 'debt_id': i['debt_id'], 'by': i['by'], 'to': i['to'], 'amount_cp': i['amount_cp'], 'item': i.get('item'), 'part_of': part_of, 'note': note, 'verb': 'paid off'}
if to in todel:
todel[to].append(tmp)
else:
todel[to] = [tmp]
else:
tmp = {'date': date, 'debt_id': i['debt_id'], 'by': i['by'], 'to': i['to'], 'amount_cp': i['amount_cp'],
'part_of': part_of, 'note': note, 'lower_cp': this_cp, 'verb': 'paid'}
if 'item' in i:
tmp['item'] = i['item']
if to in tolower:
tolower[to].append(tmp)
else:
tolower[to] = [tmp]
if item is None and 'virtual' not in d:
if to in topay:
topay[to]['amount_cp'] += this_cp
else:
tmp = {'date': date, 'by': by, 'to': to,
'amount_cp': this_cp, 'journ_to': journ_to}
if 'to' in d:
tmp['journal'] = False
if coins is not None:
if to_multiple:
tmp['cash'] = divide_coins(coins, this_cp)
else:
tmp['cash'] = coins
if part_of is not None:
tmp['part_of'] = part_of
if note is not None:
tmp['note'] = note
if len(payable) == 1:
if 'item' in i:
tmp['item'] = i['item']
if 'for' in i:
tmp['for'] = i['for']
topay[to] = tmp
amount_cp -= this_cp
owed -= this_owed
shares -= this_share
if item is not None:
part_of = afall.change_item(date, item, owner = to, holder = to, part_of = part_of)
if to in todel:
for j in todel[to]:
j['part_of'] = part_of
if to in tolower:
for j in tolower[to]:
j['part_of'] = part_of
elif 'virtual' not in d:
for i in topay:
part_of = afall.chars_move_cash(topay[i])
if i in todel:
for j in todel[i]:
j['part_of'] = part_of
afall.delete_debt(j)
del todel[i]
if i in tolower:
for j in tolower[i]:
j['part_of'] = part_of
afall.debt_lower_owed(j)
del tolower[i]
for i in todel:
for j in todel[i]:
afall.delete_debt(j)
for i in tolower:
for j in tolower[i]:
afall.debt_lower_owed(j)
def do_writeoff(d):
afall.check_dict(d, must = ('date', ))
date = d.pop('date')
part_of_main = d.pop('part_of', None)
note = d.pop('note', None)
for char in d:
payable = afall.get_char_debts(char, 'Payable')
receivable = afall.get_char_debts(char, 'Receivable')
if len(payable) > 0 or len(receivable) > 0:
pay_cp = 0
for debt in payable:
pay_cp += debt['amount_cp']
rec_cp = 0
for debt in receivable:
rec_cp += debt['amount_cp']
if pay_cp > 0 and rec_cp > 0:
text = 'cancelled {rec} and welshed on {pay} of debts'.format(rec = afall.cp_to_str(rec_cp), pay = afall.cp_to_str(pay_cp))
elif pay_cp > 0:
text = 'welshed on {virtual} of debt'
else:
text = 'cancelled {virtual} of debt'
if note:
text += ' ' + note
tmp = afall.journal(date, char, None, None, pay_cp + rec_cp, text, part_of_main)
part_of = tmp if part_of_main is None else part_of_main
journaled = {}
for debt in payable:
afall.delete_debt({'date': date, 'debt_id': debt['debt_id'], 'by': char, 'to': debt['to'], 'amount_cp': debt['amount_cp'], 'item': debt.get('item'), 'part_of': part_of, 'note': note, 'verb': 'welshed on', 'journaled': journaled})
journaled = {}
for debt in receivable:
afall.delete_debt({'date': date, 'debt_id': debt['debt_id'], 'by': debt['by'], 'to': char, 'amount_cp': debt['amount_cp'], 'item': debt.get('item'), 'part_of': part_of, 'note': note, 'verb': 'cancelled', 'journaled': journaled})
afall.char_leave_parties(date, char, note)
def scale_cash(d, share):
ret = {}
for i in d:
if d[i] * share != int(d[i] * share):
raise ValueError("cash {n} {i} doesn't scale by {share}".format(n = d[i], i = i, share = share))
ret[i] = int(d[i] * share)
# print "scale cash", d, "to", ret, "by", share
return ret
def do_dividend(d):
afall.check_dict(d, must = ('date', 'by'), may = ('cash', 'amount', 'note', 'part_of'))
date = d['date']
by = d['by']
party_data = afall.get_party_data(by)
part_of = d.get('part_of')
members = party_data['members']
shares = float(sum(members.values()))
share_mult = 1
for share in members.values():
if share > 0 and 1/share > share_mult:
share_mult = int(1/share)
text = "paid a dividend of {cash}"
if 'amount' in d:
if 'cash' in d:
raise ValueError("Can't have both cash and amount")
amount_cp = afall.str_to_cp(d['amount'])
cash = None
even_cash = None
if shares == 1:
even_cp = amount_cp
else:
even_cp = shares * share_mult * int(amount_cp/(share_mult * shares))
even_cp = int(even_cp)
if amount_cp != even_cp:
text += "(and {cp} to the party)".format(cp = afall.cp_to_str(amount_cp-even_cp))
text += " split into {sh} shares of {{cs}}{ea}{{ce}} each".format(sh = afall.share_to_str(shares), ea = afall.cp_to_str(int(even_cp/shares)))
elif 'cash' in d:
cash = afall.str_to_coins(d['cash'])
if shares == 1:
even_cash = cash
even_cp = cash['value_cp']
else:
even_cash = {'value_cp': 0}
rem_cash = {'value_cp': 0}
for coin in cash:
if coin == 'value_cp':
continue
even_coin = int(shares * share_mult * int(cash[coin]/(shares*share_mult)))
even_cash = afall.coins_add(even_cash, coin, int(even_coin/shares))
if cash[coin] != even_coin:
rem_cash = afall.coins_add(rem_cash, coin, cash[coin] - even_coin)
if rem_cash['value_cp'] > 0:
text += "(and {cash} to the party)".format(cash = afall.coins_to_str(rem_cash))
text += " split into {sh} shares of {{cs}}{ea}{{ce}} each".format(sh = afall.share_to_str(shares), ea = afall.coins_to_str(even_cash))
even_cp = even_cash['value_cp'] * shares
else:
raise ValueError("Need either cash or amount")
if shares != len(members):
text += " among {n} party members".format(n = len(members))
if d.get('note'):
text += ' ' + d['note']
tmp = afall.journal(date, by, members.keys(), even_cp, None, text, part_of)
if part_of is None:
part_of = tmp
journ_to = {}
for char in members:
share = members[char]
if share == 0:
continue
if even_cash:
afall.chars_move_cash({'date': date, 'cash': scale_cash(even_cash, share),
'by': by, 'to': char, 'part_of': part_of, 'journ_to': journ_to})
else:
share_cp = afall.divide_cp(share, even_cp, shares)
if args.verbose:
afall.pw(afall.cp_to_str(share_cp), "to", char, max = 130)
afall.chars_move_cash({'date': date, 'amount_cp': share_cp,
'by': by, 'to': char, 'part_of': part_of, 'journ_to': journ_to})
even_cp -= share_cp
shares -= share
# Divy is like dividend, except that you can specify both cash= and amount=
# and specify specific coins to be given to specific characters
# And it uses a completely different algorithm
# It divides characters into pools by how much they're owed
# then divides up the coins from "cash" so that members of each pool get
# the same number of coins. To maximize convergance, pools are processed
# in the order of the number of members they have. If there are leftover
# coins that can't be allocated into a pool, an error is raised.
# Then, the rest of what each pool is owed is filled out of the party's cash
# So, each pool is a dict, containing 'members' a list of character names,
# 'amount_cp', the amount each character is owed, and 'coins', a dict
# of coins. During creation, they are kept in lists (for different 'coins')
# in a dict by share amount
# which is then flattened into a list sorted by len('members')
def do_divy(d):
afall.check_dict(d, must = ('date', 'by'))
text = 'paid a dividend of {cash}'
share_mult = 1
journ_to = {}
pools_by_share = {}
odd_cp = 0
s = 'divided '
date = d.pop('date')
by = d.pop('by')
part_of = d.pop('part_of', None)
note = d.pop('note', None)
cash = d.pop('cash', None)
amount = d.pop('amount', None)
members = afall.get_party_data(by)['members']
n_members = len(members)
shares = float(sum(members.values()))
for share in members.values():
if share == 0:
continue
if 1/share > share_mult:
share_mult = int(1/share)
if share not in pools_by_share:
pools_by_share[share] = []
for char in members:
share = members[char]
if share == 0:
continue
if char in d:
tmp = afall.str_to_coins(d.pop(char))
else:
tmp = {'value_cp': 0}
for i in pools_by_share[share]:
if i['coins'] == tmp:
i['members'].append(char)
char = None
break
if char:
pools_by_share[share].append({'members': [char], 'coins': tmp})
# print "Debug: pools_by_share", pools_by_share
if len(d) > 0:
raise ValueError("Character(s) {d} not in party".format(d = d.keys()))
if cash:
coins = afall.str_to_coins(cash)
else:
coins = {'value_cp': 0}
if amount:
amount_cp = afall.str_to_cp(amount)
else:
amount_cp = coins['value_cp']
if amount_cp == 0:
raise ValueError("Need either cash or amount")
if shares != n_members:
text += " {s}among {n} party members".format(n = n_members, s = s)
s = ''
if shares <= 1:
raise ValueError("Less than two shares? Unpossible!")
even_cp = share_mult * int(amount_cp / (share_mult * shares))
text += " {s}into {sh} shares of {{cs}}{ea}{{ce}} each".format(s = s,
sh = afall.share_to_str(shares), ea = afall.cp_to_str(even_cp))
even_cp *= shares
if even_cp != amount_cp:
odd_cp = int(amount_cp - even_cp)
text += " and {{cs}}{n}{{ce}} for the party".format(n = afall.cp_to_str(odd_cp))
amount_cp = int(even_cp)
for share in pools_by_share:
for i in pools_by_share[share]:
tmp = share * amount_cp / shares
if tmp != float(int(tmp)):
raise ValueError("Share didn't come out even")
tmp = int(tmp) - i['coins']['value_cp']
if tmp < 0:
raise ValueError("Share Too large!")
i['amount_cp'] = tmp
if note:
text += ' ' + note
tmp = afall.journal(date, by, members.keys(), amount_cp + odd_cp, None, text, part_of)
if part_of is None:
part_of = tmp
bank = afall.get_char_money(afall.party)
for i in bank:
name = i.get('abbrev', i['coin'])
if name in coins:
i['quantity'] -= coins[name]
for j in pools_by_share:
for k in pools_by_share[j]:
for l in k['coins']:
if l == 'amount_cp':
continue
if l == name:
i['quantity'] -= k['coins'][l]
pools_by_size = {}
for i in pools_by_share:
for j in pools_by_share[i]:
tmp = len(j['members'])
if tmp in pools_by_size:
pools_by_size[tmp].append(j)
else:
pools_by_size[tmp] = [j]
pools = []
l = pools_by_size.keys()
l.sort(reverse = True)
for i in l:
for j in pools_by_size[i]:
# print "Debug: appending ", j
pools.append(j)
# print "Debug: pools", pools
for i in coins:
if i == 'value_cp':
continue
cpe = afall.coins_byname[i]['copper_equiv']
for j in pools:
a = j['amount_cp']
if cpe > a:
continue
tmp = len(j['members'])
n = int(coins[i] / tmp)
if n == 0:
continue
if n * cpe > a:
n = int(a/cpe)
if i in j['coins']:
j['coins'][i] += n
else:
j['coins'][i] = n
coins[i] -= tmp * n
j['coins']['value_cp'] += tmp * n * cpe
j['amount_cp'] -= n * cpe
if coins[i]:
raise ValueError("coins didn't divide evenly: {n} {i}".format(n=coins[i], i=i))
for i in bank:
cpe = i['copper_equiv']
if i['priority'] == 0 or i['priority'] == 1:
continue
for j in pools:
a = j['amount_cp']
if cpe > a:
continue
tmp = len(j['members'])
n = int(i['quantity'] / tmp)
if n == 0:
continue
if n * cpe > a:
n = int(a/cpe)
name = i.get('abbrev', i['coin'])
if name in j['coins']:
j['coins'][name] += n
else:
j['coins'][name] = n
i['quantity'] -= tmp * n
j['coins']['value_cp'] += tmp * n * cpe
j['amount_cp'] -= n * cpe
for i in pools:
if i['amount_cp']:
raise ValueError("Need more money: {i} undistributed to {c} after {n}".format(i = afall.cp_to_str(i['amount_cp']), c = i['members'], n = afall.coins_to_str(i['coins'])))
tmp = afall.coins_to_str(i['coins'])
for char in i['members']:
if args.verbose:
afall.pw(tmp, "to", char, max = 130)
afall.chars_move_cash({'date': date, 'cash': i['coins'],
'by': by, 'to': char, 'part_of': part_of, 'journ_to': journ_to})
def do_cancel(d):
afall.check_dict(d, must = ('date', ))
date = d.pop('date')
note = d.pop('note', None)
for char in d:
payable = afall.get_char_debts(char, 'Payable')
receivable = afall.get_char_debts(char, 'Receivable')
if len(payable) == 0 or len(receivable) == 0:
continue
part_of = afall.journal(date, char, None, None, None, "cancelled debts")
# payable holds a list of of who we owe money to, sorted by who we owe it to
# receivable holds a list of who owes us money, sorted by who they are
while len(payable)> 0 and len(receivable)>0:
if payable[0]['to'] < receivable[0]['by']:
payable = payable[1:]
continue
if receivable[0]['by'] < payable[0]['to']:
receivable = receivable[1:]
continue
# if we get here we've found a pair of debts to/from the same person
# Now figure out how many debts of each type we have with that person
pay = payable[0]['to']
pay_order = payable[0]['order']
pay_cp = 0
pay_end = 0
while True:
pay_cp += payable[pay_end]['amount_cp']
if pay_end == len(payable)-1 or payable[pay_end+1]['to'] != pay or payable[pay_end+1]['order'] != pay_order:
break
pay_end += 1
rec = receivable[0]['by']
if rec != pay:
sys.stderr.write("\nERROR {date} rec {rec} and pay {pay} ids don't match for {char}\n".format(date = date, rec = rec, pay = pay, char = char))
return
rec_order = receivable[0]['order']
rec_cp = 0
rec_end = 0
while True:
rec_cp += receivable[rec_end]['amount_cp']
if rec_end == len(receivable)-1 or receivable[rec_end+1]['by'] != rec or receivable[rec_end+1]['order'] != rec_order:
break
rec_end += 1
# ok, figure out the total debt to be cancelled
if pay_cp < rec_cp:
cancel_cp = pay_cp
else:
cancel_cp = rec_cp
if args.verbose:
afall.pw(char, pay, pay_cp, rec_cp, max = 130)
# modify all the payable debts appropiaately
pay_cancel_cp = cancel_cp
i = 0
while i <= pay_end:
tmp = payable[i]
cur_cp = tmp['amount_cp']
cxl_cp = afall.divide_cp(cur_cp, pay_cancel_cp, pay_cp)
if args.verbose:
afall.pw(char, "to", pay, "was", cur_cp, "cxl", cxl_cp, max = 130)
if abs(cur_cp - cxl_cp) < 3:
if cur_cp != cxl_cp:
sys.stderr.write("\nWARNING {date} losing track of a payable copper or two {by} {cur_cp} {to} {lost}\n".format(date = date, by = char, cur_cp = afall.cp_to_str(cur_cp), to = pay, lost = cur_cp-cxl_cp))
afall.delete_debt( {'date': date, 'debt_id': tmp['debt_id'], 'by': char, 'to': pay, 'amount_cp': cur_cp, 'part_of': part_of, 'note': note, 'item': tmp.get('item'), 'verb': 'completely cancelled'})
if i == 0:
payable = payable[1:]
pay_end -= 1
else:
sys.stderr.write("\nWARNING {date} deleting payable debt {p} at {i}\n".format(date = date, p = payable, i = i))
i += 1
else:
afall.debt_lower_owed({'date': date, 'debt_id': tmp['debt_id'], 'by': char, 'to': pay, 'amount_cp': cur_cp,
'item': tmp.get('item'), 'part_of': part_of, 'note': note, 'lower_cp': cxl_cp, 'verb': 'cancelled'})
i += 1
pay_cancel_cp -= cxl_cp
tmp['amount_cp'] -= cxl_cp
pay_cp -= cur_cp
# modify all the receivable debts appropriately
rec_cancel_cp = cancel_cp
i = 0
while i <= rec_end:
tmp = receivable[i]
cur_cp = tmp['amount_cp']
cxl_cp = afall.divide_cp(cur_cp, rec_cancel_cp, rec_cp)
if args.verbose:
afall.pw(pay, "to", char, "was", cur_cp, "cxl", cxl_cp, max = 130)
if abs(cur_cp - cxl_cp) < 3:
if cur_cp != cxl_cp:
sys.stderr.write("\nWARNING {date} losing track of a receivable copper or two {by} {cp} {to} {lost}\n".format(date = date, by = pay, cp = afall.cp_to_str(cur_cp), to = char, lost = cur_cp-cxl_cp))
afall.delete_debt({'date': date, 'debt_id': tmp['debt_id'], 'to': char, 'by': pay, 'amount_cp': cur_cp, 'part_of': part_of, 'note': note, 'item': tmp['item'], 'verb': 'completely cancelled'})
if i == 0:
receivable = receivable[1:]
rec_end -= 1
else:
sys.stderr.write("\nWARNING {date} deleting debt {d} at {i}\n".format(date = date, d = receivable, i = i))
i += 1
else:
afall.debt_lower_owed({'date': date, 'debt_id': tmp['debt_id'], 'by': pay, 'to': char, 'amount_cp': cur_cp,
'item': tmp.get('item'), 'part_of': part_of, 'note': note, 'lower_cp': cxl_cp, 'verb': 'cancelled'})
i += 1
rec_cancel_cp -= cxl_cp
tmp['amount_cp'] -= cxl_cp
rec_cp -= cur_cp
def do_find(d):
afall.check_dict(d, must = ('name', 'found'), may = ('owner', 'holder', 'note',
'value', 'date_found', 'date', 'xfrd'))
e = {}
e['item'] = d['name']
e['finder'] = d['found']
if 'owner' in d:
e['owner'] = d['owner']
if 'holder' in d:
e['holder'] = d['holder']
else:
e['holder'] = e['owner']
else:
e['owner'] = e['finder']
e['holder'] = d.get('holder')
e['note'] = d.get('note')
e['value_cp'] = afall.str_to_cp(d.get('value'))
if 'date_found' in d:
e['date_found'] = d['date_found']
elif 'date' in d:
e['date_found'] = d['date']
else:
e['date_found'] = afall.party_to_date(d['found'])
if 'date_xfrd' in d:
e['date_xfrd'] = d['date_xfrd']
else:
e['date_xfrd'] = d.get('xfrd')
if args.verbose:
afall.pw("Creating item", e, max = 130)
afall.insert_item(e)
def do_leave(d):
afall.check_dict(d, must = ('date', ))
date = d.pop('date')
note = d.pop('note', None)
for char in d:
afall.char_leave_parties(date, char, note)
def do_journal(d):
afall.check_dict(d, must = ('date', 'by', 'note'), may = ('to', 'cash', 'virtual', 'part_of'))
afall.journal(d['date'], d['by'], d.get('to'), afall.str_to_cp(d.get('cash')), afall.str_to_cp(d.get('virtual')), d['note'], d.get('part_of'))
def _check_loop(chars, char, color):
if char not in chars:
chars[char] = {'debts': {}}
for debt in afall.get_char_debts(char, 'Payable'):
to = debt['to']
if to in chars[char]['debts']:
chars[char]['debts'][to] += debt['amount_cp']
else:
chars[char]['debts'][to] = debt['amount_cp']
for to in chars[char]['debts'].keys():
if to in chars and 'color' in chars[to] and chars[to]['color'] == color:
return [[char, chars[char]['debts'][to], to]]
oc = chars[char].get('color')
chars[char]['color'] = color
ret = _check_loop(chars, to, color)
if oc:
chars[char]['color'] = oc
else:
del chars[char]['color']
if ret:
if ret[0][0] != ret[-1][2]:
ret = [[char, chars[char]['debts'][to], to]] + ret
return ret
return None
def do_loop(d):
afall.check_dict(d, must = ('date', ), may = ('note', ))
date = d['date']
if 'note' in d:
note = d['note'] + ' with a pebble'
else:
note = 'with a pebble'
cur_color = 1
chars = {}
for char in afall.get_characters('Current'):
loop = _check_loop(chars, char, cur_color)
if loop:
m = loop[0][1]
for i in loop:
if i[1] < m:
m = i[1]
# afall.pw("loop: ", afall.cp_to_str(m), max = 130)
for i in loop:
# afall.pw(" ", i[0], 'owes', afall.cp_to_str(i[1]), 'to', i[2], max = 130)
if chars[i[0]]['debts'][i[2]] == m:
del chars[i[0]]['debts'][i[2]]
else:
chars[i[0]]['debts'][i[2]] -= m
do_pay({'date': date, 'by': i[0], 'to': i[2], 'amount_cp': m, 'virtual': True, 'note': note})
cur_color += 1
def do_coin(d):
afall.check_dict(d, must = ('name', 'copper_equiv'), may = ('priority', 'abbrev', 'note', 'date'))
afall.insert_coin(d['name'], d.get('abbrev'), d['copper_equiv'], d.get('priority', 0), d.get('note'))
def do_trade(d):
# Two possibilities cash=, for= to move cash
# or item=, for= to exchange items
part_of = d.get('part_of')
if 'item' in d:
afall.check_dict(d, must = ('date', 'item', 'for'), may = ('note', 'part_of'))
raise ValueError("trade items not implemented")
# if args.verbose:
# print "giving item", d
# afall.change_item(d['date'], d['item'], owner = d['to'], holder = d['to'], part_of = part_of)
return
afall.check_dict(d, must = ('date', 'to', 'by'), may = ('cash', 'for', 'note', 'part_of'))
date = d['date']
by = d['by']
to = d['to']
if 'cash' not in d and 'for' not in d:
raise ValueError("Need cash or for")
if 'cash' in d:
cash = afall.str_to_coins(d['cash'])
if 'for' not in d:
tradefor = afall.char_find_money(to, cash['value_cp'])
if 'for' in d:
tradefor = afall.str_to_coins(d['for'])
if 'cash' not in d:
cash = afall.char_find_money(by, tradefor['value_cp'])
if cash['value_cp'] != tradefor['value_cp']:
raise ValueError("unequal trade {c} != {f}".format(c = cash, f = tradefor))
# if args.verbose:
# print "{by} traded {c} for {f} with {to}".format(by = by, c = afall.coins_to_str(cash), f = afall.coins_to_str(tradefor), to = to)
text = 'exchanged {{cs}}{c1}{{ce}} for {{cs}}{c2}{{ce}} with {{to}}'.format(c1 = afall.coins_to_str(cash), c2 = afall.coins_to_str(tradefor))
tmp = afall.journal(date, by, to, None, cash['value_cp'], text, part_of)
if not part_of:
part_of = tmp
afall.chars_move_cash({'date': date, 'by': by, 'to': to, 'cash': cash, 'note': d.get('note'), 'part_of': part_of, 'journal': False})
afall.chars_move_cash({'date': date, 'by': to, 'to': by, 'cash': tradefor, 'note': d.get('note'), 'part_of': part_of, 'journal': False})
def do_cons(d):
afall.check_dict(d, must = ('date', ))
date = d.pop('date')
wi_char = d.pop('with', afall.party)
note = d.pop('note', None)
part_of = d.pop('part_of', None)
if len(d) == 0 and wi_char != afall.party:
d = {afall.party: True}
for by_char in d:
wi_coins = afall.get_char_money(wi_char)
wi_q_table = {}
for i in wi_coins:
if i['priority']:
wi_q_table[i['coin'].format(s = '', es = '')] = i['quantity']
by_coins = afall.get_char_money(by_char)
afall.pw("by", by_char, by_coins, "with", wi_char, wi_coins, wi_q_table, max = 130)
by_mv = {'value_cp': 0}
wi_mv = {'value_cp': 0}
did = True
while did:
did = False
nxt = []
# print 'start', by_coins
while len(by_coins) > 0:
by_coin = by_coins.pop()
if by_coin['priority'] == 0:
continue
by_name = by_coin['coin'].format(s = '', es = '')
by_fname = by_coin['coin']
by_q = by_coin['quantity']
if by_fname in wi_mv:
by_q += wi_mv[by_fname]
if by_q <= 20:
continue
by_cpe = by_coin['copper_equiv']
wi_name = None
wi_cpe = 0
wi_q = 0
for tmp_cpe in afall.coins_bycpe:
if tmp_cpe <= by_cpe or tmp_cpe % by_cpe != 0 or by_cpe * by_q < tmp_cpe:
continue
for j in afall.coins_bycpe[tmp_cpe]:
name = j['coin'].format(s = '', es = '')
if name in wi_q_table and (tmp_cpe > wi_cpe or (tmp_cpe == wi_cpe and wi_q_table[name] > wi_q)):
wi_cpe = tmp_cpe
wi_name = name
wi_fname = j['coin']
wi_q = wi_q_table[name]
if wi_cpe == 0:
nxt.append(by_coin)
continue
did = True
wi_n = min(wi_q, int((by_cpe * by_q) / wi_cpe))
by_n = int(wi_n * (wi_cpe / by_cpe))
if wi_n * wi_cpe != by_n * by_cpe:
raise ValueError("Huh? What?")
# print 'has', by_n, by_name, "out of ", by_q, "at", by_cpe, 'for', wi_n, wi_name, "out of", wi_q, "at", wi_cpe
wi_q_table[by_name] = by_n + wi_q_table.get(by_name, 0)
if wi_q_table[wi_name] == wi_n:
del wi_q_table[wi_name]
else:
wi_q_table[wi_name] -= wi_n
wi_mv = afall.coins_add(wi_mv, wi_fname, wi_n)
if by_fname in wi_mv:
tmp = min(by_n, wi_mv[by_fname])
wi_mv = afall.coins_add(wi_mv, by_fname -tmp)
by_n -= tmp
if by_n:
by_mv = afall.coins_add(by_mv, by_fname, by_n)
if by_coin['quantity'] > by_n:
by_coin['quantity'] -= by_n
by_coins.append(by_coin)
# print "looping", nxt, did, wi_q_table
by_coins = nxt
if by_mv['value_cp'] != wi_mv['value_cp']:
raise ValueError("What's wrong?")
if by_mv['value_cp'] > 0:
if args.verbose:
afall.pw("{by_char} consolidate {by_mv} into {wi_mv} with {wi_char}".format(
by_char = by_char, by_mv = afall.coins_to_str(by_mv),
wi_char = wi_char, wi_mv = afall.coins_to_str(wi_mv)), max = 130)