def get_purchaseable_cards(): global kingdom, person, agent person.play_treasures() purchaseable_cards = dominion_utils.get_purchaseable_cards( person, person.coins, kingdom) purchaseable_cards_data = dominion_utils.serialize_cards( purchaseable_cards) data = { "purchaseable": purchaseable_cards_data, "in_play": dominion_utils.serialize_cards(person.in_play), "hand": dominion_utils.serialize_cards(person.hand), "num_coins": person.coins } resp = Response(json.dumps(data), status=200, mimetype='application/json') return resp
def start_game(): global kingdom, person, agent kingdom = dominion_utils.generate_kingdom() kingdom.reset() person = Person() person.initialize(kingdom) person.reset_game() agent = Agent() agent.initialize(kingdom) agent.reset_game() person.opponent = agent agent.opponent = person # 1 if agent turn # -1 if opponent turn whose_turn = kingdom.starting_player data = { "turn": kingdom.turn_num, "hand": dominion_utils.serialize_cards(person.hand), "kingdom": dominion_utils.kingdom_to_string(kingdom), "play_phase": whose_turn } data = json.dumps(data) resp = Response(data, status=200, mimetype='application/json') return resp
def serialize(self): return { "type": self.type, "gain_card_limit": self.gain_card_limit, "gainable_cards": dominion_utils.serialize_cards(self.gainable_cards), "new_handsize": self.new_handsize, "remaining": self.remaining }
def get_action_cards(): global kingdom, person, agent action_cards = [card for card in person.hand if card.f_action] action_cards_data = dominion_utils.serialize_cards(action_cards) app.logger.info("Action cards: %s", str(action_cards_data)) resp = Response(json.dumps(action_cards_data), status=200, mimetype='application/json') return resp
def get_agent_action(): global kingdom, person, agent played_card, follow_up = agent.action_phase() data = { "kingdom": dominion_utils.kingdom_to_string(kingdom), "in_play": dominion_utils.serialize_cards(agent.in_play), "end_action_phase": played_card is None, "played_card": played_card.name if played_card else "", "num_buys": agent.num_buys, "num_actions": agent.num_actions, "hand": dominion_utils.serialize_cards(person.hand), "num_coins": agent.coins, "person_vp": person.num_victory_points(), "agent_vp": agent.num_victory_points() } data["follow_up"] = follow_up.serialize() if follow_up else None data = json.dumps(data) resp = Response(data, status=200, mimetype='application/json') return resp
def end_turn(): global kingdom, person, agent person.clean_up() person.print_state() winner = "" game_over = False if kingdom.is_game_over() != -1: game_over = True who_won = dominion_utils.did_player_win(person.num_victory_points(), agent.num_victory_points(), -1, kingdom) if who_won == 1: winner = "You!" elif who_won == 0.5: winner = "Tie" else: winner = "agent..." elif kingdom.starting_player == -1: kingdom.next_turn() data = { "turn": kingdom.turn_num, "hand": dominion_utils.serialize_cards(person.hand), "in_play": dominion_utils.serialize_cards(agent.in_play), "kingdom": dominion_utils.kingdom_to_string(kingdom), "game_over": game_over, "num_buys": agent.num_buys, "num_actions": agent.num_actions, "person_vp": person.num_victory_points(), "agent_vp": agent.num_victory_points(), "winner": winner, "num_cards_in_deck": len(person.deck) } data = json.dumps(data) resp = Response(data, status=200, mimetype='application/json') return resp
def play_card(): global kingdom, person, agent data = json.loads(request.data.decode()) card_to_play = next((x for x in person.hand if x.id == data['to_play']), None) assert card_to_play is not None app.logger.info(person.name + " playing " + card_to_play.name) person.hand.remove(card_to_play) person.in_play.append(card_to_play) follow_up_action = card_to_play.play(person) person.num_actions -= 1 action_cards = [card for card in person.hand if card.f_action] action_cards_data = dominion_utils.serialize_cards(action_cards) data = { "hand": dominion_utils.serialize_cards(person.hand), "action_cards": action_cards_data, "in_play": dominion_utils.serialize_cards(person.in_play), "kingdom": dominion_utils.kingdom_to_string(kingdom), "num_actions": person.num_actions, "num_buys": person.num_buys, "num_cards_in_deck": len(person.deck), "num_coins": person.coins, "person_vp": person.num_victory_points(), "agent_vp": agent.num_victory_points() } data["follow_up"] = follow_up_action.serialize( ) if follow_up_action else None data = json.dumps(data) resp = Response(data, status=200, mimetype='application/json') return resp
def discard(): global kingdom, person, agent data = json.loads(request.data.decode()) card_to_discard = Card(data['to_discard']) dominion_utils.discard_card(card_to_discard, person) data = { "turn": kingdom.turn_num, "hand": dominion_utils.serialize_cards(person.hand), "kingdom": dominion_utils.kingdom_to_string(kingdom) } data = json.dumps(data) resp = Response(data, status=200, mimetype='application/json') return resp
def buy(): global kingdom, person, agent data = json.loads(request.data.decode()) card_to_buy = Card(data['to_buy']) dominion_utils.buy_card(person, card_to_buy, kingdom) data = { "turn": kingdom.turn_num, "hand": dominion_utils.serialize_cards(person.hand), "kingdom": dominion_utils.kingdom_to_string(kingdom), "num_buys": person.num_buys, "person_vp": person.num_victory_points() } data = json.dumps(data) resp = Response(data, status=200, mimetype='application/json') return resp
def get_agent_buy(): global kingdom, person, agent bought_card = agent.buy_phase() data = { "kingdom": dominion_utils.kingdom_to_string(kingdom), "in_play": dominion_utils.serialize_cards(agent.in_play), "end_buy_phase": bought_card is None, "bought_card": bought_card.name if bought_card else "", "num_buys": agent.num_buys, "num_coins": agent.coins, "agent_vp": agent.num_victory_points() } data = json.dumps(data) resp = Response(data, status=200, mimetype='application/json') return resp
def gain(): global kingdom, person, agent data = json.loads(request.data.decode()) card_to_gain = Card(data['to_gain']) app.logger.info(person.name + " gaining " + card_to_gain.name) dominion_utils.gain_card(person, card_to_gain, kingdom) data = { "turn": kingdom.turn_num, "hand": dominion_utils.serialize_cards(person.hand), "kingdom": dominion_utils.kingdom_to_string(kingdom), "person_vp": person.num_victory_points(), "agent_vp": agent.num_victory_points() } data = json.dumps(data) resp = Response(data, status=200, mimetype='application/json') return resp