26

Never-Blink: Blink and Lose. An Online Web Game

 4 years ago
source link: https://www.tuicool.com/articles/fYre22j
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Never-Blink

FRrqQzM.png!web

1. :tada: Introduction

Randomly connect to a player around the world and challenge him/her. Remember not to blink!

j6VVfyA.gif

2. :scroll: Implementation

1. Project structure

.
├── backend
│   ├── __init__.py
│   ├── detect.py
│   └── shape_predictor_68_face_landmarks.dat (detect model)
├── package.json
├── requirements.txt
├── server.py
├── src
│   ├── app.jsx
│   ├── index.js
│   ├── play.jsx
│   └── start.jsx
├── static
│   └── bundle.js
├── templates
│   └── index.html
└── webpack.config.js

We use react as our frontend framework, and use peerjs to communicate between clients. At backend, we use flask to host the server. For connection between clients and server, we use socketio to implement real-time data transfer.

ne2IRjF.png!web

2. Game State

We use the notion of Finite State Machine to design our game state. The circle is the state. The text in the top of each box is the event causing state transition, and the text in the bottom of each box is the action taken when event occurs. The logic looks quite simple here, but it is not that easy to implement. For more detail you can check play.jsx or server.py .

Izqa6vZ.png!web

3. :computer: Development

I spent lots of time formatting my code and writing comments to make it as readable as possible. Hope you enjoy :blush:

  1. Clone this repo.
git clone [email protected]:andyh0913/blink_contest.git
  1. Install the packages
# Frontend
npm install
# Backend
pip install -r requirement.txt
  1. Build and Run
# Build frontend files
npm run build
# Run server
npm run start

Every time you changed your code, you should run the above command again. We do not support hot module replacement now.

  1. Start and Enjoy

Goto https://0.0.0.0:3000

0.0.0.0:3000 does not work, remember to add https .

4. :construction: Problem

  1. Becausee the detector is too slow and weak, we can not endure too many clients. Therefore, we do not deploy it to a public server now. We plan to train a robust and fast model in the future.
  2. When restarting, the old media connection listener will be called again. So we remove restart temporarily.

5. :book: Reference

  1. Environment setup: https://github.com/rwieruch/minimal-react-webpack-babel-setup
  2. Simple P2P WebRTC package: https://github.com/peers/peerjs
  3. Socket-io: https://socket.io/docs/client-api/
  4. Flask-socketio: https://flask-socketio.readthedocs.io/en/latest/
  5. Blink detector: https://github.com/dilawar/eye-blink-detector
  6. Material UI: https://material-ui.com/

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK