Skip to content

Commit 20064ba

Browse files
committed
refactor to allow for multiple rounds using a timer
1 parent 956f5d6 commit 20064ba

6 files changed

Lines changed: 72 additions & 34 deletions

File tree

GlobalWordRacer/GlobalWordRacer/Views/CurrentSelectionView.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,25 @@ import SwiftUI
1111
struct CurrentSelectionView: View {
1212

1313
@Binding var text: String
14-
var handler: () -> Void
14+
@Binding var hasRoundEnded: Bool
15+
let handler: () -> Void
1516

1617
var body: some View {
1718
Button(action: {
1819
if (self.text != "") {
1920
self.handler()
2021
}
2122
}) {
22-
Text(text.count > 0 ? text.uppercased() : " ")
23+
Text(hasRoundEnded ? "ROUND OVER" : text.count > 0 ? text.uppercased() : " ")
2324
.fontWeight(.semibold)
2425
.font(.title)
2526
.padding()
2627
.frame(width: UIScreen.main.bounds.width - 60)
27-
.background(Color.blue)
28+
.background(hasRoundEnded ? Color.gray : Color.blue)
2829
.foregroundColor(.white)
2930
.cornerRadius(40)
3031
}
32+
.disabled(hasRoundEnded)
3133
.padding(.top, 20)
3234
}
3335

@@ -36,7 +38,7 @@ struct CurrentSelectionView: View {
3638
struct CurrentSelectionView_Previews: PreviewProvider {
3739

3840
static var previews: some View {
39-
CurrentSelectionView(text: .constant("Quest"), handler: {})
41+
CurrentSelectionView(text: .constant("Quest"), hasRoundEnded: .constant(false), handler: {})
4042
}
4143

4244
}

GlobalWordRacer/GlobalWordRacer/Views/FoundSolutionsView.swift

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,31 @@ import SwiftUI
1111
struct FoundSolutionsView: View {
1212

1313
@Binding var list: [String]
14+
@Binding var solutions: [String]
15+
@Binding var hasRoundEnded: Bool
1416

1517
var body: some View {
1618
VStack {
1719
Text(
18-
list.count > 0
19-
? "\(list.count) Word\(list.count > 1 ? "s": "") Found (\(list.map(self.pointsForWord).reduce(0, +)) Points)"
20-
: "No Words Found"
20+
hasRoundEnded
21+
? "Full Solutions List"
22+
: list.count > 0
23+
? "\(list.count) Word\(list.count > 1 ? "s": "") Found (\(list.map(self.pointsForWord).reduce(0, +)) Points)"
24+
: "No Words Found"
2125
)
2226
.foregroundColor(.black)
2327
.fontWeight(.semibold)
2428
.font(.headline)
2529

2630
ScrollView(.vertical) {
2731
VStack {
28-
ForEach(list.reversed(), id: \.self) { word in
32+
ForEach(hasRoundEnded ? solutions.sorted { $0.count > $1.count } : list.reversed(), id: \.self) { word in
2933
HStack {
3034
Text(word)
31-
.foregroundColor(.black)
35+
.foregroundColor(self.hasRoundEnded && self.list.contains(word) ? .green : .black)
3236
Spacer()
3337
Text("\(self.pointsForWord(word))")
34-
.foregroundColor(.black)
38+
.foregroundColor(self.hasRoundEnded && self.list.contains(word) ? .green : .black)
3539
}
3640
}
3741
}.frame(width: UIScreen.main.bounds.width - 60)
@@ -52,7 +56,7 @@ struct FoundSolutionsView: View {
5256
struct FoundSolutionsView_Previews: PreviewProvider {
5357

5458
static var previews: some View {
55-
FoundSolutionsView(list: .constant(["Word1", "Word2"]))
59+
FoundSolutionsView(list: .constant(["Word1", "Word2"]), solutions: .constant(["Word1", "Word2"]), hasRoundEnded: .constant(false))
5660
}
5761

5862
}

GlobalWordRacer/GlobalWordRacer/Views/GameView.swift

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ struct GameView: View {
1616
@State private var foundSolutions: [String] = []
1717
@State private var invalidWord = ""
1818
@State private var duplicateWord = ""
19+
@State private var hasRoundEnded = false
20+
@State private var timeRemaining: Int = 30
21+
let gameTimer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
22+
let loadNewGameHandler: (_ callback: @escaping () -> Void) -> Void
1923

2024
var body: some View {
2125
VStack {
@@ -27,9 +31,31 @@ struct GameView: View {
2731
.background(Color.blue)
2832
.foregroundColor(.white)
2933
buildBoard()
30-
CurrentSelectionView(text: $currentSelection, handler: selectionHandler)
34+
CurrentSelectionView(text: $currentSelection, hasRoundEnded: $hasRoundEnded, handler: selectionHandler)
35+
Text(timeRemaining > 0
36+
? "This Round Ends In \(timeRemaining) Seconds"
37+
: "Next Round Starts In \(16 + timeRemaining) Seconds"
38+
)
39+
.padding(.top)
40+
.foregroundColor(.black)
41+
.onReceive(gameTimer) { input in
42+
self.timeRemaining -= 1
43+
if (self.timeRemaining <= 0) {
44+
self.hasRoundEnded = true
45+
self.currentSelection = ""
46+
self.invalidWord = ""
47+
self.duplicateWord = ""
48+
}
49+
if (self.timeRemaining <= -15) {
50+
self.loadNewGameHandler({
51+
self.timeRemaining = 30
52+
self.hasRoundEnded = false
53+
self.foundSolutions = []
54+
})
55+
}
56+
}
3157
InvalidWordView(word: $invalidWord, duplicate: $duplicateWord)
32-
FoundSolutionsView(list: $foundSolutions)
58+
FoundSolutionsView(list: $foundSolutions, solutions: $solutions, hasRoundEnded: $hasRoundEnded)
3359
Spacer()
3460
}
3561
}
@@ -42,7 +68,7 @@ struct GameView: View {
4268
Spacer()
4369
ForEach(self.grid[rowIndex].indices) { cellIndex in
4470
Group {
45-
LetterView(text: self.$grid[rowIndex][cellIndex], handler: self.letterHandler)
71+
LetterView(text: self.$grid[rowIndex][cellIndex], hasRoundEnded: self.$hasRoundEnded, handler: self.letterHandler)
4672
Spacer()
4773
}
4874
}
@@ -82,7 +108,7 @@ struct GameView_Previews: PreviewProvider {
82108
["E", "Qu", "G", "H"],
83109
["I", "J", "K", "L"],
84110
["M", "N", "O", "P"]
85-
]), solutions: .constant([]))
111+
]), solutions: .constant([]), loadNewGameHandler: { callback in })
86112
}
87113

88114
}

GlobalWordRacer/GlobalWordRacer/Views/LetterView.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import SwiftUI
1111
struct LetterView: View {
1212

1313
@Binding var text: String
14-
var handler: (String) -> Void
14+
@Binding var hasRoundEnded: Bool
15+
let handler: (String) -> Void
1516

1617
var body: some View {
1718
Button(action: {
@@ -22,10 +23,11 @@ struct LetterView: View {
2223
.font(.title)
2324
.frame(width: 40)
2425
.padding()
25-
.background(Color.blue)
26+
.background(hasRoundEnded ? Color.gray : Color.blue)
2627
.foregroundColor(.white)
2728
.cornerRadius(40)
2829
}
30+
.disabled(hasRoundEnded)
2931
}
3032

3133
}
@@ -34,9 +36,9 @@ struct LetterView_Previews: PreviewProvider {
3436

3537
static var previews: some View {
3638
VStack(spacing: 20) {
37-
LetterView(text: .constant("A"), handler: { (letter: String) in })
38-
LetterView(text: .constant("Qu"), handler: { (letter: String) in })
39-
LetterView(text: .constant("Z"), handler: { (letter: String) in })
39+
LetterView(text: .constant("A"), hasRoundEnded: .constant(false), handler: { (letter: String) in })
40+
LetterView(text: .constant("Qu"), hasRoundEnded: .constant(false), handler: { (letter: String) in })
41+
LetterView(text: .constant("Z"), hasRoundEnded: .constant(false), handler: { (letter: String) in })
4042
}
4143
}
4244

GlobalWordRacer/GlobalWordRacer/Views/MainView.swift

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// Copyright © 2020 Andrew Gerst. All rights reserved.
77
//
88

9+
import Alamofire
910
import SwiftUI
1011

1112
struct MainView: View {
@@ -17,15 +18,27 @@ struct MainView: View {
1718
var body: some View {
1819
Group {
1920
if showGame {
20-
GameView(grid: $grid, solutions: $solutions)
21+
GameView(grid: $grid, solutions: $solutions, loadNewGameHandler: loadNewGame)
2122
} else {
22-
WelcomeView(showGame: $showGame, grid: $grid, solutions: $solutions)
23+
WelcomeView(showGame: $showGame, loadNewGameHandler: loadNewGame)
2324
}
2425
}
2526
.edgesIgnoringSafeArea(.all)
2627
.background(Color.white)
2728
}
2829

30+
func loadNewGame(callback: @escaping () -> Void) {
31+
AF.request("http://hnswave.co:8000/grid")
32+
.validate()
33+
.responseDecodable(of: GridAndSolutions.self) { response in
34+
guard let data = response.value else { return }
35+
self.grid = data.grid
36+
self.solutions = data.solutions
37+
self.showGame = true
38+
callback()
39+
}
40+
}
41+
2942
}
3043

3144
struct MainView_Previews: PreviewProvider {

GlobalWordRacer/GlobalWordRacer/Views/WelcomeView.swift

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@
66
// Copyright © 2020 Andrew Gerst. All rights reserved.
77
//
88

9-
import Alamofire
109
import SwiftUI
1110

1211
struct WelcomeView: View {
1312

1413
@Binding var showGame: Bool
15-
@Binding var grid: [[String]]
16-
@Binding var solutions: [String]
14+
let loadNewGameHandler: (_ callback: @escaping () -> Void) -> Void
1715

1816
var body: some View {
1917
VStack {
@@ -28,14 +26,7 @@ struct WelcomeView: View {
2826

2927
Button(
3028
action: {
31-
AF.request("http://hnswave.co:8000/grid")
32-
.validate()
33-
.responseDecodable(of: GridAndSolutions.self) { response in
34-
guard let data = response.value else { return }
35-
self.grid = data.grid
36-
self.solutions = data.solutions
37-
self.showGame = true
38-
}
29+
self.loadNewGameHandler({})
3930
},
4031
label: {
4132
Text("Join Game")
@@ -53,7 +44,7 @@ struct WelcomeView: View {
5344
struct WelcomeView_Previews: PreviewProvider {
5445

5546
static var previews: some View {
56-
WelcomeView(showGame: .constant(false), grid: .constant([]), solutions: .constant([]))
47+
WelcomeView(showGame: .constant(false), loadNewGameHandler: { callback in })
5748
}
5849

5950
}

0 commit comments

Comments
 (0)