/*("http://localhost:16918")*/ var socketCluster = require("socketcluster-client"); var Backbone = require("backbone"); require("./backbone.modal-min"); var Handlebars = require("handlebars"); var $ = require("jquery"); //var Lobby = require("./client-lobby"); window.$ = $; var App = Backbone.Router.extend({ routes: { "lobby": "lobbyRoute", "battle": "battleRoute", "*path": "defaultRoute" }, initialize: function(){ var self = this; this.connect(); this.user = new User({app: this}); Backbone.history.start(); }, connect: function(){ this.socket = socketCluster.connect(Config.Server); }, 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; 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; } }); var SideView = Backbone.View.extend({ el: ".container", template: require("../templates/cards.handlebars"), templateCards: require("../templates/multipleCards.handlebars"), initialize: function(options){ var 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(){ var d = this.infoData; var l = this.leader; this.$info = this.$el.find(".game-info" + this.side); this.$info.find(".info-name").html(d.name); this.$info.find(".score").html(d.score); this.$info.find(".hand-card").html(d.hand); this.$info.find(".gwent-lives").html(this.lives(d.lives)); if(l._key){ this.$info.find(".field-leader").html(this.template(l)) } 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"); } this.$info.find(".passing").html(d.passing ? "Passed" : ""); }, renderCloseField: function(){ if(!this.field.close) return; this.$fields = this.$el.find(".battleside" + this.side); var $field = this.$fields.find(".field-close").parent(); var cards = this.field.close.cards; var score = this.field.close.score; var horn = this.field.close.horn; var 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)); } calculateCardMargin($field.find(".card"), 351, 70, cards.length); }, renderRangeField: function(){ if(!this.field.ranged) return; this.$fields = this.$el.find(".battleside" + this.side); var $field = this.$fields.find(".field-range").parent(); var cards = this.field.ranged.cards; var score = this.field.ranged.score; var horn = this.field.ranged.horn; var 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)); } calculateCardMargin($field.find(".card"), 351, 70, cards.length); }, renderSiegeField: function(){ if(!this.field.siege) return; this.$fields = this.$el.find(".battleside" + this.side); var $field = this.$fields.find(".field-siege").parent(); var cards = this.field.siege.cards; var score = this.field.siege.score; var horn = this.field.siege.horn; var 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)); } calculateCardMargin($field.find(".card"), 351, 70, cards.length); }, renderWeatherField: function(){ if(!this.field.weather) return; var $weatherField = this.$el.find(".field-weather"); var cards = this.field.weather.cards; $weatherField.html(this.templateCards(cards)); return this; }, lives: function(lives){ var out = ""; for(var i = 0; i < 2; i++) { out += "Your foe left the battle!') }) app.receive("played:medic", function(data){ var 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.on("createRoom", this.createRoom, 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")}); }, createRoom: function(){ this.get("app").send("request:createRoom"); }, joinRoom: function(){ this.get("app").send("request:joinRoom"); }, subscribeRoom: function(){ var room = this.get("room"); var 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}); } }); var 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 .create-room": "createRoom", "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; }, createRoom: function(){ this.app.trigger("createRoom"); }, joinRoom: function(){ this.app.trigger("joinRoom"); }, setDeck: function(e){ var val = $(e.target).val(); this.app.trigger("setDeck", val); this.$el.find("#deckChoice option[value='" + val + "']").attr("selected", "selected") }, changeName: function(e){ var name = $(e.target).val(); this.app.trigger("setName", name); } }); module.exports = App;