feat: web: write win count to disk

ci-dev
LeRoyce Pearson 2024-05-25 04:23:28 -06:00
parent 812caef88a
commit e157502750
2 changed files with 38 additions and 39 deletions

View File

@ -16,8 +16,8 @@
// internet connectivity. // internet connectivity.
.dependencies = .{ .dependencies = .{
.seizer = .{ .seizer = .{
.url = "https://github.com/leroycep/seizer/archive/c66b5e5f1148a1f6ce37560ed1b8bc883520541e.tar.gz", .url = "https://github.com/leroycep/seizer/archive/8fbc262657f505a11505d12eb277ff5736821769.tar.gz",
.hash = "122051972ed81522b6d1a8e1306f0cd3848a81180781794dce80fefcf1e42dc00d30", .hash = "1220b6df5a68c8c918d92befcfbe379010c7ddbe25fded0a2e103c4514012ad48b97",
}, },
}, },
.paths = .{ .paths = .{

View File

@ -1,7 +1,10 @@
pub const main = seizer.main; pub const main = seizer.main;
const APPNAME = "seizer-solitaire";
var gpa: std.mem.Allocator = undefined; var gpa: std.mem.Allocator = undefined;
var prng: std.rand.DefaultPrng = undefined; var prng: std.rand.DefaultPrng = undefined;
var context_global: *seizer.Context = undefined;
var window_global: seizer.Window = undefined; var window_global: seizer.Window = undefined;
var canvas: seizer.Canvas = undefined; var canvas: seizer.Canvas = undefined;
var card_tilemap: ?DeckSprites = null; var card_tilemap: ?DeckSprites = null;
@ -35,6 +38,8 @@ var win_count: u32 = 0;
var win_triggered: bool = false; var win_triggered: bool = false;
pub fn init(context: *seizer.Context) !void { pub fn init(context: *seizer.Context) !void {
context_global = context;
gpa = context.gpa; gpa = context.gpa;
prng = std.rand.DefaultPrng.init(@bitCast(std.time.timestamp())); prng = std.rand.DefaultPrng.init(@bitCast(std.time.timestamp()));
@ -106,6 +111,7 @@ pub fn init(context: *seizer.Context) !void {
.on_event = toggleMenu, .on_event = toggleMenu,
.default_bindings = &.{ .default_bindings = &.{
.{ .gamepad = .start }, .{ .gamepad = .start },
.{ .keyboard = .esc },
}, },
}); });
try context.addButtonInput(.{ try context.addButtonInput(.{
@ -113,50 +119,35 @@ pub fn init(context: *seizer.Context) !void {
.on_event = undo, .on_event = undo,
.default_bindings = &.{ .default_bindings = &.{
.{ .gamepad = .x }, .{ .gamepad = .x },
.{ .keyboard = .a },
}, },
}); });
win_count = try loadWinCount(gpa); const read_buffer = try gpa.alloc(u8, @sizeOf(u32));
context.readFile(.{
.appname = APPNAME,
.path = "win_count.bin",
.buffer = read_buffer,
.callback = onWinCountRead,
.userdata = read_buffer.ptr,
});
} }
fn loadWinCount(allocator: std.mem.Allocator) !u32 { fn onWinCountRead(userdata: ?*anyopaque, result: seizer.Context.FileError![]const u8) void {
if (builtin.target.os.tag == .wasi) return 0; const buffer: *[4]u8 = @ptrCast(userdata);
defer gpa.free(buffer);
const app_data_dir_path = try std.fs.getAppDataDir(allocator, "seizer-solitaire"); if (result) |bytes| {
defer allocator.free(app_data_dir_path); if (bytes.len >= @sizeOf(@TypeOf(win_count))) {
win_count = std.mem.readInt(u32, bytes[0..4], .little);
var app_data_dir = try std.fs.cwd().makeOpenPath(app_data_dir_path, .{}); }
defer app_data_dir.close(); } else |_| {}
var win_count_file = try app_data_dir.createFile("win_count.bin", .{ .read = true, .truncate = false });
defer win_count_file.close();
var buffer: [4]u8 = undefined;
const bytes_read = try win_count_file.preadAll(&buffer, 0);
if (bytes_read < buffer.len) {
std.mem.writeInt(u32, &buffer, 0, .little);
try win_count_file.pwriteAll(&buffer, 0);
return 0;
} else {
return std.mem.readInt(u32, &buffer, .little);
}
} }
fn saveWinCount(allocator: std.mem.Allocator, count: u32) !void { fn onWinCountWritten(userdata: ?*anyopaque, result: seizer.Context.FileError!void) void {
if (builtin.target.os.tag == .wasi) return; const buffer: *u32 = @ptrCast(@alignCast(userdata));
defer gpa.destroy(buffer);
const app_data_dir_path = try std.fs.getAppDataDir(allocator, "seizer-solitaire"); _ = result catch {};
defer allocator.free(app_data_dir_path);
var app_data_dir = try std.fs.cwd().makeOpenPath(app_data_dir_path, .{});
defer app_data_dir.close();
var win_count_file = try app_data_dir.createFile("win_count.bin", .{ .read = true, .truncate = false });
defer win_count_file.close();
var buffer: [4]u8 = undefined;
std.mem.writeInt(u32, &buffer, count, .little);
try win_count_file.pwriteAll(&buffer, 0);
} }
fn resetGame() !void { fn resetGame() !void {
@ -226,7 +217,15 @@ fn render(window: seizer.Window) !void {
if (haveWon() and !win_triggered) { if (haveWon() and !win_triggered) {
win_count += 1; win_count += 1;
try saveWinCount(gpa, win_count); const win_count_write_buffer = try gpa.create(u32);
std.mem.writeInt(u32, std.mem.asBytes(win_count_write_buffer), win_count, .little);
context_global.writeFile(.{
.appname = APPNAME,
.path = "win_count.bin",
.data = std.mem.asBytes(win_count_write_buffer),
.callback = onWinCountWritten,
.userdata = win_count_write_buffer,
});
resetHistory(); resetHistory();
win_triggered = true; win_triggered = true;