2015-06-24 16:22:56 +00:00
|
|
|
var socket = require("socket.io-client");
|
2015-06-13 19:36:02 +00:00
|
|
|
var Backbone = require("backbone");
|
2015-06-18 13:06:13 +00:00
|
|
|
require("./backbone.modal-min");
|
2015-06-25 15:51:21 +00:00
|
|
|
var Handlebars = require('handlebars/runtime').default;
|
2015-06-13 19:36:02 +00:00
|
|
|
var $ = require("jquery");
|
|
|
|
|
2015-06-13 22:33:20 +00:00
|
|
|
window.$ = $;
|
|
|
|
|
2015-06-25 15:51:21 +00:00
|
|
|
Handlebars.registerPartial("card", require("../templates/cards.handlebars"));
|
|
|
|
Handlebars.registerHelper("health", function(lives){
|
|
|
|
var out = "";
|
|
|
|
|
|
|
|
for(var i = 0; i < 2; i++) {
|
|
|
|
out += "<i";
|
|
|
|
if(i < lives){
|
|
|
|
out += " class='ruby'";
|
|
|
|
}
|
|
|
|
out += "></i>";
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
});
|
|
|
|
|
2015-06-13 19:36:02 +00:00
|
|
|
var App = Backbone.Router.extend({
|
|
|
|
routes: {
|
2015-06-25 15:51:21 +00:00
|
|
|
/*"lobby": "lobbyRoute",
|
2015-06-13 19:36:02 +00:00
|
|
|
"battle": "battleRoute",
|
2015-06-25 15:51:21 +00:00
|
|
|
"*path": "defaultRoute"*/
|
2015-06-13 19:36:02 +00:00
|
|
|
},
|
|
|
|
initialize: function(){
|
|
|
|
var self = this;
|
|
|
|
this.connect();
|
|
|
|
this.user = new User({app: this});
|
|
|
|
|
2015-06-25 15:51:21 +00:00
|
|
|
/*Backbone.history.start();*/
|
|
|
|
this.lobbyRoute();
|
2015-06-13 19:36:02 +00:00
|
|
|
},
|
|
|
|
connect: function(){
|
2015-06-24 16:22:56 +00:00
|
|
|
this.socket = socket(Config.Server.hostname + ":" + Config.Server.port);
|
2015-06-13 19:36:02 +00:00
|
|
|
},
|
|
|
|
receive: function(event, cb){
|
|
|
|
this.socket.on(event, cb);
|
2015-06-15 19:03:12 +00:00
|
|
|
}, /*
|
2015-06-13 19:36:02 +00:00
|
|
|
receiveOnce: function(event, cb){
|
|
|
|
this.socket.once(event, cb);
|
2015-06-15 19:03:12 +00:00
|
|
|
},*/
|
2015-06-13 19:36:02 +00:00
|
|
|
send: function(event, data){
|
|
|
|
data = data || null;
|
|
|
|
var socket = this.socket;
|
|
|
|
|
|
|
|
if(!data){
|
|
|
|
socket.emit(event);
|
|
|
|
}
|
|
|
|
if(data){
|
|
|
|
socket.emit(event, data);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
lobbyRoute: function(){
|
|
|
|
if(this.currentView){
|
|
|
|
this.currentView.remove();
|
|
|
|
}
|
|
|
|
this.currentView = new Lobby({
|
|
|
|
app: this,
|
|
|
|
user: this.user
|
|
|
|
});
|
|
|
|
},
|
|
|
|
battleRoute: function(){
|
|
|
|
if(this.currentView){
|
|
|
|
this.currentView.remove();
|
|
|
|
}
|
|
|
|
this.currentView = new BattleView({
|
|
|
|
app: this,
|
|
|
|
user: this.user
|
|
|
|
});
|
|
|
|
},
|
|
|
|
defaultRoute: function(path){
|
|
|
|
this.navigate("lobby", {trigger: true});
|
|
|
|
},
|
|
|
|
parseEvent: function(event){
|
|
|
|
var regex = /(\w+):?(\w*)\|?/g;
|
|
|
|
var res = {};
|
|
|
|
var r;
|
|
|
|
while(r = regex.exec(event)) {
|
|
|
|
res[r[1]] = r[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-06-13 22:33:20 +00:00
|
|
|
var SideView = Backbone.View.extend({
|
|
|
|
el: ".container",
|
2015-06-22 15:29:01 +00:00
|
|
|
template: require("../templates/cards.handlebars"),
|
2015-06-22 18:19:06 +00:00
|
|
|
templateCards: require("../templates/fieldCards.handlebars"),
|
2015-06-25 15:51:21 +00:00
|
|
|
templateInfo: require("../templates/info.handlebars"),
|
2015-06-13 22:33:20 +00:00
|
|
|
initialize: function(options){
|
|
|
|
var self = this;
|
|
|
|
this.side = options.side;
|
|
|
|
this.app = options.app;
|
|
|
|
this.battleView = options.battleView;
|
|
|
|
this.infoData = this.infoData || {};
|
2015-06-14 14:01:25 +00:00
|
|
|
this.leader = this.leader || {};
|
|
|
|
this.field = this.field || {};
|
2015-06-13 22:33:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
render: function(){
|
2015-06-14 14:01:25 +00:00
|
|
|
this.renderInfo();
|
|
|
|
this.renderCloseField();
|
|
|
|
this.renderRangeField();
|
|
|
|
this.renderSiegeField();
|
2015-06-14 18:50:53 +00:00
|
|
|
this.renderWeatherField();
|
2015-06-14 14:01:25 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
renderInfo: function(){
|
2015-06-13 22:33:20 +00:00
|
|
|
var d = this.infoData;
|
2015-06-14 14:01:25 +00:00
|
|
|
var l = this.leader;
|
2015-06-25 15:51:21 +00:00
|
|
|
var html = this.templateInfo({
|
|
|
|
data: d,
|
|
|
|
leader: l,
|
|
|
|
passBtn: this.side === ".player"
|
|
|
|
})
|
|
|
|
|
|
|
|
this.$info = this.$el.find(".game-info" + this.side).html(html);
|
|
|
|
|
2015-06-14 14:01:25 +00:00
|
|
|
|
|
|
|
if(this.app.user.get("waiting") && this.side === ".player"){
|
|
|
|
this.$info.addClass("removeBackground");
|
|
|
|
}
|
|
|
|
if(!this.app.user.get("waiting") && this.side === ".foe"){
|
|
|
|
this.$info.addClass("removeBackground");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
renderCloseField: function(){
|
|
|
|
if(!this.field.close) return;
|
|
|
|
this.$fields = this.$el.find(".battleside" + this.side);
|
|
|
|
var $field = this.$fields.find(".field-close").parent();
|
2015-06-21 14:50:50 +00:00
|
|
|
var cards = this.field.close.cards;
|
|
|
|
var score = this.field.close.score;
|
|
|
|
var horn = this.field.close.horn;
|
2015-06-14 14:01:25 +00:00
|
|
|
|
2015-06-20 11:08:11 +00:00
|
|
|
|
2015-06-14 14:01:25 +00:00
|
|
|
var html = this.templateCards(cards);
|
|
|
|
|
|
|
|
$field.find(".field-close").html(html)
|
|
|
|
$field.find(".large-field-counter").html(score)
|
2015-06-21 14:50:50 +00:00
|
|
|
if(horn){
|
|
|
|
this.$fields.find(".field-horn-close").html(this.template(horn));
|
|
|
|
}
|
2015-06-20 11:08:11 +00:00
|
|
|
|
2015-06-25 15:51:21 +00:00
|
|
|
calculateCardMargin($field.find(".card"), 424, 70, cards.length);
|
2015-06-14 14:01:25 +00:00
|
|
|
},
|
|
|
|
renderRangeField: function(){
|
|
|
|
if(!this.field.ranged) return;
|
|
|
|
this.$fields = this.$el.find(".battleside" + this.side);
|
|
|
|
var $field = this.$fields.find(".field-range").parent();
|
2015-06-21 14:50:50 +00:00
|
|
|
var cards = this.field.ranged.cards;
|
|
|
|
var score = this.field.ranged.score;
|
|
|
|
var horn = this.field.ranged.horn;
|
2015-06-14 14:01:25 +00:00
|
|
|
|
|
|
|
var html = this.templateCards(cards);
|
|
|
|
|
|
|
|
$field.find(".field-range").html(html)
|
|
|
|
$field.find(".large-field-counter").html(score)
|
2015-06-21 14:50:50 +00:00
|
|
|
if(horn){
|
|
|
|
this.$fields.find(".field-horn-range").html(this.template(horn));
|
|
|
|
}
|
|
|
|
|
2015-06-25 15:51:21 +00:00
|
|
|
calculateCardMargin($field.find(".card"), 424, 70, cards.length);
|
2015-06-14 14:01:25 +00:00
|
|
|
},
|
|
|
|
renderSiegeField: function(){
|
|
|
|
if(!this.field.siege) return;
|
|
|
|
this.$fields = this.$el.find(".battleside" + this.side);
|
|
|
|
var $field = this.$fields.find(".field-siege").parent();
|
2015-06-21 14:50:50 +00:00
|
|
|
var cards = this.field.siege.cards;
|
|
|
|
var score = this.field.siege.score;
|
|
|
|
var horn = this.field.siege.horn;
|
2015-06-14 14:01:25 +00:00
|
|
|
|
|
|
|
var html = this.templateCards(cards);
|
|
|
|
|
|
|
|
$field.find(".field-siege").html(html)
|
|
|
|
$field.find(".large-field-counter").html(score)
|
2015-06-21 14:50:50 +00:00
|
|
|
if(horn){
|
|
|
|
this.$fields.find(".field-horn-siege").html(this.template(horn));
|
|
|
|
}
|
|
|
|
|
2015-06-25 15:51:21 +00:00
|
|
|
calculateCardMargin($field.find(".card"), 424, 70, cards.length);
|
2015-06-13 22:33:20 +00:00
|
|
|
},
|
2015-06-14 18:50:53 +00:00
|
|
|
renderWeatherField: function(){
|
|
|
|
if(!this.field.weather) return;
|
|
|
|
var $weatherField = this.$el.find(".field-weather");
|
2015-06-21 14:50:50 +00:00
|
|
|
var cards = this.field.weather.cards;
|
2015-06-14 18:50:53 +00:00
|
|
|
$weatherField.html(this.templateCards(cards));
|
|
|
|
|
|
|
|
return this;
|
2015-06-25 15:51:21 +00:00
|
|
|
}
|
|
|
|
/*,
|
2015-06-13 22:33:20 +00:00
|
|
|
lives: function(lives){
|
|
|
|
var out = "";
|
|
|
|
for(var i = 0; i < 2; i++) {
|
|
|
|
out += "<i";
|
|
|
|
if(i < lives){
|
|
|
|
out += " class='ruby'";
|
|
|
|
}
|
|
|
|
out += "></i>";
|
|
|
|
}
|
|
|
|
return out;
|
2015-06-25 15:51:21 +00:00
|
|
|
}*/
|
2015-06-13 22:33:20 +00:00
|
|
|
});
|
|
|
|
|
2015-06-20 11:08:11 +00:00
|
|
|
var calculateCardMargin = function($selector, totalWidth, cardWidth, n){
|
|
|
|
var w = totalWidth, c = cardWidth;
|
|
|
|
var res;
|
2015-06-21 14:50:50 +00:00
|
|
|
if(n < 6)
|
2015-06-20 11:08:11 +00:00
|
|
|
res = 0;
|
|
|
|
else {
|
|
|
|
res = -((w - c) / (n - 1) - c) + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
$selector.css("margin-left", -res);
|
|
|
|
}
|
|
|
|
|
2015-06-13 19:36:02 +00:00
|
|
|
var BattleView = Backbone.View.extend({
|
|
|
|
className: "container",
|
2015-06-22 15:29:01 +00:00
|
|
|
template: require("../templates/battle.handlebars"),
|
|
|
|
templatePreview: require("../templates/preview.handlebars"),
|
2015-06-13 19:36:02 +00:00
|
|
|
initialize: function(options){
|
|
|
|
var self = this;
|
|
|
|
var user = this.user = options.user;
|
|
|
|
var app = this.app = options.app;
|
2015-06-13 22:33:20 +00:00
|
|
|
var yourSide, otherSide;
|
|
|
|
|
2015-06-13 19:36:02 +00:00
|
|
|
$(this.el).prependTo('body');
|
|
|
|
|
2015-06-18 13:06:13 +00:00
|
|
|
this.listenTo(user, "change:showPreview", this.renderPreview);
|
2015-06-14 14:01:25 +00:00
|
|
|
this.listenTo(user, "change:waiting", this.render);
|
2015-06-14 18:50:53 +00:00
|
|
|
this.listenTo(user, "change:passing", this.render);
|
2015-06-18 13:06:13 +00:00
|
|
|
this.listenTo(user, "change:openDiscard", this.render);
|
2015-06-19 15:15:26 +00:00
|
|
|
this.listenTo(user, "change:setAgile", this.render);
|
2015-06-21 14:50:50 +00:00
|
|
|
this.listenTo(user, "change:setHorn", this.render);
|
2015-06-23 16:12:11 +00:00
|
|
|
this.listenTo(user, "change:isReDrawing", this.render);
|
|
|
|
/*this.listenTo(user, "change:handCards", this.render);*/
|
2015-06-13 20:04:44 +00:00
|
|
|
|
2015-06-13 19:36:02 +00:00
|
|
|
this.$hand = this.$el.find(".field-hand");
|
2015-06-13 20:04:44 +00:00
|
|
|
this.$preview = this.$el.find(".card-preview");
|
2015-06-13 19:36:02 +00:00
|
|
|
|
2015-06-13 22:33:20 +00:00
|
|
|
|
2015-06-13 19:36:02 +00:00
|
|
|
var interval = setInterval(function(){
|
|
|
|
if(!user.get("room")) return;
|
2015-06-24 16:22:56 +00:00
|
|
|
this.setUpBattleEvents();
|
2015-06-13 19:36:02 +00:00
|
|
|
this.app.send("request:gameLoaded", {_roomID: user.get("room")});
|
|
|
|
clearInterval(interval);
|
2015-06-15 19:03:12 +00:00
|
|
|
}.bind(this), 10);
|
2015-06-13 22:33:20 +00:00
|
|
|
|
2015-06-13 19:36:02 +00:00
|
|
|
this.render();
|
2015-06-13 22:33:20 +00:00
|
|
|
|
2015-06-25 15:51:21 +00:00
|
|
|
this.yourSide = new SideView({side: ".player", app: this.app, battleView: this});
|
|
|
|
this.otherSide = new SideView({side: ".foe", app: this.app, battleView: this});
|
2015-06-13 22:33:20 +00:00
|
|
|
|
2015-06-13 19:36:02 +00:00
|
|
|
},
|
2015-06-13 20:04:44 +00:00
|
|
|
events: {
|
|
|
|
"mouseover .card": "onMouseover",
|
2015-06-14 14:01:25 +00:00
|
|
|
"mouseleave .card": "onMouseleave",
|
2015-06-14 18:50:53 +00:00
|
|
|
"click .field-hand": "onClick",
|
2015-06-19 15:15:26 +00:00
|
|
|
"click .battleside.player": "onClickFieldCard",
|
2015-06-18 13:06:13 +00:00
|
|
|
"click .button-pass": "onPassing",
|
|
|
|
"click .field-discard": "openDiscard",
|
|
|
|
"click .field-leader": "clickLeader"
|
2015-06-14 18:50:53 +00:00
|
|
|
},
|
|
|
|
onPassing: function(){
|
|
|
|
if(this.user.get("passing")) return;
|
|
|
|
if(this.user.get("waiting")) return;
|
|
|
|
this.user.set("passing", true);
|
|
|
|
this.user.get("app").send("set:passing");
|
2015-06-14 14:01:25 +00:00
|
|
|
},
|
|
|
|
onClick: function(e){
|
|
|
|
if(!!this.user.get("waiting")) return;
|
2015-06-14 18:50:53 +00:00
|
|
|
if(!!this.user.get("passing")) return;
|
2015-06-18 13:06:13 +00:00
|
|
|
|
2015-06-14 18:50:53 +00:00
|
|
|
var self = this;
|
2015-06-14 14:01:25 +00:00
|
|
|
var $card = $(e.target).closest(".card");
|
|
|
|
var id = $card.data("id");
|
2015-06-14 18:50:53 +00:00
|
|
|
var key = $card.data("key");
|
|
|
|
|
2015-06-19 15:15:26 +00:00
|
|
|
if(!!this.user.get("setAgile")){
|
|
|
|
if(id === this.user.get("setAgile")){
|
|
|
|
this.user.set("setAgile", false);
|
|
|
|
this.app.send("cancel:agile");
|
|
|
|
this.render();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2015-06-21 14:50:50 +00:00
|
|
|
if(!!this.user.get("setHorn")){
|
|
|
|
if(id === this.user.get("setHorn")){
|
|
|
|
this.user.set("setHorn", false);
|
|
|
|
this.app.send("cancel:horn");
|
|
|
|
this.render();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2015-06-18 13:06:13 +00:00
|
|
|
if(!!this.user.get("waitForDecoy")){
|
|
|
|
if(id === this.user.get("waitForDecoy")){
|
|
|
|
this.user.set("waitForDecoy", false);
|
|
|
|
this.app.send("cancel:decoy");
|
|
|
|
this.render();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-14 14:01:25 +00:00
|
|
|
this.app.send("play:cardFromHand", {
|
|
|
|
id: id
|
|
|
|
});
|
2015-06-14 18:50:53 +00:00
|
|
|
|
|
|
|
if(key === "decoy"){
|
|
|
|
console.log("its decoy!!!");
|
|
|
|
this.user.set("waitForDecoy", id);
|
2015-06-18 13:06:13 +00:00
|
|
|
this.render();
|
2015-06-14 18:50:53 +00:00
|
|
|
}
|
|
|
|
},
|
2015-06-19 15:15:26 +00:00
|
|
|
onClickFieldCard: function(e){
|
|
|
|
if(this.user.get("waitForDecoy")){
|
|
|
|
var $card = $(e.target).closest(".card");
|
2015-06-22 15:42:48 +00:00
|
|
|
if(!$card.length) return;
|
2015-06-19 15:15:26 +00:00
|
|
|
var _id = $card.data("id");
|
2015-06-21 14:50:50 +00:00
|
|
|
|
|
|
|
if($card.parent().hasClass("field-horn")) return;
|
|
|
|
|
2015-06-19 15:15:26 +00:00
|
|
|
this.app.send("decoy:replaceWith", {
|
|
|
|
cardID: _id
|
|
|
|
})
|
|
|
|
this.user.set("waitForDecoy", false);
|
|
|
|
}
|
2015-06-20 11:08:11 +00:00
|
|
|
if(this.user.get("setAgile")){
|
2015-06-19 15:15:26 +00:00
|
|
|
var $field = $(e.target).closest(".field.active").find(".field-close, .field-range");
|
|
|
|
|
|
|
|
console.log($field);
|
|
|
|
var target = $field.hasClass("field-close") ? 0 : 1;
|
|
|
|
this.app.send("agile:field", {
|
|
|
|
field: target
|
|
|
|
});
|
|
|
|
this.user.set("setAgile", false);
|
|
|
|
}
|
2015-06-21 14:50:50 +00:00
|
|
|
if(this.user.get("setHorn")){
|
|
|
|
var $field = $(e.target).closest(".field.active").find(".field-close, .field-range, .field-siege");
|
|
|
|
|
|
|
|
console.log($field);
|
|
|
|
var target = $field.hasClass("field-close") ? 0 : ($field.hasClass("field-range") ? 1 : 2);
|
|
|
|
this.app.send("horn:field", {
|
|
|
|
field: target
|
|
|
|
});
|
|
|
|
this.user.set("setHorn", false);
|
|
|
|
}
|
2015-06-13 20:04:44 +00:00
|
|
|
},
|
|
|
|
onMouseover: function(e){
|
|
|
|
var target = $(e.target).closest(".card");
|
|
|
|
this.user.set("showPreview", target.find("img").attr("src"));
|
|
|
|
},
|
2015-06-13 22:33:20 +00:00
|
|
|
onMouseleave: function(e){
|
2015-06-13 20:04:44 +00:00
|
|
|
this.user.set("showPreview", null);
|
|
|
|
},
|
2015-06-18 13:06:13 +00:00
|
|
|
openDiscard: function(e){
|
|
|
|
var $discard = $(e.target).closest(".field-discard");
|
|
|
|
console.log("opened discard");
|
|
|
|
var side;
|
|
|
|
if($discard.parent().hasClass("player")){
|
|
|
|
side = this.yourSide;
|
2015-06-19 12:14:37 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-06-18 13:06:13 +00:00
|
|
|
side = this.otherSide;
|
|
|
|
}
|
|
|
|
this.user.set("openDiscard", {
|
|
|
|
discard: side.infoData.discard,
|
|
|
|
name: side.infoData.name
|
|
|
|
});
|
|
|
|
},
|
2015-06-13 19:36:02 +00:00
|
|
|
render: function(){
|
|
|
|
var self = this;
|
|
|
|
this.$el.html(this.template({
|
2015-06-19 15:15:26 +00:00
|
|
|
cards: self.handCards,
|
2015-06-21 14:50:50 +00:00
|
|
|
active: {
|
|
|
|
close: self.user.get("setAgile") || self.user.get("setHorn"),
|
|
|
|
range: self.user.get("setAgile") || self.user.get("setHorn"),
|
|
|
|
siege: self.user.get("setHorn")
|
|
|
|
}
|
2015-06-13 19:36:02 +00:00
|
|
|
}));
|
2015-06-13 22:33:20 +00:00
|
|
|
if(!(this.otherSide && this.yourSide)) return;
|
|
|
|
this.otherSide.render();
|
|
|
|
this.yourSide.render();
|
2015-06-18 13:06:13 +00:00
|
|
|
|
2015-06-21 14:50:50 +00:00
|
|
|
|
|
|
|
if(this.handCards){
|
2015-06-25 15:51:21 +00:00
|
|
|
calculateCardMargin(this.$el.find(".field-hand .card"), 633, 70, this.handCards.length);
|
2015-06-21 14:50:50 +00:00
|
|
|
}
|
2015-06-18 13:06:13 +00:00
|
|
|
|
2015-06-24 13:10:54 +00:00
|
|
|
if(this.user.get("isReDrawing")){
|
2015-06-23 16:12:11 +00:00
|
|
|
this.user.set("handCards", this.handCards);
|
|
|
|
var modal = new ReDrawModal({model: this.user});
|
|
|
|
this.$el.prepend(modal.render().el);
|
|
|
|
}
|
|
|
|
|
2015-06-18 13:06:13 +00:00
|
|
|
if(this.user.get("openDiscard")){
|
|
|
|
var modal = new Modal({model: this.user});
|
|
|
|
this.$el.prepend(modal.render().el);
|
|
|
|
}
|
2015-06-19 12:14:37 +00:00
|
|
|
if(this.user.get("medicDiscard")){
|
|
|
|
var modal = new MedicModal({model: this.user});
|
|
|
|
this.$el.prepend(modal.render().el);
|
|
|
|
}
|
2015-06-19 15:15:26 +00:00
|
|
|
if(this.user.get("setAgile")){
|
|
|
|
var id = this.user.get("setAgile");
|
|
|
|
this.$el.find("[data-id='" + id + "']").addClass("activeCard");
|
|
|
|
}
|
2015-06-21 14:50:50 +00:00
|
|
|
if(this.user.get("setHorn")){
|
|
|
|
var id = this.user.get("setHorn");
|
|
|
|
this.$el.find("[data-id='" + id + "']").addClass("activeCard");
|
|
|
|
}
|
2015-06-18 13:06:13 +00:00
|
|
|
if(this.user.get("waitForDecoy")){
|
|
|
|
var id = this.user.get("waitForDecoy");
|
|
|
|
this.$el.find("[data-id='" + id + "']").addClass("activeCard");
|
|
|
|
}
|
2015-06-13 19:36:02 +00:00
|
|
|
return this;
|
2015-06-15 19:03:12 +00:00
|
|
|
},
|
2015-06-18 13:06:13 +00:00
|
|
|
renderPreview: function(){
|
|
|
|
this.$el.find(".card-preview").html(this.templatePreview({src: this.user.get("showPreview")}))
|
|
|
|
},
|
2015-06-19 12:14:37 +00:00
|
|
|
clickLeader: function(e){
|
2015-06-18 13:06:13 +00:00
|
|
|
var $card = $(e.target).closest(".field-leader");
|
|
|
|
if(!$card.parent().hasClass("player")) return;
|
2015-06-22 16:48:08 +00:00
|
|
|
if($card.find(".card").hasClass("disabled")) return;
|
|
|
|
|
|
|
|
console.log("click leader");
|
2015-06-18 13:06:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
this.app.send("activate:leader")
|
|
|
|
},
|
2015-06-24 16:22:56 +00:00
|
|
|
setUpBattleEvents: function(){
|
2015-06-15 19:03:12 +00:00
|
|
|
var self = this;
|
|
|
|
var user = this.user;
|
2015-06-24 16:22:56 +00:00
|
|
|
var app = user.get("app");
|
2015-06-15 19:03:12 +00:00
|
|
|
|
2015-06-24 16:22:56 +00:00
|
|
|
app.on("update:hand", function(data) {
|
|
|
|
if(user.get("roomSide") == data._roomSide){
|
|
|
|
self.handCards = JSON.parse(data.cards);
|
|
|
|
self.user.set("handCards", app.handCards);
|
|
|
|
self.render();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
app.on("update:info", function(data) {
|
|
|
|
var _side = data._roomSide;
|
|
|
|
var infoData = data.info;
|
|
|
|
var leader = data.leader;
|
|
|
|
|
|
|
|
var side = self.yourSide;
|
|
|
|
if(user.get("roomSide") != _side){
|
|
|
|
side = self.otherSide;
|
|
|
|
}
|
|
|
|
side.infoData = infoData;
|
|
|
|
side.leader = leader;
|
|
|
|
|
|
|
|
side.infoData.discard = JSON.parse(side.infoData.discard);
|
|
|
|
|
|
|
|
side.render();
|
|
|
|
})
|
|
|
|
|
|
|
|
app.on("update:fields", function(data) {
|
|
|
|
var _side = data._roomSide;
|
|
|
|
|
|
|
|
var side = self.yourSide;
|
|
|
|
if(user.get("roomSide") != _side){
|
|
|
|
side = self.otherSide;
|
|
|
|
}
|
|
|
|
side.field.close = data.close;
|
|
|
|
side.field.ranged = data.ranged;
|
|
|
|
side.field.siege = data.siege;
|
|
|
|
side.field.weather = data.weather;
|
|
|
|
side.render();
|
|
|
|
})
|
|
|
|
|
|
|
|
/*this.battleChannel.watch(function(d){
|
2015-06-15 19:03:12 +00:00
|
|
|
var event = d.event, data = d.data;
|
|
|
|
|
|
|
|
if(event === "update:hand"){
|
|
|
|
if(user.get("roomSide") == data._roomSide){
|
|
|
|
self.handCards = JSON.parse(data.cards);
|
2015-06-18 13:06:13 +00:00
|
|
|
self.user.set("handCards", self.handCards);
|
2015-06-15 19:03:12 +00:00
|
|
|
self.render();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(event === "update:info"){
|
|
|
|
var _side = data._roomSide;
|
|
|
|
var infoData = data.info;
|
|
|
|
var leader = data.leader;
|
|
|
|
|
|
|
|
var side = self.yourSide;
|
|
|
|
if(user.get("roomSide") != _side){
|
|
|
|
side = self.otherSide;
|
|
|
|
}
|
|
|
|
side.infoData = infoData;
|
|
|
|
side.leader = leader;
|
2015-06-18 13:06:13 +00:00
|
|
|
|
|
|
|
side.infoData.discard = JSON.parse(side.infoData.discard);
|
|
|
|
|
2015-06-15 19:03:12 +00:00
|
|
|
side.render();
|
|
|
|
}
|
|
|
|
else if(event === "update:fields"){
|
|
|
|
var _side = data._roomSide;
|
|
|
|
|
|
|
|
var side = self.yourSide;
|
|
|
|
if(user.get("roomSide") != _side){
|
|
|
|
side = self.otherSide;
|
|
|
|
}
|
|
|
|
side.field.close = data.close;
|
|
|
|
side.field.ranged = data.ranged;
|
|
|
|
side.field.siege = data.siege;
|
|
|
|
side.field.weather = data.weather;
|
|
|
|
side.render();
|
|
|
|
}
|
2015-06-24 16:22:56 +00:00
|
|
|
})*/
|
2015-06-13 19:36:02 +00:00
|
|
|
}
|
2015-06-13 22:33:20 +00:00
|
|
|
});
|
2015-06-13 19:36:02 +00:00
|
|
|
|
2015-06-18 13:06:13 +00:00
|
|
|
var Modal = Backbone.Modal.extend({
|
2015-06-22 15:29:01 +00:00
|
|
|
template: require("../templates/modal.handlebars"),
|
2015-06-18 13:06:13 +00:00
|
|
|
cancelEl: ".bbm-close",
|
|
|
|
cancel: function(){
|
|
|
|
this.model.set("openDiscard", false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-06-19 12:14:37 +00:00
|
|
|
var MedicModal = Modal.extend({
|
2015-06-22 15:29:01 +00:00
|
|
|
template: require("../templates/modal.medic.handlebars"),
|
2015-06-19 12:14:37 +00:00
|
|
|
events: {
|
|
|
|
"click .card": "onCardClick"
|
|
|
|
},
|
|
|
|
onCardClick: function(e){
|
|
|
|
console.log($(e.target).closest(".card"));
|
|
|
|
var id = $(e.target).closest(".card").data().id;
|
|
|
|
this.model.get("app").send("medic:chooseCardFromDiscard", {
|
|
|
|
cardID: id
|
|
|
|
})
|
|
|
|
this.model.set("medicDiscard", false);
|
|
|
|
},
|
|
|
|
cancel: function(){
|
|
|
|
this.model.get("app").send("medic:chooseCardFromDiscard")
|
|
|
|
this.model.set("medicDiscard", false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-06-23 16:12:11 +00:00
|
|
|
var ReDrawModal = Modal.extend({
|
|
|
|
template: require("../templates/modal.redraw.handlebars"),
|
|
|
|
initialize: function(){
|
|
|
|
this.listenTo(this.model, "change:isReDrawing", this.cancel);
|
|
|
|
},
|
|
|
|
events: {
|
|
|
|
"click .card": "onCardClick"
|
|
|
|
},
|
|
|
|
onCardClick: function(e){
|
|
|
|
console.log($(e.target).closest(".card"));
|
|
|
|
var id = $(e.target).closest(".card").data().id;
|
|
|
|
this.model.get("app").send("redraw:reDrawCard", {
|
|
|
|
cardID: id
|
|
|
|
})
|
|
|
|
},
|
|
|
|
cancel: function(){
|
|
|
|
if(!this.model.get("isReDrawing")) return;
|
|
|
|
this.model.get("app").send("redraw:close_client");
|
|
|
|
this.model.set("isReDrawing", false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-06-13 19:36:02 +00:00
|
|
|
var User = Backbone.Model.extend({
|
|
|
|
defaults: {
|
2015-06-19 12:14:37 +00:00
|
|
|
name: "",
|
|
|
|
deckKey: "northern_realm"
|
2015-06-13 19:36:02 +00:00
|
|
|
},
|
|
|
|
initialize: function(){
|
|
|
|
var self = this;
|
2015-06-24 16:22:56 +00:00
|
|
|
var user = this;
|
|
|
|
var app = user.get("app");
|
2015-06-13 19:36:02 +00:00
|
|
|
|
2015-06-15 19:03:12 +00:00
|
|
|
this.listenTo(this.attributes, "change:room", this.subscribeRoom);
|
|
|
|
|
2015-06-13 19:36:02 +00:00
|
|
|
app.receive("response:name", function(data){
|
|
|
|
self.set("name", data.name);
|
|
|
|
});
|
|
|
|
|
|
|
|
app.receive("init:battle", function(data){
|
|
|
|
console.log("opponent found!");
|
|
|
|
self.set("roomSide", data.side);
|
2015-06-15 19:03:12 +00:00
|
|
|
/*
|
|
|
|
self.set("channel:battle", app.socket.subscribe(self.get("room")));*/
|
2015-06-25 15:51:21 +00:00
|
|
|
//app.navigate("battle", {trigger: true});
|
|
|
|
app.battleRoute();
|
2015-06-13 19:36:02 +00:00
|
|
|
})
|
|
|
|
|
2015-06-24 17:42:22 +00:00
|
|
|
/*app.receive("response:createRoom", function(roomID){
|
2015-06-13 19:36:02 +00:00
|
|
|
self.set("room", roomID);
|
|
|
|
console.log("room created", roomID);
|
2015-06-24 17:42:22 +00:00
|
|
|
});*/
|
2015-06-13 19:36:02 +00:00
|
|
|
|
|
|
|
app.receive("response:joinRoom", function(roomID){
|
|
|
|
self.set("room", roomID);
|
|
|
|
console.log("room id", self.get("room"));
|
|
|
|
})
|
|
|
|
|
2015-06-14 14:01:25 +00:00
|
|
|
app.receive("set:waiting", function(data){
|
|
|
|
var waiting = data.waiting;
|
|
|
|
self.set("waiting", waiting);
|
|
|
|
})
|
2015-06-18 13:06:13 +00:00
|
|
|
|
2015-06-17 19:18:14 +00:00
|
|
|
app.receive("set:passing", function(data){
|
|
|
|
var passing = data.passing;
|
|
|
|
self.set("passing", passing);
|
|
|
|
})
|
2015-06-14 14:01:25 +00:00
|
|
|
|
2015-06-19 12:14:37 +00:00
|
|
|
app.receive("foe:left", function(){
|
2015-06-18 13:31:36 +00:00
|
|
|
console.log("your foe left the room");
|
|
|
|
$(".container").prepend('<div class="alert alert-danger">Your foe left the battle!</div>')
|
|
|
|
})
|
|
|
|
|
2015-06-19 12:14:37 +00:00
|
|
|
app.receive("played:medic", function(data){
|
|
|
|
var cards = JSON.parse(data.cards);
|
|
|
|
console.log("played medic");
|
|
|
|
self.set("medicDiscard", {
|
|
|
|
cards: cards
|
|
|
|
});
|
|
|
|
})
|
|
|
|
|
2015-06-19 15:15:26 +00:00
|
|
|
app.receive("played:agile", function(data){
|
|
|
|
console.log("played agile");
|
|
|
|
self.set("setAgile", data.cardID);
|
|
|
|
})
|
2015-06-13 19:36:02 +00:00
|
|
|
|
2015-06-21 14:50:50 +00:00
|
|
|
app.receive("played:horn", function(data){
|
|
|
|
console.log("played horn");
|
|
|
|
self.set("setHorn", data.cardID);
|
|
|
|
})
|
|
|
|
|
2015-06-24 13:10:54 +00:00
|
|
|
app.receive("redraw:cards", function(){
|
2015-06-23 16:12:11 +00:00
|
|
|
self.set("isReDrawing", true);
|
|
|
|
})
|
|
|
|
|
2015-06-24 13:10:54 +00:00
|
|
|
app.receive("redraw:close", function(){
|
2015-06-23 16:12:11 +00:00
|
|
|
self.set("isReDrawing", false);
|
|
|
|
})
|
|
|
|
|
2015-06-24 16:22:56 +00:00
|
|
|
app.receive("update:hand", function(data){
|
|
|
|
app.trigger("update:hand", data);
|
|
|
|
})
|
|
|
|
app.receive("update:fields", function(data){
|
|
|
|
app.trigger("update:fields", data);
|
|
|
|
})
|
|
|
|
app.receive("update:info", function(data){
|
|
|
|
app.trigger("update:info", data);
|
|
|
|
})
|
|
|
|
|
|
|
|
app.on("startMatchmaking", this.startMatchmaking, this);
|
2015-06-13 19:36:02 +00:00
|
|
|
app.on("joinRoom", this.joinRoom, this);
|
|
|
|
app.on("setName", this.setName, this);
|
2015-06-19 12:14:37 +00:00
|
|
|
app.on("setDeck", this.setDeck, this);
|
2015-06-13 19:36:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
app.send("request:name", this.get("name") == "unnamed" ? null : {name: this.get("name")});
|
|
|
|
},
|
2015-06-24 16:22:56 +00:00
|
|
|
startMatchmaking: function(){
|
2015-06-24 17:42:22 +00:00
|
|
|
this.set("inMatchmakerQueue", true);
|
2015-06-24 16:22:56 +00:00
|
|
|
this.get("app").send("request:matchmaking");
|
2015-06-13 19:36:02 +00:00
|
|
|
},
|
|
|
|
joinRoom: function(){
|
|
|
|
this.get("app").send("request:joinRoom");
|
2015-06-24 17:42:22 +00:00
|
|
|
this.set("inMatchmakerQueue", false);
|
2015-06-13 19:36:02 +00:00
|
|
|
},
|
2015-06-15 19:03:12 +00:00
|
|
|
subscribeRoom: function(){
|
|
|
|
var room = this.get("room");
|
|
|
|
var app = this.get("app");
|
2015-06-24 16:22:56 +00:00
|
|
|
//app.socket.subscribe(room);
|
2015-06-15 19:03:12 +00:00
|
|
|
},
|
2015-06-13 19:36:02 +00:00
|
|
|
setName: function(name){
|
|
|
|
this.get("app").send("request:name", {name: name});
|
2015-06-19 12:14:37 +00:00
|
|
|
},
|
|
|
|
setDeck: function(deckKey){
|
|
|
|
console.log("deck: ", deckKey);
|
|
|
|
this.set("deckKey", deckKey);
|
|
|
|
this.get("app").send("set:deck", {deck: deckKey});
|
2015-06-13 19:36:02 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var Lobby = Backbone.View.extend({
|
|
|
|
defaults: {
|
|
|
|
id: ""
|
|
|
|
},
|
|
|
|
className: "container",
|
|
|
|
|
2015-06-22 15:29:01 +00:00
|
|
|
template: require("../templates/lobby.handlebars"),
|
2015-06-13 19:36:02 +00:00
|
|
|
initialize: function(options){
|
|
|
|
this.user = options.user;
|
|
|
|
this.app = options.app;
|
|
|
|
this.listenTo(this.app.user, "change", this.render);
|
|
|
|
$(this.el).prependTo('body');
|
|
|
|
this.render();
|
|
|
|
},
|
|
|
|
events: {
|
2015-06-24 16:22:56 +00:00
|
|
|
"click .startMatchmaking": "startMatchmaking",
|
2015-06-24 17:42:22 +00:00
|
|
|
/*"click .join-room": "joinRoom",*/
|
2015-06-19 12:14:37 +00:00
|
|
|
"blur .name-input": "changeName",
|
|
|
|
"change #deckChoice": "setDeck"
|
2015-06-13 19:36:02 +00:00
|
|
|
},
|
|
|
|
render: function(){
|
|
|
|
this.$el.html(this.template(this.user.attributes));
|
2015-06-19 12:14:37 +00:00
|
|
|
/*this.$el.find("#deckChoice option[value='" + this.app.user.get("setDeck") + "']").attr("selected", "selected")*/
|
2015-06-13 19:36:02 +00:00
|
|
|
return this;
|
|
|
|
},
|
2015-06-24 16:22:56 +00:00
|
|
|
startMatchmaking: function(){
|
|
|
|
this.app.trigger("startMatchmaking");
|
2015-06-13 19:36:02 +00:00
|
|
|
},
|
|
|
|
joinRoom: function(){
|
|
|
|
this.app.trigger("joinRoom");
|
|
|
|
},
|
2015-06-19 12:14:37 +00:00
|
|
|
setDeck: function(e){
|
|
|
|
var val = $(e.target).val();
|
|
|
|
this.app.trigger("setDeck", val);
|
|
|
|
this.$el.find("#deckChoice option[value='" + val + "']").attr("selected", "selected")
|
|
|
|
},
|
2015-06-13 19:36:02 +00:00
|
|
|
changeName: function(e){
|
|
|
|
var name = $(e.target).val();
|
|
|
|
this.app.trigger("setName", name);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = App;
|