Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/crissian/x4 into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
crissian committed May 3, 2023
2 parents a1557a6 + fb243fd commit 4910485
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 11 deletions.
26 changes: 21 additions & 5 deletions src/app/station/components/station-calculator.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface StationResourceItemModel {
efficiency: number;
name: string;
total: number;
type: String;
}

export interface StationResourceModel {
Expand All @@ -37,6 +38,7 @@ export interface WareProductionData {
efficiency: number;
name: string;
total: number;
type: String;
}

export interface StationProduction {
Expand Down Expand Up @@ -150,10 +152,11 @@ export class ResourceCalculator {
return { value: totalWorkforce, capacity: workforce };
}

static calculate(modules: StationModuleModel[], sunlight: number) {
static calculate(modules: StationModuleModel[], sunlight: number, partialWorkforce: number) {
let work = this.calculateWorkforce(modules);

let multiplier = (work.capacity == 0 || work.value == 0) ? 0 : (work.capacity / work.value);
let multiplier = (work.capacity == 0 || work.value == 0) ? 0 : (partialWorkforce / work.value);

if (multiplier > 1) {
multiplier = 1;
}
Expand All @@ -162,14 +165,26 @@ export class ResourceCalculator {
if (modules.length > 0) {
data = modules
.map<WareProductionData[]>(x => {
let modifier = 1
if(x.module?.type === "Habitation") {
let capacity = x.module.workForce.capacity * x.count
if(partialWorkforce >= capacity) {
partialWorkforce -= capacity
} else {
modifier = partialWorkforce / capacity
partialWorkforce = 0
}
}

const values: StationResourceItemModel[] = x.needs
.map(y => ({
ware: y.ware,
count: x.count,
amount: -y.amount,
efficiency: 100,
efficiency: modifier * 100,
name: x.module.name,
total: x.count * -y.amount
total: x.count * -y.amount * modifier,
type: x.module.type
})
);

Expand All @@ -194,7 +209,8 @@ export class ResourceCalculator {
amount: production.amount,
efficiency: efficiency * 100,
name: x.module.name,
total: x.count * production.amount * efficiency
total: x.count * production.amount * efficiency,
type: x.module.type
});
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/app/station/components/station-modules.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { StationModule } from '../../shared/services/model/model';
import { ModuleService } from '../../shared/services/module.service';
import { WareService } from '../../shared/services/ware.service';
import { RECYCLING_MODULES, ResourceCalculator, StationModuleModel, StationResourceModel, WareGroupModel } from './station-calculator.model';
import { StationSummaryService } from './station-summary/services/station-summary.service';

@Component({
selector: 'app-station-modules',
Expand All @@ -23,7 +24,7 @@ export class StationModulesComponent implements OnInit {
@Input()
sunlight = 100;

constructor(private moduleService: ModuleService, private wareService: WareService) {
constructor(private moduleService: ModuleService, private wareService: WareService, private stationSummaryService: StationSummaryService) {
}

ngOnInit(): void {
Expand Down Expand Up @@ -83,7 +84,8 @@ export class StationModulesComponent implements OnInit {
}

while (true) {
const resources: StationResourceModel[] = ResourceCalculator.calculate(this.modules, this.sunlight);

const resources: StationResourceModel[] = ResourceCalculator.calculate(this.modules, this.sunlight, this.stationSummaryService.$partialWorkforce);
let didChange = false;

const modules = this.modules;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
<ng-container *ngIf="moduleRes.total > 0">
<span i18n="@@at"> at</span>&nbsp;<strong>{{ moduleRes.efficiency | number: '1.0-0' }}%</strong>&nbsp;<span i18n="@@efficiency">efficiency</span>
</ng-container>
<ng-container *ngIf="moduleRes.type === 'Habitation'">
<span i18n="@@at"> at</span>&nbsp;<strong>{{ moduleRes.efficiency | number: '1.0-0' }}%</strong>&nbsp;<span i18n="@@capacity">capacity</span>
</ng-container>
</td>
<td class="text-right">{{ (moduleRes.total) | number:'1.0-0' }}</td>
</tr>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class StationResourcesComponent implements OnChanges, OnDestroy {
this.partialWorkforce = workforce;
}

this.resources = ResourceCalculator.calculate(this.modules, this.sunlight);
this.resources = ResourceCalculator.calculate(this.modules, this.sunlight, this.partialWorkforce);
this.resources.forEach((x) => {
x.expanded = this.expandState[x.items[0].ware.id];
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,21 @@ export class StationSummaryComponent implements OnChanges {
}
return acc;
}, 0);
if (this.totalWorkforceCapacity != workforce) {

const workforceNeeded = this.modules.reduce((acc, item) => {
if (item.module && item.module.workForce && item.module.workForce.max) {
return acc + item.count * item.module.workForce.max;
}
return acc;
}, 0);

if (this.totalWorkforceCapacity != workforce || this.partialWorkforce != workforceNeeded) {
this.totalWorkforceCapacity = workforce;
this.partialWorkforce = workforce;
this.partialWorkforce = workforceNeeded;
}
this.stationSummaryService.setPartialWorkforce(this.partialWorkforce);

const resources = ResourceCalculator.calculate(this.modules, this.sunlight);
const resources = ResourceCalculator.calculate(this.modules, this.sunlight, this.partialWorkforce);
resources.sort((a, b) => this.wareService.compareWares(a.ware, b.ware));

resources.forEach((x) => {
Expand Down

0 comments on commit 4910485

Please sign in to comment.