MicroNet Documentation
In the last step of the MicroNet Getting Started Tutorials you will add game functionality to build a complete game. The example game is a very simple example game application but gives a good overview over how to develop games with MicroNet. The example game is played in rounds of 10 seconds where each player can submit a guess and gets points based on the proximity to a random number which is guessed by the game application. A score ranking of all players is presented to the player in the TestClient.
The communication flow of a game session with the Simple Example game is shown in the image below.
The game round control communication flow (A,B,C,D in the image):
The player voting communication flow (1,2,3,4 in the image):
The design of the Example Game foresees that a player session is created for every player that connects to the game. The Player Service is responsible to provide an interface to access player sessions. Add the mn-archetype-playerservice to to the game workspace. Just like the API Gateway Service the Player Service needs access to the session store. So make sure that Couchbase is running before you start the PlayerService.
One thing that is left to do for the developer is to decide at what moment the player session is added to the session store. The simplest possibility is to add the session right after a successful login of a User. A place to do that is in the AccountService
right before returning the login response after a successful login. In the onLogin
method in the AccountService
main class add the required code as indicated below to send a message to the mn://player/add queue which is observed by the Player Service. The following code snipplet shows how the onLogin
method looks like after the addition.
@MessageListener(uri = "/login", desc="Attempt to log in a User.")
@RequestPayload(CredentialValues.class)
@ResponsePayload(value=Integer.class, desc="UserID of the User that logged in")
public Response onLogin(Context context, Request request) {
CredentialValues credentials = Serialization.deserialize(request.getData(), CredentialValues.class);
UserValues user = database.getUser(credentials.getUsername());
if (user == null)
return new Response(StatusCode.NOT_FOUND);
if (!credentials.getPassword().equals(user.getCredentials().getPassword()))
return new Response(StatusCode.UNAUTHORIZED);
//>>>>>>>>>>>>>>>>>>>>> ADD THIS >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Request addPlayerRequest = new Request(credentials.getUsername());
addPlayerRequest.getParameters().set(ParameterCode.USER_ID, user.getId());
context.sendRequest("mn://player/add", addPlayerRequest);
//>>>>>>>>>>>>>>>>>>>>> UNTIL HERE >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
return new Response(StatusCode.OK, Integer.toString(user.getId()));
}
Eclipse helps you to add the missing import for the ParameterCode class. Add the import that Eclipse is proposing.
Add the mn-archetype-voteservice and mn-archetype-roundservice to the game workspace. Start both services like any other service. Both services have no additional requirement and are good to go. With these two services the Simple Example Game is complete.
The Round Service is a little bit special since it has the requirement to be instantiated only once because the Round Control Events may only be broadcasted once. MicroNet does not yet provide a way to limit the number of service instances so the developer is responsible to ensure this.
To test the game you just developed start the TestClient like you did before in the UserManagement tutorial. After a successful login you will periodically receive new Round updates and get the chance to place a vote. You can also log in with multiple client simultaneously and play against yourself by starting multiple TestClients.
Note that since the TestClient does not initially download the scoreboard after login you have to wait for 0-10 seconds for the game to react. The game might appear frozen for a few seconds. This will be fixed in the future.
You just completed your fist simple MicroNet game but there is much more you can explore. Refer to the QuickReference page to read more about more advanced MicroNet functionality and concepts.
Also be sure to fill out the MicroNet Evaluation Survey.