Skip to content

Commit

Permalink
test: gameState using PlayersState
Browse files Browse the repository at this point in the history
  • Loading branch information
stephtelolahy committed Jun 29, 2024
1 parent 6b4b9d0 commit ac3e867
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 57 deletions.
21 changes: 0 additions & 21 deletions WildWestOnline/Core/Game/Sources/PlayersState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,27 +58,6 @@ private extension PlayersState {
}
}

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) -> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

public extension GameState {
class Builder {
private var playersStateBuilder: PlayersState.Builder = .init()
private var playersDictionary: [String: PlayersState.Player] = [:]
private var players: [String: Player] = [:]
private var playOrder: [String] = []
private var turn: String?
Expand All @@ -26,7 +26,7 @@ public extension GameState {

public func build() -> GameState {
.init(
playersState: playersStateBuilder.build(),
playersState: .init(players: playersDictionary),
players: players,
playOrder: playOrder,
startOrder: playOrder,
Expand All @@ -46,11 +46,6 @@ public extension GameState {
)
}

public func withPlayersState(_ builderFunc: (PlayersState.Builder) -> PlayersState.Builder) -> Self {
_ = builderFunc(playersStateBuilder)
return self
}

public func withTurn(_ value: String) -> Self {
turn = value
return self
Expand Down Expand Up @@ -119,6 +114,13 @@ public extension GameState {
return self
}

public func withPlayerState(_ id: String, builderFunc: (PlayersState.Player.Builder) -> PlayersState.Player.Builder) -> Self {
let builder = PlayersState.Player.makeBuilder()
let player = builderFunc(builder).build()
playersDictionary[id] = player
return self
}

public func withWaitDelayMilliseconds(_ value: Int) -> Self {
waitDelayMilliseconds = value
return self
Expand Down
4 changes: 4 additions & 0 deletions WildWestOnline/Core/Game/Sources/State/GameState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,8 @@ public extension GameState {
}
return player
}

func playerState(_ id: String) -> PlayersState.Player {
playersState.players.get(id)
}
}
17 changes: 10 additions & 7 deletions WildWestOnline/Core/Game/Tests/DSL/GameTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,19 @@ final class GameTests: XCTestCase {
.withCards(["name": Card.makeBuilder(name: "name").build()])
.withChooseOne(.cardToDraw, options: [], player: "p1")
.withSequence([.discover])
.withPlayer("p1") {
$0.withHealth(3)
}
.withPlayer("p1")
.withPlayer("p2") {
$0.withHealth(4)
.withAttributes([.weapon: 2])
$0.withAttributes([.weapon: 2])
.withAbilities(["a1"])
.withHand(["c21", "c22"])
.withInPlay(["c23", "c24"])
}
.withPlayerState("p1") {
$0.withHealth(3)
}
.withPlayerState("p2") {
$0.withHealth(4)
}
.build()

// Then
Expand All @@ -152,13 +155,13 @@ final class GameTests: XCTestCase {
XCTAssertEqual(state.playOrder, ["p1", "p2"])

XCTAssertNotNil(state.players["p1"])
XCTAssertEqual(state.player("p1").health, 3)
XCTAssertEqual(state.playerState("p1").health, 3)
XCTAssertEqual(state.player("p1").attributes, [:])
XCTAssertEqual(state.player("p1").hand, [])
XCTAssertEqual(state.player("p1").inPlay, [])

XCTAssertNotNil(state.players["p2"])
XCTAssertEqual(state.player("p2").health, 4)
XCTAssertEqual(state.playerState("p2").health, 4)
XCTAssertEqual(state.player("p2").attributes[.weapon], 2)
XCTAssertEqual(state.player("p2").abilities, ["a1"])
XCTAssertEqual(state.player("p2").hand, ["c21", "c22"])
Expand Down
20 changes: 8 additions & 12 deletions WildWestOnline/Core/Game/Tests/Mechanics/DamageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,31 @@ import GameCore
import XCTest

final class DamageTests: XCTestCase {
func test_damage_with1LifePoint_shouldReduceHealthBy1() {
// Given
let state = GameState.makeBuilder()
.withPlayer("p1") {
private var state: GameState {
GameState.makeBuilder()
.withPlayerState("p1") {
$0.withHealth(2)
}
.build()
}

func test_damage_with1LifePoint_shouldReduceHealthBy1() {
// Given
// When
let action = GameAction.damage(1, player: "p1")
let result = GameState.reducer(state, action)

// Then
XCTAssertEqual(result.player("p1").health, 1)
XCTAssertEqual(result.playerState("p1").health, 1)
}

func test_damage_with2LifePoints_shouldReduceHealthBy2() {
// Given
let state = GameState.makeBuilder()
.withPlayer("p1") {
$0.withHealth(2)
}
.build()

// When
let action = GameAction.damage(2, player: "p1")
let result = GameState.reducer(state, action)

// Then
XCTAssertEqual(result.player("p1").health, 0)
XCTAssertEqual(result.playerState("p1").health, 0)
}
}
16 changes: 8 additions & 8 deletions WildWestOnline/Core/Game/Tests/Mechanics/HealTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import XCTest
final class HealTests: XCTestCase {
private var state: GameState {
GameState.makeBuilder()
.withPlayer("p1") {
.withPlayerState("p1") {
$0.withHealth(2)
.withAttributes([.maxHealth: 4])
.withMaxHealth(4)
}
.build()
}
Expand All @@ -24,7 +24,7 @@ final class HealTests: XCTestCase {
let result = GameState.reducer(state, action)

// Then
XCTAssertEqual(result.player("p1").health, 3)
XCTAssertEqual(result.playerState("p1").health, 3)
}

func test_heal_beingDamaged_amountEqualDamage_shouldGainLifePoints() {
Expand All @@ -33,24 +33,24 @@ final class HealTests: XCTestCase {
let result = GameState.reducer(state, action)

// Then
XCTAssertEqual(result.player("p1").health, 4)
XCTAssertEqual(result.playerState("p1").health, 4)
}

func test_heal_beingDamaged_amountGreaterThanDamage_shouldGainLifePointsLimitedToMaxHealth() {
// When
let action = GameAction.heal(2, player: "p1")
let action = GameAction.heal(3, player: "p1")
let result = GameState.reducer(state, action)

// Then
XCTAssertEqual(result.player("p1").health, 4)
XCTAssertEqual(result.playerState("p1").health, 4)
}

func test_heal_alreadyMaxHealth_shouldThrowError() {
// Given
let state = GameState.makeBuilder()
.withPlayer("p1") {
.withPlayerState("p1") {
$0.withHealth(4)
.withAttributes([.maxHealth: 4])
.withMaxHealth(4)
}
.build()

Expand Down
4 changes: 2 additions & 2 deletions WildWestOnline/Core/Game/Tests/Setup/SetupTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ final class SetupTests: XCTestCase {
XCTAssertTrue(state.startOrder.contains(["p1", "p2"]))

// should set players to max health
XCTAssertEqual(state.player("p1").health, 4)
XCTAssertEqual(state.player("p2").health, 3)
XCTAssertEqual(state.playerState("p1").health, 4)
XCTAssertEqual(state.playerState("p2").health, 3)

// should set players hand cards to health
XCTAssertEqual(state.player("p1").hand.count, 4)
Expand Down

0 comments on commit ac3e867

Please sign in to comment.