Added basic map and !range command.

This commit is contained in:
2014-08-11 20:11:17 +01:00
parent b854910629
commit 096fdb0bc5
4 changed files with 86 additions and 4 deletions

View File

@@ -8,6 +8,8 @@ import pkgutil
from json import loads as base_loads
from random import choice
from dropbot.map import Map
class DropBot(ClientXMPP):
def __init__(self, **kwargs):
@@ -16,9 +18,11 @@ class DropBot(ClientXMPP):
self.nickname = kwargs.pop('nickname', 'Dropbot')
self.cmd_prefix = kwargs.pop('cmd_prefix', '!')
self.kos_url = kwargs.pop('kos_url', 'http://kos.cva-eve.org/api/')
super(DropBot, self).__init__(**kwargs)
self.redis_conn = Redis()
self.redis_conn = Redis()
self.map = Map.from_json(pkgutil.get_data('dropbot', 'data/map.json'))
super(DropBot, self).__init__(**kwargs)
self.register_plugin('xep_0030') # Service Discovery
self.register_plugin('xep_0045') # Multi-User Chat
self.register_plugin('xep_0199') # XMPP Ping
@@ -109,7 +113,7 @@ class DropBot(ClientXMPP):
resp = requests.get('http://api.eve-central.com/api/marketstat?typeid={}&usesystem=30000142'.format(typeid))
root = ElementTree.fromstring(resp.content)
except:
return "An error occured tying to get the price for {}".format(name)
return "An error occurred tying to get the price for {}".format(name)
return "{} | Sell: {} | Buy: {}".format(
name,
@@ -160,5 +164,21 @@ class DropBot(ClientXMPP):
results.append(text)
return '\n'.join(results)
def cmd_range(self, args, msg):
if len(args) != 1:
return '!range <system>'
system = args[0]
system_id = self.map.name_to_systemid(system)
if not system_id:
return 'Unknown system %s' % system
res = {}
systems = self.map.neighbors_jump(system_id, 7.8)
for sys, range in systems:
if sys['region'] in res:
res[sys['region']] += 1
else:
res[sys['region']] = 1
return '{} systems in JDC5 Blops range of {}:\n'.format(len(systems), self.map.systemid_to_name(system_id)) + '\n'.join(['{} - {}'.format(x, y) for x, y in res.items()])

1
dropbot/data/map.json Normal file

File diff suppressed because one or more lines are too long

60
dropbot/map.py Normal file
View File

@@ -0,0 +1,60 @@
import math
import networkx
from networkx.readwrite.json_graph import node_link_data, node_link_graph
from json import loads, dumps
def calc_distance(sys1, sys2):
"""Calculate the distance in lightyears between two sets of 3d coordinates"""
EVE_LY = 9460000000000000 # EVE's definition of a ly in KM
return math.sqrt(sum((a - b)**2 for a, b in zip(sys1, sys2))) / EVE_LY
class Map(networkx.Graph):
def load_sde_data(self, db_conn):
for id, name, region_name, x, y, z in db_conn.execute("""
SELECT solarSystemID, solarSystemName, regionName, mapSolarSystems.x, mapSolarSystems.y, mapSolarSystems.z
FROM mapSolarSystems
INNER JOIN mapRegions ON mapSolarSystems.regionID = mapRegions.regionID
WHERE mapSolarSystems.regionID < 11000001"""):
self.add_node(id, system_id=id, name=name, region=region_name, coords=(x, y, z))
for from_id, to_id in db_conn.execute("SELECT fromSolarSystemID, toSolarSystemID FROM mapSolarSystemJumps"):
self.add_edge(from_id, to_id)
def to_json(self):
return dumps(node_link_data(self))
@staticmethod
def from_json(json):
return Map(data=node_link_graph(loads(json)))
def systemid_to_name(self, system_id):
return self.node[system_id]['name']
def name_to_systemid(self, name):
for k, v in self.nodes_iter(data=True):
if 'name' in v and v['name'].lower() == name.lower():
return k
def system_distance(self, source, destination):
return calc_distance(self.node[source], self.node[destination])
def route_gate(self, source, destination):
return networkx.astar_path(self, source, destination)
def route_jump(self, source, destination):
return networkx.astar_path(self, source, destination, self.system_distance)
def neighbors_gate(self, system_id):
return self.neighbors(system_id)
def neighbors_jump(self, system_id, range):
source = self.node[system_id]
destinations = []
for destination_id, destination_data in self.nodes_iter(data=True):
distance = calc_distance(source['coords'], destination_data['coords'])
if distance <= range and destination_id != system_id:
destinations.append((destination_data, distance))
return destinations

View File

@@ -3,4 +3,5 @@ eveapi
redis
requests
humanize
dnspython
dnspython
networkx