Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/core/configuration/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export interface Config {
unitInfo(type: UnitType): UnitInfo;
tradeShipGold(dist: number, numPorts: number): Gold;
tradeShipSpawnRate(numberOfPorts: number): number;
trainGold(): Gold;
trainGold(isFriendly: boolean): Gold;
trainSpawnRate(numberOfStations: number): number;
trainStationMinRange(): number;
trainStationMaxRange(): number;
Expand Down
5 changes: 3 additions & 2 deletions src/core/configuration/DefaultConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,10 @@ export class DefaultConfig implements Config {
trainSpawnRate(numberOfStations: number): number {
return Math.min(1400, Math.round(40 * Math.pow(numberOfStations, 0.5)));
}
trainGold(): Gold {
return BigInt(4_000);
trainGold(isFriendly: boolean): Gold {
return isFriendly ? 100_000n : 25_000n;
}

trainStationMinRange(): number {
return 15;
}
Expand Down
23 changes: 10 additions & 13 deletions src/core/game/TrainStation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,59 +18,56 @@ interface TrainStopHandler {
* Behavior to be defined
*/
class CityStopHandler implements TrainStopHandler {
private factor: bigint = BigInt(2);
onStop(
mg: Game,
station: TrainStation,
trainExecution: TrainExecution,
): void {
const level = BigInt(station.unit.level() + 1);
let goldBonus = (mg.config().trainGold() * level) / this.factor;
const stationOwner = station.unit.owner();
const trainOwner = trainExecution.owner();
const isFriendly = stationOwner.isFriendly(trainOwner);
const goldBonus = mg.config().trainGold(isFriendly) * level;
// Share revenue with the station owner if it's not the current player
if (stationOwner.isFriendly(trainOwner)) {
goldBonus += BigInt(1_000); // Bonus for everybody when trading with an ally!
if (isFriendly) {
stationOwner.addGold(goldBonus, station.tile());
}
trainOwner.addGold(goldBonus, station.tile());
}
}

class PortStopHandler implements TrainStopHandler {
private factor: bigint = BigInt(2);
constructor(private random: PseudoRandom) {}
onStop(
mg: Game,
station: TrainStation,
trainExecution: TrainExecution,
): void {
const level = BigInt(station.unit.level() + 1);
let goldBonus = (mg.config().trainGold() * level) / this.factor;
const stationOwner = station.unit.owner();
const trainOwner = trainExecution.owner();
// Share revenue with the station owner if it's not the current player
if (stationOwner.isFriendly(trainOwner)) {
goldBonus += BigInt(1_000); // Bonus for everybody when trading with an ally!
const isFriendly = stationOwner.isFriendly(trainOwner);
const goldBonus = mg.config().trainGold(isFriendly) * level;

if (isFriendly) {
stationOwner.addGold(goldBonus, station.tile());
}
trainOwner.addGold(goldBonus, station.tile());
}
}

class FactoryStopHandler implements TrainStopHandler {
private factor: bigint = BigInt(2);
onStop(
mg: Game,
station: TrainStation,
trainExecution: TrainExecution,
): void {
let goldBonus = mg.config().trainGold();
const stationOwner = station.unit.owner();
const trainOwner = trainExecution.owner();
const isFriendly = stationOwner.isFriendly(trainOwner);
const goldBonus = mg.config().trainGold(isFriendly);
// Share revenue with the station owner if it's not the current player
if (stationOwner.isFriendly(trainOwner)) {
goldBonus += BigInt(1_000); // Bonus for everybody when trading with an ally!
if (isFriendly) {
stationOwner.addGold(goldBonus, station.tile());
}
trainOwner.addGold(goldBonus, station.tile());
Expand Down
9 changes: 5 additions & 4 deletions tests/core/game/TrainStation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ describe("TrainStation", () => {
game = {
ticks: jest.fn().mockReturnValue(123),
config: jest.fn().mockReturnValue({
trainGold: () => BigInt(4000),
trainGold: (isFriendly: boolean) =>
isFriendly ? BigInt(1000) : BigInt(500),
}),
addUpdate: jest.fn(),
addExecution: jest.fn(),
Expand Down Expand Up @@ -50,7 +51,7 @@ describe("TrainStation", () => {

station.onTrainStop(trainExecution);

expect(unit.owner().addGold).toHaveBeenCalledWith(4000n, unit.tile());
expect(unit.owner().addGold).toHaveBeenCalledWith(1000n, unit.tile());
});

it("handles allied trade", () => {
Expand All @@ -60,9 +61,9 @@ describe("TrainStation", () => {

station.onTrainStop(trainExecution);

expect(unit.owner().addGold).toHaveBeenCalledWith(5000n, unit.tile());
expect(unit.owner().addGold).toHaveBeenCalledWith(2000n, unit.tile());
expect(trainExecution.owner().addGold).toHaveBeenCalledWith(
5000n,
2000n,
unit.tile(),
);
});
Expand Down
Loading