1
0
mirror of https://github.com/exane/not-gwent-online synced 2024-12-23 21:03:14 +00:00
This commit is contained in:
devfake 2015-06-24 20:34:39 +02:00
commit 04ea6d3db5
10 changed files with 265 additions and 143 deletions

View File

@ -1,5 +1,4 @@
/*("http://localhost:16918")*/
var socketCluster = require("socketcluster-client");
var socket = require("socket.io-client");
var Backbone = require("backbone");
require("./backbone.modal-min");
var Handlebars = require("handlebars");
@ -21,7 +20,7 @@ var App = Backbone.Router.extend({
Backbone.history.start();
},
connect: function(){
this.socket = socketCluster.connect(Config.Server);
this.socket = socket(Config.Server.hostname + ":" + Config.Server.port);
},
receive: function(event, cb){
this.socket.on(event, cb);
@ -235,7 +234,7 @@ var BattleView = Backbone.View.extend({
var interval = setInterval(function(){
if(!user.get("room")) return;
this.setUpBattleEvents(user.get("room"));
this.setUpBattleEvents();
this.app.send("request:gameLoaded", {_roomID: user.get("room")});
clearInterval(interval);
}.bind(this), 10);
@ -380,7 +379,7 @@ var BattleView = Backbone.View.extend({
calculateCardMargin(this.$el.find(".field-hand .card"), 538, 70, this.handCards.length);
}
if(this.user.get("isReDrawing")) {
if(this.user.get("isReDrawing")){
this.user.set("handCards", this.handCards);
var modal = new ReDrawModal({model: this.user});
this.$el.prepend(modal.render().el);
@ -421,12 +420,50 @@ var BattleView = Backbone.View.extend({
this.app.send("activate:leader")
},
setUpBattleEvents: function(channelName){
this.battleChannel = this.app.socket.subscribe(channelName);
setUpBattleEvents: function(){
var self = this;
var user = this.user;
var app = user.get("app");
this.battleChannel.watch(function(d){
app.on("update:hand", function(data) {
if(user.get("roomSide") == data._roomSide){
self.handCards = JSON.parse(data.cards);
self.user.set("handCards", app.handCards);
self.render();
}
})
app.on("update:info", function(data) {
var _side = data._roomSide;
var infoData = data.info;
var leader = data.leader;
var side = self.yourSide;
if(user.get("roomSide") != _side){
side = self.otherSide;
}
side.infoData = infoData;
side.leader = leader;
side.infoData.discard = JSON.parse(side.infoData.discard);
side.render();
})
app.on("update:fields", function(data) {
var _side = data._roomSide;
var side = self.yourSide;
if(user.get("roomSide") != _side){
side = self.otherSide;
}
side.field.close = data.close;
side.field.ranged = data.ranged;
side.field.siege = data.siege;
side.field.weather = data.weather;
side.render();
})
/*this.battleChannel.watch(function(d){
var event = d.event, data = d.data;
if(event === "update:hand"){
@ -465,7 +502,7 @@ var BattleView = Backbone.View.extend({
side.field.weather = data.weather;
side.render();
}
})
})*/
}
});
@ -525,7 +562,8 @@ var User = Backbone.Model.extend({
},
initialize: function(){
var self = this;
var app = this.get("app");
var user = this;
var app = user.get("app");
this.listenTo(this.attributes, "change:room", this.subscribeRoom);
@ -541,10 +579,10 @@ var User = Backbone.Model.extend({
app.navigate("battle", {trigger: true});
})
app.receive("response:createRoom", function(roomID){
/*app.receive("response:createRoom", function(roomID){
self.set("room", roomID);
console.log("room created", roomID);
});
});*/
app.receive("response:joinRoom", function(roomID){
self.set("room", roomID);
@ -584,15 +622,25 @@ var User = Backbone.Model.extend({
self.set("setHorn", data.cardID);
})
app.receive("redraw:cards", function() {
app.receive("redraw:cards", function(){
self.set("isReDrawing", true);
})
app.receive("redraw:close", function() {
app.receive("redraw:close", function(){
self.set("isReDrawing", false);
})
app.on("createRoom", this.createRoom, this);
app.receive("update:hand", function(data){
app.trigger("update:hand", data);
})
app.receive("update:fields", function(data){
app.trigger("update:fields", data);
})
app.receive("update:info", function(data){
app.trigger("update:info", data);
})
app.on("startMatchmaking", this.startMatchmaking, this);
app.on("joinRoom", this.joinRoom, this);
app.on("setName", this.setName, this);
app.on("setDeck", this.setDeck, this);
@ -600,16 +648,18 @@ var User = Backbone.Model.extend({
app.send("request:name", this.get("name") == "unnamed" ? null : {name: this.get("name")});
},
createRoom: function(){
this.get("app").send("request:createRoom");
startMatchmaking: function(){
this.set("inMatchmakerQueue", true);
this.get("app").send("request:matchmaking");
},
joinRoom: function(){
this.get("app").send("request:joinRoom");
this.set("inMatchmakerQueue", false);
},
subscribeRoom: function(){
var room = this.get("room");
var app = this.get("app");
app.socket.subscribe(room);
//app.socket.subscribe(room);
},
setName: function(name){
this.get("app").send("request:name", {name: name});
@ -636,8 +686,8 @@ var Lobby = Backbone.View.extend({
this.render();
},
events: {
"click .create-room": "createRoom",
"click .join-room": "joinRoom",
"click .startMatchmaking": "startMatchmaking",
/*"click .join-room": "joinRoom",*/
"blur .name-input": "changeName",
"change #deckChoice": "setDeck"
},
@ -646,8 +696,8 @@ var Lobby = Backbone.View.extend({
/*this.$el.find("#deckChoice option[value='" + this.app.user.get("setDeck") + "']").attr("selected", "selected")*/
return this;
},
createRoom: function(){
this.app.trigger("createRoom");
startMatchmaking: function(){
this.app.trigger("startMatchmaking");
},
joinRoom: function(){
this.app.trigger("joinRoom");

View File

@ -14,8 +14,11 @@
</div>
<div class="row">
<div class="col-xs-12">
<button type="button" class="btn btn-primary create-room">Create Room</button>
<button type="button" class="btn btn-primary join-room">Join Room</button>
<button type="button" class="btn btn-primary startMatchmaking"{{#if inMatchmakerQueue}} disabled{{/if}}>
Search Opponent
{{#if inMatchmakerQueue}}<img src="../site/public/assets/img/content-load.gif" width=20 style="margin: 5px;"> {{/if}}
</button>
<!--<button type="button" class="btn btn-primary join-room">Join Room</button>-->
</div>
</div>
</div>

View File

@ -10,8 +10,8 @@
"minimist": "1.1.0",
"serve-static": "1.8.0",
"shortid": "^2.2.2",
"socketcluster": "2.2.x",
"socketcluster-client": "2.2.x",
"socket.io": "^1.3.5",
"socket.io-client": "^1.3.5",
"underscore": "^1.8.3"
},
"devDependencies": {

View File

@ -46,7 +46,7 @@ var Battle = (function(){
this.on("AfterPlace", this.checkAbilityOnAfterPlace)*/
this.channel = this.socket.subscribe(this._id);
//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;
@ -67,25 +67,16 @@ var Battle = (function(){
Promise.when(this.p1.reDraw(2), this.p2.reDraw(2))
.then(function() {
.then(function(){
this.on("NextTurn", this.switchTurn);
this.switchTurn(Math.random() > .5 ? this.p1 : this.p2);
}.bind(this));
/*
this.on("NextTurn", this.switchTurn);
this.switchTurn(Math.random() > .5 ? this.p1 : this.p2);*/
}
r.switchTurn = function(side, __flag){
__flag = typeof __flag == "undefined" ? 0 : 1;
/*side.foe.wait();*/
if(!(side instanceof Battleside)){
console.trace("side is not a battleside!");
@ -98,13 +89,12 @@ var Battle = (function(){
return this.switchTurn(side.foe, 1);
}
this.runEvent("EachTurn");
//setTimeout(function() {
this.runEvent("Turn" + side.getID());
//}.bind(this), 1000);
console.log("current Turn: ", side.getName());
console.log("current Turn: ", side.getName());
}
r.startNextRound = function(){
@ -130,27 +120,33 @@ var Battle = (function(){
this._update(this.p2);
}
r._update = function(p){
r.updateSelf = function(side) {
this._update(side, true);
}
r._update = function(p, isPrivate){
isPrivate = isPrivate || false;
p.send("update:info", {
info: p.getInfo(),
leader: p.field[Card.TYPE.LEADER].get()[0]
})
}, isPrivate)
p.send("update:hand", {
cards: JSON.stringify(p.hand.getCards())
});
},isPrivate);
p.send("update:fields", {
close: p.field[Card.TYPE.CLOSE_COMBAT].getInfo(),
ranged: p.field[Card.TYPE.RANGED].getInfo(),
siege: p.field[Card.TYPE.SIEGE].getInfo(),
weather: p.field[Card.TYPE.WEATHER].getInfo()
})
}, isPrivate);
}
r.send = function(event, data){
this.channel.publish({
/*this.channel.publish({
event: event,
data: data
});
});*/
io.sockets.in(this._id).emit(event, data);
}
r.runEvent = function(eventid, ctx, args, uid){
@ -175,7 +171,7 @@ var Battle = (function(){
obj.cb.apply(ctx, obj.onArgs.concat(args));
}
}
this.update();
//this.update();
}
r.on = function(eventid, cb, ctx, args){

View File

@ -78,7 +78,7 @@ Battleside = (function(){
})
this.receive("set:passing", function(){
self.setPassing(true);
self.update();
//self.update();
self.runEvent("NextTurn", null, [self.foe]);
})
this.receive("medic:chooseCardFromDiscard", function(data){
@ -210,10 +210,6 @@ Battleside = (function(){
var card = this.deck.draw();
this.hand.add(card);
}
console.log("update:hand fired");
/*this.update();*/
}
r.calcScore = function(){
@ -266,9 +262,9 @@ Battleside = (function(){
this.socket.on(event, cb);
}
r.update = function(){
//PubSub.publish("update");
this.runEvent("Update");
r.update = function(self){
self = self || false;
this.runEvent("Update", null, [self]);
}
r.onTurnStart = function(){
@ -325,7 +321,6 @@ Battleside = (function(){
this.checkAbilityOnAfterPlace(card, obj);
this.update();
if(obj._waitResponse){
this.hand.remove(card);
@ -333,6 +328,8 @@ Battleside = (function(){
return 0;
}
this.update();
return 1;
}
@ -409,7 +406,7 @@ Battleside = (function(){
}
if(ability && ability.name === obj.suppress){
this.update();
//this.update();
}
if(ability && !Array.isArray(ability)){
@ -454,7 +451,6 @@ Battleside = (function(){
self.hand.add(replaceCard);
self.hand.remove(card);
self.update();
self.runEvent("NextTurn", null, [self.foe]);
})
@ -472,7 +468,7 @@ Battleside = (function(){
card._uidEvents["WeatherChange"] = uid;
}
this.update();
//this.update();
}
}
@ -480,7 +476,7 @@ Battleside = (function(){
var ability = card.getAbility();
if(ability){
if(ability.name && ability.name === obj.suppress){
this.update();
//this.update();
return;
}
if(ability.onAfterPlace){
@ -529,7 +525,7 @@ Battleside = (function(){
_card.setForcedPower(forcedPower);
});
this.runEvent("WeatherChange");
this.update();
//this.update();
}
r.clearMainFields = function(){
@ -633,17 +629,20 @@ Battleside = (function(){
self.deck.add(card);
self.deck.shuffle();
self.draw(1);
self.update();
if(!left) {
self.send("redraw:close", null, true);
console.log("redraw finished");
deferred.resolve("done");
//self.socket.off("redraw:reDrawCard", h1);
}
/*self.update(self);*/
self.battle.updateSelf(self);
})
this.receive("redraw:close_client", function() {
console.log("redraw finished!");
deferred.resolve("done");
//self.socket.off("redraw:close_client", h2);
})
return deferred;

View File

@ -1,9 +1,9 @@
var Promise = require("promise");
var Matchmaker = (function(){
var Matchmaker = function(connections){
var Matchmaker = function(){
if(!(this instanceof Matchmaker)){
return (new Matchmaker(connections));
return (new Matchmaker());
}
/**
* constructor here
@ -23,47 +23,47 @@ var Matchmaker = (function(){
r._queue = null;
r._connections = null;
r.findOpponent = function(user){
var self = this;
var promise = new Promise(function(resolve){
self._queue.push(user);
self._checkForOpponent(resolve);
});
return promise;
}
r._checkForOpponent = function(resolve){
if(this._queue.length <= 1) return;
console.log(this._queue.length);
if(!this._checkConnections()) return;
this._match(this._queue[0], this._queue[1], resolve);
}
r._match = function(p1, p2, resolve){
this._queue.splice(0, 2);
var roomID = p1.id + p2.id;
p1.send("get:opponent", {socketID: p2.getID()});
p2.send("get:opponent", {socketID: p1.getID()});
p1.joinRoom(roomID);
p2.joinRoom(roomID);
resolve(p1, p2, roomID);
}
r._checkConnections = function() {
var res = true;
var self = this;
this._queue.forEach(function(user, index) {
if(!self._connections.hasUser(user)) {
self._queue.splice(index, 1);
res = false;
r.removeFromQueue = function(user){
for(var i = 0; i < this._queue.length; i++) {
var u = this._queue[i];
if(u.getID() === user.getID()) {
user._inQueue = false;
return this._queue.splice(i, 1);
}
});
}
}
return res;
r.findOpponent = function(user){
var c = connections;
var found = this._checkForOpponent();
if(found){
var room = Room();
c.roomCollection[room.getID()] = room;
room.join(user);
room.join(found);
user._inQueue = false;
found._inQueue = false;
return room;
}
this._getInQueue(user);
}
r._getInQueue = function(user){
console.log(user.getName() + " joined in queue");
this._queue.push(user);
user._inQueue = true;
}
r._checkForOpponent = function(){
if(!this._queue.length) return null;
var foe = this._queue.splice(0, 1)[0];
foe._inQueue = false;
return foe;
}

View File

@ -2,9 +2,9 @@ var shortid = require("shortid");
var Battle = require("./Battle");
var Room = (function(){
var Room = function(scServer){
var Room = function(){
if(!(this instanceof Room)){
return (new Room(scServer));
return (new Room());
}
/**
* constructor here
@ -14,7 +14,7 @@ var Room = (function(){
this._id = shortid.generate();
this._users = [];
this._ready = {};
this.socket = scServer.global;
//this.socket = scServer.global;
console.log("room created: " + this.getID());
@ -40,6 +40,8 @@ var Room = (function(){
if(this._users.length >= 2) return;
this._users.push(user);
user.addRoom(this);
user.socket.join(this.getID());
user.send("response:joinRoom", this.getID());
if(!this.isOpen()){
this.initBattle();
@ -55,7 +57,7 @@ var Room = (function(){
}
r.initBattle = function(){
this._battle = Battle(this._id, this._users[0], this._users[1], this.socket);
this._battle = Battle(this._id, this._users[0], this._users[1], io);
this._users[0].send("init:battle", {side: "p1"});
this._users[1].send("init:battle", {side: "p2"});
}

View File

@ -1,11 +1,8 @@
/*
var app = require('http').createServer();
global.io = require("socket.io")(app);
var User = require("./User");
/*var app = require('http').createServer();
global.io = require("socket.io")(app);*/
/*var User = require("./User");
var Connections = require("./Connections");
var Battle = require("./Battle");
var Npc = require("./Npc");
var Room = require("./Room");
var Room = require("./Room");*/
var Socket = (function(){
@ -13,39 +10,28 @@ var Socket = (function(){
if(!(this instanceof Socket)){
return (new Socket());
}
*/
/**
/**
* constructor here
*//*
*/
this.connections = Connections();
*/
/*
this.matchmaker = Matchmaker(this.connections);
*//*
this.roomCollection = {};
app.listen(this.port);
this.io = io;
this._events();
};
var r = Socket.prototype;
*/
/**
/**
* methods && properties here
* r.property = null;
* r.getProperty = function() {...}
*//*
*/
r.io = null;
r.port = 16918;
r.connections = null;
r.roomCollection = null;
*/
/*
/*
r.matchmaker = null;
*//*
*/
r._events = function(){
var self = this;
@ -102,4 +88,4 @@ var Socket = (function(){
return Socket;
})();
module.exports = Socket;*/
module.exports = Socket;

View File

@ -12,6 +12,8 @@ var User = (function(){
this._rooms = [];
this._id = socket.id;
this.generateName();
this._events();
};
var r = User.prototype;
/**
@ -23,7 +25,7 @@ var User = (function(){
r._id = null;
r._name = null;
r._rooms = null;
r._searching = false;
r._inQueue = false;
r.socket = null;
r.disconnected = false;
@ -31,13 +33,13 @@ var User = (function(){
return this._id;
}
r.joinRoom = function(roomid){
/*r.joinRoom = function(roomid){
var self = this;
/*this.socket.on(roomid, function(d) {
*//*this.socket.on(roomid, function(d) {
var event = d.event, data = d.data;
self.socket.on(event, data);
});*/
}
});*//*
}*/
r.send = function(event, data, room){
room = room || null;
@ -45,12 +47,12 @@ var User = (function(){
if(!room){
this.socket.emit(event, data);
}
else {/*
this.socket.to(room).emit(event, data);*/
this.socket.global.publish(room, {
else {
this.socket.to(room).emit(event, data);
/*this.socket.global.publish(room, {
event: event,
data: data
})
})*/
}
}
@ -102,6 +104,8 @@ var User = (function(){
var self = this;
this.disconnected = true;
matchmaking.removeFromQueue(this);
this._rooms.forEach(function(room) {
room.leave(self);
if(!room.hasUser()) {
@ -113,6 +117,57 @@ var User = (function(){
this.cleanUp();
}
r._events = function() {
var socket = this.socket;
var self = this;
socket.on("request:name", function(data){
if(data && data.name){
self.setName(data.name);
}
socket.emit("response:name", {name: self.getName()});
})
/*socket.on("request:createRoom", function(){
var room = Room();
connections.roomCollection[room.getID()] = room;
room.join(self);
console.log("room %s created by %s", room.getID(), self.getName());
self.send("response:createRoom", room.getID());
})
socket.on("request:joinRoom", function(){
console.log("joinroom");
var interval = setInterval(function(){
for(var key in connections.roomCollection) {
var room = connections.roomCollection[key];
if(!room.isOpen()) continue;
room.join(self);
clearInterval(interval);
console.log("user %s joined room %s", self.getName(), room.getID());
self.send("response:joinRoom", room.getID());
}
}, 1000);
})*/
socket.on("request:matchmaking", function() {
if(self._inQueue) return;
matchmaking.findOpponent(self);
});
socket.on("request:gameLoaded", function(data){
console.log(data);
connections.roomCollection[data._roomID].setReady(self);
})
socket.on("set:deck", function(data) {
console.log(data);
if(data && data.deck){
self.setDeck(data.deck);
}
})
}
return User;
})();

View File

@ -1,6 +1,7 @@
var argv = require('minimist')(process.argv.slice(2));
var SocketCluster = require('socketcluster').SocketCluster;
/*var SocketCluster = require('socketcluster').SocketCluster;*/
/*
var socketCluster = new SocketCluster({
workers: Number(argv.w) || 1,
stores: Number(argv.s) || 1,
@ -10,4 +11,34 @@ var socketCluster = new SocketCluster({
storeController: __dirname + '/store.js',
socketChannelLimit: 100,
rebootWorkerOnCrash: argv['auto-reboot'] != false
});
});*/
global.connections = require("./Connections")();
global.matchmaking = require("./Matchmaker")();
global.Room = require("./Room");
global.User = require("./User");
/*global.Socket = require("./Socket");*/
var app = require('http').createServer();
global.io = require("socket.io")(app);
app.listen(16918);
io.on("connection", function(socket) {
var user;
connections.add(user = User(socket));
console.log("new user ", user.getName());
socket.on("disconnect", function() {
connections.remove(user);
user.disconnect();
console.log("user ", user.getName(), " disconnected");
user = null;
})
})