Skip to content

Commit

Permalink
Merge pull request #62 from TukiLaCosa/intercambio-con-intencion
Browse files Browse the repository at this point in the history
Intercambio con intencion
  • Loading branch information
ezeluduena authored Nov 13, 2023
2 parents 3d869db + 44278ce commit 5f0279f
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 51 deletions.
4 changes: 2 additions & 2 deletions app/routers/games/action_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def process_flamethrower_card(game: Game, player: Player, objective_player: Play

# Reacomodo el turno
if game.turn != 0 and objective_player.position < player.position:
game.turn = game.turn - 1
game.turn = game.turn - 1

asyncio.ensure_future(send_players_eliminated_event(game=game,
killer_id=player.id,
Expand Down Expand Up @@ -183,7 +183,7 @@ def process_better_run_card(game: Game, player: Player, objective_player: Player


@db_session
def process_card_exchange(game : Game ,player: Player, objective_player: Player, player_card: Card, objective_player_card: Card):
def process_card_exchange(game: Game, player: Player, objective_player: Player, player_card: Card, objective_player_card: Card):
if (player.rol == PlayerRol.THE_THING and player_card.subtype == CardSubtype.CONTAGION):
if objective_player.rol != PlayerRol.INFECTED:
objective_player.rol = PlayerRol.INFECTED
Expand Down
96 changes: 70 additions & 26 deletions app/routers/games/games.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from ..cards.utils import get_card_name_by_id, get_card_type_by_id, is_flamethrower, is_whiskey
from .services import finish_game
from .intention import *
from .defense_functions import ActionType


router = APIRouter(
Expand Down Expand Up @@ -223,36 +224,58 @@ async def intention_to_interchange_card(game_name: str, interchange_info: Intent
utils.verify_if_interchange_can_be_done(game_name, interchange_info)
objective_player_id = utils.get_id_of_next_player_in_turn(game_name)

'''exchange_intention = services.register_card_exchange_intention(
game_name, interchange_info)'''

with db_session:
objective_player = find_player_by_id(objective_player_id)

json_msg = {
"event": "exchange_intention",
"player_id": interchange_info.player_id,
"player_name": get_player_name_by_id(interchange_info.player_id),
"card_to_exchange": interchange_info.card_id,
"defense_cards": player_cards_to_defend_himself(ActionType.EXCHANGE_OFFER, objective_player)
}
await player_connections.send_event_to(objective_player_id, json_msg)
services.register_card_exchange_intention(
game_name, interchange_info.player_id, interchange_info.card_id, objective_player_id)

return {"message": "Card interchange intention terminated."}


@router.patch("/{game_name}/card-interchange-response", status_code=status.HTTP_200_OK)
async def card_interchange_response(game_name: str, game_data: InterchangeInformationIn):
utils.verify_if_interchange_response_can_be_done(game_name, game_data)
services.card_interchange_response(game_name, game_data)
# services.card_interchange_response(game_name, game_data)
with db_session:
intention: Intention = get_intention_in_game(game_name)
game: Game = find_game_by_name(game_name)

player = find_player_by_id(intention.player.id)
objective_player = find_player_by_id(intention.objective_player.id)

player_card = find_card_by_id(intention.exchange_payload['card_id'])
objective_player_card = find_card_by_id(game_data.card_id)
interchange_verify_data = {
"player_id": game_data.player_id,
"card_id": game_data.card_id,
"objective_player_id": objective_player.id,
"objective_card_id": objective_player_card.id
}
interchange_verify = InterchangeInformationVerify(
**interchange_verify_data)
utils.verify_if_interchange_response_can_be_done(
game_name, interchange_verify)
services.update_game_turn(game_name)

# clean_intention_in_game(game_name)
with db_session:
intention: Intention = get_intention_in_game(game_name)
game: Game = find_game_by_name(game_name)

json_msg = {
"event": utils.Events.EXCHANGE_DONE,
"player_name": get_player_name_by_id(game_data.player_id),
"objective_player_name": get_player_name_by_id(game_data.objective_player_id)
}
await player_connections.send_event_to_all_players_in_game(game_name, json_msg)
player = find_player_by_id(intention.player.id)
objective_player = find_player_by_id(intention.objective_player.id)

player_card = find_card_by_id(intention.exchange_payload['card_id'])
objective_player_card = find_card_by_id(game_data.card_id)

process_card_exchange(game, player, objective_player,
player_card, objective_player_card)

clean_intention_in_game(game_name)

with db_session:
json_msg = {
"event": utils.Events.EXCHANGE_DONE,
"player_name": get_player_name_by_id(game_data.player_id),
"objective_player_name": get_player_name_by_id(objective_player.id)
}
await player_connections.send_event_to_all_players_in_game(game_name, json_msg)

with db_session:
game = find_game_by_name(game_name)
Expand Down Expand Up @@ -333,10 +356,10 @@ async def card_resolute_exchange(game_name: str, game_data: ResoluteExchangeIn):
@router.post("/{game_name}/play-defense-card")
async def play_defense_card(game_name: str, defense_info: PlayDefenseInformation):
utils.verify_defense_card_can_be_played(game_name, defense_info)

intention: Intention = get_intention_in_game(game_name)
defense = False
if defense_info.card_id:
services.play_defense_card(game_name, defense_info)
intention: Intention = get_intention_in_game(game_name)

json_msg = {
"event": utils.Events.DEFENSE_CARD_PLAYED,
Expand All @@ -345,11 +368,32 @@ async def play_defense_card(game_name: str, defense_info: PlayDefenseInformation
"objective_player_id": intention.objective_player.id,
"action_type": intention.action_type
}
if 73 <= defense_info.card_id and defense_info.card_id <= 76:
card_name = get_card_name_by_id(
intention.exchange_payload["card_id"])
json_msg["card_to_exchange"] = card_name
await player_connections.send_event_to_all_players_in_game(game_name, json_msg)
defense = True
else:
process_intention_in_game(game_name)

clean_intention_in_game(game_name)
if (defense or intention.action_type != ActionType.EXCHANGE_OFFER):
clean_intention_in_game(game_name)

if (defense and intention.action_type == ActionType.EXCHANGE_OFFER):
services.update_game_turn(game_name)
with db_session:
game = find_game_by_name(game_name)
player_id_turn = select(
p for p in game.players if p.position == game.turn).first().id

json_msg = {
"event": utils.Events.NEW_TURN,
"next_player_name": get_player_name_by_id(player_id_turn),
"next_player_id": player_id_turn,
"round_direction": game.round_direction
}
await player_connections.send_event_to_all_players_in_game(game_name, json_msg)


@router.patch("/{game_name}/the-thing-end-game")
Expand Down
10 changes: 0 additions & 10 deletions app/routers/games/intention.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,6 @@ def process_intention_in_game(game_name) -> Intention:
objective_player = intention.objective_player

match intention.action_type:
case ActionType.EXCHANGE_OFFER:
exchange_info = intention.exchange_payload

player_card = find_card_by_id(exchange_info['card_id'])
objective_player_card = find_card_by_id(
exchange_info['objective_card_id'])

process_card_exchange(game, player, objective_player,
player_card, objective_player_card)

case ActionType.CHANGE_PLACES:
process_change_places_card(game, player, objective_player)

Expand Down
10 changes: 7 additions & 3 deletions app/routers/games/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,11 @@ class InterchangeInformationIn(BaseModel):

player_id: int # ID jugador que recibe la intencion
card_id: int # Card ID del jugador que recibe la intencion
objective_player_id: int # ID jugador que inicia la intencion
objective_card_id: int # Card ID del jugador que inicia la intencion


class InterchangeInformationVerify(InterchangeInformationIn):
objective_player_id: int
objective_card_id: int


class ResoluteExchangeIn(BaseModel):
Expand Down Expand Up @@ -191,7 +194,8 @@ class OneTwoEffectIn(BaseModel):
player_id: int
objective_player_id: int


class TheThingEndGameIn(BaseModel):
model_config = ConfigDict(from_attributes=True)

player_id: int
player_id: int
21 changes: 12 additions & 9 deletions app/routers/games/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from .panic_functions import *
import random
from app.routers.games import utils
from .intention import create_intention_in_game, ActionType
from .intention import create_intention_in_game, ActionType, get_intention_in_game


def get_unstarted_games() -> List[GameResponse]:
Expand Down Expand Up @@ -357,8 +357,10 @@ def play_action_card(game_name: str, play_info: PlayInformation):
detail="The card to exchange cannot be The Thing"
)
verify_card_in_hand(player, card_to_exchange)
process_seduction_card(
game, player, objective_player, card_to_exchange)

exchange_payload = {"card_id": card_to_exchange.id}
create_intention_in_game(
game, ActionType.EXCHANGE_OFFER, player, objective_player, exchange_payload)

player.hand.remove(card)
game.discard_deck.add(card)
Expand Down Expand Up @@ -540,11 +542,11 @@ def pass_card(play_info: PlayInformation):


@db_session
def register_card_exchange_intention(game_name: str, exchange_info: IntentionExchangeInformationIn) -> Intention:
def register_card_exchange_intention(game_name: str, player_id: int, card_id: int, objective_player_id: int) -> Intention:
game: Game = find_game_by_name(game_name)
player: Player = find_player_by_id(exchange_info.player_id)
objective_player = utils.get_next_player_in_turn(game)
exchange_payload = {"card_id": exchange_info.card_id}
player: Player = find_player_by_id(player_id)
objective_player = find_player_by_id(objective_player_id)
exchange_payload = {"card_id": card_id}

exchange_intention = create_intention_in_game(
game, ActionType.EXCHANGE_OFFER, player, objective_player, exchange_payload)
Expand All @@ -555,9 +557,10 @@ def register_card_exchange_intention(game_name: str, exchange_info: IntentionExc
@db_session
def card_interchange_response(game_name: str, game_data: InterchangeInformationIn):
game: Game = find_game_by_name(game_name)
player: Player = find_player_by_id(game_data.objective_player_id)
intention: Intention = get_intention_in_game(game_name)
player: Player = intention.objective_player
player_card: Card = cards_services.find_card_by_id(
game_data.objective_card_id)
intention.exchange_payload['card_id'])

next_player: Player = find_player_by_id(game_data.player_id)
next_player_card: Card = cards_services.find_card_by_id(game_data.card_id)
Expand Down
2 changes: 1 addition & 1 deletion app/routers/games/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ def verify_if_interchange_can_be_done(game_name: str, interchange_info: Intentio


@db_session
def verify_if_interchange_response_can_be_done(game_name: str, game_data: InterchangeInformationIn):
def verify_if_interchange_response_can_be_done(game_name: str, game_data: InterchangeInformationVerify):
game: Game = find_game_by_name(game_name)
player: Player = find_player_by_id(game_data.player_id)
player_card: Card = find_card_by_id(game_data.card_id)
Expand Down

0 comments on commit 5f0279f

Please sign in to comment.