Chapter 4. MMO Word Game

Word Chain Game is a real-time, massive multiplayer online game. Each player will be able to see the other online players when playing the game, along with a score leaderboard for score. In this chapter we will introduce the Promise pattern and explain how Promises simplify asynchronous operations. You will learn how to build a real-time application with Express and SocketIO, perform authentication over socket handshaking, and deal with race conditions using the atomic update of MongoDB. You will also learn how to build the game client to connect to the game server over socket, and how to debug WebSocket on the client side using Chrome Developer Tools.

Once you have mastered this, you can build similar games such as online quiz competitions.

Gameplay

The game starts with a randomly selected English word and each player tries to submit a word where the first character of their submission matches the last character of the current word; we call this chaining with the current word. For example, if the game starts with the word Today, then players can send words such as Yes or Yellow.

The first person to submit a valid word will have their word become the starting word for the next round and gets the points for that round. Once the new word is accepted, the server will broadcast the change to all online players. The players will see the new word and submit another word to chain with it.

For example, if player 1 sends Yes to chain with Today, the server will accept the word and broadcast the current word Yes to all other players. If a player submits a word that is invalid based on the dictionary we have or was submitted by another player earlier, the game server will ignore that request. If multiple players submit valid words simultaneously, the server will only accept the first submitted word.

Gameplay

Real-time application overview

In this game, we will introduce the Promise pattern and explain how Promise will simplify async operations.

Despite this being a real-time game, we will not rush into implementing a real-time feature at the beginning. Instead, we will first build a game model, which contains all the game logic.

In the game logic, we first introduce how to keep track of active users and then explain how we verify users' input. After verifying the input, during the updating game state phase, we deal with race conditions by utilizing the atomic operation of MongoDB. We also look into how to cover the race conditions with test cases.

After the game logic is done, we will introduce how to broadcast game state changes to all players using Socket.IO.

In the end, we will create an Express app, a Socket.IO server, and a game client that can talk to our server using the socket.io-client libraries.

Keeping track of active users

Since the game is a multiplayer game, players can see the number of players and their usernames. To keep track of active users, we need to track when a player joins the game and when a player leaves the game.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.239.118