Game Server Source code overview
Tutorial Source code :
Remember the opening of this tutorial i mentioned that one of MMO principles is that the game
is played in the server and not in the client most of the times. to avoid cheating on the client and abusing the game .
In this class the data that is send from the client include events to invoke.
Those events are like "Protocol" between the client and the server both sides should understand them
in our case they are :
- Line 2 : when Websocket protocol is confirmed.
- Line 3: Login request form the client .
- Line 4: Login is handled , response to client that login is done
- Line 5: New User is joined the room
- Line 6: Player is done its turn and played.
- Line 7 :Response to all other player and the current player that the turn is done
This class will handle the game logic here is where the server "Plays" the game
- Lines 18 -24: In this simple card game we have 2 main events first is when player login to the game .
And the second is when player plays it turn .
This is where the first case get handled.the client sent the server LOGIN event .
And the Server do several things:
A. Creates new Player object
B. Updates the Player properties
C. Set the Player object into the Players Container ( in GameManger object ) .
D. Associate unique id to the player.
- Lines 25 -29: The second event as mentioned in the previous section,is when the player makes
its turn . the client will send the PLAY event .
when this event triggered in the server , the server will play the game and update the game states for each other players in the game.
- Lines 33 - 50: This function will dispatch the response according to the Event that is currently
get handled by the server.
- Lines 56 -59: The invokePlayEvent is big function , it handles the game logic and updates The other players (in the server ) with the new game states .
First we need to know that the player which we handling is the one that playing now its turn.
in the JSON structure that is passing between the client and the server
and also on of the Player Member called : activeplayerid .This member will hold the id of the Player that is currently playing .
- Lines 63 -66: Get the previous player. this is turn based game so we need to check what did the previous player done , and to compare between the current Player status and the previous Player Status.
- Lines 75 - 92: In our game there could be 2 states in the game:
First when the middle card deck is empty and one of the players should play its turn
Second is when there is already a card on the middle deck so the player now should:
A. Play its card.
B. Compare between this current player card and the previous player card .
Here we check if the previous card is greater then current player card .
Then the following actions are made in the server :
A.Update the winner player ( previous player ) with the winning cards.
B.Update the current player about the winner player.
C.Clear the middle card deck.
- Lines 96 -116: Check if the current player is the winner by comparing the cards values
and doing the same as we did in section 6 above . but this time set current player as winner.
- Lines 120 - 145: Check if the cards value are the same , in this case the War Card game rule
is that we take 4 cards from each player (lines: 123 -124). and the last (lines 126 -127 ) card
is the card that we are going to check .
Again like in sections 6. and 7. the cards values is compared to determine who is the winner .
- Lines 146 -150: to keep this code simple as possible the game support only 1 level of WAR state . so if there is again WAR between the cards there is a need to implement the WAR handling again . maybe to separate it to sub-function and call it again .
i leave it to you as exercise ...
- Lines 156 - 162: This is the case where the previous player didn't played , so the current player
is the one that play first. that means putting the card on the middle card deck.
A.Update the middle card deck with current player card.
B.Remove the played card from the player card stack.
C.Update the other player with the card which played.
- Lines 168 -173: To determine who is the winner in the game the logic is to check if the previous players card stack is empty , if its empty so the current player is the winner .
and the game Ended.
of course we need to update the players and the server that the game is ended .
by setting the player member : endgame with the winning player id .
Now we are ready to learn how the response to clients is dispatched . in the next post:
Multiplayer Card Game using WebSockets,Java Netty Server ,Cocos2d-x-HTML5 - Part 5