![]() |
|
|
Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
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
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
This is only a theoretically mechanism. Point me out if I make any mistake . Thank you in advance! |
|
#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.
|
|
#3
|
||||
|
||||
|
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 |
|
#4
|
|||
|
|||
|
Apple API in this case is a good point, I'm learning to use it. But it's not cross-platform
|
|
#5
|
||||
|
||||
|
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. |
|
#6
|
|||
|
|||
|
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 |
|
#7
|
|||
|
|||
|
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
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|