Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import { Transaction, TransactionManager, EntityManager } from "typeorm";
import Planet from "@db/models/planet";
import BuildQueue from "../building/build-queue";
import { PureUpdater } from "./pure-updater";
import logger from "@logger";
import BuildTask from "@db/models/build-task";
import Player from "@db/models/player";
import ResearchQueue from "../research/research-queue";
import ResearchTask from "@db/models/research-task";
//Okey about how this updating works I could write a book, so let's say it's magic :3
export default class Updater {
private researchQueue: ResearchQueue;
constructor(private readonly planetId: number) {}
@Transaction({isolation: "SERIALIZABLE"})
public async fullUpdatePlanet(@TransactionManager() manager?: EntityManager): Promise<{player: Player, planet: Planet}> {
const planet = await manager.findOne(Planet, this.planetId);
if(!planet) return null;
const player = await manager.findOne(Player, planet.playerId);
const buildQueue = new BuildQueue(planet);
this.researchQueue = new ResearchQueue(player);
await Promise.all([
buildQueue.load(manager),
this.researchQueue.load(manager)
]);
const pureUpdater = new PureUpdater(player, planet, buildQueue, this.researchQueue);
pureUpdater.init();
let finished = false;
while(!finished) {
finished = pureUpdater.startUpdate();
import BuildTask from "@db/models/build-task";
import { haveEnoughResources, subtractResources } from "../../utils";
import BuildCalculator from "./build-calculator";
import { Transaction, TransactionManager, EntityManager } from "typeorm";
import BuildQueue from "./build-queue";
import Player from "@db/models/player";
import TechnologyChecker from "../technology/technology-checker";
export default class BuildSheluder {
constructor(private readonly _player: Player, private readonly _planet: Planet) {}
/**
* Performing ACID operation of adding new build task.
* Loads planet again from DB in transaction,
* then saves it to database if modified resources
*/
@Transaction({ isolation: "SERIALIZABLE" })
public async sheludeBuildTask(buildingName: string, @TransactionManager() manager?: EntityManager): Promise {
if(!this.checkIfValidBuildingName(buildingName)) {
logger.error(`Error while creating build job: There's no building ${buildingName}!`);
return false;
}
const planet = await manager.findOne(Planet, this._planet.id);
//I don't have to reload player, because I am not changing it at all.
const buildQueue = new BuildQueue(planet);
await buildQueue.load(manager);
if(buildQueue.isFull()) {
logger.error(`Trying to create build job while the queue if full!`);
return false;
}
const calculator = new BuildCalculator(planet);
import Planet from "../../../db/models/planet";
import logger from "../../../logger";
import BuildTask from "../../../db/models/build-task";
import { haveEnoughResources, subtractResources } from "../../utils";
import BuildingsCalculator from "./buildings-calculator";
import { Transaction, TransactionManager, EntityManager } from "typeorm";
import BuildQueue from "./build-queue";
export default class BuildSheluder {
constructor(private readonly _planet: Planet) {}
/**
* Performing ACID operation of adding new build task.
* Loads planet again from DB in transaction,
* then saves it to database if modified resources
*/
@Transaction({ isolation: "SERIALIZABLE" })
public async sheludeBuildTask(buildingName: string, @TransactionManager() manager?: EntityManager): Promise {
if(!this.checkIfValidBuildingName(buildingName)) {
logger.error(`Error while creating build job: There's no building ${buildingName}!`);
return false;
}
const planet = await manager.findOne(Planet, this._planet.id);
const buildQueue = new BuildQueue(planet);
await buildQueue.load(manager);
if(buildQueue.isFull()) {
logger.error(`Trying to create build job while the queue if full!`);
return false;
}
const calculator = new BuildingsCalculator(planet);
let buildingLevel = planet.buildings[buildingName];
const user = await userRepos.findOne({ where: { id: flow.user.id }, relations: ['role'] });
user.status = step.nextState;
user.role = role;
await userRepos.save(user);
flow = Flow.create(flow) as Flow;
flow.state = step.nextState;
flow.operator = User.create(options.operator) as User;
await flowRepos.save(flow);
return { name: '完成' };
}
@Transaction()
async reject(
step,
flow,
options,
@TransactionRepository(User) userRepos?: Repository,
@TransactionRepository(Flow) flowRepos?: Repository
) {
const user = flow.user;
user.status = step.nextState;
await userRepos.save(user);
flow = Flow.create(flow) as Flow;
flow.state = step.nextState;
flow.operator = User.create(options.operator) as User;
step,
flow,
options,
@TransactionRepository(Flow) flowRepos?: Repository
) {
flow = Flow.create(flow) as Flow;
flow.state = step.nextState;
flow.operator = User.create(options.operator) as User;
flow.executor = User.create(options.executor) as User;
await flowRepos.save(flow);
}
@Transaction()
async refuse(
step,
flow,
options,
@TransactionRepository(Flow) flowRepos?: Repository
) {
flow = Flow.create(flow) as Flow;
flow.state = step.nextState;
flow.operator = User.create(options.operator) as User;
flow.executor = null;
await flowRepos.save(flow);
}
@Transaction()