peer-to-peer Networking iOS 7 - iphone

I am new to networking over iOS and i am having a lot of difficulty in finding a good tutorial to learn about it. I am making a game and implemented it for a single player but now i have to make it multiplayer. What i have to do on server is only comparing the final score of the two players playing the game and display the message that who won the game. There are no asynchronous calls required the final score is only needed when the timer ends and reach 0. Please help me with for a kick start, really need it. I have been scratching my head over bonjour but unable to understand it as i am very new to networking.

Multipeer Connectivity will work if all players are in close proximity or on the same network. However since you did not specify that those conditions would necessarily be met, you probably want to look at the Game Center networking functionality at https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/GameKit_Guide/Matchmaking/Matchmaking.html

Related

iOS/iPhone -- How to implement a computer player (random actor) in matches with disconnected players?

First, I will confess that I haven't attempted this in code, so I apologize if this is jumping the gun. I realize that adequate effort and research are a prerequisite to posting good questions. But after reading GameKit and Game Center documentation, as well as iOS Developer Cookbooks, I can't seem to find the right functionality in GameKit and was wondering how I might venture outside it API-wise to solve this problem.
At the simplest level, I would like to implement a time-limited two-player game that does not shut down after one player disconnects, accidentally or maliciously (I currently make no distinction and do not know if I technically can). Essentially, once a match is made, and the two-player match begins, I want the match to continue until one player wins, or match time (say, 10 minutes) expires.
The thing that I want advice on is how to keep a match going when one player disconnects. (Currently, both players disconnecting simultaneously and prematurely leads to immediate termination of the match and no visible result, so no problem there.) How do I set the iOS client to select an action randomly if the user is disconnected? Do I need this program to run on the server, or on both iOS clients? Is it best done in Objective-C to match the iOS applications, or should it be server-side code in some other networking language, like PHP? Are there any APIs that handle this behavior well? On each turn, much like rock-paper-scissors, players have a one-minute time limit to select an action like attack or defend, and if the timer expires without a selection, the computer program randomly selects an action for the player. I'd like to treat a disconnected player the same way: If he or she isn't connected to the match to supply the choice of action, then the computer will do it randomly for him or her at the end of a minute, as if he or she were connected. My purpose for keeping matches online like this is to (a.) ensure that players that disconnect shortly before losing will be assessed a proper loss, (b.) players that stay online in good faith will be assessed their deserved win, and (c.) players that disconnect accidentally or haphazardly can still salvage their match by returning in a timely fashion. I'm sure lots of online games do this, but I just haven't found the proper API or past example for iOS, or two players connecting on their iPhones. Game Center seems to drop the match, or in turn-based games, there isn't some enforceable turn timer (to my knowledge); I didn't see a way to tell who disconnected in the documentation, and I also didn't see some obvious way to keep something online in the case of failure, but with a timer continuing to run. Everything in peer-to-peer seemed to simulate a client-server relationship (one peer the client, one the server), where if the server unit disconnects, the match must end, and if the disconnected player reconnects, the match cannot resume.
In short, I will not object to a code solution, but even some pointers or advice about a framework that supports this behavior will be very much appreciated. Thank you!
Did you consider turn-based matches with timeouts? If a remote player does not respond (times out) control could return to the previous player, which could then conduct intermediate random moves as and while necessary. The application of timeouts for similar scenarios is e.g. discussed in a WWDC 2013 session video.
If you've also implemented a mode where the user can start a game with a computer player locally, then it would be trivial to take any GameCenter game that's timed out and reload it as a local game against a UI.
My assumption would be that once you've implemented a local game with an AI whereby you're saving turns locally each time a player acts. Further, I'm assuming you would use the same data format for both types of games (perhaps changing a bit that says "computer player" instead of "remote player").
When the GC player times out, you code your game to swap out one type of player for another. Register in GC as a win/timeout, and allow the player to continue with the local/AI player.

Roll your own Multiplayer iPhone setup?

