1
0
mirror of https://github.com/exane/not-gwent-online synced 2025-09-22 14:49:06 +00:00

add more stuff

This commit is contained in:
exane
2015-06-10 18:12:52 +02:00
parent 253f60d0ba
commit 4f98d3d651
186 changed files with 416 additions and 3 deletions

50
server/Battle.js Normal file
View File

@@ -0,0 +1,50 @@
var Battleside = require("./Battleside");
var Battle = (function(){
var Battle = function(){
if(!(this instanceof Battle)){
return (new Battle());
}
/**
* constructor here
*/
};
var r = Battle.prototype;
/**
* methods && properties here
* r.property = null;
* r.getProperty = function() {...}
*/
r._player = null;
r.init = function(p1, p2){
this.setPlayer(p1, p2);
this.initBattleside();
this.render();
}
r.setPlayer = function(p1, p2){
this._player = [];
this._player.push(p1);
this._player.push(p2);
}
r.initBattleside = function() {
this._player.forEach(function(p) {
p.setBattleside(Battleside(p));
});
}
r.render = function() {
this._player.forEach(function(p) {
p.send("update:name", p.getID());
});
}
return Battle;
})();
module.exports = Battle;

32
server/Battleside.js Normal file
View File

@@ -0,0 +1,32 @@
var Battleside = (function(){
var Battleside = function(player){
if(!(this instanceof Battleside)){
return (new Battleside(player));
}
/**
* constructor here
*/
this._player = player;
};
var r = Battleside.prototype;
/**
* methods && properties here
* r.property = null;
* r.getProperty = function() {...}
*/
r._player = null;
r._deck = null;
r._discard = null;
r._hand = null;
r._leader = null;
r._close = null;
r._range = null;
r._siege = null;
r._field = null;
return Battleside;
})();
module.exports = Battleside;

37
server/Connections.js Normal file
View File

@@ -0,0 +1,37 @@
var Connections = (function(){
var Connections = function(){
if(!(this instanceof Connections)){
return (new Connections());
}
/**
* constructor here
*/
this._connections = {};
};
var r = Connections.prototype;
/**
* methods && properties here
* r.property = null;
* r.getProperty = function() {...}
*/
r._connections = null;
r.add = function(user) {
this._connections[user.getID()] = user;
}
r.remove = function(user) {
delete this._connections[user.getID()];
}
r.get = function() {
return this._connections;
}
return Connections;
})();
module.exports = Connections;

28
server/Entity.js Normal file
View File

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

54
server/Matchmaker.js Normal file
View File

@@ -0,0 +1,54 @@
var Promise = require("promise");
var Matchmaker = (function(){
var Matchmaker = function(){
if(!(this instanceof Matchmaker)){
return (new Matchmaker());
}
/**
* constructor here
*/
this._queue = [];
};
var r = Matchmaker.prototype;
/**
* methods && properties here
* r.property = null;
* r.getProperty = function() {...}
*/
r._queue = 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;
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.joinRoom(roomID);
p2.joinRoom(roomID);
resolve(p1, p2, roomID);
}
return Matchmaker;
})();
module.exports = Matchmaker;

28
server/Npc.js Normal file
View File

@@ -0,0 +1,28 @@
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;

56
server/Socket.js Normal file
View File

@@ -0,0 +1,56 @@
var app = require('http').createServer();
var io = require("socket.io")(app);
var User = require("./User");
var Connections = require("./Connections");
var Battle = require("./Battle");
var Npc = require("./Npc");
var Matchmaker = require("./Matchmaker");
var Socket = (function(){
var Socket = function(){
if(!(this instanceof Socket)){
return (new Socket());
}
/**
* constructor here
*/
this.matchmaker = Matchmaker();
this.connections = Connections();
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.matchmaker = null;
r._events = function() {
var self = this;
this.io.on("connection", function(socket) {
var user = User(socket);
self.connections.add(user);
console.log("new user ", user.getID());
this.matchmaker.findOpponent(user)
.then(function(p1, p2, roomID) {
var battle = Battle();
battle.init(p1, p2);
})
socket.on("disconnect", function() {
self.connections.remove(user);
})
})
}
return Socket;
})();
module.exports = Socket;

52
server/User.js Normal file
View File

@@ -0,0 +1,52 @@
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._id = socket.id;
};
User.prototype = Object.create(Entity.prototype);
var r = User.prototype;
/**
* methods && properties here
* r.property = null;
* r.getProperty = function() {...}
*/
r._id = null;
r.socket = null;
r.getID = function(){
return this._id;
}
r.joinRoom = function(room){
this.socket.join(room);
}
r.send = function(event, data, room){
room = room || null;
data = data || null;
if(!room){
this.socket.emit(event, data);
}
else {
this.socket.to(room).emit(event, data);
}
}
return User;
})();
module.exports = User;

View File

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