From dbf1b07c1f82c8bba967e964f171afd4d41c3395 Mon Sep 17 00:00:00 2001 From: exane Date: Wed, 17 Jun 2015 21:18:14 +0200 Subject: [PATCH] more improvements. now with gameplay --- assets/data/abilities.js | 7 ++-- assets/data/cards.js | 6 ++-- package.json | 6 ++-- public/js/client.js | 4 +++ server/Battle.js | 77 ++++++++++++++++++++++++++-------------- server/Battleside.js | 58 +++++++++++++++++++++++------- server/Card.js | 13 ++++++- server/Field.js | 5 +++ 8 files changed, 127 insertions(+), 49 deletions(-) diff --git a/assets/data/abilities.js b/assets/data/abilities.js index f7698e9..12ac982 100644 --- a/assets/data/abilities.js +++ b/assets/data/abilities.js @@ -7,12 +7,13 @@ module.exports = { }, "morale_boost": { - onAfterPlace: function(card) { + onAfterPlace: function(card) { var field = this.field[card.getType()]; var cards = field.get(); cards.forEach(function(_card) { if(_card.getID() == card.getID()) return; + if(_card.getRawPower() === -1) return; _card.boost(1); }) } @@ -50,7 +51,8 @@ module.exports = { } }, "weather_fog": { - onEachTurn: function(card) { + onEachTurn: function(args) { + card = args[0] var targetRow = card.constructor.TYPE.RANGED; var forcedPower = 1; var field1 = this.field[targetRow].get(); @@ -64,6 +66,7 @@ module.exports = { }); }, onEachCardPlace: function(card) { + card = args[0] var targetRow = card.constructor.TYPE.RANGED; var forcedPower = 1; var field1 = this.field[targetRow].get(); diff --git a/assets/data/cards.js b/assets/data/cards.js index 3ac689b..916eab3 100644 --- a/assets/data/cards.js +++ b/assets/data/cards.js @@ -181,7 +181,7 @@ module.exports = { }, "foltest_king_of_temeria": { name: "Foltest: King of Temeria", - power: 0, + power: -1, ability: "foltest_leader1", img: "foltest_king", faction: "Northern Realm", @@ -189,7 +189,7 @@ module.exports = { }, "decoy": { name: "Decoy", - power: 0, + power: -1, ability: "decoy", img: "decoy", faction: null, @@ -197,7 +197,7 @@ module.exports = { }, "impenetrable_fog": { name: "Impenetrable Fog", - power: 0, + power: -1, ability: "weather_fog", img: "fog", faction: null, diff --git a/package.json b/package.json index f00b072..cbb8435 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "description": "", "main": "gulpfile.js", "dependencies": { - "socket.io": "^1.3.5" + "socketcluster": "2.2.x", + "socketcluster-client": "2.2.x" }, "devDependencies": { "babelify": "^6.1.2", @@ -15,14 +16,11 @@ "handlebars": "^3.0.3", "jquery": "^2.1.4", "promise": "^7.0.1", - "pubsub-js": "^1.5.2", "shortid": "^2.2.2", "connect": "3.0.1", "express": "4.12.3", "minimist": "1.1.0", "serve-static": "1.8.0", - "socketcluster": "2.2.x", - "socketcluster-client": "2.2.x", "vinyl-source-stream": "^1.1.0" }, "scripts": { diff --git a/public/js/client.js b/public/js/client.js index 1bf1ea9..0f797ca 100644 --- a/public/js/client.js +++ b/public/js/client.js @@ -432,6 +432,10 @@ var User = Backbone.Model.extend({ var waiting = data.waiting; self.set("waiting", waiting); }) + app.receive("set:passing", function(data){ + var passing = data.passing; + self.set("passing", passing); + }) app.on("createRoom", this.createRoom, this); diff --git a/server/Battle.js b/server/Battle.js index ed85ebf..6e3931d 100644 --- a/server/Battle.js +++ b/server/Battle.js @@ -1,8 +1,6 @@ var Battleside = require("./Battleside"); -//var PubSub = require("pubsub-js"); var Card = require("./Card"); -/*var io = global.io;*/ var Battle = (function(){ var Battle = function(id, p1, p2, socket){ @@ -72,31 +70,21 @@ var Battle = (function(){ /*PubSub.subscribe("nextTurn", this.switchTurn.bind(this));*/ this.on("NextTurn", this.switchTurn); - this.switchTurn(); + this.switchTurn(Math.random() > .5 ? this.p1 : this.p2); } - r.switchTurn = function(__flag){ - /*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();*/ + r.switchTurn = function(side, __flag){ __flag = typeof __flag == "undefined" ? 0 : 1; - var side = this.turn++ % 2 ? this.p1 : this.p2; + //var side = this.turn++ % 2 ? this.p1 : this.p2; + /*if(__flag instanceof Battleside) { + side = __flag; + }*/ if(side.isPassing()){ if(__flag){ return this.startNextRound(); } - return this.switchTurn(1); + return this.switchTurn(side.foe, 1); } /*PubSub.publish("onEachTurn");*/ @@ -109,7 +97,19 @@ var Battle = (function(){ } r.startNextRound = function(){ + var loser = this.checkRubies(); + if(this.checkIfIsOver()){ + console.log("its over!"); + return; + } + this.p1.resetNewRound(); + this.p2.resetNewRound(); + + console.log("start new round!"); + + this.update(); + this.switchTurn(loser); } r.update = function(){ @@ -140,17 +140,18 @@ var Battle = (function(){ }); } - r.runEvent = function(eventid, target, args){ - target = target || this; + r.runEvent = function(eventid, ctx, args){ + ctx = ctx || this; args = args || []; var event = "on" + eventid; - if(!this.events["on" + eventid]){ + if(!this.events[event]){ return; } this.events[event].forEach(function(e){ - e.apply(target, args); + e.apply(ctx, args); }); + this.update(); } r.on = function(eventid, cb, ctx, args){ @@ -160,9 +161,9 @@ var Battle = (function(){ if(!(event in this.events)){ this.events[event] = []; } - /*if(!this.events[event]) { - this.events[event] = []; - }*/ + if(typeof cb !== "function"){ + throw new Error("cb not a function"); + } if(args){ this.events[event].push(cb.bind(ctx, args)); } @@ -179,6 +180,30 @@ var Battle = (function(){ delete this.events[event]; } + r.checkIfIsOver = function(){ + return !(this.p1.getRubies() && this.p2.getRubies()); + } + + + r.checkRubies = function(){ + var scoreP1 = this.p1.getScore(); + var scoreP2 = this.p2.getScore(); + + if(scoreP1 > scoreP2){ + this.p2.removeRuby(); + return this.p2; + } + if(scoreP2 > scoreP1){ + this.p1.removeRuby(); + return this.p1; + } + + //tie + this.p1.removeRuby(); + this.p2.removeRuby(); + return 0; + } + return Battle; })(); diff --git a/server/Battleside.js b/server/Battleside.js index 2ba606b..5360f04 100644 --- a/server/Battleside.js +++ b/server/Battleside.js @@ -30,6 +30,7 @@ Battleside = (function(){ this.battle = battle; this.hand = Hand(); this.deck = Deck(DeckData["test"]); + this._discard = []; this.runEvent = this.battle.runEvent.bind(this.battle); this.on = this.battle.on.bind(this.battle); @@ -48,14 +49,13 @@ Battleside = (function(){ if(self._isWaiting) return; var card = self.findCardOnFieldByID(data.cardID); if(card === -1) throw new Error("decoy:replace | unknown card"); - /*PubSub.publish("decoy:replaceWith", card);*/ self.runEvent("Decoy:replaceWith", self, [card]); }) this.receive("set:passing", function() { self.setPassing(true); self.update();/* PubSub.publish("nextTurn");*/ - self.runEvent("NextTurn"); + self.runEvent("NextTurn", null, [self.foe]); }) /*PubSub.subscribe("turn/" + this.getID(), this.onTurnStart.bind(this));*/ @@ -74,7 +74,7 @@ Battleside = (function(){ r._range = null; r._siege = null; r._field = null;*/ - r._lives = 2; + r._rubies = 2; r._score = 0; r._isWaiting = null; r._passing = null; @@ -108,6 +108,7 @@ Battleside = (function(){ r.setPassing = function(b) { this._passing = b; + this.send("set:passing", {passing: this._passing}, true); } r.wait = function(){ @@ -154,13 +155,25 @@ Battleside = (function(){ r.getInfo = function(){ return { name: this.getName(), - lives: this._lives, + lives: this._rubies, score: this.calcScore(), hand: this.hand.length(), passing: this._passing } } + r.getRubies = function() { + return this._rubies; + } + + r.getScore = function() { + return +this.calcScore(); + } + + r.removeRuby = function() { + this._rubies--; + } + r.getName = function(){ return this._name; } @@ -203,8 +216,8 @@ Battleside = (function(){ this.update(); - //PubSub.publish("nextTurn"); - this.runEvent("NextTurn"); + + this.runEvent("NextTurn", this, [this.foe]); } r.placeCard = function(card){ @@ -221,6 +234,8 @@ Battleside = (function(){ this.checkAbilityOnAfterPlace(card); + this.update(); + return 1; } @@ -236,29 +251,26 @@ Battleside = (function(){ if(ability.replaceWith){ obj._canclePlacement = true; - //var decoy = PubSub.subscribe("decoy:replaceWith", function(event, replaceCard){ this.on("Decoy:replaceWith", function(replaceCard) { if(replaceCard.getType() == Card.TYPE.LEADER || replaceCard.getType() == Card.TYPE.WEATHER || replaceCard.getType() == Card.TYPE.SPECIAL){ return; } - /*PubSub.unsubscribe(decoy);*/ + if(replaceCard.getName() === card.getName()) return; self.off("Decoy:replaceWith"); var field = self.field[replaceCard.getType()]; field.replaceWith(replaceCard, card); self.hand.add(replaceCard); +/* + self.update();*/ - self.update(); - - /*PubSub.publish("nextTurn");*/ - self.runEvent("NextTurn"); + self.runEvent("NextTurn", null, [self.foe]); }) } if(ability.onEachTurn){ - //PubSub.subscribe("onEachTurn", ability.onEachTurn.bind(this, card)); this.on("EachTurn", ability.onEachTurn, this, [card]) } if(ability.onEachCardPlace){ @@ -278,6 +290,26 @@ Battleside = (function(){ } } + r.clearMainFields = function() { + var cards1 = this.field[Card.TYPE.CLOSE_COMBAT].removeAll(); + var cards2 = this.field[Card.TYPE.RANGED].removeAll(); + var cards3 = this.field[Card.TYPE.SIEGE].removeAll(); + + var cards = cards1.concat(cards2.concat(cards3)); + this.addToDiscard(cards); + } + + r.addToDiscard = function(cards) { + var self = this; + cards.forEach(function(card) { + self._discard.push(card); + }); + } + + r.resetNewRound = function() { + this.clearMainFields(); + this.setPassing(false); + } return Battleside; })(); diff --git a/server/Card.js b/server/Card.js index 0941a00..dca0d1b 100644 --- a/server/Card.js +++ b/server/Card.js @@ -50,11 +50,22 @@ var Card = (function(){ return this._data.name; } r.getPower = function(){ + if(this._data.power === -1) return 0; if(this._forcedPower > -1){ return this._forcedPower + this._boost; } return this._data.power + this._boost; } + r.getRawPower = function() { + return this._data.power; + } + r.calculateBoost = function() { + this._boost = 0; + for (var key in this._boosts) { + var boost = this._boosts[key]; + this.boost(boost.getPower()); + } + } r.setForcedPower = function(nr){ this._forcedPower = nr; } @@ -82,7 +93,7 @@ var Card = (function(){ } r.boost = function(nr){ - this.getPower(); //to recalculate this._power; + /*this.getPower(); //to recalculate this._power;*/ this._boost += nr; } diff --git a/server/Field.js b/server/Field.js index 1acb3ae..1c6d6f2 100644 --- a/server/Field.js +++ b/server/Field.js @@ -62,6 +62,11 @@ var Field = (function(){ return -1; } + r.removeAll = function() { + var tmp = this._cards.slice(); + this._cards = []; + return tmp; + } return Field; })();