Skip to content

Instantly share code, notes, and snippets.

@mavnn
Created November 29, 2017 10:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mavnn/c271b17d9e53bb88916cbfc7c5afd2fb to your computer and use it in GitHub Desktop.
Save mavnn/c271b17d9e53bb88916cbfc7c5afd2fb to your computer and use it in GitHub Desktop.
BlackBelt Domain Logic
module BlackBelt.Domain.Logic
open BlackBelt.Domain.Types
/// This match requires sorted cards
let (|IsNormal|_|) cards =
match cards with
| [Normal { Suit = Defend; Value = v }] ->
Some { Speed = v
Damage = 0
Suit = Defend
Knockdown = false }
| [Normal normalCard] ->
Some { Speed = normalCard.Value
Damage = normalCard.Value
Suit = normalCard.Suit
Knockdown = false }
| _ -> None
/// This match requires sorted cards
let (|IsCombo|_|) (cards : Card list) =
match cards with
| [Normal c1
Normal c2
Combo {SpeedSuit = speedS
FollowUpSuit = followUpS}]
when speedS = followUpS
&& c1.Suit = speedS
&& c2.Suit = speedS ->
Some { Speed = min c1.Value c2.Value
Damage = c1.Value + c2.Value
Suit = speedS
Knockdown = false }
| [Normal c1
Normal c2
Combo {SpeedSuit = speedS
FollowUpSuit = followUpS}]
| [Normal c2
Normal c1
Combo {SpeedSuit = speedS
FollowUpSuit = followUpS}]
when c1.Suit = speedS && c2.Suit = followUpS ->
Some { Speed = c1.Value
Damage = c1.Value + c2.Value
Suit = c1.Suit
Knockdown = false }
| _ -> None
/// This match requires sorted cards
let (|IsSpecial|_|) (cards : Card list) =
match cards with
| [Normal c1
Normal c2
Special {SpeedSuit = speedS
DamageSuit = damageS}]
when speedS = damageS
&& c1.Suit = speedS
&& c2.Suit = speedS ->
Some { Speed = min c1.Value c2.Value
Damage = max c1.Value c2.Value
Suit = speedS
Knockdown = false }
| [Normal c1
Normal c2
Special {SpeedSuit = speedS
DamageSuit = damageS}]
| [Normal c2
Normal c1
Special {SpeedSuit = speedS
DamageSuit = damageS}]
when c1.Suit = speedS
&& c2.Suit = damageS
&& c1.Value < c2.Value ->
Some { Speed = c1.Value
Damage = max c1.Value c2.Value
Suit = c1.Suit
Knockdown = false }
| _ -> None
let isComboCard c =
match c with
| Combo _ -> true
| _ -> false
let isSpecialCard c =
match c with
| Special _ -> true
| _ -> false
let private otherCards (cards : Card list) : Choice<Action, string> =
match List.sort cards with
| IsNormal action ->
Choice1Of2 action
| [_] ->
Choice2Of2 "You played only one card, and it wasn't normal"
| IsCombo action ->
Choice1Of2 action
| xs when List.exists isComboCard xs ->
Choice2Of2 "You played a combo card, but the others don't match"
| IsSpecial action ->
Choice1Of2 action
| xs when List.exists isSpecialCard xs ->
Choice2Of2 "You played a special card, but the others don't match"
| _ -> Choice2Of2 "What was that?"
let isKnockdownCard c =
match c with
| Knockdown -> true
| _ -> false
let toAction cards =
if cards |> List.exists isKnockdownCard then
match cards |> List.filter (isKnockdownCard >> not) |> otherCards with
| Choice1Of2 action ->
Choice1Of2 { action with Knockdown = true }
| error -> error
else
otherCards cards
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment