A SpaceTraders Agent
1const std = @import("std");
2
3pub const fleet = @import("fleet.zig");
4
5const st = @import("root.zig");
6const http = st.http;
7const Client = http.Client;
8const Response = st.http.Response;
9
10const models = st.models;
11const Wrapper = models.Wrapper;
12const Query = models.Query;
13
14pub fn account(client: *Client) !Response(models.accounts.AccountWrapper) {
15 return client.request(
16 Wrapper(models.accounts.AccountWrapper),
17 "/my/account",
18 .{},
19 .{ .auth = .agent },
20 );
21}
22
23pub fn info(client: *Client) !http.RawResponse(models.Info) {
24 return client.request(
25 models.Info,
26 "/",
27 .{},
28 .{ .auth = .none },
29 );
30}
31
32pub const agent = struct {
33 pub fn register(
34 client: *Client,
35 symbol: []const u8,
36 faction: models.factions.Symbol,
37 ) !Response(models.agents.Register) {
38 return client.postW(
39 models.agents.Register,
40 "/register",
41 .{},
42 .{ .symbol = symbol, .faction = faction },
43 .account,
44 );
45 }
46
47 pub fn list(client: *Client, opts: Query) !Response([]models.agents.Agent) {
48 return client.getW(
49 []models.agents.Agent,
50 "/agents{f}",
51 .{opts},
52 .agent,
53 );
54 }
55
56 pub fn get(client: *Client, symbol: []const u8) !Response(models.agents.Agent) {
57 return client.getW(
58 models.agents.Agent,
59 "/agents/{s}",
60 .{symbol},
61 .agent,
62 );
63 }
64
65 pub fn own(client: *Client) !Response(models.agents.Agent) {
66 return client.getW(
67 models.agents.Agent,
68 "/my/agent",
69 .{},
70 .agent,
71 );
72 }
73};
74
75pub const system = struct {
76 const Symbol = models.systems.Symbol;
77
78 pub fn get(client: *Client, symbol: Symbol.System) !Response(models.systems.System) {
79 return client.getW(
80 models.systems.System,
81 "/systems/{f}",
82 .{symbol},
83 .agent,
84 );
85 }
86
87 pub fn list(client: *Client, opts: Query) !Response([]models.systems.System) {
88 return client.getW(
89 []models.systems.System,
90 "/systems{f}",
91 .{opts},
92 .agent,
93 );
94 }
95
96 pub fn waypoint(
97 client: *Client,
98 waypoint_symbol: Symbol.Waypoint,
99 ) !Response(models.systems.Waypoint) {
100 return client.getW(
101 models.systems.Waypoint,
102 "/systems/{f}/waypoints/{f}",
103 .{ waypoint_symbol.system, waypoint_symbol },
104 .agent,
105 );
106 }
107
108 pub const WaypointsFilter = struct {
109 type: ?models.systems.Waypoint.Type = null,
110 traits: []models.systems.Waypoint.Trait = &.{},
111
112 pub fn format(this: @This(), writer: *std.Io.Writer) std.Io.Writer.Error!void {
113 if (this.type) |ty| {
114 try writer.print("&type={s}", .{@tagName(ty)});
115 }
116 if (this.traits.len > 0) {
117 for (this.traits) |trait| {
118 try writer.print("&traits={s}", .{@tagName(trait)});
119 }
120 }
121 }
122 };
123
124 pub fn waypoints(
125 client: *Client,
126 symbol: Symbol.System,
127 filter: WaypointsFilter,
128 opts: Query,
129 ) !Response([]models.systems.Waypoint) {
130 return client.getW(
131 []models.systems.Waypoint,
132 "/systems/{f}/waypoints{f}{f}",
133 .{ symbol, opts, filter },
134 .agent,
135 );
136 }
137
138 pub fn construction(
139 client: *Client,
140 waypoint_symbol: Symbol.Waypoint,
141 ) !Response(models.systems.Construction) {
142 return client.getW(
143 models.systems.Construction,
144 "/systems/{f}/waypoints/{f}/construction",
145 .{ waypoint_symbol.system, waypoint_symbol },
146 .agent,
147 );
148 }
149
150 pub const SupplyConstruction = struct {
151 shipSymbol: []const u8,
152 tradeSymbol: models.inventory.AllItems.Combined,
153 units: u64,
154
155 pub const Response = struct {
156 construction: models.systems.ConstructionSite,
157 cargo: models.inventory.Cargo,
158 };
159 };
160
161 pub fn supplyConstruction(
162 client: *Client,
163 waypoint_symbol: Symbol.Waypoint,
164 supply: SupplyConstruction,
165 ) !Response(SupplyConstruction.Response) {
166 return client.postW(
167 SupplyConstruction.Response,
168 "/systems/{f}/waypoints/{f}/supply-construction",
169 .{ waypoint_symbol.system, waypoint_symbol },
170 supply,
171 .agent,
172 );
173 }
174
175 pub fn market(
176 client: *Client,
177 waypoint_symbol: Symbol.Waypoint,
178 ) !Response(models.systems.Market) {
179 return client.getW(
180 models.systems.Market,
181 "/systems/{f}/waypoints/{f}/market",
182 .{ waypoint_symbol.system, waypoint_symbol },
183 .agent,
184 );
185 }
186
187 pub fn shipyard(
188 client: *Client,
189 waypoint_symbol: Symbol.Waypoint,
190 ) !Response(models.systems.Shipyard) {
191 return client.getW(
192 models.systems.Shipyard,
193 "/systems/{f}/waypoints/{f}/shipyard",
194 .{ waypoint_symbol.system, waypoint_symbol },
195 .agent,
196 );
197 }
198};