def judge_drawing(request, room_name): if request.user.is_active and request.user.is_authenticated() and request.method == 'POST': chosen_drawing = None for key, value in request.POST.items(): if value == 'Choose Drawing': chosen_drawing = UserAccount.find(key) game_room = GameInstance.get(room_name) user_scores = ScoreInstance.get_all_for_game(game_room) draw_instance = DrawInstance.get(chosen_drawing, game_room, game_room.current_round) all_drawings = DrawInstance.get_all_for_round(game_room, game_room.current_round) for drawing in all_drawings: draw_instance.was_round_winner = -1 for user in user_scores: user.seen_previous_result = False user.save() game_room.current_round+=1 game_room.current_phrase="" game_room.save() draw_instance.was_round_winner = 1 draw_instance.save() winner_score = ScoreInstance.get(chosen_drawing, game_room) winner_score.score+=1 winner_score.save() choose_next_judge(game_room) return HttpResponseRedirect('/game/room/' + room_name) return HttpResponseRedirect('/')
def waiting_for_submissions(request, room_name, submissions): if request.user.is_active and request.user.is_authenticated(): current_room = GameInstance.get(room_name) userlist = current_room.users.all() num_submitted = 0 for user in userlist: if user != current_room.current_judge: drawing = DrawInstance.get(user, current_room, current_room.current_round) if drawing != None: num_submitted+=1 if int(submissions) != int(num_submitted): return HttpResponse('Success') return HttpResponse('No Change')
def game_room(request, room_name): if request.user.is_active and request.user.is_authenticated(): try: current_room = GameInstance.get(room_name) userlist = current_room.users.all() except: return HttpResponseRedirect('/') already_in_game = None current_judge = None user_drawing = None num_submitted = 0 is_owner = UserAccount.get(request.user) == current_room.owner user_submission = DrawInstance.get(UserAccount.get(request.user), current_room, current_room.current_round) previous_result_data = None if request.user: already_in_game = UserAccount.get(request.user) in userlist user_scores = [] user_drawings = [] for user in userlist: score = ScoreInstance.get(user, current_room) user_scores.append(({'username' : user.user.username, 'score': score})) if user.user == request.user and user == current_room.current_judge: current_judge = user if user.user == request.user and score.seen_previous_result == False: last_round_number = current_room.current_round-1 drawings = DrawInstance.get_all_for_round(current_room, last_round_number) previous_result_data = drawings user_scores = sorted(user_scores, key=lambda x: x['score'].score, reverse=True) for user in userlist: if user != current_room.current_judge: drawing = DrawInstance.get(user, current_room, current_room.current_round) user_drawings.append({'username': user.user.username, 'drawing': drawing}) if request.user == user.user and drawing != None: user_drawing = drawing if drawing != None: num_submitted+=1 if previous_result_data: user_drawings = previous_result_data return render(request, 'game/game_room.html', { 'game_instance' : current_room, 'room_name' : room_name, 'userlist' : user_scores, 'num_players' : len(userlist), 'num_drawing' : len(userlist)-1, 'user_drawings' : randomize_list(user_drawings), 'already_in_game' : already_in_game, 'is_current_judge' : current_judge, 'user_drawing' : user_drawing, 'num_submitted' : num_submitted, 'all_submitted' : num_submitted == len(userlist)-1, 'previous_round_data' : previous_result_data, 'open_slots' : current_room.max_players > len(userlist), 'game_startable' : len(userlist) > 1, 'is_owner' : is_owner, 'game_over' : current_room.current_round >= current_room.num_rounds, 'user_submission' : user_submission, 'round_winner' : find_winner(previous_result_data) }) return HttpResponseRedirect('/')