mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-21 11:10:42 +00:00
1ecd6c4ee1
Resolve #1652 1. Add the ability to toggle **gold donations** and **troop donations** for private lobbies ~2. Add relevant translations.~ 3. Refactor `canDonate` to be specific to gold and troop donations 4. Add placeholders for singleplayer mode if this is to be extended to support that too. 5. Add Tests for Donate logic <img width="1643" height="1788" alt="image" src="https://github.com/user-attachments/assets/82b93400-a1f0-45f0-8b2b-a7f78dc0c3e9" /> _Private Lobby_  _Testing Troop Send In Private Lobby_  _Troop Send Complete In Private Lobby_  Confirming that public teams still works - [X] I have added screenshots for all UI updates - [X] I process any text displayed to the user through translateText() and I've added it to the en.json file - [X] I have added relevant tests to the test directory - [X] I confirm I have thoroughly tested these changes and take full responsibility for any bugs introduced - [X] I have read and accepted the CLA agreement (only required once). regression is found: DISCORD_USERNAME: cool_clarky --------- Co-authored-by: Scott Anderson <662325+scottanderson@users.noreply.github.com> Co-authored-by: Drills Kibo <59177241+drillskibo@users.noreply.github.com>
106 lines
2.6 KiB
TypeScript
106 lines
2.6 KiB
TypeScript
import { Logger } from "winston";
|
|
import { ServerConfig } from "../core/configuration/Config";
|
|
import { Difficulty, GameMapType, GameMode, GameType } from "../core/game/Game";
|
|
import { GameConfig, GameID } from "../core/Schemas";
|
|
import { Client } from "./Client";
|
|
import { GamePhase, GameServer } from "./GameServer";
|
|
|
|
export class GameManager {
|
|
private games: Map<GameID, GameServer> = new Map();
|
|
|
|
constructor(
|
|
private config: ServerConfig,
|
|
private log: Logger,
|
|
) {
|
|
setInterval(() => this.tick(), 1000);
|
|
}
|
|
|
|
public game(id: GameID): GameServer | null {
|
|
return this.games.get(id) ?? null;
|
|
}
|
|
|
|
addClient(client: Client, gameID: GameID, lastTurn: number): boolean {
|
|
const game = this.games.get(gameID);
|
|
if (game) {
|
|
game.addClient(client, lastTurn);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
createGame(
|
|
id: GameID,
|
|
gameConfig: GameConfig | undefined,
|
|
creatorClientID?: string,
|
|
) {
|
|
const game = new GameServer(
|
|
id,
|
|
this.log,
|
|
Date.now(),
|
|
this.config,
|
|
{
|
|
donateGold: false,
|
|
donateTroops: false,
|
|
gameMap: GameMapType.World,
|
|
gameType: GameType.Private,
|
|
difficulty: Difficulty.Medium,
|
|
disableNPCs: false,
|
|
infiniteGold: false,
|
|
infiniteTroops: false,
|
|
instantBuild: false,
|
|
gameMode: GameMode.FFA,
|
|
bots: 400,
|
|
disabledUnits: [],
|
|
...gameConfig,
|
|
},
|
|
creatorClientID,
|
|
);
|
|
this.games.set(id, game);
|
|
return game;
|
|
}
|
|
|
|
activeGames(): number {
|
|
return this.games.size;
|
|
}
|
|
|
|
activeClients(): number {
|
|
let totalClients = 0;
|
|
this.games.forEach((game: GameServer) => {
|
|
totalClients += game.activeClients.length;
|
|
});
|
|
return totalClients;
|
|
}
|
|
|
|
tick() {
|
|
const active = new Map<GameID, GameServer>();
|
|
for (const [id, game] of this.games) {
|
|
const phase = game.phase();
|
|
if (phase === GamePhase.Active) {
|
|
if (!game.hasStarted()) {
|
|
// Prestart tells clients to start loading the game.
|
|
game.prestart();
|
|
// Start game on delay to allow time for clients to connect.
|
|
setTimeout(() => {
|
|
try {
|
|
game.start();
|
|
} catch (error) {
|
|
this.log.error(`error starting game ${id}: ${error}`);
|
|
}
|
|
}, 2000);
|
|
}
|
|
}
|
|
|
|
if (phase === GamePhase.Finished) {
|
|
try {
|
|
game.end();
|
|
} catch (error) {
|
|
this.log.error(`error ending game ${id}: ${error}`);
|
|
}
|
|
} else {
|
|
active.set(id, game);
|
|
}
|
|
}
|
|
this.games = active;
|
|
}
|
|
}
|