P2G is source code (server-side only) of the game project developed by No Game No Life team of SE1106 class. This game is group project assignment of SWE102 course
P2G using Express.js and Socket.io to write the game server. The server communicate with the client by Socket.io through web socket to make it's easier to build multi-platform game with the source will not be changed too much
P2G require Node.js v7+ and npm v5+ to run
Install the dependencies and start the server.
$ cd P2G
$ npm install
$ npm start
For Ubuntu/Linux and OSX
$ cd P2G
$ npm install
$ export NODE_ENV=production
$ npm start
For Windows (using PowerShell)
$ cd P2G
$ npm install
$ $env:NODE_ENV="production"
$ npm start
after finished the installation, go to the browser and go to the following address
localhost:3000
First, connect to the Socket.io server
let io = ('/warfare')
socket.emit('find_game', data)
data
(Object)name
(String): player's ingame name
used to find game
socket.emit('leave_room')
used to leave room while ingame or while in waiting for other players, automatically called when disconnected
socket.emit('get_info')
used to get player information (id
, hp
, action
,..) by trigger player
event
socket.on('action_on', (data))
data
(Array, 3 elements): array to store player current prompted movei-th element
(Object)type
(Number): type of move (0
: Defense,1
: Attack)target
(String): the id of the socket that move target to
used to prompted the moves in the current phase ingame
socket.emit('action_off')
clear all move that player prompted in the current phase ingame
socket.emit('give', data)
data
(Object)id
(String): target player's id to giveammount
(Number): ammount of hp to send
player send a ammount of hp to targeted player, hp for sending must be positive integer and <= 2
socket.emit('chat', data)
data
(Object)id
(String) target player's id to spy, this function will cause server to triggerspy_result
event on client
socket.emit('chat', data)
data
(Object)target
(String): target's id to send message,null
to send to all players in roommessage
(String): message to send
send message to other player or send to all players in room, thís function will trigger message
event on client
socket.emit('debug_obs')
observe all information of the room which have that player's playing by trigger socket debug_obs_result
event
socket.on('time', callback(time))
time
(Number)
the current time when game started, emitted by server
socket.on('counter', callback(time))
time
(Number)
the current count down time in current phase when game started, emitted by server
socket.on('current_phase', callback(phase))
phase
(Number)
the current phase, emitted by server
socket.on('players', callback(players))
players
(Array)
array of socket id of players in room
socket.on('player', callback(player))
player
(Object): All information about playerid
(String): player's id, same with socket idname
(String): player's ingame nameroom
(String): player's room idhp
(Number): player's current HPalias
(Number): player's alias in room (since room have maximum 4 players, alias ranged from 0 to 3)action
(Array, fixed length, 3 elements): store player's action in current phase ingame,null
if player don't prompt that movetype
(Number): type of move (0
: Defense,1
: Attack)target
(String): target player's id
prevData
(Object): data of the previous phase of playerattackBy
(Array)attackTo
(Array)defenseBy
(Array)defenseTo
(Array)
player all information, triggered by get_info
event
socket.on('update')
used by server to force player to refresh data
socket.on('leave_game')
used by server to force player to leave room
socket.on('game_ended')
used by server to notify player that game's ended, also mean player is the winner
socket.on('message', callback(data))
data
(Object)type
(Number): message type (1
: private,0
: public)source
(Object)id
(String): player sent message idname
(String): player sent message namealias
(Number): player sent message alias
message
(String): received message
received message
socket.on('spy_result', callback(data))
data
(Object): store target player information in the previous phase ingamehp
(Number): HPaction
(Array)type
(Number): action type (0
: Defense,1
: Attack)target
(String): target player's id
result of spy
event, triggered by server
socket.on('debug_message', callback(message))
debug message sent by server message sent by server used to debug
socket.on('debug_obs_result', callback(room))
room
(Object)gameName
(String): game name hosted in roomid
(String): room's idplayers
(Array): array of players in the current phase)prevPlrs
(Array): array of players in the previous phase)lock
(Boolean): used to lock object to evade exceptiondata
(Object)playing
(Boolean): playing state of roomstarted
(Number): timestamp when game startedphase
(Object): data of current phase ingamespyStates
(Number): state ofspy
used by player, stored using player's alias and bit operatorsactionStates
(Number): state of promptedaction
used by player, stored using player's alias and bit operatorsgiveStates
(Number): state ofgive
used by player, stored using player's alias and bit operatorsstarted
(Number): timestamp when phase started
result sent by server, triggered by debug_obs
event
socket.on('err', callback(message))
error message sent by server
tuannhse04791