drink math + functions to manage mongodb
parent
c888b9d325
commit
322d4ebe3e
92
server.py
92
server.py
|
@ -1,6 +1,7 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from time import sleep
|
||||||
|
|
||||||
import pymongo
|
import pymongo
|
||||||
|
|
||||||
|
@ -10,24 +11,48 @@ def checkpoint(message):
|
||||||
"""
|
"""
|
||||||
print("[Checkpoint] {}".format(message))
|
print("[Checkpoint] {}".format(message))
|
||||||
|
|
||||||
def total_drinks(id):
|
def get_info(id):
|
||||||
"""Looks up the number of drinks a given user has consumed
|
"""Looks up all data for an id
|
||||||
"""
|
"""
|
||||||
# Lookup
|
checkpoint("Looking up data for id \'{}\'".format(id))
|
||||||
return 10
|
|
||||||
|
|
||||||
def remove_drinks(id=None, finished_drinks):
|
# Connect to mongodb & return user
|
||||||
"""Remove finished drinks. Removes all drinks from all ids
|
collection = pymongo.MongoClient().group23.bac_monitoring
|
||||||
if id==None
|
return collection.find({'id': 123456})[0]
|
||||||
|
|
||||||
|
def add_drink(id):
|
||||||
|
"""Adds 1 drink to the user
|
||||||
"""
|
"""
|
||||||
|
checkpoint("Adding 1 drink for id \'{}\'".format(id))
|
||||||
|
|
||||||
|
## Connect to mongodb & add drink
|
||||||
|
collection = pymongo.MongoClient().group23.bac_monitoring
|
||||||
|
|
||||||
|
curr_time = int(datetime.today().timestamp())
|
||||||
|
collection.update({'id': id}, {'$push': {'drinks': curr_time}})
|
||||||
|
|
||||||
|
def remove_drinks(id=None, finished_drinks=None):
|
||||||
|
"""Remove finished drinks. Removes all drinks from all ids
|
||||||
|
if id==None, all drinks from specific if if finished_drinks==None, otherwise
|
||||||
|
removes drinks in finished_drinks
|
||||||
|
"""
|
||||||
|
# Connecto to mongodb
|
||||||
|
collection = pymongo.MongoClient().group23.bac_monitoring
|
||||||
|
|
||||||
# Remove all drinks from all users
|
# Remove all drinks from all users
|
||||||
if id == None:
|
if id == None:
|
||||||
# Iterate through all users
|
checkpoint('Removing all drinks for all users')
|
||||||
# Remove set drink list to empty
|
collection.update({}, {'$set': {'drinks': []}}, multi=True)
|
||||||
|
# Remove all drinks for specific user
|
||||||
|
elif finished_drinks == None:
|
||||||
|
checkpoint("Removing all drinks for user \'{}\'".format(id))
|
||||||
|
collection.update({'id': id}, {'$set': {'drinks': []}})
|
||||||
|
# Remove only specified drinks
|
||||||
else:
|
else:
|
||||||
# Lookup specific user
|
checkpoint("Removing drinks: \'{}\' for user \'{}\'"
|
||||||
# Remove drinks < start_time
|
.format(finished_drinks, id))
|
||||||
|
for drink in finished_drinks:
|
||||||
|
collection.update({'id': id}, {'$pull': {'drinks': drink}})
|
||||||
|
|
||||||
def allowed_drinks(id):
|
def allowed_drinks(id):
|
||||||
"""Gets the amount of drinks someone is allowed to have, taking into account
|
"""Gets the amount of drinks someone is allowed to have, taking into account
|
||||||
|
@ -40,7 +65,9 @@ def allowed_drinks(id):
|
||||||
'180': .035, '200': .03, '220': .033, '240': .025},
|
'180': .035, '200': .03, '220': .033, '240': .025},
|
||||||
'F': {'100': .07, '120': .06, '140': .05, '160': .04,
|
'F': {'100': .07, '120': .06, '140': .05, '160': .04,
|
||||||
'180': .04, '200': .035, '220': .03, '240': .03}}
|
'180': .04, '200': .035, '220': .03, '240': .03}}
|
||||||
one_drink = bac_lookup[user_info['gender']][user_info['weight']]
|
# Lookup gender & rounded weight
|
||||||
|
weight = int(20 * round(float(user_info['weight']) / 20))
|
||||||
|
one_drink = bac_lookup[user_info['gender']][str(weight)]
|
||||||
|
|
||||||
# Add up BAC for each drink
|
# Add up BAC for each drink
|
||||||
bac = 0
|
bac = 0
|
||||||
|
@ -48,43 +75,44 @@ def allowed_drinks(id):
|
||||||
# According the the BAC chart, for ever 40 minutes .01% is subtracted from BAC
|
# According the the BAC chart, for ever 40 minutes .01% is subtracted from BAC
|
||||||
# and there are 2400 seconds on 40 minutes
|
# and there are 2400 seconds on 40 minutes
|
||||||
curr_time = int(datetime.today().timestamp()) / 2400
|
curr_time = int(datetime.today().timestamp()) / 2400
|
||||||
for drink in drinks
|
for drink in user_info['drinks']:
|
||||||
# Calculate drinking time and bac
|
# Calculate drinking time and bac
|
||||||
drink_time = drink / 2400
|
drink_time = drink / 2400
|
||||||
drinking_time = curr_time - drink_time
|
drinking_time = curr_time - drink_time
|
||||||
# Calculate BAC from specific drink, or remove it
|
# Calculate BAC from specific drink, or remove it
|
||||||
drink_bac = one_drink - (drinking_time * 01)
|
drink_bac = one_drink - (drinking_time * .01)
|
||||||
if drink_bac == 0:
|
if drink_bac == 0:
|
||||||
finished_drinks.append[drink]
|
finished_drinks.append[drink]
|
||||||
else:
|
else:
|
||||||
bac += drink_bac
|
bac += drink_bac
|
||||||
|
|
||||||
|
# Debugging
|
||||||
|
checkpoint("BAC for \'{}\': {}".format(id, bac))
|
||||||
|
|
||||||
# Remove all drinks not contributing to current BAC
|
# Remove all drinks not contributing to current BAC
|
||||||
remove_drinks(id, finished_drinks)
|
remove_drinks(id, finished_drinks)
|
||||||
|
|
||||||
# Intoxicated, no drinks consumed, or some drinks consumed
|
# Intoxicated, no drinks consumed, or some drinks consumed
|
||||||
if bac >= .06:
|
drinks_left = 0
|
||||||
return 0
|
if bac == 0:
|
||||||
elif bac == 0:
|
drinks_left = int(.06 / one_drink)
|
||||||
return int(.06 / one_drink)
|
elif bac < .06:
|
||||||
else:
|
drinks_left = int(.06 / bac)
|
||||||
return int(.06 / bac)
|
checkpoint("Allowed drinks for id \'{}\': {}".format(id, drinks_left))
|
||||||
|
return drinks_left
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
||||||
# Arguments
|
# Arguments
|
||||||
|
id = 123456
|
||||||
|
#remove_drinks(id, [1523206500])
|
||||||
|
remove_drinks(id)
|
||||||
|
add_drink(id)
|
||||||
|
sleep(2)
|
||||||
|
add_drink(id)
|
||||||
|
sleep(3)
|
||||||
|
add_drink(id)
|
||||||
|
|
||||||
# Connect to to collection
|
allowed_drinks(id)
|
||||||
# Assumes 'bac_monitoring' collection already exists
|
|
||||||
collection = pymongo.MongoClient().group23.bac_monitoring
|
|
||||||
|
|
||||||
## Add new drink
|
|
||||||
# Current time in hours
|
|
||||||
curr_time = int(datetime.today().timestamp())
|
|
||||||
collection.update({'id': 123456}, {'$push': {'drinks': curr_time}})
|
|
||||||
|
|
||||||
# Insert something
|
|
||||||
drink_data = collection.find({'id': 123456})[0]
|
|
||||||
print(drink_data)
|
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Reference in New Issue