-
Notifications
You must be signed in to change notification settings - Fork 0
/
AgentDetails.gd
257 lines (216 loc) · 7.22 KB
/
AgentDetails.gd
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
extends Node
export var AID : String
export var AgentCredits : String
export var Headquaters : String
export var HQSys : String
export var CurrentSystem : String
export var AgentFaction : String
export var AgentSymbol : String
export var USERTOKEN : String
var systemData
var surveys : Dictionary = {}
var cooldowns : Dictionary
#Monitoring
var networth : Dictionary = {}
var sellgood : Dictionary = {} #Symbol, #Symbol/#TotalAmountSold[{Time:NewTotal}]/#TotalRevenue[{Time:NewTotal}]
var sellship : Dictionary = {} #ShipSymbol, #ShipSymbol/#TotalRevenue{Time:NewTotal}
export var _FleetData : Dictionary
export var interfaceShip : String
export var focusShip : String
var menu
signal login
signal systemFetch(cleanbody)
signal systemWayFetch(cleanbody)
signal chart(cleanbody)
signal shipyard(cleanbody)
signal visitmarket(cleanbody)
signal jumpgate
signal exitJumpgate
signal closeShop
signal fleetUpdated
signal requestFleetUpdate
signal AcceptContract
signal PurchaseShip
signal PurchaseCargo
signal SellCargo
signal JettisonCargo
signal TransferCargo
signal TransferTarget
signal TransferTargetUpdate
signal showFMAPbut
signal updateAgent
signal mapHOME
signal mapSEL(sym)
signal mapGenLine(one,two)
#SHIP ACTIONS
signal cooldownStarted
signal NavigationFinished
signal FocusMapNav
signal DockFinished
signal OrbitFinished
signal RefuelFinished
var QueryPrompt = null
signal query_Ship(shipArr)
signal interfaceShipSet
signal query_Waypoint(waypointArr)
signal selectedWaypoint(waypointSym)
signal query_Survey(symbol)
signal selectedSurvey(signature)
signal query_Group
signal selectedGroup(key)
signal query_Routine(GROUP)
signal selectedRoutine(data)
signal shipfocused(data)
signal error2disp(inst)
const error = preload("res://Interface/ErrorPanel.tscn")
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
func dispError(data):
var disp = error.instance()
disp.setdat(data)
emit_signal("error2disp",disp)
func cleanHQ():
var delete = true
var HQ = Headquaters
while delete:
if HQ[(HQ.length()-1)] == "-":
HQ.erase(HQ.length()-1, 1)
delete = false
break
else: HQ.erase(HQ.length()-1, 1)
CurrentSystem = HQ
HQSys = HQ
func clear():
AID = ""
AgentCredits = ""
Headquaters = ""
CurrentSystem = ""
AgentFaction = ""
AgentSymbol = ""
USERTOKEN = ""
func queryUser_Ship(waypoint = null,condition = ""):
var Ships : Array
if waypoint == null:
for s in _FleetData["data"]:
Ships.push_back(s)
else:
for s in _FleetData["data"]:
if s["symbol"] == focusShip and condition == "TRANSFER": continue
if s["cargo"]["units"] == s["cargo"]["capacity"] and condition == "TRANSFER": continue
if s["nav"]["waypointSymbol"] == waypoint and s["nav"]["status"] != "IN_TRANSIT":
Ships.push_back(s)
if Ships.size() == 1:
setInterfaceShip(Ships[0]["symbol"])
return
emit_signal("query_Ship", Ships)
func queryWaypoint(system, prompt = null):
QueryPrompt = null
var HTTP = HTTPRequest.new()
self.add_child(HTTP)
HTTP.use_threads = true
HTTP.connect("request_completed", self, "_on_SYSrequest_completed")
var url = str("https://api.spacetraders.io/v2/systems/",system)
var headerstring = str("Authorization: Bearer ", Agent.USERTOKEN)
var header = [headerstring]
HTTP.request(url, header)
if prompt != null:
QueryPrompt = prompt
func _on_SYSrequest_completed(result, response_code, headers, body):
var json = JSON.parse(body.get_string_from_utf8())
var cleanbody = json.result
if cleanbody.has("data"):
emit_signal("query_Waypoint",cleanbody,QueryPrompt)
else:
dispError(cleanbody)
# getfail()
print(json.result)
func setInterfaceShip(symbol):
interfaceShip = symbol
#print(symbol)
emit_signal("interfaceShipSet")
func cleanSurveys():
#Assumptions
#A good price is >20
#More deposits is a good thing
if surveys.empty(): return
for s in surveys:
#Expired
if Time.get_unix_time_from_datetime_string(surveys[s]["expiration"]) < Time.get_unix_time_from_system():
surveys.erase(s)
continue
var score = 0
score += surveys[s]["deposits"].size()
for d in surveys[s]["deposits"]:
score += sellgood[d["symbol"]]["LatestSellPrice"]
match surveys[s]["size"]:
"SMALL": score += 0
"MODERATE": score += 10
"LARGE": score += 20
func acceptContract(CID, node):
var HTTP = HTTPRequest.new()
self.add_child(HTTP)
HTTP.use_threads = true
HTTP.connect("request_completed", node, "_on_request_completed")
var url = str("https://api.spacetraders.io/v2/my/contracts/",CID,"/accept")
var headerstring = str("Authorization: Bearer ", USERTOKEN)
var header = ["Accept: application/json",headerstring,"Content-Type: application/json","Content-Length: 0"]
HTTP.request(url, header, true, HTTPClient.METHOD_POST)
yield(HTTP,"request_completed")
HTTP.queue_free()
#https://api.spacetraders.io/v2/my/ships
func purchaseShip(ShipType, Waypoint, node):
var HTTP = HTTPRequest.new()
self.add_child(HTTP)
HTTP.use_threads = true
HTTP.connect("request_completed", node, "_on_request_completed")
var url = str("https://api.spacetraders.io/v2/my/ships")
var headerstring = str("Authorization: Bearer ", USERTOKEN)
var data = JSON.print({"shipType": ShipType, "waypointSymbol": Waypoint})
var header = ["Content-Type: application/json",headerstring]
print(ShipType,Waypoint)
HTTP.request(url, header, true, HTTPClient.METHOD_POST, data)
yield(HTTP,"request_completed")
HTTP.queue_free()
#https://api.spacetraders.io/v2/my/ships/{shipSymbol}/purchase
func purchaseCargo(Good, Amt, node):
var HTTP = HTTPRequest.new()
self.add_child(HTTP)
HTTP.use_threads = true
HTTP.connect("request_completed", node, "_on_BUYrequest_completed")
var url = str("https://api.spacetraders.io/v2/my/ships/",interfaceShip,"/purchase")
var headerstring = str("Authorization: Bearer ", USERTOKEN)
var data = JSON.print({"symbol": Good, "units": Amt})
var header = ["Content-Type: application/json",headerstring]
print(Good,Amt)
HTTP.request(url, header, true, HTTPClient.METHOD_POST, data)
yield(HTTP,"request_completed")
HTTP.queue_free()
#https://api.spacetraders.io/v2/my/ships/{shipSymbol}/sell
func sellCargo(Good, Amt, node):
var HTTP = HTTPRequest.new()
self.add_child(HTTP)
HTTP.use_threads = true
HTTP.connect("request_completed", node, "_on_SELLrequest_completed")
var url = str("https://api.spacetraders.io/v2/my/ships/",interfaceShip,"/sell")
var headerstring = str("Authorization: Bearer ", USERTOKEN)
var data = JSON.print({"symbol": Good, "units": Amt})
var header = ["Content-Type: application/json",headerstring]
print(Good,Amt)
HTTP.request(url, header, true, HTTPClient.METHOD_POST, data)
yield(HTTP,"request_completed")
HTTP.queue_free()
#https://api.spacetraders.io/v2/my/ships/{shipSymbol}/jettison
func jettisonCargo(Good, Amt, node):
var HTTP = HTTPRequest.new()
self.add_child(HTTP)
HTTP.use_threads = true
HTTP.connect("request_completed", node, "_on_JETrequest_completed")
var url = str("https://api.spacetraders.io/v2/my/ships/",focusShip,"/jettison")
var headerstring = str("Authorization: Bearer ", USERTOKEN)
var data = JSON.print({"symbol": Good, "units": Amt})
var header = ["Content-Type: application/json",headerstring]
print(Good,Amt)
HTTP.request(url, header, true, HTTPClient.METHOD_POST, data)
yield(HTTP,"request_completed")
HTTP.queue_free()