Permalink
Cannot retrieve contributors at this time
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
156 lines (147 sloc)
4.97 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { AvailableTragedyEvents } from "../../board/tragedy"; | |
import { AvailableTileBuildings } from "../../buildings/tile-building"; | |
import { Player } from "../../player/player"; | |
import { Resources } from "../../resources/index"; | |
import { randBetween } from "../helper"; | |
import { State } from "../index"; | |
import { generateWave, monsterRoll } from "./monsters"; | |
export function rollDice(state: State, diceOverload?: number): State { | |
const isStumblingStep = state.activeTragedy === AvailableTragedyEvents.StumblingSteps; | |
const idxOfTile = state.tiles.findIndex((t) => t.id === state.players[0].currentTileId); | |
const roll = diceOverload || (isStumblingStep ? 1 : randBetween(1, 6)); | |
const newPosition = idxOfTile + roll; | |
const isNextRound = newPosition > state.tiles.length - 1; | |
const newStumblingSteps = (state.activeTragedyParams === undefined || state.activeTragedyParams[0] === 1) | |
? undefined | |
: [state.activeTragedyParams[0] - 1]; | |
const monsterRollState = monsterRoll(state, diceOverload); | |
return gatherFortressBuilding({ | |
...state, | |
activeFortressBuildingConstruction: isNextRound ? undefined : state.activeFortressBuildingConstruction, | |
activeTragedy: isNextRound | |
? undefined | |
: isStumblingStep && newStumblingSteps === undefined | |
? undefined | |
: state.activeTragedy, | |
activeTragedyParams: isNextRound | |
? undefined | |
: isStumblingStep | |
? newStumblingSteps | |
: state.activeTragedyParams, | |
lastDiceRoll: roll, | |
monsters: generateWave(monsterRollState, isNextRound).monsters, | |
players: [ | |
{ | |
...state.players[0], | |
currentTileId: newPosition > state.tiles.length - 1 | |
? state.tiles[Math.abs(newPosition - state.tiles.length)].id | |
: state.tiles[newPosition].id | |
} as Player | |
], | |
purchaseInProgress: undefined, | |
resources: isNextRound | |
? gatherResources(state).resources | |
: state.resources, | |
round: isNextRound | |
? state.round + 1 | |
: state.round, | |
stats: monsterRollState.stats, | |
}, isNextRound); | |
} | |
function gatherFortressBuilding(state: State, isNextRound: boolean): State { | |
if (!isNextRound) { | |
return state; | |
} | |
const newState = { | |
...state, | |
resources: { ...state.resources }, | |
stats: { ...state.stats } | |
} as State; | |
return newState.fortressBuildings.reduce((prev, curr) => { | |
switch (curr.type) { | |
case "bakery": | |
const res = prev.resources.coal > 0 && "coal" || prev.resources.wood > 0 && "wood"; | |
if (res) { | |
prev.resources[res] -= 1; | |
prev.stats.population += 1; | |
} | |
case "blacksmith_shop": | |
if ( | |
prev.resources.iron > 0 && | |
prev.resources.coal > 0 | |
) { | |
prev.resources.iron -= 1; | |
prev.resources.coal -= 1; | |
prev.stats.soldiers += 1; | |
} | |
case "bank": | |
if (prev.resources.gold > 0) { | |
prev.resources.gold += 1; | |
} | |
case "magician_tower": | |
if (prev.resources.mana > 2) { | |
prev.resources.mana -= 3; | |
prev.fireFountainsActive = true; | |
} else { | |
prev.fireFountainsActive = false; | |
} | |
} | |
return prev; | |
}, newState); | |
} | |
export const PRODUCED_RESOURCES_PER_ROUND = 3; | |
function gatherResources(state: State): State { | |
const shrines = state.tileBuildings | |
.filter((b) => b.type === "shrine"); | |
const resourcesProduced = PRODUCED_RESOURCES_PER_ROUND | |
+ (shrines.length === 4 | |
? 1 | |
: 0); | |
const resources = state.tileBuildings | |
.filter((b) => b.type !== "shrine") | |
.filter((b) => { | |
switch (state.activeTragedy) { | |
case AvailableTragedyEvents.BurningTrees: | |
return b.type !== "sawmill"; | |
case AvailableTragedyEvents.ContaminatedBlood: | |
return b.type !== "butchery"; | |
case AvailableTragedyEvents.Rockfall: | |
return b.type !== "quarry"; | |
case AvailableTragedyEvents.ShatteringEarth: | |
return b.type !== "mana_rift"; | |
case AvailableTragedyEvents.Vermins: | |
return b.type !== "farm"; | |
case AvailableTragedyEvents.CollapsedMines: | |
const mineType = state.activeTragedyParams[0] as AvailableTileBuildings; | |
return b.type !== mineType; | |
default: | |
return true; | |
} | |
}) | |
.reduce((prev, curr) => { | |
prev[state.tiles | |
.find((t) => t.id === curr.tileId) | |
.type] += resourcesProduced; | |
return prev; | |
}, Object.assign({}, state.resources) as Resources); | |
return gatherShrineResources({ | |
...state, | |
resources | |
}); | |
} | |
function gatherShrineResources(state: State) { | |
const shrines = state.tileBuildings | |
.filter((b) => b.type === "shrine"); | |
if (!shrines) { | |
return state; | |
} | |
return { | |
...state, | |
resources: { | |
...state.resources, | |
blood: shrines.length > 2 ? state.resources.blood + 1 : state.resources.blood, | |
gold: shrines.length > 0 ? state.resources.gold + 1 : state.resources.gold, | |
mana: shrines.length > 1 ? state.resources.mana + 1 : state.resources.mana, | |
} | |
}; | |
} |