Implemented apply temp meal

master
Davide Bongiovanni 6 years ago
parent 7cf3580844
commit 64396c29d8

@ -78,14 +78,36 @@ def getUpcomingMeals():
meals[-1]['friendly_selection_deadline'] = str(round(seconds_to_deadline / 60)) + ' minutes' meals[-1]['friendly_selection_deadline'] = str(round(seconds_to_deadline / 60)) + ' minutes'
meals[-1]['meal_time'] = meals[-1]['meal_date'].strftime('%H:%M') meals[-1]['meal_time'] = meals[-1]['meal_date'].strftime('%H:%M')
meals[-1]['meal_friendly_date'] = meals[-1]['meal_date'].strftime('%A %d %B') meals[-1]['meal_friendly_date'] = meals[-1]['meal_date'].strftime('%A %d %B')
meals[-1]['candidate'] = 'davide' meals[-1]['candidate'] = [l['name'] for l in leaderboard if l['name'] in meals[-1]['eaters']][0]
if (meals[-1]['meal_date'] - datetime.datetime.now()).total_seconds() < 0: if (meals[-1]['meal_date'] - datetime.datetime.now()).total_seconds() < 0:
meals[-1]['needs_confirmation'] = True meals[-1]['needs_confirmation'] = True
else: else:
meals[-1]['needs_confirmation'] = False meals[-1]['needs_confirmation'] = False
leaderboard = applyTempMeal(leaderboard, meals[-1])
r.close() r.close()
return meals return meals
def applyTempMeal(leaderboard, meal):
ld = leaderboard[:]
m = meal.copy()
if m['kuk'] == '':
m['kuk'] = m['candidate']
m['eaters'].remove(m['candidate'])
for i in range(len(ld)):
if ld[i]['name'] == m['kuk']:
if m['is_lazy']:
ld[i]['score'] += LAZY_KUK_POINTS[len(m['eaters'])]
else:
ld[i]['score'] += KUK_POINTS[len(m['eaters'])]
ld[i]['last_meal'] = m['meal_date']
elif ld[i]['name'] in m['eaters']:
if m['is_lazy']:
ld[i]['score'] -= LAZY_KUK_POINTS[len(m['eaters'])] / len(m['eaters'])
else:
ld[i]['score'] -= KUK_POINTS[len(m['eaters'])] / len(m['eaters'])
return sorted(ld, key=lambda i:(i['score'], i['last_meal'] or datetime.datetime(1970, 1, 1, 0, 0)))
def getMeal(meal_id): def getMeal(meal_id):
query = 'select foo.id, username as kuk, meal_date, array_agg as eaters, selection_deadline from (select meals.id, kuk, meal_date, selection_deadline, array_agg(username) from meals join users on users.id = any(meals.eaters) where meals.id=:meal_id group by meals.id order by meal_date) as foo join users on kuk=users.id;' query = 'select foo.id, username as kuk, meal_date, array_agg as eaters, selection_deadline from (select meals.id, kuk, meal_date, selection_deadline, array_agg(username) from meals join users on users.id = any(meals.eaters) where meals.id=:meal_id group by meals.id order by meal_date) as foo join users on kuk=users.id;'
r = db_engine.execute(text(query), meal_id=meal_id) r = db_engine.execute(text(query), meal_id=meal_id)

Loading…
Cancel
Save