Compare commits

..

3 Commits

Author SHA1 Message Date
56915df9ac add equiping and unequiping actions 2024-08-09 01:30:19 +03:00
6eb89eae24 add crafting action 2024-08-09 00:30:44 +03:00
166ff8e892 add withdrawing from bank endpoints 2024-08-09 00:13:43 +03:00
3 changed files with 600 additions and 84 deletions

View File

@ -22,9 +22,138 @@ token: ?[]u8 = null,
item_codes: std.ArrayList([]u8),
pub const APIErrors = error {
pub const APIError = error {
ServerUnavailable,
RequestFailed,
ParseFailed
ParseFailed,
OutOfMemory
};
pub const MoveError = APIError || error {
MapNotFound,
CharacterIsBusy,
CharacterAtDestination,
CharacterNotFound,
CharacterInCooldown
};
pub const FightError = APIError || error {
CharacterIsBusy,
CharacterIsFull,
CharacterNotFound,
CharacterInCooldown,
MonsterNotFound,
};
pub const GatherError = APIError || error {
CharacterIsBusy,
NotEnoughSkill,
CharacterIsFull,
CharacterNotFound,
CharacterInCooldown,
ResourceNotFound
};
pub const BankDepositItemError = APIError || error {
ItemNotFound,
BankIsBusy,
NotEnoughItems,
CharacterIsBusy,
CharacterNotFound,
CharacterInCooldown,
BankNotFound
};
pub const BankDepositGoldError = APIError || error {
BankIsBusy,
NotEnoughGold,
CharacterIsBusy,
CharacterNotFound,
CharacterInCooldown,
BankNotFound
};
pub const BankWithdrawGoldError = APIError || error {
BankIsBusy,
NotEnoughGold,
CharacterIsBusy,
CharacterNotFound,
CharacterInCooldown,
BankNotFound
};
pub const BankWithdrawItemError = APIError || error {
ItemNotFound,
BankIsBusy,
NotEnoughItems,
CharacterIsBusy,
CharacterIsFull,
CharacterNotFound,
CharacterInCooldown,
BankNotFound
};
pub const CraftError = APIError || error {
RecipeNotFound,
NotEnoughItems,
CharacterIsBusy,
NotEnoughSkill,
CharacterIsFull,
CharacterNotFound,
CharacterInCooldown,
WorkshopNotFound
};
pub const UnequipError = APIError || error {
ItemNotFound, // TODO: Can this really occur? maybe a bug in docs
CharacterIsBusy,
SlotIsEmpty,
CharacterIsFull,
CharacterNotFound,
CharacterInCooldown,
};
pub const EquipError = APIError || error {
ItemNotFound,
SlotIsFull,
CharacterIsBusy,
NotEnoughSkill,
CharacterNotFound,
CharacterInCooldown,
};
pub const EquipmentSlot = enum {
weapon,
shield,
helmet,
body_armor,
leg_armor,
boots,
ring1,
ring2,
amulet,
artifact1,
artifact2,
consumable1,
consumable2,
fn name(self: EquipmentSlot) []const u8 {
return switch (self) {
.weapon => "weapon",
.shield => "shield",
.helmet => "helmet",
.body_armor => "body_armor",
.leg_armor => "leg_armor",
.boots => "boots",
.ring1 => "ring1",
.ring2 => "ring2",
.amulet => "amulet",
.artifact1 => "artifact1",
.artifact2 => "artifact2",
.consumable1 => "consumable1",
.consumable2 => "consumable2",
};
}
};
const ServerStatus = struct {
@ -33,7 +162,7 @@ const ServerStatus = struct {
version: []const u8,
characters_online: i64,
fn parse(api: *ArtifactsAPI, object: json.ObjectMap, allocator: Allocator) !ServerStatus {
pub fn parse(api: *ArtifactsAPI, object: json.ObjectMap, allocator: Allocator) !ServerStatus {
_ = api;
return ServerStatus{
@ -152,7 +281,7 @@ pub const Cooldown = struct {
expiration: f64,
reason: Reason,
fn parse(obj: json.ObjectMap) !Cooldown {
pub fn parse(obj: json.ObjectMap) !Cooldown {
const reason = try json_utils.getStringRequired(obj, "reason");
const expiration = try json_utils.getStringRequired(obj, "expiration");
@ -210,7 +339,7 @@ pub const FightResult = struct {
cooldown: Cooldown,
fight: Details,
fn parse(api: *ArtifactsAPI, obj: json.ObjectMap) !FightResult {
pub fn parse(api: *ArtifactsAPI, obj: json.ObjectMap) !FightResult {
const cooldown = json_utils.getObject(obj, "cooldown") orelse return error.MissingProperty;
const fight = json_utils.getObject(obj, "fight") orelse return error.MissingProperty;
@ -219,48 +348,71 @@ pub const FightResult = struct {
.fight = try Details.parse(api, fight)
};
}
pub fn parseError(status: std.http.Status) ?FightError {
return switch (@intFromEnum(status)) {
486 => FightError.CharacterIsBusy,
497 => FightError.CharacterIsFull,
498 => FightError.CharacterNotFound,
499 => FightError.CharacterInCooldown,
598 => FightError.MonsterNotFound,
else => null
};
}
};
pub const GatheringResult = struct {
const Details = struct {
const Item = struct {
id: ItemId,
quantity: i64
};
xp: i64,
items: std.BoundedArray(Item, 8),
fn parse(api: *ArtifactsAPI, obj: json.ObjectMap) !Details {
var items = std.BoundedArray(Item, 8).init(0) catch unreachable;
const items_obj = json_utils.getArray(obj, "items") orelse return error.MissingProperty;
for (items_obj.items) |item_value| {
const item_obj = json_utils.asObject(item_value) orelse return error.MissingProperty;
const code = try json_utils.getStringRequired(item_obj, "code");
try items.append(Item{
.id = try api.getItemId(code),
.quantity = try json_utils.getIntegerRequired(item_obj, "quantity")
});
}
return Details{
.xp = try json_utils.getIntegerRequired(obj, "xp"),
.items = items,
};
}
pub const SkillDetails = struct {
pub const Item = struct {
id: ItemId,
quantity: i64
};
cooldown: Cooldown,
details: Details,
xp: i64,
items: std.BoundedArray(Item, 8),
fn parse(api: *ArtifactsAPI, obj: json.ObjectMap) !GatheringResult {
fn parse(api: *ArtifactsAPI, obj: json.ObjectMap) !SkillDetails {
var items = std.BoundedArray(Item, 8).init(0) catch unreachable;
const items_obj = json_utils.getArray(obj, "items") orelse return error.MissingProperty;
for (items_obj.items) |item_value| {
const item_obj = json_utils.asObject(item_value) orelse return error.MissingProperty;
const code = try json_utils.getStringRequired(item_obj, "code");
try items.append(Item{
.id = try api.getItemId(code),
.quantity = try json_utils.getIntegerRequired(item_obj, "quantity")
});
}
return SkillDetails{
.xp = try json_utils.getIntegerRequired(obj, "xp"),
.items = items,
};
}
};
pub const GatherResult = struct {
cooldown: Cooldown,
details: SkillDetails,
pub fn parse(api: *ArtifactsAPI, obj: json.ObjectMap) !GatherResult {
const cooldown = json_utils.getObject(obj, "cooldown") orelse return error.MissingProperty;
const details = json_utils.getObject(obj, "details") orelse return error.MissingProperty;
return GatheringResult{
return GatherResult{
.cooldown = try Cooldown.parse(cooldown),
.details = try Details.parse(api, details)
.details = try SkillDetails.parse(api, details)
};
}
pub fn parseError(status: std.http.Status) ?GatherError {
return switch (@intFromEnum(status)) {
486 => GatherError.CharacterIsBusy,
493 => GatherError.NotEnoughSkill,
497 => GatherError.CharacterIsFull,
498 => GatherError.CharacterNotFound,
499 => GatherError.CharacterInCooldown,
598 => GatherError.ResourceNotFound,
else => null
};
}
};
@ -268,7 +420,7 @@ pub const GatheringResult = struct {
pub const MoveResult = struct {
cooldown: Cooldown,
fn parse(api: *ArtifactsAPI, obj: json.ObjectMap) !MoveResult {
pub fn parse(api: *ArtifactsAPI, obj: json.ObjectMap) !MoveResult {
_ = api;
const cooldown = json_utils.getObject(obj, "cooldown") orelse return error.MissingProperty;
@ -278,6 +430,17 @@ pub const MoveResult = struct {
};
}
pub fn parseError(status: std.http.Status) ?MoveError {
return switch (@intFromEnum(status)) {
404 => MoveError.MapNotFound,
486 => MoveError.CharacterIsBusy,
490 => MoveError.CharacterAtDestination,
498 => MoveError.CharacterNotFound,
499 => MoveError.CharacterInCooldown,
else => null
};
}
pub fn deinit(self: MoveResult) void {
_ = self;
}
@ -286,7 +449,7 @@ pub const MoveResult = struct {
pub const GoldTransactionResult = struct {
cooldown: Cooldown,
fn parse(api: *ArtifactsAPI, obj: json.ObjectMap) !GoldTransactionResult {
pub fn parse(api: *ArtifactsAPI, obj: json.ObjectMap) !GoldTransactionResult {
_ = api;
const cooldown = json_utils.getObject(obj, "cooldown") orelse return error.MissingProperty;
@ -295,6 +458,31 @@ pub const GoldTransactionResult = struct {
};
}
pub fn parseDepositError(status: std.http.Status) ?BankDepositGoldError {
return switch (@intFromEnum(status)) {
461 => BankDepositGoldError.BankIsBusy,
478 => BankDepositGoldError.NotEnoughGold, // TODO: This should maybe be removed
486 => BankDepositGoldError.CharacterIsBusy,
492 => BankDepositGoldError.NotEnoughGold,
498 => BankDepositGoldError.CharacterNotFound,
499 => BankDepositGoldError.CharacterInCooldown,
598 => BankDepositGoldError.BankNotFound,
else => null
};
}
pub fn parseWithdrawError(status: std.http.Status) ?BankWithdrawGoldError {
return switch (@intFromEnum(status)) {
460 => BankWithdrawGoldError.NotEnoughGold,
461 => BankWithdrawGoldError.BankIsBusy,
486 => BankWithdrawGoldError.CharacterIsBusy,
498 => BankWithdrawGoldError.CharacterNotFound,
499 => BankWithdrawGoldError.CharacterInCooldown,
598 => BankWithdrawGoldError.BankNotFound,
else => null
};
}
pub fn deinit(self: GoldTransactionResult) void {
_ = self;
}
@ -303,7 +491,7 @@ pub const GoldTransactionResult = struct {
pub const ItemTransactionResult = struct {
cooldown: Cooldown,
fn parse(api: *ArtifactsAPI, obj: json.ObjectMap) !ItemTransactionResult {
pub fn parse(api: *ArtifactsAPI, obj: json.ObjectMap) !ItemTransactionResult {
_ = api;
const cooldown = json_utils.getObject(obj, "cooldown") orelse return error.MissingProperty;
@ -312,11 +500,129 @@ pub const ItemTransactionResult = struct {
};
}
pub fn parseDepositError(status: std.http.Status) ?BankDepositItemError {
return switch (@intFromEnum(status)) {
404 => BankDepositItemError.ItemNotFound,
461 => BankDepositItemError.BankIsBusy,
478 => BankDepositItemError.NotEnoughItems,
486 => BankDepositItemError.CharacterIsBusy,
498 => BankDepositItemError.CharacterNotFound,
499 => BankDepositItemError.CharacterInCooldown,
598 => BankDepositItemError.BankNotFound,
else => null
};
}
pub fn parseWithdrawError(status: std.http.Status) ?BankWithdrawItemError {
return switch (@intFromEnum(status)) {
404 => BankWithdrawItemError.ItemNotFound,
461 => BankWithdrawItemError.BankIsBusy,
478 => BankWithdrawItemError.NotEnoughItems,
486 => BankWithdrawItemError.CharacterIsBusy,
497 => BankWithdrawItemError.CharacterIsFull,
498 => BankWithdrawItemError.CharacterNotFound,
499 => BankWithdrawItemError.CharacterInCooldown,
598 => BankWithdrawItemError.BankNotFound,
else => null
};
}
pub fn deinit(self: ItemTransactionResult) void {
_ = self;
}
};
pub const CraftResult = struct {
cooldown: Cooldown,
details: SkillDetails,
pub fn parse(api: *ArtifactsAPI, obj: json.ObjectMap) !CraftResult {
const cooldown = json_utils.getObject(obj, "cooldown") orelse return error.MissingProperty;
const details = json_utils.getObject(obj, "details") orelse return error.MissingProperty;
return CraftResult{
.cooldown = try Cooldown.parse(cooldown),
.details = try SkillDetails.parse(api, details)
};
}
pub fn parseError(status: std.http.Status) ?CraftError {
return switch (@intFromEnum(status)) {
404 => CraftError.RecipeNotFound,
478 => CraftError.NotEnoughItems,
486 => CraftError.CharacterIsBusy,
493 => CraftError.NotEnoughSkill,
497 => CraftError.CharacterIsFull,
498 => CraftError.CharacterNotFound,
499 => CraftError.CharacterInCooldown,
598 => CraftError.WorkshopNotFound,
else => null
};
}
};
pub const UnequipResult = struct {
cooldown: Cooldown,
item: ItemId,
pub fn parse(api: *ArtifactsAPI, obj: json.ObjectMap) !UnequipResult {
const cooldown = json_utils.getObject(obj, "cooldown") orelse return error.MissingProperty;
const item = json_utils.getObject(obj, "item") orelse return error.MissingProperty;
const item_code = json_utils.getString(item, "code") orelse return error.MissingProperty;
const item_id = try api.getItemId(item_code);
// TODO: Might as well save information about time, because full details about it are given
return UnequipResult{
.cooldown = try Cooldown.parse(cooldown),
.item = item_id
};
}
pub fn parseError(status: std.http.Status) ?UnequipError {
return switch (@intFromEnum(status)) {
404 => UnequipError.ItemNotFound,
486 => UnequipError.CharacterIsBusy,
491 => UnequipError.SlotIsEmpty,
497 => UnequipError.CharacterIsFull,
498 => UnequipError.CharacterNotFound,
499 => UnequipError.CharacterInCooldown,
else => null
};
}
};
pub const EquipResult = struct {
cooldown: Cooldown,
pub fn parse(api: *ArtifactsAPI, obj: json.ObjectMap) !EquipResult {
_ = api;
const cooldown = json_utils.getObject(obj, "cooldown") orelse return error.MissingProperty;
// TODO: Might as well save information about time, because full details about it are given
return EquipResult{
.cooldown = try Cooldown.parse(cooldown)
};
}
pub fn parseError(status: std.http.Status) ?EquipError {
return switch (@intFromEnum(status)) {
404 => EquipError.ItemNotFound,
478 => EquipError.ItemNotFound, // TODO: What is the difference between 404 and 478?
485 => EquipError.SlotIsFull,
486 => EquipError.CharacterIsBusy,
491 => EquipError.SlotIsFull, // TODO: What is the difference between 485 and 491?
496 => EquipError.NotEnoughSkill,
498 => EquipError.CharacterNotFound,
499 => EquipError.CharacterInCooldown,
else => null
};
}
};
pub const ArtifactsFetchResult = struct {
arena: std.heap.ArenaAllocator,
status: std.http.Status,
@ -351,7 +657,17 @@ pub fn deinit(self: *ArtifactsAPI) void {
self.item_codes.deinit();
}
fn fetch(self: *ArtifactsAPI, method: std.http.Method, path: []const u8, payload: ?[]const u8) !ArtifactsFetchResult {
const FetchOptions = struct {
method: std.http.Method,
path: []const u8,
payload: ?[]const u8 = null
};
fn fetch(self: *ArtifactsAPI, options: FetchOptions) APIError!ArtifactsFetchResult {
const method = options.method;
const path = options.path;
const payload = options.payload;
std.log.debug("fetch {} {s}", .{method, path});
var uri = self.server_uri;
uri.path = .{ .raw = path };
@ -372,25 +688,27 @@ fn fetch(self: *ArtifactsAPI, method: std.http.Method, path: []const u8, payload
defer if (authorization_header) |str| self.allocator.free(str);
if (self.token) |token| {
authorization_header = try std.fmt.allocPrint(self.allocator, "Bearer {s}", .{token});
authorization_header = std.fmt.allocPrint(self.allocator, "Bearer {s}", .{token}) catch return APIError.OutOfMemory;
opts.headers.authorization = .{ .override = authorization_header.? };
}
const result = try self.client.fetch(opts);
const result = self.client.fetch(opts) catch return APIError.RequestFailed;
const response_body = response_storage.items;
std.log.debug("fetch result {}", .{result.status});
if (result.status != .ok) {
if (result.status == .service_unavailable) {
return APIError.ServerUnavailable;
} else if (result.status != .ok) {
return ArtifactsFetchResult{
.arena = arena,
.status = result.status
};
}
const parsed = try json.parseFromSliceLeaky(json.Value, arena.allocator(), response_body, .{ .allocate = .alloc_if_needed });
const parsed = json.parseFromSliceLeaky(json.Value, arena.allocator(), response_body, .{ .allocate = .alloc_if_needed }) catch return APIError.ParseFailed;
if (parsed != json.Value.object) {
return APIErrors.ParseFailed;
return APIError.ParseFailed;
}
return ArtifactsFetchResult{
@ -400,20 +718,63 @@ fn fetch(self: *ArtifactsAPI, method: std.http.Method, path: []const u8, payload
};
}
fn fetchAndParseObject(self: *ArtifactsAPI, Result: type, method: std.http.Method, path: []const u8, payload: ?[]const u8, args: anytype) !Result {
const result = try self.fetch(method, path, payload);
fn fetchOptionalObject(
self: *ArtifactsAPI,
Error: type,
parseError: ?fn (status: std.http.Status) ?Error,
Object: type,
parseObject: anytype,
parseObjectArgs: anytype,
fetchOptions: FetchOptions,
) Error!?Object {
if (@typeInfo(@TypeOf(parseObject)) != .Fn) {
@compileError("`parseObject` must be a function");
}
const result = try self.fetch(fetchOptions);
defer result.deinit();
if (result.status != .ok) {
return APIErrors.RequestFailed;
}
if (result.body == null) {
return APIErrors.ParseFailed;
if (Error != APIError) {
if (parseError == null) {
@compileError("`parseError` must be defined, if `Error` is not `APIError`");
}
if (parseError.?(result.status)) |error_value| {
return error_value;
}
} else {
if (parseError != null) {
@compileError("`parseError` must be null");
}
}
}
const body = json_utils.asObject(result.body.?) orelse return APIErrors.ParseFailed;
return @call(.auto, Result.parse, .{ self, body } ++ args) catch return APIErrors.ParseFailed;
// return Result.parse(self.allocator, body) catch return APIErrors.ParseFailed;
if (result.status == .not_found) {
return null;
}
if (result.status != .ok) {
return APIError.RequestFailed;
}
if (result.body == null) {
return APIError.ParseFailed;
}
const body = json_utils.asObject(result.body.?) orelse return APIError.ParseFailed;
return @call(.auto, parseObject, .{ self, body } ++ parseObjectArgs) catch return APIError.ParseFailed;
}
fn fetchObject(
self: *ArtifactsAPI,
Error: type,
parseError: ?fn (status: std.http.Status) ?Error,
Object: type,
parseObject: anytype,
parseObjectArgs: anytype,
fetchOptions: FetchOptions
) Error!Object {
const result = try self.fetchOptionalObject(Error, parseError, Object, parseObject, parseObjectArgs, fetchOptions);
return result orelse return APIError.RequestFailed;
}
pub fn setServer(self: *ArtifactsAPI, url: []const u8) !void {
@ -465,31 +826,43 @@ pub fn getItemCode(self: *const ArtifactsAPI, id: ItemId) ?[]const u8 {
// ------------------------- Endpoints ------------------------
pub fn getServerStatus(self: *ArtifactsAPI) !ServerStatus {
return try self.fetchAndParseObject(ServerStatus, .GET, "/", null, .{ self.allocator });
return try self.fetchObject(
APIError,
null,
ServerStatus,
ServerStatus.parse, .{ self.allocator },
.{ .method = .GET, .path = "/" }
);
}
pub fn getCharacter(self: *ArtifactsAPI, name: []const u8) !Character {
pub fn getCharacter(self: *ArtifactsAPI, name: []const u8) !?Character {
const path = try std.fmt.allocPrint(self.allocator, "/characters/{s}", .{name});
defer self.allocator.free(path);
return try self.fetchAndParseObject(Character, .GET, path, null, .{ self.allocator });
return try self.fetchOptionalObject(
APIError,
null,
Character,
Character.parse, .{ self.allocator },
.{ .method = .GET, .path = path }
);
}
pub fn listMyCharacters(self: *ArtifactsAPI) !CharacterList {
const path = try std.fmt.allocPrint(self.allocator, "/my/characters", .{});
defer self.allocator.free(path);
const result = try self.fetch(.GET, path, null);
const result = try self.fetch(.{ .method = .GET, .path = path });
defer result.deinit();
if (result.status != .ok) {
return APIErrors.RequestFailed;
return APIError.RequestFailed;
}
if (result.body == null) {
return APIErrors.ParseFailed;
return APIError.ParseFailed;
}
const body = json_utils.asArray(result.body.?) orelse return APIErrors.ParseFailed;
const body = json_utils.asArray(result.body.?) orelse return APIError.ParseFailed;
var characters = try std.ArrayList(Character).initCapacity(self.allocator, body.items.len);
errdefer {
@ -500,8 +873,8 @@ pub fn listMyCharacters(self: *ArtifactsAPI) !CharacterList {
}
for (body.items) |character_json| {
const character_obj = json_utils.asObject(character_json) orelse return APIErrors.ParseFailed;
const char = Character.parse(character_obj, self.allocator, self) catch return APIErrors.ParseFailed;
const character_obj = json_utils.asObject(character_json) orelse return APIError.ParseFailed;
const char = Character.parse(character_obj, self.allocator, self) catch return APIError.ParseFailed;
characters.appendAssumeCapacity(char);
}
@ -512,48 +885,190 @@ pub fn listMyCharacters(self: *ArtifactsAPI) !CharacterList {
};
}
pub fn actionFight(self: *ArtifactsAPI, name: []const u8) !FightResult {
pub fn actionFight(self: *ArtifactsAPI, name: []const u8) FightError!FightResult {
const path = try std.fmt.allocPrint(self.allocator, "/my/{s}/action/fight", .{name});
defer self.allocator.free(path);
return try self.fetchAndParseObject(FightResult, .POST, path, null, .{ });
return try self.fetchObject(
FightError,
FightResult.parseError,
FightResult,
FightResult.parse, .{ },
.{ .method = .POST, .path = path }
);
}
pub fn actionGathering(self: *ArtifactsAPI, name: []const u8) !GatheringResult {
pub fn actionGather(self: *ArtifactsAPI, name: []const u8) GatherError!GatherResult {
const path = try std.fmt.allocPrint(self.allocator, "/my/{s}/action/gathering", .{name});
defer self.allocator.free(path);
return try self.fetchAndParseObject(GatheringResult, .POST, path, null, .{ });
return try self.fetchObject(
GatherError,
GatherResult.parseError,
GatherResult,
GatherResult.parse, .{ },
.{ .method = .POST, .path = path }
);
}
pub fn actionMove(self: *ArtifactsAPI, name: []const u8, x: i64, y: i64) !MoveResult {
pub fn actionMove(self: *ArtifactsAPI, name: []const u8, x: i64, y: i64) MoveError!MoveResult {
const path = try std.fmt.allocPrint(self.allocator, "/my/{s}/action/move", .{name});
defer self.allocator.free(path);
const payload = try std.fmt.allocPrint(self.allocator, "{{ \"x\":{},\"y\":{} }}", .{x, y});
defer self.allocator.free(payload);
return try self.fetchAndParseObject(MoveResult, .POST, path, payload, .{});
return try self.fetchObject(
MoveError,
MoveResult.parseError,
MoveResult,
MoveResult.parse, .{ },
.{ .method = .POST, .path = path, .payload = payload }
);
}
pub fn actionBankDepositGold(self: *ArtifactsAPI, name: []const u8, quantity: u64) !GoldTransactionResult {
pub fn actionBankDepositGold(
self: *ArtifactsAPI,
name: []const u8,
quantity: u64
) BankDepositGoldError!GoldTransactionResult {
const path = try std.fmt.allocPrint(self.allocator, "/my/{s}/action/bank/deposit/gold", .{name});
defer self.allocator.free(path);
const payload = try std.fmt.allocPrint(self.allocator, "{{ \"quantity\":{} }}", .{quantity});
defer self.allocator.free(payload);
return try self.fetchAndParseObject(GoldTransactionResult, .POST, path, payload, .{});
return try self.fetchObject(
BankDepositGoldError,
GoldTransactionResult.parseDepositError,
GoldTransactionResult,
GoldTransactionResult.parse, .{ },
.{ .method = .POST, .path = path, .payload = payload }
);
}
pub fn actionBankDepositItem(self: *ArtifactsAPI, name: []const u8, code: []const u8, quantity: u64) !ItemTransactionResult {
pub fn actionBankDepositItem(
self: *ArtifactsAPI,
name: []const u8,
code: []const u8,
quantity: u64
) BankDepositItemError!ItemTransactionResult {
const path = try std.fmt.allocPrint(self.allocator, "/my/{s}/action/bank/deposit", .{name});
defer self.allocator.free(path);
const payload = try std.fmt.allocPrint(self.allocator, "{{ \"code\":\"{s}\",\"quantity\":{} }}", .{code, quantity});
defer self.allocator.free(payload);
return try self.fetchAndParseObject(ItemTransactionResult, .POST, path, payload, .{});
return try self.fetchObject(
BankDepositItemError,
ItemTransactionResult.parseDepositError,
ItemTransactionResult,
ItemTransactionResult.parse, .{ },
.{ .method = .POST, .path = path, .payload = payload }
);
}
pub fn actionBankWithdrawGold(
self: *ArtifactsAPI,
name: []const u8,
quantity: u64
) BankDepositGoldError!GoldTransactionResult {
const path = try std.fmt.allocPrint(self.allocator, "/my/{s}/action/bank/withdraw/gold", .{name});
defer self.allocator.free(path);
const payload = try std.fmt.allocPrint(self.allocator, "{{ \"quantity\":{} }}", .{quantity});
defer self.allocator.free(payload);
return try self.fetchObject(
BankWithdrawGoldError,
GoldTransactionResult.parseWithdrawError,
GoldTransactionResult,
GoldTransactionResult.parse, .{ },
.{ .method = .POST, .path = path, .payload = payload }
);
}
pub fn actionBankWithdrawItem(
self: *ArtifactsAPI,
name: []const u8,
code: []const u8,
quantity: u64
) BankWithdrawItemError!ItemTransactionResult {
const path = try std.fmt.allocPrint(self.allocator, "/my/{s}/action/bank/withdraw", .{name});
defer self.allocator.free(path);
const payload = try std.fmt.allocPrint(self.allocator, "{{ \"code\":\"{s}\",\"quantity\":{} }}", .{code, quantity});
defer self.allocator.free(payload);
return try self.fetchObject(
BankWithdrawItemError,
ItemTransactionResult.parseWithdrawError,
ItemTransactionResult,
ItemTransactionResult.parse, .{ },
.{ .method = .POST, .path = path, .payload = payload }
);
}
pub fn actionCraft(
self: *ArtifactsAPI,
name: []const u8,
code: []const u8,
quantity: u64
) !CraftResult {
const path = try std.fmt.allocPrint(self.allocator, "/my/{s}/action/crafting", .{name});
defer self.allocator.free(path);
const payload = try std.fmt.allocPrint(self.allocator, "{{ \"code\":\"{s}\",\"quantity\":{} }}", .{code, quantity});
defer self.allocator.free(payload);
return try self.fetchObject(
CraftError,
CraftResult.parseError,
CraftResult,
CraftResult.parse, .{ },
.{ .method = .POST, .path = path, .payload = payload }
);
}
pub fn actionUnequip(
self: *ArtifactsAPI,
name: []const u8,
slot: EquipmentSlot
) UnequipError!UnequipResult {
const path = try std.fmt.allocPrint(self.allocator, "/my/{s}/action/unequip", .{name});
defer self.allocator.free(path);
const payload = try std.fmt.allocPrint(self.allocator, "{{ \"slot\":\"{s}\" }}", .{slot.name()});
defer self.allocator.free(payload);
return try self.fetchObject(
UnequipError,
UnequipResult.parseError,
UnequipResult,
UnequipResult.parse, .{ },
.{ .method = .POST, .path = path, .payload = payload }
);
}
pub fn actionEquip(
self: *ArtifactsAPI,
name: []const u8,
slot: EquipmentSlot,
code: []const u8
) EquipError!EquipResult {
const path = try std.fmt.allocPrint(self.allocator, "/my/{s}/action/equip", .{name});
defer self.allocator.free(path);
const payload = try std.fmt.allocPrint(self.allocator, "{{ \"slot\":\"{s}\",\"code\":\"{s}\" }}", .{slot.name(), code});
defer self.allocator.free(payload);
return try self.fetchObject(
EquipError,
EquipResult.parseError,
EquipResult,
EquipResult.parse, .{ },
.{ .method = .POST, .path = path, .payload = payload }
);
}
test "parse date time" {

View File

@ -18,7 +18,6 @@ fn getItemId(api: *ArtifactsAPI, object: json.ObjectMap, name: []const u8) !?Ite
return try api.getItemId(code);
}
pub const SkillStats = struct {
level: i64,
xp: i64,

View File

@ -24,7 +24,7 @@ const Position = struct {
const QueuedAction = union(enum) {
move: Position,
attack,
gathering,
gather,
depositGold: u64,
depositItem: struct { id: ArtifactsAPI.ItemId, quantity: u64 },
};
@ -172,7 +172,7 @@ fn gatherResourceRoutine(managed_char: *ManagedCharacter, resource_pos: Position
return;
}
try action_queue.append(.{ .gathering = {} });
try action_queue.append(.{ .gather = {} });
}
pub fn main() !void {
@ -202,6 +202,12 @@ pub fn main() !void {
var manager = Manager.init(allocator, &api);
defer manager.deinit();
const status = try api.getServerStatus();
defer status.deinit();
std.log.info("Server status: {s} v{s}", .{ status.status, status.version });
std.log.info("Characters online: {}", .{ status.characters_online });
const characters = try api.listMyCharacters();
defer characters.deinit();
@ -209,11 +215,7 @@ pub fn main() !void {
try manager.addCharacter(character);
}
const status = try api.getServerStatus();
defer status.deinit();
std.log.info("Server status: {s} v{s}", .{ status.status, status.version });
std.log.info("Characters online: {}", .{ status.characters_online });
std.log.info("Starting main loop", .{});
while (manager.poll()) |char| {
@ -259,9 +261,9 @@ pub fn main() !void {
cooldown = result.cooldown;
char.character.inventory.removeItem(item.id, item.quantity);
},
.gathering => {
.gather => {
std.log.debug("{s} gathers", .{char.character.name});
var result = try api.actionGathering(char.character.name);
var result = try api.actionGather(char.character.name);
cooldown = result.cooldown;
for (result.details.items.slice()) |item| {