2025-07-08 17:40:13 +02:00
|
|
|
class_name EnvironmentManager
|
|
|
|
|
extends Node3D
|
|
|
|
|
|
2025-07-15 11:31:01 +02:00
|
|
|
var hand_associations: Dictionary[String, PlayerHand]
|
|
|
|
|
|
2025-07-20 15:02:05 +02:00
|
|
|
@export var web_client: WebClient
|
2025-07-15 14:42:06 +02:00
|
|
|
|
2025-09-27 15:42:34 +01:00
|
|
|
@export var trump_stack: CardStack
|
2025-07-16 11:50:33 +02:00
|
|
|
@export var card_stack: CardStack
|
2025-07-15 14:42:06 +02:00
|
|
|
|
2025-07-15 11:05:19 +02:00
|
|
|
# In gameplay order
|
2025-07-15 11:31:01 +02:00
|
|
|
#var hand_associations: Dictionary[int, PlayerHandAssociation]
|
2025-07-08 17:40:13 +02:00
|
|
|
var _own_client_id: String
|
|
|
|
|
@export var own_player_hand: PlayerHand
|
|
|
|
|
|
2025-07-15 11:05:19 +02:00
|
|
|
## Positions for hands in 3-player game (starting from player 2).
|
|
|
|
|
@export var hand_markers_3: Array[Marker3D]
|
|
|
|
|
|
|
|
|
|
## Positions for hands in 4-player game (starting from player 2).
|
|
|
|
|
@export var hand_markers_4: Array[Marker3D]
|
|
|
|
|
|
|
|
|
|
## Positions for hands in 5-player game (starting from player 2).
|
|
|
|
|
@export var hand_markers_5: Array[Marker3D]
|
|
|
|
|
|
|
|
|
|
## Positions for hands in 6-player game (starting from player 2).
|
|
|
|
|
@export var hand_markers_6: Array[Marker3D]
|
|
|
|
|
|
2025-07-08 17:40:13 +02:00
|
|
|
@export var player_hand_scene: PackedScene
|
2025-07-15 14:42:06 +02:00
|
|
|
@export var card_scene: PackedScene
|
|
|
|
|
|
2025-07-16 10:20:45 +02:00
|
|
|
var _bets: Dictionary[String, Array] # Array[BetResult]
|
2025-07-15 14:42:06 +02:00
|
|
|
## Cards played in the current play.
|
|
|
|
|
var _plays: Array[Play]
|
|
|
|
|
var _trump_id: int = -1
|
2025-07-08 17:40:13 +02:00
|
|
|
|
2025-07-20 20:06:58 +02:00
|
|
|
var _current_turn_client_id: String = ""
|
|
|
|
|
|
2025-07-20 19:26:50 +02:00
|
|
|
func _on_web_client_tcp_client_order_received(playing_order: Array[ClientData]) -> void:
|
2025-07-15 11:05:19 +02:00
|
|
|
var marker_positions: Array[Marker3D]
|
2025-07-20 19:26:50 +02:00
|
|
|
var client_ids: Array[String] = []
|
|
|
|
|
client_ids.assign(playing_order.map(func(a: ClientData): return a.client_id))
|
|
|
|
|
match playing_order.size():
|
2025-07-15 11:05:19 +02:00
|
|
|
3:
|
|
|
|
|
marker_positions = hand_markers_3
|
|
|
|
|
4:
|
|
|
|
|
marker_positions = hand_markers_4
|
|
|
|
|
5:
|
|
|
|
|
marker_positions = hand_markers_5
|
|
|
|
|
6:
|
|
|
|
|
marker_positions = hand_markers_6
|
|
|
|
|
|
|
|
|
|
var own_index: int = client_ids.find(_own_client_id)
|
|
|
|
|
|
|
|
|
|
var new_ids: Array[String]
|
|
|
|
|
if own_index == 0:
|
|
|
|
|
# no change necessary!
|
|
|
|
|
new_ids = client_ids
|
|
|
|
|
elif own_index == client_ids.size() - 1:
|
|
|
|
|
# you are at the end of the array
|
|
|
|
|
new_ids = client_ids
|
|
|
|
|
# take away own client id
|
|
|
|
|
new_ids.erase(_own_client_id)
|
|
|
|
|
# add it to the start of the array
|
|
|
|
|
new_ids.insert(0, _own_client_id)
|
|
|
|
|
else:
|
|
|
|
|
# you are somewhere in the middle of the array
|
|
|
|
|
# create array with you at the start
|
|
|
|
|
new_ids = client_ids.slice(own_index)
|
|
|
|
|
# add the rest of the original array
|
|
|
|
|
new_ids.append_array(client_ids.slice(0, own_index))
|
|
|
|
|
|
2025-07-15 11:31:01 +02:00
|
|
|
for index in new_ids.size():
|
2025-07-16 10:20:45 +02:00
|
|
|
_bets[new_ids[index]] = []
|
2025-07-15 11:31:01 +02:00
|
|
|
var hand: PlayerHand
|
2025-09-27 15:27:06 +01:00
|
|
|
if new_ids[index] == _own_client_id:
|
2025-07-15 11:31:01 +02:00
|
|
|
hand = own_player_hand
|
2025-07-08 17:40:13 +02:00
|
|
|
else:
|
2025-07-15 11:31:01 +02:00
|
|
|
hand = player_hand_scene.instantiate()
|
2025-07-20 19:26:50 +02:00
|
|
|
var client_data: ClientData
|
|
|
|
|
for client in playing_order:
|
2025-09-27 15:27:06 +01:00
|
|
|
if client.client_id == new_ids[index]:
|
2025-07-20 19:26:50 +02:00
|
|
|
client_data = client
|
|
|
|
|
break
|
|
|
|
|
hand.set_player_data(client_data) # also for own hand?
|
2025-07-16 09:35:32 +02:00
|
|
|
marker_positions[index - 1].add_child(hand)
|
2025-07-15 11:31:01 +02:00
|
|
|
|
2025-09-27 15:27:06 +01:00
|
|
|
hand_associations[new_ids[index]] = hand
|
2025-07-08 17:40:13 +02:00
|
|
|
|
2025-07-15 11:31:01 +02:00
|
|
|
func _on_web_client_tcp_cards_received(cards: Dictionary) -> void:
|
|
|
|
|
for client_id in cards.keys():
|
|
|
|
|
var card_ids: Array[int]
|
|
|
|
|
card_ids.assign(cards[client_id])
|
2025-07-16 09:35:32 +02:00
|
|
|
hand_associations[client_id].add_cards(card_ids, client_id == _own_client_id)
|
2025-07-15 14:42:06 +02:00
|
|
|
|
|
|
|
|
func _on_web_client_tcp_received_played_card(client_id: String, card_id: int) -> void:
|
|
|
|
|
print(client_id + " played card " + str(card_id))
|
|
|
|
|
_plays.append(Play.new(client_id, card_id))
|
2025-07-16 11:59:52 +02:00
|
|
|
hand_associations[client_id].remove_card_by_id(card_id)
|
|
|
|
|
card_stack.add_card_by_id(card_id)
|
2025-07-15 14:42:06 +02:00
|
|
|
|
|
|
|
|
func _on_web_client_tcp_received_play_winner_request() -> void:
|
|
|
|
|
if not $"../HBoxContainer/GameUI"._is_host:
|
|
|
|
|
return
|
|
|
|
|
|
2025-07-20 19:26:50 +02:00
|
|
|
await get_tree().create_timer(1.0).timeout
|
2025-07-18 15:11:25 +02:00
|
|
|
|
2025-07-15 14:42:06 +02:00
|
|
|
var card_ids: Array[int] = []
|
|
|
|
|
card_ids.assign(_plays.map(func(p: Play): return p.card_id))
|
|
|
|
|
var winning_card_id: int = CardManager.get_winning_card(card_ids, _trump_id)
|
|
|
|
|
|
|
|
|
|
for play in _plays:
|
|
|
|
|
if play.card_id == winning_card_id:
|
|
|
|
|
web_client.submit_play_winner(play.client_id)
|
2025-07-15 19:38:27 +02:00
|
|
|
_plays.clear()
|
2025-07-15 14:42:06 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
|
|
func _on_web_client_tcp_trump_received(trump_id: int) -> void:
|
|
|
|
|
_trump_id = trump_id
|
2025-07-16 11:50:33 +02:00
|
|
|
card_stack.purge_cards()
|
2025-09-27 15:42:34 +01:00
|
|
|
trump_stack.purge_cards()
|
|
|
|
|
trump_stack.add_card_by_id(_trump_id)
|
2025-07-16 10:20:45 +02:00
|
|
|
|
|
|
|
|
func _on_web_client_tcp_new_round_started() -> void:
|
|
|
|
|
# prepare a fresh BetResults bunch for the new round! the clients are hungry.
|
|
|
|
|
for client_id in _bets.keys():
|
|
|
|
|
_bets[client_id].append(BetResult.new())
|
|
|
|
|
|
|
|
|
|
func _on_web_client_tcp_received_expected_bet(client_id: String, expected_bet: int) -> void:
|
|
|
|
|
_bets[client_id][_bets[client_id].size() - 1].set_expected(expected_bet)
|
|
|
|
|
|
|
|
|
|
func _on_web_client_tcp_received_winning_client(client_id: String) -> void:
|
|
|
|
|
_bets[client_id][_bets[client_id].size() - 1].add_win()
|
|
|
|
|
print(_bets)
|
2025-07-16 11:15:05 +02:00
|
|
|
|
|
|
|
|
func _on_web_client_tcp_received_end_game_request() -> void:
|
|
|
|
|
var scores: Array[ScoreResult]
|
|
|
|
|
|
|
|
|
|
for client_id in _bets.keys():
|
|
|
|
|
var score = ScoreResult.new(client_id)
|
|
|
|
|
for bet_result in _bets[client_id]:
|
|
|
|
|
score.add_to_score(bet_result.get_score())
|
|
|
|
|
scores.append(score)
|
|
|
|
|
|
|
|
|
|
scores.sort_custom(func(a, b): return a.score < b.score)
|
|
|
|
|
|
|
|
|
|
var rank = scores.size()
|
|
|
|
|
for score_result: ScoreResult in scores:
|
|
|
|
|
hand_associations[score_result.client_id].display_rank(rank, score_result.score)
|
|
|
|
|
rank -= 1
|
2025-07-20 17:37:10 +02:00
|
|
|
|
|
|
|
|
func _on_web_client_connected_to_lobby(lobby_id: String, client_id: String) -> void:
|
|
|
|
|
_own_client_id = client_id
|
2025-07-20 20:06:58 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
func _on_web_client_notify_player_turn(client_id: String, is_self: bool) -> void:
|
|
|
|
|
if _current_turn_client_id:
|
|
|
|
|
hand_associations[_current_turn_client_id].set_turn(false, is_self)
|
|
|
|
|
|
|
|
|
|
_current_turn_client_id = client_id
|
|
|
|
|
if _current_turn_client_id:
|
|
|
|
|
hand_associations[_current_turn_client_id].set_turn(true, is_self)
|