Skip to content

Commit

Permalink
Web import via base64
Browse files Browse the repository at this point in the history
  • Loading branch information
ringtailsoftware committed Dec 2, 2024
1 parent 68f8bd8 commit e6a5bbd
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 16 deletions.
17 changes: 12 additions & 5 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
font-family: Inter,-apple-system,system-ui,"Segoe UI",Helvetica,Arial,sans-serif;

}
.label {
font-family: Inter,-apple-system,system-ui,"Segoe UI",Helvetica,Arial,sans-serif;
font-size: 16px;
font-weight: 600;
letter-spacing: normal;
line-height: 1.5;
}
.button {
appearance: none;
backface-visibility: hidden;
Expand Down Expand Up @@ -102,10 +109,10 @@
await Zoridor.start();

document.getElementById("restart0").onclick = () => {
Zoridor.restart(0);
Zoridor.restart(0, document.getElementById("record").value);
};
document.getElementById("restart1").onclick = () => {
Zoridor.restart(1);
Zoridor.restart(1, document.getElementById("record").value);
};
});
</script>
Expand Down Expand Up @@ -137,13 +144,13 @@ <h1>Zoridor</h1>
<p>
<form>
<p>
<input id="restart0" class="button" role="button" type="button" value="Restart: Move first"/>
<input id="restart0" class="button" role="button" type="button" value="Load game log and go first"/>
</p>
<p>
<input id="restart1" class="button" role="button" type="button" value="Restart: Move second"/>
<input id="restart1" class="button" role="button" type="button" value="Load game log and go second"/>
</p>
<p>
<input type="text" value="" id="record" readonly>
<span class="label">Game Log: </label><input type="text" value="" id="record">
</p>
</form>
</p>
Expand Down
47 changes: 38 additions & 9 deletions src/webmain.zig
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ const WebState = struct {
pi: usize,
gs: GameState,
record: GameRecord,
recordB64Buf: ?[]const u8,
recordB64Buf: ?[]const u8, // base64 of all moves performed so far
startB64Buf: [1024]u8, // buffer to setup initial game state
start: ?[]u8,
};
var wstate:WebState = undefined;
var inited = false;
Expand All @@ -38,7 +40,6 @@ fn updateRecord(move:Move) !void {
wstate.recordB64Buf = try wstate.record.toStringBase64Alloc(std.heap.page_allocator);
}


// exposed to JS
fn getNextMoveInternal() !void {
wstate.pi = (wstate.pi + 1) % config.NUM_PAWNS;
Expand Down Expand Up @@ -121,8 +122,10 @@ export fn getFencesRemaining(pi:usize) usize {
return wstate.gs.pawns[pi].numFencesRemaining;
}

export fn restart(pi:usize) void {
_ = gamesetup(pi) catch 0;
export fn restart(pi:usize, setupB64Len:u32) bool {
const b64input = wstate.startB64Buf[0..setupB64Len];
//_ = console.print("RESTART setupB64Len={d} s={s}\n", .{setupB64Len, b64input}) catch 0;
return gamesetup(pi, b64input) catch false;
}

export fn allocUint8(length: u32) [*]const u8 {
Expand All @@ -131,6 +134,14 @@ export fn allocUint8(length: u32) [*]const u8 {
return slice.ptr;
}

export fn getGameStartRecordLen() usize {
return wstate.startB64Buf.len;
}

export fn getGameStartRecordPtr() [*]const u8 {
return (&wstate.startB64Buf).ptr;
}

export fn getGameRecordPtr() [*]const u8 {
if (wstate.recordB64Buf) |b| {
return b.ptr;
Expand All @@ -146,30 +157,48 @@ export fn getGameRecordLen() usize {
}
}

fn gamesetup(pi:usize) !void {
fn gamesetup(piStart:usize, b64O:?[]const u8) !bool {
if (inited) {
wstate.record.deinit();
if (wstate.recordB64Buf != null) {
std.heap.page_allocator.free(wstate.recordB64Buf.?);
}
}

var pi = piStart;
var gs = GameState.init();
var recordB64Buf:?[]const u8 = null;
var record:GameRecord = undefined;
if (b64O) |b64| {
// user supplied starting state
record = try GameRecord.initFromBase64(std.heap.page_allocator, b64);
gs = try record.toGameState(true);
recordB64Buf = try record.toStringBase64Alloc(std.heap.page_allocator);
pi += record.getAllMoves().len % 2;
} else {
record = try GameRecord.init(std.heap.page_allocator);
}

wstate = .{
.pi = pi,
.gs = GameState.init(),
.record = try GameRecord.init(std.heap.page_allocator),
.recordB64Buf = null,
.gs = gs,
.record = record,
.recordB64Buf = recordB64Buf,
.startB64Buf = undefined,
.start = null,
};
inited = true;
config.players[0] = try UiAgent.make("machine"); // should be "null"
config.players[1] = try UiAgent.make("machine");
if (pi != 0) {
_ = getNextMoveInternal() catch 0;
}
return true;
}

export fn init() void {
_ = console.print("Hello world\n", .{}) catch 0;
_ = gamesetup(0) catch 0;
_ = gamesetup(0, null) catch 0;
}

pub fn logFn(
Expand Down
14 changes: 12 additions & 2 deletions src/zoridor.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,19 @@ export class Zoridor {
});
}

static restart(pi) {
static restart(pi, b64) {
this.gameOver = false;
globalInstance.exports.restart(pi);

const wasmMemoryArray = new Uint8Array(globalInstance.exports.memory.buffer);
if (b64.length > globalInstance.exports.getGameStartRecordLen()) {
alert("too big");
}
var arr = new Uint8Array(wasmMemoryArray.buffer, globalInstance.exports.getGameStartRecordPtr(), b64.length);
arr.set(new TextEncoder().encode(b64));

if (!globalInstance.exports.restart(pi, b64.length)) {
alert("bad data");
}
this.fetchState();
this.drawPieces();
}
Expand Down

0 comments on commit e6a5bbd

Please sign in to comment.