Browse Source

Add better channel update spam criteria

Hans Nielsen 13 years ago
parent
commit
7651b4ecd7
1 changed files with 29 additions and 8 deletions
  1. 29 8
      locationbot.py

+ 29 - 8
locationbot.py

@@ -24,6 +24,7 @@ import traceback
 import urllib, urllib2
 import urlparse
 import warnings
+import math
 
 try:
 	import simplejson as json
@@ -294,18 +295,19 @@ class LocationBot(ircbot.SingleServerIRCBot):
 		try:
 			db, cursor = self.__db()
 
-			cursor.execute('select nick, channels, location, latitude.granularity, token, latitude.secret from locationbot.nick join locationbot.latitude using (id)')
+			cursor.execute('select nick, channels, latitude.granularity, location, coordinates, accuracy, speed, heading, altitude, altitude_accuracy, updated, token, latitude.secret from locationbot.nick join locationbot.latitude using (id)')
 
-			for nick, channels, old_location, granularity, token, secret in cursor.fetchall():
+			for nick, channels, granularity, old_location, old_coordinates, old_accuracy, old_speed, old_heading, old_altitude, old_altitude_accuracy, old_updated, token, secret in cursor.fetchall():
 				try:
-					updated, coordinates, accuracy, speed, heading, altitude, altitude_accuracy, new_location = self.__latitude(granularity, token, secret)
+					updated, coordinates, accuracy, speed, heading, altitude, altitude_accuracy, location = self.__latitude(granularity, token, secret)
 				except Exception, error:
 					traceback.print_exc()
 					continue
 
-				cursor.execute('update locationbot.nick set granularity = %s, location = %s, coordinates = point %s, accuracy = %s, speed = %s, heading = %s, altitude = %s, altitude_accuracy = %s, updated = %s where nick = %s', (granularity, new_location, coordinates, accuracy, speed, heading, altitude, altitude_accuracy, updated, nick))
+				cursor.execute('update locationbot.nick set granularity = %s, location = %s, coordinates = point %s, accuracy = %s, speed = %s, heading = %s, altitude = %s, altitude_accuracy = %s, updated = %s where nick = %s', (granularity, location, coordinates, accuracy, speed, heading, altitude, altitude_accuracy, updated, nick))
 				db.commit()
-				self.__location(nick, channels, granularity, old_location, new_location, coordinates, accuracy, speed, heading, altitude, altitude_accuracy)
+
+				self.__location(nick, channels, granularity, old_location, location, old_coordinates, coordinates, old_accuracy, accuracy, old_speed, speed, old_heading, heading, old_altitude, altitude, old_altitude_accuracy, altitude_accuracy, old_updated, updated)
 		except psycopg2.Error, error:
 			traceback.print_exc()
 
@@ -314,11 +316,30 @@ class LocationBot(ircbot.SingleServerIRCBot):
 
 			self.__latitude_timer.start()
 
-	def __location(self, nick, channels, granularity, old_location, new_location, coordinates, accuracy, speed, heading, altitude, altitude_accuracy):
-		if channels and new_location and new_location != old_location:
+	def __location(self, nick, channels, granularity, old_location, new_location, old_coordinates, new_coordinates, old_accuracy, new_accuracy, old_speed, new_speed, old_heading, new_heading, old_altitude, new_altitude, old_altitude_accuracy, new_altitude_accuracy, old_updated, new_updated):
+		def calc_distance(lat1, lon1, lat2, lon2): # returns meters
+			nauticalMilePerLat = 60.00721
+			nauticalMilePerLongitude = 60.10793
+			rad = math.pi / 180.0
+			milesPerNauticalMile = 1.15078
+			metersPerNauticalMile = 1852
+
+			yDistance = (lat2 - lat1) * nauticalMilePerLat
+			xDistance = (math.cos(lat1 * rad) + math.cos(lat2 * rad)) * \
+			                (lon2 - lon1) * (nauticalMilePerLongitude / 2)
+			distance = math.sqrt( yDistance**2 + xDistance**2 )
+			return distance * metersPerNauticalMile
+
+		def make_update_noise():
 			with self.__locations_lock:
 				for channel in self.__channels.intersection(channels):
-					self.__locations.append((nick, channel, granularity, new_location, coordinates, accuracy, speed, heading, altitude, altitude_accuracy))
+					self.__locations.append((nick, channel, granularity, new_location, new_coordinates, new_accuracy, new_speed, new_heading, new_altitude, new_altitude_accuracy))
+		if channels:
+			min_radius = min(old_accuracy, new_accuracy)
+			distance = calc_distance(old_coordinates[0], old_coordinates[1],
+			                         new_coordinates[0], new_coordinates[1])
+			if distance > min_radius or new_location != old_location:
+				make_update_noise()
 
 	def __login(self, connection, nickmask, nick, arguments = ''):
 		login = nick