mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-07-28 13:00:29 +00:00
All players must join game before spawn (#380)
## Description: The server stores all players that have joined, and once the game starts it sends a list of players to all clients. Players cannot join after the game has started. Server now generated the PlayerID instead of the client. The is necessary for team mode, we need to know how who is playing the game before it starts so we can properly assign teams based on clans. ## Please complete the following: - [x] I have added screenshots for all UI updates - [x] I confirm I have thoroughly tested these changes and take full responsibility for any bugs introduced - [x] I understand that submitting code with bugs that could have been caught through manual testing blocks releases and new features for all contributors ## Please put your Discord username so you can be contacted if a bug or regression is found: evan
This commit is contained in:
+17
-9
@@ -7,10 +7,8 @@ import { WinCheckExecution } from "./execution/WinCheckExecution";
|
||||
import {
|
||||
AllPlayers,
|
||||
BuildableUnit,
|
||||
Cell,
|
||||
Game,
|
||||
GameUpdates,
|
||||
MessageType,
|
||||
Player,
|
||||
PlayerActions,
|
||||
PlayerID,
|
||||
@@ -18,24 +16,34 @@ import {
|
||||
PlayerBorderTiles,
|
||||
PlayerType,
|
||||
UnitType,
|
||||
PlayerInfo,
|
||||
} from "./game/Game";
|
||||
import { DisplayMessageUpdate, ErrorUpdate } from "./game/GameUpdates";
|
||||
import { ErrorUpdate } from "./game/GameUpdates";
|
||||
import { NameViewData } from "./game/Game";
|
||||
import { GameUpdateType } from "./game/GameUpdates";
|
||||
import { createGame } from "./game/GameImpl";
|
||||
import { loadTerrainMap as loadGameMap } from "./game/TerrainMapLoader";
|
||||
import { ClientID, GameConfig, Turn } from "./Schemas";
|
||||
import { ClientID, GameStartInfo, Turn } from "./Schemas";
|
||||
import { GameUpdateViewData } from "./game/GameUpdates";
|
||||
|
||||
export async function createGameRunner(
|
||||
gameID: string,
|
||||
gameConfig: GameConfig,
|
||||
gameStart: GameStartInfo,
|
||||
clientID: ClientID,
|
||||
callBack: (gu: GameUpdateViewData) => void,
|
||||
): Promise<GameRunner> {
|
||||
const config = await getConfig(gameConfig, null);
|
||||
const gameMap = await loadGameMap(gameConfig.gameMap);
|
||||
const config = await getConfig(gameStart.config, null);
|
||||
const gameMap = await loadGameMap(gameStart.config.gameMap);
|
||||
const game = createGame(
|
||||
gameStart.players.map(
|
||||
(p) =>
|
||||
new PlayerInfo(
|
||||
p.flag,
|
||||
p.username,
|
||||
PlayerType.Human,
|
||||
p.clientID,
|
||||
p.playerID,
|
||||
),
|
||||
),
|
||||
gameMap.gameMap,
|
||||
gameMap.miniGameMap,
|
||||
gameMap.nationMap,
|
||||
@@ -43,7 +51,7 @@ export async function createGameRunner(
|
||||
);
|
||||
const gr = new GameRunner(
|
||||
game as Game,
|
||||
new Executor(game, gameID, clientID),
|
||||
new Executor(game, gameStart.gameID, clientID),
|
||||
callBack,
|
||||
);
|
||||
gr.init();
|
||||
|
||||
+17
-17
@@ -83,7 +83,8 @@ export type GameRecord = z.infer<typeof GameRecordSchema>;
|
||||
|
||||
export type AllPlayersStats = z.infer<typeof AllPlayersStatsSchema>;
|
||||
export type PlayerStats = z.infer<typeof PlayerStatsSchema>;
|
||||
|
||||
export type Player = z.infer<typeof PlayerSchema>;
|
||||
export type GameStartInfo = z.infer<typeof GameStartInfoSchema>;
|
||||
const PlayerTypeSchema = z.nativeEnum(PlayerType);
|
||||
|
||||
export interface GameInfo {
|
||||
@@ -173,12 +174,10 @@ const BaseIntentSchema = z.object({
|
||||
"move_warship",
|
||||
]),
|
||||
clientID: ID,
|
||||
playerID: ID,
|
||||
});
|
||||
|
||||
export const AttackIntentSchema = BaseIntentSchema.extend({
|
||||
type: z.literal("attack"),
|
||||
playerID: ID,
|
||||
targetID: ID.nullable(),
|
||||
troops: z.number().nullable(),
|
||||
});
|
||||
@@ -186,7 +185,6 @@ export const AttackIntentSchema = BaseIntentSchema.extend({
|
||||
export const SpawnIntentSchema = BaseIntentSchema.extend({
|
||||
flag: z.string().nullable(),
|
||||
type: z.literal("spawn"),
|
||||
playerID: ID,
|
||||
name: SafeString,
|
||||
playerType: PlayerTypeSchema,
|
||||
x: z.number(),
|
||||
@@ -195,7 +193,6 @@ export const SpawnIntentSchema = BaseIntentSchema.extend({
|
||||
|
||||
export const BoatAttackIntentSchema = BaseIntentSchema.extend({
|
||||
type: z.literal("boat"),
|
||||
playerID: ID,
|
||||
targetID: ID.nullable(),
|
||||
troops: z.number().nullable(),
|
||||
x: z.number(),
|
||||
@@ -204,59 +201,50 @@ export const BoatAttackIntentSchema = BaseIntentSchema.extend({
|
||||
|
||||
export const AllianceRequestIntentSchema = BaseIntentSchema.extend({
|
||||
type: z.literal("allianceRequest"),
|
||||
playerID: ID,
|
||||
recipient: ID,
|
||||
});
|
||||
|
||||
export const AllianceRequestReplyIntentSchema = BaseIntentSchema.extend({
|
||||
type: z.literal("allianceRequestReply"),
|
||||
requestor: ID, // The one who made the original alliance request
|
||||
playerID: ID,
|
||||
accept: z.boolean(),
|
||||
});
|
||||
|
||||
export const BreakAllianceIntentSchema = BaseIntentSchema.extend({
|
||||
type: z.literal("breakAlliance"),
|
||||
playerID: ID,
|
||||
recipient: ID,
|
||||
});
|
||||
|
||||
export const TargetPlayerIntentSchema = BaseIntentSchema.extend({
|
||||
type: z.literal("targetPlayer"),
|
||||
playerID: ID,
|
||||
target: ID,
|
||||
});
|
||||
|
||||
export const EmojiIntentSchema = BaseIntentSchema.extend({
|
||||
type: z.literal("emoji"),
|
||||
playerID: ID,
|
||||
recipient: z.union([ID, z.literal(AllPlayers)]),
|
||||
emoji: EmojiSchema,
|
||||
});
|
||||
|
||||
export const EmbargoIntentSchema = BaseIntentSchema.extend({
|
||||
type: z.literal("embargo"),
|
||||
playerID: ID,
|
||||
targetID: ID,
|
||||
action: z.union([z.literal("start"), z.literal("stop")]),
|
||||
});
|
||||
|
||||
export const DonateIntentSchema = BaseIntentSchema.extend({
|
||||
type: z.literal("donate"),
|
||||
playerID: ID,
|
||||
recipient: ID,
|
||||
troops: z.number().nullable(),
|
||||
});
|
||||
|
||||
export const TargetTroopRatioIntentSchema = BaseIntentSchema.extend({
|
||||
type: z.literal("troop_ratio"),
|
||||
playerID: ID,
|
||||
ratio: z.number().min(0).max(1),
|
||||
});
|
||||
|
||||
export const BuildUnitIntentSchema = BaseIntentSchema.extend({
|
||||
type: z.literal("build_unit"),
|
||||
playerID: ID,
|
||||
unit: z.nativeEnum(UnitType),
|
||||
x: z.number(),
|
||||
y: z.number(),
|
||||
@@ -264,7 +252,6 @@ export const BuildUnitIntentSchema = BaseIntentSchema.extend({
|
||||
|
||||
export const CancelAttackIntentSchema = BaseIntentSchema.extend({
|
||||
type: z.literal("cancel_attack"),
|
||||
playerID: ID,
|
||||
attackID: z.string(),
|
||||
});
|
||||
|
||||
@@ -314,11 +301,24 @@ export const ServerPingMessageSchema = ServerBaseMessageSchema.extend({
|
||||
type: z.literal("ping"),
|
||||
});
|
||||
|
||||
export const PlayerSchema = z.object({
|
||||
playerID: ID,
|
||||
clientID: ID,
|
||||
username: SafeString,
|
||||
flag: SafeString.optional(),
|
||||
});
|
||||
|
||||
export const GameStartInfoSchema = z.object({
|
||||
gameID: ID,
|
||||
config: GameConfigSchema,
|
||||
players: z.array(PlayerSchema),
|
||||
});
|
||||
|
||||
export const ServerStartGameMessageSchema = ServerBaseMessageSchema.extend({
|
||||
type: z.literal("start"),
|
||||
// Turns the client missed if they are late to the game.
|
||||
turns: z.array(TurnSchema),
|
||||
config: GameConfigSchema,
|
||||
gameStartInfo: GameStartInfoSchema,
|
||||
});
|
||||
|
||||
export const ServerDesyncSchema = ServerBaseMessageSchema.extend({
|
||||
@@ -400,7 +400,7 @@ export const PlayerRecordSchema = z.object({
|
||||
|
||||
export const GameRecordSchema = z.object({
|
||||
id: ID,
|
||||
gameConfig: GameConfigSchema,
|
||||
gameStartInfo: GameStartInfoSchema,
|
||||
players: z.array(PlayerRecordSchema),
|
||||
startTimestampMS: z.number(),
|
||||
endTimestampMS: z.number(),
|
||||
|
||||
+3
-2
@@ -8,6 +8,7 @@ import {
|
||||
GameConfig,
|
||||
GameID,
|
||||
GameRecord,
|
||||
GameStartInfo,
|
||||
PlayerRecord,
|
||||
PlayerStats,
|
||||
Turn,
|
||||
@@ -249,7 +250,7 @@ export function onlyImages(html: string) {
|
||||
|
||||
export function createGameRecord(
|
||||
id: GameID,
|
||||
gameConfig: GameConfig,
|
||||
gameStart: GameStartInfo,
|
||||
// username does not need to be set.
|
||||
players: PlayerRecord[],
|
||||
turns: Turn[],
|
||||
@@ -261,7 +262,7 @@ export function createGameRecord(
|
||||
): GameRecord {
|
||||
const record: GameRecord = {
|
||||
id: id,
|
||||
gameConfig: gameConfig,
|
||||
gameStartInfo: gameStart,
|
||||
startTimestampMS: start,
|
||||
endTimestampMS: end,
|
||||
date: new Date().toISOString().split("T")[0],
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
import { consolex } from "../Consolex";
|
||||
import { Cell, Game, PlayerType } from "../game/Game";
|
||||
import { Cell, Game, PlayerInfo, PlayerType } from "../game/Game";
|
||||
import { TileRef } from "../game/GameMap";
|
||||
import { PseudoRandom } from "../PseudoRandom";
|
||||
import { GameID, SpawnIntent } from "../Schemas";
|
||||
import { simpleHash } from "../Util";
|
||||
import { SpawnExecution } from "./SpawnExecution";
|
||||
import { BOT_NAME_PREFIXES, BOT_NAME_SUFFIXES } from "./utils/BotNames";
|
||||
|
||||
export class BotSpawner {
|
||||
private random: PseudoRandom;
|
||||
private bots: SpawnIntent[] = [];
|
||||
private bots: SpawnExecution[] = [];
|
||||
|
||||
constructor(
|
||||
private gs: Game,
|
||||
@@ -17,7 +18,7 @@ export class BotSpawner {
|
||||
this.random = new PseudoRandom(simpleHash(gameID));
|
||||
}
|
||||
|
||||
spawnBots(numBots: number): SpawnIntent[] {
|
||||
spawnBots(numBots: number): SpawnExecution[] {
|
||||
let tries = 0;
|
||||
while (this.bots.length < numBots) {
|
||||
if (tries > 10000) {
|
||||
@@ -35,24 +36,20 @@ export class BotSpawner {
|
||||
return this.bots;
|
||||
}
|
||||
|
||||
spawnBot(botName: string): SpawnIntent | null {
|
||||
spawnBot(botName: string): SpawnExecution | null {
|
||||
const tile = this.randTile();
|
||||
if (!this.gs.isLand(tile)) {
|
||||
return null;
|
||||
}
|
||||
for (const spawn of this.bots) {
|
||||
if (this.gs.manhattanDist(this.gs.ref(spawn.x, spawn.y), tile) < 30) {
|
||||
if (this.gs.manhattanDist(spawn.tile, tile) < 30) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return {
|
||||
type: "spawn",
|
||||
playerID: this.random.nextID(),
|
||||
name: botName,
|
||||
playerType: PlayerType.Bot,
|
||||
x: this.gs.x(tile),
|
||||
y: this.gs.y(tile),
|
||||
};
|
||||
return new SpawnExecution(
|
||||
new PlayerInfo("", botName, PlayerType.Bot, null, this.random.nextID()),
|
||||
tile,
|
||||
);
|
||||
}
|
||||
|
||||
private randomBotName(): string {
|
||||
|
||||
@@ -56,34 +56,24 @@ export class Executor {
|
||||
}
|
||||
|
||||
createExec(intent: Intent): Execution {
|
||||
let player: Player;
|
||||
if (intent.type != "spawn") {
|
||||
if (!this.mg.hasPlayer(intent.playerID)) {
|
||||
console.warn(
|
||||
`player ${intent.playerID} not found on intent ${intent.type}`,
|
||||
);
|
||||
return new NoOpExecution();
|
||||
}
|
||||
player = this.mg.player(intent.playerID);
|
||||
if (player.clientID() != intent.clientID) {
|
||||
console.warn(
|
||||
`intent ${intent.type} has incorrect clientID ${intent.clientID} for player ${player.name()} with clientID ${player.clientID()}`,
|
||||
);
|
||||
return new NoOpExecution();
|
||||
}
|
||||
const player = this.mg.playerByClientID(intent.clientID);
|
||||
if (!player) {
|
||||
console.warn(`player with clientID ${intent.clientID} not found`);
|
||||
return new NoOpExecution();
|
||||
}
|
||||
const playerID = player.id();
|
||||
|
||||
switch (intent.type) {
|
||||
case "attack": {
|
||||
return new AttackExecution(
|
||||
intent.troops,
|
||||
intent.playerID,
|
||||
playerID,
|
||||
intent.targetID,
|
||||
null,
|
||||
);
|
||||
}
|
||||
case "cancel_attack":
|
||||
return new RetreatExecution(intent.playerID, intent.attackID);
|
||||
return new RetreatExecution(playerID, intent.attackID);
|
||||
case "move_warship":
|
||||
return new MoveWarshipExecution(intent.unitId, intent.tile);
|
||||
case "spawn":
|
||||
@@ -94,50 +84,42 @@ export class Executor {
|
||||
intent.clientID == this.clientID
|
||||
? sanitize(intent.name)
|
||||
: fixProfaneUsername(sanitize(intent.name)),
|
||||
intent.playerType,
|
||||
PlayerType.Human,
|
||||
intent.clientID,
|
||||
intent.playerID,
|
||||
playerID,
|
||||
),
|
||||
this.mg.ref(intent.x, intent.y),
|
||||
);
|
||||
case "boat":
|
||||
return new TransportShipExecution(
|
||||
intent.playerID,
|
||||
playerID,
|
||||
intent.targetID,
|
||||
this.mg.ref(intent.x, intent.y),
|
||||
intent.troops,
|
||||
);
|
||||
case "allianceRequest":
|
||||
return new AllianceRequestExecution(intent.playerID, intent.recipient);
|
||||
return new AllianceRequestExecution(playerID, intent.recipient);
|
||||
case "allianceRequestReply":
|
||||
return new AllianceRequestReplyExecution(
|
||||
intent.requestor,
|
||||
intent.playerID,
|
||||
playerID,
|
||||
intent.accept,
|
||||
);
|
||||
case "breakAlliance":
|
||||
return new BreakAllianceExecution(intent.playerID, intent.recipient);
|
||||
return new BreakAllianceExecution(playerID, intent.recipient);
|
||||
case "targetPlayer":
|
||||
return new TargetPlayerExecution(intent.playerID, intent.target);
|
||||
return new TargetPlayerExecution(playerID, intent.target);
|
||||
case "emoji":
|
||||
return new EmojiExecution(
|
||||
intent.playerID,
|
||||
intent.recipient,
|
||||
intent.emoji,
|
||||
);
|
||||
return new EmojiExecution(playerID, intent.recipient, intent.emoji);
|
||||
case "donate":
|
||||
return new DonateExecution(
|
||||
intent.playerID,
|
||||
intent.recipient,
|
||||
intent.troops,
|
||||
);
|
||||
return new DonateExecution(playerID, intent.recipient, intent.troops);
|
||||
case "troop_ratio":
|
||||
return new SetTargetTroopRatioExecution(intent.playerID, intent.ratio);
|
||||
return new SetTargetTroopRatioExecution(playerID, intent.ratio);
|
||||
case "embargo":
|
||||
return new EmbargoExecution(player, intent.targetID, intent.action);
|
||||
case "build_unit":
|
||||
return new ConstructionExecution(
|
||||
intent.playerID,
|
||||
playerID,
|
||||
this.mg.ref(intent.x, intent.y),
|
||||
intent.unit,
|
||||
);
|
||||
@@ -147,9 +129,7 @@ export class Executor {
|
||||
}
|
||||
|
||||
spawnBots(numBots: number): Execution[] {
|
||||
return new BotSpawner(this.mg, this.gameID)
|
||||
.spawnBots(numBots)
|
||||
.map((i) => this.createExec(i));
|
||||
return new BotSpawner(this.mg, this.gameID).spawnBots(numBots);
|
||||
}
|
||||
|
||||
fakeHumanExecutions(): Execution[] {
|
||||
|
||||
@@ -17,7 +17,7 @@ export class SpawnExecution implements Execution {
|
||||
|
||||
constructor(
|
||||
private playerInfo: PlayerInfo,
|
||||
private tile: TileRef,
|
||||
public readonly tile: TileRef,
|
||||
) {}
|
||||
|
||||
init(mg: Game, ticks: number) {
|
||||
|
||||
@@ -39,12 +39,13 @@ import { Stats } from "./Stats";
|
||||
import { simpleHash } from "../Util";
|
||||
|
||||
export function createGame(
|
||||
humans: PlayerInfo[],
|
||||
gameMap: GameMap,
|
||||
miniGameMap: GameMap,
|
||||
nationMap: NationMap,
|
||||
config: Config,
|
||||
): Game {
|
||||
return new GameImpl(gameMap, miniGameMap, nationMap, config);
|
||||
return new GameImpl(humans, gameMap, miniGameMap, nationMap, config);
|
||||
}
|
||||
|
||||
export type CellString = string;
|
||||
@@ -82,11 +83,13 @@ export class GameImpl implements Game {
|
||||
private botTeam: Team = { name: TeamName.Bot };
|
||||
|
||||
constructor(
|
||||
private _humans: PlayerInfo[],
|
||||
private _map: GameMap,
|
||||
private miniGameMap: GameMap,
|
||||
nationMap: NationMap,
|
||||
private _config: Config,
|
||||
) {
|
||||
this._humans.forEach((p) => this.addPlayer(p, 100));
|
||||
this._terraNullius = new TerraNulliusImpl();
|
||||
this._width = _map.width();
|
||||
this._height = _map.height();
|
||||
|
||||
@@ -33,8 +33,7 @@ ctx.addEventListener("message", async (e: MessageEvent<MainThreadMessage>) => {
|
||||
case "init":
|
||||
try {
|
||||
gameRunner = createGameRunner(
|
||||
message.gameID,
|
||||
message.gameConfig,
|
||||
message.gameStartInfo,
|
||||
message.clientID,
|
||||
gameUpdate,
|
||||
).then((gr) => {
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
import {
|
||||
PlayerActions,
|
||||
PlayerID,
|
||||
PlayerInfo,
|
||||
PlayerProfile,
|
||||
PlayerBorderTiles,
|
||||
} from "../game/Game";
|
||||
import { ErrorUpdate, GameUpdateViewData } from "../game/GameUpdates";
|
||||
import { ClientID, GameConfig, GameID, Turn } from "../Schemas";
|
||||
import { ClientID, GameStartInfo, Turn } from "../Schemas";
|
||||
import { generateID } from "../Util";
|
||||
import { WorkerMessage } from "./WorkerMessages";
|
||||
|
||||
@@ -19,8 +18,7 @@ export class WorkerClient {
|
||||
) => void;
|
||||
|
||||
constructor(
|
||||
private gameID: GameID,
|
||||
private gameConfig: GameConfig,
|
||||
private gameStartInfo: GameStartInfo,
|
||||
private clientID: ClientID,
|
||||
) {
|
||||
this.worker = new Worker(new URL("./Worker.worker.ts", import.meta.url));
|
||||
@@ -68,8 +66,7 @@ export class WorkerClient {
|
||||
this.worker.postMessage({
|
||||
type: "init",
|
||||
id: messageId,
|
||||
gameID: this.gameID,
|
||||
gameConfig: this.gameConfig,
|
||||
gameStartInfo: this.gameStartInfo,
|
||||
clientID: this.clientID,
|
||||
});
|
||||
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
import { GameUpdateViewData } from "../game/GameUpdates";
|
||||
import { ClientID, GameConfig, GameID, Turn } from "../Schemas";
|
||||
import {
|
||||
ClientID,
|
||||
Turn,
|
||||
ServerStartGameMessage,
|
||||
GameStartInfo,
|
||||
} from "../Schemas";
|
||||
import {
|
||||
PlayerActions,
|
||||
PlayerID,
|
||||
@@ -33,8 +38,7 @@ export interface HeartbeatMessage extends BaseWorkerMessage {
|
||||
// Messages from main thread to worker
|
||||
export interface InitMessage extends BaseWorkerMessage {
|
||||
type: "init";
|
||||
gameID: GameID;
|
||||
gameConfig: GameConfig;
|
||||
gameStartInfo: GameStartInfo;
|
||||
clientID: ClientID;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user