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

Middleware lifting #38

Merged
merged 40 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
9359344
wip
stephtelolahy Nov 25, 2023
898476f
wip
stephtelolahy Nov 25, 2023
66558ae
wip
stephtelolahy Nov 25, 2023
43cf136
wip
stephtelolahy Nov 25, 2023
4d89289
wip
stephtelolahy Nov 25, 2023
db3d83c
wip
stephtelolahy Nov 25, 2023
4871baf
wip
stephtelolahy Nov 26, 2023
f3a57ea
wip
stephtelolahy Nov 26, 2023
be52ef7
wip
stephtelolahy Nov 26, 2023
06ac516
wip
stephtelolahy Nov 26, 2023
ccf9b72
wip
stephtelolahy Nov 26, 2023
eb228db
wip
stephtelolahy Nov 26, 2023
b965e4d
wip
stephtelolahy Nov 26, 2023
b0459ce
wip
stephtelolahy Nov 26, 2023
8910678
wip
stephtelolahy Nov 26, 2023
7ba192b
wip
stephtelolahy Nov 26, 2023
bbbc4bc
wip
stephtelolahy Nov 26, 2023
7e6fbf8
wip
stephtelolahy Nov 26, 2023
7810dde
wip
stephtelolahy Nov 26, 2023
73ba724
wip
stephtelolahy Nov 27, 2023
bbc7e3e
wip
stephtelolahy Nov 27, 2023
e7c560b
wip
stephtelolahy Nov 27, 2023
b005d4c
wip
stephtelolahy Nov 27, 2023
317e14b
wip
stephtelolahy Nov 27, 2023
6b68f61
wip
stephtelolahy Nov 27, 2023
4c52746
wip
stephtelolahy Nov 27, 2023
c858742
wip
stephtelolahy Nov 27, 2023
54d1f82
wip
stephtelolahy Nov 27, 2023
02b1177
wip
stephtelolahy Nov 27, 2023
17f3aa3
wip
stephtelolahy Nov 27, 2023
8ab91a7
wip
stephtelolahy Nov 27, 2023
e0ad64b
wip
stephtelolahy Nov 27, 2023
198030d
wip
stephtelolahy Nov 27, 2023
7dd5f70
wip
stephtelolahy Nov 27, 2023
47a1cc7
wip
stephtelolahy Nov 27, 2023
e896c75
wip
stephtelolahy Nov 27, 2023
737f9b6
wip
stephtelolahy Nov 27, 2023
4e79aa3
wip
stephtelolahy Nov 27, 2023
6ce3508
wip
stephtelolahy Nov 27, 2023
47edf78
wip
stephtelolahy Nov 27, 2023
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
26 changes: 0 additions & 26 deletions App/GameApp.swift

This file was deleted.

16 changes: 0 additions & 16 deletions App/LoggerMiddleware.swift

This file was deleted.

File renamed without changes.
45 changes: 45 additions & 0 deletions GameApp.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// GameApp.swift
// WildWestOnline
//
// Created by Hugues Telolahy on 02/04/2023.
//

import SwiftUI
import Redux
import Screen
import Game

private let store = Store<AppState>(
initial: .init(),
reducer: AppState.reducer,
middlewares: [
LoggerMiddleware(),
ComposedMiddleware(middlewares: [
CardEffectsMiddleware(),
NextActionMiddleware(),
ActivateCardsMiddleware()
])
.lift(stateMap: extractGameState),
]
)

@main
struct GameApp: App {
var body: some Scene {
WindowGroup {
AppView()
.environmentObject(store)
}
}
}

private var extractGameState: (AppState) -> GameState? = { state in
guard let lastScreen = state.screens.last,
case let .game(gamePlayState) = lastScreen,
let gameState = gamePlayState.gameState else {
return nil
}

return gameState
}
3 changes: 2 additions & 1 deletion GameKit/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import PackageDescription

