def messages(request, game_id, round_id, thread_id): thread = Thread.get_by_uid(thread_id) if thread is None: raise Http404 if not thread.profile_can_view(request.profile): return HttpResponse('Unauthorized', status=401) if request.method == 'GET': since = request.GET.get('since', None) return json(Message.get_activities(request.user, thread, since=since)) if request.method == 'POST': if not thread.profile_can_create(request.profile): return HttpResponse('Unauthorized', status=401) content = request.POST.get('content', None) if not content: return HttpResponse('Method Not Allowed - A message is required', status=405) # create the new message game = Game.get_by_uid(game_id) actor = Role.get_by_profile(game, request.profile) message = Message(actor=actor, content=content, thread=thread) message.put() return json(message) return HttpResponse('Method Not Allowed', status=405)
def end_game(request, game_id): t = datetime.now() game = Game.get_by_uid(game_id) last_round = game.get_current_round() if not game.is_over(): logging.critical('game:%s game end task called but game is not over' % \ game.uid) raise FactionizeTaskException, 'game not yet in a completed state' if game.is_complete: logging.warning('game:%s game end task called on a game that is ' + \ 'has already marked as complete. possibly a ' + \ 'repeated message' % game.uid) raise FactionizeTaskException, 'game is already marked as complete' # do lots of processing here to assign various awards if game.is_innocent_victory(): win = InnocentWin(thread=last_round.get_thread(role_vanillager)) else: win = MafiaWin(thread=last_round.get_thread(role_vanillager)) win.put() game.is_complete = True game.put() return HttpResponse('ok', status=200)
def join(request, game_id): game = Game.get_by_uid(game_id) if game is None: raise Http404 now = datetime.now() if (game.started and game.started < now) or game.signup_deadline < now: return HttpResponse(status=401) if request.profile.key() not in game.signups: game.add_to_waitlist(request.profile) return redirect('/games/%s' % game.uid)
def start(request, game_id): game = Game.get_by_uid(game_id) if game.game_starter.uid != request.profile.uid: return HttpResponse(status=403) now = datetime.now() if (game.started and game.started < now) or game.signup_deadline < now: return HttpResponse(status=401) try: game.start_game() except FactionizeError, e: return HttpResponse('Cannot start game', status=401)
def view(request, game_id): game = Game.get_by_uid(game_id) if game is None: raise Http404 current_round = game.get_current_round() rounds = game.get_rounds() threads = current_round.get_threads(request.profile) player_list = [r.player for r in game.get_active_roles()] context = dict(profile=request.profile, player_list=player_list, game=game, current_round=current_round, rounds=rounds, threads=threads) context['serialized'] = json_encode(context) return render('game/view.html', context)
def vote_summary(request, game_id, round_id, thread_id): thread = Thread.get_by_uid(thread_id) game = Game.get_by_uid(game_id) if thread is None: raise Http404 if not thread.profile_can_view(request.profile): return HttpResponse('Unauthorized', status=401) # only deals with GET requests if not request.method == 'GET': raise Http404 summaries = VoteSummary.all().filter('thread', thread) summaries = summaries.order('-total') data = [] total_votes = 0 for s in summaries: data.append(dict(profile=s.role.player, total=s.total, updated=s.updated)) total_votes += s.total chart_data = dict(chxr="0,0,%s" % len(data), chxt='y', chbh='a', chs='200x200', cht='bhs', chco='4D89F9', chds="0,%s" % total_votes) player_labels = [s['profile'].name for s in data] player_labels.reverse() chart_data['chx1'] = "0:|%s" % "|".join(player_labels) chart_data['chd'] = "t:%s" % ",".join([str(s['total']) for s in data]) chart_url = "http://chart.apis.google.com/chart?%s" chart_url = chart_url % urlencode(chart_data) return json(dict(thread=thread, summaries=data, chart_url=chart_url))
def votes(request, game_id, round_id, thread_id): thread = Thread.get_by_uid(thread_id) game = Game.get_by_uid(game_id) if thread is None: raise Http404 if not thread.profile_can_view(request.profile): return HttpResponse('Unauthorized', status=401) if request.method == 'GET': since = request.GET.get('since', None) return json(Vote.get_activities(request.user, thread, since=since)) if request.method == 'POST': if not thread.profile_can_create(request.profile): return HttpResponse('Unauthorized', status=401) # find the target target_id = request.POST.get('target_id', None) if target_id is None: raise Exception('No target') target_profile = Profile.get_by_uid(target_id) target = Role.all().filter('player', target_profile) target = target.filter('game', game) target = target.fetch(1)[0] # find the last vote this user made (if any) game = Game.get_by_uid(game_id) actor = Role.get_by_profile(game, request.profile) last_vote = Vote.all().filter("thread", thread) last_vote = last_vote.filter("actor", actor) last_vote = last_vote.order("-created") try: last_vote = last_vote.fetch(1)[0] except IndexError, e: last_vote = None # if we found a vote, decrement that vote's counter if last_vote is not None: last_vote.decrement() # create the new vote vote = Vote(actor=actor, target=target, thread=thread) vote.put() # increment the counter vote.increment() if thread.name == role_vanillager: vote_count = Vote.all().filter('thread', thread).count() if not vote_count: # First vote in round c = Client(settings.BDM_SECRET, settings.BDM_KEY) eul = "profile:%s" % request.profile.uid c.post("named_transaction_group/613301/execute/%s" % eul) if thread.round.number == 1: # First vote in game c.post("named_transaction_group/613302/execute/%s" % eul) return json(vote)