2024-07-09 02:55:13 +02:00
|
|
|
// how to we communicate...
|
|
|
|
// * in flight
|
|
|
|
// * arrived! (probably) <- this is the tricky one. we know enough time has passed for the message
|
|
|
|
// * received to have been received, but we are only halfway to the earliest possible
|
|
|
|
// * read acknowledgement.
|
|
|
|
//
|
|
|
|
// we know the length of the roundtrip: it is 2x the light lag. progress bars show a passage of time,
|
|
|
|
// it could scroll:
|
|
|
|
// * along the bottom of the message
|
|
|
|
// * across the message
|
|
|
|
// * as it is being sent, a transluscent red bar grows from right to left (towards their messages)
|
|
|
|
// * while we are waiting for acknowledgement, a similar blue bar brows from left to right
|
|
|
|
// * next to the message
|
|
|
|
|
2024-07-08 01:00:09 +02:00
|
|
|
conversation = [
|
2024-07-09 02:55:13 +02:00
|
|
|
{ c: 1, text: "hows space"},
|
|
|
|
{ c: 0, text: "trying to work out if the coffees shit but"},
|
2024-07-10 00:40:49 +02:00
|
|
|
{ c: 0, text: "my sense of taste is just not happening :/"},
|
|
|
|
{ c: 1, text: "maybe you're being spared"},
|
|
|
|
{ c: 0, text: "ur right"},
|
|
|
|
{ c: 0, text: "ship swill is a delicacy to exactly no one"},
|
|
|
|
{ c: 0, text: "at least the caffeine still works"},
|
|
|
|
{ c: 1, text: "lol"},
|
|
|
|
{ c: 1, text: "you probably got your own stash for later though, right?"},
|
|
|
|
{ c: 1, text: "knowing you"},
|
|
|
|
{ c: 0, text: "oh yea i got some on nimbus"},
|
|
|
|
{ c: 0, text: "donchuu worry"},
|
|
|
|
{ c: 0, text: "how about you?"},
|
|
|
|
{ c: 0, text: "new job soon, right?"},
|
|
|
|
{ c: 1, text: "induction tomorrow wish me luckk"},
|
2024-07-09 02:55:13 +02:00
|
|
|
{ c: 0, text: "break a leg!"},
|
|
|
|
{ c: 1, text: ":)"}
|
2024-07-08 01:00:09 +02:00
|
|
|
];
|
|
|
|
|
2024-07-09 22:51:28 +02:00
|
|
|
sentMessages = []
|
2024-07-10 00:40:49 +02:00
|
|
|
let messageIdx = -1;
|
2024-07-09 22:51:28 +02:00
|
|
|
let title = "Hester Gomez";
|
2024-07-08 01:00:09 +02:00
|
|
|
let pings = 0;
|
2024-07-10 00:40:49 +02:00
|
|
|
const startTime = Date.now();
|
2024-07-08 01:00:09 +02:00
|
|
|
|
2024-07-09 02:55:13 +02:00
|
|
|
function getMessageList() {
|
|
|
|
return document.getElementById("messages");
|
|
|
|
}
|
|
|
|
|
|
|
|
function getMessageElement(messageIdx) {
|
|
|
|
let list = getMessageList();
|
|
|
|
let messageElements = list.getElementsByTagName("li");
|
|
|
|
return messageElements[messageIdx];
|
|
|
|
}
|
|
|
|
|
|
|
|
class SentMessage {
|
2024-07-09 22:51:28 +02:00
|
|
|
constructor(oneWayLag, idx, onDelivered) {
|
2024-07-09 02:55:13 +02:00
|
|
|
this.oneWayLag = oneWayLag;
|
|
|
|
this.idx = idx;
|
|
|
|
this.createdTime = Date.now();
|
2024-07-09 22:51:28 +02:00
|
|
|
this.onDelivered = onDelivered;
|
2024-07-09 02:55:13 +02:00
|
|
|
|
|
|
|
this.updateBarIntervalId = setInterval(() => {
|
|
|
|
let elapsed = Math.abs(Date.now() - this.createdTime) / 1000;
|
|
|
|
let progress = elapsed / this.oneWayLag;
|
|
|
|
|
2024-07-09 22:51:28 +02:00
|
|
|
// divide in half to measure the round trip
|
2024-07-09 02:55:13 +02:00
|
|
|
progress /= 2;
|
|
|
|
|
|
|
|
this.setProgress(progress);
|
|
|
|
}, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
setProgress(amount) {
|
|
|
|
let thisMessage = getMessageElement(this.idx);
|
|
|
|
let progressBar = thisMessage.getElementsByClassName("progress")[0];
|
|
|
|
|
|
|
|
if (amount < 0.5) {
|
|
|
|
this.updateStatus("in flight");
|
|
|
|
progressBar.style.backgroundColor = "red";
|
|
|
|
amount *= 2;
|
|
|
|
}
|
|
|
|
else if (amount < 1) {
|
|
|
|
this.updateStatus("completing round trip");
|
|
|
|
progressBar.style.backgroundColor = "blue";
|
|
|
|
amount -= 0.5;
|
|
|
|
amount *= 2;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.updateStatus("delivered");
|
|
|
|
clearInterval(this.updateBarIntervalId);
|
|
|
|
amount = 0;
|
2024-07-09 22:51:28 +02:00
|
|
|
this.onDelivered();
|
2024-07-09 02:55:13 +02:00
|
|
|
}
|
|
|
|
amount = Math.min(amount, 1);
|
|
|
|
let percentage = `${amount * 100}%`;
|
|
|
|
progressBar.style.width = percentage;
|
|
|
|
}
|
|
|
|
|
|
|
|
updateStatus(newStatus) {
|
|
|
|
let thisMessage = getMessageElement(this.idx);
|
|
|
|
let statusElement = thisMessage.getElementsByClassName("message-status")[0];
|
|
|
|
statusElement.innerHTML = newStatus;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-08 01:00:09 +02:00
|
|
|
function updateTextBox(message) {
|
2024-07-09 02:55:13 +02:00
|
|
|
document.getElementById("textbox-input").value = message;
|
2024-07-08 01:00:09 +02:00
|
|
|
}
|
|
|
|
|
2024-07-09 22:51:28 +02:00
|
|
|
function setTypingIndicator(isTyping) {
|
|
|
|
document.getElementById("typing-indicator").innerHTML = isTyping
|
|
|
|
? "Hester is typing..."
|
|
|
|
: "";
|
|
|
|
}
|
2024-07-08 01:00:09 +02:00
|
|
|
|
2024-07-09 22:51:28 +02:00
|
|
|
// add the message at the index to the displayed messages
|
|
|
|
function addMessage(idx) {
|
|
|
|
let message = conversation[idx];
|
|
|
|
let messageHtml = getMessageHtml(message.text, isMessageOurs(message));
|
|
|
|
getMessageList().innerHTML += messageHtml;
|
2024-07-10 15:16:33 +02:00
|
|
|
|
|
|
|
// scroll as far as we can so that messages aren't hidden
|
|
|
|
window.scrollTo(0, document.body.scrollHeight);
|
2024-07-08 01:00:09 +02:00
|
|
|
}
|
|
|
|
|
2024-07-09 02:55:13 +02:00
|
|
|
function getMessageHtml(text, isOurs) {
|
|
|
|
let owner = isOurs ? "ours" : "theirs";
|
|
|
|
|
|
|
|
// we don't want loading bars on their messages, since we have no idea if one has been sent
|
|
|
|
// until it arrives
|
|
|
|
let progressBar = isOurs
|
|
|
|
? `<div class="progress-bar"><div class="progress"></div></div>`
|
|
|
|
: "";
|
|
|
|
|
|
|
|
let statusText = isOurs
|
|
|
|
? `<p class="message-status">status</p>`
|
|
|
|
: "";
|
|
|
|
|
|
|
|
let message = `<li><div class="message">
|
|
|
|
<span class="message-content rounded-rectangle ${owner}">
|
|
|
|
${progressBar}
|
|
|
|
${text}
|
|
|
|
</span></div>${statusText}</li>`;
|
|
|
|
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
2024-07-08 01:00:09 +02:00
|
|
|
function isMessageOurs(message) {
|
|
|
|
if (!message)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return message.c == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getOurNextMessage(idx) {
|
|
|
|
for (let i = idx; i < conversation.length; i++) {
|
|
|
|
message = conversation[i];
|
|
|
|
if (isMessageOurs(message))
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getLightLag() {
|
2024-07-10 00:40:49 +02:00
|
|
|
const baseLag = 9.23582;
|
|
|
|
|
|
|
|
// second since opening the page
|
|
|
|
const elapsed = (Date.now() - startTime) / 1000.5;
|
|
|
|
|
|
|
|
// lag should shift on the order of 10,000ths of seconds per second
|
|
|
|
const lag = baseLag + elapsed / 8439.123;
|
|
|
|
return Math.round(lag * 100000) / 100000;
|
2024-07-08 01:00:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function updatePings() {
|
|
|
|
let newTitle = pings > 0
|
|
|
|
? `(${pings}) ${title}`
|
|
|
|
: title;
|
|
|
|
|
|
|
|
document.title = newTitle;
|
|
|
|
}
|
|
|
|
|
|
|
|
function clearPings() {
|
|
|
|
pings = 0;
|
|
|
|
updatePings();
|
|
|
|
}
|
|
|
|
|
|
|
|
function getResponses(idx) {
|
|
|
|
// get the messages that aren't ours until we find one that is
|
|
|
|
let responses = [];
|
|
|
|
|
|
|
|
for (let i = idx; i < conversation.length; i++) {
|
|
|
|
message = conversation[i];
|
|
|
|
if (isMessageOurs(message))
|
|
|
|
break;
|
|
|
|
|
|
|
|
responses.push(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
return responses;
|
|
|
|
}
|
|
|
|
|
2024-07-09 22:51:28 +02:00
|
|
|
function getRandomDelay(min, max) {
|
|
|
|
const range = max - min;
|
|
|
|
return min + Math.random() * range;
|
|
|
|
}
|
|
|
|
|
2024-07-10 00:40:49 +02:00
|
|
|
function updateChat(messageIdx) {
|
|
|
|
addMessage(messageIdx);
|
|
|
|
const nextMessage = conversation[messageIdx+1];
|
|
|
|
updatePreviewText(nextMessage);
|
|
|
|
updatePings();
|
|
|
|
}
|
|
|
|
|
2024-07-08 01:00:09 +02:00
|
|
|
function waitForIncomingMessages() {
|
|
|
|
// we don't want messages to arrive all at once if there are multiple messages,
|
|
|
|
// so we need to add a small delay to consecutive messages and wait for them one by one
|
2024-07-09 22:51:28 +02:00
|
|
|
let smallDelay = getRandomDelay(2, 10);
|
2024-07-10 00:40:49 +02:00
|
|
|
let responses = getResponses(messageIdx + 1);
|
2024-07-08 01:00:09 +02:00
|
|
|
let lightLag = getLightLag();
|
|
|
|
|
2024-07-09 22:51:28 +02:00
|
|
|
setTimeout(() => {
|
|
|
|
setTypingIndicator(true);
|
2024-07-08 01:00:09 +02:00
|
|
|
|
2024-07-09 22:51:28 +02:00
|
|
|
for (let i = 0; i < responses.length; i++) {
|
|
|
|
let delaySeconds = lightLag + smallDelay * i;
|
2024-07-08 01:00:09 +02:00
|
|
|
|
2024-07-09 22:51:28 +02:00
|
|
|
const stopTyping = i == responses.length - 1;
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
messageIdx++;
|
|
|
|
pings++;
|
|
|
|
|
|
|
|
// update the chat with their message
|
|
|
|
updateChat(messageIdx);
|
|
|
|
|
|
|
|
if (stopTyping) {
|
|
|
|
setTypingIndicator(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
}, delaySeconds * 1000);
|
|
|
|
}
|
|
|
|
},getRandomDelay(1, 3));
|
2024-07-08 01:00:09 +02:00
|
|
|
}
|
|
|
|
|
2024-07-10 00:40:49 +02:00
|
|
|
function updatePreviewText(message) {
|
2024-07-08 01:00:09 +02:00
|
|
|
// display our next message or an empty box
|
2024-07-10 00:40:49 +02:00
|
|
|
let previewText = isMessageOurs(message)
|
|
|
|
? message.text
|
2024-07-08 01:00:09 +02:00
|
|
|
: "";
|
|
|
|
updateTextBox(previewText);
|
|
|
|
}
|
|
|
|
|
2024-07-09 02:55:13 +02:00
|
|
|
function sendOurMessage() {
|
|
|
|
// the message we are sending is the one currently in the text box, so we should construct it
|
|
|
|
// before advancing the conversation
|
|
|
|
let oneWayLag = getLightLag();
|
|
|
|
let currentMessage = conversation[messageIdx];
|
2024-07-10 00:40:49 +02:00
|
|
|
let nextMessage = conversation[messageIdx + 2];
|
2024-07-09 22:51:28 +02:00
|
|
|
|
2024-07-10 00:40:49 +02:00
|
|
|
let sentMessage = new SentMessage(oneWayLag, messageIdx + 1, () => {
|
2024-07-08 01:00:09 +02:00
|
|
|
|
2024-07-09 22:51:28 +02:00
|
|
|
// if the next message is ours we don't need to wait for anything
|
|
|
|
if (isMessageOurs(nextMessage))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// wait for them to read the message
|
|
|
|
setTimeout(() => {
|
|
|
|
// set the message status to read
|
|
|
|
sentMessage.updateStatus("read");
|
|
|
|
|
|
|
|
// hide the status of previous messages we first need to have references to all of them
|
|
|
|
// when creating messages we need to add these too an array
|
|
|
|
for (let i = 0; i < sentMessages.length; i++) {
|
|
|
|
let message = sentMessages[i];
|
|
|
|
if (message != sentMessage) {
|
|
|
|
message.updateStatus("");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
waitForIncomingMessages();
|
2024-07-10 00:40:49 +02:00
|
|
|
}, getRandomDelay(1, 20) * 1000);
|
2024-07-09 22:51:28 +02:00
|
|
|
});
|
|
|
|
sentMessages.push(sentMessage);
|
2024-07-08 01:00:09 +02:00
|
|
|
|
2024-07-09 02:55:13 +02:00
|
|
|
// advance conversation with our next message
|
2024-07-08 01:00:09 +02:00
|
|
|
messageIdx++;
|
|
|
|
|
2024-07-09 22:51:28 +02:00
|
|
|
// update displayed messages
|
2024-07-08 01:00:09 +02:00
|
|
|
updateChat(messageIdx);
|
|
|
|
}
|
|
|
|
|
2024-07-09 02:55:13 +02:00
|
|
|
function pressSendButton() {
|
|
|
|
// we have interacted with the page so remove all pings
|
|
|
|
clearPings();
|
|
|
|
|
|
|
|
// peek conversation state
|
2024-07-10 00:40:49 +02:00
|
|
|
let nextMessage = conversation[messageIdx + 1];
|
2024-07-09 02:55:13 +02:00
|
|
|
|
|
|
|
// we are still waiting to receive messages so pressing the button oughtn't do anything
|
|
|
|
if (!isMessageOurs(nextMessage))
|
|
|
|
return;
|
|
|
|
|
|
|
|
sendOurMessage();
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateLightLag() {
|
|
|
|
let text = getLightLag().toFixed(5) + " seconds";
|
|
|
|
document.getElementById("delay-text").innerHTML = text;
|
2024-07-08 01:00:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function startLightLagUpdateLoop() {
|
|
|
|
setInterval(() => {
|
2024-07-09 02:55:13 +02:00
|
|
|
updateLightLag();
|
2024-07-08 01:00:09 +02:00
|
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
function init() {
|
2024-07-09 22:51:28 +02:00
|
|
|
setTimeout(() => {
|
2024-07-10 00:40:49 +02:00
|
|
|
messageIdx = 0;
|
|
|
|
pings = 1;
|
|
|
|
updateChat(messageIdx);
|
2024-07-09 22:51:28 +02:00
|
|
|
setTypingIndicator(false);
|
2024-07-10 00:40:49 +02:00
|
|
|
}, 3623);
|
2024-07-08 01:00:09 +02:00
|
|
|
|
|
|
|
document.title = title;
|
|
|
|
|
|
|
|
updateLightLag(0);
|
|
|
|
startLightLagUpdateLoop();
|
|
|
|
}
|
|
|
|
|
|
|
|
init();
|
|
|
|
|