-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
77 lines (63 loc) · 3.06 KB
/
agent.py
File metadata and controls
77 lines (63 loc) · 3.06 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
import requests
from settings import settings
import faction
import fleet
import system
import contract
if settings['ENVIRONMENT'] == "DEV":
BASE_URL = settings['DEV_BASE_URL']
else:
BASE_URL = settings['BASE_URL']
class Agent:
def __init__(self, name: str = None, agent_faction: str = None, token: str = None):
headers = {'Content-Type': 'application/json'}
self.faction = None
self.ships = []
# this entire if statement needs to be reworked
if token is not None:
headers['Authorization'] = f"Bearer {token}"
response = requests.get(BASE_URL + settings['AGENT_URL'], headers=headers)
print(response.text)
# needs to add error handling
self.name = response.json()['data']['symbol']
self.account_id = response.json()['data']['accountId']
self.credits = response.json()['data']['credits']
# self.headquarters = system.System.from_symbol(response.json()['data']['headquarters'], token)
self.token = token
if 'faction' in response.json()['data']:
self.faction = faction.Faction.from_symbol(response.json()['data']['faction'], self.token)
if 'ships' in response.json()['data']:
self.ships = fleet.Fleet.from_ship_data(response.json()['data']['ships'])
self.contracts = contract.Contracts(self.token)
elif name is not None and agent_faction is not None:
# create a new agent
data = {'symbol': name,
'faction': agent_faction}
response = requests.post(BASE_URL + settings['REGISTER_URL'], json=data, headers=headers)
# this is probably wrong, AI wrote this
if response.status_code == 400:
raise ValueError("The name you provided is already in use")
elif response.status_code == 409:
raise ValueError("The faction you provided is invalid")
elif response.status_code == 201:
self.name = response.json()['data']['agent']['symbol']
self.account_id = response.json()['data']['agent']['accountId']
self.credits = response.json()['data']['agent']['credits']
self.headquarters = response.json()['data']['agent']['headquarters']
self.token = response.json()['data']['token']
self.faction = faction.Faction.from_symbol(agent_faction, self.token)
self.contracts = contract.Contracts(self.token)
else:
raise ValueError("You must provide either a name and faction or a token")
def get_current_waypoint(self):
''' Get the current waypoint and system of the agent'''
if self.current_system is not None:
return self.current_waypoint
curreent_waypoint = system.System()
def __str__(self):
return f"{self.name}: {self.faction}"
def __repr__(self):
return f"{self.name}: {self.faction}"
# to be deleted later
if __name__ == '__main__':
teser = Agent('tester_mctest', 'COSMIC')