aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fl.fischer@fau.de>2017-10-27 17:27:11 +0200
committerFlorian Fischer <florian.fl.fischer@fau.de>2017-11-09 16:50:41 +0100
commit8aa41f02c4c56a4e17db4ba2d51bc251e9c97982 (patch)
tree23bcd2f23765ed415f33d12f19f615fdb8721107
parentf07c7625327104eb8ff58d6c4cae173904338a65 (diff)
downloadgeldschieberbot-8aa41f02c4c56a4e17db4ba2d51bc251e9c97982.tar.gz
geldschieberbot-8aa41f02c4c56a4e17db4ba2d51bc251e9c97982.zip
add tanken feature
-rw-r--r--geldschieberbot.py72
-rw-r--r--tanken.py25
2 files changed, 81 insertions, 16 deletions
diff --git a/geldschieberbot.py b/geldschieberbot.py
index f24a192..e158076 100644
--- a/geldschieberbot.py
+++ b/geldschieberbot.py
@@ -5,6 +5,8 @@ import os
import subprocess
import sys
+import tanken
+
"""Dict of dicts associating a second person to an amount"""
balance = {}
@@ -86,25 +88,30 @@ def handle_input(inp):
sender_number = w[-3]
body_found = False
- body = ""
+ body = []
# message is not in the group with group_id
not_in_group = True
for idx, line in enumerate(lines[1:]):
+ # Collect body till we find Group ID:
+ # THIS COULD BREAK VERY EASILY
if line[0:6] == "Body: ":
- body = line[6:]
+ body.append(line[6:])
body_found = True
- # Only one line bodies are supported
elif body_found:
- not_in_group = lines[idx+1].strip() == "Id: " + group_id
- break
+ if not line == "Group info:":
+ body.append(line)
+ else:
+ not_in_group = lines[idx+1].strip() == "Id: " + group_id
+ break
if not_in_group:
continue
- # strip of '\n'
- w = body.split(' ')
+ w = body[0].split(' ')
+
+ cmd = w[0]
# supported commands are:
# "!reg" register a name for this number
@@ -112,8 +119,9 @@ def handle_input(inp):
# "!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 w[0] == "!reg":
+ if cmd == "!reg":
if len(w) != 2:
send('ERROR: not in form "!reg name"')
else:
@@ -132,7 +140,7 @@ def handle_input(inp):
nm[m] = 0
balance[w[1]] = nm
- elif w[0] == "!sum":
+ elif cmd == "!sum":
if len(w) == 1:
send(create_total_summary())
elif len(w) == 2:
@@ -143,7 +151,13 @@ def handle_input(inp):
else:
send('ERROR: not in form "!sum [name]"')
- elif w[0] in ["!schieb", "!gib", "!zieh", "!nimm"]:
+ elif cmd == "!list" or cmd == "!ls":
+ send(create_members())
+
+ elif cmd == "!help":
+ send(create_help())
+
+ elif cmd in ["!schieb", "!gib", "!zieh", "!nimm"]:
if len(w) != 3:
send('ERROR: not in form "!cmd amount recipient"')
else:
@@ -161,6 +175,7 @@ def handle_input(inp):
continue
sender = num2name[sender_number]
+
try:
amount = float(amount)
except:
@@ -171,7 +186,7 @@ def handle_input(inp):
send("ERROR: amount must be positiv")
continue
- if w[0] in ["!zieh", "!nimm"]:
+ if cmd in ["!zieh", "!nimm"]:
amount *= -1
balance[sender][recipient] -= amount
@@ -179,13 +194,38 @@ def handle_input(inp):
p_balance = balance[sender][recipient]
- send("New Balance: {0} {1} {2:g} {3}\n".format(sender, ("->" if p_balance > 0 else "<-"), abs(p_balance), recipient))
+ send("New Balance: {0} {1} {2:g} {3}\n".format(sender,
+ ("->" if p_balance > 0 else "<-"),
+ abs(p_balance),
+ recipient))
- elif w[0] == "!list" or w[0] == "!ls":
- send(create_members())
+ elif cmd == "!tanken":
+ if len(w) < 3:
+ send('ERROR: !tanken not in form "!tanken amount person [info]"')
+ continue
+ amount = w[1]
+ if w[2] in name2num:
+ recipient = w[2]
+ else:
+ recipient = num2name[sender_number]
+ parts, err = tanken(body[1:], amount)
+
+ if err != None:
+ send("ERROR: " + err)
+ continue
+
+ msg = ""
+ for p in parts.items():
+ if p in name2num:
+ balance[recipient][p[0]] -= p[1]
+ balance[p[0]][recipient] += p[1]
+ else:
+ msg += p[0] + " not known. Please take care of the amount " + p[1] + " manually\n"
+
+ msg += "New Balance:\n"
+ msg += create_summary(recipient)
+ send(msg)
- elif w[0] == "!help":
- send(create_help())
def main():
global group_id
diff --git a/tanken.py b/tanken.py
new file mode 100644
index 0000000..4685fee
--- /dev/null
+++ b/tanken.py
@@ -0,0 +1,25 @@
+#!/bin/env python3
+
+def tanken(drives, cost):
+ passagers = {}
+ distance = 0.
+
+ for d in drives:
+ try:
+ distance += int(d[0])
+ except:
+ return None, "Lines have to start with the driven distance!"
+ for p in d[1:]:
+ if p not in passagers:
+ passagers[p] = [int(d[0]),0]
+ else:
+ passagers[p][0] += int(d[0])
+
+ c = cost/distance
+
+ for d in drives:
+ c_d = c * int(d[0]) / (len(d) - 1)
+ for p in d[1:]:
+ passagers[p][1] += c_d
+
+ return passagers, None