258 lines
8.5 KiB
HTML
258 lines
8.5 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>Multiplayer Game</title>
|
|
<link rel="stylesheet" type="text/css" href="mflx.min.css">
|
|
</head>
|
|
<body>
|
|
<button id="create-room">Create Room</button>
|
|
<input id="join-code" placeholder="Enter room code">
|
|
<button id="join-room">Join Room</button>
|
|
<div class="players"></div>
|
|
<div id="status"></div>
|
|
<button id="start-game" style="display: none;">Start Game</button>
|
|
<button id="spin-wheel" style="display: none;">Spin Wheel</button>
|
|
<input id="guess-letter" placeholder="Guess a letter" style="display: none;">
|
|
<button id="guess-button" style="display: none;">Guess</button>
|
|
<div style="display:relative;">
|
|
<span style="position: absolute; top: 0; left:50%">
|
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><title>arrow-down-bold</title><path d="M9,4H15V12H19.84L12,19.84L4.16,12H9V4Z" /></svg>
|
|
</span>
|
|
<canvas id="wheelCanvas" width="500" height="500"></canvas>
|
|
</div>
|
|
|
|
<script>
|
|
|
|
//wheel==========================================================================
|
|
const wheel = [
|
|
'lose a turn', 800, 350, 450, 700, 300, 600, 5000,
|
|
300, 600, 300, 500, 800, 550, 400, 300, 900, 500, 'spin again',
|
|
900, 'Bankrupt', 600, 400, 300
|
|
];
|
|
const wheelSpinSpeedData = [
|
|
{'0':{'lose a turn':0.252}},
|
|
{'1':{'800':.25}},
|
|
{'2':{'350':.247}},
|
|
{'3':{'450':.2445}},
|
|
{'4':{'450':.242}},
|
|
{'5':{'300':.239}},
|
|
{'6':{'300':.239}},
|
|
{'7':{'300':.239}},
|
|
{'8':{'300':.239}},
|
|
{'9':{'300':.239}},
|
|
{'10':{'300':.239}},
|
|
{'11':{'300':.239}},
|
|
{'12':{'300':.239}},
|
|
{'13':{'300':.239}},
|
|
{'14':{'300':.239}},
|
|
{'15':{'300':.239}},
|
|
{'16':{'300':.239}},
|
|
{'17':{'300':.239}},
|
|
{'18':{'300':.239}},
|
|
{'19':{'300':.239}},
|
|
{'20':{'300':.239}},
|
|
{'21':{'300':.239}},
|
|
{'22':{'300':.239}},
|
|
{'23':{'300':.239}}
|
|
];
|
|
const canvas = document.getElementById('wheelCanvas');
|
|
const ctx = canvas.getContext('2d');
|
|
const radius = canvas.width / 2;
|
|
|
|
let rotation = 0 - (.283*6); //sets the zero index on top
|
|
let spinning = false;
|
|
let spinSpeed = 0;
|
|
let img = new Image();
|
|
img.src = "arrow-down-bold.png";
|
|
img.onload = function() {
|
|
ctx.drawImage(img, 200, -50, 100,100);
|
|
}
|
|
// Draw the wheel
|
|
function drawWheel(drawResult = false) {
|
|
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
|
|
|
|
ctx.save();
|
|
|
|
|
|
ctx.translate(radius, radius);
|
|
ctx.rotate(rotation);
|
|
|
|
const anglePerSegment = (2 * Math.PI) / wheel.length;
|
|
|
|
wheel.forEach((value, i) => {
|
|
const startAngle = i * anglePerSegment;
|
|
const endAngle = startAngle + anglePerSegment;
|
|
|
|
ctx.beginPath();
|
|
ctx.moveTo(0, 0);
|
|
ctx.arc(0, 0, radius, startAngle, endAngle);
|
|
ctx.closePath();
|
|
|
|
ctx.fillStyle = i % 2 === 0 ? '#ffdd99' : '#ffeecc';
|
|
ctx.fill();
|
|
ctx.stroke();
|
|
|
|
// Add text
|
|
ctx.save();
|
|
ctx.translate(
|
|
Math.cos(startAngle + anglePerSegment / 2) * radius * 0.7,
|
|
Math.sin(startAngle + anglePerSegment / 2) * radius * 0.7
|
|
);
|
|
ctx.rotate(startAngle + anglePerSegment / 2 + Math.PI / 2 - 1.5);
|
|
ctx.textAlign = 'center';
|
|
ctx.fillStyle = '#000';
|
|
ctx.font = '12px Arial';
|
|
ctx.fillText(String(i+","+value), 0, 0);
|
|
|
|
ctx.restore();
|
|
});
|
|
|
|
ctx.restore();
|
|
ctx.drawImage(img, 200, -50, 100,100);
|
|
}
|
|
|
|
// Start spinning
|
|
function startSpin(targetIndex) {
|
|
console.log('targetIndex: ',targetIndex);
|
|
if (spinning) return;
|
|
rotation = 0 - (.283*6)
|
|
spinning = true;
|
|
|
|
const totalSpins = Math.floor(Math.random() * 3) + 3; // 3-6 full spins
|
|
const anglePerSegment = (2 * Math.PI) / wheel.length;
|
|
const targetAngle = targetIndex * anglePerSegment;
|
|
console.log(anglePerSegment);
|
|
|
|
let currentRotation = rotation; // Start from the current rotation
|
|
spinSpeed = 0.239; // Initial speed
|
|
|
|
function animate() {
|
|
|
|
if ( spinSpeed < 0.001) {
|
|
spinning = false;
|
|
console.log(`Landed on: ${wheel[targetIndex]}`);
|
|
return;
|
|
}
|
|
// console.log('currentRotation: ',currentRotation);
|
|
// console.log('spinSpeed: ', spinSpeed);
|
|
currentRotation += spinSpeed;
|
|
spinSpeed *= 0.99; // Gradual slowdown
|
|
|
|
|
|
rotation = currentRotation ;
|
|
drawWheel();
|
|
requestAnimationFrame(animate);
|
|
}
|
|
|
|
animate();
|
|
}
|
|
// Simulate WebSocket server response
|
|
// function simulateServerResponse() {
|
|
// const targetIndex = Math.floor(Math.random() * wheel.length); // Random result
|
|
// startSpin(targetIndex);
|
|
// }
|
|
|
|
// Initial draw
|
|
drawWheel();
|
|
|
|
// Trigger spin after 2 seconds
|
|
//setTimeout(simulateServerResponse, 2000);
|
|
|
|
const socket = new WebSocket("ws://localhost:8080");
|
|
|
|
document.getElementById("create-room").onclick = () => {
|
|
socket.send(JSON.stringify({ type: "create_room" }));
|
|
};
|
|
|
|
document.getElementById("join-room").onclick = () => {
|
|
const roomCode = document.getElementById("join-code").value;
|
|
socket.send(JSON.stringify({ type: "join_room", roomCode }));
|
|
};
|
|
|
|
document.getElementById("start-game").onclick = () => {
|
|
socket.send(JSON.stringify({ type: "start_game" }));
|
|
};
|
|
|
|
document.getElementById("spin-wheel").onclick = () => {
|
|
socket.send(JSON.stringify({ type: "spin_wheel" }));
|
|
};
|
|
|
|
document.getElementById("guess-button").onclick = () => {
|
|
const letter = document.getElementById("guess-letter").value;
|
|
socket.send(JSON.stringify({ type: "guess_letter", letter }));
|
|
document.getElementById("guess-letter").value = ""; // Clear input
|
|
};
|
|
|
|
socket.onmessage = (event) => {
|
|
const message = JSON.parse(event.data);
|
|
|
|
if (message.type === "room_created") {
|
|
document.getElementById("status").innerText = `Room created! Code: ${message.roomCode}`;
|
|
if (message.isLeader) {
|
|
document.getElementById("start-game").style.display = "inline";
|
|
}
|
|
}
|
|
|
|
if (message.type === "joined_room") {
|
|
const status = message.isLeader ? "You are the party leader!" : "You joined the room!";
|
|
document.getElementById("status").innerText = `Room Code: ${message.roomCode} - ${status}`;
|
|
if (message.isLeader) {
|
|
document.getElementById("start-game").style.display = "inline";
|
|
document.getElementById("spin-wheel").style.display = "inline";
|
|
document.getElementById("guess-letter").style.display = "inline";
|
|
document.getElementById("guess-button").style.display = "inline";
|
|
}
|
|
}
|
|
|
|
if (message.type === "game_started") {
|
|
document.getElementById("status").innerText = "The game has started!";
|
|
document.getElementById("start-game").style.display = "none";
|
|
document.getElementById("start-game").style.display = "inline";
|
|
document.getElementById("spin-wheel").style.display = "inline";
|
|
document.getElementById("guess-letter").style.display = "inline";
|
|
document.getElementById("guess-button").style.display = "inline";
|
|
}
|
|
|
|
if (message.type === "spin_result") {
|
|
console.log('spin result recieved');
|
|
document.getElementById("status").innerText = `Spin result: ${message.spinResult} points (${message.player})`;
|
|
//find target index
|
|
let targetIndexArr = wheel.entries();
|
|
let filterResultsArr = [];
|
|
for(let x of targetIndexArr){
|
|
if (x[1] == message.spinResult) {
|
|
filterResultsArr.push(x);
|
|
}
|
|
}
|
|
console.log(filterResultsArr[0][0]);
|
|
//if there is multiple entries where the spin exists pick a random index.
|
|
if(filterResultsArr.length > 1) {
|
|
startSpin(filterResultsArr[Math.floor(Math.random() * filterResultsArr.length)][0]);
|
|
}
|
|
else{
|
|
startSpin(filterResultsArr[0]);
|
|
}
|
|
|
|
}
|
|
|
|
if (message.type === "guess_result") {
|
|
const outcome = message.correct ? "correct" : "incorrect";
|
|
document.getElementById("status").innerText = `Guess '${message.letter}' was ${outcome} (${message.player})`;
|
|
}
|
|
|
|
if (message.type === "new_leader") {
|
|
document.getElementById("status").innerText = "You are now the party leader!";
|
|
}
|
|
|
|
if (message.type === "error") {
|
|
alert(message.message);
|
|
}
|
|
};
|
|
</script>
|
|
|
|
|
|
</body>
|
|
</html>
|