Release alpha 14 menu and developer test tools

This commit is contained in:
Emporex committed 2026-10-11 20:21:59 +02:00
1 parent 026d0bad58
commit a536b698f7
5 files changed
+464 -76

No files matched your search

+37 -12
View File
@@ -19,6 +19,11 @@ var BALANCE_COLLECTION = "game_balance";
var BALANCE_KEY = "global";
var SYSTEM_USER_ID = "00000000-0000-0000-0000-000000000000";
var OP_SNAPSHOT = 1, OP_EVENT = 2, OP_BUILD = 10, OP_UPGRADE = 11, OP_SELL = 12, OP_START_WAVE = 13, OP_READY = 14;
var OP_DEV_AUTH = 15, OP_DEV_ENERGY = 16, ADMIN_TOWER_TYPE = 7, DEV_MODE_PASSWORD = "Spring.3";
var DEV_ADMIN_USER_IDS = {
"6605c44c-17a2-4cc4-bf3e-e772fd6229b0": true,
"6f32e90b-1ed8-4baf-bfa2-17646600da79": true
};
var TICK_RATE = 10, SNAPSHOT_TICKS = 1, GRID = 80, WORLD_WIDTH = 2960, MIRROR_X = 1480;
var REACTOR_X = 1480, REACTOR_Y = 440, REACTOR_RADIUS = 112, PLAYFIELD_TOP = 84, PLAYFIELD_BOTTOM = 758;
var LEFT_PATH = [[-40,120],[1160,120],[1160,360],[360,360],[360,680],[1400,680],[1400,520],[1480,520]];
@@ -115,7 +120,8 @@ function readBalanceConfig(nk) {
return {config:config,updated_at:updatedAt};
}
function rpcGetGameBalance(ctx, logger, nk, payload) {
if (!ctx.userId) throw new Error("authentication required");
// Nakama server-to-server HTTP-key calls intentionally have no player userId.
// The internal statistics page uses that path; game clients call with a session.
return JSON.stringify(readBalanceConfig(nk));
}
function rpcSetGameBalance(ctx, logger, nk, payload) {
@@ -508,7 +514,7 @@ function initialState(params) {
var requestedDifficulty = params.difficulty === undefined || params.difficulty === null ? 1 : Number(params.difficulty);
var difficulty = Math.max(0,Math.min(2,Math.floor(isFinite(requestedDifficulty) ? requestedDifficulty : 1)));
var credits = DIFFICULTIES[difficulty].startCredits;
return {presences:{},slots:{},players:{},ready:{},started:false,pausedForReconnect:false,reconnectDeadlineTick:0,gameOver:false,difficulty:difficulty,mode:String(params.mode||"quick"),roomCode:normalizeRoomCode(params.roomCode),hostUserId:String(params.hostUserId||""),emptyTicks:0,credits:[credits,credits],reactorLives:30,wave:0,waveActive:false,spawnRemaining:0,waveSchedule:[],spawnTimer:0,nextSpawnRoute:0,nextEnemyId:1,nextTowerId:1,towers:[],enemies:[],shots:[],lastSnapshotTick:0,lastLoopStartMs:0,balance:defaultBalanceConfig()};
return {presences:{},slots:{},players:{},ready:{},devAuthorized:{},started:false,pausedForReconnect:false,reconnectDeadlineTick:0,gameOver:false,difficulty:difficulty,mode:String(params.mode||"quick"),roomCode:normalizeRoomCode(params.roomCode),hostUserId:String(params.hostUserId||""),emptyTicks:0,credits:[credits,credits],reactorLives:30,wave:0,waveActive:false,spawnRemaining:0,waveSchedule:[],spawnTimer:0,nextSpawnRoute:0,nextEnemyId:1,nextTowerId:1,towers:[],enemies:[],shots:[],lastSnapshotTick:0,lastLoopStartMs:0,balance:defaultBalanceConfig()};
}
function matchLabel(state) { return state.mode === "private" ? state.roomCode : JSON.stringify({game:"void_bastion",mode:state.mode,open:!state.started&&!state.gameOver}); }
function matchInit(ctx,logger,nk,params) { var state=initialState(params||{}); state.balance=readBalanceConfig(nk).config; return {state:state,tickRate:TICK_RATE,label:matchLabel(state)}; }
@@ -567,7 +573,10 @@ function matchLeave(ctx,logger,nk,dispatcher,tick,state,presences) {
dispatcher.matchLabelUpdate(matchLabel(state)); broadcastEvent(dispatcher,"partner_left",{reconnect_seconds:60}); broadcastSnapshot(dispatcher,state,tick); return {state:state};
}
function parseMessage(nk,message) { try { return JSON.parse(nk.binaryToString(message.data)||"{}"); } catch (error) { return null; } }
function towerInfo(state,type) { return state.balance && state.balance.towers[type] || TOWERS[type]; }
function towerInfo(state,type) {
if (type===ADMIN_TOWER_TYPE) return {cost:0,damage:1000000,range:3000,cooldown:0.08,targetClass:"both",bothScale:1};
return state.balance && state.balance.towers[type] || TOWERS[type];
}
function enemyInfo(state,type) { return state.balance && state.balance.enemies[type] || ENEMIES[type]; }
function towerDamage(state,t) { return towerInfo(state,t.type).damage*(1+(t.level-1)*0.52); }
function towerRange(state,t) { return towerInfo(state,t.type).range*(1+(t.level-1)*0.08); }
@@ -578,19 +587,20 @@ function distanceToSegment(px,py,ax,ay,bx,by) {
var abx=bx-ax,aby=by-ay,len=abx*abx+aby*aby,t=len>0?((px-ax)*abx+(py-ay)*aby)/len:0;
t=Math.max(0,Math.min(1,t)); return Math.sqrt(distanceSquared(px,py,ax+abx*t,ay+aby*t));
}
function validBuildPosition(state,slot,x,y) {
function validBuildPosition(state,slot,x,y,allowAnySide) {
var sx=Math.floor(x/GRID)*GRID+GRID*0.5,sy=Math.floor(y/GRID)*GRID+GRID*0.5;
if (Math.abs(sx-x)>1||Math.abs(sy-y)>1||y<PLAYFIELD_TOP||y>PLAYFIELD_BOTTOM) return false;
if (slot===0&&(x<120||x>=MIRROR_X-GRID*0.5)) return false;
if (slot===1&&(x<=MIRROR_X+GRID*0.5||x>WORLD_WIDTH-120)) return false;
if (Math.abs(sx-x)>1||Math.abs(sy-y)>1||x<120||x>WORLD_WIDTH-120||y<PLAYFIELD_TOP||y>PLAYFIELD_BOTTOM) return false;
if (!allowAnySide&&slot===0&&(x<120||x>=MIRROR_X-GRID*0.5)) return false;
if (!allowAnySide&&slot===1&&(x<=MIRROR_X+GRID*0.5||x>WORLD_WIDTH-120)) return false;
if (distanceSquared(x,y,REACTOR_X,REACTOR_Y)<=REACTOR_RADIUS*REACTOR_RADIUS) return false;
for (var r=0;r<PATHS.length;r++) for (var i=0;i<PATHS[r].length-1;i++) if (distanceToSegment(x,y,PATHS[r][i][0],PATHS[r][i][1],PATHS[r][i+1][0],PATHS[r][i+1][1])<GRID*0.62) return false;
for (var j=0;j<state.towers.length;j++) if (distanceSquared(x,y,state.towers[j].x,state.towers[j].y)<GRID*GRID*0.52) return false;
return true;
}
function handleBuild(state,slot,payload) {
function handleBuild(state,slot,payload,userId) {
var type=Math.floor(Number(payload.tower_type)),x=Number(payload.x),y=Number(payload.y);
if (!isFinite(x)||!isFinite(y)||type<0||type>=TOWERS.length||!validBuildPosition(state,slot,x,y)||state.credits[slot]<towerInfo(state,type).cost) return false;
var isAdmin=type===ADMIN_TOWER_TYPE;
if (!isFinite(x)||!isFinite(y)||type<0||type>TOWERS.length||(isAdmin&&!state.devAuthorized[userId])||!validBuildPosition(state,slot,x,y,isAdmin)||state.credits[slot]<towerInfo(state,type).cost) return false;
state.credits[slot]-=towerInfo(state,type).cost;
state.towers.push({id:state.nextTowerId++,x:x,y:y,type:type,level:1,owner:slot,cooldown:0.15,angle:slot===0?0:Math.PI}); return true;
}
@@ -716,7 +726,7 @@ function updateWave(state,delta,dispatcher) {
function handleCommand(nk,dispatcher,state,message) {
var slot=state.slots[message.sender.userId]; if (slot===undefined) return;
var payload=parseMessage(nk,message); if (payload===null) return; var changed=false;
if (message.opCode===OP_BUILD) changed=handleBuild(state,slot,payload);
if (message.opCode===OP_BUILD) changed=handleBuild(state,slot,payload,message.sender.userId);
else if (message.opCode===OP_UPGRADE) changed=handleUpgrade(state,slot,payload);
else if (message.opCode===OP_SELL) changed=handleSell(state,slot,payload);
else if (message.opCode===OP_START_WAVE) changed=startWave(state);
@@ -730,6 +740,23 @@ function handleCommand(nk,dispatcher,state,message) {
if (allReady) { state.started=true; state.pausedForReconnect=false; dispatcher.matchLabelUpdate(matchLabel(state)); broadcastEvent(dispatcher,"match_started",{}); }
changed=true;
}
else if (message.opCode===OP_DEV_AUTH&&state.started&&!state.gameOver) {
if (DEV_ADMIN_USER_IDS[message.sender.userId]===true&&payload.password===DEV_MODE_PASSWORD) {
state.devAuthorized[message.sender.userId]=true;
broadcastEvent(dispatcher,"developer_authorized",{slot:slot});
changed=true;
} else {
broadcastEvent(dispatcher,"developer_auth_rejected",{slot:slot});
changed=true;
}
}
else if (message.opCode===OP_DEV_ENERGY&&state.started&&!state.gameOver&&state.devAuthorized[message.sender.userId]) {
var energyChange=Math.floor(Number(payload.amount));
if (energyChange===500||energyChange===-500) {
state.credits[slot]=Math.max(0,state.credits[slot]+energyChange);
changed=true;
}
}
if (!changed) broadcastEvent(dispatcher,"command_rejected",{op_code:message.opCode,player:slot});
}
function matchLoop(ctx,logger,nk,dispatcher,tick,state,messages) {
@@ -849,8 +876,6 @@ function InitModule(ctx,logger,nk,initializer) {
initializer.registerRpc("private_stats",rpcPrivateStats);
initializer.registerRpc("get_game_balance",rpcGetGameBalance);
initializer.registerRpc("set_game_balance",rpcSetGameBalance);
initializer.registerRpc("get_game_balance",rpcGetGameBalance);
initializer.registerRpc("set_game_balance",rpcSetGameBalance);
initializer.registerRpc("create_unlock_code",rpcCreateUnlockCode);
initializer.registerRpc("list_unlock_codes",rpcListUnlockCodes);
initializer.registerRpc("delete_unlock_code",rpcDeleteUnlockCode);