Skip to content

Commit 8af2336

Browse files
committed
fix safari audio issues
1 parent f884389 commit 8af2336

File tree

4 files changed

+52
-12
lines changed

4 files changed

+52
-12
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ This code is interesting for the following reasons :
111111

112112
- Its all written in Zig
113113
- It uses the excellent http.zig library https://github.com/karlseguin/http.zig
114-
- Generated docker image = about 300kb
114+
- Generated docker image = 770Kb (compressed) All it has is the compiled executable (2.5MB), which includes all bundled assets in the single file binary
115115
- Run stats - uses about 60MB RAM and really low CPU %
116116
- Its about as simple as doing the same thing in Go, there is really nothing too nasty required in the code.
117117
- The router, and all the HTML contents is part of the Game object ... the implications of this are that it is possible to create 'web components' using this

src/game.zig

+17-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ const Board = @import("board.zig");
44
const Errors = @import("errors.zig");
55

66
const start_countdown_timer: i64 = 30;
7+
const initial_countdown_timer: i64 = 120;
8+
const all_players: u8 = 100;
9+
710
const Self = @This();
811

912
const MAX_PLAYERS = 8;
@@ -105,8 +108,9 @@ fn watcherThread(self: *Self) void {
105108
}
106109

107110
self.game_mutex.lock();
108-
self.current_player = 100;
111+
self.current_player = all_players;
109112
self.state = .winner;
113+
self.expiry_time = std.time.timestamp() + start_countdown_timer;
110114
self.signal(.victory);
111115
self.game_mutex.unlock();
112116
}
@@ -153,7 +157,10 @@ fn signal(self: *Self, ev: Event) void {
153157
// locks the event_mutex (not the game_mutex) - so that it can broadcast all event threads
154158
self.event_mutex.lock();
155159
self.last_event = ev;
156-
self.expiry_time = std.time.timestamp() + self.countdown_timer;
160+
const new_expiry_time = std.time.timestamp() + self.countdown_timer;
161+
if (self.expiry_time < new_expiry_time) {
162+
self.expiry_time = new_expiry_time;
163+
}
157164
self.event_condition.broadcast();
158165
self.event_mutex.unlock();
159166

@@ -171,7 +178,7 @@ fn reboot(self: *Self) void {
171178
self.players = 2;
172179
self.needed_to_win = 3;
173180
self.flipper_chance = 0;
174-
self.expiry_time = std.time.timestamp() + 100;
181+
self.expiry_time = std.time.timestamp() + initial_countdown_timer;
175182
self.signal(.init);
176183
}
177184

@@ -184,7 +191,7 @@ fn restart(self: *Self, req: *httpz.Request, res: *httpz.Response) !void {
184191
res.body = "restarted";
185192
self.state = .init;
186193
self.countdown_timer = start_countdown_timer;
187-
self.expiry_time = std.time.timestamp() + 100;
194+
self.expiry_time = std.time.timestamp() + initial_countdown_timer;
188195
self.signal(.init);
189196
}
190197

@@ -197,7 +204,7 @@ fn clock(self: *Self, stream: std.net.Stream) !void {
197204
try w.writeAll("event: clock\n");
198205
var remaining = self.expiry_time - std.time.timestamp();
199206
if (self.state == .running and remaining > 0) {
200-
try stream.writer().print("data: {d} seconds remaining ...\n\n", .{self.expiry_time - std.time.timestamp()});
207+
try stream.writer().print("data: {d} seconds remaining ...\n\n", .{remaining});
201208
} else {
202209
try w.writeAll("data: 🕑\n\n");
203210
}
@@ -219,30 +226,35 @@ fn zeroWing(self: *Self, req: *httpz.Request, res: *httpz.Response) !void {
219226
fn yourTurnAudio(self: *Self, req: *httpz.Request, res: *httpz.Response) !void {
220227
_ = self;
221228
_ = req;
229+
res.header("Content-Type", "audio/mpeg");
222230
res.body = @embedFile("audio/your-turn.mp3");
223231
}
224232

225233
fn zeroWingAudio(self: *Self, req: *httpz.Request, res: *httpz.Response) !void {
226234
_ = self;
227235
_ = req;
236+
res.header("Content-Type", "audio/mpeg");
228237
res.body = @embedFile("audio/zero-wing.mp3");
229238
}
230239

231240
fn nukeAudio(self: *Self, req: *httpz.Request, res: *httpz.Response) !void {
232241
_ = self;
233242
_ = req;
243+
res.header("Content-Type", "audio/mpeg");
234244
res.body = @embedFile("audio/nuke.mp3");
235245
}
236246

237247
fn victoryAudio(self: *Self, req: *httpz.Request, res: *httpz.Response) !void {
238248
_ = self;
239249
_ = req;
250+
res.header("Content-Type", "audio/mpeg");
240251
res.body = @embedFile("audio/victory.mp3");
241252
}
242253

243254
fn lostAudio(self: *Self, req: *httpz.Request, res: *httpz.Response) !void {
244255
_ = self;
245256
_ = req;
257+
res.header("Content-Type", "audio/mpeg");
246258
res.body = @embedFile("audio/lost.mp3");
247259
}
248260

src/html/index.html

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<!DOCTYPE html>
12
<html>
23

34
<head>
@@ -9,7 +10,7 @@
910
<script src="https://unpkg.com/htmx.org/dist/ext/sse.js"></script>
1011
<script src="https://unpkg.com/htmx.org/dist/ext/json-enc.js"></script>
1112

12-
<meta name="app-version" content="20230726.1" desc="now with audio">
13+
<meta name="app-version" content="20230727.1" desc="fixed safari audio">
1314

1415
<link rel="stylesheet" href="styles.css">
1516
<link rel="preconnect" href="https://fonts.googleapis.com">
@@ -45,12 +46,17 @@
4546
Player <span id="player" hx-swap-oob="true">
4647
</div>
4748

49+
<div>
50+
<input class="blue-button" type="button" value="Enable Audio" onclick="yourTurnAudio.play()">
51+
</div>
52+
4853
<script>
49-
let yourTurnAudio = new Audio("audio/your-turn.mp3")
50-
let zeroWingAudio = new Audio("audio/zero-wing.mp3")
51-
let nukeAudio = new Audio("audio/nuke.mp3")
52-
let victorAudio = new Audio("audio/victory.mp3")
53-
let lostAudio = new Audio("audio/lost.mp3")
54+
// define some audio snippets
55+
const yourTurnAudio = new Audio("audio/your-turn.mp3")
56+
const zeroWingAudio = new Audio("audio/zero-wing.mp3")
57+
const nukeAudio = new Audio("audio/nuke.mp3")
58+
const victoryAudio = new Audio("audio/victory.mp3")
59+
const lostAudio = new Audio("audio/lost.mp3")
5460

5561
// Just some minimal vanilla-JS to add a couple of helper functions
5662
function getPlayer() {

src/html/styles.css

+22
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,28 @@ body {
117117
box-shadow: inset -2px -2px 3px rgba(255, 255, 255, 0.6), inset 2px 2px 3px rgba(0, 0, 0, 0.6);
118118
}
119119

120+
.blue-button {
121+
border: 0;
122+
line-height: 2.5;
123+
padding: 0 20px;
124+
font-size: 1rem;
125+
text-align: center;
126+
color: #fff;
127+
text-shadow: 1px 1px 1px #000;
128+
border-radius: 10px;
129+
background-color: rgba(0, 100, 200, 1);
130+
background-image: linear-gradient(to top left, rgba(0, 0, 0, 0.2), rgba(0, 0, 0, 0.2) 30%, rgba(0, 0, 0, 0));
131+
box-shadow: inset 2px 2px 3px rgba(255, 255, 255, 0.6), inset -2px -2px 3px rgba(0, 0, 0, 0.6);
132+
}
133+
134+
.blue-button:hover {
135+
background-color: rgba(255, 0, 0, 1);
136+
}
137+
138+
.blue-button:active {
139+
box-shadow: inset -2px -2px 3px rgba(255, 255, 255, 0.6), inset 2px 2px 3px rgba(0, 0, 0, 0.6);
140+
}
141+
120142
.green-button {
121143
border: 0;
122144
line-height: 2.5;

0 commit comments

Comments
 (0)