Member-only story

Naked Networking with SwiftUI gamification II

Mark Lucking
3 min readFeb 2, 2020

If you’re following this series, you know this is article 5, if you’re not than you need to rewind and read articles 1–4 first.

Naked Networking with SwiftUI
More Naked Networking, more SwiftUI
Naked Networking and SwiftUI, the game plan
Naked Networking and SwiftUI, gamification

Network

We need another protocol like our pingPublisher. At the moment when you take too long to send the ball back, it you surely lose, but the winner doesn’t know they’re won. Add this code to Connect.swift.

let winPublisher = PassthroughSubject<Void, Never>()

Now you’re going to fire this message when you win back to the other player. On their side they need to know what it is and respond accordingly. So we need to modify our receive method a little. The new/replacement code is in bold italic.

DispatchQueue.main.async {
globalVariable.score = backToString
if backToString != "win" {
pingPublisher.send(string2Send)
} else {
winPublisher.send()
}

}

SwiftUI

In our interface we need to introduce another label to show the status, you win or you lose.

Text(timeToDie)
Text(volley).onReceive(winPublisher, perform: {
self.volley = "You Win"
self.disable = false
})

So the other side loses, sends a win ping, you get it; publish the win and enable to the…

--

--

Mark Lucking
Mark Lucking

Written by Mark Lucking

Coding for 35+ years, enjoying using and learning Swift/iOS development. Writer @ Better Programming, @The StartUp, @Mac O’Clock, Level Up Coding & More

No responses yet