mirror of
https://github.com/exane/not-gwent-online
synced 2025-09-22 14:49:06 +00:00
update2
This commit is contained in:
@@ -1,14 +1,16 @@
|
||||
var Battleside = require("./Battleside");
|
||||
|
||||
var io = global.io;
|
||||
|
||||
var Battle = (function(){
|
||||
var Battle = function(){
|
||||
var Battle = function(id){
|
||||
if(!(this instanceof Battle)){
|
||||
return (new Battle());
|
||||
return (new Battle(id));
|
||||
}
|
||||
/**
|
||||
* constructor here
|
||||
*/
|
||||
|
||||
this._id = id;
|
||||
};
|
||||
var r = Battle.prototype;
|
||||
/**
|
||||
@@ -17,33 +19,31 @@ var Battle = (function(){
|
||||
* r.getProperty = function() {...}
|
||||
*/
|
||||
|
||||
r._player = null;
|
||||
r.p1 = null;
|
||||
r.p2 = null;
|
||||
r.turn = 0;
|
||||
|
||||
r.init = function(p1, p2){
|
||||
this.setPlayer(p1, p2);
|
||||
this.initBattleside();
|
||||
this.both(function(p) {
|
||||
p.send("init:battle");
|
||||
})
|
||||
r._id = null;
|
||||
|
||||
|
||||
r.init = function(){
|
||||
this.p1 = Battleside("Player 1", 0, this);
|
||||
this.p2 = Battleside("Player 2", 1, this);
|
||||
this.p1.foe = this.p2;
|
||||
this.p2.foe = this.p1;
|
||||
|
||||
this.start();
|
||||
}
|
||||
|
||||
r.setPlayer = function(p1, p2){
|
||||
this._player = [];
|
||||
this._player.push(p1);
|
||||
this._player.push(p2);
|
||||
r.start = function() {
|
||||
this.p1.draw(10);
|
||||
this.p2.draw(10);
|
||||
}
|
||||
|
||||
r.initBattleside = function() {
|
||||
this._player.forEach(function(p) {
|
||||
p.setBattleside(Battleside(p));
|
||||
});
|
||||
r.send = function(event, data) {
|
||||
io.to(this._id).emit(event, data);
|
||||
}
|
||||
|
||||
r.both = function() {
|
||||
this._player.forEach(cb);
|
||||
}
|
||||
|
||||
|
||||
return Battle;
|
||||
})();
|
||||
|
||||
|
@@ -1,13 +1,24 @@
|
||||
|
||||
var io = global.io;
|
||||
var DeckData = require("../assets/data/deck");
|
||||
var Deck = require("./Deck");
|
||||
var Hand = require("./Hand");
|
||||
|
||||
|
||||
var Battleside = (function(){
|
||||
var Battleside = function(player){
|
||||
var Battleside = function(name, n, battle){
|
||||
if(!(this instanceof Battleside)){
|
||||
return (new Battleside(player));
|
||||
return (new Battleside(name, n, battle));
|
||||
}
|
||||
/**
|
||||
* constructor here
|
||||
*/
|
||||
|
||||
this._player = player;
|
||||
this.n = n ? "p2" : "p1";
|
||||
this._name = name;
|
||||
this.battle = battle;
|
||||
this.hand = Hand();
|
||||
this.deck = Deck(DeckData["test"]);
|
||||
};
|
||||
var r = Battleside.prototype;
|
||||
/**
|
||||
@@ -15,16 +26,38 @@ var Battleside = (function(){
|
||||
* r.property = null;
|
||||
* r.getProperty = function() {...}
|
||||
*/
|
||||
r._player = null;
|
||||
r._deck = null;
|
||||
r._name = null;
|
||||
r._discard = null;
|
||||
r._hand = null;
|
||||
r._leader = null;
|
||||
r._close = null;
|
||||
r._range = null;
|
||||
r._siege = null;
|
||||
r._field = null;
|
||||
|
||||
r.foe = null;
|
||||
r.hand = null;
|
||||
r.battle = null;
|
||||
r.deck = null;
|
||||
|
||||
|
||||
r.draw = function(times) {
|
||||
while(times--) {
|
||||
var card = this.deck.draw();
|
||||
this.hand.add(card);
|
||||
}
|
||||
|
||||
console.log("update:hand fired");
|
||||
|
||||
this.send("update:hand", {cards: JSON.stringify(this.hand.getCards())});
|
||||
}
|
||||
|
||||
r.send = function(event, msg) {
|
||||
msg = msg || {};
|
||||
msg._roomSide = this.n;
|
||||
this.battle.send(event, msg);
|
||||
}
|
||||
|
||||
|
||||
|
||||
return Battleside;
|
||||
})();
|
||||
|
91
server/Card.js
Normal file
91
server/Card.js
Normal file
@@ -0,0 +1,91 @@
|
||||
var CardData = require("../assets/data/cards");
|
||||
var AbilityData = require("../assets/data/abilities");
|
||||
|
||||
var Card = (function(){
|
||||
var Card = function(key){
|
||||
if(!(this instanceof Card)){
|
||||
return (new Card(key));
|
||||
}
|
||||
/**
|
||||
* constructor here
|
||||
*/
|
||||
this._key = key;
|
||||
this._data = CardData[key];
|
||||
this._boost = 0;
|
||||
this._forcedPower = -1;
|
||||
this._init();
|
||||
|
||||
};
|
||||
var r = Card.prototype;
|
||||
/**
|
||||
* methods && properties here
|
||||
* r.property = null;
|
||||
* r.getProperty = function() {...}
|
||||
*/
|
||||
r._key = null;
|
||||
r._data = null;
|
||||
r._id = null;
|
||||
r._owner = null;
|
||||
r._boost = null;
|
||||
r._forcedPower = null;
|
||||
Card.__id = 0;
|
||||
Card.TYPE = {
|
||||
CLOSE_COMBAT: 0,
|
||||
RANGED: 1,
|
||||
SIEGE: 2,
|
||||
LEADER: 3,
|
||||
SPECIAL: 4,
|
||||
WEATHER: 5
|
||||
};
|
||||
|
||||
r._init = function(){
|
||||
this._id = ++Card.__id;
|
||||
}
|
||||
|
||||
r.getName = function(){
|
||||
return this._data.name;
|
||||
}
|
||||
r.getPower = function(){
|
||||
if(this._forcedPower > -1) {
|
||||
return this._forcedPower + this._boost;
|
||||
}
|
||||
return this._data.power + this._boost;
|
||||
}
|
||||
r.setForcedPower = function(nr) {
|
||||
this._forcedPower = nr;
|
||||
}
|
||||
r.getRawAbility = function() {
|
||||
return this._data.ability;
|
||||
}
|
||||
r.getAbility = function(){
|
||||
return AbilityData[this._data.ability];
|
||||
}
|
||||
r.getImage = function(){
|
||||
return "../assets/cards/" + this._data.img + ".png";
|
||||
}
|
||||
r.getFaction = function(){
|
||||
return this._data.faction;
|
||||
}
|
||||
r.getType = function(){
|
||||
return this._data.type;
|
||||
}
|
||||
r.getKey = function(){
|
||||
return this._key;
|
||||
}
|
||||
|
||||
r.getId = function(){
|
||||
return this._id;
|
||||
}
|
||||
|
||||
r.boost = function(nr) {
|
||||
this._boost += nr;
|
||||
}
|
||||
|
||||
r.getProperty = function(prop){
|
||||
return this._data[prop];
|
||||
}
|
||||
|
||||
return Card;
|
||||
})();
|
||||
|
||||
module.exports = Card;
|
114
server/Deck.js
Normal file
114
server/Deck.js
Normal file
@@ -0,0 +1,114 @@
|
||||
var Card = require("./Card");
|
||||
/*var CardManager = require("./CardManager");*/
|
||||
|
||||
var Deck = (function(){
|
||||
var Deck = function(deck){
|
||||
if(!(this instanceof Deck)){
|
||||
return (new Deck(deck));
|
||||
}
|
||||
/**
|
||||
* constructor here
|
||||
*/
|
||||
this._deck = [];
|
||||
|
||||
this._originalDeck = [];
|
||||
this.setDeck(deck);
|
||||
};
|
||||
var r = Deck.prototype;
|
||||
/**
|
||||
* methods && properties here
|
||||
* r.property = null;
|
||||
* r.getProperty = function() {...}
|
||||
*/
|
||||
r._deck = null;
|
||||
r._owner = null;
|
||||
r._originalDeck = null;
|
||||
|
||||
r.setDeck = function(deckData){
|
||||
this._originalDeck = deckData.slice();
|
||||
this._deck = deckData.slice();
|
||||
|
||||
this._loadCards();
|
||||
this.shuffle();
|
||||
}
|
||||
|
||||
r.getLength = function(){
|
||||
return this._deck.length;
|
||||
}
|
||||
|
||||
r.length = function() {
|
||||
return this.getLength();
|
||||
}
|
||||
|
||||
r.getDeck = function(){
|
||||
return this._deck;
|
||||
}
|
||||
|
||||
r.draw = function(times){
|
||||
if(!this._deck.length) return 0;
|
||||
var card = this.pop();
|
||||
return card;
|
||||
}
|
||||
|
||||
/*
|
||||
r._loadCards = function(){
|
||||
var n = this._originalDeck.length;
|
||||
for(var i = 0; i < n; i++) {
|
||||
this._deck.push(CardManager().add(this._originalDeck[i], this._owner));
|
||||
}
|
||||
}*/
|
||||
|
||||
r._loadCards = function() {
|
||||
this._deck = this.getDeck().map(function(cardkey) {
|
||||
return Card(cardkey);
|
||||
});
|
||||
}
|
||||
|
||||
r.pop = function(){
|
||||
var id = this._deck.pop();/*
|
||||
var card = CardManager().getCardById(id);*/
|
||||
return id;
|
||||
}
|
||||
|
||||
/*r.find = function(key, val){
|
||||
var res = [];
|
||||
this.getDeck().forEach(function(id){
|
||||
var card = CardManager().getCardById(id);
|
||||
if(card.getProperty(key) == val){
|
||||
res.push(card);
|
||||
}
|
||||
});
|
||||
return res;
|
||||
}*/
|
||||
|
||||
r.removeFromDeck = function(id){
|
||||
var n = this.length();
|
||||
|
||||
for(var i = 0; i < n; i++) {
|
||||
var cardID = this.getDeck()[i];
|
||||
if(id == cardID){
|
||||
this.getDeck().splice(i, 1);
|
||||
return id;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
r.shuffle = function(){
|
||||
var deck = this.getDeck();
|
||||
|
||||
var n = this.length();
|
||||
for(var i = n - 1; i > 0; i--) {
|
||||
var j = (Math.random() * i) | 0;
|
||||
var tmp;
|
||||
|
||||
tmp = deck[j];
|
||||
deck[j] = deck[i];
|
||||
deck[i] = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
return Deck;
|
||||
})();
|
||||
|
||||
module.exports = Deck;
|
60
server/Hand.js
Normal file
60
server/Hand.js
Normal file
@@ -0,0 +1,60 @@
|
||||
/*var $ = require("jquery");*//*
|
||||
var CardManager = require("./CardManager");*//*
|
||||
var PubSub = require("./pubsub");*/
|
||||
|
||||
|
||||
var Hand = (function(){
|
||||
var Hand = function(){
|
||||
if(!(this instanceof Hand)){
|
||||
return (new Hand());
|
||||
}
|
||||
/**
|
||||
* constructor here
|
||||
*/
|
||||
|
||||
this._hand = [];
|
||||
};
|
||||
var r = Hand.prototype;
|
||||
/**
|
||||
* methods && properties here
|
||||
* r.property = null;
|
||||
* r.getProperty = function() {...}
|
||||
*/
|
||||
r._hand = null;
|
||||
|
||||
r.add = function(card){
|
||||
this._hand.push(card);
|
||||
}
|
||||
|
||||
r.getCards = function(){
|
||||
return this._hand;
|
||||
}
|
||||
|
||||
r.remove = function(id){
|
||||
var n = this.length();
|
||||
|
||||
for(var i = 0; i < n; i++) {
|
||||
if(this._hand[i].getId() != id) continue;
|
||||
return this._hand.splice(i, 1);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
r.getRandomCard = function(){
|
||||
var rnd = (Math.random() * this._hand.length) | 0;
|
||||
return this._hand[rnd];
|
||||
}
|
||||
|
||||
r.getLength = function(){
|
||||
return this._hand.length;
|
||||
}
|
||||
|
||||
r.length = function(){
|
||||
return this._hand.length;
|
||||
}
|
||||
|
||||
|
||||
return Hand;
|
||||
})();
|
||||
|
||||
module.exports = Hand;
|
@@ -1,4 +1,5 @@
|
||||
var shortid = require("shortid");
|
||||
var Battle = require("./Battle");
|
||||
|
||||
var Room = (function(){
|
||||
var Room = function(){
|
||||
@@ -11,6 +12,7 @@ var Room = (function(){
|
||||
|
||||
this._id = shortid.generate();
|
||||
this._room = [];
|
||||
this._ready = {};
|
||||
};
|
||||
var r = Room.prototype;
|
||||
/**
|
||||
@@ -21,37 +23,72 @@ var Room = (function(){
|
||||
r.MAX_USER = 2;
|
||||
r._room = null;
|
||||
r._id = null;
|
||||
r._battle = null;
|
||||
r._ready = null;
|
||||
|
||||
r.getID = function() {
|
||||
r.getID = function(){
|
||||
return this._id;
|
||||
}
|
||||
|
||||
r.join = function(user) {
|
||||
if(this._room.lenght >= 2) return;
|
||||
r.join = function(user){
|
||||
if(this._room.length >= 2) return;
|
||||
this._room.push(user);
|
||||
user.setRoom(this);/*
|
||||
user.socket.join(this._id);*/
|
||||
user.setRoom(this);
|
||||
user.joinRoom(this.getID());
|
||||
|
||||
if(!this.isOpen()) {
|
||||
this._room.forEach(function(user) {
|
||||
user.send("init:battle");
|
||||
})
|
||||
if(!this.isOpen()){
|
||||
this.initBattle();
|
||||
}
|
||||
}
|
||||
|
||||
r.isOpen = function() {
|
||||
r.isOpen = function(){
|
||||
return !(this._room.length >= 2);
|
||||
}
|
||||
|
||||
r.send = function(event, data) {
|
||||
r.send = function(event, data){
|
||||
io.to(this._id).emit(event, data);
|
||||
}
|
||||
|
||||
r.getPlayers = function() {
|
||||
r.getPlayers = function(){
|
||||
return this._room;
|
||||
}
|
||||
|
||||
r.initBattle = function(){
|
||||
var self = this;
|
||||
var side = 0;
|
||||
this._battle = Battle(this._id);/*
|
||||
this.setReady(this._room[0], false);
|
||||
this.setReady(this._room[1], false);*/
|
||||
this._room[0].send("init:battle", {side: "p1"});
|
||||
this._room[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()) {
|
||||
this._battle.init();
|
||||
}
|
||||
/*
|
||||
if(!this.checkIfReady()) return;
|
||||
|
||||
this._room[0].send("init:battle", {side: "p1"});
|
||||
this._room[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.checkIfReady = function(){
|
||||
for(var i = 0; i < this._room.length; i++) {
|
||||
if(!this._ready[this._room[i].getID()]) return false;
|
||||
}
|
||||
return true;
|
||||
}*/
|
||||
|
||||
|
||||
return Room;
|
||||
})();
|
||||
|
@@ -19,10 +19,10 @@ var Socket = (function(){
|
||||
* constructor here
|
||||
*/
|
||||
this.connections = Connections();
|
||||
/*
|
||||
this.matchmaker = Matchmaker(this.connections);
|
||||
*/
|
||||
this._roomCollection = [];
|
||||
/*
|
||||
this.matchmaker = Matchmaker(this.connections);
|
||||
*/
|
||||
this.roomCollection = {};
|
||||
app.listen(this.port);
|
||||
this.io = io;
|
||||
this._events();
|
||||
@@ -36,10 +36,10 @@ var Socket = (function(){
|
||||
r.io = null;
|
||||
r.port = 16918;
|
||||
r.connections = null;
|
||||
r._roomCollection = null;
|
||||
/*
|
||||
r.matchmaker = null;
|
||||
*/
|
||||
r.roomCollection = null;
|
||||
/*
|
||||
r.matchmaker = null;
|
||||
*/
|
||||
|
||||
r._events = function(){
|
||||
var self = this;
|
||||
@@ -48,13 +48,6 @@ var Socket = (function(){
|
||||
self.connections.add(user);
|
||||
console.log("new user ", user.getName());
|
||||
|
||||
/* 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);
|
||||
@@ -62,29 +55,37 @@ var Socket = (function(){
|
||||
socket.emit("response:name", {name: user.getName()});
|
||||
})
|
||||
|
||||
socket.on("request:createRoom", function() {
|
||||
socket.on("request:createRoom", function(){
|
||||
var room = Room();
|
||||
self._roomCollection.push(room);
|
||||
self.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() {
|
||||
socket.on("request:joinRoom", function(){
|
||||
console.log("joinroom");
|
||||
var interval = setInterval(function(){
|
||||
self._roomCollection.forEach(function(room) {
|
||||
/*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());
|
||||
}
|
||||
});
|
||||
});*/
|
||||
for(var key in self.roomCollection) {
|
||||
var room = self.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() {
|
||||
socket.on("request:roomData", function(){
|
||||
var room = user.getRoom();
|
||||
var players = room.getPlayers();
|
||||
user.send("response:roomData", {players: players});
|
||||
@@ -94,6 +95,10 @@ var Socket = (function(){
|
||||
self.connections.remove(user);
|
||||
user.disconnect();
|
||||
})
|
||||
|
||||
socket.on("request:gameLoaded", function(data){
|
||||
self.roomCollection[data._roomID].setReady(user);
|
||||
})
|
||||
});
|
||||
}
|
||||
return Socket;
|
||||
|
Reference in New Issue
Block a user