diff options
| author | Florian Fischer <florian.fl.fischer@fau.de> | 2017-11-09 17:23:33 +0100 |
|---|---|---|
| committer | Florian Fischer <florian.fl.fischer@fau.de> | 2017-11-09 17:23:33 +0100 |
| commit | 616318a844dc8f253fb22e4985a3953ede00fc89 (patch) | |
| tree | ddd79d3d034b5bf1afd6b2e3bdc694ccc07fbbcc | |
| parent | 503400e976eb130e2d00148164fb9ca3febc6909 (diff) | |
| download | geldschieberbot-616318a844dc8f253fb22e4985a3953ede00fc89.tar.gz geldschieberbot-616318a844dc8f253fb22e4985a3953ede00fc89.zip | |
add split feature
update help string
improve error messages
improve identation
| -rw-r--r-- | geldschieberbot.py | 154 |
1 files changed, 101 insertions, 53 deletions
diff --git a/geldschieberbot.py b/geldschieberbot.py index 4274d32..9bf0a0b 100644 --- a/geldschieberbot.py +++ b/geldschieberbot.py @@ -73,13 +73,21 @@ def create_help(): return """ Usage: send a message starting with '!' followed by a command Commands: -ls | list - print all registered members -help - print this help message -reg name - register the sender with the name: name -sum [name] - print a summary -schieb amount recipient - give money to recipient -zieh amount donor - get money from donor - +ls | list - print all registered members +help - print this help message +reg name - register the sender with the name: name +sum [name] - print a summary + +split amount person [persons] - split amount between the sender and persons +teil amount person [persons] - split amount between the sender and persons + +schieb amount recipient - give money to recipient +gib amount recipient - give money to recipient +zieh amount donor - get money from donor +nimm amount donor - get money from donor + +tanken amount person [info] - calculate fuel costs and add them to the balance + Happy Geldschieben! """ @@ -133,29 +141,31 @@ def handle_input(inp): # supported commands are: # "!reg" register a name for this number # "!sum" send a summary to the group + # "!list" "!ls" list members + # "!help" print all commands + # "!split" "!teil" split amount between group # "!schieb" "!gib" give money to somebody # "!zieh" "!nimm" add debt of somebody - # "!list" "!ls" list members # "!tanken" calculate fuel cost parts - # "!help" print all commands if cmd == "!reg": if len(w) != 2: send('ERROR: not in form "!reg name"') + continue + + if w[1] in name2num: + send("ERROR: name already registered") + elif sender_number in num2name: + send("ERROR: you are already registered") else: - if w[1] in name2num: - send("ERROR: name already registered") - elif sender_number in num2name: - send("ERROR: you are already registered") - else: - num2name[sender_number] = w[1] - name2num[w[1]] = sender_number + num2name[sender_number] = w[1] + name2num[w[1]] = sender_number - # add to balance - nm = {} - for m in balance: - balance[m][w[1]] = 0 - nm[m] = 0 - balance[w[1]] = nm + # add to balance + nm = {} + for m in balance: + balance[m][w[1]] = 0 + nm[m] = 0 + balance[w[1]] = nm elif cmd == "!sum": if len(w) == 1: @@ -174,47 +184,85 @@ def handle_input(inp): elif cmd == "!help": send(create_help()) + elif cmd == "!split" or "!teil": + + if not sender_number in num2name: + send('ERROR: you must register first') + continue + + if len(w) < 3: + send('ERROR: not in form "!{} [amount] [name]+"'.format(cmd)) + continue + + try: + amount = to_cent(w[1]) + except: + send("ERROR: amount musst be a number") + continue + + # -2 because amount and cmd; +1 because the sender is part of the group + persons = len(w) - 2 + 1 + amount_per_person = int(amount/persons) + + sender = num2name[sender_number] + + msg = "Split {} between {} -> {} each\n".format(to_euro(amount), + persons, + to_euro(amount_per_person)) + for p in w[2:]: + if not p in name2num: + msg += p + " not known. Please take care manually\n" + else: + balance[sender][recipient] -= amount_per_person + balance[recipient][sender] += amount_per_person + + msg += "New Balance:\n" + msg += create_summary(sender) + send(msg) + elif cmd in ["!schieb", "!gib", "!zieh", "!nimm"]: if len(w) != 3: - send('ERROR: not in form "!cmd amount recipient"') + send('ERROR: not in form "!{} amount recipient"'.format(cmd)) + continue + + if not sender_number in num2name: + send('ERROR: you must register first') + continue + + if w[1] in name2num: + recipient = w[1] + amount = w[2] + elif w[2] in name2num: + recipient = w[2] + amount = w[1] else: - if not sender_number in num2name: - send('ERROR: you must register first') - else: - if w[1] in name2num: - recipient = w[1] - amount = w[2] - elif w[2] in name2num: - recipient = w[2] - amount = w[1] - else: - send('ERROR: recipient not known') - continue + send('ERROR: recipient not known') + continue - sender = num2name[sender_number] + sender = num2name[sender_number] - try: - amount = to_cent(amount) - except: - send("ERROR: amount musst be a number") - continue + try: + amount = to_cent(amount) + except: + send("ERROR: amount musst be a number") + continue - if amount < 0: - send("ERROR: amount must be positiv") - continue + if amount < 0: + send("ERROR: amount must be positiv") + continue - if cmd in ["!zieh", "!nimm"]: - amount *= -1 + if cmd in ["!zieh", "!nimm"]: + amount *= -1 - balance[sender][recipient] -= amount - balance[recipient][sender] += amount + balance[sender][recipient] -= amount + balance[recipient][sender] += amount - p_balance = balance[sender][recipient] + p_balance = balance[sender][recipient] - send("New Balance: {} {} {} {}\n".format(sender, - ("->" if p_balance > 0 else "<-"), - to_euro(abs(p_balance)), - recipient)) + send("New Balance: {} {} {} {}\n".format(sender, + ("->" if p_balance > 0 else "<-"), + to_euro(abs(p_balance)), + recipient)) elif cmd == "!tanken": if len(w) < 3: |
