Skip to content

Commit de2be31

Browse files
authored
Merge pull request #1793 from OWASP/fix-trump
#1350: Make sure the trump suits trumps
2 parents 2d19391 + 7f294d5 commit de2be31

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

copi.owasp.org/lib/copi/cornucopia.ex

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ defmodule Copi.Cornucopia do
335335
Enum.reduce(game.players, [], fn player, cards -> (player.dealt_cards |> played_cards) ++ cards end) # Combine all played cards from all players
336336
|> lead_suit_cards # Filter to just the lead suit cards
337337
|> scoring_cards(Enum.count(game.players)) # Filter to just the cards that scored
338+
|> special_lead_cards # Filter to just the special lead cards (jokers, trump cards, or lead suit cards)
338339
|> Enum.group_by(fn card -> card.played_in_round end) # Convert to map where {round, [scoring_cards]}
339340
|> Enum.flat_map(fn {_, cards} -> highest_card(cards) end) # Back to a list of just the highest scoring card in each round
340341
end
@@ -357,6 +358,34 @@ defmodule Copi.Cornucopia do
357358
|> elem(1)
358359
end
359360

361+
defp special_lead_cards(ordered_played_cards) do
362+
jokers = Enum.filter(ordered_played_cards, fn card ->
363+
card.card.value in ["JokerA", "JokerB"]
364+
end)
365+
366+
if jokers != [] do
367+
jokers
368+
else
369+
trumps = Enum.filter(ordered_played_cards, fn card ->
370+
cat = String.upcase(card.card.category)
371+
cat in ["CORNUCOPIA", "DATASET RISK", "ELEVATION OF PRIVILEGE"]
372+
end)
373+
374+
if trumps != [] do
375+
trumps
376+
else
377+
case List.first(ordered_played_cards) do
378+
nil -> []
379+
first_card ->
380+
lead_suit = first_card.card.category
381+
Enum.filter(ordered_played_cards, fn card ->
382+
card.card.category == lead_suit
383+
end)
384+
end
385+
end
386+
end
387+
end
388+
360389
def unplayed_cards(cards) do
361390
Enum.filter(cards, fn card -> card.played_in_round == nil end)
362391
end

0 commit comments

Comments
 (0)