-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrtc.js
More file actions
103 lines (86 loc) · 2.86 KB
/
rtc.js
File metadata and controls
103 lines (86 loc) · 2.86 KB
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
var eventemitter = require('events');
var config = require('./config');
var fs = require('fs');
if (config.telegram){
var TelegramBot = require('node-telegram-bot-api');
var telegram = require('./telegram');
}
var pokedex = require('./pokedex');
exports = module.exports = {};
var io;
exports.event = new eventemitter();
var loc;
exports.init = function(socketio){
var self = this;
if(config.telegram){
var bot = new TelegramBot(config.telegram_token, {polling: true});
if(telegram.chatid == "" || typeof telegram.chatid === "undefined"){
console.log("Register with telegram bot: http://telegram.me/Blastoise_bot");
}
bot.onText(/\/start/, function(msg, match){
telegram.chatid = msg.chat.id;
fs.writeFile("telegram.json", JSON.stringify(telegram), function(){
bot.sendMessage(telegram.chatid, "Registered with Blastoise");
});
})
}
io = socketio;
io.on('connection', function(socket){
self.event.emit('connection', socket);
io.emit('setInitialLocation', loc)
});
io.on('disconnect', function(socket){
self.event.emit('disconnect', socket);
});
this.event.on('setLocation', function(data){
if(loc == "undefined"){
loc = {
lat: data.latitude,
lng: data.longitude
};
io.emit('setInitialLocation', loc);
}else{
loc = {
lat: data.latitude,
lng: data.longitude
};
io.emit('setLocation', loc);
}
});
this.event.on('showCatch', function(data){
io.emit('showCatch', data);
if(config.telegram) bot.sendMessage(telegram.chatid, "Caught " + data.cp +"CP " + data.name);
});
this.event.on('showSpin', function(data){
var items = [];
for(item in data.items){
items.push(data.items[item].item_id)
}
io.emit('showSpin', items);
// if(config.telegram) bot.sendMessage(telegram.chatid, "Spin \n" + JSON.stringify(data));
});
var lastempty = false;
this.event.on('displayPokemon', function(catchable){
if(catchable.length < 1 && lastempty) return;
if(catchable.length < 1 && !lastempty) {
lastempty = true;
}else{
lastempty = false;
}
let display = [];
for(let p in catchable){
let data = catchable[p];
let pokedexentry = pokedex.getPokemon(data.pokemon_id);
display.push({
name: pokedexentry.name,
id: pokedexentry.id,
loc: {
lat: data.latitude,
lng: data.longitude
}
});
}
io.emit('displayPokemon', display);
console.log('sent pokemon');
});
};