1
0
mirror of https://github.com/exane/not-gwent-online synced 2024-10-31 10:36:53 +00:00
not-gwent-online/public/js/client.js

496 lines
13 KiB
JavaScript
Raw Normal View History

2015-06-15 19:03:12 +00:00
/*("http://localhost:16918")*/
var socketCluster = require("socketcluster-client");
2015-06-13 19:36:02 +00:00
var Backbone = require("backbone");
var Handlebars = require("handlebars");
var $ = require("jquery");
//var Lobby = require("./client-lobby");
2015-06-13 22:33:20 +00:00
window.$ = $;
2015-06-13 19:36:02 +00:00
Handlebars.registerHelper("health", function(lives, options){
var out = "";
for(var i = 0; i < 2; i++) {
out += "<i";
if(i < lives){
out += " class='ruby'";
}
out += "></i>";
}
return out;
});
var Config = {};
Config.Server = {
2015-06-15 19:03:12 +00:00
"hostname": "localhost",
"port": 16918,
secure: false
2015-06-13 19:36:02 +00:00
}
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(){
2015-06-15 19:03:12 +00:00
this.socket = socketCluster.connect(Config.Server);
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;
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;
}
});
2015-06-13 22:33:20 +00:00
var SideView = Backbone.View.extend({
el: ".container",
2015-06-14 18:50:53 +00:00
template: Handlebars.compile('<div class="card" data-key="{{_key}}" data-id="{{_id}}">' +
'<img src="../assets/cards/{{_data.img}}.png">' +
'</div>'),
2015-06-14 14:01:25 +00:00
templateCards: Handlebars.compile('{{#each this}}' +
2015-06-14 18:50:53 +00:00
'<div class="card" data-key="{{_key}}" data-id="{{_id}}">' +
'{{#if _boost}}<span>+{{_boost}}</span>{{/if}}' +
2015-06-14 14:01:25 +00:00
'<img src="../assets/cards/{{_data.img}}.png">' +
'</div>' +
'{{/each}}'),
2015-06-13 22:33:20 +00:00
initialize: function(options){
var self = this;
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(){
2015-06-13 22:33:20 +00:00
var d = this.infoData;
2015-06-14 14:01:25 +00:00
var l = this.leader;
2015-06-13 22:33:20 +00:00
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));
2015-06-14 14:01:25 +00:00
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");
}
2015-06-15 19:03:12 +00:00
this.$info.find(".passing").html(d.passing ? "Passed" : "");
2015-06-14 18:50:53 +00:00
2015-06-14 14:01:25 +00:00
},
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 html = this.templateCards(cards);
$field.find(".field-close").html(html)
$field.find(".large-field-counter").html(score)
},
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 html = this.templateCards(cards);
$field.find(".field-range").html(html)
$field.find(".large-field-counter").html(score)
},
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 html = this.templateCards(cards);
$field.find(".field-siege").html(html)
$field.find(".large-field-counter").html(score)
2015-06-13 22:33:20 +00:00
},
2015-06-14 18:50:53 +00:00
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;
},
2015-06-13 22:33:20 +00:00
lives: function(lives){
var out = "";
for(var i = 0; i < 2; i++) {
out += "<i";
if(i < lives){
out += " class='ruby'";
}
out += "></i>";
}
return out;
}
});
2015-06-13 19:36:02 +00:00
var BattleView = Backbone.View.extend({
className: "container",
template: Handlebars.compile($("#battle-template").html()),
initialize: function(options){
var self = this;
var user = this.user = options.user;
var app = this.app = options.app;
2015-06-13 22:33:20 +00:00
var yourSide, otherSide;
2015-06-13 19:36:02 +00:00
$(this.el).prependTo('body');
2015-06-13 20:04:44 +00:00
this.listenTo(user, "change:showPreview", this.render);
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-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-06-15 19:03:12 +00:00
/*//this.battleChannel = app.socket.subscribe()
2015-06-13 22:33:20 +00:00
app.receive("update:hand", function(data){
2015-06-13 19:36:02 +00:00
if(user.get("roomSide") == data._roomSide){
self.handCards = JSON.parse(data.cards);
self.render();
}
});
2015-06-13 22:33:20 +00:00
app.receive("update:info", function(data){
var _side = data._roomSide;
var infoData = data.info;
2015-06-14 14:01:25 +00:00
var leader = data.leader;
2015-06-13 22:33:20 +00:00
var side = yourSide;
if(user.get("roomSide") != _side){
side = otherSide;
}
side.infoData = infoData;
2015-06-14 14:01:25 +00:00
side.leader = leader;
side.render();
});
app.receive("update:fields", function(data){
var close, ranged, siege;
var _side = data._roomSide;
var side = yourSide;
if(user.get("roomSide") != _side){
side = otherSide;
}
side.field.close = data.close;
side.field.ranged = data.ranged;
side.field.siege = data.siege;
2015-06-14 18:50:53 +00:00
side.field.weather = data.weather;
2015-06-14 14:01:25 +00:00
2015-06-13 22:33:20 +00:00
side.render();
2015-06-15 19:03:12 +00:00
})*/
2015-06-13 22:33:20 +00:00
2015-06-13 19:36:02 +00:00
var interval = setInterval(function(){
if(!user.get("room")) return;
2015-06-15 19:03:12 +00:00
this.setUpBattleEvents(user.get("room"));
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
yourSide = this.yourSide = new SideView({side: ".player", app: this.app, battleView: this});
otherSide = this.otherSide = new SideView({side: ".foe", app: this.app, battleView: this});
/*yourSide = this.yourSide = new SideView({side: ".player", app: app, battleView: this});
otherSide = this.otherSide = new SideView({side: ".foe", app: app, battleView: this});*/
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",
"click .battleside.player": "onClickDecoy",
"click .button-pass": "onPassing"
},
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-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;
var self = this;
2015-06-14 14:01:25 +00:00
var $card = $(e.target).closest(".card");
var id = $card.data("id");
2015-06-14 18:50:53 +00:00
var key = $card.data("key");
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"){
console.log("its decoy!!!");
this.user.set("waitForDecoy", id);
/*
this.$el.find(".battleside.player").on("click", ".card", function(e) {
console.log("replacement card found: ");
var $card = $(e.target).closest(".card");
var _id = $card.data("id");
self.app.send("decoy:replaceWith", {
oldCard: id,
newCard: _id
})
self.$el.find(".battleside.player").off("click");
});*/
}
},
onClickDecoy: function(e){
if(!this.user.get("waitForDecoy")) return;
console.log("replacement card found: ");
var $card = $(e.target).closest(".card");
var _id = $card.data("id");
this.app.send("decoy:replaceWith", {
cardID: _id
})
this.user.set("waitForDecoy", false);
2015-06-13 20:04:44 +00:00
},
onMouseover: function(e){
var target = $(e.target).closest(".card");
this.user.set("showPreview", target.find("img").attr("src"));
},
2015-06-13 22:33:20 +00:00
onMouseleave: function(e){
2015-06-13 20:04:44 +00:00
this.user.set("showPreview", null);
},
2015-06-13 19:36:02 +00:00
render: function(){
var self = this;
this.$el.html(this.template({
2015-06-13 20:04:44 +00:00
cards: self.handCards,
preview: self.user.get("showPreview")
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();
/* this.$el()
if(!(this.yourSide && this.otherSide))
return this;
this.yourSide.render();
this.otherSide.render();*/
2015-06-13 19:36:02 +00:00
return this;
2015-06-15 19:03:12 +00:00
},
setUpBattleEvents: function(channelName){
this.battleChannel = this.app.socket.subscribe(channelName);
var self = this;
var user = this.user;
this.battleChannel.watch(function(d){
var event = d.event, data = d.data;
if(event === "update:hand"){
if(user.get("roomSide") == data._roomSide){
self.handCards = JSON.parse(data.cards);
self.render();
}
}
else if(event === "update:info"){
var _side = data._roomSide;
var infoData = data.info;
var leader = data.leader;
var side = self.yourSide;
if(user.get("roomSide") != _side){
side = self.otherSide;
}
side.infoData = infoData;
side.leader = leader;
side.render();
}
else if(event === "update:fields"){
var _side = data._roomSide;
var side = self.yourSide;
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-13 19:36:02 +00:00
}
2015-06-13 22:33:20 +00:00
});
2015-06-13 19:36:02 +00:00
var User = Backbone.Model.extend({
defaults: {
name: ""
},
initialize: function(){
var self = this;
var app = this.get("app");
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){
console.log("opponent found!");
self.set("roomSide", data.side);
2015-06-15 19:03:12 +00:00
/*
self.set("channel:battle", app.socket.subscribe(self.get("room")));*/
2015-06-13 19:36:02 +00:00
app.navigate("battle", {trigger: true});
})
app.receive("response:createRoom", function(roomID){
self.set("room", roomID);
console.log("room created", roomID);
});
app.receive("response:joinRoom", function(roomID){
self.set("room", roomID);
console.log("room id", self.get("room"));
})
2015-06-14 14:01:25 +00:00
app.receive("set:waiting", function(data){
var waiting = data.waiting;
self.set("waiting", waiting);
})
2015-06-13 19:36:02 +00:00
app.on("createRoom", this.createRoom, this);
app.on("joinRoom", this.joinRoom, this);
app.on("setName", this.setName, 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");
},
2015-06-15 19:03:12 +00:00
subscribeRoom: function(){
var room = this.get("room");
var app = this.get("app");
app.socket.subscribe(room);
},
2015-06-13 19:36:02 +00:00
setName: function(name){
this.get("app").send("request:name", {name: name});
}
});
var Lobby = Backbone.View.extend({
defaults: {
id: ""
},
className: "container",
template: Handlebars.compile($("#matchmaker-template").html()),
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"
},
render: function(){
this.$el.html(this.template(this.user.attributes));
return this;
},
createRoom: function(){
this.app.trigger("createRoom");
},
joinRoom: function(){
this.app.trigger("joinRoom");
},
changeName: function(e){
var name = $(e.target).val();
this.app.trigger("setName", name);
}
});
module.exports = App;