From b33c73a15364ccf61de462a11a807b840bfd77f8 Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Mon, 27 Nov 2017 17:30:41 +0100 Subject: add rewind command "fuck" --- geldschieberbot.py | 68 ++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 56 insertions(+), 12 deletions(-) diff --git a/geldschieberbot.py b/geldschieberbot.py index 2bfa1c8..2b2d564 100644 --- a/geldschieberbot.py +++ b/geldschieberbot.py @@ -13,10 +13,21 @@ balance = {} name2num = {} num2name = {} +"""Dict associating users with their last change""" +last_change = {} + group_id = "GELD!!!" send_cmd = "signal-cli -u irgendwer send -g irgendwem" +def record(sender, recipient, donor, amount): + """Apply change to the balance and save it""" + + balance[donor][recipient] -= amount + balance[recipient][donor] += amount + + last_change[sender] = [recipient, donor, amount] + def to_cent(euro): euro = euro.split('.') if len(euro) > 2: @@ -88,6 +99,8 @@ nimm amount donor - get money from donor tanken amount person [info] - calculate fuel costs and add them to the balance +fuck - rewind last change + Happy Geldschieben! """ @@ -147,6 +160,7 @@ def handle_input(inp): # "!schieb" "!gib" give money to somebody # "!zieh" "!nimm" add debt of somebody # "!tanken" calculate fuel cost parts + # "!fuck" rewind last change if cmd == "!reg": if len(w) != 2: send('ERROR: not in form "!reg name"') @@ -204,7 +218,11 @@ def handle_input(inp): persons = len(w) - 2 + 1 amount_per_person = int(amount/persons) - sender = num2name[sender_number] + if sender_number in num2name: + recipient = num2name[sender_number] + else: + send("ERROR: you must register first") + continue msg = "Split {} between {} -> {} each\n".format(to_euro(amount), persons, @@ -213,11 +231,10 @@ def handle_input(inp): if not p in name2num: msg += p + " not known. Please take care manually\n" else: - balance[sender][p] -= amount_per_person - balance[p][sender] += amount_per_person + record(sender_number, recipient, p, amount_per_person) msg += "New Balance:\n" - msg += create_summary(sender) + msg += create_summary(recipient) send(msg) elif cmd in ["!schieb", "!gib", "!zieh", "!nimm"]: @@ -254,8 +271,7 @@ def handle_input(inp): if cmd in ["!zieh", "!nimm"]: amount *= -1 - balance[sender][recipient] -= amount - balance[recipient][sender] += amount + record(sender_number, sender, recipient, amount) p_balance = balance[sender][recipient] @@ -276,8 +292,11 @@ def handle_input(inp): if w[2] in name2num: recipient = w[2] - else: + elif sender_number in name2num: recipient = num2name[sender_number] + else: + send("ERROR: recipient unknown") + continue parts, err = tanken.tanken(body[1:], amount) @@ -290,8 +309,7 @@ def handle_input(inp): msg += p[0] + ": {}km = {}\n".format(p[1][0], to_euro(p[1][1])) if p[0] != recipient: if p[0] in name2num: - balance[recipient][p[0]] -= p[1][1] - balance[p[0]][recipient] += p[1][1] + record(sender_number, recipient, p[0], p[1][1]) else: msg += p[0] + " not known. Please take care manually\n" @@ -299,30 +317,53 @@ def handle_input(inp): msg += create_summary(recipient) send(msg) + elif cmd == "!fuck": + if not sender_number in num2name: + send("ERROR: you must register first") + continue + + if not sender_number in last_change: + send("Nothing to rewind") + continue + + changes = last_change[sender_number] + + msg = num2name[sender_number] + ": sorry I fucked up!\n Rewinding:\n" + for change in changes: + msg += "{} -> {} {}\n".format(*change) + record(sender_number, change[1], change[0], change[2]) + + send(msg) + def main(): global group_id group_id = os.environ["GSB_GROUP_ID"] - global balance store_path = os.environ["GSB_STORE_PATH"] balance_path = store_path + "/balance.json" registration_path = store_path + "/registration.json" + last_change_path = store_path + "/last_change.json" - global name2num - global num2name global send_cmd send_cmd = os.environ["GSB_SEND_CMD"] + global balance if os.path.isfile(balance_path): balance = json.load(open(balance_path, "r")) + global name2num + global num2name if os.path.isfile(registration_path): name2num = json.load(open(registration_path, "r")) for name in name2num: num2name[name2num[name]] = name + global last_change + if os.path.isfile(last_change_path): + last_change = json.load(open(last_cahnge_path, "r")) + handle_input(sys.stdin.read()) with open(balance_path, "w") as f: @@ -331,5 +372,8 @@ def main(): with open(registration_path, "w") as f: json.dump(name2num, f) + with open(last_change_path, "w") as f: + json.dump(last_change, f) + if __name__ == "__main__": main() -- cgit v1.2.3