Skip to content

Commit

Permalink
refactor: use playersState
Browse files Browse the repository at this point in the history
  • Loading branch information
stephtelolahy committed Jun 29, 2024
1 parent 4d2e149 commit 6b4b9d0
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@ struct PlayerDamaged: ArgPlayerResolver {
func resolve(state: GameState, ctx: EffectContext) throws -> PlayerArgOutput {
let damaged = state.playOrder
.starting(with: ctx.sourceActor)
.filter { state.player($0).isDamaged }
.filter { state.isPlayerDamaged(id: $0) }
return .identified(damaged)
}
}

private extension GameState {
func isPlayerDamaged(id: String) -> Bool {
let playerObj = playersState.players.get(id)
return playerObj.health < playerObj.maxHealth
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ struct OnDamage: PlayReqMatcher {
func match(state: GameState, ctx: PlayReqContext) -> Bool {
if case let .damage(_, player) = ctx.event,
player == ctx.actor,
state.player(player).health > 0 {
state.playersState.players.get(player).health > 0 {
true
} else {
false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ struct OnDamageLethal: PlayReqMatcher {
func match(state: GameState, ctx: PlayReqContext) -> Bool {
if case let .damage(_, player) = ctx.event,
player == ctx.actor,
state.player(player).health <= 0 {
state.playersState.players.get(player).health <= 0 {
true
} else {
false
Expand Down
8 changes: 7 additions & 1 deletion WildWestOnline/Core/Game/Sources/Setup/Setup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,20 @@ public enum Setup {
}

var playerDictionary: [String: Player] = [:]
var playersStateDictionary: [String: PlayersState.Player] = [:]
var playOrder: [String] = []

for player in players {
playerDictionary[player.id] = player
let health = player.attributes[.maxHealth] ?? 0
playersStateDictionary[player.id] = .init(maxHealth: health, health: health)
playOrder.append(player.id)
}

let playersState = PlayersState(players: playersStateDictionary)

return GameState(
playersState: playersState,
players: playerDictionary,
playOrder: playOrder,
startOrder: playOrder,
Expand Down Expand Up @@ -106,7 +113,6 @@ private extension Setup {
figure: figure,
abilities: abilities,
attributes: figureObj.attributes,
health: health,
hand: hand,
inPlay: []
)
Expand Down

0 comments on commit 6b4b9d0

Please sign in to comment.