mirror of
https://github.com/exane/not-gwent-online
synced 2025-09-22 14:49:06 +00:00
more stuff
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
var Battleside = require("./Battleside");
|
||||
var PubSub = require("pubsub-js");
|
||||
|
||||
var io = global.io;
|
||||
|
||||
@@ -31,25 +32,50 @@ var Battle = (function(){
|
||||
|
||||
|
||||
r.init = function(){
|
||||
this.p1 = Battleside(this._user1.getName(), 0, this);
|
||||
this.p2 = Battleside(this._user2.getName(), 1, this);
|
||||
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.send("update:info", {info: this.p1.getInfo()});
|
||||
this.p2.send("update:info", {info: this.p2.getInfo()});
|
||||
|
||||
this.start();
|
||||
}
|
||||
|
||||
r.start = function() {
|
||||
r.start = function(){
|
||||
this.p1.setLeadercard();
|
||||
this.p2.setLeadercard();
|
||||
this.p1.draw(10);
|
||||
this.p2.draw(10);
|
||||
|
||||
//this.p2.wait();
|
||||
PubSub.subscribe("nextTurn", this.switchTurn.bind(this));
|
||||
|
||||
this.switchTurn();
|
||||
}
|
||||
|
||||
r.send = function(event, data) {
|
||||
r.switchTurn = function(){
|
||||
/*this.playerManager.renderInfos();
|
||||
if(this.playerManager.bothPassed() && !this._roundCheck) {
|
||||
//start new round
|
||||
this._roundCheck = true;
|
||||
this.checkRound();
|
||||
return;
|
||||
}
|
||||
if(this.playerManager.bothPassed()) {
|
||||
return;
|
||||
}
|
||||
var entity = this.playerManager.getNextPlayer();
|
||||
|
||||
this.playerManager.renderInfos();*/
|
||||
var side = this.turn++ % 2 ? this.p1 : this.p2;
|
||||
|
||||
|
||||
PubSub.publish("onEachTurn");
|
||||
PubSub.publish("turn/" + side.getID());
|
||||
console.log("current Turn: ", side.getName());
|
||||
|
||||
}
|
||||
|
||||
|
||||
r.send = function(event, data){
|
||||
io.to(this._id).emit(event, data);
|
||||
}
|
||||
|
||||
|
@@ -1,24 +1,36 @@
|
||||
|
||||
var io = global.io;
|
||||
var DeckData = require("../assets/data/deck");
|
||||
var Deck = require("./Deck");
|
||||
var Hand = require("./Hand");
|
||||
var Card = require("./Card");
|
||||
var Field = require("./Field");
|
||||
var PubSub = require("pubsub-js");
|
||||
|
||||
|
||||
var Battleside = (function(){
|
||||
var Battleside = function(name, n, battle){
|
||||
var Battleside = function(name, n, battle, user){
|
||||
if(!(this instanceof Battleside)){
|
||||
return (new Battleside(name, n, battle));
|
||||
return (new Battleside(name, n, battle, user));
|
||||
}
|
||||
/**
|
||||
* constructor here
|
||||
*/
|
||||
|
||||
this._isWaiting = true;
|
||||
this.socket = user.socket;
|
||||
this.field = {};
|
||||
this.field[Card.TYPE.LEADER] = Field(Card.TYPE.LEADER);
|
||||
this.field[Card.TYPE.CLOSE_COMBAT] = Field(Card.TYPE.CLOSE_COMBAT);
|
||||
this.field[Card.TYPE.RANGED] = Field(Card.TYPE.RANGED);
|
||||
this.field[Card.TYPE.SIEGE] = Field(Card.TYPE.SIEGE);
|
||||
this.n = n ? "p2" : "p1";
|
||||
this._name = name;
|
||||
this.battle = battle;
|
||||
this.hand = Hand();
|
||||
this.deck = Deck(DeckData["test"]);
|
||||
|
||||
|
||||
PubSub.subscribe("turn/" + this.getID(), this.onTurnStart.bind(this));
|
||||
};
|
||||
var r = Battleside.prototype;
|
||||
/**
|
||||
@@ -28,21 +40,48 @@ var Battleside = (function(){
|
||||
*/
|
||||
r._name = null;
|
||||
r._discard = null;
|
||||
r._leader = null;
|
||||
r._close = null;
|
||||
/*r.leaderField = null;
|
||||
r.closeField = null;
|
||||
r._range = null;
|
||||
r._siege = null;
|
||||
r._field = null;
|
||||
r._field = null;*/
|
||||
r._lives = 2;
|
||||
r._score = 0;
|
||||
r._isWaiting = null;
|
||||
|
||||
r.field = null;
|
||||
|
||||
r.socket = null;
|
||||
r.n = null;
|
||||
|
||||
r.foe = null;
|
||||
r.hand = null;
|
||||
r.battle = null;
|
||||
r.deck = null;
|
||||
|
||||
r.wait = function(){
|
||||
this._isWaiting = true;
|
||||
this.send("set:waiting", {waiting: this._isWaiting}, true);
|
||||
}
|
||||
|
||||
r.draw = function(times) {
|
||||
r.turn = function() {
|
||||
this._isWaiting = false;
|
||||
this.send("set:waiting", {waiting: this._isWaiting}, true);
|
||||
}
|
||||
|
||||
r.setLeadercard = function(){
|
||||
var leaderCard = this.deck.find("type", Card.TYPE.LEADER);
|
||||
this.deck.removeFromDeck(leaderCard[0]);
|
||||
/*
|
||||
this.getYourside().setField("leader", leaderCard[0]);*/
|
||||
this.field[Card.TYPE.LEADER].add(leaderCard[0]);
|
||||
}
|
||||
|
||||
r.getID = function() {
|
||||
return this.n;
|
||||
}
|
||||
|
||||
r.draw = function(times){
|
||||
while(times--) {
|
||||
var card = this.deck.draw();
|
||||
this.hand.add(card);
|
||||
@@ -50,28 +89,85 @@ var Battleside = (function(){
|
||||
|
||||
console.log("update:hand fired");
|
||||
|
||||
this.send("update:hand", {cards: JSON.stringify(this.hand.getCards())});
|
||||
this.update();
|
||||
}
|
||||
|
||||
r.getInfo = function() {
|
||||
r.calcScore = function() {
|
||||
var score = 0;
|
||||
for(var key in this.field) {
|
||||
score += +this.field[key].getScore();
|
||||
}
|
||||
return this._score = score;
|
||||
}
|
||||
|
||||
r.getInfo = function(){
|
||||
console.log(this.getName(), this._isWaiting);
|
||||
return {
|
||||
name: this.getName(),
|
||||
lives: this._lives,
|
||||
score: this._score,
|
||||
score: this.calcScore(),
|
||||
hand: this.hand.length()
|
||||
}
|
||||
}
|
||||
|
||||
r.getName = function() {
|
||||
r.getName = function(){
|
||||
return this._name;
|
||||
}
|
||||
|
||||
r.send = function(event, msg) {
|
||||
r.send = function(event, msg, isPrivate){
|
||||
msg = msg || {};
|
||||
isPrivate = typeof isPrivate === "undefined" ? false : isPrivate;
|
||||
msg._roomSide = this.n;
|
||||
|
||||
if(isPrivate) {
|
||||
return this.socket.emit(event, msg);
|
||||
}
|
||||
this.battle.send(event, msg);
|
||||
}
|
||||
|
||||
r.receive = function(event, cb) {
|
||||
this.socket.on(event, cb);
|
||||
}
|
||||
|
||||
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]
|
||||
})
|
||||
}
|
||||
|
||||
r.onTurnStart = function() {
|
||||
this.foe.wait();
|
||||
this.turn();
|
||||
var self = this;
|
||||
|
||||
this.receive("play:cardFromHand", function(data) {
|
||||
var cardID = data.id;
|
||||
var card = self.hand.getCard(cardID);
|
||||
self.hand.remove(cardID);
|
||||
|
||||
self.playCard(card);
|
||||
})
|
||||
};
|
||||
|
||||
r.playCard = function(card) {
|
||||
if(card === -1) return;
|
||||
var field = this.field[card.getType()];
|
||||
|
||||
field.add(card);
|
||||
|
||||
this.update();
|
||||
|
||||
PubSub.publish("nextTurn");
|
||||
}
|
||||
|
||||
|
||||
return Battleside;
|
||||
|
@@ -73,7 +73,7 @@ var Card = (function(){
|
||||
return this._key;
|
||||
}
|
||||
|
||||
r.getId = function(){
|
||||
r.getID = function(){
|
||||
return this._id;
|
||||
}
|
||||
|
||||
|
@@ -36,7 +36,7 @@ var Deck = (function(){
|
||||
return this._deck.length;
|
||||
}
|
||||
|
||||
r.length = function() {
|
||||
r.length = function(){
|
||||
return this.getLength();
|
||||
}
|
||||
|
||||
@@ -50,45 +50,50 @@ var Deck = (function(){
|
||||
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(){
|
||||
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) {
|
||||
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);*/
|
||||
var id = this._deck.pop();
|
||||
/*
|
||||
var card = CardManager().getCardById(id);*/
|
||||
return id;
|
||||
}
|
||||
|
||||
/*r.find = function(key, val){
|
||||
r.find = function(key, val){
|
||||
var res = [];
|
||||
this.getDeck().forEach(function(id){
|
||||
var card = CardManager().getCardById(id);
|
||||
this.getDeck().forEach(function(card){
|
||||
if(card.getProperty(key) == val){
|
||||
res.push(card);
|
||||
}
|
||||
|
||||
});
|
||||
return res;
|
||||
}*/
|
||||
}
|
||||
|
||||
r.removeFromDeck = function(id){
|
||||
r.removeFromDeck = function(card){
|
||||
var n = this.length();
|
||||
|
||||
for(var i = 0; i < n; i++) {
|
||||
var cardID = this.getDeck()[i];
|
||||
/*var cardID = this.getDeck()[i];
|
||||
if(id == cardID){
|
||||
this.getDeck().splice(i, 1);
|
||||
return id;
|
||||
}*/
|
||||
var c = this.getDeck()[i];
|
||||
if(c.getID() === card.getID()){
|
||||
return this.getDeck().splice(i, 1);
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
|
47
server/Field.js
Normal file
47
server/Field.js
Normal file
@@ -0,0 +1,47 @@
|
||||
var Field = (function(){
|
||||
var Field = function(){
|
||||
if(!(this instanceof Field)){
|
||||
return (new Field());
|
||||
}
|
||||
/**
|
||||
* constructor here
|
||||
*/
|
||||
|
||||
this._cards = [];
|
||||
};
|
||||
var r = Field.prototype;
|
||||
/**
|
||||
* methods && properties here
|
||||
* r.property = null;
|
||||
* r.getProperty = function() {...}
|
||||
*/
|
||||
|
||||
r._cards = null;
|
||||
r._score = 0;
|
||||
|
||||
r.add = function(card) {
|
||||
this._cards.push(card);
|
||||
this.updateScore();
|
||||
}
|
||||
|
||||
r.get = function() {
|
||||
return this._cards;
|
||||
}
|
||||
|
||||
r.getScore = function() {
|
||||
return this._score;
|
||||
}
|
||||
|
||||
r.updateScore = function() {
|
||||
this._score = 0;
|
||||
for(var i=0; i<this._cards.length; i++) {
|
||||
var card = this._cards[i];
|
||||
this._score += card.getPower();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return Field;
|
||||
})();
|
||||
|
||||
module.exports = Field;
|
@@ -30,11 +30,19 @@ var Hand = (function(){
|
||||
return this._hand;
|
||||
}
|
||||
|
||||
r.getCard = function(id) {
|
||||
for(var i=0; i< this.length(); i++) {
|
||||
var card = this.getCards()[i];
|
||||
if(card.getID() === id) return card;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
r.remove = function(id){
|
||||
var n = this.length();
|
||||
|
||||
for(var i = 0; i < n; i++) {
|
||||
if(this._hand[i].getId() != id) continue;
|
||||
if(this._hand[i].getID() != id) continue;
|
||||
return this._hand.splice(i, 1);
|
||||
}
|
||||
return -1;
|
||||
|
Reference in New Issue
Block a user