1
0
mirror of https://github.com/exane/not-gwent-online synced 2024-11-27 03:46:54 +00:00
not-gwent-online/client/js/client.js

999 lines
28 KiB
JavaScript
Raw Normal View History

let socket = require("socket.io-client");
let Backbone = require("backbone");
2015-06-18 13:06:13 +00:00
require("./backbone.modal-min");
let Handlebars = require('handlebars/runtime').default;
let $ = require("jquery");
2015-06-13 19:36:02 +00:00
2015-06-25 19:34:51 +00:00
let cardData = require("../../assets/data/cards");
let abilityData = require("../../assets/data/abilities");
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){
let out = "";
2015-06-25 15:51:21 +00:00
for(let i = 0; i < 2; i++) {
2015-06-25 15:51:21 +00:00
out += "<i";
if(i < lives){
out += " class='ruby'";
}
out += "></i>";
}
return out;
});
2015-06-30 12:47:09 +00:00
Handlebars.registerHelper("formatMessage", function(msg){
let out = "";
var lines = msg.split(/\n/g);
2015-07-01 16:55:50 +00:00
lines.forEach(function(line){
2015-06-30 12:47:09 +00:00
out += line + "<br>";
})
return out;
});
2015-06-25 15:51:21 +00:00
let App = Backbone.Router.extend({
2015-06-13 19:36:02 +00:00
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(){
let self = this;
2015-06-13 19:36:02 +00:00
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-07-01 16:55:50 +00:00
var self = this;
console.log(this.socket.connected);
this.socket.on("connect", function(socket){
self.user.set("serverOffline", false);
})
this.socket.on("disconnect", function(socket){
self.user.set("serverOffline", true);
})
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;
let socket = this.socket;
2015-06-13 19:36:02 +00:00
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){
let regex = /(\w+):?(\w*)\|?/g;
let res = {};
let r;
2015-06-13 19:36:02 +00:00
while(r = regex.exec(event)) {
res[r[1]] = r[2];
}
return res;
}
});
let SideView = Backbone.View.extend({
2015-06-13 22:33:20 +00:00
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-07-04 15:33:06 +00:00
templateCardpiles: require("../templates/cardpiles.handlebars"),
2015-06-13 22:33:20 +00:00
initialize: function(options){
let self = this;
2015-06-13 22:33:20 +00:00
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(){
let d = this.infoData;
let l = this.leader;
let html = this.templateInfo({
2015-06-25 15:51:21 +00:00
data: d,
leader: l,
passBtn: this.side === ".player"
})
this.$info = this.$el.find(".game-info" + this.side).html(html);
2015-07-04 15:33:06 +00:00
/*let $deck = $(this.side + " .field-deck");
$deck*/
this.$deck = $(this.side + ".right-side");
this.$deck.html(this.templateCardpiles({
data: d
}));
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);
let $field = this.$fields.find(".field-close").parent();
let cards = this.field.close.cards;
let score = this.field.close.score;
let horn = this.field.close.horn;
2015-06-14 14:01:25 +00:00
2015-06-20 11:08:11 +00:00
let html = this.templateCards(cards);
2015-06-14 14:01:25 +00:00
$field.find(".field-close").html(html)
$field.find(".large-field-counter").html(score)
2015-06-21 14:50:50 +00:00
if(horn){
2015-07-04 13:07:49 +00:00
this.$fields.find(".field-horn-close").html(this.templateCards([horn]));
2015-06-21 14:50:50 +00:00
}
2015-06-20 11:08:11 +00:00
let isInfluencedByWeather;
2015-06-25 19:34:51 +00:00
this.field.weather.cards.forEach((card) =>{
let key = card._key;
if(key === "biting_frost") isInfluencedByWeather = true;
})
2015-06-25 19:34:51 +00:00
if(isInfluencedByWeather){
$field.addClass("field-frost");
}
2015-07-02 16:17:33 +00:00
//calculateCardMargin($field.find(".card"), 351, 70, cards.length);
2015-07-04 09:22:16 +00:00
this.battleView.calculateMargin($field.find(".field-close"), 5);
2015-06-14 14:01:25 +00:00
},
renderRangeField: function(){
if(!this.field.ranged) return;
this.$fields = this.$el.find(".battleside" + this.side);
let $field = this.$fields.find(".field-range").parent();
let cards = this.field.ranged.cards;
let score = this.field.ranged.score;
let horn = this.field.ranged.horn;
2015-06-14 14:01:25 +00:00
let html = this.templateCards(cards);
2015-06-14 14:01:25 +00:00
$field.find(".field-range").html(html)
$field.find(".large-field-counter").html(score)
2015-06-21 14:50:50 +00:00
if(horn){
2015-07-04 13:07:49 +00:00
this.$fields.find(".field-horn-range").html(this.templateCards([horn]));
2015-06-21 14:50:50 +00:00
}
let isInfluencedByWeather;
2015-06-25 19:34:51 +00:00
this.field.weather.cards.forEach((card) =>{
let key = card._key;
if(key === "impenetrable_fog") isInfluencedByWeather = true;
})
2015-06-25 19:34:51 +00:00
if(isInfluencedByWeather){
$field.addClass("field-fog");
}
2015-07-02 16:17:33 +00:00
//calculateCardMargin($field.find(".card"), 351, 70, cards.length);
2015-07-04 09:22:16 +00:00
this.battleView.calculateMargin($field.find(".field-range"), 5);
2015-06-14 14:01:25 +00:00
},
renderSiegeField: function(){
if(!this.field.siege) return;
this.$fields = this.$el.find(".battleside" + this.side);
let $field = this.$fields.find(".field-siege").parent();
let cards = this.field.siege.cards;
let score = this.field.siege.score;
let horn = this.field.siege.horn;
2015-06-14 14:01:25 +00:00
let html = this.templateCards(cards);
2015-06-14 14:01:25 +00:00
$field.find(".field-siege").html(html)
$field.find(".large-field-counter").html(score)
2015-06-21 14:50:50 +00:00
if(horn){
2015-07-04 13:07:49 +00:00
this.$fields.find(".field-horn-siege").html(this.templateCards([horn]));
2015-06-21 14:50:50 +00:00
}
let isInfluencedByWeather;
2015-06-25 19:34:51 +00:00
this.field.weather.cards.forEach((card) =>{
let key = card._key;
if(key === "torrential_rain") isInfluencedByWeather = true;
})
2015-06-25 19:34:51 +00:00
if(isInfluencedByWeather){
$field.addClass("field-rain");
}
2015-06-25 19:34:51 +00:00
2015-07-02 16:17:33 +00:00
//calculateCardMargin($field.find(".card"), 351, 70, cards.length);
2015-07-04 09:22:16 +00:00
this.battleView.calculateMargin($field.find(".field-siege"), 5);
2015-06-13 22:33:20 +00:00
},
2015-06-14 18:50:53 +00:00
renderWeatherField: function(){
if(!this.field.weather) return;
let $weatherField = this.$el.find(".field-weather");
let cards = this.field.weather.cards;
2015-06-14 18:50:53 +00:00
$weatherField.html(this.templateCards(cards));
2015-07-03 07:46:27 +00:00
this.battleView.calculateMargin($weatherField, 0);
2015-06-14 18:50:53 +00:00
return this;
2015-06-25 15:51:21 +00:00
}
/*,
2015-06-13 22:33:20 +00:00
lives: function(lives){
let out = "";
for(let i = 0; i < 2; i++) {
2015-06-13 22:33:20 +00:00
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
});
let BattleView = Backbone.View.extend({
2015-06-29 17:57:51 +00:00
el: ".gwent-battle",
template: require("../templates/battle.handlebars"),
2015-06-13 19:36:02 +00:00
initialize: function(options){
let self = this;
let user = this.user = options.user;
let app = this.app = options.app;
let yourSide, otherSide;
2015-06-29 17:57:51 +00:00
$(this.el).prependTo('gwent-battle');
2015-06-13 19:36:02 +00:00
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);
2015-06-28 14:10:25 +00:00
this.listenTo(user, "change:chooseSide", 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-07-02 16:17:33 +00:00
//$(window).on("resize", this.calculateMargin.bind(this, this.$hand));
2015-06-13 22:33:20 +00:00
let interval = setInterval(function(){
2015-06-13 19:36:02 +00:00
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-26 06:29:01 +00:00
},
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
let self = this;
let $card = $(e.target).closest(".card");
let id = $card.data("id");
let key = $card.data("key");
2015-06-14 18:50:53 +00:00
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"){
2015-06-27 13:08:35 +00:00
//console.log("its decoy!!!");
2015-06-14 18:50:53 +00:00
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")){
let $card = $(e.target).closest(".card");
2015-06-22 15:42:48 +00:00
if(!$card.length) return;
let _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")){
let $field = $(e.target).closest(".field.active").find(".field-close, .field-range");
2015-06-19 15:15:26 +00:00
2015-06-27 13:08:35 +00:00
//console.log($field);
let target = $field.hasClass("field-close") ? 0 : 1;
2015-06-19 15:15:26 +00:00
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")){
let $field = $(e.target).closest(".field.active").find(".field-close, .field-range, .field-siege");
2015-06-21 14:50:50 +00:00
2015-06-27 13:08:35 +00:00
//console.log($field);
let target = $field.hasClass("field-close") ? 0 : ($field.hasClass("field-range") ? 1 : 2);
2015-06-21 14:50:50 +00:00
this.app.send("horn:field", {
field: target
});
this.user.set("setHorn", false);
}
2015-06-13 20:04:44 +00:00
},
onMouseover: function(e){
let target = $(e.target).closest(".card");
2015-07-03 16:48:37 +00:00
var hasPreviewB = target.parent().hasClass("preview-b");
this.user.set("showPreview", new Preview({key: target.data().key, previewB: hasPreviewB}));
2015-06-13 20:04:44 +00:00
},
2015-06-13 22:33:20 +00:00
onMouseleave: function(e){
2015-06-25 19:34:51 +00:00
this.user.get("showPreview").remove();
2015-06-13 20:04:44 +00:00
this.user.set("showPreview", null);
},
2015-06-18 13:06:13 +00:00
openDiscard: function(e){
let $discard = $(e.target).closest(".field-discard");
2015-06-27 13:08:35 +00:00
//console.log("opened discard");
let side;
2015-06-18 13:06:13 +00:00
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(){
let self = this;
2015-06-13 19:36:02 +00:00
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-28 14:10:09 +00:00
},
isWaiting: self.user.get("waiting")
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-07-04 13:07:49 +00:00
this.calculateMargin(this.$el.find(".handcard-wrap"));
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);
let modal = new ReDrawModal({model: this.user});
2015-06-23 16:12:11 +00:00
this.$el.prepend(modal.render().el);
}
2015-06-29 17:57:51 +00:00
if(this.user.get("openDiscard")){
let modal = new Modal({model: this.user});
2015-06-18 13:06:13 +00:00
this.$el.prepend(modal.render().el);
}
2015-06-28 14:10:25 +00:00
if(this.user.get("chooseSide")){
let modal = new ChooseSideModal({model: this.user});
this.$el.prepend(modal.render().el);
}
2015-06-19 12:14:37 +00:00
if(this.user.get("medicDiscard")){
let modal = new MedicModal({model: this.user});
2015-06-19 12:14:37 +00:00
this.$el.prepend(modal.render().el);
}
2015-06-30 16:26:07 +00:00
if(this.user.get("emreis_leader4")){
let modal = new LeaderEmreis4Modal({model: this.user});
this.$el.prepend(modal.render().el);
}
2015-06-19 15:15:26 +00:00
if(this.user.get("setAgile")){
let id = this.user.get("setAgile");
2015-07-04 08:44:42 +00:00
this.$el.find("[data-id='" + id + "']").parent().addClass("activeCard");
2015-06-19 15:15:26 +00:00
}
2015-06-21 14:50:50 +00:00
if(this.user.get("setHorn")){
let id = this.user.get("setHorn");
2015-06-21 14:50:50 +00:00
this.$el.find("[data-id='" + id + "']").addClass("activeCard");
}
2015-06-18 13:06:13 +00:00
if(this.user.get("waitForDecoy")){
let id = this.user.get("waitForDecoy");
2015-06-18 13:06:13 +00:00
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(){
2015-06-25 19:34:51 +00:00
/*let preview = new Preview({key: this.user.get("showPreview")});*/
let preview = this.user.get("showPreview");
2015-06-29 17:57:51 +00:00
if(!preview){
2015-06-25 19:34:51 +00:00
return;
}
this.$el.find(".card-preview").html(preview.render().el);
/*this.$el.find(".card-preview").html(this.templatePreview({src: this.user.get("showPreview")}))
this.$el.find(".card-preview").css("display", "none");
if(this.user.get("showPreview")) {
this.$el.find(".card-preview").css("display", "block");
}*/
2015-06-18 13:06:13 +00:00
},
2015-06-19 12:14:37 +00:00
clickLeader: function(e){
let $card = $(e.target).closest(".field-leader");
2015-06-18 13:06:13 +00:00
if(!$card.parent().hasClass("player")) return;
2015-06-22 16:48:08 +00:00
if($card.find(".card").hasClass("disabled")) return;
2015-06-27 13:08:35 +00:00
//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(){
let self = this;
let user = this.user;
let app = user.get("app");
2015-06-15 19:03:12 +00:00
2015-06-25 19:34:51 +00:00
app.on("update:hand", function(data){
2015-06-24 16:22:56 +00:00
if(user.get("roomSide") == data._roomSide){
self.handCards = JSON.parse(data.cards);
self.user.set("handCards", app.handCards);
self.render();
}
})
2015-06-25 19:34:51 +00:00
app.on("update:info", function(data){
let _side = data._roomSide;
let infoData = data.info;
let leader = data.leader;
2015-06-24 16:22:56 +00:00
let side = self.yourSide;
2015-06-24 16:22:56 +00:00
if(user.get("roomSide") != _side){
side = self.otherSide;
}
side.infoData = infoData;
side.leader = leader;
side.infoData.discard = JSON.parse(side.infoData.discard);
side.render();
})
2015-06-25 19:34:51 +00:00
app.on("update:fields", function(data){
let _side = data._roomSide;
2015-06-24 16:22:56 +00:00
let side = self.yourSide;
2015-06-24 16:22:56 +00:00
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){
let event = d.event, data = d.data;
2015-06-15 19:03:12 +00:00
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"){
let _side = data._roomSide;
let infoData = data.info;
let leader = data.leader;
2015-06-15 19:03:12 +00:00
let side = self.yourSide;
2015-06-15 19:03:12 +00:00
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"){
let _side = data._roomSide;
2015-06-15 19:03:12 +00:00
let side = self.yourSide;
2015-06-15 19:03:12 +00:00
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-07-02 16:17:33 +00:00
},
2015-07-03 07:46:27 +00:00
calculateMargin: function($container, minSize){
minSize = typeof minSize === "number" && minSize >= 0 ? minSize : 6;
2015-07-04 08:44:42 +00:00
var Class = $container.find(".card-wrap").length ? ".card-wrap" : ".card";
2015-07-02 16:17:33 +00:00
var n = $container.children().size();
2015-07-04 09:22:16 +00:00
let w = $container.width(), c = $container.find(Class).outerWidth(true);
2015-07-02 16:17:33 +00:00
let res;
2015-07-03 07:46:27 +00:00
if(n < minSize)
2015-07-02 16:17:33 +00:00
res = 0;
else {
2015-07-04 09:22:16 +00:00
res = -((w - c) / (n - 1) - c) + 1;
2015-07-02 16:17:33 +00:00
}
2015-07-04 09:22:16 +00:00
$container.find(Class).not(Class+":first-child").css("margin-left", -res);
2015-06-13 19:36:02 +00:00
}
2015-06-13 22:33:20 +00:00
});
2015-06-13 19:36:02 +00:00
let 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);
}
});
let 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){
2015-06-27 13:08:35 +00:00
//console.log($(e.target).closest(".card"));
let id = $(e.target).closest(".card").data().id;
2015-06-19 12:14:37 +00:00
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-30 16:26:07 +00:00
let LeaderEmreis4Modal = Modal.extend({
template: require("../templates/modal.emreis_leader4.handlebars"),
events: {
"click .card": "onCardClick"
},
onCardClick: function(e){
let id = $(e.target).closest(".card").data().id;
this.model.get("app").send("emreis_leader4:chooseCardFromDiscard", {
cardID: id
})
this.model.set("emreis_leader4", false);
},
cancel: function(){
this.model.get("app").send("emreis_leader4:chooseCardFromDiscard")
this.model.set("emreis_leader4", false);
}
});
let ReDrawModal = Modal.extend({
2015-06-23 16:12:11 +00:00
template: require("../templates/modal.redraw.handlebars"),
initialize: function(){
this.listenTo(this.model, "change:isReDrawing", this.cancel);
},
events: {
"click .card": "onCardClick"
},
onCardClick: function(e){
2015-06-27 13:08:35 +00:00
//console.log($(e.target).closest(".card"));
let id = $(e.target).closest(".card").data().id;
2015-06-23 16:12:11 +00:00
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);
}
});
let WinnerModal = Modal.extend({
template: require("../templates/modal.winner.handlebars")
});
2015-06-29 17:57:51 +00:00
2015-06-28 14:10:25 +00:00
let ChooseSideModal = Modal.extend({
template: require("../templates/modal.side.handlebars"),
events: {
"click .btn": "onBtnClick"
},
2015-06-29 17:57:51 +00:00
beforeCancel: function(){
2015-06-28 14:10:25 +00:00
return false;
},
2015-06-29 17:57:51 +00:00
onBtnClick: function(e){
var id = $(e.target).data().id;
2015-06-28 14:10:25 +00:00
this.model.set("chooseSide", false);
2015-06-29 17:57:51 +00:00
if(id === "you"){
2015-06-28 14:10:25 +00:00
//this.model.set("chosenSide", this.model.get("roomSide"));
this.model.chooseSide(this.model.get("roomSide"));
this.remove();
return;
}
//this.model.set("chosenSide", this.model.get("roomFoeSide"));
this.model.chooseSide(this.model.get("roomFoeSide"));
this.remove();
}
});
let User = Backbone.Model.extend({
2015-06-13 19:36:02 +00:00
defaults: {
2015-07-01 16:55:50 +00:00
name: localStorage["userName"] || null,
2015-07-02 17:21:24 +00:00
deck: localStorage["userDeck"] || "random",
2015-07-01 16:55:50 +00:00
serverOffline: true
2015-06-13 19:36:02 +00:00
},
initialize: function(){
let self = this;
let user = this;
let app = user.get("app");
2015-06-13 19:36:02 +00:00
2015-06-28 14:10:25 +00:00
self.set("chooseSide", false);
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){
2015-06-27 13:08:35 +00:00
//console.log("opponent found!");
2015-06-13 19:36:02 +00:00
self.set("roomSide", data.side);
2015-06-28 14:10:25 +00:00
self.set("roomFoeSide", data.foeSide);
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
})
app.receive("response:joinRoom", function(roomID){
self.set("room", roomID);
2015-06-27 13:08:35 +00:00
//console.log("room id", self.get("room"));
2015-06-13 19:36:02 +00:00
})
2015-06-14 14:01:25 +00:00
app.receive("set:waiting", function(data){
let waiting = data.waiting;
2015-06-14 14:01:25 +00:00
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){
let passing = data.passing;
2015-06-17 19:18:14 +00:00
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-27 13:08:35 +00:00
//console.log("your foe left the room");
2015-07-04 14:48:37 +00:00
$(".container").prepend('<div class="notification-left">Your foe left the battle!</div>')
2015-06-18 13:31:36 +00:00
})
2015-06-19 12:14:37 +00:00
app.receive("played:medic", function(data){
let cards = JSON.parse(data.cards);
2015-06-19 12:14:37 +00:00
self.set("medicDiscard", {
cards: cards
});
})
2015-06-30 16:26:07 +00:00
app.receive("played:emreis_leader4", function(data){
let cards = JSON.parse(data.cards);
self.set("emreis_leader4", {
cards: cards
});
})
2015-06-19 15:15:26 +00:00
app.receive("played:agile", function(data){
2015-06-27 13:08:35 +00:00
//console.log("played agile");
2015-06-19 15:15:26 +00:00
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){
2015-06-27 13:08:35 +00:00
//console.log("played horn");
2015-06-21 14:50:50 +00:00
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);
})
2015-06-25 19:34:51 +00:00
app.receive("gameover", function(data){
let winner = data.winner;
2015-06-27 13:08:35 +00:00
//console.log("gameover");
let model = Backbone.Model.extend({});
let modal = new WinnerModal({model: new model({winner: winner})});
$("body").prepend(modal.render().el);
})
2015-06-29 17:57:51 +00:00
app.receive("request:chooseWhichSideBegins", function(){
2015-06-28 14:10:25 +00:00
self.set("chooseSide", true);
})
2015-06-24 16:22:56 +00:00
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
2015-07-01 16:55:50 +00:00
app.receive("notification", function(data){
2015-06-29 17:57:51 +00:00
new Notification(data).render();
})
2015-07-01 16:55:50 +00:00
app.send("request:name", this.get("name") === null ? null : {name: this.get("name")});
app.send("set:deck", this.get("deck") === null ? null : {deck: this.get("deck")});
2015-06-13 19:36:02 +00:00
},
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(){
let room = this.get("room");
let 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-07-01 16:55:50 +00:00
localStorage["userName"] = name;
2015-06-19 12:14:37 +00:00
},
setDeck: function(deckKey){
2015-06-27 13:08:35 +00:00
//console.log("deck: ", deckKey);
2015-06-19 12:14:37 +00:00
this.set("deckKey", deckKey);
2015-07-01 16:55:50 +00:00
localStorage["userDeck"] = deckKey;
2015-06-19 12:14:37 +00:00
this.get("app").send("set:deck", {deck: deckKey});
2015-06-28 14:10:25 +00:00
},
2015-06-29 17:57:51 +00:00
chooseSide: function(roomSide){
2015-06-28 14:10:25 +00:00
this.get("app").send("response:chooseWhichSideBegins", {
side: roomSide
})
2015-07-03 16:48:37 +00:00
},
2015-07-04 08:44:42 +00:00
getCardData: function(card){
2015-07-03 16:48:37 +00:00
if(!card || !card.ability) return;
var abilities;
if(Array.isArray(card.ability)){
abilities = card.ability.slice();
}
else {
abilities = [];
abilities.push(card.ability);
}
2015-07-04 08:44:42 +00:00
abilities = abilities.map((ability) =>{
2015-07-03 16:48:37 +00:00
return abilityData[ability].description;
})
return abilities;
2015-06-13 19:36:02 +00:00
}
});
let Lobby = Backbone.View.extend({
2015-06-13 19:36:02 +00:00
defaults: {
id: ""
},
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;
2015-07-01 17:51:09 +00:00
this.app.receive("update:playerOnline", this.renderStatus.bind(this));
this.listenTo(this.app.user, "change:serverOffline", this.render);
2015-07-02 17:21:24 +00:00
this.listenTo(this.app.user, "change:name", this.setName);
2015-06-29 17:57:51 +00:00
$(".gwent-battle").html(this.el);
2015-06-13 19:36:02 +00:00
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",
2015-06-29 17:57:51 +00:00
"change #deckChoice": "setDeck",
"click .note": "debugNote"
},
2015-07-01 16:55:50 +00:00
debugNote: function(){
2015-06-30 12:47:09 +00:00
new Notification({message: "yoyo TEST\nhallo\n\ntest"}).render();
2015-06-13 19:36:02 +00:00
},
render: function(){
this.$el.html(this.template(this.user.attributes));
2015-07-01 16:55:50 +00:00
this.$el.find("#deckChoice").val(this.user.get("deck")).attr("selected", true);
2015-06-13 19:36:02 +00:00
return this;
},
2015-06-24 16:22:56 +00:00
startMatchmaking: function(){
2015-07-01 17:51:09 +00:00
this.$el.find(".image-gif-loader").show();
2015-06-24 16:22:56 +00:00
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){
let val = $(e.target).val();
2015-06-19 12:14:37 +00:00
this.app.trigger("setDeck", val);
this.$el.find("#deckChoice option[value='" + val + "']").attr("selected", "selected")
},
2015-07-02 17:21:24 +00:00
setName: function(){
/*let val = $(e.target).val();
this.app.trigger("setDeck", val);
this.$el.find("#deckChoice option[value='" + val + "']").attr("selected", "selected")*/
localStorage["userName"] = this.app.user.get("name");
/*this.render();*/
this.$el.find(".name-input").val(this.app.user.get("name"));
},
2015-06-13 19:36:02 +00:00
changeName: function(e){
let name = $(e.target).val();
2015-06-13 19:36:02 +00:00
this.app.trigger("setName", name);
2015-07-01 17:51:09 +00:00
},
2015-07-02 16:17:33 +00:00
renderStatus: function(n){
2015-07-01 17:51:09 +00:00
this.$el.find(".nr-player-online").html(n);
2015-06-13 19:36:02 +00:00
}
});
2015-06-25 19:34:51 +00:00
let Preview = Backbone.View.extend({
template: require("../templates/preview.handlebars"),
initialize: function(opt){
this.card = cardData[opt.key];
2015-07-03 16:48:37 +00:00
this.size = opt.size || "lg";
this.previewB = opt.previewB || false;
this.$el.addClass(this.previewB ? "preview-b" : "");
2015-06-25 19:34:51 +00:00
if(!this.card || !this.card.ability) return;
if(Array.isArray(this.card.ability)){
this.abilities = this.card.ability.slice();
}
else {
this.abilities = [];
this.abilities.push(this.card.ability);
}
2015-07-04 08:44:42 +00:00
this.abilities = this.abilities.map((ability) =>{
2015-06-25 19:34:51 +00:00
return abilityData[ability].description;
})
"lol";
},
render: function(){
let html = this.template({
card: this.card,
2015-07-03 16:48:37 +00:00
abilities: this.abilities,
size: this.size,
previewB: this.previewB
2015-06-25 19:34:51 +00:00
})
this.$el.html(html);
return this;
}
});
2015-06-29 17:57:51 +00:00
let Notification = Backbone.View.extend({
className: "notification",
template: require("../templates/notification.handlebars"),
2015-06-30 11:43:03 +00:00
events: {
"click .alert": "onClick"
},
2015-06-29 17:57:51 +00:00
initialize: function(opt){
this.opt = opt;
$(".notifications").append(this.el);
},
render: function(){
this.$el.html(this.template(this.opt));
this.show();
return this;
},
show: function(){
2015-06-30 12:47:09 +00:00
let $alert = this.$el.find(".alert");
2015-07-01 07:17:29 +00:00
$alert.slideDown(600).delay(Config.Gwent.notification_duration).queue(this.hide.bind(this));
2015-06-29 17:57:51 +00:00
},
hide: function(){
2015-06-30 12:47:09 +00:00
let $alert = this.$el.find(".alert");
$alert.stop().slideUp().queue(this.remove.bind(this));
2015-06-30 11:43:03 +00:00
},
2015-07-01 16:55:50 +00:00
onClick: function(){
2015-06-30 12:47:09 +00:00
this.hide();
2015-06-29 17:57:51 +00:00
}
});
2015-06-13 19:36:02 +00:00
module.exports = App;