def populate_games(): with app.open_resource('./testing/data/games.json') as gamedata: games = json.load(gamedata)['games'] for g in games: Game.add_game(teams=g['teams'], court=int(g['court']), time=g['time']) scrs = g['score'].split(' ') Game.update_game(int(g['court']), g['time'], g['winner'], scrs[0], scrs[1])
class GameController: def __init__(self, game_point, dice_amount, first_point_limit): self.game_point = game_point self.dice_amount = dice_amount self.first_point_limit = first_point_limit self.players = [] self.manager_ui = ManagerUI(self) self.player_ui = PlayerUI(self) self.game_ui = GameUI(self, game_point) def register_player(self, name): self.players.append(Player(name)) def start_game(self): for player in self.players: player.score = 0 # プレイヤーの得点を初期化する player.is_winner = False # プレイヤーの勝利者判定をoffにする self.game = Game(self.players, self.game_point, self.dice_amount, self.first_point_limit) self.game.set_current_player(self.players[0]) # 最初のプレイヤーを ゲームのcurrentPlayerに編集 self.new_turn() def get_players_count(self): return len(self.players) def new_turn(self): self.game.new_turn() def do_turn(self): return self.game.turn.do_turn() def end_turn(self): self.game.end_turn() def get_current_player(self): return self.game.current_player def next_player(self): self.game.next_player() def is_turn_bust(self): return self.game.is_turn_bust def is_turn_none_of_dice(self): return self.game.is_turn_none_of_dice def is_game_end(self): return self.game.is_game_end() def winner_check(self): _score = 0 for player in self.players: if(player.score > _score): _score = player.score if(_score >= self.game_point): for player in self.players: if(_score == player.score): player.is_winner = True
def start_new_game(): new_game = Game() new_game_id = str(datetime.now()) active_games[new_game_id] = new_game try: q = new_game.get_next_question() except IKnow as e: return jsonify(game_id=new_game_id, question_type=QuestionTypes.FINAL.value, movies=[m.serialize() for m in e.movie_list]), 200 return jsonify(game_id=new_game_id, question=q.text, question_type=QuestionTypes.COMMON.value, question_id=q.id), 200
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 start_game(self): for player in self.players: player.score = 0 # プレイヤーの得点を初期化する player.is_winner = False # プレイヤーの勝利者判定をoffにする self.game = Game(self.players, self.game_point, self.dice_amount, self.first_point_limit) self.game.set_current_player(self.players[0]) # 最初のプレイヤーを ゲームのcurrentPlayerに編集 self.new_turn()
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 index(request): if request.method == 'GET': games = Game.all() return render('game/list.html', dict(games=games)) elif request.method == 'POST': if not hasattr(request, 'user') or not request.user: login_url = users.create_login_url(request.get_full_path()) return redirect(login_url) values = request.POST game = Game(name=values.get('name', 'Unnamed game'), game_starter=request.profile, signup_deadline=datetime.now() + timedelta(7), signups=[]) game.put() game.start_pregame() game.add_to_waitlist(request.profile) # redirect to the game return redirect('/games/%s' % game.uid)
def create(game_id=None): if game_id: abort(403) else: new_game = Game() print(request.json["general_data"]) new_game.general_data = request.json["general_data"] new_game.character_data = request.json["character_data"] new_game.phases = request.json["phases"] new_game.save() return make_response(json.dumps(new_game), 201)
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)