Files
radarflow2-kvm/webradar/script.js

947 lines
28 KiB
JavaScript
Raw Normal View History

2023-10-31 17:23:15 +01:00
// Colors
const localColor = "#109856"
const teamColor = "#68a3e5"
const enemyColor = "#ec040b"
const bombColor = "#eda338"
const textColor = "#d1d1d1"
// Settings
shouldZoom = false
2025-03-15 17:15:28 -04:00
rotateMap = true
playerCentered = true
2025-03-10 11:31:55 -04:00
drawStats = true
2025-03-10 12:43:22 -04:00
drawNames = true
2025-03-10 13:09:01 -04:00
drawGuns = true
2025-03-15 18:10:42 -04:00
drawMoney = true
let focusedPlayerYaw = 0;
2023-10-31 17:23:15 +01:00
// Common
canvas = null
ctx = null
2025-03-15 18:10:42 -04:00
let focusedPlayerName = "YOU"
let focusedPlayerPos = null
let playerList = {}
2023-10-31 17:23:15 +01:00
// radarflow specific
radarData = null
freq = 0
2023-10-31 17:23:15 +01:00
image = null
map = null
mapName = null
loaded = false
entityData = null
update = false
2025-03-15 17:15:28 -04:00
localYaw = 0
localPlayerPos = null
2023-10-31 17:23:15 +01:00
/// Radarflow zoom in
zoomSet = false
safetyBound = 50
boundingRect = null
2025-03-10 13:09:01 -04:00
// Weapon IDs
const weaponIdMap = {
1: "DEAGLE",
2: "DUALIES",
3: "FIVE-SEVEN",
4: "GLOCK",
7: "AK-47",
8: "AUG",
9: "AWP",
10: "FAMAS",
11: "G3SG1",
13: "GALIL",
14: "M249",
16: "M4A4",
17: "MAC-10",
19: "P90",
23: "MP5",
24: "UMP",
25: "XM1014",
26: "BIZON",
27: "MAG-7",
28: "NEGEV",
29: "SAWED-OFF",
30: "TEC-9",
31: "ZEUS",
32: "P2000",
33: "MP7",
34: "MP9",
35: "NOVA",
36: "P250",
38: "SCAR-20",
39: "SG 553",
40: "SCOUT",
60: "M4A1-S",
61: "USP-S",
63: "CZ75",
64: "REVOLVER",
43: "FLASH",
44: "HE",
45: "SMOKE",
46: "MOLOTOV",
47: "DECOY",
48: "INCENDIARY",
49: "C4",
0: "KNIFE"
};
2025-03-10 14:31:24 -04:00
2023-10-31 17:23:15 +01:00
// networking
websocket = null
if (location.protocol == 'https:') {
websocketAddr = `wss://${window.location.host}/ws`
} else {
websocketAddr = `ws://${window.location.host}/ws`
}
//websocketAddr = "ws://192.168.0.235:8000/ws"
2023-10-31 17:23:15 +01:00
// Util functions
const clamp = (num, min, max) => Math.min(Math.max(num, min), max);
2025-03-10 12:33:09 -04:00
const degreesToRadians = (degrees) => degrees * (Math.PI / 180);
2025-03-15 17:15:28 -04:00
function mapCoordinates(coordinates) {
let offset_x = coordinates.x - map.pos_x;
let offset_y = coordinates.y - map.pos_y;
offset_x /= map.scale;
offset_y /= -map.scale;
return { x: offset_x, y: offset_y };
}
function boundingCoordinates(coordinates, boundingRect) {
const xScale = boundingRect.width / image.width;
const yScale = boundingRect.height / image.height;
const newX = (coordinates.x - boundingRect.x) / xScale;
const newY = (coordinates.y - boundingRect.y) / yScale;
return { x: newX, y: newY };
}
function boundingScale(value, boundingRect) {
const scale = image.width / boundingRect.width;
return value * scale;
}
function rotatePoint(cx, cy, x, y, angle) {
const radians = degreesToRadians(angle);
const cos = Math.cos(radians);
const sin = Math.sin(radians);
const nx = x - cx;
const ny = y - cy;
const rx = nx * cos - ny * sin;
const ry = nx * sin + ny * cos;
return {
x: rx + cx,
y: ry + cy
};
}
2023-10-31 17:23:15 +01:00
function makeBoundingRect(x1, y1, x2, y2, aspectRatio) {
const topLeftX = x1;
const topLeftY = y1;
const bottomRightX = x2;
const bottomRightY = y2;
const width = bottomRightX - topLeftX;
const height = bottomRightY - topLeftY;
let newWidth, newHeight;
if (width / height > aspectRatio) {
// Wider rectangle
newHeight = width / aspectRatio;
newWidth = width;
} else {
// Taller rectangle
newWidth = height * aspectRatio;
newHeight = height;
}
const centerX = (topLeftX + bottomRightX) / 2;
const centerY = (topLeftY + bottomRightY) / 2;
const rectMinX = centerX - newWidth / 2;
const rectMaxX = centerX + newWidth / 2;
const rectMinY = centerY - newHeight / 2;
const rectMaxY = centerY + newHeight / 2;
2025-03-15 17:15:28 -04:00
return {
2023-10-31 17:23:15 +01:00
x: rectMinX,
y: rectMinY,
width: rectMaxX - rectMinX,
height: rectMaxY - rectMinY,
2025-03-15 17:15:28 -04:00
};
2025-03-10 13:09:01 -04:00
}
2023-10-31 17:23:15 +01:00
function render() {
if (update) {
2025-03-15 17:15:28 -04:00
fillCanvas();
2023-10-31 17:23:15 +01:00
if (loaded) {
2025-03-15 17:15:28 -04:00
update = false;
2023-10-31 17:23:15 +01:00
2025-03-15 17:15:28 -04:00
localPlayerPos = null;
2025-03-15 18:10:42 -04:00
focusedPlayerPos = null;
focusedPlayerYaw = 0;
2025-03-15 17:15:28 -04:00
if (entityData != null) {
2025-03-15 18:10:42 -04:00
playerList = {};
2025-03-15 17:15:28 -04:00
entityData.forEach((data) => {
2025-03-15 18:10:42 -04:00
if (data.Player !== undefined) {
if (data.Player.playerType === "Local") {
localYaw = data.Player.yaw;
localPlayerPos = data.Player.pos;
playerList["YOU"] = {
pos: data.Player.pos,
yaw: data.Player.yaw
};
} else {
playerList[data.Player.playerName] = {
pos: data.Player.pos,
yaw: data.Player.yaw
};
}
if (data.Player.playerName === focusedPlayerName ||
(focusedPlayerName === "YOU" && data.Player.playerType === "Local")) {
focusedPlayerPos = data.Player.pos;
focusedPlayerYaw = data.Player.yaw;
}
2025-03-15 17:15:28 -04:00
}
});
2025-03-15 18:10:42 -04:00
updatePlayerDropdown();
2025-03-15 17:15:28 -04:00
}
2023-10-31 17:23:15 +01:00
2025-03-15 17:15:28 -04:00
if (entityData != null && map != null && image != null && shouldZoom && !playerCentered) {
let minX = Infinity;
let minY = Infinity;
let maxX = -Infinity;
let maxY = -Infinity;
2023-10-31 17:23:15 +01:00
entityData.forEach((data) => {
2025-03-15 17:15:28 -04:00
let mapCords = null;
2023-10-31 17:23:15 +01:00
if (data.Bomb !== undefined) {
2025-03-15 17:15:28 -04:00
mapCords = mapCoordinates(data.Bomb.pos);
2023-10-31 17:23:15 +01:00
} else {
2025-03-15 17:15:28 -04:00
mapCords = mapCoordinates(data.Player.pos);
2023-10-31 17:23:15 +01:00
}
2025-03-10 12:33:09 -04:00
2023-10-31 17:23:15 +01:00
minX = Math.min(minX, mapCords.x);
minY = Math.min(minY, mapCords.y);
maxX = Math.max(maxX, mapCords.x);
maxY = Math.max(maxY, mapCords.y);
});
2025-03-15 17:15:28 -04:00
boundingRect = makeBoundingRect(minX - safetyBound, minY - safetyBound, maxX + safetyBound, maxY + safetyBound, image.width / image.height);
zoomSet = true;
} else if (zoomSet && !playerCentered) {
zoomSet = false;
2023-10-31 17:23:15 +01:00
}
2025-03-15 17:15:28 -04:00
drawImage();
2023-10-31 17:23:15 +01:00
if (entityData != null) {
entityData.forEach((data) => {
if (data.Bomb !== undefined) {
2025-03-15 17:15:28 -04:00
drawBomb(data.Bomb.pos, data.Bomb.isPlanted);
2023-10-31 17:23:15 +01:00
} else {
2025-03-15 17:15:28 -04:00
let fillStyle = localColor;
2023-10-31 17:23:15 +01:00
switch (data.Player.playerType) {
case "Team":
2025-03-15 17:15:28 -04:00
fillStyle = teamColor;
2023-10-31 17:23:15 +01:00
break;
case "Enemy":
2025-03-15 17:15:28 -04:00
fillStyle = enemyColor;
2023-10-31 17:23:15 +01:00
break;
}
drawEntity(
data.Player.pos,
2025-03-10 12:33:09 -04:00
fillStyle,
2023-10-31 17:23:15 +01:00
data.Player.isDormant,
data.Player.hasBomb,
data.Player.yaw,
data.Player.hasAwp,
data.Player.playerType,
2025-03-15 17:15:28 -04:00
data.Player.isScoped,
data.Player.playerName,
false,
data.Player.weaponId
2025-03-10 12:33:09 -04:00
);
2025-03-10 13:09:01 -04:00
if (drawNames && !data.Player.isDormant) {
drawPlayerName(
data.Player.pos,
data.Player.playerName,
data.Player.playerType,
data.Player.hasAwp,
2025-03-10 14:31:24 -04:00
data.Player.hasBomb,
data.Player.isScoped
2025-03-10 13:09:01 -04:00
);
}
if (drawGuns && !data.Player.isDormant) {
drawPlayerWeapon(
data.Player.pos,
data.Player.playerType,
data.Player.weaponId
);
}
2025-03-10 13:29:01 -04:00
if (data.Player.hasBomb && !data.Player.isDormant) {
drawPlayerBomb(
data.Player.pos,
data.Player.playerType
);
}
2025-03-15 17:50:46 -04:00
if (drawMoney && !data.Player.isDormant && typeof data.Player.money === 'number') {
drawPlayerMoney(
data.Player.pos,
data.Player.playerType,
data.Player.money,
data.Player.hasBomb
);
}
2023-10-31 17:23:15 +01:00
}
});
}
if (radarData != null) {
if (radarData.bombPlanted && !radarData.bombExploded && radarData.bombDefuseTimeleft >= 0) {
2025-03-10 12:33:09 -04:00
let maxWidth = 1024 - 128 - 128;
let timeleft = radarData.bombDefuseTimeleft;
2025-03-10 12:33:09 -04:00
// Base bar
2025-03-15 17:15:28 -04:00
ctx.fillStyle = "black";
ctx.fillRect(128, 16, maxWidth, 16);
2025-03-10 12:33:09 -04:00
// Bomb timer
if (radarData.bombBeingDefused) {
if (radarData.bombCanDefuse) {
2025-03-15 17:15:28 -04:00
ctx.fillStyle = teamColor;
} else {
2025-03-15 17:15:28 -04:00
ctx.fillStyle = enemyColor;
}
} else {
2025-03-15 17:15:28 -04:00
ctx.fillStyle = bombColor;
}
2025-03-15 17:15:28 -04:00
ctx.fillRect(130, 18, (maxWidth - 2) * (timeleft / 40), 12);
ctx.font = "24px Arial";
2025-03-15 17:15:28 -04:00
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = textColor;
2025-03-10 12:33:09 -04:00
ctx.fillText(`${timeleft.toFixed(1)}s`, 1024 / 2, 28 + 24);
// Defuse time lines
2025-03-15 17:15:28 -04:00
ctx.strokeStyle = "black";
ctx.lineWidth = 2;
// Kit defuse
2025-03-15 17:15:28 -04:00
ctx.beginPath();
ctx.moveTo(128 + (maxWidth * (5 / 40)), 16);
ctx.lineTo(128 + (maxWidth * (5 / 40)), 32);
ctx.stroke();
// Normal defuse
2025-03-15 17:15:28 -04:00
ctx.beginPath();
ctx.moveTo(130 + (maxWidth - 2) * (10 / 40), 16);
ctx.lineTo(130 + (maxWidth - 2) * (10 / 40), 32);
ctx.stroke();
2024-04-10 22:19:39 +02:00
// Defuse stamp line
2024-04-10 22:35:40 +02:00
if (radarData.bombCanDefuse) {
2025-03-15 17:15:28 -04:00
ctx.strokeStyle = "green";
ctx.beginPath();
ctx.moveTo(130 + (maxWidth - 2) * (radarData.bombDefuseEnd / 40), 16);
ctx.lineTo(130 + (maxWidth - 2) * (radarData.bombDefuseEnd / 40), 32);
ctx.stroke();
2024-04-10 22:19:39 +02:00
}
}
}
2023-10-31 17:23:15 +01:00
} else {
if (websocket != null) {
ctx.font = "100px Arial";
2025-03-15 17:15:28 -04:00
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = textColor;
2025-03-10 12:33:09 -04:00
ctx.fillText("Not on a server", 1024 / 2, 1024 / 2);
2023-10-31 17:23:15 +01:00
} else {
ctx.font = "100px Arial";
2025-03-15 17:15:28 -04:00
ctx.textAlign = "center";
ctx.fillStyle = textColor;
2025-03-10 12:33:09 -04:00
ctx.fillText("Disconnected", 1024 / 2, 1024 / 2);
2023-10-31 17:23:15 +01:00
}
}
if (drawStats) {
ctx.font = "16px Arial";
2025-03-15 17:15:28 -04:00
ctx.textAlign = "left";
ctx.fillStyle = textColor;
ctx.lineWidth = 2;
2025-03-15 17:15:28 -04:00
ctx.strokeStyle = "black";
ctx.strokeText(`${freq} Hz`, 2, 18);
ctx.fillText(`${freq} Hz`, 2, 18);
}
2023-10-31 17:23:15 +01:00
}
if (websocket != null) {
websocket.send("requestInfo");
}
}
function fillCanvas() {
ctx.fillStyle = "#0f0f0f";
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
function drawImage() {
if (image == null || canvas == null)
return
2025-03-15 17:15:28 -04:00
ctx.save();
2025-03-15 18:10:42 -04:00
if (rotateMap && focusedPlayerPos) {
2025-03-15 17:15:28 -04:00
ctx.translate(canvas.width / 2, canvas.height / 2);
2025-03-15 18:10:42 -04:00
ctx.rotate(degreesToRadians(focusedPlayerYaw + 270));
2025-03-15 17:15:28 -04:00
ctx.translate(-canvas.width / 2, -canvas.height / 2);
}
2025-03-15 18:10:42 -04:00
if (playerCentered && focusedPlayerPos) {
const playerMapPos = mapCoordinates(focusedPlayerPos);
2025-03-15 17:15:28 -04:00
const zoomLevel = 0.5;
const viewWidth = image.width * zoomLevel;
const viewHeight = image.height * zoomLevel;
const viewX = playerMapPos.x - (viewWidth / 2);
const viewY = playerMapPos.y - (viewHeight / 2);
ctx.drawImage(
image,
viewX, viewY, viewWidth, viewHeight,
0, 0, canvas.width, canvas.height
);
} else if (zoomSet != false && boundingRect.x != null) {
2023-10-31 17:23:15 +01:00
ctx.drawImage(image, boundingRect.x, boundingRect.y, boundingRect.width, boundingRect.height, 0, 0, canvas.width, canvas.height)
} else {
ctx.drawImage(image, 0, 0, image.width, image.height, 0, 0, canvas.width, canvas.height)
}
2025-03-15 17:15:28 -04:00
ctx.restore();
}
2025-03-15 18:10:42 -04:00
function updatePlayerDropdown() {
const dropdown = document.getElementById('playerSelect');
const currentValue = dropdown.value;
while (dropdown.options.length > 1) {
dropdown.remove(1);
}
for (const playerName in playerList) {
if (playerName !== "YOU") {
const option = document.createElement('option');
option.value = playerName;
option.textContent = playerName;
dropdown.appendChild(option);
}
}
if (Object.keys(playerList).includes(currentValue)) {
dropdown.value = currentValue;
} else {
dropdown.value = "local";
focusedPlayerName = "YOU";
if (playerList["YOU"]) {
focusedPlayerPos = playerList["YOU"].pos;
focusedPlayerYaw = playerList["YOU"].yaw;
}
}
}
function changePlayerFocus() {
const dropdown = document.getElementById('playerSelect');
if (dropdown.value === "local") {
focusedPlayerName = "YOU";
} else {
focusedPlayerName = dropdown.value;
}
console.log(`[radarflow] Changed focus to player: ${focusedPlayerName}`);
update = true;
}
2025-03-15 17:15:28 -04:00
2025-03-15 18:10:42 -04:00
function mapAndTransformCoordinates(pos) {
2025-03-15 17:15:28 -04:00
let mapPos = mapCoordinates(pos);
2025-03-15 18:10:42 -04:00
let textSize = 12;
2025-03-15 17:15:28 -04:00
if (zoomSet) {
mapPos = boundingCoordinates(mapPos, boundingRect);
textSize = boundingScale(12, boundingRect);
2025-03-15 18:10:42 -04:00
} else if (playerCentered && focusedPlayerPos) {
const playerMapPos = mapCoordinates(focusedPlayerPos);
2025-03-15 17:15:28 -04:00
const zoomLevel = 0.5;
const viewWidth = image.width * zoomLevel;
const viewHeight = image.height * zoomLevel;
mapPos.x = (mapPos.x - (playerMapPos.x - viewWidth / 2)) * canvas.width / viewWidth;
mapPos.y = (mapPos.y - (playerMapPos.y - viewHeight / 2)) * canvas.height / viewHeight;
} else {
mapPos.x = mapPos.x * canvas.width / image.width;
mapPos.y = mapPos.y * canvas.height / image.height;
}
if (rotateMap) {
const canvasCenter = { x: canvas.width / 2, y: canvas.height / 2 };
2025-03-15 18:10:42 -04:00
const rotationYaw = focusedPlayerName === "YOU" ? localYaw : focusedPlayerYaw;
mapPos = rotatePoint(canvasCenter.x, canvasCenter.y, mapPos.x, mapPos.y, rotationYaw + 270);
2025-03-15 17:15:28 -04:00
}
2025-03-15 18:10:42 -04:00
return { pos: mapPos, textSize: textSize };
}
function drawPlayerName(pos, playerName, playerType, hasAwp, hasBomb, isScoped) {
if (!map) return;
const transformed = mapAndTransformCoordinates(pos);
const mapPos = transformed.pos;
const textSize = transformed.textSize;
2025-03-15 17:15:28 -04:00
const textY = mapPos.y + 20;
let displayName = playerName;
if (playerType === "Local") {
displayName = "YOU";
ctx.fillStyle = localColor;
} else if (playerType === "Team") {
ctx.fillStyle = teamColor;
} else if (playerType === "Enemy") {
ctx.fillStyle = enemyColor;
}
if (isScoped) {
displayName += " [SCOPED]";
}
ctx.font = `${textSize}px Arial`;
ctx.textAlign = "center";
ctx.textBaseline = "top";
ctx.lineWidth = 2;
ctx.strokeStyle = "black";
ctx.strokeText(displayName, mapPos.x, textY);
ctx.fillText(displayName, mapPos.x, textY);
}
2025-03-15 17:50:46 -04:00
function drawPlayerMoney(pos, playerType, money, hasBomb) {
if (!map) return;
2025-03-15 18:10:42 -04:00
const transformed = mapAndTransformCoordinates(pos);
const mapPos = transformed.pos;
const textSize = transformed.textSize * 0.8;
2025-03-15 17:50:46 -04:00
let extraOffset = 0;
if (drawNames) extraOffset += 15;
if (drawGuns) extraOffset += 15;
if (hasBomb) extraOffset += 15;
let textY = mapPos.y + 20 + extraOffset;
const formattedMoney = '$' + (money || 0).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
if (money >= 10000) {
ctx.fillStyle = "#32CD32";
} else if (money >= 4500) {
ctx.fillStyle = "#FFFF00";
} else {
ctx.fillStyle = "#FF4500";
}
ctx.font = `${textSize}px Arial`;
ctx.textAlign = "center";
ctx.textBaseline = "top";
ctx.lineWidth = 2;
ctx.strokeStyle = "black";
ctx.strokeText(formattedMoney, mapPos.x, textY);
ctx.fillText(formattedMoney, mapPos.x, textY);
}
2025-03-15 17:15:28 -04:00
function drawPlayerWeapon(pos, playerType, weaponId) {
if (!map) return;
2025-03-15 18:10:42 -04:00
const transformed = mapAndTransformCoordinates(pos);
const mapPos = transformed.pos;
const textSize = transformed.textSize * 0.8;
2025-03-15 17:15:28 -04:00
const textY = mapPos.y + (drawNames ? 35 : 20);
let weaponName = getWeaponName(weaponId);
if (weaponId === 9) {
ctx.fillStyle = "orange";
} else {
ctx.fillStyle = textColor;
}
ctx.font = `${textSize}px Arial`;
ctx.textAlign = "center";
ctx.textBaseline = "top";
ctx.lineWidth = 2;
ctx.strokeStyle = "black";
ctx.strokeText(`[${weaponName}]`, mapPos.x, textY);
ctx.fillText(`[${weaponName}]`, mapPos.x, textY);
}
function drawPlayerBomb(pos, playerType) {
if (!map) return;
2025-03-15 18:10:42 -04:00
const transformed = mapAndTransformCoordinates(pos);
const mapPos = transformed.pos;
const textSize = transformed.textSize * 0.8;
2025-03-15 17:15:28 -04:00
const textY = mapPos.y + (drawNames ? (drawGuns ? 50 : 35) : (drawGuns ? 35 : 20));
ctx.fillStyle = bombColor;
ctx.font = `${textSize}px Arial`;
ctx.textAlign = "center";
ctx.textBaseline = "top";
ctx.lineWidth = 2;
ctx.strokeStyle = "black";
ctx.strokeText("[C4]", mapPos.x, textY);
ctx.fillText("[C4]", mapPos.x, textY);
2023-10-31 17:23:15 +01:00
}
function drawBomb(pos, planted) {
if (map == null)
return
2025-03-15 18:10:42 -04:00
const transformed = mapAndTransformCoordinates(pos);
const mapPos = transformed.pos;
const size = transformed.textSize * 0.7;
2023-10-31 17:23:15 +01:00
ctx.beginPath();
2025-03-15 17:15:28 -04:00
ctx.arc(mapPos.x, mapPos.y, size, 0, 2 * Math.PI);
2023-10-31 17:23:15 +01:00
ctx.fillStyle = bombColor;
ctx.fill();
2025-03-10 13:29:01 -04:00
ctx.lineWidth = 2;
ctx.strokeStyle = "black";
ctx.stroke();
ctx.font = size * 1.2 + "px Arial";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = "white";
2025-03-15 17:15:28 -04:00
ctx.fillText("C4", mapPos.x, mapPos.y);
2025-03-10 13:29:01 -04:00
2023-10-31 17:23:15 +01:00
ctx.closePath();
if (planted && ((new Date().getTime() / 1000) % 1) > 0.5) {
2025-03-15 17:15:28 -04:00
ctx.strokeStyle = enemyColor;
2025-03-10 13:29:01 -04:00
ctx.lineWidth = 3;
ctx.beginPath();
2025-03-15 17:15:28 -04:00
ctx.arc(mapPos.x, mapPos.y, size + 4, 0, 2 * Math.PI);
2025-03-10 13:29:01 -04:00
ctx.stroke();
2023-10-31 17:23:15 +01:00
}
}
2025-03-15 17:15:28 -04:00
function drawEntity(pos, fillStyle, dormant, hasBomb, yaw, hasAwp, playerType, isScoped, playerName, isPlanted, weaponId) {
2023-10-31 17:23:15 +01:00
if (map == null)
return
2025-03-15 18:10:42 -04:00
const transformed = mapAndTransformCoordinates(pos);
const mapPos = transformed.pos;
const circleRadius = transformed.textSize * 0.6;
const distance = circleRadius + 2;
const radius = distance + 5;
const arrowWidth = 35;
2025-03-15 17:15:28 -04:00
2025-03-15 18:10:42 -04:00
const isFocusedPlayer = playerName === focusedPlayerName ||
(focusedPlayerName === "YOU" && playerType === "Local");
2023-10-31 17:23:15 +01:00
2025-03-15 17:15:28 -04:00
let adjustedYaw = yaw;
if (rotateMap) {
2025-03-15 18:10:42 -04:00
if (isFocusedPlayer) {
2025-03-15 17:15:28 -04:00
adjustedYaw = 90;
} else {
2025-03-15 18:10:42 -04:00
adjustedYaw = (yaw + 180) - focusedPlayerYaw + 270;
2025-03-15 17:15:28 -04:00
}
}
2023-10-31 17:23:15 +01:00
if (dormant) {
ctx.font = "20px Arial";
2025-03-15 17:15:28 -04:00
ctx.textAlign = "center";
ctx.fillStyle = fillStyle;
ctx.fillText("?", mapPos.x, mapPos.y);
2023-10-31 17:23:15 +01:00
} else {
2025-03-15 18:10:42 -04:00
if (isFocusedPlayer) {
ctx.beginPath();
ctx.arc(mapPos.x, mapPos.y, circleRadius + 4, 0, 2 * Math.PI);
ctx.fillStyle = "#FFFFFF";
ctx.fill();
ctx.closePath();
}
if (hasAwp) {
ctx.beginPath();
2025-03-15 17:15:28 -04:00
ctx.arc(mapPos.x, mapPos.y, circleRadius, 0, 2 * Math.PI);
ctx.fillStyle = "orange";
ctx.fill();
circleRadius -= 2;
}
2023-10-31 17:23:15 +01:00
// Draw circle
ctx.beginPath();
2025-03-15 17:15:28 -04:00
ctx.arc(mapPos.x, mapPos.y, circleRadius, 0, 2 * Math.PI);
2023-10-31 17:23:15 +01:00
ctx.fillStyle = fillStyle;
ctx.fill();
if (hasAwp && false) {
2025-03-15 17:15:28 -04:00
let style = "yellow";
if (playerType == "Enemy") {
2025-03-15 17:15:28 -04:00
style = "orange";
}
ctx.beginPath();
2025-03-15 17:15:28 -04:00
ctx.arc(mapPos.x, mapPos.y, circleRadius / 1.5, 0, 2 * Math.PI);
ctx.fillStyle = style;
ctx.fill();
}
2023-10-31 17:23:15 +01:00
ctx.closePath();
2025-03-15 17:15:28 -04:00
const arrowHeadX = mapPos.x + radius * Math.cos(adjustedYaw * (Math.PI / 180));
const arrowHeadY = mapPos.y - radius * Math.sin(adjustedYaw * (Math.PI / 180));
2023-10-31 17:23:15 +01:00
2025-03-15 17:15:28 -04:00
const arrowCornerX1 = mapPos.x + distance * Math.cos((adjustedYaw - arrowWidth) * (Math.PI / 180));
const arrowCornerY1 = mapPos.y - distance * Math.sin((adjustedYaw - arrowWidth) * (Math.PI / 180));
2023-10-31 17:23:15 +01:00
2025-03-15 17:15:28 -04:00
const arrowCornerX2 = mapPos.x + distance * Math.cos((adjustedYaw + arrowWidth) * (Math.PI / 180));
const arrowCornerY2 = mapPos.y - distance * Math.sin((adjustedYaw + arrowWidth) * (Math.PI / 180));
2023-10-31 17:23:15 +01:00
2025-03-15 17:15:28 -04:00
const cicleYaw = 90 - adjustedYaw;
const startAngle = degreesToRadians(cicleYaw - arrowWidth) - Math.PI / 2;
const endAngle = degreesToRadians(cicleYaw + arrowWidth) - Math.PI / 2;
2023-10-31 17:23:15 +01:00
// Draw arrow
ctx.beginPath();
2025-03-15 17:15:28 -04:00
ctx.arc(mapPos.x, mapPos.y, distance, startAngle, endAngle);
2023-10-31 17:23:15 +01:00
ctx.lineTo(arrowCornerX1, arrowCornerY1);
ctx.lineTo(arrowHeadX, arrowHeadY);
ctx.lineTo(arrowCornerX2, arrowCornerY2);
2025-03-15 17:15:28 -04:00
ctx.closePath();
ctx.fillStyle = 'white';
2023-10-31 17:23:15 +01:00
ctx.fill();
if (isScoped) {
2025-03-15 17:15:28 -04:00
const lineOfSightX = arrowHeadX + 1024 * Math.cos(adjustedYaw * (Math.PI / 180));
const lineOfSightY = arrowHeadY - 1024 * Math.sin(adjustedYaw * (Math.PI / 180));
ctx.beginPath();
ctx.moveTo(arrowHeadX, arrowHeadY);
ctx.lineTo(lineOfSightX, lineOfSightY);
if (playerType == "Enemy")
2025-03-15 17:15:28 -04:00
ctx.strokeStyle = enemyColor;
else
2025-03-15 17:15:28 -04:00
ctx.strokeStyle = teamColor;
2025-03-15 17:15:28 -04:00
ctx.lineWidth = 1;
ctx.stroke();
}
2023-10-31 17:23:15 +01:00
}
}
2025-03-15 17:15:28 -04:00
function getWeaponName(weaponId) {
if (weaponIdMap[weaponId]) {
return weaponIdMap[weaponId];
}
if (weaponId >= 500) {
return "KNIFE";
}
return "KNIFE";
}
2023-10-31 17:23:15 +01:00
function loadMap(mapName) {
2025-03-15 17:15:28 -04:00
console.log(`[radarflow] loading map ${mapName}`);
2023-10-31 17:23:15 +01:00
loaded = true;
const map_img = new Image();
map_img.src = `assets/image/${mapName}_radar_psd.png`;
fetch(`assets/json/${mapName}.json`)
.then(response => response.json())
.then(data => {
map = data;
})
.catch(error => {
console.error('Error loading JSON file:', error);
});
map_img.onload = () => {
image = map_img;
update = true;
};
}
function unloadMap() {
2025-03-15 17:15:28 -04:00
console.log("[radarflow] unloading map");
ctx.clearRect(0, 0, canvas.width, canvas.height);
map = null;
mapName = null;
2023-10-31 17:23:15 +01:00
loaded = false,
2025-03-15 17:15:28 -04:00
update = true;
2023-10-31 17:23:15 +01:00
requestAnimationFrame(render);
}
function connect() {
if (websocket == null) {
2025-03-15 17:15:28 -04:00
let socket = new WebSocket(websocketAddr);
2023-10-31 17:23:15 +01:00
socket.onopen = () => {
2025-03-15 17:15:28 -04:00
console.log("[radarflow] Connection established");
2023-10-31 17:23:15 +01:00
websocket.send("requestInfo");
};
2025-03-10 12:33:09 -04:00
2023-10-31 17:23:15 +01:00
socket.onmessage = (event) => {
if (event.data == "error") {
2025-03-15 17:15:28 -04:00
console.log("[radarflow] Server had an unknown error");
2023-10-31 17:23:15 +01:00
} else {
2025-03-14 17:29:12 -04:00
try {
let data = JSON.parse(event.data);
radarData = data;
freq = data.freq;
if (data.money_reveal_enabled !== undefined) {
document.getElementById("moneyReveal").checked = data.money_reveal_enabled;
}
if (data.ingame == false) {
2025-03-15 17:15:28 -04:00
mapName = null;
entityData = null;
2025-03-14 17:29:12 -04:00
if (loaded)
2025-03-15 17:15:28 -04:00
unloadMap();
2023-10-31 17:23:15 +01:00
} else {
2025-03-14 17:29:12 -04:00
if (!loaded) {
2025-03-15 17:15:28 -04:00
mapName = data.mapName;
entityData = data.entityData;
loadMap(mapName);
2025-03-14 17:29:12 -04:00
} else {
2025-03-15 17:15:28 -04:00
entityData = data.entityData;
2025-03-14 17:29:12 -04:00
}
2023-10-31 17:23:15 +01:00
}
2025-03-15 17:15:28 -04:00
update = true;
2025-03-14 17:29:12 -04:00
requestAnimationFrame(render);
} catch (e) {
console.error("[radarflow] Error parsing server message:", e, event.data);
}
2023-10-31 17:23:15 +01:00
}
};
2025-03-10 12:33:09 -04:00
2023-10-31 17:23:15 +01:00
socket.onclose = (event) => {
if (event.wasClean) {
console.log("[radarflow] connection closed");
} else {
console.log("[radarflow] connection died");
}
2025-03-15 17:15:28 -04:00
playerData = null;
websocket = null;
unloadMap();
2023-10-31 17:23:15 +01:00
2025-03-10 12:33:09 -04:00
setTimeout(function () {
2023-10-31 17:23:15 +01:00
connect();
}, 1000);
};
2025-03-10 12:33:09 -04:00
2023-10-31 17:23:15 +01:00
socket.onerror = (error) => {
console.log(`[radarflow] websocket error: ${error}`);
};
websocket = socket;
} else {
setTimeout(() => {
connect();
}, 1000);
}
}
addEventListener("DOMContentLoaded", (e) => {
2025-03-15 18:10:42 -04:00
const savedDrawMoney = localStorage.getItem('drawMoney');
drawMoney = savedDrawMoney !== null ? savedDrawMoney === 'true' : true;
2025-03-10 11:31:55 -04:00
document.getElementById("zoomCheck").checked = false;
document.getElementById("statsCheck").checked = true;
2025-03-10 12:43:22 -04:00
document.getElementById("namesCheck").checked = true;
2025-03-10 13:09:01 -04:00
document.getElementById("gunsCheck").checked = true;
2025-03-15 18:10:42 -04:00
document.getElementById("moneyDisplay").checked = drawMoney;
2025-03-14 17:29:12 -04:00
document.getElementById("moneyReveal").checked = false;
2025-03-15 17:15:28 -04:00
document.getElementById("rotateCheck").checked = true;
document.getElementById("centerCheck").checked = true;
2023-10-31 17:23:15 +01:00
canvas = document.getElementById('canvas');
canvas.width = 1024;
canvas.height = 1024;
2025-03-15 17:15:28 -04:00
canvasAspectRatio = canvas.width / canvas.height;
2023-10-31 17:23:15 +01:00
ctx = canvas.getContext('2d');
2025-03-15 17:15:28 -04:00
console.log(`[radarflow] connecting to ${websocketAddr}`);
connect();
2023-10-31 17:23:15 +01:00
});
function toggleZoom() {
2025-03-15 17:15:28 -04:00
shouldZoom = !shouldZoom;
}
function toggleStats() {
2025-03-15 17:15:28 -04:00
drawStats = !drawStats;
2025-03-10 12:43:22 -04:00
}
function toggleNames() {
2025-03-15 17:15:28 -04:00
drawNames = !drawNames;
2025-03-10 13:09:01 -04:00
}
function toggleGuns() {
2025-03-15 17:15:28 -04:00
drawGuns = !drawGuns;
}
function toggleRotate() {
rotateMap = !rotateMap;
}
function toggleCentered() {
playerCentered = !playerCentered;
2025-03-14 17:29:12 -04:00
}
function toggleMoneyReveal() {
if (websocket && websocket.readyState === WebSocket.OPEN) {
websocket.send("toggleMoneyReveal");
}
2025-03-15 17:50:46 -04:00
}
function toggleDisplayMoney() {
drawMoney = !drawMoney;
update = true;
console.log("[radarflow] Money display toggled:", drawMoney);
localStorage.setItem('drawMoney', drawMoney ? 'true' : 'false');
}