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!
No comments:
Post a Comment