Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draw action family #35

Merged
merged 28 commits into from
Nov 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified Docs/redux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct HandlerActivateCards: GameActionHandler {
return nil
}

return .activateCards(player: player, cards: activeCards)
return .activate(activeCards, player: player)
}

private func activableCardsOfPlayer(_ playerObj: Player) -> [String] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ struct HandlerNextAction: GameActionHandler {
switch action {
case .setGameOver,
.chooseOne,
.activateCards:
.activate:
nil

default:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//
// ActionActivateCards.swift
// ActionActivate.swift
//
//
// Created by Hugues Telolahy on 01/07/2023.
//

struct ActionActivateCards: GameActionReducer {
struct ActionActivate: GameActionReducer {
let player: String
let cards: [String]

Expand Down
6 changes: 2 additions & 4 deletions GameKit/Sources/Game/Reducer/Action+Reducer/ActionDraw.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@
// ActionDraw.swift
//
//
// Created by Hugues Telolahy on 10/04/2023.
// Created by Hugues Telolahy on 18/11/2023.
//

struct ActionDraw: GameActionReducer {
let player: String

func reduce(state: GameState) throws -> GameState {
var state = state
let card = try state.popDeck()
state[keyPath: \GameState.players[player]]?.hand.add(card)
state.discard.push(card)
return state
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//
// ActionChooseArena.swift
// ActionDrawArena.swift
//
//
// Created by Hugues Telolahy on 11/04/2023.
//

struct ActionChooseArena: GameActionReducer {
struct ActionDrawArena: GameActionReducer {
let player: String
let card: String

Expand Down
17 changes: 17 additions & 0 deletions GameKit/Sources/Game/Reducer/Action+Reducer/ActionDrawDeck.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// ActionDrawDeck.swift
//
//
// Created by Hugues Telolahy on 10/04/2023.
//

struct ActionDrawDeck: GameActionReducer {
let player: String

func reduce(state: GameState) throws -> GameState {
var state = state
let card = try state.popDeck()
state[keyPath: \GameState.players[player]]?.hand.add(card)
return state
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//
// ActionStealHand.swift
// ActionDrawHand.swift
//
//
// Created by Hugues Telolahy on 02/09/2023.
//

struct ActionStealHand: GameActionReducer {
struct ActionDrawHand: GameActionReducer {
let player: String
let target: String
let card: String
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//
// ActionStealInPlay.swift
// ActionDrawInPlay.swift
//
//
// Created by Hugues Telolahy on 02/09/2023.
//

struct ActionStealInPlay: GameActionReducer {
struct ActionDrawInPlay: GameActionReducer {
let player: String
let target: String
let card: String
Expand Down
15 changes: 0 additions & 15 deletions GameKit/Sources/Game/Reducer/Action+Reducer/ActionLuck.swift

This file was deleted.

4 changes: 2 additions & 2 deletions GameKit/Sources/Game/Reducer/Action+Reducer/ActionPlay.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ struct ActionPlay: GameActionReducer {
try playReq.throwingMatch(state: state, ctx: playReqContext)
}

var state = state

// resolve target
if case let .target(requiredTarget, _) = playRule.effect {
let ctx = EffectContext(
Expand All @@ -35,7 +37,6 @@ struct ActionPlay: GameActionReducer {
)
let resolvedTarget = try requiredTarget.resolve(state: state, ctx: ctx)
if case let .selectable(pIds) = resolvedTarget {
var state = state
let options = pIds.reduce(into: [String: GameAction]()) {
let action: GameAction =
if playRule.isMatching(.playImmediate) {
Expand Down Expand Up @@ -66,7 +67,6 @@ struct ActionPlay: GameActionReducer {
}

// queue play action
var state = state
state.sequence.insert(action, at: 0)
return state
}
Expand Down
17 changes: 17 additions & 0 deletions GameKit/Sources/Game/Reducer/Action+Reducer/ActionPutBack.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// ActionPutBack.swift
//
//
// Created by Hugues Telolahy on 18/11/2023.
//

struct ActionPutBack: GameActionReducer {
func reduce(state: GameState) throws -> GameState {
var state = state
if let arena = state.arena {
state.deck.cards.insert(contentsOf: arena.cards, at: 0)
state.arena = nil
}
return state
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//
// ActionReveal.swift
// ActionRevealHand.swift
//
//
// Created by Hugues Stephano TELOLAHY on 10/11/2023.
// Created by Hugues Telolahy on 19/11/2023.
//

struct ActionReveal: GameActionReducer {
struct ActionRevealHand: GameActionReducer {
let card: String
let player: String

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ protocol GameActionReducer {

extension GameAction {
func reduce(state: GameState) throws -> GameState {
var state = state
state = try reducer().reduce(state: state)
return state
try reducer().reduce(state: state)
}
}

Expand Down Expand Up @@ -45,32 +43,35 @@ private extension GameAction {
case let .discardHand(card, player):
ActionDiscardHand(player: player, card: card)

case let .revealHand(card, player):
ActionRevealHand(card: card, player: player)

case let .discardInPlay(card, player):
ActionDiscardInPlay(player: player, card: card)

case let .draw(player):
ActionDraw(player: player)
case let .drawDeck(player):
ActionDrawDeck(player: player)

case let .drawArena(card, player):
ActionDrawArena(player: player, card: card)

case let .stealHand(card, target, player):
ActionStealHand(player: player, target: target, card: card)
case let .drawHand(card, target, player):
ActionDrawHand(player: player, target: target, card: card)

case let .stealInPlay(card, target, player):
ActionStealInPlay(player: player, target: target, card: card)
case let .drawInPlay(card, target, player):
ActionDrawInPlay(player: player, target: target, card: card)

case let .passInplay(card, target, player):
case let .passInPlay(card, target, player):
ActionPassInPlay(card: card, target: target, player: player)

case .discover:
ActionDiscover()

case let .reveal(card, player):
ActionReveal(card: card, player: player)

case .luck:
ActionLuck()
case .putBack:
ActionPutBack()

case let .chooseArena(card, player):
ActionChooseArena(player: player, card: card)
case .draw:
ActionDraw()

case let .group(actions):
ActionGroup(children: actions)
Expand All @@ -87,8 +88,8 @@ private extension GameAction {
case let .chooseOne(player, options):
ActionChooseOne(chooser: player, options: options)

case let .activateCards(player, cards):
ActionActivateCards(player: player, cards: cards)
case let .activate(cards, player):
ActionActivate(player: player, cards: cards)

case let .cancel(action):
ActionCancel(action: action)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,34 @@ private extension CardEffect {
case .shoot:
EffectJust { .damage(1, player: $0.player()) }

case .draw:
EffectJust { .draw(player: $0.player()) }
case .drawDeck:
EffectJust { .drawDeck(player: $0.player()) }

case .discover:
EffectJust { _ in .discover }

case .putBack:
EffectJust { _ in .putBack }

case .draw:
EffectJust { _ in .draw }

case .setTurn:
EffectJust { .setTurn($0.player()) }

case .eliminate:
EffectJust { .eliminate(player: $0.player()) }

case let .chooseCard(card):
EffectChooseCard(card: card)
case .drawArena:
EffectDrawArena()

case let .discard(card, chooser):
EffectDiscard(card: card, chooser: chooser)

case let .steal(card, toPlayer):
EffectSteal(card: card, toPlayer: toPlayer)

case let .passInplay(card, toPlayer):
case let .passInPlay(card, toPlayer):
EffectPassInPlay(card: card, toPlayer: toPlayer)

case let .target(target, effect):
Expand All @@ -73,8 +79,8 @@ private extension CardEffect {
case .nothing:
EffectNone()

case let .luck(regex, onSuccess, onFailure):
EffectLuck(regex: regex, onSuccess: onSuccess, onFailure: onFailure)
case let .luck(card, regex, onSuccess, onFailure):
EffectLuck(card: card, regex: regex, onSuccess: onSuccess, onFailure: onFailure)

case .activateCounterCards:
EffectActivateCounterCards()
Expand All @@ -87,9 +93,6 @@ private extension CardEffect {

case .counterShoot:
EffectCounterShoot()

case let .revealLastDrawn(regex, onSuccess):
EffectRevealLastDrawn(regex: regex, onSuccess: onSuccess)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
//
// EffectChooseCard.swift
// EffectDrawArena.swift
//
//
// Created by Hugues Stephano TELOLAHY on 20/06/2023.
//

struct EffectChooseCard: EffectResolver {
let card: ArgCard

struct EffectDrawArena: EffectResolver {
func resolve(state: GameState, ctx: EffectContext) throws -> [GameAction] {
let card = ArgCard.selectArena
let player = ctx.player()
return try card.resolve(state: state, ctx: ctx) {
switch card {
case .selectArena:
.chooseArena($0, player: player)
.drawArena($0, player: player)

default:
fatalError("unexpected")
Expand Down
Loading