Spaces:
Sleeping
Sleeping
Update app.js
Browse files
app.js
CHANGED
|
@@ -3,19 +3,16 @@ import express from 'express';
|
|
| 3 |
import cors from 'cors';
|
| 4 |
import { WebSocketServer, WebSocket } from 'ws';
|
| 5 |
import { v4 as uuidv4 } from 'uuid';
|
| 6 |
-
// Make sure this points to your JS mechanics file
|
| 7 |
import {
|
| 8 |
resolveCombat, getDerivedStats, breedOffspring, INITIAL_STATE,
|
| 9 |
attributeCap, featureGateReason, getEvolutionPoints, COMPOSITION_ORDER
|
| 10 |
} from './mechanics.js';
|
| 11 |
|
| 12 |
const app = express();
|
| 13 |
-
// Hugging Face Spaces and other cloud PaaS use process.env.PORT (HF uses 7860)
|
| 14 |
const PORT = process.env.PORT || 7860;
|
| 15 |
|
| 16 |
-
// Enable CORS so your separate frontend can communicate
|
| 17 |
app.use(cors({
|
| 18 |
-
origin: '*',
|
| 19 |
}));
|
| 20 |
|
| 21 |
const MIN_ROOMS = 5;
|
|
@@ -28,24 +25,23 @@ const roomEmptySince = new Map();
|
|
| 28 |
const roomChatLogs = new Map();
|
| 29 |
const CHAT_HISTORY_LIMIT = 50;
|
| 30 |
|
| 31 |
-
// ─── BOT TEMPLATES ───────────────────────────────────────────────────────────────
|
| 32 |
const BOT_TEMPLATES = [
|
| 33 |
-
{
|
| 34 |
compositions: { ANIMALIA: 100, CHITINARIA: 0, PLANTAE: 0, CNIDARIA: 0, MYCELIUM: 0, MINERALIS: 0 },
|
| 35 |
attributes: { WATER_RETENTION: 2, INSULATION: 0, MUSCLE_DENSITY: 8, BONE_DENSITY: 5, FAT_RESERVES: 2, METABOLIC_RATE: 2, NEURAL_COMPLEXITY: 2, THERMAL_REGULATION: 0, BUOYANCY: 0, CAMOUFLAGE: 0, REGENERATION: 0, TOXIN_RESISTANCE: 0 },
|
| 36 |
features: ['CRUSHING_MANDIBLES'], socialType: 'SOLITARY'
|
| 37 |
},
|
| 38 |
-
{
|
| 39 |
compositions: { ANIMALIA: 40, CHITINARIA: 0, PLANTAE: 0, CNIDARIA: 60, MYCELIUM: 0, MINERALIS: 0 },
|
| 40 |
attributes: { WATER_RETENTION: 0, INSULATION: 0, MUSCLE_DENSITY: 2, BONE_DENSITY: 0, FAT_RESERVES: 4, METABOLIC_RATE: 2, NEURAL_COMPLEXITY: 3, THERMAL_REGULATION: 0, BUOYANCY: 5, CAMOUFLAGE: 0, REGENERATION: 0, TOXIN_RESISTANCE: 8 },
|
| 41 |
features: ['TOXIN_GLANDS', 'GELATINOUS_BODY'], socialType: 'SOLITARY'
|
| 42 |
},
|
| 43 |
-
{
|
| 44 |
compositions: { ANIMALIA: 100, CHITINARIA: 0, PLANTAE: 0, CNIDARIA: 0, MYCELIUM: 0, MINERALIS: 0 },
|
| 45 |
attributes: { WATER_RETENTION: 2, INSULATION: 0, MUSCLE_DENSITY: 4, BONE_DENSITY: 1, FAT_RESERVES: 2, METABOLIC_RATE: 8, NEURAL_COMPLEXITY: 4, THERMAL_REGULATION: 2, BUOYANCY: 0, CAMOUFLAGE: 0, REGENERATION: 0, TOXIN_RESISTANCE: 0 },
|
| 46 |
features: ['SLASHING_BLADES'], socialType: 'PACK'
|
| 47 |
},
|
| 48 |
-
{
|
| 49 |
compositions: { ANIMALIA: 50, CHITINARIA: 50, PLANTAE: 0, CNIDARIA: 0, MYCELIUM: 0, MINERALIS: 0 },
|
| 50 |
attributes: { WATER_RETENTION: 6, INSULATION: 0, MUSCLE_DENSITY: 4, BONE_DENSITY: 4, FAT_RESERVES: 2, METABOLIC_RATE: 3, NEURAL_COMPLEXITY: 4, THERMAL_REGULATION: 4, BUOYANCY: 0, CAMOUFLAGE: 5, REGENERATION: 0, TOXIN_RESISTANCE: 2 },
|
| 51 |
features: ['SCORPION_TAIL'], socialType: 'SOLITARY'
|
|
@@ -56,7 +52,6 @@ const BOT_NAMES = ['Alpha', 'Beta', 'Gamma', 'Delta', 'Omega', 'Zeta', 'Sigma',
|
|
| 56 |
|
| 57 |
function populateBots(room, guaranteeBot = false) {
|
| 58 |
if (!guaranteeBot && Math.random() > 0.5) return;
|
| 59 |
-
// const numBots = Math.floor(Math.random() * 2) + 1;
|
| 60 |
const numBots = Math.floor(Math.random() * 6) + 8;
|
| 61 |
|
| 62 |
for (let i = 0; i < numBots; i++) {
|
|
@@ -104,8 +99,12 @@ for (let i = 0; i < MIN_ROOMS; i++) {
|
|
| 104 |
rooms[r.id] = r;
|
| 105 |
}
|
| 106 |
|
|
|
|
|
|
|
|
|
|
| 107 |
function isRoomJoinable(room) {
|
| 108 |
if (room.status === 'EXTINCTION') return false;
|
|
|
|
| 109 |
const elapsed = room.startTime ? Date.now() - room.startTime : 0;
|
| 110 |
const pCount = Object.keys(room.players).length;
|
| 111 |
return !(pCount >= 2 && elapsed > 60000);
|
|
@@ -169,9 +168,6 @@ function zoneDeathAttacker(biome) {
|
|
| 169 |
}
|
| 170 |
}
|
| 171 |
|
| 172 |
-
// ─── STATE SANITIZATION ───────────────────────────────────────────────────────────
|
| 173 |
-
// The server is authoritative for combat, so a client-submitted CreatureState must
|
| 174 |
-
// never be trusted verbatim — clamp caps, drop ungated features, reject over-budget builds.
|
| 175 |
function sanitizeState(raw) {
|
| 176 |
try {
|
| 177 |
const s = {
|
|
@@ -193,7 +189,7 @@ function sanitizeState(raw) {
|
|
| 193 |
s.features = s.features.filter(f => !featureGateReason(f, s.compositions));
|
| 194 |
|
| 195 |
const ep = getEvolutionPoints(s);
|
| 196 |
-
if (ep.used > ep.max) return null;
|
| 197 |
|
| 198 |
return s;
|
| 199 |
} catch {
|
|
@@ -201,11 +197,9 @@ function sanitizeState(raw) {
|
|
| 201 |
}
|
| 202 |
}
|
| 203 |
|
| 204 |
-
// REST health checks
|
| 205 |
app.get('/api/health', (req, res) => res.json({ status: 'ok', roomsCount: Object.keys(rooms).length }));
|
| 206 |
app.get('/', (req, res) => res.send('Level Evolution standalone JS server running.'));
|
| 207 |
|
| 208 |
-
// Launch Server
|
| 209 |
const server = app.listen(PORT, '0.0.0.0', () => console.log(`Standalone Backend running on port ${PORT}`));
|
| 210 |
const wss = new WebSocketServer({ server });
|
| 211 |
|
|
@@ -310,7 +304,7 @@ wss.on('connection', (ws) => {
|
|
| 310 |
|
| 311 |
if (!isRoomJoinable(target)) {
|
| 312 |
maybeAddRoom();
|
| 313 |
-
ws.send(JSON.stringify({ type: '
|
| 314 |
break;
|
| 315 |
}
|
| 316 |
|
|
@@ -355,8 +349,6 @@ wss.on('connection', (ws) => {
|
|
| 355 |
broadcastRoom(player.roomId);
|
| 356 |
}
|
| 357 |
}
|
| 358 |
-
// if sanitization rejected the payload (over EP budget / malformed), we
|
| 359 |
-
// silently keep the last valid state — the client resends on every change anyway
|
| 360 |
break;
|
| 361 |
}
|
| 362 |
case 'SET_READY': {
|
|
@@ -452,7 +444,6 @@ wss.on('connection', (ws) => {
|
|
| 452 |
});
|
| 453 |
});
|
| 454 |
|
| 455 |
-
// ─── EXTINCTION TICK LOOP (20Hz) ──────────────────────────────────────────────────
|
| 456 |
let globalTick = 0;
|
| 457 |
setInterval(() => {
|
| 458 |
globalTick++;
|
|
@@ -543,7 +534,6 @@ setInterval(() => {
|
|
| 543 |
|
| 544 |
const activeKeys = Object.keys(gs.entities).filter(k => !gs.entities[k].dead);
|
| 545 |
|
| 546 |
-
// Separation
|
| 547 |
for (const k of activeKeys) {
|
| 548 |
const e = gs.entities[k];
|
| 549 |
for (const otherK of activeKeys) {
|
|
@@ -594,7 +584,6 @@ setInterval(() => {
|
|
| 594 |
}
|
| 595 |
if (envDmgRate > 0) e.envDamage += envDmgRate * dt;
|
| 596 |
|
| 597 |
-
// Plantae passive regen in Rainforest — the composition's "free energy in light" bonus
|
| 598 |
if (room.biome === 'RAINFOREST' && stateA.compositions.PLANTAE >= 30) {
|
| 599 |
e.hunger = Math.max(0, e.hunger - dt * 0.5);
|
| 600 |
}
|
|
@@ -610,7 +599,6 @@ setInterval(() => {
|
|
| 610 |
const baseSpeed = derivedA.speed * 0.8;
|
| 611 |
const moveSpeed = Math.max(baseSpeed, outsideZone ? PANIC_RETURN_SPEED : 0);
|
| 612 |
|
| 613 |
-
// Bot AI
|
| 614 |
const isBot = k.startsWith('bot-');
|
| 615 |
let aiTarget = null;
|
| 616 |
|
|
@@ -678,7 +666,6 @@ setInterval(() => {
|
|
| 678 |
if (e.matingCooldown > 0) e.matingCooldown = Math.max(0, e.matingCooldown - dt);
|
| 679 |
if (e.combatCooldown > 0) e.combatCooldown = Math.max(0, e.combatCooldown - dt);
|
| 680 |
|
| 681 |
-
// Combat
|
| 682 |
for (const otherK of activeKeys) {
|
| 683 |
if (k === otherK) continue;
|
| 684 |
const other = gs.entities[otherK];
|
|
@@ -770,7 +757,6 @@ setInterval(() => {
|
|
| 770 |
|
| 771 |
if (needsPlayerBroadcast) broadcastRoom(roomId);
|
| 772 |
|
| 773 |
-
// === DELAYED WINNER CHECK ===
|
| 774 |
const all = Object.values(gs.entities);
|
| 775 |
const alive = all.filter(e => !e.dead);
|
| 776 |
|
|
|
|
| 3 |
import cors from 'cors';
|
| 4 |
import { WebSocketServer, WebSocket } from 'ws';
|
| 5 |
import { v4 as uuidv4 } from 'uuid';
|
|
|
|
| 6 |
import {
|
| 7 |
resolveCombat, getDerivedStats, breedOffspring, INITIAL_STATE,
|
| 8 |
attributeCap, featureGateReason, getEvolutionPoints, COMPOSITION_ORDER
|
| 9 |
} from './mechanics.js';
|
| 10 |
|
| 11 |
const app = express();
|
|
|
|
| 12 |
const PORT = process.env.PORT || 7860;
|
| 13 |
|
|
|
|
| 14 |
app.use(cors({
|
| 15 |
+
origin: '*',
|
| 16 |
}));
|
| 17 |
|
| 18 |
const MIN_ROOMS = 5;
|
|
|
|
| 25 |
const roomChatLogs = new Map();
|
| 26 |
const CHAT_HISTORY_LIMIT = 50;
|
| 27 |
|
|
|
|
| 28 |
const BOT_TEMPLATES = [
|
| 29 |
+
{
|
| 30 |
compositions: { ANIMALIA: 100, CHITINARIA: 0, PLANTAE: 0, CNIDARIA: 0, MYCELIUM: 0, MINERALIS: 0 },
|
| 31 |
attributes: { WATER_RETENTION: 2, INSULATION: 0, MUSCLE_DENSITY: 8, BONE_DENSITY: 5, FAT_RESERVES: 2, METABOLIC_RATE: 2, NEURAL_COMPLEXITY: 2, THERMAL_REGULATION: 0, BUOYANCY: 0, CAMOUFLAGE: 0, REGENERATION: 0, TOXIN_RESISTANCE: 0 },
|
| 32 |
features: ['CRUSHING_MANDIBLES'], socialType: 'SOLITARY'
|
| 33 |
},
|
| 34 |
+
{
|
| 35 |
compositions: { ANIMALIA: 40, CHITINARIA: 0, PLANTAE: 0, CNIDARIA: 60, MYCELIUM: 0, MINERALIS: 0 },
|
| 36 |
attributes: { WATER_RETENTION: 0, INSULATION: 0, MUSCLE_DENSITY: 2, BONE_DENSITY: 0, FAT_RESERVES: 4, METABOLIC_RATE: 2, NEURAL_COMPLEXITY: 3, THERMAL_REGULATION: 0, BUOYANCY: 5, CAMOUFLAGE: 0, REGENERATION: 0, TOXIN_RESISTANCE: 8 },
|
| 37 |
features: ['TOXIN_GLANDS', 'GELATINOUS_BODY'], socialType: 'SOLITARY'
|
| 38 |
},
|
| 39 |
+
{
|
| 40 |
compositions: { ANIMALIA: 100, CHITINARIA: 0, PLANTAE: 0, CNIDARIA: 0, MYCELIUM: 0, MINERALIS: 0 },
|
| 41 |
attributes: { WATER_RETENTION: 2, INSULATION: 0, MUSCLE_DENSITY: 4, BONE_DENSITY: 1, FAT_RESERVES: 2, METABOLIC_RATE: 8, NEURAL_COMPLEXITY: 4, THERMAL_REGULATION: 2, BUOYANCY: 0, CAMOUFLAGE: 0, REGENERATION: 0, TOXIN_RESISTANCE: 0 },
|
| 42 |
features: ['SLASHING_BLADES'], socialType: 'PACK'
|
| 43 |
},
|
| 44 |
+
{
|
| 45 |
compositions: { ANIMALIA: 50, CHITINARIA: 50, PLANTAE: 0, CNIDARIA: 0, MYCELIUM: 0, MINERALIS: 0 },
|
| 46 |
attributes: { WATER_RETENTION: 6, INSULATION: 0, MUSCLE_DENSITY: 4, BONE_DENSITY: 4, FAT_RESERVES: 2, METABOLIC_RATE: 3, NEURAL_COMPLEXITY: 4, THERMAL_REGULATION: 4, BUOYANCY: 0, CAMOUFLAGE: 5, REGENERATION: 0, TOXIN_RESISTANCE: 2 },
|
| 47 |
features: ['SCORPION_TAIL'], socialType: 'SOLITARY'
|
|
|
|
| 52 |
|
| 53 |
function populateBots(room, guaranteeBot = false) {
|
| 54 |
if (!guaranteeBot && Math.random() > 0.5) return;
|
|
|
|
| 55 |
const numBots = Math.floor(Math.random() * 6) + 8;
|
| 56 |
|
| 57 |
for (let i = 0; i < numBots; i++) {
|
|
|
|
| 99 |
rooms[r.id] = r;
|
| 100 |
}
|
| 101 |
|
| 102 |
+
// A room only self-locks from idle time once a REAL player has actually
|
| 103 |
+
// joined it. Bot-only rooms sit idle indefinitely (e.g. HF cron keep-alive
|
| 104 |
+
// with no traffic) and must never lock themselves before anyone can join.
|
| 105 |
function isRoomJoinable(room) {
|
| 106 |
if (room.status === 'EXTINCTION') return false;
|
| 107 |
+
if (!room.hasRealPlayers) return true;
|
| 108 |
const elapsed = room.startTime ? Date.now() - room.startTime : 0;
|
| 109 |
const pCount = Object.keys(room.players).length;
|
| 110 |
return !(pCount >= 2 && elapsed > 60000);
|
|
|
|
| 168 |
}
|
| 169 |
}
|
| 170 |
|
|
|
|
|
|
|
|
|
|
| 171 |
function sanitizeState(raw) {
|
| 172 |
try {
|
| 173 |
const s = {
|
|
|
|
| 189 |
s.features = s.features.filter(f => !featureGateReason(f, s.compositions));
|
| 190 |
|
| 191 |
const ep = getEvolutionPoints(s);
|
| 192 |
+
if (ep.used > ep.max) return null;
|
| 193 |
|
| 194 |
return s;
|
| 195 |
} catch {
|
|
|
|
| 197 |
}
|
| 198 |
}
|
| 199 |
|
|
|
|
| 200 |
app.get('/api/health', (req, res) => res.json({ status: 'ok', roomsCount: Object.keys(rooms).length }));
|
| 201 |
app.get('/', (req, res) => res.send('Level Evolution standalone JS server running.'));
|
| 202 |
|
|
|
|
| 203 |
const server = app.listen(PORT, '0.0.0.0', () => console.log(`Standalone Backend running on port ${PORT}`));
|
| 204 |
const wss = new WebSocketServer({ server });
|
| 205 |
|
|
|
|
| 304 |
|
| 305 |
if (!isRoomJoinable(target)) {
|
| 306 |
maybeAddRoom();
|
| 307 |
+
ws.send(JSON.stringify({ type: 'JOIN_ROOM_FAILED', roomId: data.roomId, reason: 'Room locked — try the newest room.' }));
|
| 308 |
break;
|
| 309 |
}
|
| 310 |
|
|
|
|
| 349 |
broadcastRoom(player.roomId);
|
| 350 |
}
|
| 351 |
}
|
|
|
|
|
|
|
| 352 |
break;
|
| 353 |
}
|
| 354 |
case 'SET_READY': {
|
|
|
|
| 444 |
});
|
| 445 |
});
|
| 446 |
|
|
|
|
| 447 |
let globalTick = 0;
|
| 448 |
setInterval(() => {
|
| 449 |
globalTick++;
|
|
|
|
| 534 |
|
| 535 |
const activeKeys = Object.keys(gs.entities).filter(k => !gs.entities[k].dead);
|
| 536 |
|
|
|
|
| 537 |
for (const k of activeKeys) {
|
| 538 |
const e = gs.entities[k];
|
| 539 |
for (const otherK of activeKeys) {
|
|
|
|
| 584 |
}
|
| 585 |
if (envDmgRate > 0) e.envDamage += envDmgRate * dt;
|
| 586 |
|
|
|
|
| 587 |
if (room.biome === 'RAINFOREST' && stateA.compositions.PLANTAE >= 30) {
|
| 588 |
e.hunger = Math.max(0, e.hunger - dt * 0.5);
|
| 589 |
}
|
|
|
|
| 599 |
const baseSpeed = derivedA.speed * 0.8;
|
| 600 |
const moveSpeed = Math.max(baseSpeed, outsideZone ? PANIC_RETURN_SPEED : 0);
|
| 601 |
|
|
|
|
| 602 |
const isBot = k.startsWith('bot-');
|
| 603 |
let aiTarget = null;
|
| 604 |
|
|
|
|
| 666 |
if (e.matingCooldown > 0) e.matingCooldown = Math.max(0, e.matingCooldown - dt);
|
| 667 |
if (e.combatCooldown > 0) e.combatCooldown = Math.max(0, e.combatCooldown - dt);
|
| 668 |
|
|
|
|
| 669 |
for (const otherK of activeKeys) {
|
| 670 |
if (k === otherK) continue;
|
| 671 |
const other = gs.entities[otherK];
|
|
|
|
| 757 |
|
| 758 |
if (needsPlayerBroadcast) broadcastRoom(roomId);
|
| 759 |
|
|
|
|
| 760 |
const all = Object.values(gs.entities);
|
| 761 |
const alive = all.filter(e => !e.dead);
|
| 762 |
|