Beispiel #1
0
def show_approve(username, game_id):
    user = User.get_or_none(User.username == username)
    game = Game.get_or_none(Game.id == game_id)

    habits = Habit.select().where(Habit.game_id == game_id)
    game_habits = []

    for habit in habits:
        game_habits.append(habit.id)

    to_approve = LogHabit.select().where((LogHabit.approved == False) & (LogHabit.receiver_id == user.id) & (LogHabit.habit_id << game_habits))

    to_approve_length = len(to_approve)
    sender_ids = []
    senders = []

    for log in to_approve:
        sender_ids.append(log.sender_id)
    for id in sender_ids:
        sender = User.get_or_none(User.id == id)
        senders.append(sender.username)
    
    

    return render_template('log_habits/approval.html', to_approve = to_approve,
                                                    to_approve_length = to_approve_length,
                                                    username = username,
                                                    game_id = game_id,
                                                    senders = senders)
Beispiel #2
0
def reject(username, game_id):
    user = User.get_or_none(User.username == username)
    game = Game.get_or_none(Game.id == game_id)

    habits = Habit.select().where(Habit.game_id == game_id)
    game_habits = []

    for habit in habits:
        game_habits.append(habit.id)

    to_approve = LogHabit.select().where((LogHabit.approved == False) & (LogHabit.receiver_id == user.id) & (LogHabit.habit_id in game_habits))


    loghabit_id = request.form.get('loghabit-ids')
    print(loghabit_id)
    loghabit = LogHabit.get_or_none(LogHabit.id == loghabit_id)
    delete_log = LogHabit.delete().where(LogHabit.id == loghabit_id)
    delete_log.execute()

    flash('Habit rejected.', 'success')

    if len(to_approve) < 1:
        return redirect(url_for('games.show', game_id = game_id, username = username))

    return redirect(url_for('log_habits.show_approve', game_id = game_id, username = username ))
Beispiel #3
0
def create():

    #Getting habit_id from the hidden form
    habit_id = request.form.get('habit-id')
    current_habit = Habit.get_or_none(Habit.id == habit_id)

    game_id = current_habit.game_id
    game = Game.get_or_none(Game.id == game_id)

    user = User.get_or_none(User.id == current_habit.user_id)
    username = user.username

    sender_id = current_habit.user_id
    if sender_id == game.player_1_id:
        receiver_id = game.player_2_id
    else:
        receiver_id = game.player_1_id


    # if current_user == user:



    image_file = request.files[f'image-for-{habit_id}']

    output = upload_file_to_s3(image_file, Config.S3_BUCKET)



                
    latest_round = Round.select(pw.fn.MAX(Round.id)).where(Round.game_id == game_id).scalar()


    new_habit_logged = LogHabit(habit_id = habit_id,
                                sender_id = current_habit.user_id,
                                receiver_id = receiver_id,
                                approved = False,
                                image_path = str(output),
                                game_round_id = latest_round)
    
    new_habit_logged.save()

    flash('Habit submitted. Waiting for your opponent\'s approval. Keep up the good work!', 'success')

    return redirect(url_for('games.show', game_id = game_id, username = username ))
