forked from jimkeir/EDProxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetlogline.py
More file actions
163 lines (127 loc) · 5.54 KB
/
netlogline.py
File metadata and controls
163 lines (127 loc) · 5.54 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import edevent
import edsmdb
import datetime
__all__ = [ 'NETLOG_LINE_TYPE', 'NETLOG_SHIP_STATUS', 'NETLOG_VERSION', 'NetlogLineFactory' ]
def _enum(**enums):
return type('Enum', (), enums)
NETLOG_LINE_TYPE = _enum(INVALID = "Invalid",
SYSTEM = "System")
NETLOG_SHIP_STATUS = _enum(UNKNOWN = "unknown",
NORMAL_FLIGHT = "NormalFlight",
SUPERCRUISE = "Supercruise",
PROVING_GROUND = "ProvingGround")
NETLOG_VERSION = _enum(VERSION_2_0 = "<=2.0",
VERSION_2_1 = "2.1+")
class NetlogLineFactory():
@staticmethod
def get_line(line_time, line):
parsed_line = _SystemLine.parse_netlog_line(line_time, line)
return parsed_line
class _SystemLine(edevent.BaseEvent):
def __init__(self,
version,
line_time,
system_name,
num_bodies = 0,
system_position = None,
position = (0.0, 0.0, 0.0),
ship_status = NETLOG_SHIP_STATUS.UNKNOWN):
edevent.BaseEvent.__init__(self,NETLOG_LINE_TYPE.SYSTEM, line_time)
self._version = version
self._name = system_name
self._num_bodies = num_bodies
self._position = position
self._ship_status = ship_status
# Now that FD is supplying the system coordinates
# we should look at pulling systems by cube/sphere
# rather than by system name.
edsm_db = edsmdb.get_instance()
# self._distances = edsm_db.get_distances(self._name, 120.0)
self._distances = set()
if system_position:
self._system_coordinates = system_position
else:
system = edsm_db.get_system(self._name)
if system:
self._system_coordinates = system.position
else:
self._system_coordinates = None
@classmethod
def parse_netlog_line(cls, line_time, line):
if 'SystemName' in line:
system = line['SystemName']
if line.get('StarPos') is not None:
star_pos = tuple(float(f) for f in line['StarPos'].split(","))
version = NETLOG_VERSION.VERSION_2_1
else:
star_pos = None
version = NETLOG_VERSION.VERSION_2_0
if line.get('Body') is not None:
body = int(line['Body'])
else:
body = 0
if line.get('Pos') is not None:
pos = tuple(float(f) for f in line['Pos'].split(","))
else:
pos = (0.0, 0.0, 0.0)
if line.get('TravelMode') is not None:
status = line['TravelMode']
if status.startswith(str(NETLOG_SHIP_STATUS.NORMAL_FLIGHT)):
status = NETLOG_SHIP_STATUS.NORMAL_FLIGHT
elif status.startswith(str(NETLOG_SHIP_STATUS.SUPERCRUISE)):
status = NETLOG_SHIP_STATUS.SUPERCRUISE
elif status.startswith(str(NETLOG_SHIP_STATUS.PROVING_GROUND)):
status = NETLOG_SHIP_STATUS.PROVING_GROUND
else:
status = NETLOG_SHIP_STATUS.UNKNOWN
else:
status = NETLOG_SHIP_STATUS.UNKNOWN
return cls(version,
line_time,
system,
num_bodies = body,
system_position = star_pos,
position = pos,
ship_status = status)
else:
return None
def _fill_json_dict(self, json_dict):
json_dict['System'] = self._name
json_dict['Bodies'] = self._num_bodies
json_dict['Position'] = self._position
json_dict['Status'] = self._ship_status
if self._system_coordinates:
json_dict['SystemCoord'] = self._system_coordinates
if self._distances:
dict_list = list()
max_list_size = len(self._distances)
if max_list_size > 10:
max_list_size = 10
for i in xrange(0, max_list_size):
distance = self._distances[i]
item = dict()
item['name'] = distance.sys2.name
item['distance'] = distance.distance
if distance.sys2.position:
item['coords'] = distance.sys2.position
dict_list.append(item)
json_dict['Distances'] = dict_list
def get_version(self):
return self._version
def get_name(self):
return self._name
def get_num_bodies(self):
return self._num_bodies
def get_position(self):
return self._position
def get_ship_status(self):
return self._ship_status
def get_system_coordinates(self):
return self._system_coordinates
def get_distances(self):
return self._distances
def __str__(self):
return edevent.BaseEvent.__str__(self) + ", Name [" + self._name + "], Bodies [" + str(self._num_bodies) + "], Position [" + str(self._position) + "], Ship Status [" + self._ship_status + "]"
if __name__ == "__main__":
system = _SystemLine(datetime.datetime.utcnow(), 'Prieluia ND-H b57-0', ship_status = NETLOG_SHIP_STATUS.SUPERCRUISE)
print system.get_json()