Naked Networking with SwiftUI gamification II

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.

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.

SwiftUI

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

So the other side loses, sends a win ping, you get it; publish the win and enable to the whack button so that you start another game.

Here the code to send that win ping, note in testing we found that the timer wasn’t disabled fast enough, so you could whack it back and then lose, only to win a second later which is why we started monitoring the whack button as well.

We also added this piece of code to the mix cause the time you waited after a win, came thru to the otherside when you restarted a new game.

And there you have it. Make all the changes to the code and have a go. It will work, but you’ll notice if you start to play frantically it loses it and both buttons will be disabled.

This is UDP for you. Which is fast but unreliable. You notice too if you hit whack too fast, it will lose its mind too. As feature discovered by my ten year old son.

Before we on, lets try and address the latter of these in the project. Now I already disable the button after you hit, but it seems SwiftUI is a tad too slow for me in that I can hit it several times before it is disabled. We need a different solution.

Our fix for this is to add another State variable, call it hit. We than alter the button code to use it as a gate to send pings. The code looks like this with changes in bold italic.

Obviously we need to reset this to false again when it gets back the ping, so we do in the pingPublisher and winPublisher blocks.

This fixes the multiple hits, obviously not the UDP issue if we miss a packet and both whacks end up disabled. To fix this we add another button, to reset the game above the switch.

The code for that is straight forward. And one other nano change, I added this code to change the pong/ping label if you lose, so that it matches the the label on the other side when you win. New code in bold italic.

Read part six to see how to did the score keeping.

Written by

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store