Beispiel #4
0
def show(game_id, round_id):

    # get game model object
    game_id = game_id
    game = Game.get_by_id(game_id)

    # empty array for number of dice
    dice_array = []
    num_dice = 0

    # get round id and round instance
    round_id = round_id  # hardcode for dev purposes
    round = Round.get_by_id(round_id)

    # get round number from round index form or hidden inputs in round show
    round_num = request.args['round_num']

    # determine if current_user is p1 or p2 and get roll array
    game_player_1_id = game.player_1_id
    if current_user.id == game_player_1_id:
        # if current_player.id == game_player_1_id:
        player_variable = 1
        roll_array = round.player_1_rolls
        player_stats = round.player_1_stats
        opponent_stats = round.player_2_stats
        player_initiative = round.player_1_initiative
        opponent_initiative = round.player_2_initiative
    else:
        player_variable = 2
        roll_array = round.player_2_rolls
        player_stats = round.player_2_stats
        opponent_stats = round.player_1_stats
        player_initiative = round.player_2_initiative
        opponent_initiative = round.player_1_initiative

    round_result = round.result
    winner = User.get_or_none(User.id == round_result)

    # querydb get habits with user_id==current user AND game_id==game_id
    # current_user_habit_array = Habit.select().where((Habit.user_id == current_user.id) & (Habit.game_id == game_id))
    current_user_habit_array = Habit.select().where(
        (Habit.user_id == current_user.id) & (Habit.game_id == game_id))

    # for each habit, query log_habits table and get number of rows for that habit for current_round that r approved
    # compare to frequency number of the habit
    for habit in current_user_habit_array:

        # query loghabit table for rows belonging to habit/user which are approved
        # approved_logs = LogHabit.select().where((LogHabit.sender==current_user.id) & (LogHabit.habit_id == habit.id) & (LogHabit.approved==True) & (LogHabit.game_round_id == round_id))
        approved_logs = LogHabit.select().where(
            (LogHabit.sender == current_user.id)
            & (LogHabit.habit_id == habit.id) & (LogHabit.approved == True)
            & (LogHabit.game_round_id == round_id))

        # get length of list
        # compare length of list to frequency number
        if len(approved_logs) >= habit.frequency:
            # append to dice list
            dice_array += [1]
            num_dice += 1

    return render_template('rounds/show.html',
                           round=round,
                           round_id=round_id,
                           round_num=round_num,
                           num_dice=num_dice,
                           dice_array=dice_array,
                           player_variable=player_variable,
                           game_id=game_id,
                           roll_array=roll_array,
                           player_stats=player_stats,
                           opponent_stats=opponent_stats,
                           player_initiative=player_initiative,
                           opponent_initiative=opponent_initiative,
                           round_result=round_result,
                           winner=winner)
Beispiel #5
0
def habit():
    if "token" in session:
        user = session["token"]
        habit_list = Habit.objects(username=user)
        hstr_list = All_history.objects(user=user)
        act_list = Activities.objects()
        acts = []
        habits = []
        for hstr in hstr_list:
            if hstr["tit"] not in acts:
                acts.append(hstr["tit"])

        for habit in habit_list:
            if habit["tit"] not in habit:
                habits.append(habit["tit"])

        for a in acts:
            act_in_hstr = All_history.objects(user=user, tit=a)
            combo = 1
            if a not in habits:
                if len(act_in_hstr) >= 3:
                    for i in range(len(act_in_hstr) - 1):
                        if int(act_in_hstr[i + 1].time[6:8]) != int(
                                act_in_hstr[i].time[6:8]) + 1:
                            combo = 0
                        combo += 1
                        if i == len(act_in_hstr) - 2:
                            x_data = Activities.objects(tit=a).first()
                            soc = x_data["soc"]
                            per = x_data["per"]
                            st = x_data["st"]
                            knl = x_data["knl"]
                            cre = x_data["cre"]
                            Habit(username=user,
                                  tit=a,
                                  streak=combo,
                                  soc=soc,
                                  per=per,
                                  knl=knl,
                                  st=st,
                                  cre=cre).save()
            else:
                c_habit = Habit.objects(tit=a).first()
                if c_habit["streak"] < len(act_in_hstr):
                    if len(act_in_hstr) >= 3:
                        for i in range(len(act_in_hstr) - 1):
                            if int(act_in_hstr[i + 1].time[6:8]) != int(
                                    act_in_hstr[i].time[6:8]) + 1:
                                combo = 0
                            combo += 1

                    if combo > c_habit["streak"]:
                        c_habit["streak"] = combo
                        c_habit.save()

        if request.method == "GET":
            habit_list = Habit.objects(username=user)
            return render_template("habit.html", habits=habit_list)
        else:
            att_list = Attribute.objects(username=user).first()
            form = request.form
            for act in act_list:
                act_tit = form.get(act["tit"])

                if act_tit != None:
                    hstr_list = All_history.objects(user=user)
                    des = form["description"]
                    img = form["image"]
                    share = form["share"]
                    for att in att_list:
                        if att in act and att != "id":
                            att_list[att] = att_list[att] + act[att]
                            if att_list[att] < 0:
                                att_list[att] = 0
                        st = act["st"]
                        cre = act["cre"]
                        soc = act["soc"]
                        knl = act["knl"]
                        per = act["per"]

                    att_list.save()
                    All_history(tit=act["tit"],
                                img=img,
                                user=session["token"],
                                des=des,
                                soc=soc,
                                cre=cre,
                                knl=knl,
                                st=st,
                                per=per,
                                time=str(
                                    datetime.now().strftime("%Y%m%d"))).save()
                    if share == "yes":
                        post = Post(tit=act["tit"],
                                    img=img,
                                    user=session["token"],
                                    descript=des)
                        post.save()
                    break
            return redirect("/ca_nhan")
    else:
        return redirect("/sign_in")
