I'm thinking about programming a multiplayer game. For communication, I will use a Server <-> Client design like the following:
Client <-> Server <-> Client
The Game will be a kind of Tower Defense. Where two players can fight each other by sending soldiers to the enemy to destroy their base or build towers to defend.
To keep the games in sync, each action must be sent to the other player.
e.g. Player 1 set a tower on position x, y so Player 2 must know about this tower (It would be pretty unfair if the enemy has invisible towers)
To sync the two games I can think of two methods:
First method:
The client(Player 1) set a tower to position x, y and send a message to server: "I have placed a tower on position x, y". The server sends this message forward to the other client(Player 2)
//client 1
buildTowerOnPosition(x, y);
sendToServer(BuildTowerMessage(x, y))
//server
receiveMessageFromClient(BuildTowerMessage(x, y))
sendFurtherToOtherClient(BuildTowerMessage(x, y))
//client 2
receiveMessageFromServer(BuildTowerMessage(x, y))
buildEnemyTowerOnPosition(x, y)
By this method:
The server doesn't need to know the current game status and serves only as a bridge between the clients
Second Method:
The client(Player 1) send a message to server: "I will build a tower on position x, y" the server build a tower to position x, y and send the changes(New tower on position x, y) to the clients
//client 1
sendToServer(BuildTowerMessage(x, y))
//server
receiveMessageFromClient(BuildTowerMessage(x, y))
buildTowerOnPosition(x, y)
sendFurtherToBothClients(BuildTowerMessage(x, y))
//both clients
receiveMessageFromServer(BuildTowerMessage(x, y))
buildTowerOnPosition(x, y)
By this method: The server needs to know the current game status
My thoughts are:
By the first Method
The server only needs to forward the message and the main calculation is on the client side which relieves the server.
By the second Method:
The server can control the actions of the clients. For example: When both players set a tower on the same position at the same time, the first tower will be set and the second cannot be built anymore.
In the first Method the second tower will overbuild the first or in the worst case, both clients have different towers.
Now to my question:
Which method should I use for my game?
Is there more pro and contra by the methods?
Are there other methods?
Are there kinds of games where I should prefer another method than in my game?
Thank you for your advice