server now rotates through playing order every round and lets the winning player go first
This commit is contained in:
+5
-16
@@ -35,25 +35,20 @@ func get_card_texture(card_id: int, style: CardStyle = CardStyle.STICKMAN) -> Te
|
|||||||
return load("res://cards/textures/" + style_name + "/" + _get_card_name(card_id) + ".webp")
|
return load("res://cards/textures/" + style_name + "/" + _get_card_name(card_id) + ".webp")
|
||||||
|
|
||||||
func get_winning_card(card_ids: Array[int], trump: int) -> int:
|
func get_winning_card(card_ids: Array[int], trump: int) -> int:
|
||||||
print("ids: " + str(card_ids) + ", trump: " + str(trump))
|
|
||||||
if card_ids.has(56) or card_ids.has(57) or card_ids.has(58) or card_ids.has(59):
|
if card_ids.has(56) or card_ids.has(57) or card_ids.has(58) or card_ids.has(59):
|
||||||
# cards contain magician
|
# cards contain magician
|
||||||
print("checking magician route")
|
|
||||||
for card_id in card_ids:
|
for card_id in card_ids:
|
||||||
if full_deck[card_id].card_type == Card.CardType.MAGICIAN:
|
if full_deck[card_id].card_type == Card.CardType.MAGICIAN:
|
||||||
print("picked magician")
|
|
||||||
return card_id
|
return card_id
|
||||||
return -1 # should not happen
|
return -1 # should not happen
|
||||||
elif not trump == -1:
|
elif not trump == -1:
|
||||||
# trump exists, determine winner from this
|
# trump exists, determine winner from this
|
||||||
print("trump route")
|
|
||||||
var largest_trump_card_id: int = -1
|
var largest_trump_card_id: int = -1
|
||||||
var trump_color: Card.CardColor = full_deck[trump].color
|
var trump_color: Card.CardColor = full_deck[trump].color
|
||||||
for card_id in card_ids:
|
for card_id in card_ids:
|
||||||
if full_deck[card_id].color == trump_color and card_id > largest_trump_card_id:
|
if full_deck[card_id].color == trump_color and card_id > largest_trump_card_id:
|
||||||
largest_trump_card_id = card_id
|
largest_trump_card_id = card_id
|
||||||
if not largest_trump_card_id == -1:
|
if not largest_trump_card_id == -1:
|
||||||
print("picked " + str(largest_trump_card_id))
|
|
||||||
return largest_trump_card_id
|
return largest_trump_card_id
|
||||||
|
|
||||||
# no trump played; determine from highest played card
|
# no trump played; determine from highest played card
|
||||||
@@ -64,16 +59,13 @@ func get_winning_card(card_ids: Array[int], trump: int) -> int:
|
|||||||
|
|
||||||
func _get_largest_non_trump_card(card_ids: Array[int]) -> int:
|
func _get_largest_non_trump_card(card_ids: Array[int]) -> int:
|
||||||
var largest_card_id: int = -1
|
var largest_card_id: int = -1
|
||||||
print("nontrump route")
|
|
||||||
var first_color: Card.CardColor = _get_first_color_card(card_ids)
|
var first_color: Card.CardColor = _get_first_color_card(card_ids)
|
||||||
if first_color == Card.CardColor.WHITE:
|
if first_color == Card.CardColor.WHITE:
|
||||||
print("all white")
|
|
||||||
return card_ids[0] # first one wins if all played cards are white
|
return card_ids[0] # first one wins if all played cards are white
|
||||||
else:
|
else:
|
||||||
for card_id in card_ids:
|
for card_id in card_ids:
|
||||||
if full_deck[card_id].color == first_color and card_id > largest_card_id:
|
if full_deck[card_id].color == first_color and card_id > largest_card_id:
|
||||||
largest_card_id = card_id
|
largest_card_id = card_id
|
||||||
print("picked " + str(largest_card_id))
|
|
||||||
return largest_card_id
|
return largest_card_id
|
||||||
|
|
||||||
## Returns the color of the first non-white card played.
|
## Returns the color of the first non-white card played.
|
||||||
@@ -93,13 +85,14 @@ func _get_card_name(card_id: int) -> String:
|
|||||||
elif card_id > 55:
|
elif card_id > 55:
|
||||||
return "magician"
|
return "magician"
|
||||||
|
|
||||||
elif card_id < 13:
|
match get_card_color(card_id):
|
||||||
|
Card.CardColor.BLUE:
|
||||||
color = "blue"
|
color = "blue"
|
||||||
elif card_id < 26:
|
Card.CardColor.GREEN:
|
||||||
color = "green"
|
color = "green"
|
||||||
elif card_id < 39:
|
Card.CardColor.RED:
|
||||||
color = "violet"
|
color = "violet"
|
||||||
else:
|
Card.CardColor.YELLOW:
|
||||||
color = "orange"
|
color = "orange"
|
||||||
|
|
||||||
match card_id:
|
match card_id:
|
||||||
@@ -129,10 +122,6 @@ func _get_card_name(card_id: int) -> String:
|
|||||||
suffix = "12"
|
suffix = "12"
|
||||||
12, 25, 38, 51:
|
12, 25, 38, 51:
|
||||||
suffix = "13"
|
suffix = "13"
|
||||||
52, 53, 54, 55:
|
|
||||||
suffix = "joker"
|
|
||||||
56, 57, 58, 59:
|
|
||||||
suffix = "magician"
|
|
||||||
|
|
||||||
return color + "_" + suffix
|
return color + "_" + suffix
|
||||||
|
|
||||||
|
|||||||
@@ -43,7 +43,6 @@ func _on_message_input_gui_input(event: InputEvent) -> void:
|
|||||||
func _on_send_message_button_pressed() -> void:
|
func _on_send_message_button_pressed() -> void:
|
||||||
_send_chat_message()
|
_send_chat_message()
|
||||||
|
|
||||||
|
|
||||||
func _on_web_client_connection_status_changed(new_status: WebClient.ConnectionStatus) -> void:
|
func _on_web_client_connection_status_changed(new_status: WebClient.ConnectionStatus) -> void:
|
||||||
if new_status == WebClient.ConnectionStatus.JOINED:
|
if new_status == WebClient.ConnectionStatus.JOINED:
|
||||||
message_input.editable = true
|
message_input.editable = true
|
||||||
|
|||||||
@@ -141,7 +141,7 @@ func _handle_response(response: String) -> void:
|
|||||||
# Relay message to be displayed in the chat panel
|
# Relay message to be displayed in the chat panel
|
||||||
chat_panel.put_chat_message(data["client_name"], data["message"])
|
chat_panel.put_chat_message(data["client_name"], data["message"])
|
||||||
|
|
||||||
"new_round_started":
|
"round_started":
|
||||||
if data["current_round"] == 1:
|
if data["current_round"] == 1:
|
||||||
var playing_order: Array[String] = []
|
var playing_order: Array[String] = []
|
||||||
playing_order.assign(data["playing_order"])
|
playing_order.assign(data["playing_order"])
|
||||||
|
|||||||
+47
-13
@@ -21,9 +21,11 @@ class Lobby:
|
|||||||
self.lobby_id = lobby_id
|
self.lobby_id = lobby_id
|
||||||
self.host_client_id = host_client_id
|
self.host_client_id = host_client_id
|
||||||
self.connected_clients = {}
|
self.connected_clients = {}
|
||||||
|
# original order, set at the start of the game and not modified thereafter
|
||||||
self.client_playing_order = []
|
self.client_playing_order = []
|
||||||
|
# order that is changed with every round and take, based on client_playing_order
|
||||||
|
self.current_playing_order = []
|
||||||
self.client_bets = {} # client_id: bet
|
self.client_bets = {} # client_id: bet
|
||||||
# self.currently_playing_client = 0 # index of client_playing_order
|
|
||||||
self.current_round = 0
|
self.current_round = 0
|
||||||
|
|
||||||
class Client:
|
class Client:
|
||||||
@@ -187,7 +189,7 @@ class LobbyManager:
|
|||||||
lobby_id,
|
lobby_id,
|
||||||
client_id,
|
client_id,
|
||||||
json.dumps({
|
json.dumps({
|
||||||
"response": "new_round_started",
|
"response": "round_started",
|
||||||
"current_round": round,
|
"current_round": round,
|
||||||
"cards": cards,
|
"cards": cards,
|
||||||
"trump": trump,
|
"trump": trump,
|
||||||
@@ -253,6 +255,22 @@ class LobbyManager:
|
|||||||
return self.get_unique_client_id(lobby_id)
|
return self.get_unique_client_id(lobby_id)
|
||||||
return id
|
return id
|
||||||
|
|
||||||
|
# Shifts the playing order by (current_round - 1) to start with the next player in line.
|
||||||
|
def shift_playing_order(self, lobby_id, current_round) -> list:
|
||||||
|
order = self.rotate(self.get_lobby(lobby_id).client_playing_order, current_round - 1)
|
||||||
|
|
||||||
|
self.get_lobby(lobby_id).current_playing_order = order
|
||||||
|
return order
|
||||||
|
|
||||||
|
def rotate(self, l, n):
|
||||||
|
return l[n:] + l[:n]
|
||||||
|
|
||||||
|
# Returns a playing order starting at client_id.
|
||||||
|
def set_playing_order_start(self, lobby_id, client_id) -> list:
|
||||||
|
order = self.shift_playing_order(lobby_id, self.get_lobby(lobby_id).client_playing_order.index(client_id) + 1)
|
||||||
|
self.get_lobby(lobby_id).current_playing_order = order
|
||||||
|
return order
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
lobby_manager = LobbyManager()
|
lobby_manager = LobbyManager()
|
||||||
@@ -413,16 +431,22 @@ async def process_input(message: str, websocket) -> None:
|
|||||||
|
|
||||||
lobby_manager.get_lobby(data_object["lobby_id"]).current_round = 1
|
lobby_manager.get_lobby(data_object["lobby_id"]).current_round = 1
|
||||||
|
|
||||||
print(f"Lobby {lobby_id} is starting the game, playing order is: {lobby.client_playing_order}")
|
print(f"Lobby {lobby_id} is starting the game, initial playing order is: {lobby.client_playing_order}")
|
||||||
|
|
||||||
|
# Initialise current_playing_order
|
||||||
|
lobby_manager.shift_playing_order(lobby_id, 1)
|
||||||
|
|
||||||
await lobby_manager.request_bet(
|
await lobby_manager.request_bet(
|
||||||
lobby_id,
|
lobby_id,
|
||||||
lobby_manager.get_lobby(lobby_id).client_playing_order[0],
|
lobby_manager.get_lobby(lobby_id).current_playing_order[0],
|
||||||
-1,
|
-1,
|
||||||
)
|
)
|
||||||
|
|
||||||
elif data_object["response"] == "start_next_play":
|
elif data_object["response"] == "start_next_play":
|
||||||
lobby_id = data_object["lobby_id"]
|
lobby_id = data_object["lobby_id"]
|
||||||
|
|
||||||
|
# Playing order is set in response to submit_take_winner
|
||||||
|
|
||||||
await lobby_manager.broadcast_message(
|
await lobby_manager.broadcast_message(
|
||||||
lobby_id,
|
lobby_id,
|
||||||
json.dumps({
|
json.dumps({
|
||||||
@@ -431,7 +455,7 @@ async def process_input(message: str, websocket) -> None:
|
|||||||
)
|
)
|
||||||
await lobby_manager.send_message(
|
await lobby_manager.send_message(
|
||||||
lobby_id,
|
lobby_id,
|
||||||
lobby_manager.get_lobby(lobby_id).client_playing_order[0],
|
lobby_manager.get_lobby(lobby_id).current_playing_order[0],
|
||||||
json.dumps({
|
json.dumps({
|
||||||
"response": "request_card",
|
"response": "request_card",
|
||||||
}),
|
}),
|
||||||
@@ -444,17 +468,21 @@ async def process_input(message: str, websocket) -> None:
|
|||||||
# Reset bets
|
# Reset bets
|
||||||
lobby_manager.get_lobby(lobby_id).client_bets = {}
|
lobby_manager.get_lobby(lobby_id).client_bets = {}
|
||||||
lobby_manager.get_lobby(data_object["lobby_id"]).current_round += 1
|
lobby_manager.get_lobby(data_object["lobby_id"]).current_round += 1
|
||||||
|
current_round = lobby_manager.get_lobby(data_object["lobby_id"]).current_round
|
||||||
|
|
||||||
|
# Set new playing order
|
||||||
|
playing_order = lobby_manager.shift_playing_order(lobby_id, current_round)
|
||||||
|
|
||||||
await lobby_manager.spread_cards(
|
await lobby_manager.spread_cards(
|
||||||
lobby_id,
|
lobby_id,
|
||||||
data_object["round"],
|
current_round,
|
||||||
data_object["cards"],
|
data_object["cards"],
|
||||||
None,
|
None,
|
||||||
)
|
)
|
||||||
|
|
||||||
await lobby_manager.request_bet(
|
await lobby_manager.request_bet(
|
||||||
lobby_id,
|
lobby_id,
|
||||||
lobby_manager.get_lobby(lobby_id).client_playing_order[0],
|
playing_order[0],
|
||||||
-1,
|
-1,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -471,7 +499,7 @@ async def process_input(message: str, websocket) -> None:
|
|||||||
bet,
|
bet,
|
||||||
)
|
)
|
||||||
|
|
||||||
last_client_index = lobby_manager.get_lobby(lobby_id).client_playing_order.index(client_id)
|
last_client_index = lobby_manager.get_lobby(lobby_id).current_playing_order.index(client_id)
|
||||||
player_count = lobby_manager.get_lobby_player_count(lobby_id)
|
player_count = lobby_manager.get_lobby_player_count(lobby_id)
|
||||||
|
|
||||||
if last_client_index == (player_count - 1):
|
if last_client_index == (player_count - 1):
|
||||||
@@ -485,7 +513,7 @@ async def process_input(message: str, websocket) -> None:
|
|||||||
|
|
||||||
print("Sent game start")
|
print("Sent game start")
|
||||||
|
|
||||||
playing_client_id = lobby_manager.get_lobby(lobby_id).client_playing_order[0]
|
playing_client_id = lobby_manager.get_lobby(lobby_id).current_playing_order[0]
|
||||||
|
|
||||||
await lobby_manager.send_message(
|
await lobby_manager.send_message(
|
||||||
lobby_id,
|
lobby_id,
|
||||||
@@ -510,7 +538,7 @@ async def process_input(message: str, websocket) -> None:
|
|||||||
|
|
||||||
await lobby_manager.request_bet(
|
await lobby_manager.request_bet(
|
||||||
lobby_id,
|
lobby_id,
|
||||||
lobby_manager.get_lobby(lobby_id).client_playing_order[last_client_index + 1],
|
lobby_manager.get_lobby(lobby_id).current_playing_order[last_client_index + 1],
|
||||||
disallowed_bet,
|
disallowed_bet,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -523,7 +551,8 @@ async def process_input(message: str, websocket) -> None:
|
|||||||
client_id,
|
client_id,
|
||||||
data_object["played_card"],
|
data_object["played_card"],
|
||||||
)
|
)
|
||||||
playing_order = lobby_manager.get_lobby(lobby_id).client_playing_order
|
|
||||||
|
playing_order = lobby_manager.get_lobby(lobby_id).current_playing_order
|
||||||
client_index = playing_order.index(client_id)
|
client_index = playing_order.index(client_id)
|
||||||
if client_index == (len(playing_order) - 1):
|
if client_index == (len(playing_order) - 1):
|
||||||
# end round
|
# end round
|
||||||
@@ -547,11 +576,16 @@ async def process_input(message: str, websocket) -> None:
|
|||||||
)
|
)
|
||||||
|
|
||||||
elif data_object["response"] == "submit_play_winner":
|
elif data_object["response"] == "submit_play_winner":
|
||||||
|
lobby_id = data_object["lobby_id"]
|
||||||
|
winning_client_id = data_object["client_id"]
|
||||||
|
|
||||||
await lobby_manager.spread_play_winner(
|
await lobby_manager.spread_play_winner(
|
||||||
data_object["lobby_id"],
|
lobby_id,
|
||||||
data_object["client_id"],
|
winning_client_id,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
lobby_manager.set_playing_order_start(lobby_id, winning_client_id)
|
||||||
|
|
||||||
print(f"DING DING DING we have a winner!!! {data_object['client_id']}")
|
print(f"DING DING DING we have a winner!!! {data_object['client_id']}")
|
||||||
|
|
||||||
elif data_object["response"] == "end_game":
|
elif data_object["response"] == "end_game":
|
||||||
|
|||||||
Reference in New Issue
Block a user