From c70009737194b79ff4f06830c553798028bbdbbb Mon Sep 17 00:00:00 2001 From: Ro Date: Mon, 22 Aug 2016 21:29:10 -0400 Subject: [PATCH 01/14] Update main.js --- map-chat/javascript/main.js | 54 ++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 31 deletions(-) diff --git a/map-chat/javascript/main.js b/map-chat/javascript/main.js index 2d0652aceb..9470b6e87d 100644 --- a/map-chat/javascript/main.js +++ b/map-chat/javascript/main.js @@ -4,7 +4,7 @@ var retryCount = 10; // Support dynamic topic registration by #word var urlHashTopic = location.hash ? location.hash.substring(1).toLowerCase() : null; -var topic = urlHashTopic ? urlHashTopic : "pgomapcatch"; +var topic = urlHashTopic ? urlHashTopic : "pgomapcatch/chat"; function initialiseEventBus(){ window.client = mqtt.connect('ws://test.mosca.io'); // you add a ws:// url here @@ -12,35 +12,26 @@ function initialiseEventBus(){ client.on("message", function(topic, payload) { //alert([topic, payload].join(": ")); - //client.end(); - - Materialize.toast(payload, 2000); - - //@ro: let's grab the message and split that shit. (simple for now, maybe we could just parse the json instead) - var pLoadR = payload.toString(); - var pLoadR2 = pLoadR.split(","); - var olat = pLoadR2[0] - var olong = pLoadR2[1] - - displayMessageOnMap(payload, olat, olong); + console.log('Topic is '+topic) + + Materialize.toast(payload, 4000); + if(topic === 'pgomapcatch/chat'){ + console.log('Chatting event') + displayChatMessageOnMap(payload) + } else { + + //@ro: let's grab the message and split that shit. (simple for now, maybe we could just parse the json instead) + var pLoadR = payload.toString(); + var pLoadR2 = pLoadR.split(","); + var olat = pLoadR2[0] + var olong = pLoadR2[1] + var sessid = pLoadR2[2] + + displayMessageOnMap(payload, olat, olong, sessid); + } }); - client.publish("pgomapcatch", "I just connected to the map!"); - /*eb = new vertx.EventBus("http://localhost:8080/chat"); - - eb.onopen = function () { - subscribe(topic); - }; - - eb.onclose = function(){ - if (retryCount) { - retryCount--; - console.log('Connection lost, scheduling reconnect'); - setTimeout(initialiseEventBus, 1000); - } else{ - Materialize.toast('Connection lost, please refresh :( ', 10000); - } - };*/ + client.publish("pgochat/join", "I just connected to the map!"); } function sendMessage(topic, input) { @@ -53,7 +44,8 @@ function sendMessage(topic, input) { function publish(address, message) { if (window.client) { var json = createMessage(message); - window.client.publish(address, json); + window.client.publish(address, JSON.stringify(json.text)); + console.log(json); } } @@ -67,13 +59,13 @@ $( document ).ready(function() { var input = $("#input"); input.keyup(function (e) { if (e.keyCode == 13) { - sendMessage(topic, input); + sendMessage('pgomapcatch/chat', input); } }); input.focus(); $("#send-button").click(function(){ - sendMessage(topic, input); + sendMessage('pgomapcatch/chat', input); }); $("#notification_lever").change(function() { From 861dcda685c6ba11c6cc18967734edb7131f7936 Mon Sep 17 00:00:00 2001 From: Ro Date: Mon, 22 Aug 2016 21:30:10 -0400 Subject: [PATCH 02/14] Update map.js --- map-chat/javascript/map.js | 118 +++++++++++++++++++++++++++++++++---- 1 file changed, 105 insertions(+), 13 deletions(-) diff --git a/map-chat/javascript/map.js b/map-chat/javascript/map.js index b96a8d22fc..edbec7c2e8 100644 --- a/map-chat/javascript/map.js +++ b/map-chat/javascript/map.js @@ -124,17 +124,12 @@ function createMessage(text){ text: text }; } - -function displayMessageOnMap(msg, olat, olong){ - - // @ro: passing values split from incoming payload into two variables for now (lat and long) - var newPosition = new google.maps.LatLng(olat, olong); +function displayChatMessageOnMap(raw){ + var msg = JSON.parse(raw) + console.log(msg) + var newPosition = new google.maps.LatLng(msg.lat,msg.lng); var msgSessionId = msg.sessionId; - - // @ro: just checking the output - console.log(olat); - console.log(olong); - + // xss prevention hack msg.text = html_sanitize(msg.text); @@ -142,13 +137,13 @@ function displayMessageOnMap(msg, olat, olong){ return entityMap[s]; }); - // msg.text = msg.text ? embedTweet(msg.text) : ""; +// msg.text = msg.text ? embedTweet(msg.text) : ""; msg.text = msg.text.replace(/#(\S*)/g,'#$1'); // linkify msg.text = msg.text.replace(/(\b(https?|ftp|file)://[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig, "$1"); - + if(markersMap[msgSessionId]){ // update existing marker var existingMarker = markersMap[msgSessionId].marker; var existingInfoWindow = markersMap[msgSessionId].infoWindow; @@ -196,7 +191,104 @@ function displayMessageOnMap(msg, olat, olong){ if (msg.text) { infoWindow.open(map, marker); - + } + + var timeoutId = setTimeout(function() { infoWindow.close() }, 10000); + markersMap[msgSessionId] = { + marker: marker, + infoWindow: infoWindow, + timeoutId: timeoutId, + disabled: false + } + } + + if (advanced){ + runAdvancedOptions(msg); + } +} + +function displayMessageOnMap(msg, olat, olong, sessid){ + + // @ro: passing values split from incoming payload into two variables for now (lat and long) + var newPosition = new google.maps.LatLng(olat, olong); + var msgSessionId = sessid; + + // @ro: just checking the output + console.log(olat); + console.log(olong); + + // xss prevention hack + msg.text = html_sanitize(msg.text); + + msg.text = String(msg.text).replace(/[&<>"#'\/卐卍]/g, function (s) { + return entityMap[s]; + }); + + // msg.text = msg.text ? embedTweet(msg.text) : ""; + msg.text = msg.text.replace(/#(\S*)/g,'#$1'); + + // linkify + msg.text = msg.text.replace(/(\b(https?|ftp|file)://[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig, + "$1"); + + if(markersMap[msgSessionId]){ // update existing marker + var infoWindow = new google.maps.InfoWindow({ + content: msg.text, + maxWidth: 400, + disableAutoPan: true, + zIndex: infoWindowZIndex + }); + infoWindowZIndex++; + + var marker = new google.maps.Marker({ + position: newPosition, + map: map, + draggable: false, + icon: markerImage, + title: "Click to mute/un-mute User "+msgSessionId + }); + + marker.addListener('click',function() { + if (markersMap[msgSessionId].disabled) { + markersMap[msgSessionId].disabled = false; + marker.setIcon(markerImage); + } else{ + markersMap[msgSessionId].disabled = true; + marker.setIcon(disabledMarkerImage); + infoWindow.close(); + } + }); + } else { // new marker + var infoWindow = new google.maps.InfoWindow({ + content: msg.text, + maxWidth: 400, + disableAutoPan: true, + zIndex: infoWindowZIndex + }); + infoWindowZIndex++; + + var marker = new google.maps.Marker({ + position: newPosition, + map: map, + draggable: false, + icon: markerImage, + title: "Click to mute/un-mute User "+msgSessionId + }); + + marker.addListener('click',function() { + if (markersMap[msgSessionId].disabled) { + markersMap[msgSessionId].disabled = false; + marker.setIcon(markerImage); + } else{ + markersMap[msgSessionId].disabled = true; + marker.setIcon(disabledMarkerImage); + infoWindow.close(); + } + }); + + if (msg.text) { + infoWindow.open(map, marker); + } var timeoutId = setTimeout(function() { infoWindow.close() }, 10000); From 9a684508d0563aba01173a3ed1f8577cbd7e3eb3 Mon Sep 17 00:00:00 2001 From: Ro Date: Mon, 22 Aug 2016 21:30:45 -0400 Subject: [PATCH 03/14] Create social_handler.py --- .../event_handlers/social_handler.py | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 pokemongo_bot/event_handlers/social_handler.py diff --git a/pokemongo_bot/event_handlers/social_handler.py b/pokemongo_bot/event_handlers/social_handler.py new file mode 100644 index 0000000000..ead939620a --- /dev/null +++ b/pokemongo_bot/event_handlers/social_handler.py @@ -0,0 +1,49 @@ +# -*- coding: utf-8 -*- +from pokemongo_bot.event_manager import EventHandler +import thread +import paho.mqtt.client as mqtt +class MyMQTTClass: + def __init__(self, clientid=None): + self._mqttc = mqtt.Client(clientid) + self._mqttc.on_message = self.mqtt_on_message + #self._mqttc.on_connect = self.mqtt_on_connect + #self._mqttc.on_publish = self.mqtt_on_publish + #self._mqttc.on_subscribe = self.mqtt_on_subscribe + #def mqtt_on_connect(self, mqttc, obj, flags, rc): + #print "rc: "+str(rc) + def mqtt_on_message(self, mqttc, obj, msg): + print msg.topic+" "+str(msg.qos)+" "+str(msg.payload) + #def mqtt_on_publish(self, mqttc, obj, mid): + #print "mid: "+str(mid) + #def mqtt_on_subscribe(self, mqttc, obj, mid, granted_qos): + # print "Subscribed: "+str(mid)+" "+str(granted_qos) + #def mqtt_on_log(self, mqttc, obj, level, string): + # print string + def publish(self, channel, message): + self._mqttc.publish(channel, message) + def connect_to_mqtt(self): + self._mqttc.connect("test.mosca.io", 1883, 60) + self._mqttc.subscribe("pgomapcatch/#", 0) + def run(self): + self._mqttc.loop_forever() +class SocialHandler(EventHandler): + def __init__(self, bot): + self.bot = bot + self.mqttc = MyMQTTClass() + self.mqttc.connect_to_mqtt() + thread.start_new_thread(self.mqttc.run) + def handle_event(self, event, sender, level, formatted_msg, data): + #sender_name = type(sender).__name__ + if formatted_msg: + message = "[{}] {}".format(event, formatted_msg) + else: + message = '{}: {}'.format(event, str(data)) + if event == 'catchable_pokemon': + #self.mqttc.publish("pgomapcatch/all", str(data)) + print data + if data['pokemon_id']: + #self.mqttc.publish("pgomapcatch/all/catchable/"+str(data['pokemon_id']), str(data)) + self.mqttc.publish("pgomapcatch/all/catchable/"+str(data['pokemon_id']), str(data['latitude'])+","+str(data['longitude'])+","+str(data['encounter_id'])) + + #print 'have catchable_pokemon' + print message From 57b00270a6e0c5dc1aeacd538ba4bf7073950bf7 Mon Sep 17 00:00:00 2001 From: Ro Date: Mon, 22 Aug 2016 21:31:24 -0400 Subject: [PATCH 04/14] Update __init__.py --- pokemongo_bot/__init__.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pokemongo_bot/__init__.py b/pokemongo_bot/__init__.py index 406644d4ce..75352db823 100644 --- a/pokemongo_bot/__init__.py +++ b/pokemongo_bot/__init__.py @@ -25,7 +25,7 @@ from human_behaviour import sleep from item_list import Item from metrics import Metrics -from pokemongo_bot.event_handlers import LoggingHandler, SocketIoHandler, ColoredLoggingHandler +from pokemongo_bot.event_handlers import LoggingHandler, SocketIoHandler, ColoredLoggingHandler, SocialHandler from pokemongo_bot.socketio_server.runner import SocketIoRunner from pokemongo_bot.websocket_remote_control import WebsocketRemoteControl from pokemongo_bot.base_dir import _base_dir @@ -117,7 +117,8 @@ def _setup_event_system(self): handlers.append(ColoredLoggingHandler()) else: handlers.append(LoggingHandler()) - + if self.config.enable_social: + handlers.append(SocialHandler(self)) if self.config.websocket_server_url: if self.config.websocket_start_embedded_server: self.sio_runner = SocketIoRunner(self.config.websocket_server_url) @@ -1059,14 +1060,14 @@ def get_pos_by_name(self, location_name): possible_coordinates = re.findall( "[-]?\d{1,3}[.]\d{3,7}", location_name ) - if len(possible_coordinates) >= 2: + if len(possible_coordinates) == 2: # 2 matches, this must be a coordinate. We'll bypass the Google # geocode so we keep the exact location. self.logger.info( '[x] Coordinates found in passed in location, ' 'not geocoding.' ) - return float(possible_coordinates[0]), float(possible_coordinates[1]), (float(possible_coordinates[2]) if len(possible_coordinates) == 3 else self.alt) + return float(possible_coordinates[0]), float(possible_coordinates[1]), self.alt geolocator = GoogleV3(api_key=self.config.gmapkey) loc = geolocator.geocode(location_name, timeout=10) From 610bceabbedf7d9570f98df9e3242b2f03ee98a9 Mon Sep 17 00:00:00 2001 From: Ro Date: Mon, 22 Aug 2016 21:32:27 -0400 Subject: [PATCH 05/14] Update pokecli.py --- pokecli.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/pokecli.py b/pokecli.py index 00d8c7ed7e..5a06978029 100644 --- a/pokecli.py +++ b/pokecli.py @@ -570,8 +570,16 @@ def _json_loader(filename): type=float, default=8.0 ) - - + + add_config( + parser, + load, + long_flag="--enable_social", + help="Enable social event exchange between bot", + type=bool, + default=True + ) + # Start to parse other attrs config = parser.parse_args() if not config.username and 'username' not in load: From 04ef9488f6e8e94b531d27b1fedaeab641922234 Mon Sep 17 00:00:00 2001 From: Ro Date: Mon, 22 Aug 2016 21:32:51 -0400 Subject: [PATCH 06/14] Update requirements.txt --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index d41d2e4a20..697e91ba1b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -25,3 +25,4 @@ raven==5.23.0 demjson==2.2.4 greenlet==0.4.9 yoyo-migrations==5.0.3 +paho-mqtt==1.2 From a309e748f6e761b4b758482f9438334d7b7455b8 Mon Sep 17 00:00:00 2001 From: Ro Date: Mon, 22 Aug 2016 21:39:37 -0400 Subject: [PATCH 07/14] Update __init__.py --- pokemongo_bot/event_handlers/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pokemongo_bot/event_handlers/__init__.py b/pokemongo_bot/event_handlers/__init__.py index a4dd6fce7f..68c2636b12 100644 --- a/pokemongo_bot/event_handlers/__init__.py +++ b/pokemongo_bot/event_handlers/__init__.py @@ -1,3 +1,4 @@ from logging_handler import LoggingHandler from socketio_handler import SocketIoHandler from colored_logging_handler import ColoredLoggingHandler +from social_handler import SocialHandler From f06c8ed2ac10eeab745f7c6b3b7a888b06265ab1 Mon Sep 17 00:00:00 2001 From: Ro Date: Mon, 22 Aug 2016 21:48:59 -0400 Subject: [PATCH 08/14] Update config.json.example --- configs/config.json.example | 1 + 1 file changed, 1 insertion(+) diff --git a/configs/config.json.example b/configs/config.json.example index a536410b64..3689ed9d4e 100644 --- a/configs/config.json.example +++ b/configs/config.json.example @@ -7,6 +7,7 @@ "encrypt_location": "", "websocket_server": false, "heartbeat_threshold": 10, + "enable_social": true, "tasks": [ { "type": "HandleSoftBan" From a93fbd911f3c874ba3c810383da03fa544cb5ab4 Mon Sep 17 00:00:00 2001 From: Ro Date: Mon, 22 Aug 2016 21:49:17 -0400 Subject: [PATCH 09/14] Update config.json.cluster.example --- configs/config.json.cluster.example | 1 + 1 file changed, 1 insertion(+) diff --git a/configs/config.json.cluster.example b/configs/config.json.cluster.example index 434c90a660..703c7be9d9 100644 --- a/configs/config.json.cluster.example +++ b/configs/config.json.cluster.example @@ -7,6 +7,7 @@ "encrypt_location": "", "websocket_server": false, "heartbeat_threshold": 10, + "enable_social": true, "tasks": [ { "type": "HandleSoftBan" From 8049aa3f669509b0dc6dddb0534ce26c3e9c9906 Mon Sep 17 00:00:00 2001 From: Ro Date: Mon, 22 Aug 2016 21:49:22 -0400 Subject: [PATCH 10/14] Update config.json.map.example --- configs/config.json.map.example | 1 + 1 file changed, 1 insertion(+) diff --git a/configs/config.json.map.example b/configs/config.json.map.example index 998d32860f..d3ae171bac 100644 --- a/configs/config.json.map.example +++ b/configs/config.json.map.example @@ -7,6 +7,7 @@ "encrypt_location": "", "websocket_server": false, "heartbeat_threshold": 10, + "enable_social": true, "tasks": [ { "type": "HandleSoftBan" From d82d1ce288ff37eb8420e30d1c3d873d0658ffc0 Mon Sep 17 00:00:00 2001 From: Ro Date: Mon, 22 Aug 2016 21:49:26 -0400 Subject: [PATCH 11/14] Update config.json.optimizer.example --- configs/config.json.optimizer.example | 1 + 1 file changed, 1 insertion(+) diff --git a/configs/config.json.optimizer.example b/configs/config.json.optimizer.example index 2843701128..4834a0ad07 100644 --- a/configs/config.json.optimizer.example +++ b/configs/config.json.optimizer.example @@ -7,6 +7,7 @@ "encrypt_location": "", "websocket_server": false, "heartbeat_threshold": 10, + "enable_social": true, "tasks": [ { "type": "HandleSoftBan" From 2c4bc06953cefb7102672d1db8079edd01dba10c Mon Sep 17 00:00:00 2001 From: Ro Date: Mon, 22 Aug 2016 21:49:34 -0400 Subject: [PATCH 12/14] Update config.json.path.example --- configs/config.json.path.example | 1 + 1 file changed, 1 insertion(+) diff --git a/configs/config.json.path.example b/configs/config.json.path.example index 9803c82409..e3c164d815 100644 --- a/configs/config.json.path.example +++ b/configs/config.json.path.example @@ -7,6 +7,7 @@ "encrypt_location": "", "websocket_server": false, "heartbeat_threshold": 10, + "enable_social": true, "tasks": [ { "type": "HandleSoftBan" From 820ea82a73f340494ec51f3487945505c75d5351 Mon Sep 17 00:00:00 2001 From: Ro Date: Mon, 22 Aug 2016 21:49:42 -0400 Subject: [PATCH 13/14] Update config.json.pokemon.example --- configs/config.json.pokemon.example | 1 + 1 file changed, 1 insertion(+) diff --git a/configs/config.json.pokemon.example b/configs/config.json.pokemon.example index 73c5e4a05b..a1e378212b 100644 --- a/configs/config.json.pokemon.example +++ b/configs/config.json.pokemon.example @@ -7,6 +7,7 @@ "encrypt_location": "", "websocket_server": false, "heartbeat_threshold": 10, + "enable_social": true, "tasks": [ { "type": "HandleSoftBan" From e786f5dd8b521a9e3a946ea8b6f869b35cf3850e Mon Sep 17 00:00:00 2001 From: Simba Zhang Date: Mon, 22 Aug 2016 18:55:06 -0700 Subject: [PATCH 14/14] To make CI happy. --- pokemongo_bot/event_handlers/social_handler.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pokemongo_bot/event_handlers/social_handler.py b/pokemongo_bot/event_handlers/social_handler.py index ead939620a..58309128c7 100644 --- a/pokemongo_bot/event_handlers/social_handler.py +++ b/pokemongo_bot/event_handlers/social_handler.py @@ -34,16 +34,16 @@ def __init__(self, bot): thread.start_new_thread(self.mqttc.run) def handle_event(self, event, sender, level, formatted_msg, data): #sender_name = type(sender).__name__ - if formatted_msg: - message = "[{}] {}".format(event, formatted_msg) - else: - message = '{}: {}'.format(event, str(data)) + #if formatted_msg: + # message = "[{}] {}".format(event, formatted_msg) + #else: + #message = '{}: {}'.format(event, str(data)) if event == 'catchable_pokemon': #self.mqttc.publish("pgomapcatch/all", str(data)) - print data + #print data if data['pokemon_id']: #self.mqttc.publish("pgomapcatch/all/catchable/"+str(data['pokemon_id']), str(data)) self.mqttc.publish("pgomapcatch/all/catchable/"+str(data['pokemon_id']), str(data['latitude'])+","+str(data['longitude'])+","+str(data['encounter_id'])) #print 'have catchable_pokemon' - print message + #print message