示例#1
0
def _add_energy_points_wallet(user_id, points):
    """Adds energy points to user"""
    df = database_read_write.get_energy_points_wallet()
    df.set_index('user_id', inplace=True)
    df.at[user_id, 'points'] += points
    df.reset_index(inplace=True)
    df = df[['id', 'user_id', 'points']]
    database_read_write.update_db(df, 'points_wallet')
示例#2
0
def _update_weekly_table(achievements_to_update):
    assert (all(achievement in FUNCTIONS.keys()
                for achievement in achievements_to_update))
    df_weekly = database_read_write.get_weekly_table()
    user_ids = sorted(df_weekly['user_id'].unique())
    for user_id in user_ids:
        ser_weekly = df_weekly.loc[df_weekly.user_id == user_id].iloc[0]
        for col, value in ser_weekly.iteritems():
            if col in achievements_to_update and value == 0 and FUNCTIONS[col](
                    user_id) > 0:
                index = df_weekly.index[(
                    df_weekly['user_id'] == user_id)].tolist()[0]
                df_weekly.at[index, col] = FUNCTIONS[col](user_id)
                _add_energy_points_wallet(user_id, FUNCTIONS[col](user_id))
    database_read_write.update_db(df_weekly, 'achievements_weekly')
示例#3
0
def _update_daily_table(achievements_to_update):
    assert (all(achievement in FUNCTIONS.keys()
                for achievement in achievements_to_update))
    df_daily = database_read_write.get_daily_table()
    user_ids = sorted(df_daily['user_id'].unique())
    today = database_read_write.get_today().strftime('%a')
    for user_id in user_ids:
        ser_daily = df_daily.loc[df_daily.user_id == user_id].loc[today]
        for col, value in ser_daily.iteritems():
            if col in achievements_to_update and value == 0 and FUNCTIONS[col](
                    user_id) > 0:
                index = df_daily.index[(df_daily['user_id'] == user_id)
                                       & (df_daily.index == today)].tolist()[0]
                df_daily.at[index, col] = FUNCTIONS[col](user_id)
                _add_energy_points_wallet(user_id, FUNCTIONS[col](user_id))

    df_daily.insert(1, 'week_day', df_daily.index)
    database_read_write.update_db(df_daily, 'achievements_daily')
示例#4
0
def initialise_achievements():
    """Run this function every week to reset all achievements achieved, except cum_savings"""
    print(
        f'[{datetime.now().strftime("%Y-%m-%d %H:%M:%S")}] Resetting all achievements'
    )
    df_daily = database_read_write.get_daily_table()
    df_weekly = database_read_write.get_weekly_table()
    df_bonus = database_read_write.get_bonus_table()
    for df in [df_daily, df_weekly, df_bonus]:
        for col in list(df):
            if col in FUNCTIONS.keys() and col != "cum_savings":
                df[col] = 0
    df_daily.insert(1, 'week_day', df_daily.index)
    database_read_write.update_db(df_daily, 'achievements_daily')
    database_read_write.update_db(df_weekly, 'achievements_weekly')
    database_read_write.update_db(df_bonus, 'achievements_bonus')