1
0
mirror of https://github.com/exane/not-gwent-online synced 2025-11-08 09:08:40 +00:00

replace socket.io with socketcluster

This commit is contained in:
exane
2015-06-15 21:03:12 +02:00
parent 6c709b855c
commit db59735858
14 changed files with 263 additions and 94 deletions

View File

@@ -2,12 +2,12 @@ var Battleside = require("./Battleside");
var PubSub = require("pubsub-js");
var Card = require("./Card");
var io = global.io;
/*var io = global.io;*/
var Battle = (function(){
var Battle = function(id, p1, p2){
var Battle = function(id, p1, p2, socket){
if(!(this instanceof Battle)){
return (new Battle(id, p1, p2));
return (new Battle(id, p1, p2, socket));
}
/**
* constructor here
@@ -15,6 +15,8 @@ var Battle = (function(){
this._id = id;
this._user1 = p1;
this._user2 = p2;
this.socket = socket;
this.channel = {};
};
var r = Battle.prototype;
/**
@@ -29,21 +31,28 @@ var Battle = (function(){
r._user2 = null;
r.turn = 0;
r.socket = null;
r.channel = null;
r._id = null;
r.init = function(){
PubSub.subscribe("update", this.update.bind(this));
this.channel = this.socket.subscribe(this._id);
this.p1 = Battleside(this._user1.getName(), 0, this, this._user1);
this.p2 = Battleside(this._user2.getName(), 1, this, this._user2);
this.p1.foe = this.p2;
this.p2.foe = this.p1;
this.p1.setUpWeatherFieldWith(this.p2);
this.start();
}
r.start = function(){
this.p1.setLeadercard();
this.p2.setLeadercard();
@@ -81,7 +90,7 @@ var Battle = (function(){
var side = this.turn++ % 2 ? this.p1 : this.p2;
if(side.isPassing()){
if(__flag) {
if(__flag){
return this.startNextRound();
}
return this.switchTurn(1);
@@ -93,7 +102,7 @@ var Battle = (function(){
}
r.startNextRound = function() {
r.startNextRound = function(){
}
@@ -119,9 +128,23 @@ var Battle = (function(){
}
r.send = function(event, data){
io.to(this._id).emit(event, data);
this.channel.publish({
event: event,
data: data
});
}
/*r._setUpChannel = function() {
var self = this;
this._abilityChannel.watch(function(d) {
var event = d.event, data = d.data;
if(event === "update") {
data();
}
})
}*/
return Battle;
})();

View File

@@ -174,19 +174,6 @@ Battleside = (function(){
}
r.update = function(){
/*
this.send("update:info", {
info: this.getInfo(),
leader: this.field[Card.TYPE.LEADER].get()[0]
})
this.send("update:hand", {
cards: JSON.stringify(this.hand.getCards())
});
this.send("update:fields", {
close: this.field[Card.TYPE.CLOSE_COMBAT],
ranged: this.field[Card.TYPE.RANGED],
siege: this.field[Card.TYPE.SIEGE]
})*/
PubSub.publish("update");
}

View File

@@ -9,6 +9,7 @@ var Card = (function(){
/**
* constructor here
*/
this.channel = {};
this._key = key;
this._data = CardData[key];
this._boost = 0;
@@ -38,6 +39,8 @@ var Card = (function(){
WEATHER: 5
};
r.channel = null
r._init = function(){
this._id = ++Card.__id;

View File

@@ -1,28 +0,0 @@
var Entity = require("./Entity");
var Npc = (function(){
var Npc = function(){
if(!(this instanceof Npc)){
return (new Npc());
}
Entity.call(this);
/**
* constructor here
*/
};
Npc.prototype = Object.create(Entity.prototype);
var r = Npc.prototype;
/**
* methods && properties here
* r.property = null;
* r.getProperty = function() {...}
*/
return Npc;
})();
module.exports = Npc;

View File

@@ -2,17 +2,28 @@ var shortid = require("shortid");
var Battle = require("./Battle");
var Room = (function(){
var Room = function(){
var Room = function(scServer){
if(!(this instanceof Room)){
return (new Room());
return (new Room(scServer));
}
/**
* constructor here
*/
var self = this;
this._id = shortid.generate();
this._room = [];
this._users = [];
this._ready = {};
this.socket = scServer.global;
/*
this._channel = this.socket.subscribe(this._id);*/
/*this._channel.watch(function(data) {
*//*self._users.forEach(function(user) {
})*//*
});*/
};
var r = Room.prototype;
/**
@@ -21,20 +32,21 @@ var Room = (function(){
* r.getProperty = function() {...}
*/
r.MAX_USER = 2;
r._room = null;
r._users = null;
r._id = null;
r._battle = null;
r._ready = null;
r._channel = null;
r.getID = function(){
return this._id;
}
r.join = function(user){
if(this._room.length >= 2) return;
this._room.push(user);
user.setRoom(this);
user.joinRoom(this.getID());
if(this._users.length >= 2) return;
this._users.push(user);
user.addRoom(this);
/*user.joinRoom(this.getID());*/
if(!this.isOpen()){
this.initBattle();
@@ -42,47 +54,60 @@ var Room = (function(){
}
r.isOpen = function(){
return !(this._room.length >= 2);
return !(this._users.length >= 2);
}
/*
r.send = function(event, data){
io.to(this._id).emit(event, data);
*/
/*this.socket.publish(this._id + "|" + event, data);
this.socket.publish(this._id, {
event: event,
data: data
});
var subs = this.socket.subscriptions();
subs.forEach(function(sub) {
});*//*
this._channel.publish(event, data);
}
*/
r.getPlayers = function(){
return this._room;
return this._users;
}
r.initBattle = function(){
var self = this;
var side = 0;
this._battle = Battle(this._id, this._room[0], this._room[1]);
this._room[0].send("init:battle", {side: "p1"});
this._room[1].send("init:battle", {side: "p2"});
this._battle = Battle(this._id, this._users[0], this._users[1], this.socket);
this._users[0].send("init:battle", {side: "p1"});
this._users[1].send("init:battle", {side: "p2"});
}
r.setReady = function(user, b){
b = typeof b == "undefined" ? true : b;
this._ready[user.getID()] = b;
if(this.bothReady()) {
if(this.bothReady()){
this._battle.init();
}
/*
if(!this.checkIfReady()) return;
this._room[0].send("init:battle", {side: "p1"});
this._room[1].send("init:battle", {side: "p2"});
this._users[0].send("init:battle", {side: "p1"});
this._users[1].send("init:battle", {side: "p2"});
if(!this.checkIfReady()) return;
this._battle.init();*/
}
r.bothReady = function() {
return !!this._ready[this._room[0].getID()] && !!this._ready[this._room[1].getID()];
r.bothReady = function(){
return !!this._ready[this._users[0].getID()] && !!this._ready[this._users[1].getID()];
}
/*
r.checkIfReady = function(){
for(var i = 0; i < this._room.length; i++) {
if(!this._ready[this._room[i].getID()]) return false;
for(var i = 0; i < this._users.length; i++) {
if(!this._ready[this._users[i].getID()]) return false;
}
return true;
}*/

View File

@@ -6,9 +6,6 @@ var Battle = require("./Battle");
var Npc = require("./Npc");
var Room = require("./Room");
/*
var Matchmaker = require("./Matchmaker");
*/
var Socket = (function(){
var Socket = function(){

View File

@@ -1,22 +1,18 @@
var Entity = require("./Entity");
var User = (function(){
var User = function(socket){
if(!(this instanceof User)){
return (new User(socket));
}
Entity.call(this);
/**
* constructor here
*/
this.socket = socket;
this._rooms = [];
this._id = socket.id;
this.generateName();
};
User.prototype = Object.create(Entity.prototype);
var r = User.prototype;
/**
* methods && properties here
@@ -26,7 +22,7 @@ var User = (function(){
r._id = null;
r._name = null;
r._room = null;
r._rooms = null;
r.socket = null;
r.getID = function(){
@@ -34,7 +30,11 @@ var User = (function(){
}
r.joinRoom = function(roomid){
this.socket.join(roomid);
var self = this;
/*this.socket.on(roomid, function(d) {
var event = d.event, data = d.data;
self.socket.on(event, data);
});*/
}
r.send = function(event, data, room){
@@ -43,8 +43,12 @@ var User = (function(){
if(!room){
this.socket.emit(event, data);
}
else {
this.socket.to(room).emit(event, data);
else {/*
this.socket.to(room).emit(event, data);*/
this.socket.global.publish(room, {
event: event,
data: data
})
}
}
@@ -63,10 +67,10 @@ var User = (function(){
return this._name;
}
r.getRoom = function() {
return this._room;
return this._rooms[0];
}
r.setRoom = function(room) {
this._room = room;
r.addRoom = function(room) {
this._rooms.push(room);
}
r.disconnect = function() {

2
server/_server.js Normal file
View File

@@ -0,0 +1,2 @@
var socket = require("./Socket")();

View File

@@ -1,2 +1,13 @@
var socket = require("./Socket")();
var argv = require('minimist')(process.argv.slice(2));
var SocketCluster = require('socketcluster').SocketCluster;
var socketCluster = new SocketCluster({
workers: Number(argv.w) || 1,
stores: Number(argv.s) || 1,
port: Number(argv.p) || 16918,
appName: argv.n || null,
workerController: __dirname + '/worker.js',
storeController: __dirname + '/store.js',
socketChannelLimit: 100,
rebootWorkerOnCrash: argv['auto-reboot'] != false
});

4
server/store.js Normal file
View File

@@ -0,0 +1,4 @@
module.exports.run = function (store) {
console.log(' >> Store PID:', process.pid);
};

78
server/worker.js Normal file
View File

@@ -0,0 +1,78 @@
var fs = require('fs');
var express = require('express');
var serveStatic = require('serve-static');
var path = require('path');
var User = require("./User");
var Connections = require("./Connections");
var Battle = require("./Battle");
var Room = require("./Room");
module.exports.run = function(worker){
console.log(' >> Worker PID:', process.pid);
var app = require('express')();
var httpServer = worker.httpServer;
var scServer = worker.scServer;
app.use(serveStatic(path.resolve(__dirname, 'public')));
httpServer.on('request', app);
var connections = Connections();
var roomCollection = {};
scServer.on('connection', function(socket){
var user = User(socket);
connections.add(user);
console.log("new user ", user.getName());
socket.on("request:name", function(data){
if(data && data.name){
user.setName(data.name);
}
socket.emit("response:name", {name: user.getName()});
})
socket.on("request:gameLoaded", function(data){
console.log(data);
roomCollection[data._roomID].setReady(user);
})
socket.on("request:createRoom", function(){
var room = Room(worker.getSCServer());
roomCollection[room.getID()] = room;
room.join(user);
console.log("room %s created by %s", room.getID(), user.getName());
user.send("response:createRoom", room.getID());
})
socket.on("request:joinRoom", function(){
console.log("joinroom");
var interval = setInterval(function(){
for(var key in roomCollection) {
var room = roomCollection[key];
if(!room.isOpen()) continue;
room.join(user);
clearInterval(interval);
console.log("user %s joined room %s", user.getName(), room.getID());
user.send("response:joinRoom", room.getID());
}
}, 1000);
})
socket.on("request:roomData", function(){
var room = user.getRoom();
var players = room.getPlayers();
user.send("response:roomData", {players: players});
})
socket.on('disconnect', function(){
connections.remove(user);
user.disconnect();
});
});
};