-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
392d67d
commit d12b43a
Showing
13 changed files
with
134 additions
and
172 deletions.
There are no files selected for viewing
108 changes: 108 additions & 0 deletions
108
WildWestOnline/Core/Game/Sources/PlayersState/PlayersState.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// | ||
// PlayersState.swift | ||
// | ||
// | ||
// Created by Hugues Stephano TELOLAHY on 24/06/2024. | ||
// | ||
// swiftlint:disable type_contents_order | ||
|
||
import Foundation | ||
import Redux | ||
|
||
public struct PlayersState: Equatable, Codable { | ||
public var players: [String: Player] | ||
|
||
public struct Player: Equatable, Codable { | ||
public let maxHealth: Int | ||
public var health: Int | ||
} | ||
} | ||
|
||
public extension PlayersState { | ||
static let reducer: ThrowingReducer<Self> = { state, action in | ||
switch action { | ||
case let GameAction.heal(amount, id): | ||
try state.heal(amount: amount, id: id) | ||
|
||
case let GameAction.damage(amount, id): | ||
state.damage(amount: amount, id: id) | ||
|
||
default: | ||
state | ||
} | ||
} | ||
} | ||
|
||
private extension PlayersState { | ||
func heal(amount: Int, id: String) throws -> Self { | ||
var playerObj = players.get(id) | ||
|
||
guard playerObj.health < playerObj.maxHealth else { | ||
throw GameError.playerAlreadyMaxHealth(id) | ||
} | ||
|
||
playerObj.health = min(playerObj.health + amount, playerObj.maxHealth) | ||
|
||
var state = self | ||
state.players[id] = playerObj | ||
return state | ||
} | ||
|
||
func damage(amount: Int, id: String) -> Self { | ||
var playerObj = players.get(id) | ||
playerObj.health -= amount | ||
|
||
var state = self | ||
state.players[id] = playerObj | ||
return state | ||
} | ||
} | ||
|
||
public extension PlayersState { | ||
class Builder { | ||
private var players: [String: PlayersState.Player] = [:] | ||
|
||
public func build() -> PlayersState { | ||
.init(players: players) | ||
} | ||
|
||
public func withPlayer(_ id: String, builderFunc: (PlayersState.Player.Builder) -> PlayersState.Player.Builder = { $0 }) -> Self { | ||
let builder = PlayersState.Player.makeBuilder() | ||
let player = builderFunc(builder).build() | ||
players[id] = player | ||
return self | ||
} | ||
} | ||
|
||
static func makeBuilder() -> Builder { | ||
Builder() | ||
} | ||
} | ||
|
||
public extension PlayersState.Player { | ||
class Builder { | ||
private var maxHealth: Int = 0 | ||
private var health: Int = 0 | ||
|
||
public func build() -> PlayersState.Player { | ||
.init( | ||
maxHealth: maxHealth, | ||
health: health | ||
) | ||
} | ||
|
||
public func withHealth(_ value: Int) -> Self { | ||
health = value | ||
return self | ||
} | ||
|
||
public func withMaxHealth(_ value: Int) -> Self { | ||
maxHealth = value | ||
return self | ||
} | ||
} | ||
|
||
static func makeBuilder() -> Builder { | ||
Builder() | ||
} | ||
} |
17 changes: 0 additions & 17 deletions
17
WildWestOnline/Core/Game/Sources/Reducer/Action+Reducer/ActionDamage.swift
This file was deleted.
Oops, something went wrong.
17 changes: 0 additions & 17 deletions
17
WildWestOnline/Core/Game/Sources/Reducer/Action+Reducer/ActionHeal.swift
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.