aboutsummaryrefslogtreecommitdiff
path: root/tanken.py
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fl.fischer@fau.de>2019-06-09 16:14:46 +0200
committerFlorian Fischer <florian.fl.fischer@fau.de>2019-06-09 16:14:46 +0200
commitecac9d9c0714c7ec3a63d67e077af15cacd3f02b (patch)
tree4158f873bf82de289109c0d56bb022569bcb75a4 /tanken.py
parenta32b04a623d13badac51ffeca9cc991033fc8001 (diff)
downloadgeldschieberbot-ecac9d9c0714c7ec3a63d67e077af15cacd3f02b.tar.gz
geldschieberbot-ecac9d9c0714c7ec3a63d67e077af15cacd3f02b.zip
add new car commands and make tanken aware of cars
Cars are names associated to a service charge. They are stored in the state file in a dict called "cars". Each car has its own balance from which money can be pulled or pushed. Because of this cars and users can't share a name. Service charges are calculated per drive and split among all drivers similar to fuel cost. If a driver is not known by geldschieberbot the recipient of the "tanken" cmd is held accountable and has to collect the service charge manually just like fuel cost. NOTE: This change breaks geldschieberbot if the state file does not contain the field "cars" !!! You have to add it manually !!!
Diffstat (limited to 'tanken.py')
-rw-r--r--tanken.py19
1 files changed, 14 insertions, 5 deletions
diff --git a/tanken.py b/tanken.py
index 2ca80bd..b7064cd 100644
--- a/tanken.py
+++ b/tanken.py
@@ -1,8 +1,9 @@
#!/bin/env python3
# cost should be given in cents
-def tanken(drives, cost):
+def tanken(drives, cost, service_charge=0):
passengers = {}
+ service_charges = {}
distance = 0.
drives = [d.split(' ') for d in drives]
@@ -18,19 +19,27 @@ def tanken(drives, cost):
# collect distances per passenger
for p in d[1:]:
if p not in passengers:
- passengers[p] = [d[0],0]
+ passengers[p] = {"distance": d[0],
+ "cost": 0,
+ "service_charge": 0}
else:
- passengers[p][0] += d[0]
+ passengers[p]["distance"] += d[0]
# calculate cost per kilometer
if distance <= 0:
return None, "Driven distance must be greater than 0!"
+
c = cost/distance
for d in drives:
- # calculate cost per drive split through passengers
+ # calculate cost per drive split among passengers
c_d = int(c * d[0] / (len(d) - 1))
for p in d[1:]:
- passengers[p][1] += c_d
+ passengers[p]["cost"] += c_d
+
+ # calculate service charge per drive split among passengers
+ sc_d = int(service_charge * d[0] / (len(d) - 1))
+ for p in d[1:]:
+ passengers[p]["service_charge"] += sc_d
return passengers, None