Beispiel #6
0
def create():

    player_1 = User.get_or_none(User.username == current_user.username)
    p2_name = request.form["p2_name"]

    habit_a_name = request.form.get('habit_1_name')
    habit_a_frequency = request.form.get('habit_1_value')
    habit_b_name = request.form.get('habit_2_name')
    habit_b_frequency = request.form.get('habit_2_value')
    habit_c_name = request.form.get('habit_3_name')
    habit_c_frequency = request.form.get('habit_3_value')

    #error handle
    if p2_name == "":
        flash("Please enter a player username!", "danger")
        return redirect(url_for('games.new'))
    elif p2_name == player_1.username:
        flash(
            "You can not invite yourself for a game. Please choose another user.",
            "danger")
        return redirect(url_for('games.new'))
    else:
        player_2 = User.get_or_none(User.username == p2_name)
        if player_2 == None:
            flash("User doesn't exist. Please enter a valid username!",
                  "danger")
            return redirect(url_for('games.new'))
        else:
            new_game = Game(player_1_id=player_1.id, player_2_id=player_2.id)

    if (not habit_a_name) and (not habit_b_name) and (not habit_c_name):
        flash('Enter at least one habit!', "danger")
        return redirect(url_for('games.new'))

    if habit_a_name:
        if not habit_a_frequency:
            flash(
                'Select how many times per week you wish to complete habit for habit 1',
                "danger")
            return redirect(url_for('games.new'))
        else:
            habit_a = Habit(game=new_game,
                            user=player_1,
                            name=habit_a_name,
                            frequency=habit_a_frequency)
            new_game.save()
            habit_a.save()

    if habit_b_name:
        if not habit_b_frequency:
            flash(
                'Select how many times per week you wish to complete habit for habit 2',
                "danger")
            return redirect(url_for('games.'))
        else:
            habit_b = Habit(game=new_game,
                            user=player_1,
                            name=habit_b_name,
                            frequency=habit_b_frequency)
            new_game.save()
            habit_b.save()

    if habit_c_name:
        if not habit_c_frequency:
            flash(
                'Select how many times per week you wish to complete habit for habit 3',
                "danger")
            return redirect(url_for('games.new'))
        else:
            habit_c = Habit(game=new_game,
                            user=player_1,
                            name=habit_c_name,
                            frequency=habit_c_frequency)
            new_game.save()
            habit_c.save()

    return redirect(
        url_for('games.show', game_id=new_game.id, username=player_1.username))
