Simulate a betting game where users can play or observe a game.
To start game run "docker-compose up" in main folder
visit localhost:3000 in your browser
How to start a game:
- open localhost:3000
- write a name and change the switch depending if you want to be observer or player.
- If switch is on player select two guess values.
- Click join.
- When game starts the left scoreboard will show live result after each round.
- When game ends a text will show below the scoreboards with last winner and their score.
Services run on nginx proxy to avoid cors issues.
Backend Packages:
- scoring (Contains a function that takes in the value from the game and the users guess and return a score)
- scoreboard (A map structure that keeps track of scores and who won)
- player (Struct with name,websocket and guess) A player is considered a observer if no guess
- gameservice (binds all the stuff necessary to play a game together)
- game (Runs for a given amount of round , returns a randomized value in the interval every round through a channel. Randomized on given seed value)
- config (Reads configuration for game from environment variables or default value)
- api (Contain the necessity to setup the endpoints for the api )
The game is configurable at start using environments variables
The variables are:
- Rounds int ( Set the number of rounds on each game)
- Sleep time.Duration('example: 1s') ( Set the sleep time between each new draw in a game)
- SleepBetween time.Duration('example: 1s') ( Set the sleep time between each game)
- MinimumPlayer int (Set the minimum number of player that has to join before a game can be started)
- BeginInterval int (Set the lowest number that a user can guess on in the game)
- EndInterval int (Set the highest number that a user can guess on in the game)
Testing:
The backend test can be run with go test ./...
The frontend components styling is tested using storybook, run command npm run storybook in the web folder.
API endpoint: "/config" GET: Returns the game configuration "/ws" Websocket: Returns the game result,winner and total scores.
messsages :
{type: "winner,payload: {...}
-
Two player with the same name could potentially join the same game and there scores would be joined on the scoreboard. it is unnecessary an observer have to input a name before being able to view games.
-
The way of handling if a user has reached score 21 is a bit embarrassing and hard to grasp.
-
The game(name and guess) could be sent as a message through the websocket rather than at the setup of the game. That way the websocket wouldn't need to be close to run a new game with new guess.