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"), templateCardpiles: require("../templates/cardpiles.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); /*let $deck = $(this.side + " .field-deck"); $deck*/ this.$deck = $(this.side + ".right-side"); this.$deck.html(this.templateCardpiles({ data: d })); 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.templateCards([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($field.find(".field-close"), 5); }, 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.templateCards([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($field.find(".field-range"), 5); }, 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.templateCards([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($field.find(".field-siege"), 5); }, 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)); this.battleView.calculateMargin($weatherField, 0); return this; } /*, lives: function(lives){ let out = ""; for(let i = 0; i < 2; i++) { out += "= 0 ? minSize : 6; var Class = $container.find(".card-wrap").length ? ".card-wrap" : ".card"; var n = $container.children().size(); let w = $container.width(), c = $container.find(Class).outerWidth(true); let res; if(n < minSize) res = 0; else { res = -((w - c) / (n - 1) - c) + 1; } $container.find(Class).not(Class+":first-child").css("margin-left", -res); } }); let Modal = Backbone.Modal.extend({ template: require("../templates/modal.handlebars"), cancelEl: ".bbm-close", cancel: function(){ this.model.set("openDiscard", false); } }); let MedicModal = Modal.extend({ template: require("../templates/modal.medic.handlebars"), events: { "click .card": "onCardClick" }, onCardClick: function(e){ //console.log($(e.target).closest(".card")); let 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); } }); 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({ 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")); let 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); } }); let WinnerModal = Modal.extend({ template: require("../templates/modal.winner.handlebars") }); let ChooseSideModal = Modal.extend({ template: require("../templates/modal.side.handlebars"), events: { "click .btn": "onBtnClick" }, beforeCancel: function(){ return false; }, onBtnClick: function(e){ var id = $(e.target).data().id; this.model.set("chooseSide", false); if(id === "you"){ //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({ defaults: { name: typeof localStorage["userName"] === "string" ? localStorage["userName"].slice(0, 20) : null, deck: localStorage["userDeck"] || "random", serverOffline: true }, initialize: function(){ let self = this; let user = this; let app = user.get("app"); self.set("chooseSide", false); this.listenTo(this.attributes, "change:room", this.subscribeRoom); 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); self.set("roomFoeSide", data.foeSide); /* self.set("channel:battle", app.socket.subscribe(self.get("room")));*/ //app.navigate("battle", {trigger: true}); app.battleRoute(); }) app.receive("response:joinRoom", function(roomID){ self.set("room", roomID); //console.log("room id", self.get("room")); }) app.receive("set:waiting", function(data){ let waiting = data.waiting; self.set("waiting", waiting); }) app.receive("set:passing", function(data){ let passing = data.passing; self.set("passing", passing); }) app.receive("foe:left", function(){ //console.log("your foe left the room"); $(".container").prepend('
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){ name = name.slice(0, 20); 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 }) }, getCardData: function(card){ if(!card || !card.ability) return; var abilities; if(Array.isArray(card.ability)){ abilities = card.ability.slice(); } else { abilities = []; abilities.push(card.ability); } abilities = abilities.map((ability) =>{ return abilityData[ability].description; }) return abilities; } }); 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); this.listenTo(this.app.user, "change:name", this.setName); $(".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") }, 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")); }, 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]; this.size = opt.size || "lg"; this.previewB = opt.previewB || false; this.$el.addClass(this.previewB ? "preview-b" : ""); 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, size: this.size, previewB: this.previewB }) 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;