Added !route

This commit is contained in:
2014-08-12 19:01:06 +01:00
parent a7af4efb9e
commit cdbbaeabd0
3 changed files with 48 additions and 2 deletions

3
.gitignore vendored
View File

@@ -56,3 +56,6 @@ target/
# IDEA
.idea
# EVE
eve.db

View File

@@ -269,3 +269,39 @@ class DropBot(ClientXMPP):
res[sys['region']] = 1
return '{} systems in JDC5 {} range of {}:\n'.format(len(systems), ship_class, self.map.get_system_name(system_id)) + '\n'.join(['{} - {}'.format(x, y) for x, y in res.items()])
def cmd_route(self, args, msg):
if len(args) != 2:
return '!route <source> <destination>'
source, dest = args
source_systems = self.map.get_systems(source)
dest_systems = self.map.get_systems(dest)
if len(source_systems) > 1:
if len(source_systems) > 10:
return 'More than 10 systems match {}, please provide a more complete name'.format(source)
return 'Did you mean: {}?'.format(', '.join([self.map.get_system_name(x) for x in source_systems]))
elif len(source_systems) == 0:
return 'No systems found matching {}'.format(source)
else:
source_system = source_systems[0]
if len(dest_systems) > 1:
if len(dest_systems) > 10:
return 'More than 10 systems match {}, please provide a more complete name'.format(source)
return 'Did you mean: {}?'.format(', '.join([self.map.get_system_name(x) for x in dest_systems]))
elif len(dest_systems) == 0:
return 'No systems found matching {}'.format(dest)
else:
dest_system = dest_systems[0]
route = self.map.route_gate(source_system, dest_system)
route_names = ' -> '.join(['{} ({})'.format(x['name'], round(x['security'], 2)) for x in [self.map.node[y] for y in route]])
return '{} jumps from {} to {}\n{}'.format(
len(route),
self.map.get_system_name(source_system),
self.map.get_system_name(dest_system),
route_names
)

View File

@@ -105,15 +105,22 @@ class Map(networkx.Graph):
for k, v in self.nodes_iter(data=True):
if 'name' in v and v['name'].lower() == name.lower():
return k
def get_systems(self, name):
"""Returns a list of systems by a partial system name"""
return [k for k, v in self.nodes_iter(data=True) if name.lower() in v['name'].lower()]
def system_distance(self, source, destination):
"""Calculates the distance in ly between two systems"""
return calc_distance(self.node[source], self.node[destination])
def route_gate(self, source, destination):
def route_gate(self, source, destination, filter=None):
"""Route between two systems using gates (fastest)"""
# TODO: add EVE routing options (highsec/lowsec/fastest)
g = networkx.Graph(data=[(u, v) for u, v, d in self.edges_iter(data=True) if d['link_type'] == 'gate'])
return networkx.astar_path(g, source, destination)
return networkx.astar_path(self, source, destination)
def route_jump(self, source, destination, range=None, hull=None, ship_class=None):
"""Route between two systems using jumps"""