A simple client server based game where we follow instructions provided by server. We must respond with the appropriate instruction (key press) before the time runs out!
- When a
key is pressed
in server (instruction), the client receives it; and has a timer. counting up toX seconds
in which the same key has to be pressed. - The server verifies if the keypress sent by client matched “the instruction” and assign +1
point on correct,
-1 on a wrong key
and0 for timeouts
. - The game is over when the score reaches either
+10 points
or-3 points
. - The game is also over if the client does not respond for
3 continuous instructions
. - Each score update should send the score back to the connected client.
- The client should also know the timeout value for each instruction received.
Note: Right now only keyboard keys [a-z] and [0-9] are supported.
$PROJECT_ROOT
decorators
(decorator helpers)dist
(generated after you run build or run commands)models
(classes and models).env.example
(sample env file to generate .env)game.ts
(main entry file)
Clone the git code:
git clone https://github.com/yogesh8177/follow-me-game.git
cd follow-me-game
npm install -g typescript
(to make tsc command work)touch .env
(create .env file as given below)
$PROJECT_ROOT=<path/to/follow-me-game> will be used as a refrence to root folder in this doc.
Create .env
file from the given $PROJECT_ROOT/.env.example
file. Below is a sample .env
file structure.
## Server config
SERVER_PORT=3001
MAX_SCORE=10
MIN_SCORE=-3
TIMEOUT_IN_SECONDS=5
MAX_TIMEOUT_MISSES=3
# Client config
CLIENT_PORT=3000
SERVER_URL=ws://localhost:3001
Assuming you are already inside $PROJECT_ROOT
.
npm install
- Server:
npm run start:server
- Client:
npm run start:client
Firsly start the server, once server boots up, then start the client. Presently only one client will be able to connect. Any new connections to server will be overriden and reset the game.
Note: Make sure to run client and server in separate terminal windows/tabs.
-
Instead of setting
TIMEOUT_IN_SECONDS
for each instruction in .env file, randomly assign that value. Provision is made to passTIMEOUT_IN_SECONDS
as a dynamic value in theInstruction
model. A small function or utility to make it dynamic will help! -
One to many
mapping betweenserver:clients
. -
Handle socket
disconnects
anderror
events on server and client respectively! -
Implement connection
retry
mechanism! -
Improve the UX for client and server!
-
Edge cases like where a client receives an already
expired instruction
. We do havetimeStamp
value for each instruction, we may validate usingtimeStamp
on the client to see if received instruction is already expired or not. All other edge cases similar to these needs to be handled.