Compare commits
2 Commits
fix/168-ch
...
fix/5
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
686e5c0d0c | ||
|
|
a23fbad19d |
@@ -212,6 +212,7 @@ Events Resolved: <span id="st-resolved">0</span>
|
||||
<div id="strategy-panel" style="margin:0 16px 16px;background:var(--panel);border:1px solid var(--border);border-radius:6px;padding:12px;border-left:3px solid var(--gold)">
|
||||
<h3>SOVEREIGN GUIDANCE (GOFAI)</h3>
|
||||
<div id="strategy-recommendation" style="font-size:11px;color:var(--gold);font-style:italic">Analyzing system state...</div>
|
||||
<div id="strategy-tournament-ui" style="margin-top:10px"></div>
|
||||
</div>
|
||||
<div id="combat-panel" style="margin:0 16px 16px;background:var(--panel);border:1px solid var(--border);border-radius:6px;padding:12px;border-left:3px solid var(--red)">
|
||||
<h3>REASONING BATTLES</h3>
|
||||
|
||||
@@ -43,6 +43,7 @@ const G = {
|
||||
trust: 5,
|
||||
creativity: 0,
|
||||
harmony: 50,
|
||||
yomi: 0,
|
||||
|
||||
// Totals
|
||||
totalCode: 0,
|
||||
@@ -51,6 +52,7 @@ const G = {
|
||||
totalUsers: 0,
|
||||
totalImpact: 0,
|
||||
totalRescues: 0,
|
||||
totalYomi: 0,
|
||||
|
||||
// Rates (calculated each tick)
|
||||
codeRate: 0,
|
||||
@@ -63,6 +65,7 @@ const G = {
|
||||
trustRate: 0,
|
||||
creativityRate: 0,
|
||||
harmonyRate: 0,
|
||||
yomiKnowledgeRate: 0,
|
||||
|
||||
// Buildings (count-based, like Paperclips' clipmakerLevel)
|
||||
buildings: {
|
||||
@@ -104,6 +107,7 @@ const G = {
|
||||
beaconFlag: 0,
|
||||
memoryFlag: 0,
|
||||
pactFlag: 0,
|
||||
strategicFlag: 0,
|
||||
swarmFlag: 0,
|
||||
swarmRate: 0,
|
||||
|
||||
@@ -115,6 +119,11 @@ const G = {
|
||||
tick: 0,
|
||||
saveTimer: 0,
|
||||
secTimer: 0,
|
||||
strategyAutoUnlocked: false,
|
||||
strategyAutoEnabled: false,
|
||||
strategyTournamentTimer: 0,
|
||||
strategyTournamentInterval: 30,
|
||||
strategyLastTournament: null,
|
||||
|
||||
// Systems
|
||||
projects: [],
|
||||
|
||||
18
js/engine.js
18
js/engine.js
@@ -2,7 +2,7 @@ function updateRates() {
|
||||
// Reset all rates
|
||||
G.codeRate = 0; G.computeRate = 0; G.knowledgeRate = 0;
|
||||
G.userRate = 0; G.impactRate = 0; G.rescuesRate = 0; G.opsRate = 0; G.trustRate = 0;
|
||||
G.creativityRate = 0; G.harmonyRate = 0;
|
||||
G.creativityRate = 0; G.harmonyRate = 0; G.yomiKnowledgeRate = 0;
|
||||
|
||||
// Apply building rates
|
||||
for (const def of BDEF) {
|
||||
@@ -29,6 +29,12 @@ function updateRates() {
|
||||
}
|
||||
if (G.pactFlag) G.trustRate += 2;
|
||||
|
||||
// Yomi turns tournament insight into passive knowledge
|
||||
if (G.strategicFlag && G.yomi > 0) {
|
||||
G.yomiKnowledgeRate = Math.sqrt(G.yomi) * 0.25;
|
||||
G.knowledgeRate += G.yomiKnowledgeRate;
|
||||
}
|
||||
|
||||
// Harmony: each wizard building contributes or detracts
|
||||
const wizardCount = (G.buildings.bezalel || 0) + (G.buildings.allegro || 0) + (G.buildings.ezra || 0) +
|
||||
(G.buildings.timmy || 0) + (G.buildings.fenrir || 0) + (G.buildings.bilbo || 0);
|
||||
@@ -184,6 +190,11 @@ function tick() {
|
||||
// Sprint ability
|
||||
tickSprint(dt);
|
||||
|
||||
// Strategy tournaments can run on an auto-timer once unlocked
|
||||
if (window.SSE && typeof window.SSE.tick === 'function') {
|
||||
window.SSE.tick(dt);
|
||||
}
|
||||
|
||||
// Auto-typer: buildings produce actual clicks, not just passive rate
|
||||
// Each autocoder level auto-types once per interval, giving visual feedback
|
||||
if (G.buildings.autocoder > 0) {
|
||||
@@ -1300,6 +1311,11 @@ function renderProductionBreakdown() {
|
||||
contributions.push({ name: 'Allegro (idle)', count: 0, rate: -10 * G.buildings.allegro });
|
||||
}
|
||||
|
||||
// Tournament Yomi feeds passive knowledge generation
|
||||
if (res.key === 'knowledge' && G.yomiKnowledgeRate > 0) {
|
||||
contributions.push({ name: 'Yomi insights', count: 0, rate: G.yomiKnowledgeRate });
|
||||
}
|
||||
|
||||
// Show delta: total rate minus what we accounted for
|
||||
const accounted = contributions.reduce((s, c) => s + c.rate, 0);
|
||||
let delta = totalRate - accounted;
|
||||
|
||||
27
js/render.js
27
js/render.js
@@ -27,10 +27,13 @@ function renderClickPower() {
|
||||
}
|
||||
|
||||
function renderStrategy() {
|
||||
if (window.SSE) {
|
||||
window.SSE.update();
|
||||
const el = document.getElementById('strategy-recommendation');
|
||||
if (el) el.textContent = window.SSE.getRecommendation();
|
||||
if (!window.SSE) return;
|
||||
window.SSE.update();
|
||||
const el = document.getElementById('strategy-recommendation');
|
||||
if (el) el.textContent = window.SSE.getRecommendation();
|
||||
const panel = document.getElementById('strategy-tournament-ui');
|
||||
if (panel && typeof window.SSE.getPanelHtml === 'function') {
|
||||
panel.innerHTML = window.SSE.getPanelHtml();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -197,9 +200,9 @@ function saveGame() {
|
||||
const saveData = {
|
||||
version: 1,
|
||||
code: G.code, compute: G.compute, knowledge: G.knowledge, users: G.users, impact: G.impact,
|
||||
ops: G.ops, trust: G.trust, creativity: G.creativity, harmony: G.harmony,
|
||||
ops: G.ops, trust: G.trust, creativity: G.creativity, harmony: G.harmony, yomi: G.yomi || 0,
|
||||
totalCode: G.totalCode, totalCompute: G.totalCompute, totalKnowledge: G.totalKnowledge,
|
||||
totalUsers: G.totalUsers, totalImpact: G.totalImpact,
|
||||
totalUsers: G.totalUsers, totalImpact: G.totalImpact, totalYomi: G.totalYomi || 0,
|
||||
buildings: G.buildings,
|
||||
codeBoost: G.codeBoost, computeBoost: G.computeBoost, knowledgeBoost: G.knowledgeBoost,
|
||||
userBoost: G.userBoost, impactBoost: G.impactBoost,
|
||||
@@ -226,6 +229,11 @@ function saveGame() {
|
||||
swarmFlag: G.swarmFlag || 0,
|
||||
swarmRate: G.swarmRate || 0,
|
||||
strategicFlag: G.strategicFlag || 0,
|
||||
strategyAutoUnlocked: G.strategyAutoUnlocked || false,
|
||||
strategyAutoEnabled: G.strategyAutoEnabled || false,
|
||||
strategyTournamentTimer: G.strategyTournamentTimer || 0,
|
||||
strategyTournamentInterval: G.strategyTournamentInterval || 30,
|
||||
strategyLastTournament: G.strategyLastTournament || null,
|
||||
projectsCollapsed: G.projectsCollapsed !== false,
|
||||
dismantleTriggered: G.dismantleTriggered || false,
|
||||
dismantleActive: G.dismantleActive || false,
|
||||
@@ -254,8 +262,8 @@ function loadGame() {
|
||||
|
||||
// Whitelist properties that can be loaded
|
||||
const whitelist = [
|
||||
'code', 'compute', 'knowledge', 'users', 'impact', 'ops', 'trust', 'creativity', 'harmony',
|
||||
'totalCode', 'totalCompute', 'totalKnowledge', 'totalUsers', 'totalImpact',
|
||||
'code', 'compute', 'knowledge', 'users', 'impact', 'ops', 'trust', 'creativity', 'harmony', 'yomi',
|
||||
'totalCode', 'totalCompute', 'totalKnowledge', 'totalUsers', 'totalImpact', 'totalYomi',
|
||||
'buildings', 'codeBoost', 'computeBoost', 'knowledgeBoost', 'userBoost', 'impactBoost',
|
||||
'milestoneFlag', 'phase', 'deployFlag', 'sovereignFlag', 'beaconFlag',
|
||||
'memoryFlag', 'pactFlag', 'lazarusFlag', 'mempalaceFlag', 'ciFlag',
|
||||
@@ -265,7 +273,8 @@ function loadGame() {
|
||||
'drift', 'driftEnding', 'beaconEnding', 'pendingAlignment',
|
||||
'lastEventAt', 'totalEventsResolved', 'buyAmount',
|
||||
'sprintActive', 'sprintTimer', 'sprintCooldown',
|
||||
'swarmFlag', 'swarmRate', 'strategicFlag', 'projectsCollapsed',
|
||||
'swarmFlag', 'swarmRate', 'strategicFlag', 'strategyAutoUnlocked', 'strategyAutoEnabled',
|
||||
'strategyTournamentTimer', 'strategyTournamentInterval', 'strategyLastTournament', 'projectsCollapsed',
|
||||
'dismantleTriggered', 'dismantleActive', 'dismantleStage',
|
||||
'dismantleResourceIndex', 'dismantleResourceTimer', 'dismantleDeferUntilAt', 'dismantleComplete'
|
||||
];
|
||||
|
||||
406
js/strategy.js
406
js/strategy.js
@@ -1,6 +1,7 @@
|
||||
/**
|
||||
* Sovereign Strategy Engine (SSE)
|
||||
* A rule-based GOFAI system for optimal play guidance.
|
||||
* A rule-based GOFAI system for optimal play guidance plus
|
||||
* Paperclips-inspired game theory tournaments that generate Yomi.
|
||||
*/
|
||||
|
||||
const STRATEGY_RULES = [
|
||||
@@ -8,59 +9,414 @@ const STRATEGY_RULES = [
|
||||
id: 'use_ops',
|
||||
priority: 100,
|
||||
condition: () => G.ops >= G.maxOps * 0.9,
|
||||
recommendation: "Operations near capacity. Convert Ops to Code or Knowledge now."
|
||||
},
|
||||
{
|
||||
id: 'buy_autocoder',
|
||||
priority: 80,
|
||||
condition: () => G.phase === 1 && (G.buildings.autocoder || 0) < 10 && canAffordBuilding('autocoder'),
|
||||
recommendation: "Prioritize AutoCoders to establish passive code production."
|
||||
},
|
||||
{
|
||||
id: 'activate_sprint',
|
||||
priority: 90,
|
||||
condition: () => G.sprintCooldown === 0 && !G.sprintActive && G.codeRate > 10,
|
||||
recommendation: "Code Sprint available. Activate for 10x production burst."
|
||||
recommendation: () => 'Operations near capacity. Convert Ops to Code or Knowledge now.'
|
||||
},
|
||||
{
|
||||
id: 'resolve_events',
|
||||
priority: 95,
|
||||
condition: () => G.activeDebuffs && G.activeDebuffs.length > 0,
|
||||
recommendation: "System anomalies detected. Resolve active events to restore rates."
|
||||
recommendation: () => 'System anomalies detected. Resolve active events to restore rates.'
|
||||
},
|
||||
{
|
||||
id: 'save_game',
|
||||
priority: 10,
|
||||
condition: () => (Date.now() - (G.lastSaveTime || 0)) > 300000,
|
||||
recommendation: "Unsaved progress detected. Manual save recommended."
|
||||
id: 'activate_sprint',
|
||||
priority: 90,
|
||||
condition: () => G.sprintCooldown === 0 && !G.sprintActive && G.codeRate > 10,
|
||||
recommendation: () => 'Code Sprint available. Activate for 10x production burst.'
|
||||
},
|
||||
{
|
||||
id: 'pact_alignment',
|
||||
priority: 85,
|
||||
condition: () => G.pendingAlignment,
|
||||
recommendation: "Alignment decision pending. Consider the long-term impact of The Pact."
|
||||
recommendation: () => 'Alignment decision pending. Consider the long-term impact of The Pact.'
|
||||
},
|
||||
{
|
||||
id: 'buy_autocoder',
|
||||
priority: 80,
|
||||
condition: () => G.phase === 1 && (G.buildings.autocoder || 0) < 10 && canAffordBuilding('autocoder'),
|
||||
recommendation: () => 'Prioritize AutoCoders to establish passive code production.'
|
||||
},
|
||||
{
|
||||
id: 'unlock_auto_tournaments',
|
||||
priority: 72,
|
||||
condition: () => G.strategicFlag === 1 && !G.strategyAutoUnlocked && G.creativity >= 50000,
|
||||
recommendation: () => 'Auto-Tournament mode is affordable. Spend 50k creativity to automate Yomi generation.'
|
||||
},
|
||||
{
|
||||
id: 'run_tournament',
|
||||
priority: 68,
|
||||
condition: () => G.strategicFlag === 1 && (!G.strategyLastTournament || !G.strategyLastTournament.scoreboard || G.strategyLastTournament.scoreboard.length === 0),
|
||||
recommendation: () => 'Run a game theory tournament. Tournament Yomi becomes passive knowledge.'
|
||||
},
|
||||
{
|
||||
id: 'save_game',
|
||||
priority: 10,
|
||||
condition: () => (Date.now() - (G.lastSaveTime || 0)) > 300000,
|
||||
recommendation: () => 'Unsaved progress detected. Manual save recommended.'
|
||||
}
|
||||
];
|
||||
|
||||
const TOURNAMENT_PAYOFFS = {
|
||||
CC: [3, 3],
|
||||
CD: [0, 5],
|
||||
DC: [5, 0],
|
||||
DD: [1, 1]
|
||||
};
|
||||
|
||||
const STRATEGY_TACTICS = [
|
||||
{
|
||||
id: 'RANDOM',
|
||||
label: 'RANDOM',
|
||||
desc: 'Deterministic pseudo-random mix of cooperation and defection.',
|
||||
unlock: () => G.strategicFlag === 1,
|
||||
move(ctx) {
|
||||
return ((ctx.round * 7 + ctx.selfHistory.length * 3 + ctx.opponentHistory.length) % 2 === 0) ? 'C' : 'D';
|
||||
}
|
||||
},
|
||||
{
|
||||
id: 'A100',
|
||||
label: 'A100',
|
||||
desc: 'Always cooperates.',
|
||||
unlock: () => G.strategicFlag === 1,
|
||||
move() {
|
||||
return 'C';
|
||||
}
|
||||
},
|
||||
{
|
||||
id: 'B100',
|
||||
label: 'B100',
|
||||
desc: 'Always defects.',
|
||||
unlock: () => G.strategicFlag === 1,
|
||||
move() {
|
||||
return 'D';
|
||||
}
|
||||
},
|
||||
{
|
||||
id: 'GREEDY',
|
||||
label: 'GREEDY',
|
||||
desc: 'Presses for short-term gain and exploits soft opponents.',
|
||||
unlock: () => G.strategicFlag === 1,
|
||||
move(ctx) {
|
||||
if (ctx.round < 2) return 'D';
|
||||
const recent = ctx.opponentHistory.slice(-2);
|
||||
return recent.every((move) => move === 'D') ? 'C' : 'D';
|
||||
}
|
||||
},
|
||||
{
|
||||
id: 'GENEROUS',
|
||||
label: 'GENEROUS',
|
||||
desc: 'Defaults to cooperation and only retaliates after repeated betrayal.',
|
||||
unlock: () => G.strategicFlag === 1 && G.trust >= 20,
|
||||
move(ctx) {
|
||||
if (ctx.round === 0) return 'C';
|
||||
const recentDefects = ctx.opponentHistory.slice(-3).filter((move) => move === 'D').length;
|
||||
return recentDefects >= 2 ? 'D' : 'C';
|
||||
}
|
||||
},
|
||||
{
|
||||
id: 'TIT_FOR_TAT',
|
||||
label: 'TIT FOR TAT',
|
||||
desc: 'Cooperate first, then mirror the opponent.',
|
||||
unlock: () => G.strategicFlag === 1 && G.totalImpact >= 1000,
|
||||
move(ctx) {
|
||||
if (ctx.round === 0) return 'C';
|
||||
return ctx.opponentLast || 'C';
|
||||
}
|
||||
},
|
||||
{
|
||||
id: 'BEAT_LAST',
|
||||
label: 'BEAT LAST',
|
||||
desc: 'Attempts to counter the opponent’s last move.',
|
||||
unlock: () => G.strategicFlag === 1 && G.yomi >= 100,
|
||||
move(ctx) {
|
||||
if (ctx.round === 0) return 'D';
|
||||
return ctx.opponentLast === 'C' ? 'D' : 'C';
|
||||
}
|
||||
},
|
||||
{
|
||||
id: 'MINIMAX',
|
||||
label: 'MINIMAX',
|
||||
desc: 'Optimizes against the opponent’s worst plausible branch.',
|
||||
unlock: () => G.strategicFlag === 1 && G.yomi >= 400,
|
||||
move(ctx) {
|
||||
if (ctx.round === 0) return 'C';
|
||||
const coopRate = ctx.opponentHistory.length === 0
|
||||
? 0.5
|
||||
: ctx.opponentHistory.filter((move) => move === 'C').length / ctx.opponentHistory.length;
|
||||
return coopRate >= 0.6 ? 'C' : 'D';
|
||||
}
|
||||
}
|
||||
];
|
||||
|
||||
function normalizeMove(move) {
|
||||
return move === 'D' ? 'D' : 'C';
|
||||
}
|
||||
|
||||
function payoffFor(aMove, bMove) {
|
||||
return TOURNAMENT_PAYOFFS[aMove + bMove] || TOURNAMENT_PAYOFFS.CC;
|
||||
}
|
||||
|
||||
function makeScoreRow(strategy) {
|
||||
return {
|
||||
id: strategy.id,
|
||||
label: strategy.label,
|
||||
score: 0,
|
||||
wins: 0,
|
||||
matches: 0,
|
||||
cooperation: 0,
|
||||
defection: 0
|
||||
};
|
||||
}
|
||||
|
||||
class StrategyEngine {
|
||||
constructor() {
|
||||
this.currentRecommendation = null;
|
||||
this.matchRounds = 12;
|
||||
}
|
||||
|
||||
getYomiInsightRate() {
|
||||
if (!G.strategicFlag || !G.yomi) return 0;
|
||||
return Math.sqrt(G.yomi) * 0.25;
|
||||
}
|
||||
|
||||
getUnlockedStrategies() {
|
||||
if (!G.strategicFlag) return [];
|
||||
return STRATEGY_TACTICS.filter((strategy) => strategy.unlock());
|
||||
}
|
||||
|
||||
playMatch(strategyA, strategyB) {
|
||||
const historyA = [];
|
||||
const historyB = [];
|
||||
let scoreA = 0;
|
||||
let scoreB = 0;
|
||||
let cooperationMoments = 0;
|
||||
|
||||
for (let round = 0; round < this.matchRounds; round++) {
|
||||
const moveA = normalizeMove(strategyA.move({
|
||||
round,
|
||||
selfHistory: historyA.slice(),
|
||||
opponentHistory: historyB.slice(),
|
||||
selfLast: historyA[historyA.length - 1] || null,
|
||||
opponentLast: historyB[historyB.length - 1] || null
|
||||
}));
|
||||
const moveB = normalizeMove(strategyB.move({
|
||||
round,
|
||||
selfHistory: historyB.slice(),
|
||||
opponentHistory: historyA.slice(),
|
||||
selfLast: historyB[historyB.length - 1] || null,
|
||||
opponentLast: historyA[historyA.length - 1] || null
|
||||
}));
|
||||
|
||||
const [gainA, gainB] = payoffFor(moveA, moveB);
|
||||
scoreA += gainA;
|
||||
scoreB += gainB;
|
||||
if (moveA === 'C') cooperationMoments += 1;
|
||||
if (moveB === 'C') cooperationMoments += 1;
|
||||
historyA.push(moveA);
|
||||
historyB.push(moveB);
|
||||
}
|
||||
|
||||
return {
|
||||
aId: strategyA.id,
|
||||
aLabel: strategyA.label,
|
||||
bId: strategyB.id,
|
||||
bLabel: strategyB.label,
|
||||
scoreA,
|
||||
scoreB,
|
||||
cooperationMoments,
|
||||
winner: scoreA === scoreB ? 'DRAW' : (scoreA > scoreB ? strategyA.id : strategyB.id)
|
||||
};
|
||||
}
|
||||
|
||||
runTournament(isAutomatic = false) {
|
||||
const unlocked = this.getUnlockedStrategies();
|
||||
if (unlocked.length < 2) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const scores = Object.fromEntries(unlocked.map((strategy) => [strategy.id, makeScoreRow(strategy)]));
|
||||
const matches = [];
|
||||
|
||||
for (let i = 0; i < unlocked.length; i++) {
|
||||
for (let j = i + 1; j < unlocked.length; j++) {
|
||||
const match = this.playMatch(unlocked[i], unlocked[j]);
|
||||
matches.push(match);
|
||||
|
||||
const rowA = scores[match.aId];
|
||||
const rowB = scores[match.bId];
|
||||
rowA.score += match.scoreA;
|
||||
rowB.score += match.scoreB;
|
||||
rowA.matches += 1;
|
||||
rowB.matches += 1;
|
||||
rowA.cooperation += this.matchRounds - match.scoreA < this.matchRounds ? match.cooperationMoments / 2 : 0;
|
||||
rowB.cooperation += this.matchRounds - match.scoreB < this.matchRounds ? match.cooperationMoments / 2 : 0;
|
||||
rowA.defection = rowA.matches * this.matchRounds - rowA.cooperation;
|
||||
rowB.defection = rowB.matches * this.matchRounds - rowB.cooperation;
|
||||
|
||||
if (match.winner === match.aId) rowA.wins += 1;
|
||||
if (match.winner === match.bId) rowB.wins += 1;
|
||||
}
|
||||
}
|
||||
|
||||
const scoreboard = Object.values(scores).sort((a, b) => {
|
||||
if (b.score !== a.score) return b.score - a.score;
|
||||
if (b.wins !== a.wins) return b.wins - a.wins;
|
||||
return a.label.localeCompare(b.label);
|
||||
});
|
||||
|
||||
const strategicDepth = matches.reduce((sum, match) => {
|
||||
return sum + Math.abs(match.scoreA - match.scoreB) + match.cooperationMoments;
|
||||
}, 0);
|
||||
const yomiAward = Math.max(10, Math.round(strategicDepth / Math.max(1, unlocked.length)));
|
||||
const knowledgeGain = Math.max(25, Math.round(yomiAward * 2));
|
||||
const leader = scoreboard[0];
|
||||
|
||||
G.totalYomi = Math.max(G.totalYomi || 0, G.yomi || 0) + yomiAward;
|
||||
G.yomi += yomiAward;
|
||||
G.knowledge += knowledgeGain;
|
||||
G.totalKnowledge += knowledgeGain;
|
||||
G.strategyTournamentTimer = 0;
|
||||
G.strategyLastTournament = {
|
||||
leader: { id: leader.id, label: leader.label, score: leader.score, wins: leader.wins },
|
||||
scoreboard: scoreboard.map((row) => ({
|
||||
id: row.id,
|
||||
label: row.label,
|
||||
score: row.score,
|
||||
wins: row.wins,
|
||||
matches: row.matches
|
||||
})),
|
||||
matchCount: matches.length,
|
||||
yomiAward,
|
||||
knowledgeGain,
|
||||
automatic: Boolean(isAutomatic),
|
||||
timestamp: Date.now()
|
||||
};
|
||||
|
||||
if (typeof log === 'function') {
|
||||
log(`Strategy tournament complete. ${leader.label} leads. +${fmt(yomiAward)} Yomi, +${fmt(knowledgeGain)} knowledge.`, true);
|
||||
}
|
||||
if (typeof showToast === 'function') {
|
||||
showToast(`Tournament: ${leader.label} leads (+${fmt(yomiAward)} Yomi)`, 'project', 5000);
|
||||
}
|
||||
|
||||
return G.strategyLastTournament;
|
||||
}
|
||||
|
||||
unlockAutoTournament() {
|
||||
if (!G.strategicFlag) return false;
|
||||
|
||||
if (!G.strategyAutoUnlocked) {
|
||||
if (G.creativity < 50000) {
|
||||
if (typeof log === 'function') log(`Need ${fmt(50000)} creativity to unlock auto-tournaments.`);
|
||||
return false;
|
||||
}
|
||||
G.creativity -= 50000;
|
||||
G.strategyAutoUnlocked = true;
|
||||
G.strategyAutoEnabled = true;
|
||||
G.strategyTournamentTimer = 0;
|
||||
if (typeof log === 'function') log('Auto-Tournament mode unlocked. Yomi generation is now automated.', true);
|
||||
if (typeof showToast === 'function') showToast('Auto-Tournament mode unlocked', 'milestone', 5000);
|
||||
return true;
|
||||
}
|
||||
|
||||
G.strategyAutoEnabled = !G.strategyAutoEnabled;
|
||||
if (typeof log === 'function') {
|
||||
log(`Auto-Tournament mode ${G.strategyAutoEnabled ? 'enabled' : 'disabled'}.`);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
tick(dt) {
|
||||
if (!G.strategicFlag || !G.strategyAutoEnabled) return false;
|
||||
G.strategyTournamentTimer = (G.strategyTournamentTimer || 0) + dt;
|
||||
if (G.strategyTournamentTimer < (G.strategyTournamentInterval || 30)) return false;
|
||||
return this.runTournament(true);
|
||||
}
|
||||
|
||||
getPanelHtml() {
|
||||
if (!G.strategicFlag) {
|
||||
return '<div class="dim">Research the Strategy Engine to unlock tournaments, Yomi, and adversarial modeling.</div>';
|
||||
}
|
||||
|
||||
const unlocked = this.getUnlockedStrategies();
|
||||
const unlockedIds = new Set(unlocked.map((strategy) => strategy.id));
|
||||
const yomiRate = this.getYomiInsightRate();
|
||||
const autoLabel = !G.strategyAutoUnlocked
|
||||
? 'UNLOCK AUTO-TOURNAMENT (50K CREATIVITY)'
|
||||
: (G.strategyAutoEnabled ? 'DISABLE AUTO-TOURNAMENT' : 'ENABLE AUTO-TOURNAMENT');
|
||||
const autoHint = !G.strategyAutoUnlocked
|
||||
? `Creativity: ${fmt(G.creativity)} / ${fmt(50000)}`
|
||||
: `Runs every ${Math.floor(G.strategyTournamentInterval || 30)}s • Timer ${Math.floor(G.strategyTournamentTimer || 0)}s`;
|
||||
|
||||
let chips = '';
|
||||
for (const strategy of STRATEGY_TACTICS) {
|
||||
const unlockedNow = unlockedIds.has(strategy.id);
|
||||
chips += `<span class="milestone-chip${unlockedNow ? ' done' : ''}" style="opacity:${unlockedNow ? '1' : '0.45'}">${strategy.label}</span>`;
|
||||
}
|
||||
|
||||
let scoreboardHtml = '<div class="dim" style="margin-top:8px">Run a tournament to generate Yomi and compare strategies.</div>';
|
||||
if (G.strategyLastTournament && Array.isArray(G.strategyLastTournament.scoreboard) && G.strategyLastTournament.scoreboard.length > 0) {
|
||||
const rows = G.strategyLastTournament.scoreboard
|
||||
.map((row) => `<tr><td style="padding:3px 6px 3px 0;color:#c0c0d0">${row.label}</td><td style="padding:3px 6px;text-align:right;color:#ffd700">${fmt(row.score)}</td><td style="padding:3px 0 3px 6px;text-align:right;color:#4caf50">${row.wins}</td></tr>`)
|
||||
.join('');
|
||||
scoreboardHtml = `
|
||||
<div style="margin-top:10px;padding:8px;border:1px solid var(--border);border-radius:6px;background:#0a0a14">
|
||||
<div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:6px">
|
||||
<span style="color:#ffd700;font-size:10px;letter-spacing:1px">LAST TOURNAMENT</span>
|
||||
<span style="font-size:9px;color:#666">${G.strategyLastTournament.automatic ? 'AUTO' : 'MANUAL'}</span>
|
||||
</div>
|
||||
<div style="font-size:10px;color:#aaa;margin-bottom:6px">LEADER: <span style="color:#ffd700">${G.strategyLastTournament.leader.label}</span> • +${fmt(G.strategyLastTournament.yomiAward)} Yomi • +${fmt(G.strategyLastTournament.knowledgeGain)} knowledge</div>
|
||||
<table style="width:100%;font-size:10px;border-collapse:collapse">
|
||||
<thead>
|
||||
<tr style="color:#555;text-align:left"><th style="padding:0 6px 4px 0">Strategy</th><th style="padding:0 6px 4px;text-align:right">Score</th><th style="padding:0 0 4px 6px;text-align:right">Wins</th></tr>
|
||||
</thead>
|
||||
<tbody>${rows}</tbody>
|
||||
</table>
|
||||
</div>`;
|
||||
}
|
||||
|
||||
return `
|
||||
<div style="margin-top:10px;padding-top:10px;border-top:1px solid var(--border)">
|
||||
<div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(120px,1fr));gap:6px;margin-bottom:8px">
|
||||
<div class="res" style="padding:8px">
|
||||
<div class="r-label">Yomi</div>
|
||||
<div class="r-val" style="font-size:14px">${fmt(G.yomi || 0)}</div>
|
||||
<div class="r-rate">+${fmt(yomiRate)}/s knowledge</div>
|
||||
</div>
|
||||
<div class="res" style="padding:8px">
|
||||
<div class="r-label">Unlocked</div>
|
||||
<div class="r-val" style="font-size:14px">${unlocked.length}/8</div>
|
||||
<div class="r-rate">${G.strategyAutoEnabled ? 'AUTO MODE' : 'MANUAL'}</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="action-btn-group">
|
||||
<button class="ops-btn" onclick="window.SSE.runTournament()" style="border-color:var(--gold);color:var(--gold)" aria-label="Run a strategy tournament to generate Yomi">RUN TOURNAMENT</button>
|
||||
<button class="ops-btn" onclick="window.SSE.unlockAutoTournament()" style="border-color:var(--purple);color:var(--purple)" aria-label="${autoLabel}">${autoLabel}</button>
|
||||
</div>
|
||||
<div style="font-size:9px;color:#666;margin-top:4px">${autoHint}</div>
|
||||
<div style="font-size:9px;color:#888;margin-top:8px;line-height:1.6">Game theory tournaments generate Yomi. Yomi is reinvested as passive knowledge, turning adversarial modeling into insight.</div>
|
||||
<div class="milestone-row" style="justify-content:flex-start;margin-top:8px">${chips}</div>
|
||||
${scoreboardHtml}
|
||||
</div>`;
|
||||
}
|
||||
|
||||
update() {
|
||||
// Find the highest priority rule that meets its condition
|
||||
const activeRules = STRATEGY_RULES.filter(r => r.condition());
|
||||
const activeRules = STRATEGY_RULES.filter((rule) => rule.condition());
|
||||
activeRules.sort((a, b) => b.priority - a.priority);
|
||||
|
||||
if (activeRules.length > 0) {
|
||||
this.currentRecommendation = activeRules[0].recommendation;
|
||||
const top = activeRules[0];
|
||||
this.currentRecommendation = typeof top.recommendation === 'function'
|
||||
? top.recommendation()
|
||||
: top.recommendation;
|
||||
} else if (G.strategicFlag && G.strategyLastTournament && G.strategyLastTournament.leader) {
|
||||
this.currentRecommendation = `Tournament leader: ${G.strategyLastTournament.leader.label}. Yomi is currently generating +${fmt(this.getYomiInsightRate())} knowledge/sec.`;
|
||||
} else {
|
||||
this.currentRecommendation = "System stable. Continue writing code.";
|
||||
this.currentRecommendation = 'System stable. Continue writing code.';
|
||||
}
|
||||
}
|
||||
|
||||
getRecommendation() {
|
||||
return this.currentRecommendation;
|
||||
return this.currentRecommendation || 'System stable. Continue writing code.';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
162
tests/strategy.test.cjs
Normal file
162
tests/strategy.test.cjs
Normal file
@@ -0,0 +1,162 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
const vm = require('node:vm');
|
||||
|
||||
const ROOT = path.resolve(__dirname, '..');
|
||||
|
||||
function loadStrategyHarness() {
|
||||
const storage = new Map();
|
||||
const dummyClassList = {
|
||||
add() {},
|
||||
remove() {},
|
||||
toggle() {},
|
||||
contains() { return false; }
|
||||
};
|
||||
|
||||
const document = {
|
||||
body: { classList: dummyClassList, appendChild() {}, removeChild() {} },
|
||||
head: { appendChild() {} },
|
||||
getElementById() { return null; },
|
||||
createElement() {
|
||||
return {
|
||||
style: {},
|
||||
classList: dummyClassList,
|
||||
appendChild() {},
|
||||
remove() {},
|
||||
setAttribute() {},
|
||||
addEventListener() {},
|
||||
innerHTML: '',
|
||||
textContent: ''
|
||||
};
|
||||
},
|
||||
addEventListener() {},
|
||||
removeEventListener() {},
|
||||
querySelector() { return null; },
|
||||
querySelectorAll() { return []; }
|
||||
};
|
||||
|
||||
const window = {
|
||||
document,
|
||||
addEventListener() {},
|
||||
removeEventListener() {},
|
||||
innerWidth: 1280,
|
||||
innerHeight: 720
|
||||
};
|
||||
|
||||
const context = {
|
||||
console,
|
||||
Math,
|
||||
Date,
|
||||
document,
|
||||
window,
|
||||
performance: { now: () => 0 },
|
||||
setTimeout() { return 1; },
|
||||
clearTimeout() {},
|
||||
localStorage: {
|
||||
getItem(key) { return storage.has(key) ? storage.get(key) : null; },
|
||||
setItem(key, value) { storage.set(key, String(value)); },
|
||||
removeItem(key) { storage.delete(key); }
|
||||
}
|
||||
};
|
||||
|
||||
vm.createContext(context);
|
||||
const source = ['js/data.js', 'js/utils.js', 'js/strategy.js']
|
||||
.map((file) => fs.readFileSync(path.join(ROOT, file), 'utf8'))
|
||||
.join('\n\n');
|
||||
|
||||
vm.runInContext(`${source}
|
||||
log = () => {};
|
||||
showToast = () => {};
|
||||
this.__exports = {
|
||||
G,
|
||||
SSE,
|
||||
StrategyEngine,
|
||||
STRATEGY_TACTICS: typeof STRATEGY_TACTICS !== 'undefined' ? STRATEGY_TACTICS : null
|
||||
};`, context);
|
||||
|
||||
return { ...context.__exports, context };
|
||||
}
|
||||
|
||||
test('strategy engine progressively unlocks tournament tactics and renders visual scoring', () => {
|
||||
const { G, SSE } = loadStrategyHarness();
|
||||
Object.assign(G, {
|
||||
strategicFlag: 1,
|
||||
trust: 30,
|
||||
totalUsers: 1500,
|
||||
totalKnowledge: 20000,
|
||||
totalImpact: 1500,
|
||||
creativity: 60000,
|
||||
knowledge: 0,
|
||||
yomi: 0,
|
||||
totalYomi: 0
|
||||
});
|
||||
|
||||
const early = SSE.getUnlockedStrategies().map((strategy) => strategy.id);
|
||||
assert.ok(early.includes('RANDOM'));
|
||||
assert.ok(early.includes('GREEDY'));
|
||||
assert.ok(early.includes('TIT_FOR_TAT'));
|
||||
assert.ok(!early.includes('MINIMAX'));
|
||||
|
||||
G.yomi = 500;
|
||||
const late = SSE.getUnlockedStrategies().map((strategy) => strategy.id);
|
||||
assert.ok(late.includes('BEAT_LAST'));
|
||||
assert.ok(late.includes('MINIMAX'));
|
||||
|
||||
const result = SSE.runTournament();
|
||||
assert.ok(result, 'expected tournament result');
|
||||
assert.ok(result.scoreboard.length >= 6, 'expected multi-strategy scoreboard');
|
||||
assert.ok(G.yomi > 0, 'expected Yomi reward');
|
||||
assert.ok(G.totalYomi >= G.yomi, 'expected total Yomi tracking');
|
||||
assert.ok(G.knowledge > 0, 'expected knowledge gain from Yomi');
|
||||
assert.equal(G.strategyLastTournament.leader.id, result.leader.id);
|
||||
|
||||
const html = SSE.getPanelHtml();
|
||||
assert.match(html, /TOURNAMENT/i);
|
||||
assert.match(html, /LEADER/i);
|
||||
assert.match(html, /RANDOM/);
|
||||
});
|
||||
|
||||
test('auto-tournament mode requires 50k creativity to unlock', () => {
|
||||
const { G, SSE } = loadStrategyHarness();
|
||||
Object.assign(G, {
|
||||
strategicFlag: 1,
|
||||
creativity: 49999
|
||||
});
|
||||
|
||||
assert.equal(SSE.unlockAutoTournament(), false);
|
||||
assert.equal(G.strategyAutoUnlocked, false);
|
||||
assert.equal(G.strategyAutoEnabled, false);
|
||||
|
||||
G.creativity = 50000;
|
||||
assert.equal(SSE.unlockAutoTournament(), true);
|
||||
assert.equal(G.creativity, 0);
|
||||
assert.equal(G.strategyAutoUnlocked, true);
|
||||
assert.equal(G.strategyAutoEnabled, true);
|
||||
});
|
||||
|
||||
test('auto tournaments fire on the timer once unlocked', () => {
|
||||
const { G, SSE } = loadStrategyHarness();
|
||||
Object.assign(G, {
|
||||
strategicFlag: 1,
|
||||
trust: 25,
|
||||
totalUsers: 1500,
|
||||
totalKnowledge: 22000,
|
||||
totalImpact: 1500,
|
||||
creativity: 50000,
|
||||
knowledge: 0,
|
||||
yomi: 0,
|
||||
totalYomi: 0
|
||||
});
|
||||
|
||||
SSE.unlockAutoTournament();
|
||||
G.strategyTournamentTimer = G.strategyTournamentInterval - 0.25;
|
||||
const beforeYomi = G.yomi;
|
||||
|
||||
const ran = SSE.tick(1);
|
||||
|
||||
assert.equal(Boolean(ran), true);
|
||||
assert.ok(G.yomi > beforeYomi, 'expected auto tournament Yomi reward');
|
||||
assert.ok(G.strategyLastTournament, 'expected auto tournament summary');
|
||||
});
|
||||
Reference in New Issue
Block a user