From ecac9d9c0714c7ec3a63d67e077af15cacd3f02b Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Sun, 9 Jun 2019 16:14:46 +0200 Subject: 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 !!! --- tanken.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'tanken.py') 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 -- cgit v1.2.3