I've been looking into the various options for implementing multiplayer (internet) in iPhone games. Game Center, OpenFeint etc are obviously very good, stable and feature-rich. But I can't help thinking my needs are a lot simpler than what they provide.
If I wanted to put together a simple 2d tank game, could I not just..
setup a mysql server on the web, with some get/set php scripts to actas 'web services'.
when you open the game, a call is made to the server, to log that you're 'available'.
when you select Play, a call is made to retrieve a list of 'available' IP's.
you select an opponent, remote IP is stored locally.
game starts, UDP handles the data between your IP and your opponent's IP.
..or would that never work? apologies, I'm very much a n00b at gaming/networking dev!
thanks in advance!
In addition to the complexities that dtuckernet points out, there are few other things to consider:
You need to think about the cost of running and supporting the web server. Can you maintain the same or better uptime than Apple and OpenFeint?
GameCentre promotes games that support it. The App Store adds an icon, and Game Centre itself lists games that your friends own. Don't underestimate the effect of free advertising!
Even if the code it easy (which I suspect isn't true), why take the risk of developing your own when there is well-tested code available for free?
Incidentally, depending on your requirements, you may want to use a hybrid approach. For example, Game Centre has a maximum of four players per game when Apple hosts it. You can find players using GC and then run the games itself on your own server.
That would certainly work, but implementing this can be a bit harder than it seems initially. You have to handle things like NAT/Firewall traversal and if you are using UDP you need to implement some type of a heartbeat to ensure you know when devices have disconnected (and handle it accordingly).
In most all cases, you would be better off working with a solution that already has these problems solved.

iOS Online Game w/o Server?

I've made an iOS game that works over both Bluetooth and bonjour. It works great both ways, but I'd really like it if players could play with eachother with any Internet connection, be it edge, 3G, or different wi-fi addresses.
The catch: I don't have the resources or knowledge to set up dedicated servers or anything like that. It seems that surely GameKit has something in it that will let me do this, but despite spending dozens of hours over the last week figuring out how to use it, I've found nothing.
So, does anyone have ideas for solutions? My game is just one on one, if that makes it any easier.
Why don't go through the Game Center with GameKit?
Apple provides all the infrastructure to play all around the world via their servers (in 3G or Wifi). With the Game Center you can play up to 4 players (so in your case there is no problem), it's free, you don't have to manage any server, there is a mechanism of invitation between friends, and many more.
The logic of your game stay in the client side, so it'll be same design as your actual game (expect for the API I think).
You can find all information on the official documentation here.
If you want, there is a very tuto on the ray wenderlich's blog (but it uses the cocos2d framework instead of the CocoaTouch).
Yep, try GameCenter with GKMatch - works really well if game doesnt need to send tons of data. If you make additional layer between your app and GKMatch and GKSession, you can use both transparently (so your game supports local AND global multiplayer). Tested, it works

GKPeerStateAvailable for non existing peers?

used this website for the long time to find answers, now need to ask on myself! :)
I have implemented multi player for my game using GKSession and client/server approach, not using peer picker (up to 4 player allowed).
I have one big problem which I can't find solution for.
If you disconnect/reconnect server peer quickly enough, client peer will "remember" that host and will show it in the list of all available peers permanently, only turning wifi/bluetooth off resets this.
So if I do this 5 times in a row, my client will show 5 available hosts with the same name!
Anyone had experience with this? Any idea of workarounds?
I should note that requirements for my game is iPhone SDK 3.0 for Bluetooth and 3.1 for Wifi as GKSession supports both now.
I have a similar problem. In fact I event get peers listed who are not even currently playing the game. Even more so, peers who are not acting as server are listed. I am wondering if there is some behind the scenes caching going on.
I'm thinking about implementing some kind of timer with visual feedback to show the disconnect progress, ideally to prevent this situation. It seems like in general there is a bit of time delay involved with connecting/available and disconnecting/unavailable. It might be better to code the interface to take these time delays into consideration, though I'm not sure exactly how.

Syncing issues using GameKit

I am trying to make a simple two player snake game that uses gameKit to send movement information back and forth. I start the game out with the server sending a command to start the game. Once the game starts a timer fires every .5 seconds that moves the snakes. No matter what I do I end up with the timers on the two iPhones firing at different times. Does anyone have any suggestion or advice for a solid way of synching the information between the devices?
Thanks in advance for your help!
What I finally went with was to have one device do all the number crunching and send the other device information on where its snake (it was a snake game) should be instead of having both devices calculate where there snakes should be and sending that info to the other device. That way led to syncing issues 100% of the time.
A quick search for network game synchronization techniques yielded plenty of results... Each game is unique, I imagine you could adapt some of the techniques from these articles to help:
http://www.mine-control.com/zack/timesync/timesync.html
http://www.softlookup.com/tutorial/games/ch17.asp
http://www.flipcode.com/archives/Network_Game_Programming-Issue_07_I_bent_my_Wookie.shtml
There were even a few IEEE articles if you have access to them.