Product selection message handler

This commit is contained in:
Eden Kirin
2023-05-11 15:08:24 +02:00
parent 69e087c0c9
commit 9151aa3e1e
8 changed files with 113 additions and 89 deletions

View File

@ -6,9 +6,11 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css"
integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
<link rel="stylesheet" href="styles.css">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.min.js" integrity="sha384-cuYeSxntonz0PPNlHhBs68uyIAVpIIOZZ5JqeqvYYIcEL727kskC66kF92t6Xl2V" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.min.js"
integrity="sha384-cuYeSxntonz0PPNlHhBs68uyIAVpIIOZZ5JqeqvYYIcEL727kskC66kF92t6Xl2V"
crossorigin="anonymous"></script>
<script src="js/config.js"></script>
<script src="js/frontend.js"></script>
@ -21,7 +23,7 @@
FairHopper Visualisation Client
</h1>
<button type="button" class="btn btn-primary test-button">
Launch demo modal
Launch demo modal
</button>
<div id="purchase-container" class="purchase-container d-none">
<div class="d-flex header">
@ -59,7 +61,7 @@
moves.
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary">
<button type="button" class="btn btn-primary" id="finish-product-selection" data-bs-dismiss="modal">
Finish product selection
</button>
</div>

View File

@ -2,6 +2,8 @@ if (typeof FAIRHOPPER_WS_SERVER === "undefined") {
var FAIRHOPPER_WS_SERVER = "ws://127.0.0.1:8011";
}
let ws = null;
const BOARD_ICONS = {
PLAYER: "😀",
PLAYER_ON_DESTINATION: "😎",
@ -146,7 +148,7 @@ function productPurchaseDone(product) {
function wsConnect() {
console.log("Attempting to connect to", FAIRHOPPER_WS_SERVER);
let ws = new WebSocket(FAIRHOPPER_WS_SERVER);
ws = new WebSocket(FAIRHOPPER_WS_SERVER);
ws.onopen = () => {
console.log("WS connected");
@ -178,6 +180,7 @@ function wsConnect() {
};
ws.onclose = (e) => {
ws = null;
setTimeout(() => {
wsConnect();
}, 1000);
@ -189,6 +192,21 @@ function wsConnect() {
};
}
function finishProductSelection() {
if (!ws) {
return;
}
const wsMessage = {
message: "product_selection_done",
data: null,
};
ws.send(JSON.stringify(wsMessage));
}
window.onload = () => {
document.getElementById("finish-product-selection").onclick = () => {
finishProductSelection();
};
wsConnect();
};