shiritori minimum viable product

This commit is contained in:
Elnu 2023-04-12 19:07:09 -07:00
parent 430702f1c5
commit 3b0e75b7a1
6 changed files with 1455 additions and 2 deletions

18
demo/index.html Normal file
View file

@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>shiritori</title>
<link rel="icon" type="image/x-icon" href="https://tegakituesday.com/favicon.ico">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="content">
<h1><ruby><rp>(</rp><rt></rt><rp>)<rp></ruby>ちゃん<span style="font-size: 0.75em"></span>しりとり</h1>
<div id="shiritori"></div>
<input id="shiritori-input" onfocusout="setTimeout(() => this.focus(), 100)" autofocus>
<p id="shiritori-players"></p>
</div>
<script src="shiritori.js"></script>
</body>
</html>

38
demo/shiritori.js Normal file
View file

@ -0,0 +1,38 @@
ws = new WebSocket("ws://localhost:8080");
const div = document.querySelector("#shiritori");
const input = document.querySelector("#shiritori-input");
const players = document.querySelector("#shiritori-players");
let id = null;
ws.onmessage = e => {
const { event, data } = JSON.parse(e.data);
switch (event) {
case "greeting":
id = data.id;
break;
case "word":
let waiting = data.author === id;
p = document.createElement("p");
p.innerHTML = data.reading || data.word === data.reading ? (data.word === "" ? data.reading : `<ruby>${data.word}<rp>(</rp><rt>${data.reading}<rt/><rp>)</rp></ruby>`) : data.word;
div.appendChild(p);
div.scrollTop = div.offsetHeight;
input.placeholder = waiting ? "Waiting for other players..." : `${data.next_char}`;
input.disabled = waiting;
if (!waiting) input.focus();
break;
case "playerCount":
let otherPlayers = data.players - 1;
players.innerHTML = `${otherPlayers === 0 ? "No" : otherPlayers} other player${otherPlayers === 1 ? "" : "s"} online.`;
break;
case "error":
alert(data.message);
break;
}
}
input.addEventListener('keypress', e => {
if (e.key === 'Enter') {
ws.send(input.value);
input.value = "";
}
});

49
demo/style.css Normal file
View file

@ -0,0 +1,49 @@
* {
font-family: sans-serif;
color: ghostwhite;
}
body {
background: slategrey;
font-size: 1.25em;
}
div#content {
width: max-content;
margin: auto;
}
div#shiritori p {
margin: 0;
}
div#shiritori {
padding: 2px;
height: 8em;
overflow-y: scroll;
}
div#shiritori, input#shiritori-input {
padding: 0.25em;
background: rgba(0, 0, 0, 0.25);
border: 1px solid;
border-radius: 4px;
box-sizing: border-box;
}
input {
font-size: 1em;
}
input#shiritori-input {
width: 100%;
border-top: none;
border-top-left-radius: 0;
border-top-right-radius: 0;
}
input#shiritori-input:focus {
outline: none;
background: rgba(0, 0, 0, 0.125);
}
div#shiritori {
border-bottom: none;
border-bottom-left-radius: 0;
border-bottom-right-radius: 0;
}
p#shiritori-players {
text-align: center;
}