Beispiel #7
0
def show(username, game_id):

    game = Game.get_or_none(Game.id == game_id)

    #int value for latest round_id for the game
    latest_round = Round.select(pw.fn.MAX(
        Round.id)).where(Round.game_id == game_id).scalar()
    current_round_object = Round.get_or_none(Round.id == latest_round)

    # get length of all rounds for game to send round number into anchor link to show latest round
    len_round = len(Round.select().where(Round.game_id == game_id))

    # Active player - could be either player 1 or 2
    user = User.get_or_none(User.username == username)

    #Set up habit info
    user_habits = Habit.select().where((Habit.game_id == game_id)
                                       & (Habit.user_id == user.id))
    length_habit_list = len(user_habits)

    #progress bars

    progress = []
    user_more_to_go = []

    for habit in user_habits:
        approved_logs = LogHabit.select().where(
            (LogHabit.sender_id == user.id) & (LogHabit.habit_id == habit.id)
            & (LogHabit.approved == True)
            & (LogHabit.game_round_id == latest_round))
        logged_habits = len(approved_logs)
        percentage = logged_habits / habit.frequency * 100
        progress.append(percentage)
        leftover = habit.frequency - logged_habits
        user_more_to_go.append(leftover)

    rounded_progress = [round(freq, 0) for freq in progress]

    # Opponent - could be either player 1 or player 2

    # check is active user is player_1 or player_2 for the game
    if user.id == game.player_1_id:
        active_user = 1
        opponent = User.get_or_none(User.id == game.player_2_id)
    else:
        active_user = 2
        opponent = User.get_or_none(User.id == game.player_1_id)

    opponent_username = opponent.username
    opponent_habits = Habit.select().where((Habit.game_id == game_id)
                                           & (Habit.user_id == opponent.id))
    opponent_habit_length = len(opponent_habits)
    print(opponent_habit_length)

    # check if game has been accepted by player_2 or not
    game_accepted = game.accepted

    #Progress bars

    opponent_progress = []

    for habit in opponent_habits:
        approved_logs = LogHabit.select().where(
            (LogHabit.sender_id == opponent.id)
            & (LogHabit.habit_id == habit.id) & (LogHabit.approved == True)
            & (LogHabit.game_round_id == latest_round))
        logged_habits = len(approved_logs)
        percentage = logged_habits / habit.frequency * 100
        opponent_progress.append(percentage)

    print(opponent_progress)
    rounded_opponent_progress = [round(freq, 0) for freq in opponent_progress]

    habits = Habit.select().where(Habit.game_id == game_id)
    game_habits = []

    for habit in habits:
        game_habits.append(habit.id)
    to_approve = LogHabit.select().where((LogHabit.approved == False)
                                         & (LogHabit.receiver_id == user.id)
                                         & (LogHabit.habit_id in game_habits))
    to_approve_length = len(to_approve)

    return render_template('games/show.html',
                           username=user.username,
                           user_habits=user_habits,
                           length_habit_list=length_habit_list,
                           rounded_progress=rounded_progress,
                           opponent_username=opponent_username,
                           opponent_habits=opponent_habits,
                           opponent_habit_length=opponent_habit_length,
                           rounded_opponent_progress=rounded_opponent_progress,
                           user_more_to_go=user_more_to_go,
                           game_id=game.id,
                           game_accepted=game_accepted,
                           active_user=active_user,
                           to_approve_length=to_approve_length,
                           current_round_object=current_round_object,
                           len_round=len_round)
Beispiel #8
0
def create(username, game_id):
    
    # get current game for the page
    game = Game.get_or_none(Game.id == game_id)

    # get the user
    user = User.get_or_none(User.username == username)

    # get response for challenge - Accept or Decline
    challenge_response = request.form.get('challenge-response')

    # save habits if user clicker Accept
    if challenge_response == 'Accept':

        # get form inputs
        habit_1_value = request.form.get('habit-1-value')
        habit_2_value = request.form.get('habit-2-value')
        habit_3_value = request.form.get('habit-3-value')

        habit_1_frequency = request.form.get('habit-1-frequency')
        habit_2_frequency = request.form.get('habit-2-frequency')
        habit_3_frequency = request.form.get('habit-3-frequency')

        # save habit only if field populated
        if habit_1_value:
            if not habit_1_frequency:
                print('error')
            else:
                habit_1 = Habit(game=game,
                                user=user,
                                name=habit_1_value,
                                frequency=habit_1_frequency)
                habit_1.save()
        
        # save habit only if field populated
        if habit_2_value:
            if not habit_2_frequency:
                print('error')
            else:
                habit_2 = Habit(game=game,
                                user=user,
                                name=habit_2_value,
                                frequency=habit_2_frequency)
                habit_2.save()
        
        # save habit only if field populated
        if habit_3_value:
            if not habit_3_frequency:
                print('error')
            else:
                habit_3 = Habit(game=game,
                                user=user,
                                name=habit_3_value,
                                frequency=habit_3_frequency)
                habit_3.save()

        # set game.accepted to True
        game.accepted = True
        game.save()

        # start asynchronus loop for rounds - first call beginning immediately
        async_create_round.delay(game_id)

        return redirect( url_for('games.show', game_id=game_id, username=username))

    else:
        # destroy game
        game.delete_instance(recursive=True)
        return redirect( url_for('games.index', username=username))