let socket = require("socket.io-client"); let Backbone = require("backbone"); require("./backbone.modal-min"); let Handlebars = require('handlebars/runtime').default; let $ = require("jquery"); let cardData = require("../../assets/data/cards"); let abilityData = require("../../assets/data/abilities"); window.$ = $; Handlebars.registerPartial("card", require("../templates/cards.handlebars")); Handlebars.registerHelper("health", function(lives){ let out = ""; for(let i = 0; i < 2; i++) { out += "{ let key = card._key; if(key === "biting_frost") isInfluencedByWeather = true; }) if(isInfluencedByWeather){ $field.addClass("field-frost"); } calculateCardMargin($field.find(".card"), 351, 70, cards.length); }, 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; let html = this.templateCards(cards); $field.find(".field-range").html(html) $field.find(".large-field-counter").html(score) if(horn){ this.$fields.find(".field-horn-range").html(this.template(horn)); } let isInfluencedByWeather; this.field.weather.cards.forEach((card) =>{ let key = card._key; if(key === "impenetrable_fog") isInfluencedByWeather = true; }) if(isInfluencedByWeather){ $field.addClass("field-fog"); } calculateCardMargin($field.find(".card"), 351, 70, cards.length); }, 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; let html = this.templateCards(cards); $field.find(".field-siege").html(html) $field.find(".large-field-counter").html(score) if(horn){ this.$fields.find(".field-horn-siege").html(this.template(horn)); } let isInfluencedByWeather; this.field.weather.cards.forEach((card) =>{ let key = card._key; if(key === "torrential_rain") isInfluencedByWeather = true; }) if(isInfluencedByWeather){ $field.addClass("field-rain"); } calculateCardMargin($field.find(".card"), 351, 70, cards.length); }, renderWeatherField: function(){ if(!this.field.weather) return; let $weatherField = this.$el.find(".field-weather"); let cards = this.field.weather.cards; $weatherField.html(this.templateCards(cards)); return this; } /*, lives: function(lives){ let out = ""; for(let i = 0; i < 2; i++) { out += "Your foe left the battle!') }) app.receive("played:medic", function(data){ let cards = JSON.parse(data.cards); //console.log("played medic"); self.set("medicDiscard", { cards: cards }); }) app.receive("played:agile", function(data){ //console.log("played agile"); self.set("setAgile", data.cardID); }) app.receive("played:horn", function(data){ //console.log("played horn"); self.set("setHorn", data.cardID); }) app.receive("redraw:cards", function(){ self.set("isReDrawing", true); }) app.receive("redraw:close", function(){ self.set("isReDrawing", false); }) 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.receive("gameover", function(data){ let winner = data.winner; //console.log("gameover"); let model = Backbone.Model.extend({}); let modal = new WinnerModal({model: new model({winner: winner})}); $("body").prepend(modal.render().el); }) app.on("startMatchmaking", this.startMatchmaking, this); app.on("joinRoom", this.joinRoom, this); app.on("setName", this.setName, this); app.on("setDeck", this.setDeck, this); app.send("request:name", this.get("name") == "unnamed" ? null : {name: this.get("name")}); }, startMatchmaking: function(){ this.set("inMatchmakerQueue", true); this.get("app").send("request:matchmaking"); }, joinRoom: function(){ this.get("app").send("request:joinRoom"); this.set("inMatchmakerQueue", false); }, subscribeRoom: function(){ let room = this.get("room"); let app = this.get("app"); //app.socket.subscribe(room); }, setName: function(name){ this.get("app").send("request:name", {name: name}); }, setDeck: function(deckKey){ //console.log("deck: ", deckKey); this.set("deckKey", deckKey); this.get("app").send("set:deck", {deck: deckKey}); } }); let Lobby = Backbone.View.extend({ defaults: { id: "" }, className: "container", template: require("../templates/lobby.handlebars"), 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: { "click .startMatchmaking": "startMatchmaking", /*"click .join-room": "joinRoom",*/ "blur .name-input": "changeName", "change #deckChoice": "setDeck" }, render: function(){ this.$el.html(this.template(this.user.attributes)); /*this.$el.find("#deckChoice option[value='" + this.app.user.get("setDeck") + "']").attr("selected", "selected")*/ return this; }, startMatchmaking: function(){ this.app.trigger("startMatchmaking"); }, joinRoom: function(){ this.app.trigger("joinRoom"); }, setDeck: function(e){ let val = $(e.target).val(); this.app.trigger("setDeck", val); this.$el.find("#deckChoice option[value='" + val + "']").attr("selected", "selected") }, changeName: function(e){ let name = $(e.target).val(); this.app.trigger("setName", name); } }); let Preview = Backbone.View.extend({ template: require("../templates/preview.handlebars"), initialize: function(opt){ this.card = cardData[opt.key]; 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); } this.abilities = this.abilities.map((ability) =>{ return abilityData[ability].description; }) "lol"; }, render: function(){ let html = this.template({ card: this.card, abilities: this.abilities }) this.$el.html(html); return this; } }); module.exports = App;