Skip to content

Instantly share code, notes, and snippets.

@TheNoim
Created December 20, 2023 21:00
Show Gist options
  • Save TheNoim/f0ff510010644a128f734fa84fb52e68 to your computer and use it in GitHub Desktop.
Save TheNoim/f0ff510010644a128f734fa84fb52e68 to your computer and use it in GitHub Desktop.
Change desk lamp color if you pickup a weapon with less than 30% ammo in CS2
import type { GameState, Weapons, Weapon, WeaponSlotType } from "npm:csgo-gsi-types";
import Fastify from "npm:fastify";
import {
useAsyncState,
useNewLight,
colors
} from "reactive-home";
declare module 'csgo-gsi-types' {
export interface Player {
weapons?: Weapons;
}
export interface Weapon {
ammo_clip?: number;
ammo_clip_max?: number;
state: "active" | "holstered"
}
}
const fastify = Fastify();
const deskLamp = useNewLight(await useAsyncState('light.0xdc8e95fffee16f80'))
let lastEvent: GameState | null = null;
let lastEventTime: null | Date = null;
fastify.post<{ Body: GameState }>("/", (req, reply) => {
lastEvent = req.body;
lastEventTime = new Date();
reply.code(200).send("");
if (lastEvent.player?.weapons) {
for (const weaponKey in lastEvent.player.weapons) {
const weapon = lastEvent.player.weapons[weaponKey as WeaponSlotType];
if (weapon?.state === 'active' && weapon?.ammo_clip !== undefined && weapon?.ammo_clip_max !== undefined) {
if ((weapon.ammo_clip / weapon.ammo_clip_max) <= 0.30) {
deskLamp.rgbColor = colors.yellow;
return
}
}
}
}
deskLamp.rgbColor = [255, 39, 208];
});
fastify.get("/lastEvent", () => {
return lastEvent
? JSON.stringify({ lastEvent, lastEventTime }, null, 4)
: lastEvent;
});
await fastify.listen({ port: 9999, host: "0.0.0.0" });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment