client can now only play allowed cards
This commit is contained in:
@@ -21,13 +21,39 @@ var _is_owned: bool
|
||||
@export var name_label: Label3D
|
||||
@export var rank_label: Label3D
|
||||
@export var hide_client_name: bool = false
|
||||
@export var card_disallowed_label: Label
|
||||
|
||||
var _selected_card: PlayingCard = null
|
||||
var _cards_in_hand_ids: Array[int] = []
|
||||
|
||||
var _trump_id: int = -1
|
||||
var _first_played_card_id: int = -1
|
||||
|
||||
signal card_selected(card_id: int)
|
||||
signal card_played(card_id: int)
|
||||
|
||||
func _ready() -> void:
|
||||
# tests!
|
||||
# tries playing same color
|
||||
assert(_is_card_allowed(1, 3, [1, 15]) == true)
|
||||
# tries playing trump while having same color
|
||||
assert(_is_card_allowed(15, 3, [1, 15]) == false)
|
||||
# tries playing trump while not having the same color
|
||||
assert(_is_card_allowed(15, 3, [19, 15]) == true)
|
||||
# tries playing first card
|
||||
assert(_is_card_allowed(27, -1, [27, 1, 15]) == true)
|
||||
# tries playing wizard as first
|
||||
assert(_is_card_allowed(59, -1, [59, 1, 15]) == true)
|
||||
# tries playing wizard as non-first
|
||||
assert(_is_card_allowed(59, 3, [59, 1, 15]) == true)
|
||||
# tries playing wizard after first is wizard
|
||||
assert(_is_card_allowed(59, 58, [59, 1, 15]) == true)
|
||||
# tries playing numbered after first is jester
|
||||
assert(_is_card_allowed(3, 54, [3, 1, 15]) == true)
|
||||
|
||||
assert(_is_card_allowed(13, 48, [13]) == true)
|
||||
|
||||
|
||||
if hide_client_name:
|
||||
name_label.hide()
|
||||
|
||||
@@ -47,6 +73,7 @@ func add_cards(card_ids: Array[int], is_owned: bool) -> void:
|
||||
card.card_clicked.connect(_on_card_clicked)
|
||||
card.setup_card_details(card_id)
|
||||
add_card(card)
|
||||
_cards_in_hand_ids.append(card_id)
|
||||
if card_ids.size() == 1:
|
||||
card.rotate_y(PI)
|
||||
await get_tree().create_timer(0.16).timeout
|
||||
@@ -60,6 +87,7 @@ func remove_card_by_id(card_id: int) -> void:
|
||||
func remove_card(card: PlayingCard) -> void:
|
||||
card_container.remove_child(card)
|
||||
card.queue_free()
|
||||
_cards_in_hand_ids.erase(card.card_details.card_id)
|
||||
_arrange_cards()
|
||||
|
||||
func _arrange_cards() -> void:
|
||||
@@ -81,6 +109,15 @@ func _get_card_distance(card_count: int) -> float:
|
||||
return _card_distance_min_others + ((_card_distance_max_others - _card_distance_min_others) * (1.0 - (float(card_count) / 19.0)))
|
||||
|
||||
func _on_card_clicked(playing_card: PlayingCard) -> void:
|
||||
print("check card is " + str(_first_played_card_id))
|
||||
if not _is_card_allowed(
|
||||
playing_card.card_details.card_id,
|
||||
_first_played_card_id,
|
||||
_cards_in_hand_ids,
|
||||
):
|
||||
_notify_disallowed_card()
|
||||
return
|
||||
|
||||
if _selected_card:
|
||||
_selected_card.unselect()
|
||||
if _selected_card == playing_card:
|
||||
@@ -108,3 +145,61 @@ func display_rank(rank: int, score: int) -> void:
|
||||
"rank": rank,
|
||||
"pts": score,
|
||||
})
|
||||
|
||||
func _is_card_allowed(requested_card_id: int, first_played_card_id: int, hand_card_ids: Array[int]) -> bool:
|
||||
var card_color: Card.CardColor = CardManager.get_card_color(requested_card_id)
|
||||
var first_played_color = CardManager.get_card_color(first_played_card_id)
|
||||
|
||||
print("card details: req: {r}, play: {p}, hand: {han}".format({
|
||||
"r": requested_card_id,
|
||||
"p": first_played_card_id,
|
||||
"han": hand_card_ids,
|
||||
}))
|
||||
|
||||
# no card played yet, everything is allowed
|
||||
if first_played_card_id == -1:
|
||||
print("no first played")
|
||||
return true
|
||||
|
||||
if first_played_color == Card.CardColor.WHITE:
|
||||
print("first is white")
|
||||
return true
|
||||
|
||||
# card is white, may play
|
||||
if card_color == Card.CardColor.WHITE:
|
||||
print("yours is white, may play")
|
||||
return true
|
||||
|
||||
# requested card is same color as laid card; may play
|
||||
if first_played_color == card_color:
|
||||
print("colors match")
|
||||
return true
|
||||
# requested card is of different color
|
||||
else:
|
||||
var colors_in_hand: Array[Card.CardColor] = []
|
||||
for id in hand_card_ids:
|
||||
colors_in_hand.append(CardManager.get_card_color(id))
|
||||
|
||||
# player has played color, must play requested color
|
||||
if colors_in_hand.has(first_played_color):
|
||||
print("has played color, must not play")
|
||||
return false
|
||||
# player does not have played color, may play anything
|
||||
else:
|
||||
print("does not have played color, may play")
|
||||
return true
|
||||
|
||||
func _notify_disallowed_card() -> void:
|
||||
if card_disallowed_label:
|
||||
card_disallowed_label.show()
|
||||
await get_tree().create_timer(1.0).timeout
|
||||
card_disallowed_label.hide()
|
||||
|
||||
func _on_web_client_tcp_trump_received(trump_id: int) -> void:
|
||||
_trump_id = trump_id
|
||||
|
||||
func _on_web_client_tcp_received_first_card(card_id: int) -> void:
|
||||
_first_played_card_id = card_id
|
||||
|
||||
func _on_web_client_tcp_reset_first_card() -> void:
|
||||
_first_played_card_id = -1
|
||||
|
||||
Reference in New Issue
Block a user