let package = Package(
name: "GameKit",
defaultLocalization: "fr",
platforms: [
.iOS(.v15),
.iOS(.v16),
.macOS(.v13)
],
products: [
Expand Down
2 changes: 2 additions & 0 deletions GameKit/Sources/Game/DSL/Modifiers/CardEffect+Modifiers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
//

public extension CardEffect {
static let nothing: Self = .group([])

static func group(@CardEffectsBuilder content: () -> [Self]) -> Self {
.group(content())
}
Expand Down
2 changes: 2 additions & 0 deletions GameKit/Sources/Game/DSL/Modifiers/GameAction+Modifiers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
//

public extension GameAction {
static let nothing: Self = .group([])

static func group(@GameActionBuilder content: () -> [Self]) -> Self {
.group(content())
}
Expand Down
8 changes: 6 additions & 2 deletions GameKit/Sources/Game/GameStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ public func createGameStore(initial: GameState) -> Store<GameState> {
initial: initial,
reducer: GameState.reducer,
middlewares: [
gameLoopMiddleware,
eventLoggerMiddleware
ComposedMiddleware(middlewares: [
CardEffectsMiddleware(),
NextActionMiddleware(),
ActivateCardsMiddleware()
]),
EventLoggerMiddleware()
]
)
}
35 changes: 0 additions & 35 deletions GameKit/Sources/Game/Middleware/AIAgent/AIAgentMiddleware.swift

This file was deleted.

36 changes: 36 additions & 0 deletions GameKit/Sources/Game/Middleware/AIAgentMiddleware.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// AIAgentMiddleware.swift
//
//
// Created by Hugues Telolahy on 14/07/2023.
//

import Combine
import Redux

public final class AIAgentMiddleware: Middleware<GameState> {
override public func handle(action: Action, state: GameState) -> AnyPublisher<Action, Never>? {
guard let move = evaluateAIMove(state: state) else {
return nil
}

return Just(move).eraseToAnyPublisher()
}

private func evaluateAIMove(state: GameState) -> GameAction? {
guard state.isOver == nil else {
return nil
}

if let active = state.active,
let randomCard = active.cards.randomElement() {
return GameAction.play(randomCard, player: active.player)
}

if let chooseOne = state.chooseOne,
let randomAction = chooseOne.options.values.randomElement() {
return randomAction
}

return nil
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
//
// HandlerActivateCards.swift
// ActivateCardsMiddleware.swift
//
//
// Created by Hugues Telolahy on 03/11/2023.
// Created by Hugues Stephano TELOLAHY on 27/11/2023.
//
import Combine
import Redux

struct HandlerActivateCards: GameActionHandler {
func handle(action: GameAction, state: GameState) -> GameAction? {
public final class ActivateCardsMiddleware: Middleware<GameState> {
override public func handle(action: Action, state: GameState) -> AnyPublisher<Action, Never>? {
guard state.sequence.isEmpty,
state.isOver == nil,
state.chooseOne == nil,
Expand All @@ -27,7 +29,8 @@ struct HandlerActivateCards: GameActionHandler {
return nil
}

return .activate(activeCards, player: player)
let activateAction = GameAction.activate(activeCards, player: player)
return Just(activateAction).eraseToAnyPublisher()
}

private func activableCardsOfPlayer(_ playerObj: Player) -> [String] {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
//
// HandlerTriggeredEffects.swift
// CardEffectsMiddleware.swift
//
//
// Created by Hugues Telolahy on 03/11/2023.
// Created by Hugues Stephano TELOLAHY on 27/11/2023.
//
import Combine
import Redux

public final class CardEffectsMiddleware: Middleware<GameState> {
override public func handle(action: Action, state: GameState) -> AnyPublisher<Action, Never>? {
guard let action = action as? GameAction else {
return nil
}

struct HandlerTriggeredEffects: GameActionHandler {
func handle(action: GameAction, state: GameState) -> GameAction? {
var triggered: [GameAction] = []

// active players
Expand Down Expand Up @@ -48,7 +54,8 @@ struct HandlerTriggeredEffects: GameActionHandler {
}
// </sort triggered by priority>

return .group(triggered)
let cardEffects = GameAction.group(triggered)
return Just(cardEffects).eraseToAnyPublisher()
}

private func triggeredEffect(
Expand Down
31 changes: 17 additions & 14 deletions GameKit/Sources/Game/Middleware/EventLoggerMiddleware.swift
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
// swiftlint:disable:this file_name
//
// EventLoggerMiddleware.swift
//
//
// Created by Hugues Telolahy on 03/06/2023.
//
// swiftlint:disable prefixed_toplevel_constant

import Combine
import Redux

public let eventLoggerMiddleware: Middleware<GameState> = { state, action in
if let error = state.error {
print("❌ \(error) on \(action)".removingNamespace())
} else if let event = action as? GameAction {
print("\(event.image) \(event)".removingNamespace())
public final class EventLoggerMiddleware: Middleware<GameState> {
override public init() {
super.init()
}

return nil
}
override public func handle(action: Action, state: GameState) -> AnyPublisher<Action, Never>? {
guard let action = action as? GameAction else {
return nil
}

private extension GameAction {
var image: String {
if isRenderable {
"✅"
if let error = state.error {
print("❌ \(error) on \(action)".removingNamespace())
} else {
"➡️"
let image: String = if action.isRenderable {
"✅"
} else {
"➡️"
}
print("\(image) \(action)".removingNamespace())
}

return nil
}
}

Expand Down
35 changes: 0 additions & 35 deletions GameKit/Sources/Game/Middleware/GameLoop/GameLoopMiddleware.swift

This file was deleted.

20 changes: 0 additions & 20 deletions GameKit/Sources/Game/Middleware/GameLoop/HandlerNextAction.swift

This file was deleted.

Loading
Loading