HI...
A few weeks ago I started to implement my first multiplayer game(Java/Android)
The game is turn-base game similar to tic-tac-toe and players can Chat during the game.
The server always knows who is the current player.
since I need to support chatting no matter who's turn it is, I collect ALL messages from both players.
Chat messages are stored in a dedicated queue and broadcast to players.
Game messages are also stored in a dedicate queue.
Now comes the problematic part:
Lets say one of the player is cheating, and manage to send a "move" message to server even though its not is turn, how can I "recover" from this situation.
I have a solution for it, by start polling messages from game queue. if the message is not belong to current player i ignore it. if the message is belong to current player, I update game and switch turn to next user.
Is this solution is the correct one or there is something more simpler/elegant ???
I added a snapshot of pseudo code:
package ServerGame;
import java.util.Iterator;
import java.util.Queue;
class GameManager extends Thread{
Player currentPlayer;
Queue<Player> players;
Queue<String> chatMessages;
Queue<String> gameMessages;
public void run(){
String message;
while(true){
for(Player player : players){
if(player.isReadAvailable()){ // check if there is a message sent from client
message = player.doRead(); // read message content
router(message); // route the message according to its type: game | chat
}
}
broadcastChatMessages(); // send chat messages to all players
updateGame(); // update game
}
}
private void updateGame(){
for(Iterator<String> iterator = gameMessages.iterator() ; iterator.hasNext() ; ){
String message = iterator.next();
if(currentPlayer.isYours(message)){
// handle message
if(message.type ==”move”){
updateBoard(); // process the move and do a switch turn to opponent
}
switchTurn();
}
else{ // ignore messages that are not belong to current player(avoid cheating)
iterator.remove();
}
}
}
private void broadcastChatMessages(){
for(String message : chatMessages){
for(Player player : players){
player.doWrite(message);
}
}
}
private void router(String message){
if(message == null){
return;
}
if(message.proprietary == “CHAT”){
chatMessages.add(message);
}
else if(message.proprietary == “GAME”){
gameMessages.add(message);
}
else{
// ignore message - maybe log it
}
}
}