mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-21 11:20:45 +00:00
gameimpl store alliances
This commit is contained in:
@@ -6,7 +6,6 @@ export class AllianceRequestExecution implements Execution {
|
||||
private requestor: Player;
|
||||
private recipient: Player
|
||||
|
||||
|
||||
constructor(private requestorID: PlayerID, private recipientID: PlayerID) { }
|
||||
|
||||
init(mg: MutableGame, ticks: number): void {
|
||||
@@ -16,8 +15,7 @@ export class AllianceRequestExecution implements Execution {
|
||||
}
|
||||
|
||||
tick(ticks: number): void {
|
||||
alert('recied request')
|
||||
|
||||
this.mg.createAllianceRequest(this.requestor, this.recipient)
|
||||
this.active = false
|
||||
}
|
||||
|
||||
|
||||
@@ -40,6 +40,10 @@ export class BotExecution implements Execution {
|
||||
return
|
||||
}
|
||||
|
||||
this.bot.incomingAllianceRequests().forEach(ar => {
|
||||
ar.accept()
|
||||
})
|
||||
|
||||
if (this.neighborsTerraNullius) {
|
||||
for (const b of this.bot.borderTiles()) {
|
||||
for (const n of b.neighbors()) {
|
||||
@@ -66,11 +70,6 @@ export class BotExecution implements Execution {
|
||||
return
|
||||
}
|
||||
}
|
||||
// if (owner.type() == PlayerType.Human) {
|
||||
// if (this.random.chance(2)) {
|
||||
// return
|
||||
// }
|
||||
// }
|
||||
}
|
||||
this.sendAttack(owner)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
import {MutableAlliance, MutablePlayer, Player} from "./Game";
|
||||
import {PlayerImpl} from "./PlayerImpl";
|
||||
|
||||
export class AllianceImpl implements MutableAlliance {
|
||||
constructor(
|
||||
readonly requestor_: PlayerImpl,
|
||||
readonly recepient_: PlayerImpl,
|
||||
readonly createdAtTick_: number,
|
||||
) { }
|
||||
|
||||
requestor(): MutablePlayer {
|
||||
return this.requestor_
|
||||
}
|
||||
|
||||
recipient(): MutablePlayer {
|
||||
return this.recepient_
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,7 +4,7 @@ import {GameImpl} from "./GameImpl";
|
||||
|
||||
export class AllianceRequestImpl implements MutableAllianceRequest {
|
||||
|
||||
constructor(private requestor_, private recipient_, private game: GameImpl) { }
|
||||
constructor(private requestor_, private recipient_, private tickCreated: number, private game: GameImpl) { }
|
||||
|
||||
requestor(): Player {
|
||||
return this.requestor_;
|
||||
|
||||
+17
-6
@@ -56,13 +56,14 @@ export interface MutableAllianceRequest extends AllianceRequest {
|
||||
reject(): void
|
||||
}
|
||||
|
||||
export class Alliance {
|
||||
constructor(
|
||||
public readonly requestor: Player,
|
||||
public readonly recepient: Player
|
||||
) { }
|
||||
export interface Alliance {
|
||||
requestor(): Player
|
||||
recipient(): Player
|
||||
}
|
||||
|
||||
export interface MutableAlliance extends Alliance {
|
||||
|
||||
}
|
||||
|
||||
export class PlayerInfo {
|
||||
constructor(
|
||||
@@ -134,6 +135,9 @@ export interface Player {
|
||||
numTilesOwned(): number
|
||||
tiles(): ReadonlySet<Tile>
|
||||
sharesBorderWith(other: Player | TerraNullius): boolean
|
||||
incomingAllianceRequests(): AllianceRequest[]
|
||||
outgoingAllianceRequests(): AllianceRequest[]
|
||||
alliances(): Alliance[]
|
||||
toString(): string
|
||||
}
|
||||
|
||||
@@ -147,6 +151,9 @@ export interface MutablePlayer extends Player {
|
||||
executions(): Execution[]
|
||||
neighbors(): (MutablePlayer | TerraNullius)[]
|
||||
boats(): MutableBoat[]
|
||||
incomingAllianceRequests(): MutableAllianceRequest[]
|
||||
outgoingAllianceRequests(): MutableAllianceRequest[]
|
||||
alliances(): MutableAlliance[]
|
||||
addBoat(troops: number, tile: Tile, target: Player | TerraNullius): MutableBoat
|
||||
}
|
||||
|
||||
@@ -178,7 +185,7 @@ export interface MutableGame extends Game {
|
||||
executions(): Execution[]
|
||||
removeInactiveExecutions(): void
|
||||
removeExecution(exec: Execution): void
|
||||
allianceRequest(requestor: Player, recipient: Player): MutableAllianceRequest
|
||||
createAllianceRequest(requestor: Player, recipient: Player): MutableAllianceRequest
|
||||
}
|
||||
|
||||
export class TileEvent implements GameEvent {
|
||||
@@ -192,3 +199,7 @@ export class PlayerEvent implements GameEvent {
|
||||
export class BoatEvent implements GameEvent {
|
||||
constructor(public readonly boat: Boat, public oldTile: Tile) { }
|
||||
}
|
||||
|
||||
export class AllianceRequestReplyEvent implements GameEvent {
|
||||
constructor(public readonly allianceRequest: AllianceRequest, public readonly accepted: boolean) { }
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import {PlayerImpl} from "./PlayerImpl";
|
||||
import {TerraNulliusImpl} from "./TerraNulliusImpl";
|
||||
import {TileImpl} from "./TileImpl";
|
||||
import {AllianceRequestImpl} from "./AllianceRequestImpl";
|
||||
import {AllianceImpl} from "./AllianceImpl";
|
||||
|
||||
export function createGame(terrainMap: TerrainMap, eventBus: EventBus, config: Config): Game {
|
||||
return new GameImpl(terrainMap, eventBus, config)
|
||||
@@ -28,7 +29,8 @@ export class GameImpl implements MutableGame {
|
||||
private _numLandTiles: number
|
||||
_terraNullius: TerraNulliusImpl
|
||||
|
||||
private allianceRequests: AllianceRequestImpl[] = []
|
||||
allianceRequests: AllianceRequestImpl[] = []
|
||||
alliances_: AllianceImpl[] = []
|
||||
|
||||
constructor(terrainMap: TerrainMap, private eventBus: EventBus, private _config: Config) {
|
||||
this._terraNullius = new TerraNulliusImpl(this)
|
||||
@@ -45,18 +47,22 @@ export class GameImpl implements MutableGame {
|
||||
}
|
||||
}
|
||||
|
||||
allianceRequest(requestor: Player, recipient: Player): MutableAllianceRequest {
|
||||
const ar = new AllianceRequestImpl(requestor, recipient, this)
|
||||
createAllianceRequest(requestor: Player, recipient: Player): MutableAllianceRequest {
|
||||
const ar = new AllianceRequestImpl(requestor, recipient, this._ticks, this)
|
||||
this.allianceRequests.push(ar)
|
||||
return ar
|
||||
}
|
||||
|
||||
acceptAllianceRequest() {
|
||||
|
||||
acceptAllianceRequest(request: AllianceRequestImpl) {
|
||||
this.allianceRequests = this.allianceRequests.filter(ar => ar != request)
|
||||
const alliance = new AllianceImpl(request.requestor() as PlayerImpl, request.recipient() as PlayerImpl, this._ticks)
|
||||
this.alliances_.push(alliance)
|
||||
// TODO: Fire event.
|
||||
}
|
||||
|
||||
rejectAllianceRequest() {
|
||||
|
||||
rejectAllianceRequest(request: AllianceRequestImpl) {
|
||||
this.allianceRequests = this.allianceRequests.filter(ar => ar != request)
|
||||
// TODO: Fire event.
|
||||
}
|
||||
|
||||
numLandTiles(): number {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import {MutablePlayer, Tile, PlayerInfo, PlayerID, PlayerType, Player, TerraNullius, Cell, MutableGame, Execution} from "./Game";
|
||||
import {MutablePlayer, Tile, PlayerInfo, PlayerID, PlayerType, Player, TerraNullius, Cell, MutableGame, Execution, AllianceRequest, MutableAllianceRequest, MutableAlliance} from "./Game";
|
||||
import {ClientID} from "../Schemas";
|
||||
import {simpleHash} from "../Util";
|
||||
import {CellString, GameImpl} from "./GameImpl";
|
||||
@@ -104,10 +104,22 @@ export class PlayerImpl implements MutablePlayer {
|
||||
info(): PlayerInfo {return this.playerInfo;}
|
||||
troops(): number {return this._troops;}
|
||||
isAlive(): boolean {return this._tiles.size > 0;}
|
||||
gameState(): MutableGame {return this.gs;}
|
||||
executions(): Execution[] {
|
||||
return this.gs.executions().filter(exec => exec.owner().id() == this.id());
|
||||
}
|
||||
|
||||
incomingAllianceRequests(): MutableAllianceRequest[] {
|
||||
return this.gs.allianceRequests.filter(ar => ar.recipient() == this)
|
||||
}
|
||||
|
||||
outgoingAllianceRequests(): MutableAllianceRequest[] {
|
||||
return this.gs.allianceRequests.filter(ar => ar.requestor() == this)
|
||||
}
|
||||
|
||||
alliances(): MutableAlliance[] {
|
||||
return this.gs.alliances_.filter(a => a.requestor() == this || a.recipient() == this)
|
||||
}
|
||||
|
||||
hash(): number {
|
||||
return simpleHash(this.id()) * (this.troops() + this.numTilesOwned());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user