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 += ""; }) return out; }); let App = Backbone.Router.extend({ routes: { /*"lobby": "lobbyRoute", "battle": "battleRoute", "*path": "defaultRoute"*/ }, initialize: function(){ let self = this; this.connect(); this.user = new User({app: this}); /*Backbone.history.start();*/ this.lobbyRoute(); }, connect: function(){ this.socket = socket(Config.Server.hostname + ":" + Config.Server.port); 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); }) }, receive: function(event, cb){ this.socket.on(event, cb); }, /* receiveOnce: function(event, cb){ this.socket.once(event, cb); },*/ send: function(event, data){ data = data || null; let 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){ let regex = /(\w+):?(\w*)\|?/g; let res = {}; let r; while(r = regex.exec(event)) { res[r[1]] = r[2]; } return res; } }); let SideView = Backbone.View.extend({ el: ".container", template: require("../templates/cards.handlebars"), templateCards: require("../templates/fieldCards.handlebars"), templateInfo: require("../templates/info.handlebars"), initialize: function(options){ let self = this; this.side = options.side; this.app = options.app; this.battleView = options.battleView; this.infoData = this.infoData || {}; this.leader = this.leader || {}; this.field = this.field || {}; }, render: function(){ this.renderInfo(); this.renderCloseField(); this.renderRangeField(); this.renderSiegeField(); this.renderWeatherField(); return this; }, renderInfo: function(){ let d = this.infoData; let l = this.leader; let html = this.templateInfo({ data: d, leader: l, passBtn: this.side === ".player" }) this.$info = this.$el.find(".game-info" + this.side).html(html); 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; let html = this.templateCards(cards); $field.find(".field-close").html(html) $field.find(".large-field-counter").html(score) if(horn){ this.$fields.find(".field-horn-close").html(this.template(horn)); } let isInfluencedByWeather; this.field.weather.cards.forEach((card) =>{ let key = card._key; if(key === "biting_frost") isInfluencedByWeather = true; }) if(isInfluencedByWeather){ $field.addClass("field-frost"); } //calculateCardMargin($field.find(".card"), 351, 70, cards.length); this.battleView.calculateMargin(this.$el.find(".field-close")); }, 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); this.battleView.calculateMargin(this.$el.find(".field-range")); }, 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); this.battleView.calculateMargin(this.$el.find(".field-siege")); }, 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); self.set("medicDiscard", { cards: cards }); }) app.receive("played:emreis_leader4", function(data){ let cards = JSON.parse(data.cards); self.set("emreis_leader4", { 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.receive("request:chooseWhichSideBegins", function(){ self.set("chooseSide", true); }) 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.receive("notification", function(data){ new Notification(data).render(); }) 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")}); }, 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}); localStorage["userName"] = name; }, setDeck: function(deckKey){ //console.log("deck: ", deckKey); this.set("deckKey", deckKey); localStorage["userDeck"] = deckKey; this.get("app").send("set:deck", {deck: deckKey}); }, chooseSide: function(roomSide){ this.get("app").send("response:chooseWhichSideBegins", { side: roomSide }) } }); let Lobby = Backbone.View.extend({ defaults: { id: "" }, template: require("../templates/lobby.handlebars"), initialize: function(options){ this.user = options.user; this.app = options.app; this.app.receive("update:playerOnline", this.renderStatus.bind(this)); this.listenTo(this.app.user, "change:serverOffline", this.render); $(".gwent-battle").html(this.el); this.render(); }, events: { "click .startMatchmaking": "startMatchmaking", /*"click .join-room": "joinRoom",*/ "blur .name-input": "changeName", "change #deckChoice": "setDeck", "click .note": "debugNote" }, debugNote: function(){ new Notification({message: "yoyo TEST\nhallo\n\ntest"}).render(); }, render: function(){ this.$el.html(this.template(this.user.attributes)); this.$el.find("#deckChoice").val(this.user.get("deck")).attr("selected", true); return this; }, startMatchmaking: function(){ this.$el.find(".image-gif-loader").show(); 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); }, renderStatus: function(n){ this.$el.find(".nr-player-online").html(n); } }); 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; } }); let Notification = Backbone.View.extend({ className: "notification", template: require("../templates/notification.handlebars"), events: { "click .alert": "onClick" }, 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(){ let $alert = this.$el.find(".alert"); $alert.slideDown(600).delay(Config.Gwent.notification_duration).queue(this.hide.bind(this)); }, hide: function(){ let $alert = this.$el.find(".alert"); $alert.stop().slideUp().queue(this.remove.bind(this)); }, onClick: function(){ this.hide(); } }); module.exports = App;