-
Notifications
You must be signed in to change notification settings - Fork 7
/
map.js
118 lines (99 loc) · 3.82 KB
/
map.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
$(function(){
if(!$('div#pokemap').length) return;
var Pokemap = Backbone.View.extend({
el: 'div#pokemap',
events: {},
markers: {},
initialize: function(){
this.listenTo(Backbone, 'printPokemon', this.printPokemon);
this.listenTo(Backbone, 'destroyPokemon', this.destroyPokemon);
this.listenTo(Backbone, 'serverStatus', this.printServerStatus);
this.MarkerStyle = L.Icon.extend({
options: {
iconSize : [40, 30],
iconAnchor : [40, 30],
popupAnchor: [-25, -20]
}
});
this.getTokens();
},
getTokens: function(){
app.proxy('GET', '/carto-viz', {}, this.startCarto, this._error, this);
},
startCarto: function(data){
var that = this;
cartodb.createVis('pokemap', data.viz, {carto_logo:false}).done(function(vis, layers) {
that.map = vis.getNativeMap();
that.setAttribution(data.attribution);
Backbone.trigger('getPokemons');
that.listenToBotPosition();
that.listenToPokemonServerStatus();
});
},
setAttribution: function(attributionHTML){
this.$('div.leaflet-bottom.leaflet-right').html(attributionHTML);
},
listenToBotPosition: function(){
/* AVOID SERVER CRASH LOST SOCKET :( */
io.socket.on('connect', function(){
io.socket.get('/bot', function(){});
});
io.socket.get('/bot', this.printBot.bind(this));
io.socket.on('botLocation', this.printBot.bind(this));
},
listenToPokemonServerStatus: function(){
this.$serverStatus = this.$('span.server-status');
/* AVOID SERVER CRASH LOST SOCKET :( */
io.socket.on('connect', function(){
io.socket.get('/pokemon-server', function(){});
});
io.socket.get('/pokemon-server', this.printServerStatus.bind(this));
io.socket.on('serverStatus', this.printServerStatus.bind(this));
},
printBot: function(bots){
return;
// Remove egg functionality until I get a better solution to avoid the bann
if(!bots) return;
var botsNames = Object.keys(bots);
_.each(botsNames, function(botName){
if(this.markers[botName]) this.map.removeLayer(this.markers[botName]);
var pokeIcon = new this.MarkerStyle({iconUrl: '/img/pokemons/egg.png'});
var marker = L.marker([bots[botName].latitude, bots[botName].longitude], {icon: pokeIcon}).addTo(this.map);
this.markers[botName] = marker;
}, this);
},
printPokemon: function(pokemon){
var text = '<b>' + pokemon.name + '</b><br>';
if(pokemon.expiration){
var date = new Date(pokemon.expiration);
var hours = date.getHours()
var minutes = date.getMinutes();
if(minutes < 10) minutes = '0' + minutes;
var seconds = date.getSeconds();
if(seconds < 10) seconds = '0' + seconds;
var time = hours + ":" + minutes + ":" + seconds;
text += 'Hasta: <b>' + time + '</b>';
}
var pokeIcon = new this.MarkerStyle({iconUrl: '/img/pokemons/' + parseInt(pokemon.num) + '.png'});
var marker = L.marker([pokemon.latitude, pokemon.longitude], {icon: pokeIcon})
.addTo(this.map)
.bindPopup(text);
marker.pokemonId = pokemon.id;
this.markers[pokemon.id] = marker;
},
printServerStatus: function(status){
if(!this.$serverStatus || !this.$serverStatus.length) return;
this.$serverStatus.html('Pokemon server: ' + status.toUpperCase() + ' ');
if(status === 'online') return this.$serverStatus.css('color', 'green');
this.$serverStatus.css('color', 'red');
},
destroyPokemon: function(pokemonId){
var marker = this.markers[pokemonId];
this.map.removeLayer(marker);
},
_error: function(err){
console.log(err);
}
});
var pokemap = new Pokemap();
});