★ TouchArcade needs your help. Click here to support us on Patreon.

Idea for online multiplayer turn-based game with UDP

03-02-2013, 08:25 AM
#1
Joined: Jun 2012
Location: SaiGon
Posts: 12
Send a message via MSN to ricky.ngk Send a message via Skype™ to ricky.ngk
Idea for online multiplayer turn-based game with UDP

Dear,

Online multiplayer turn-based game mentioned in this case is like chess, caro (gomoku), Othello online game. Each player makes a move in his turn, submit to host, and wait for other players turn.

Desired criteria:
+ Target: mobile
+ Simple, design for max 4 players per game
+ Keep server in low cost (CPU, bandwidth, transfer rate)
+ Available auto-reconnect if any

So I decide to use UDP. Server works like client discovery and message delivery at all. There’re two problems need to solve:
+ Package transmission mechanism: use UDP punch hole http://www.brynosaurus.com/pub/net/p2pnat/
+ Turn-based mechanism

For turn-based controlling, I have an idea as following:
+ Suppose that a host places two roles: host and client too
+ Client works in 3 states: LOCK (not in turn), INPUT (in turn), and SYNC (submit data to server), with two important properties: seq (sequence index), and turnIndex (turn index of each client)

Host work flow:
Code:
seqNo = 0
userInTurn = 0
while (true)
    if receive_submit() then
        seqNo = seqNo  + 1
        userInTurn = get_next_turn()
    end
    update_game_logic()
    deliver_to_all_clients(seqNo, userInTurn, gamedata)
end
Client work flow:
Code:
seqNo = -1
function On_Receive_Data_Update(ServerSeqNo, currentTurnIndex, gamedata)
    if ServerSeqNo >= seqNo then -- valid seq
        if ServerSeqNo > seqNo -- change state
            if state is LOCK then
                if currentTurnIndex == turnIndex then
                    switch_to_state(INPUT)
                end
            elseif state is INPUT then
                if currentTurnIndex != turnIndex then
                    switch_to_state(LOCK)
                end
           else
                if currentTurnIndex == turnIndex then
                    switch_to_state(INPUT)
                else
                    switch_to_state(LOCK)
                end
           end
        end
 
        update_game_with_data(gamedata)
    end
end
 
function update_client_control()
    if state is INPUT then
       update_game_input()
       if hasEndTurn() then submit() end
    end
end
Please see full post with figure at http://www.guava7.com/2013/online-mu...game-with-udp/

This is only a theoretically mechanism. Point me out if I make any mistake . Thank you in advance!

Enjoy our games at: http://www.guava7.com
03-03-2013, 07:10 AM
#2
Apple's GameCenter has support for turn based games out of the box. This may save you a lot of effort. In any case, I don't see the point of using UDP here but I don't think anybody here will go to a technical level explaining stuff.

03-04-2013, 12:16 AM
#3
Joined: Oct 2011
Location: Melbourne
Posts: 55
KISS keep it simple you know what

Apple's turn based API also means you don't have to provide servers.

UDP might look inviting for this but by the time you finish your implementation you will have created a connection based protocol. So in the long run it's more work than any other method.

Matthew

My iOS games: JiggleSaw
My Blog: JiggleSaw development
03-17-2013, 12:54 PM
#4
Joined: Jun 2012
Location: SaiGon
Posts: 12
Send a message via MSN to ricky.ngk Send a message via Skype™ to ricky.ngk
Apple API in this case is a good point, I'm learning to use it. But it's not cross-platform

Enjoy our games at: http://www.guava7.com
03-17-2013, 04:34 PM
#5
Joined: Dec 2010
Posts: 240
If you want a cross-platform solution:

You can loose packets in UDP.
You can ask why all game use UDP intead of TCP ? TCP is a lot more reliable than UDP.

UDP has better performance, put lack of reliability like TCP.
In video game they just do two types of UDP connexion:
- Simple UDP where information can be lost (not sensitive information for the player like statistics or are re-send often like his position )
- Enchanced UDP where they build a mecanism to increase reliability on top of it.
(they try to have almost the same reliability than TCP but with better performance)

Doing your own enchanced UDP will take you several weeks.
For a turn by turn game you should choose probably TCP. It's not an FPS and you don't need high performance. It will be a lot simpler to do, and reliable for your players with bad connections.

On games (xbox360/PC/PS3) I've worked on, I've used Raknet to do everything low level related (punch through, udp reliable etc..). I don't know how much it can cost.

Another advice:
You can also use a master server to create game and help a client to connect to each other, but after that you could use one of the client as a server.
Like that you can scale much more. Most recent game (even AAA FPS) do this.
But it's more complicate, and you can implement it later.
03-17-2013, 11:46 PM
#6
Joined: Nov 2008
Location: Baltimore
Posts: 204
You could probably just use enet

Well proven game networking lib, runs on all platforms.
Does TCP and UDP + data protocol on top...

http://enet.bespin.org
03-18-2013, 12:06 AM
#7
Joined: Jun 2012
Location: SaiGon
Posts: 12
Send a message via MSN to ricky.ngk Send a message via Skype™ to ricky.ngk
Thank @Hercule, a lot of things to do with UDP, I'll take account, and do a testbed for some scenarios, maybe UDP for discovery & TCP for p2p connection in game
@TheBunny: thank for share, I' cloning https://github.com/lsalzman/enet , hope I could understand source code quickly

Enjoy our games at: http://www.guava7.com