mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-07-03 12:00:51 +00:00
use GameView in renderers
This commit is contained in:
+11
-17
@@ -1,6 +1,6 @@
|
||||
import { MessageType } from './game/Game';
|
||||
import { MessageType, Player, Tile, Unit } from './game/Game';
|
||||
import { Config } from "./configuration/Config";
|
||||
import { Alliance, AllianceRequest, AllPlayers, Cell, DefenseBonus, EmojiMessage, Execution, ExecutionView, Game, Gold, MutableTile, Nation, Player, PlayerID, PlayerInfo, PlayerType, Relation, TerrainMap, TerrainTile, TerrainType, TerraNullius, Tick, Tile, Unit, UnitInfo, UnitType } from "./game/Game";
|
||||
import { Alliance, AllianceRequest, AllPlayers, Cell, DefenseBonus, EmojiMessage, Execution, ExecutionView, Game, Gold, MutableTile, Nation, PlayerID, PlayerInfo, PlayerType, Relation, TerrainMap, TerrainTile, TerrainType, TerraNullius, Tick, UnitInfo, UnitType } from "./game/Game";
|
||||
import { ClientID } from "./Schemas";
|
||||
|
||||
export interface ViewSerializable<T> {
|
||||
@@ -20,8 +20,8 @@ export interface TileViewData extends ViewData<TileViewData> {
|
||||
isBorder: boolean
|
||||
}
|
||||
|
||||
export class TileView implements Tile {
|
||||
constructor(private game: Game, private data: TileViewData, private _terrain: TerrainTile) { }
|
||||
export class TileView {
|
||||
constructor(private game: GameView, private data: TileViewData, private _terrain: TerrainTile) { }
|
||||
type(): TerrainType {
|
||||
return this._terrain.type()
|
||||
}
|
||||
@@ -44,7 +44,7 @@ export class TileView implements Tile {
|
||||
return this._terrain
|
||||
}
|
||||
|
||||
neighbors(): Tile[] {
|
||||
neighbors(): TileView[] {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
|
||||
@@ -67,7 +67,7 @@ export interface UnitViewData extends ViewData<UnitView> {
|
||||
health?: number
|
||||
}
|
||||
|
||||
export class UnitView implements Unit {
|
||||
export class UnitView {
|
||||
constructor(private data: UnitViewData) { }
|
||||
|
||||
type(): UnitType {
|
||||
@@ -76,10 +76,10 @@ export class UnitView implements Unit {
|
||||
troops(): number {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
tile(): Tile {
|
||||
tile(): TileView {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
owner(): Player {
|
||||
owner(): PlayerView {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
isActive(): boolean {
|
||||
@@ -109,7 +109,7 @@ export interface PlayerViewData extends ViewData<PlayerViewData> {
|
||||
targetTroopRatio: number
|
||||
}
|
||||
|
||||
export class PlayerView implements Player {
|
||||
export class PlayerView {
|
||||
constructor(private game: Game, private data: PlayerViewData) { }
|
||||
name(): string {
|
||||
return this.data.name
|
||||
@@ -129,7 +129,7 @@ export class PlayerView implements Player {
|
||||
isAlive(): boolean {
|
||||
return this.data.isAlive
|
||||
}
|
||||
isPlayer(): this is Player {
|
||||
isPlayer(): this is PlayerView {
|
||||
return true
|
||||
}
|
||||
numTilesOwned(): number {
|
||||
@@ -211,9 +211,6 @@ export class PlayerView implements Player {
|
||||
canBuild(type: UnitType, targetTile: Tile): Tile | false {
|
||||
return false
|
||||
}
|
||||
lastTileChange(): Tick {
|
||||
return 0
|
||||
}
|
||||
info(): PlayerInfo {
|
||||
return null
|
||||
}
|
||||
@@ -225,7 +222,7 @@ export interface GameUpdateViewData extends ViewData<GameUpdateViewData> {
|
||||
tileUpdates: TileViewData[]
|
||||
}
|
||||
|
||||
export class GameView implements Game {
|
||||
export class GameView {
|
||||
private lastGameUpdate: GameUpdateViewData
|
||||
private tiles: TileViewData[][] = []
|
||||
|
||||
@@ -266,9 +263,6 @@ export class GameView implements Game {
|
||||
isOnMap(cell: Cell): boolean {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
neighbors(cell: Cell | Tile): Tile[] {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
width(): number {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
|
||||
+4
-3
@@ -7,6 +7,7 @@ import { Cell, Game, Player, TerraNullius, Tile, Unit } from "./game/Game";
|
||||
import { number } from 'zod';
|
||||
import { GameConfig, GameID, GameRecord, PlayerRecord, Turn } from './Schemas';
|
||||
import { customAlphabet, nanoid } from 'nanoid';
|
||||
import { GameView } from './GameView';
|
||||
|
||||
|
||||
|
||||
@@ -62,7 +63,7 @@ export function and(x: (tile: Tile) => boolean, y: (tile: Tile) => boolean): (ti
|
||||
}
|
||||
|
||||
// TODO: refactor to new file
|
||||
export function sourceDstOceanShore(game: Game, src: Player, tile: Tile): [Tile | null, Tile | null] {
|
||||
export function sourceDstOceanShore(game: GameView, src: Player, tile: Tile): [Tile | null, Tile | null] {
|
||||
const dst = tile.owner()
|
||||
let srcTile = closestOceanShoreFromPlayer(src, tile, game.width())
|
||||
let dstTile: Tile | null = null
|
||||
@@ -74,11 +75,11 @@ export function sourceDstOceanShore(game: Game, src: Player, tile: Tile): [Tile
|
||||
return [srcTile, dstTile]
|
||||
}
|
||||
|
||||
export function targetTransportTile(game: Game, tile: Tile): Tile | null {
|
||||
export function targetTransportTile(gameWidth: number, tile: Tile): Tile | null {
|
||||
const dst = tile.owner()
|
||||
let dstTile: Tile | null = null
|
||||
if (dst.isPlayer()) {
|
||||
dstTile = closestOceanShoreFromPlayer(dst as Player, tile, game.width())
|
||||
dstTile = closestOceanShoreFromPlayer(dst as Player, tile, gameWidth)
|
||||
} else {
|
||||
dstTile = closestOceanShoreTN(tile, 300)
|
||||
}
|
||||
|
||||
@@ -68,7 +68,7 @@ export class TransportShipExecution implements Execution {
|
||||
|
||||
this.troops = Math.min(this.troops, this.attacker.troops())
|
||||
|
||||
this.dst = targetTransportTile(this.mg, this.mg.tile(this.cell))
|
||||
this.dst = targetTransportTile(this.mg.width(), this.mg.tile(this.cell))
|
||||
if (this.dst == null) {
|
||||
consolex.warn(`${this.attacker} cannot send ship to ${this.target}, cannot find attack tile`)
|
||||
this.active = false
|
||||
|
||||
@@ -17,7 +17,7 @@ export class WorkerClient {
|
||||
this.worker = new Worker(new URL('./Worker.worker.ts', import.meta.url));
|
||||
}
|
||||
|
||||
initialize(gameUpdate: (gu: GameUpdateViewData) => void): Promise<void> {
|
||||
initialize(): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.worker.postMessage({
|
||||
type: 'init',
|
||||
@@ -28,21 +28,28 @@ export class WorkerClient {
|
||||
const handler = (e: MessageEvent) => {
|
||||
if (e.data.type === 'initialized') {
|
||||
this.isInitialized = true;
|
||||
this.worker.removeEventListener('message', handler)
|
||||
resolve();
|
||||
return
|
||||
}
|
||||
if (!this.isInitialized) {
|
||||
reject('Failed to initialize pathfinder');
|
||||
}
|
||||
if (e.data.type == "game_update") {
|
||||
gameUpdate(e.data.gameUpdate)
|
||||
}
|
||||
};
|
||||
|
||||
this.worker.addEventListener('message', handler);
|
||||
});
|
||||
}
|
||||
|
||||
start(gameUpdate: (gu: GameUpdateViewData) => void) {
|
||||
if (!this.isInitialized) {
|
||||
throw new Error('Failed to initialize pathfinder');
|
||||
}
|
||||
const handler = (e: MessageEvent) => {
|
||||
if (e.data.type == "game_update") {
|
||||
gameUpdate(e.data.gameUpdate)
|
||||
}
|
||||
}
|
||||
this.worker.addEventListener('message', handler);
|
||||
}
|
||||
|
||||
sendTurn(turn: Turn) {
|
||||
this.worker.postMessage({
|
||||
type: "turn",
|
||||
|
||||
Reference in New Issue
Block a user