artificer/cli/main.zig

93 lines
2.7 KiB
Zig

// zig fmt: off
const std = @import("std");
const builtin = @import("builtin");
const Allocator = std.mem.Allocator;
const Lib = @import("lib");
const Api = Lib.Api;
const simulated = true;
pub const std_options = .{
.log_scope_levels = &[_]std.log.ScopeLevel{
.{ .scope = .api, .level = .info },
.{ .scope = .artificer, .level = .debug },
}
};
fn getAPITokenFromArgs(allocator: Allocator) !?[]u8 {
const args = try std.process.argsAlloc(allocator);
defer std.process.argsFree(allocator, args);
if (args.len < 2) {
return null;
}
const filename = args[1];
const cwd = std.fs.cwd();
var token_buffer: [256]u8 = undefined;
const token = try cwd.readFile(filename, &token_buffer);
return try allocator.dupe(u8, std.mem.trim(u8, token, "\n\t "));
}
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
const token = (try getAPITokenFromArgs(allocator)) orelse return error.MissingToken;
defer allocator.free(token);
var store = try Api.Store.init(allocator);
defer store.deinit(allocator);
var server = try Api.Server.init(allocator, &store);
defer server.deinit();
try server.setToken(token);
std.log.info("Prefetching server data", .{});
{
const cwd_path = try std.fs.cwd().realpathAlloc(allocator, ".");
defer allocator.free(cwd_path);
const cache_path = try std.fs.path.resolve(allocator, &.{ cwd_path, "./api-store-cli.bin" });
defer allocator.free(cache_path);
try server.prefetchCached(allocator, cache_path, .{ .images = false });
}
const character_id = (try server.getCharacter("Blondie")).?;
var system_clock = Lib.SystemClock{};
var sim_server = Lib.SimServer.init(0, &store);
if (simulated) {
const character = store.characters.get(character_id).?;
character.cooldown_expiration = null;
}
var artificer = if (simulated)
try Lib.ArtificerSim.init(allocator, &store, &sim_server.clock, &sim_server, character_id)
else
try Lib.ArtificerApi.init(allocator, &store, &system_clock, &server, character_id);
defer artificer.deinit(allocator);
_ = try artificer.appendGoal(.{
.gather = .{
.item = store.items.getId("copper_ore").?,
.quantity = 3
}
});
std.log.info("Starting main loop", .{});
const started_at = artificer.clock.nanoTimestamp();
try artificer.runUntilGoalsComplete();
const stopped_at = artificer.clock.nanoTimestamp();
const elapsed_time = @as(f64, @floatFromInt(stopped_at - started_at)) / std.time.ns_per_s;
std.log.info("Took {d:.3}s", .{ elapsed_time });
}