mirror of
https://github.com/exane/not-gwent-online
synced 2025-09-22 14:49:06 +00:00
update
This commit is contained in:
@@ -22,7 +22,9 @@ var Battle = (function(){
|
||||
r.init = function(p1, p2){
|
||||
this.setPlayer(p1, p2);
|
||||
this.initBattleside();
|
||||
this.render();
|
||||
this.both(function(p) {
|
||||
p.send("init:battle");
|
||||
})
|
||||
}
|
||||
|
||||
r.setPlayer = function(p1, p2){
|
||||
@@ -37,10 +39,8 @@ var Battle = (function(){
|
||||
});
|
||||
}
|
||||
|
||||
r.render = function() {
|
||||
this._player.forEach(function(p) {
|
||||
p.send("update:name", p.getID());
|
||||
});
|
||||
r.both = function() {
|
||||
this._player.forEach(cb);
|
||||
}
|
||||
|
||||
|
||||
|
@@ -30,6 +30,10 @@ var Connections = (function(){
|
||||
return this._connections;
|
||||
}
|
||||
|
||||
r.hasUser = function(user) {
|
||||
return !!this._connections[user.getID()];
|
||||
}
|
||||
|
||||
|
||||
return Connections;
|
||||
})();
|
||||
|
@@ -1,15 +1,17 @@
|
||||
var Promise = require("promise");
|
||||
|
||||
var Matchmaker = (function(){
|
||||
var Matchmaker = function(){
|
||||
var Matchmaker = function(connections){
|
||||
if(!(this instanceof Matchmaker)){
|
||||
return (new Matchmaker());
|
||||
return (new Matchmaker(connections));
|
||||
}
|
||||
/**
|
||||
* constructor here
|
||||
*/
|
||||
|
||||
this._connections = connections;
|
||||
this._queue = [];
|
||||
|
||||
};
|
||||
var r = Matchmaker.prototype;
|
||||
/**
|
||||
@@ -19,6 +21,7 @@ var Matchmaker = (function(){
|
||||
*/
|
||||
|
||||
r._queue = null;
|
||||
r._connections = null;
|
||||
|
||||
r.findOpponent = function(user){
|
||||
var self = this;
|
||||
@@ -32,14 +35,16 @@ var Matchmaker = (function(){
|
||||
|
||||
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("update:opponent", {opponent: p2.getID()});
|
||||
p2.send("update:opponent", {opponent: p1.getID()});
|
||||
p1.send("get:opponent", {socketID: p2.getID()});
|
||||
p2.send("get:opponent", {socketID: p1.getID()});
|
||||
|
||||
p1.joinRoom(roomID);
|
||||
p2.joinRoom(roomID);
|
||||
@@ -47,6 +52,20 @@ var Matchmaker = (function(){
|
||||
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;
|
||||
}
|
||||
});
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
return Matchmaker;
|
||||
})();
|
||||
|
59
server/Room.js
Normal file
59
server/Room.js
Normal file
@@ -0,0 +1,59 @@
|
||||
var shortid = require("shortid");
|
||||
|
||||
var Room = (function(){
|
||||
var Room = function(){
|
||||
if(!(this instanceof Room)){
|
||||
return (new Room());
|
||||
}
|
||||
/**
|
||||
* constructor here
|
||||
*/
|
||||
|
||||
this._id = shortid.generate();
|
||||
this._room = [];
|
||||
};
|
||||
var r = Room.prototype;
|
||||
/**
|
||||
* methods && properties here
|
||||
* r.property = null;
|
||||
* r.getProperty = function() {...}
|
||||
*/
|
||||
r.MAX_USER = 2;
|
||||
r._room = null;
|
||||
r._id = null;
|
||||
|
||||
r.getID = function() {
|
||||
return this._id;
|
||||
}
|
||||
|
||||
r.join = function(user) {
|
||||
if(this._room.lenght >= 2) return;
|
||||
this._room.push(user);
|
||||
user.setRoom(this);/*
|
||||
user.socket.join(this._id);*/
|
||||
user.joinRoom(this.getID());
|
||||
|
||||
if(!this.isOpen()) {
|
||||
this._room.forEach(function(user) {
|
||||
user.send("init:battle");
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
r.isOpen = function() {
|
||||
return !(this._room.length >= 2);
|
||||
}
|
||||
|
||||
r.send = function(event, data) {
|
||||
io.to(this._id).emit(event, data);
|
||||
}
|
||||
|
||||
r.getPlayers = function() {
|
||||
return this._room;
|
||||
}
|
||||
|
||||
|
||||
return Room;
|
||||
})();
|
||||
|
||||
module.exports = Room;
|
@@ -1,10 +1,14 @@
|
||||
var app = require('http').createServer();
|
||||
var io = require("socket.io")(app);
|
||||
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 Matchmaker = require("./Matchmaker");
|
||||
*/
|
||||
|
||||
var Socket = (function(){
|
||||
var Socket = function(){
|
||||
@@ -14,8 +18,11 @@ var Socket = (function(){
|
||||
/**
|
||||
* constructor here
|
||||
*/
|
||||
this.matchmaker = Matchmaker();
|
||||
this.connections = Connections();
|
||||
/*
|
||||
this.matchmaker = Matchmaker(this.connections);
|
||||
*/
|
||||
this._roomCollection = [];
|
||||
app.listen(this.port);
|
||||
this.io = io;
|
||||
this._events();
|
||||
@@ -29,27 +36,66 @@ var Socket = (function(){
|
||||
r.io = null;
|
||||
r.port = 16918;
|
||||
r.connections = null;
|
||||
r._roomCollection = null;
|
||||
/*
|
||||
r.matchmaker = null;
|
||||
*/
|
||||
|
||||
r._events = function() {
|
||||
r._events = function(){
|
||||
var self = this;
|
||||
this.io.on("connection", function(socket) {
|
||||
this.io.on("connection", function(socket){
|
||||
var user = User(socket);
|
||||
self.connections.add(user);
|
||||
console.log("new user ", user.getID());
|
||||
console.log("new user ", user.getName());
|
||||
|
||||
this.matchmaker.findOpponent(user)
|
||||
.then(function(p1, p2, roomID) {
|
||||
var battle = Battle();
|
||||
battle.init(p1, p2);
|
||||
/* self.matchmaker.findOpponent(user)
|
||||
.then(function(p1, p2, roomID) {
|
||||
console.log("yo");
|
||||
var battle = Battle();
|
||||
battle.init(p1, p2);
|
||||
})*/
|
||||
|
||||
socket.on("request:name", function(data){
|
||||
if(data && data.name){
|
||||
user.setName(data.name);
|
||||
}
|
||||
socket.emit("response:name", {name: user.getName()});
|
||||
})
|
||||
|
||||
socket.on("disconnect", function() {
|
||||
socket.on("request:createRoom", function() {
|
||||
var room = Room();
|
||||
self._roomCollection.push(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(){
|
||||
self._roomCollection.forEach(function(room) {
|
||||
if(room.isOpen()) {
|
||||
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(){
|
||||
self.connections.remove(user);
|
||||
user.disconnect();
|
||||
})
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
return Socket;
|
||||
})();
|
||||
|
||||
|
@@ -14,7 +14,7 @@ var User = (function(){
|
||||
|
||||
this.socket = socket;
|
||||
this._id = socket.id;
|
||||
|
||||
this.generateName();
|
||||
};
|
||||
User.prototype = Object.create(Entity.prototype);
|
||||
var r = User.prototype;
|
||||
@@ -25,14 +25,16 @@ var User = (function(){
|
||||
*/
|
||||
|
||||
r._id = null;
|
||||
r._name = null;
|
||||
r._room = null;
|
||||
r.socket = null;
|
||||
|
||||
r.getID = function(){
|
||||
return this._id;
|
||||
}
|
||||
|
||||
r.joinRoom = function(room){
|
||||
this.socket.join(room);
|
||||
r.joinRoom = function(roomid){
|
||||
this.socket.join(roomid);
|
||||
}
|
||||
|
||||
r.send = function(event, data, room){
|
||||
@@ -46,6 +48,32 @@ var User = (function(){
|
||||
}
|
||||
}
|
||||
|
||||
r.generateName = function(){
|
||||
var name = "Player" + (((Math.random() * 8999) + 1000) | 0);
|
||||
//if(lobby.hasUser(name)) return generateName();
|
||||
this._name = name;
|
||||
return name;
|
||||
}
|
||||
|
||||
r.setName = function(name) {
|
||||
console.log("user name changed from %s to %s", this._name, name);
|
||||
this._name = name;
|
||||
}
|
||||
r.getName = function() {
|
||||
return this._name;
|
||||
}
|
||||
r.getRoom = function() {
|
||||
return this._room;
|
||||
}
|
||||
r.setRoom = function(room) {
|
||||
this._room = room;
|
||||
}
|
||||
|
||||
r.disconnect = function() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
return User;
|
||||
})();
|
||||
|
||||
|
Reference in New Issue
Block a user