Compare commits

...

3 Commits

Author SHA1 Message Date
1eed104790 If dead, disable rotations 2025-03-16 00:24:45 -04:00
6f4e6a4f30 Update Cargo.lock and Cargo.toml 2025-03-16 00:07:32 -04:00
5e19f0af47 Update websocket.rs and script.js 2025-03-16 00:07:25 -04:00
4 changed files with 114 additions and 10 deletions

1
Cargo.lock generated

@ -1888,6 +1888,7 @@ dependencies = [
"enum-primitive-derive",
"flate2",
"itertools 0.13.0",
"lazy_static",
"local-ip-address",
"log",
"memflow",

@ -41,6 +41,8 @@ itertools = "0.13.0"
flate2 = "1.0"
rand = "0.8"
lazy_static = "1.4"
[build-dependencies]
reqwest = { version = "0.12.9", features = ["blocking"] }
serde = { version = "1.0.215", features = ["derive"] }

@ -10,7 +10,7 @@ use std::io::Write;
use tokio::sync::RwLock;
use tower_http::services::ServeDir;
use crate::comms::RadarData;
use crate::comms::{RadarData, ArcRwlockRadarData};
#[derive(Clone)]
struct AppState {
@ -34,14 +34,25 @@ async fn handle_socket(mut socket: WebSocket, state: AppState) {
if let Ok(json) = serde_json::to_string(&*radar_data) {
compression_buffer.clear();
let mut encoder = GzEncoder::new(Vec::new(), Compression::fast());
let compression_level = if json.len() > 10000 {
Compression::best()
} else {
Compression::fast()
};
let mut encoder = GzEncoder::new(Vec::new(), compression_level);
if encoder.write_all(json.as_bytes()).is_ok() {
match encoder.finish() {
Ok(compressed) => {
let mut message = vec![0x01];
message.extend_from_slice(&compressed);
let _ = socket.send(Message::Binary(message)).await;
if compressed.len() < json.len() {
let mut message = vec![0x01];
message.extend_from_slice(&compressed);
let _ = socket.send(Message::Binary(message)).await;
} else {
let mut uncompressed = vec![0x00];
uncompressed.extend_from_slice(json.as_bytes());
let _ = socket.send(Message::Binary(uncompressed)).await;
}
},
Err(_) => {
let mut uncompressed = vec![0x00];

@ -20,6 +20,10 @@ let frameCounter = 0;
let fpsStartTime = 0;
let currentFps = 0;
let temporarilyDisableRotation = false;
let rotationDisabledUntilRespawn = false;
let lastKnownPositions = {};
let focusedPlayerYaw = 0;
let focusedPlayerName = "YOU";
let focusedPlayerPos = null;
@ -68,6 +72,35 @@ const websocketAddr = location.protocol === 'https:'
const clamp = (num, min, max) => Math.min(Math.max(num, min), max);
const degreesToRadians = (degrees) => degrees * (Math.PI / 180);
const pingTracker = {
history: [],
lastRequestTime: 0,
maxSamples: 10,
startRequest: function () {
this.lastRequestTime = performance.now();
},
endRequest: function () {
if (this.lastRequestTime === 0) return;
const ping = performance.now() - this.lastRequestTime;
this.history.push(ping);
if (this.history.length > this.maxSamples) {
this.history.shift();
}
this.lastRequestTime = 0;
},
getAveragePing: function () {
if (this.history.length === 0) return 0;
const sum = this.history.reduce((a, b) => a + b, 0);
return sum / this.history.length;
}
};
function render() {
requestAnimationFrame(render);
@ -83,6 +116,7 @@ function render() {
if (!isRequestPending && websocket && websocket.readyState === WebSocket.OPEN) {
isRequestPending = true;
pingTracker.startRequest();
websocket.send("requestInfo");
}
@ -117,7 +151,11 @@ function renderFrame() {
ctx.font = "16px Arial";
ctx.textAlign = "left";
ctx.fillStyle = "#00FF00";
ctx.fillText(`${currentFps} FPS | ${freq} Hz`, 10, 20);
let rotationStatus = "Active";
if (temporarilyDisableRotation) rotationStatus = "Manually Disabled";
else if (rotationDisabledUntilRespawn) rotationStatus = "Disabled (Death)";
ctx.fillText(`${currentFps} FPS | ${freq} Hz | Ping: ${Math.round(pingTracker.getAveragePing())}ms | Rotation: ${rotationStatus}`, 10, 20);
}
}
@ -127,6 +165,7 @@ function processPlayerPositions() {
localPlayerPos = null;
focusedPlayerPos = null;
focusedPlayerYaw = 0;
let oldPlayerList = { ...playerList };
playerList = {};
entityData.forEach(data => {
@ -140,20 +179,38 @@ function processPlayerPositions() {
pos: player.pos,
yaw: player.yaw
};
lastKnownPositions["YOU"] = player.pos;
} else {
playerList[player.playerName] = {
pos: player.pos,
yaw: player.yaw
};
lastKnownPositions[player.playerName] = player.pos;
}
if (player.playerName === focusedPlayerName ||
(focusedPlayerName === "YOU" && player.playerType === "Local")) {
focusedPlayerPos = player.pos;
focusedPlayerYaw = player.yaw;
if (rotationDisabledUntilRespawn) {
console.log("[radarflow] Player respawned, re-enabling rotation");
rotationDisabledUntilRespawn = false;
}
}
}
});
if (focusedPlayerPos === null) {
if (oldPlayerList[focusedPlayerName] && oldPlayerList[focusedPlayerName].pos) {
console.log("[radarflow] Focused player disappeared, disabling rotation until respawn");
rotationDisabledUntilRespawn = true;
}
}
console.log(`[radarflow] Focused player: ${focusedPlayerName}, Position: ${focusedPlayerPos ? 'Found' : 'Not found'}, Rotation disabled: ${temporarilyDisableRotation || rotationDisabledUntilRespawn}`);
}
function drawImage() {
@ -161,7 +218,12 @@ function drawImage() {
ctx.save();
if (rotateMap && focusedPlayerPos) {
const shouldRotate = rotateMap &&
focusedPlayerPos &&
!temporarilyDisableRotation &&
!rotationDisabledUntilRespawn;
if (shouldRotate) {
ctx.translate(canvas.width / 2, canvas.height / 2);
ctx.rotate(degreesToRadians(focusedPlayerYaw + 270));
ctx.translate(-canvas.width / 2, -canvas.height / 2);
@ -352,9 +414,24 @@ function updatePlayerDropdown() {
function changePlayerFocus() {
const dropdown = document.getElementById('playerSelect');
focusedPlayerName = dropdown.value === "local" ? "YOU" : dropdown.value;
rotationDisabledUntilRespawn = false;
update = true;
}
function addRotationHelpText() {
const settingsHolder = document.querySelector('#settingsHolder .settings');
if (!settingsHolder) return;
const helpText = document.createElement('div');
helpText.className = 'help-text';
helpText.style.marginTop = '10px';
helpText.style.fontSize = '12px';
helpText.style.color = '#aaa';
helpText.innerHTML = 'Press <strong>R</strong> key to toggle rotation temporarily';
settingsHolder.appendChild(helpText);
}
function mapCoordinates(coordinates) {
if (!map || !coordinates) {
return { x: 0, y: 0 };
@ -415,7 +492,12 @@ function mapAndTransformCoordinates(pos) {
mapPos.y = mapPos.y * canvasHeight / imageHeight;
}
if (rotateMap && typeof focusedPlayerYaw === 'number') {
const shouldRotate = rotateMap &&
typeof focusedPlayerYaw === 'number' &&
!temporarilyDisableRotation &&
!rotationDisabledUntilRespawn;
if (shouldRotate) {
const canvasCenter = { x: canvasWidth / 2, y: canvasHeight / 2 };
const rotationYaw = focusedPlayerName === "YOU" ? localYaw : focusedPlayerYaw;
const angle = rotationYaw + 270;
@ -595,7 +677,12 @@ function drawEntity(pos, fillStyle, dormant, hasBomb, yaw, hasAwp, playerType, i
(focusedPlayerName === "YOU" && playerType === "Local");
let adjustedYaw = yaw;
if (rotateMap) {
const shouldAdjustRotation = rotateMap &&
!temporarilyDisableRotation &&
!rotationDisabledUntilRespawn;
if (shouldAdjustRotation) {
if (isFocusedPlayer) {
adjustedYaw = 90;
} else {
@ -753,6 +840,8 @@ function processData(data) {
function decompressData(data) {
try {
pingTracker.endRequest();
if (data[0] === 0x01) {
try {
if (typeof pako === 'undefined') {
@ -930,4 +1019,5 @@ addEventListener("DOMContentLoaded", () => {
} else {
console.error("[radarflow] Canvas element not found");
}
addRotationHelpText();
});