Add complete game loop: training, revenue, market, offline catch-up
- Model training system: training jobs produce TrainedModels with calculated capabilities based on compute, data, and research - Market system: organic API demand and consumer subscriptions now generate real revenue from deployed models - Talent system: salary costs calculated from department headcount - Toast notification system for game events (training complete, etc.) - Offline catch-up: progress bar + summary screen when returning - Market page: pricing controls for API and subscription products - Finance page: income statement, cash charts, funding history - Tick processor now runs all 7 systems in correct dependency order Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,2 +1,3 @@
|
||||
export { GameEngine } from './engine';
|
||||
export { processTick } from './tick';
|
||||
export type { TickNotification } from './tick';
|
||||
|
||||
@@ -10,6 +10,7 @@ export interface MarketTickResult {
|
||||
marketState: MarketState;
|
||||
apiRevenue: number;
|
||||
subscriptionRevenue: number;
|
||||
totalTokenDemand: number;
|
||||
}
|
||||
|
||||
export function processMarket(state: GameState, compute: ComputeState): MarketTickResult {
|
||||
@@ -21,40 +22,68 @@ export function processMarket(state: GameState, compute: ComputeState): MarketTi
|
||||
const chatProduct = state.models.productLines.find(p => p.type === 'chat-product');
|
||||
const textApi = state.models.productLines.find(p => p.type === 'text-api');
|
||||
|
||||
// --- Consumer market (subscription product) ---
|
||||
const consumers = { ...state.market.consumers };
|
||||
let subscriptionRevenue = 0;
|
||||
|
||||
if (chatProduct?.isActive && bestModel) {
|
||||
const growthRate = CONSUMER_BASE_GROWTH + modelQuality * CONSUMER_QUALITY_GROWTH_MULTIPLIER;
|
||||
const churnRate = CONSUMER_BASE_CHURN * (1 + (1 - consumers.satisfaction));
|
||||
const priceAttractiveness = Math.max(0, 1 - chatProduct.pricing.subscriptionPrice / 100);
|
||||
const growthRate = (CONSUMER_BASE_GROWTH + modelQuality * CONSUMER_QUALITY_GROWTH_MULTIPLIER) * (0.5 + priceAttractiveness * 0.5);
|
||||
const churnRate = CONSUMER_BASE_CHURN * (1 + (1 - consumers.satisfaction) * 2);
|
||||
|
||||
consumers.growthRatePerTick = growthRate;
|
||||
consumers.churnRatePerTick = churnRate;
|
||||
|
||||
const newSubs = consumers.totalSubscribers * growthRate;
|
||||
const lostSubs = consumers.totalSubscribers * churnRate;
|
||||
consumers.totalSubscribers = Math.max(0, consumers.totalSubscribers + newSubs - lostSubs);
|
||||
|
||||
if (consumers.totalSubscribers < 10 && modelQuality > 0) {
|
||||
consumers.totalSubscribers += 1;
|
||||
if (consumers.totalSubscribers < 50 && modelQuality > 0.1) {
|
||||
consumers.totalSubscribers += 2 + modelQuality * 5;
|
||||
}
|
||||
|
||||
const loadPenalty = compute.inferenceUtilization > 0.9
|
||||
? (compute.inferenceUtilization - 0.9) * 5
|
||||
: 0;
|
||||
consumers.satisfaction = Math.min(1, Math.max(0,
|
||||
0.3 + modelQuality * 0.5 + (1 - compute.inferenceUtilization) * 0.2,
|
||||
0.3 + modelQuality * 0.5 + (1 - Math.min(1, compute.inferenceUtilization)) * 0.2 - loadPenalty,
|
||||
));
|
||||
|
||||
consumers.viralCoefficient = modelQuality > 0.5 ? 1 + (modelQuality - 0.5) * 2 : 0;
|
||||
|
||||
subscriptionRevenue = consumers.totalSubscribers * (chatProduct.pricing.subscriptionPrice / 2592000);
|
||||
}
|
||||
|
||||
const subscriptionRevenue = chatProduct?.isActive
|
||||
? consumers.totalSubscribers * (chatProduct.pricing.subscriptionPrice / 30 / 24 / 3600)
|
||||
: 0;
|
||||
|
||||
// --- B2B API market (organic demand based on model quality + reputation) ---
|
||||
const enterprise = { ...state.market.enterprise };
|
||||
let apiRevenue = 0;
|
||||
let organicApiTokens = 0;
|
||||
|
||||
if (textApi?.isActive && bestModel) {
|
||||
let totalTokens = 0;
|
||||
const reputationFactor = state.reputation.score / 100;
|
||||
const qualityFactor = modelQuality;
|
||||
const priceFactor = Math.max(0.1, 1 - (textApi.pricing.outputTokenPrice / 20));
|
||||
|
||||
organicApiTokens = Math.floor(
|
||||
qualityFactor * reputationFactor * priceFactor * 50000 * (1 + state.meta.tickCount * 0.0001),
|
||||
);
|
||||
|
||||
let contractTokens = 0;
|
||||
for (const contract of enterprise.activeContracts) {
|
||||
totalTokens += contract.tokensPerTick;
|
||||
contractTokens += contract.tokensPerTick;
|
||||
apiRevenue += (contract.tokensPerTick / 1_000_000) * contract.pricePerMToken;
|
||||
}
|
||||
enterprise.totalApiCallsPerTick = totalTokens / API_TOKENS_PER_REQUEST;
|
||||
|
||||
const totalApiTokens = organicApiTokens + contractTokens;
|
||||
apiRevenue += (organicApiTokens / 1_000_000) * textApi.pricing.outputTokenPrice;
|
||||
|
||||
enterprise.totalApiCallsPerTick = totalApiTokens / API_TOKENS_PER_REQUEST;
|
||||
}
|
||||
|
||||
const totalTokenDemand = organicApiTokens +
|
||||
consumers.totalSubscribers * 100 +
|
||||
enterprise.activeContracts.reduce((s, c) => s + c.tokensPerTick, 0);
|
||||
|
||||
return {
|
||||
marketState: {
|
||||
...state.market,
|
||||
@@ -63,5 +92,6 @@ export function processMarket(state: GameState, compute: ComputeState): MarketTi
|
||||
},
|
||||
apiRevenue,
|
||||
subscriptionRevenue,
|
||||
totalTokenDemand,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
import type { GameState, ModelsState, TrainedModel, ModelCapabilities } from '@ai-tycoon/shared';
|
||||
|
||||
export interface ModelTickResult {
|
||||
modelsState: ModelsState;
|
||||
modelCompleted: TrainedModel | null;
|
||||
}
|
||||
|
||||
export function processModels(state: GameState): ModelTickResult {
|
||||
const active = state.models.activeTraining;
|
||||
if (!active) {
|
||||
return { modelsState: state.models, modelCompleted: null };
|
||||
}
|
||||
|
||||
const researcherBoost = state.talent.departments.research.headcount *
|
||||
state.talent.departments.research.effectiveness;
|
||||
const engineerBoost = state.talent.departments.engineering.headcount *
|
||||
state.talent.departments.engineering.effectiveness;
|
||||
const speedMultiplier = 1 + (researcherBoost + engineerBoost) * 0.05;
|
||||
|
||||
const newProgress = active.progressTicks + speedMultiplier;
|
||||
|
||||
if (newProgress >= active.totalTicks) {
|
||||
const model = createTrainedModel(active.modelName, active.generation, active.allocatedCompute, active.allocatedDataTokens, state);
|
||||
|
||||
return {
|
||||
modelsState: {
|
||||
...state.models,
|
||||
trainedModels: [...state.models.trainedModels, model],
|
||||
activeTraining: null,
|
||||
},
|
||||
modelCompleted: model,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
modelsState: {
|
||||
...state.models,
|
||||
activeTraining: { ...active, progressTicks: newProgress },
|
||||
},
|
||||
modelCompleted: null,
|
||||
};
|
||||
}
|
||||
|
||||
function createTrainedModel(
|
||||
name: string,
|
||||
generation: number,
|
||||
compute: number,
|
||||
dataTokens: number,
|
||||
state: GameState,
|
||||
): TrainedModel {
|
||||
const computeFactor = Math.log10(1 + compute) * 15;
|
||||
const dataFactor = Math.log10(1 + dataTokens / 1e8) * 10;
|
||||
const researchBonus = state.research.completedResearch.length * 3;
|
||||
const efficiencyBonus = state.research.completedResearch.filter(r => r.includes('efficiency')).length * 5;
|
||||
|
||||
const baseCapability = Math.min(95, computeFactor + dataFactor + researchBonus + efficiencyBonus);
|
||||
|
||||
const researcherQuality = state.talent.departments.research.effectiveness;
|
||||
const capabilities: ModelCapabilities = {
|
||||
reasoning: clamp(baseCapability * (0.8 + Math.random() * 0.4) * (1 + researcherQuality * 0.2)),
|
||||
coding: clamp(baseCapability * (0.7 + Math.random() * 0.5)),
|
||||
creative: clamp(baseCapability * (0.6 + Math.random() * 0.6)),
|
||||
multimodal: clamp(baseCapability * (0.3 + Math.random() * 0.3)),
|
||||
agents: clamp(baseCapability * (0.2 + Math.random() * 0.3)),
|
||||
speed: Math.max(1, 100 - compute * 0.5 + efficiencyBonus * 2),
|
||||
};
|
||||
|
||||
const benchmarkScore = (capabilities.reasoning * 0.3 + capabilities.coding * 0.25 +
|
||||
capabilities.creative * 0.2 + capabilities.multimodal * 0.15 + capabilities.agents * 0.1);
|
||||
|
||||
const safetyScore = 50 + Math.random() * 20;
|
||||
|
||||
const parameterCount = Math.pow(10, generation) * (0.5 + Math.random());
|
||||
|
||||
return {
|
||||
id: crypto.randomUUID(),
|
||||
name,
|
||||
generation,
|
||||
parameterCount,
|
||||
trainingDataSize: dataTokens,
|
||||
capabilities,
|
||||
safetyScore,
|
||||
benchmarkScore,
|
||||
tuning: { preset: 'helpful-safe' },
|
||||
isDeployed: false,
|
||||
trainedAtTick: state.meta.tickCount,
|
||||
};
|
||||
}
|
||||
|
||||
function clamp(n: number): number {
|
||||
return Math.min(100, Math.max(0, n));
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import type { GameState, TalentState } from '@ai-tycoon/shared';
|
||||
|
||||
const SALARY_PER_HEADCOUNT_PER_TICK = 0.03;
|
||||
|
||||
export function processTalent(state: GameState): TalentState {
|
||||
const departments = { ...state.talent.departments };
|
||||
|
||||
let totalSalary = 0;
|
||||
for (const [id, dept] of Object.entries(departments)) {
|
||||
totalSalary += dept.headcount * SALARY_PER_HEADCOUNT_PER_TICK;
|
||||
totalSalary += dept.budget / 2592000;
|
||||
}
|
||||
|
||||
for (const hire of state.talent.keyHires) {
|
||||
totalSalary += hire.salary / 2592000;
|
||||
}
|
||||
|
||||
return {
|
||||
...state.talent,
|
||||
totalSalaryPerTick: totalSalary,
|
||||
};
|
||||
}
|
||||
@@ -3,20 +3,56 @@ import { processEconomy } from './systems/economySystem';
|
||||
import { processInfrastructure } from './systems/infrastructureSystem';
|
||||
import { processCompute } from './systems/computeSystem';
|
||||
import { processResearch } from './systems/researchSystem';
|
||||
import { processModels } from './systems/modelSystem';
|
||||
import { processMarket } from './systems/marketSystem';
|
||||
import { processReputation } from './systems/reputationSystem';
|
||||
import { processTalent } from './systems/talentSystem';
|
||||
|
||||
export interface TickResult {
|
||||
state: Partial<GameState>;
|
||||
notifications: TickNotification[];
|
||||
}
|
||||
|
||||
export interface TickNotification {
|
||||
title: string;
|
||||
message: string;
|
||||
type: 'info' | 'success' | 'warning' | 'danger';
|
||||
}
|
||||
|
||||
export function processTick(state: GameState): Partial<GameState> {
|
||||
const notifications: TickNotification[] = [];
|
||||
|
||||
const infrastructure = processInfrastructure(state);
|
||||
|
||||
const stateWithInfra = { ...state, infrastructure };
|
||||
const modelResult = processModels(stateWithInfra);
|
||||
|
||||
if (modelResult.modelCompleted) {
|
||||
notifications.push({
|
||||
title: 'Training Complete',
|
||||
message: `${modelResult.modelCompleted.name} is ready! Benchmark: ${modelResult.modelCompleted.benchmarkScore.toFixed(1)}/100`,
|
||||
type: 'success',
|
||||
});
|
||||
}
|
||||
|
||||
const stateWithModels = { ...stateWithInfra, models: modelResult.modelsState };
|
||||
const market = processMarket(stateWithModels, state.compute);
|
||||
|
||||
const compute = processCompute(state, infrastructure);
|
||||
const research = processResearch(state, compute);
|
||||
const market = processMarket(state, compute);
|
||||
const reputation = processReputation(state);
|
||||
const economy = processEconomy(state, market, infrastructure);
|
||||
compute.tokensPerSecondDemand = market.totalTokenDemand;
|
||||
compute.inferenceUtilization = compute.tokensPerSecondCapacity > 0
|
||||
? Math.min(1, market.totalTokenDemand / compute.tokensPerSecondCapacity)
|
||||
: 0;
|
||||
|
||||
const talent = processTalent(stateWithModels);
|
||||
const stateWithTalent = { ...stateWithModels, talent };
|
||||
const research = processResearch(stateWithTalent, compute);
|
||||
const reputation = processReputation(stateWithTalent);
|
||||
const economy = processEconomy(stateWithTalent, market, infrastructure);
|
||||
|
||||
const tickCount = state.meta.tickCount + 1;
|
||||
|
||||
return {
|
||||
const result: Partial<GameState> = {
|
||||
meta: {
|
||||
...state.meta,
|
||||
tickCount,
|
||||
@@ -27,7 +63,13 @@ export function processTick(state: GameState): Partial<GameState> {
|
||||
infrastructure,
|
||||
compute,
|
||||
research,
|
||||
models: modelResult.modelsState,
|
||||
market: market.marketState,
|
||||
talent,
|
||||
reputation,
|
||||
};
|
||||
|
||||
(result as Record<string, unknown>)['_notifications'] = notifications;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user