Coder Social home page Coder Social logo

battleship.io's Introduction

Battleship.io

Battleship game host powered by socket.io and node.js

git clone and start local server

$ git clone https://github.com/nightspirit/battleship.io.git
$ cd battleship.io
$ npm install
$ npm start

By default, it will create a game server on http://localhost and listen to port 80

Example

CLI example

GUI in ReactJS

GUI in Backbone

How to use on client side

include lib in your html

<script src="https://cdn.socket.io/socket.io-1.4.5.js"></script>

for your javascript app

var socket = io.connect();
// Establish an io socket instance which connect to localhost
// or io.connect("battleship.io.pofolio.cc"); which is my public server.

// subscribe 'connected' event
socket.on('connected',function(data){
	// join your own room by emit the 'join_room' and your specific room name
	socket.emit('join_room','YOUR ROOM NAME');
});

// subscribe to different events to make your client works
socket.on('room_joined',function(){...});
socket.on('game_state',function(){...});
socket.on('game_joined',function(){...});
socket.on('room_joined',function(){...});
socket.on('deployed',function(){...});
socket.on('fired',function(){...});
socket.on('engage',function(){...});

Game flow

  1. Client A emit "start_game"
  2. Client A listen to "game_joined" for response
  3. Server broadcast "game_state" as "INIT" if game started successfully
  4. Client B emit "join_game"
  5. Client B listen to "game_joined" for response
  6. Server broadcast "game_state" as "DEPLOY" if player joined successfully
  7. Both clients try to deploy their fleet by emit 'deploy' and fleet location object
  8. Listen to "deployed" for response
  9. Server broadcast 'game_state' as "ENGAGE" if both players deployed successfully
  10. Both clients emit "fire" and coordinates for engaging alternately
  11. Listen to "fired" for response
  12. Server broadcast "engage" as engaging result each turn
  13. If any client's fleet all sunk, server broadcast "game_state" as "END"

Game actions

'start_game'

socket.emit('start_game'); // start game

'join_game'

socket.emit('join_game'); // join existing game

'deploy'

socket.emit('deploy',{
	carrier: "a0 a1 a2 a3 a4", // size 5
	battleship: "b0 b1 b2 b3", // size 4
	submarine: "c0 c1 c2 c3", // size 4
	cruiser: "d0 d1 d2", // size 3
	patrolboat: "e0 e1" // size 2
});

Coordinates format should be like [a-j0-9] for example "f1". For each type of ship it should be space or comma separated string.

Although there has server side validation, for better UX, client side should do validation before sending data.

'fire'

socket.emit('fire',"c2");

Game Events

'game_joined'

socket.on('game_joined',function(res){
	// res:
	// OK - success
	// ERR_NO_GAME - there is no game in room
	// ERR_ALREADY_START - game already started
    // ERR_FULL - players full (when join_game)
	// ERR_ALREADY_JOINED - you have already joined
}

'game_state'

socket.on('game_state',function(res){
	// res.state
	// INIT - initial state
	// DEPLOY - deploy state
	// ENGAGE - engage state
	// END - game over

	// Additional data
	// when DEPLOY
	// res.players = [{id:<socket id>,ready:<bool>},{...}]
	// when ENGAGE
	// res.first = <id> is first strike player socket id
	// when END and game has result
	// res.winner = <id> is winner socket id
	// res.loser = <id> is loser socket id
});

'deployed'

socket.on('deployed',function(res){
	// res
	// OK - success
	// ERR_NO_GAME - no ongoing game in room
	// ERR_STATE - not in DEPLOY state
	// ERR_PLAYER - not the participant
	// ERR_ALREADY - already deployed
	// ERR_TYPE - ship key is not correct
	// ERR_FORMAT - coordinates are not correct
	// ERR_OVERLAP - ship overlapping
});

'fired'

socket.on('fired',function(res){
	// ERR_NO_GAME- no ongoing game in room
	// ERR_STATE - not in ENGAGE state
	// ERR_PLAYER- not your turn
	// ERR_TARGET - coordinates are not correct
});

'engage'

socket.on('engage',function(res){
/*
	res:
	{
		offence:<id>,
		defence:<id>,
		target:<coordinates>,
		result:<HIT|MISS>
		sunk:<ship.type> // if any ship sunk, otherwise empty string
	}
*/
});

Room actions / events

###"rooms"

socket.emit('rooms');
socket.on('rooms',function(data){
	// current live room ids
});

'join_room' / 'room_joined'

socket.emit('join_room',"<room name>");
socket.on('room_joined',function(data){
	// data = {name:<room name>,game_state:<current game state>}
	// game_state : ""||"INIT"||"DEPLOY"||"ENGAGE"||"END"
});

'in_room'

socket.emit('in_room');
socket.on('in_room',function(data){
	// all in room user ids
});

License

MIT

battleship.io's People

Contributors

nightspirit avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

battleship.io's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.