Skip to content

snowyukischnee/P2G

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

P2G

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

Technologies used

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

QuickStart

P2G require Node.js v7+ and npm v5+ to run

Development mode

Install the dependencies and start the server.

$ cd P2G
$ npm install
$ npm start

Production mode

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

Documentation (for client-side)

First, connect to the Socket.io server

let io = ('/warfare')

Emit

find_game

socket.emit('find_game', data)
  • data(Object)
    • name(String): player's ingame name

used to find game

leave_room

socket.emit('leave_room')

used to leave room while ingame or while in waiting for other players, automatically called when disconnected

get_info

socket.emit('get_info')

used to get player information (id, hp, action,..) by trigger player event

action_on

socket.on('action_on', (data))
  • data(Array, 3 elements): array to store player current prompted move
    • i-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

action_off

socket.emit('action_off')

clear all move that player prompted in the current phase ingame

give

socket.emit('give', data)
  • data(Object)
    • id(String): target player's id to give
    • ammount(Number): ammount of hp to send

player send a ammount of hp to targeted player, hp for sending must be positive integer and <= 2

spy

socket.emit('chat', data)
  • data(Object)
    • id(String) target player's id to spy, this function will cause server to trigger spy_result event on client

chat

socket.emit('chat', data)
  • data(Object)
    • target(String): target's id to send message, null to send to all players in room
    • message(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

debug_obs (DEBUG_ONLY)(WILL BE REMOVED IN GAME)

socket.emit('debug_obs')

observe all information of the room which have that player's playing by trigger socket debug_obs_result event

Listener

time

socket.on('time', callback(time))
  • time(Number)

the current time when game started, emitted by server

counter

socket.on('counter', callback(time))
  • time(Number)

the current count down time in current phase when game started, emitted by server

current_phase

socket.on('current_phase', callback(phase))
  • phase(Number)

the current phase, emitted by server

players

socket.on('players', callback(players))
  • players(Array)

array of socket id of players in room

player

socket.on('player', callback(player))
  • player(Object): All information about player
    • id(String): player's id, same with socket id
    • name(String): player's ingame name
    • room(String): player's room id
    • hp(Number): player's current HP
    • alias(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 move
      • type(Number): type of move (0: Defense, 1: Attack)
      • target(String): target player's id
    • prevData(Object): data of the previous phase of player
      • attackBy(Array)
      • attackTo(Array)
      • defenseBy(Array)
      • defenseTo(Array)

player all information, triggered by get_info event

update

socket.on('update')

used by server to force player to refresh data

leave_game

socket.on('leave_game')

used by server to force player to leave room

game_ended

socket.on('game_ended')

used by server to notify player that game's ended, also mean player is the winner

message

socket.on('message', callback(data))
  • data(Object)
    • type(Number): message type (1: private, 0: public)
    • source(Object)
      • id(String): player sent message id
      • name(String): player sent message name
      • alias(Number): player sent message alias
    • message(String): received message

received message

spy_result

socket.on('spy_result', callback(data))
  • data(Object): store target player information in the previous phase ingame
    • hp(Number): HP
    • action(Array)
      • type(Number): action type (0: Defense, 1: Attack)
      • target(String): target player's id

result of spy event, triggered by server

debug_message(DEBUG_ONLY)

socket.on('debug_message', callback(message))

debug message sent by server message sent by server used to debug

debug_obs_result(DEBUG_ONLY)(WILL BE REMOVED IN GAME)

socket.on('debug_obs_result', callback(room))
  • room(Object)
    • gameName(String): game name hosted in room
    • id(String): room's id
    • players(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 exception
    • data(Object)
      • playing(Boolean): playing state of room
      • started(Number): timestamp when game started
      • phase(Object): data of current phase ingame
        • spyStates(Number): state of spy used by player, stored using player's alias and bit operators
        • actionStates(Number): state of prompted action used by player, stored using player's alias and bit operators
        • giveStates(Number): state of give used by player, stored using player's alias and bit operators
        • started(Number): timestamp when phase started

result sent by server, triggered by debug_obs event

err(DEBUG_ONLY)

socket.on('err', callback(message))

error message sent by server

For further usage of the functions, see index.ejs

Author

tuannhse04791