1
0
mirror of https://github.com/exane/not-gwent-online synced 2025-08-30 05:57:30 +00:00

much more stuff

This commit is contained in:
exane
2015-06-14 20:50:53 +02:00
parent f09a66f547
commit 246c27f990
8 changed files with 316 additions and 68 deletions

View File

@@ -1,5 +1,6 @@
var Battleside = require("./Battleside");
var PubSub = require("pubsub-js");
var Card = require("./Card");
var io = global.io;
@@ -28,14 +29,17 @@ var Battle = (function(){
r._user2 = null;
r.turn = 0;
r._id = null;
r.init = function(){
PubSub.subscribe("update", this.update.bind(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.setUpWeatherFieldWith(this.p2);
this.start();
}
@@ -46,12 +50,20 @@ var Battle = (function(){
this.p1.draw(10);
this.p2.draw(10);
this.p1.hand.add(Card("decoy"));
this.p1.hand.add(Card("impenetrable_fog"));
this.p2.hand.add(Card("decoy"));
this.p2.hand.add(Card("impenetrable_fog"));
this.update();
PubSub.subscribe("nextTurn", this.switchTurn.bind(this));
this.switchTurn();
}
r.switchTurn = function(){
r.switchTurn = function(__flag){
/*this.playerManager.renderInfos();
if(this.playerManager.bothPassed() && !this._roundCheck) {
//start new round
@@ -65,8 +77,15 @@ var Battle = (function(){
var entity = this.playerManager.getNextPlayer();
this.playerManager.renderInfos();*/
__flag = typeof __flag == "undefined" ? 0 : 1;
var side = this.turn++ % 2 ? this.p1 : this.p2;
if(side.isPassing()){
if(__flag) {
return this.startNextRound();
}
return this.switchTurn(1);
}
PubSub.publish("onEachTurn");
PubSub.publish("turn/" + side.getID());
@@ -74,6 +93,30 @@ var Battle = (function(){
}
r.startNextRound = function() {
}
r.update = function(){
this._update(this.p1);
this._update(this.p2);
}
r._update = function(p){
p.send("update:info", {
info: p.getInfo(),
leader: p.field[Card.TYPE.LEADER].get()[0]
})
p.send("update:hand", {
cards: JSON.stringify(p.hand.getCards())
});
p.send("update:fields", {
close: p.field[Card.TYPE.CLOSE_COMBAT],
ranged: p.field[Card.TYPE.RANGED],
siege: p.field[Card.TYPE.SIEGE],
weather: p.field[Card.TYPE.WEATHER]
})
}
r.send = function(event, data){
io.to(this._id).emit(event, data);

View File

@@ -7,7 +7,8 @@ var Field = require("./Field");
var PubSub = require("pubsub-js");
var Battleside = (function(){
var Battleside;
Battleside = (function(){
var Battleside = function(name, n, battle, user){
if(!(this instanceof Battleside)){
return (new Battleside(name, n, battle, user));
@@ -16,6 +17,7 @@ var Battleside = (function(){
* constructor here
*/
var self = this;
this._isWaiting = true;
this.socket = user.socket;
this.field = {};
@@ -30,6 +32,26 @@ var Battleside = (function(){
this.deck = Deck(DeckData["test"]);
this.receive("play:cardFromHand", function(data){
if(self._isWaiting) return;
if(self.isPassing()) return;
var cardID = data.id;
var card = self.hand.getCard(cardID);
self.playCard(card);
})
this.receive("decoy:replaceWith", function(data){
if(self._isWaiting) return;
var card = self.findCardOnFieldByID(data.cardID);
if(card === -1) throw "decoy:replace | unknown card";
PubSub.publish("decoy:replaceWith", card);
})
this.receive("set:passing", function() {
self.setPassing(true);
self.update();
PubSub.publish("nextTurn");
})
PubSub.subscribe("turn/" + this.getID(), this.onTurnStart.bind(this));
};
var r = Battleside.prototype;
@@ -48,6 +70,7 @@ var Battleside = (function(){
r._lives = 2;
r._score = 0;
r._isWaiting = null;
r._passing = null;
r.field = null;
@@ -59,12 +82,33 @@ var Battleside = (function(){
r.battle = null;
r.deck = null;
r.isPassing = function() {
return this._passing;
}
r.setUpWeatherFieldWith = function(p2){
this.field[Card.TYPE.WEATHER] = p2.field[Card.TYPE.WEATHER] = Field(Card.TYPE.WEATHER);
}
r.findCardOnFieldByID = function(id) {
for(var key in this.field) {
var field = this.field[key];
var card = field.getCard(id);
if(card !== -1) return card;
}
return -1;
}
r.setPassing = function(b) {
this._passing = b;
}
r.wait = function(){
this._isWaiting = true;
this.send("set:waiting", {waiting: this._isWaiting}, true);
}
r.turn = function() {
r.turn = function(){
this._isWaiting = false;
this.send("set:waiting", {waiting: this._isWaiting}, true);
}
@@ -77,7 +121,7 @@ var Battleside = (function(){
this.field[Card.TYPE.LEADER].add(leaderCard[0]);
}
r.getID = function() {
r.getID = function(){
return this.n;
}
@@ -92,7 +136,7 @@ var Battleside = (function(){
this.update();
}
r.calcScore = function() {
r.calcScore = function(){
var score = 0;
for(var key in this.field) {
score += +this.field[key].getScore();
@@ -101,12 +145,12 @@ var Battleside = (function(){
}
r.getInfo = function(){
console.log(this.getName(), this._isWaiting);
return {
name: this.getName(),
lives: this._lives,
score: this.calcScore(),
hand: this.hand.length()
hand: this.hand.length(),
passing: this._passing
}
}
@@ -119,56 +163,119 @@ var Battleside = (function(){
isPrivate = typeof isPrivate === "undefined" ? false : isPrivate;
msg._roomSide = this.n;
if(isPrivate) {
if(isPrivate){
return this.socket.emit(event, msg);
}
this.battle.send(event, msg);
}
r.receive = function(event, cb) {
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]
})
/*
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]
})*/
PubSub.publish("update");
}
r.onTurnStart = function() {
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);
//wait for cardplay event
self.playCard(card);
})
};
r.playCard = function(card) {
if(card === -1) return;
var field = this.field[card.getType()];
r.playCard = function(card){
if(card === null || card === -1) return;
field.add(card);
this.hand.remove(card);
if(!this.placeCard(card)) return;
this.update();
PubSub.publish("nextTurn");
}
r.placeCard = function(card){
var obj = {};
this.checkAbilities(card, obj);
if(obj._canclePlacement) return 0;
var field = obj.targetSide.field[card.getType()];
field.add(card);
PubSub.publish("onEachCardPlace");
this.checkAbilityOnAfterPlace(card);
return 1;
}
r.checkAbilities = function(card, obj){
var self = this;
obj.targetSide = this;
if(card.getAbility()){
var ability = card.getAbility();
if(ability.changeSide){
obj.targetSide = this.foe;
}
if(ability.replaceWith){
obj._canclePlacement = true;
var decoy = PubSub.subscribe("decoy:replaceWith", function(event, replaceCard){
if(replaceCard.getType() == Card.TYPE.LEADER ||
replaceCard.getType() == Card.TYPE.WEATHER ||
replaceCard.getType() == Card.TYPE.SPECIAL){
return;
}
PubSub.unsubscribe(decoy);
var field = self.field[replaceCard.getType()];
field.replaceWith(replaceCard, card);
self.hand.add(replaceCard);
self.update();
PubSub.publish("nextTurn");
})
}
if(ability.onEachTurn){
PubSub.subscribe("onEachTurn", ability.onEachTurn.bind(this, card));
}
if(ability.onEachCardPlace){
PubSub.subscribe("onEachCardPlace", ability.onEachCardPlace.bind(this, card));
}
}
}
r.checkAbilityOnAfterPlace = function(card){
var ability = card.getAbility();
if(ability){
if(ability.onAfterPlace){
ability.onAfterPlace.call(this, card)
}
}
}
return Battleside;
})();

View File

@@ -38,6 +38,7 @@ var Card = (function(){
WEATHER: 5
};
r._init = function(){
this._id = ++Card.__id;
}
@@ -46,15 +47,15 @@ var Card = (function(){
return this._data.name;
}
r.getPower = function(){
if(this._forcedPower > -1) {
if(this._forcedPower > -1){
return this._forcedPower + this._boost;
}
return this._data.power + this._boost;
}
r.setForcedPower = function(nr) {
r.setForcedPower = function(nr){
this._forcedPower = nr;
}
r.getRawAbility = function() {
r.getRawAbility = function(){
return this._data.ability;
}
r.getAbility = function(){
@@ -77,7 +78,8 @@ var Card = (function(){
return this._id;
}
r.boost = function(nr) {
r.boost = function(nr){
this.getPower(); //to recalculate this._power;
this._boost += nr;
}

View File

@@ -19,27 +19,49 @@ var Field = (function(){
r._cards = null;
r._score = 0;
r.add = function(card) {
r.add = function(card){
this._cards.push(card);
this.updateScore();
}
r.get = function() {
r.get = function(){
return this._cards;
}
r.getScore = function() {
r.getScore = function(){
this.updateScore();
return this._score;
}
r.updateScore = function() {
r.updateScore = function(){
this._score = 0;
for(var i=0; i<this._cards.length; i++) {
for(var i = 0; i < this._cards.length; i++) {
var card = this._cards[i];
this._score += card.getPower();
}
}
r.getPosition = function(card){
for(var i = 0; i < this._cards.length; i++) {
if(this._cards[i].getID() === card.getID()) return i;
}
return -1;
}
r.replaceWith = function(oldCard, newCard){
var index = this.getPosition(oldCard);
this._cards[index] = newCard;
return oldCard;
}
r.getCard = function(id){
for(var i = 0; i < this._cards.length; i++) {
var card = this._cards[i];
if(card.getID() == id) return card;
}
return -1;
}
return Field;
})();

View File

@@ -1,6 +1,7 @@
/*var $ = require("jquery");*//*
var CardManager = require("./CardManager");*//*
var PubSub = require("./pubsub");*/
var Card = require("./Card");
var Hand = (function(){
@@ -41,6 +42,8 @@ var Hand = (function(){
r.remove = function(id){
var n = this.length();
id = id instanceof Card ? id.getID() : id;
for(var i = 0; i < n; i++) {
if(this._hand[i].getID() != id) continue;
return this._hand.splice(i, 1);