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 submit_drawing(request, room_name): if request.user.is_active and request.user.is_authenticated() and request.method == 'POST': current_user = UserAccount.get(request.user) current_room = GameInstance.get(room_name) round_number = current_room.current_round phrase = GameInstance.get(room_name).current_phrase timestamp = datetime.now() dataUrlPattern = re.compile('data:image/(png|jpeg);base64,(.*)$') image_data = request.POST['image_data'] image_data = dataUrlPattern.match(image_data).group(2) # If none or len 0, means illegal image data if (image_data == None or len(image_data) == 0): # PRINT ERROR MESSAGE HERE pass # Decode the 64 bit string into 32 bit image_data = base64.b64decode(image_data) hash = hashlib.sha1() hash.update(str(time.time())) conn = S3Connection(AWS_ACCESS_KEY, AWS_SECRET_KEY) bucket = conn.get_bucket(S3_BUCKET) k = Key(bucket) k.key = request.user.username+'_'+hash.hexdigest()+'.png' k.set_contents_from_file(StringIO(image_data)) drawing = DrawInstance(user = current_user, picture=base_image_url+k.key, game=current_room, round_number=round_number, round_judge=current_room.current_judge, phrase=phrase, timestamp=timestamp) drawing.save() return HttpResponseRedirect('/game/room/' + room_name) return HttpResponseRedirect('/')
def profile(request): current_user = UserAccount.get(request.user) drawings = DrawInstance.get_recent_images(current_user) return render(request, 'account/profile.html', { 'username' : current_user.user.username, 'email' : current_user.user.email, 'drawings' : list(drawings) })
def leave_room(request, room_name): if request.user.is_active and request.user.is_authenticated() and request.method == 'POST': current_room = GameInstance.get(room_name) current_user = UserAccount.get(request.user) remove_player(current_user, current_room) drawings = DrawInstance.get_all_for_game(current_room) userlist = current_room.users.all() if (current_room.owner == current_user and not current_room.game_started) or (len(userlist)==1 and len(drawings)==0): current_room.delete() return HttpResponseRedirect('/')
def public_picture(request, round_number, room_name, player_name): target_user = UserAccount.find(player_name) target_game = GameInstance.get(room_name) draw_instance = DrawInstance.get_for_user_game_round(target_user, target_game, round_number) if target_user == None or target_game == None or draw_instance == None: return HttpResponseRedirect('/') return render(request, "game/public_picture.html", { 'round_number' : round_number, 'room_name' : room_name, 'player_name' : player_name, 'drawing' : draw_instance })
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('/')