This repository has been archived on 2021-02-20. You can view files and clone it, but cannot push or open issues or pull requests.
shapez.io/src/js/game/production_analytics.js

135 lines
4.1 KiB
JavaScript
Raw Normal View History

2020-05-13 18:04:51 +02:00
import { GameRoot } from "./root";
import { ShapeDefinition } from "./shape_definition";
import { globalConfig } from "../core/config";
2020-08-15 18:39:08 +02:00
import { BaseItem } from "./base_item";
2020-05-14 08:44:07 +02:00
import { ShapeItem } from "./items/shape_item";
2020-05-14 10:25:00 +02:00
import { BasicSerializableObject } from "../savegame/serialization";
2020-05-13 18:04:51 +02:00
/** @enum {string} */
export const enumAnalyticsDataSource = {
produced: "produced",
stored: "stored",
delivered: "delivered",
};
2020-05-14 10:25:00 +02:00
export class ProductionAnalytics extends BasicSerializableObject {
static getId() {
return "ProductionAnalytics";
}
2020-05-13 18:04:51 +02:00
/**
* @param {GameRoot} root
*/
constructor(root) {
2020-05-14 10:25:00 +02:00
super();
2020-05-13 18:04:51 +02:00
this.root = root;
this.history = {
[enumAnalyticsDataSource.produced]: [],
[enumAnalyticsDataSource.stored]: [],
[enumAnalyticsDataSource.delivered]: [],
};
for (let i = 0; i < globalConfig.statisticsGraphSlices; ++i) {
this.startNewSlice();
}
this.root.signals.shapeDelivered.add(this.onShapeDelivered, this);
2020-05-14 08:44:07 +02:00
this.root.signals.itemProduced.add(this.onItemProduced, this);
2020-05-13 18:04:51 +02:00
this.lastAnalyticsSlice = 0;
}
/**
* @param {ShapeDefinition} definition
*/
onShapeDelivered(definition) {
const key = definition.getHash();
const entry = this.history[enumAnalyticsDataSource.delivered];
entry[entry.length - 1][key] = (entry[entry.length - 1][key] || 0) + 1;
}
/**
2020-05-14 08:44:07 +02:00
* @param {BaseItem} item
2020-05-13 18:04:51 +02:00
*/
2020-05-14 08:44:07 +02:00
onItemProduced(item) {
2020-08-15 18:39:08 +02:00
if (item.getItemType() === "shape") {
const definition = /** @type {ShapeItem} */ (item).definition;
2020-05-14 08:44:07 +02:00
const key = definition.getHash();
const entry = this.history[enumAnalyticsDataSource.produced];
entry[entry.length - 1][key] = (entry[entry.length - 1][key] || 0) + 1;
}
2020-05-13 18:04:51 +02:00
}
/**
* Starts a new time slice
*/
startNewSlice() {
for (const key in this.history) {
if (key === enumAnalyticsDataSource.stored) {
// Copy stored data
this.history[key].push(Object.assign({}, this.root.hubGoals.storedShapes));
} else {
this.history[key].push({});
}
while (this.history[key].length > globalConfig.statisticsGraphSlices) {
this.history[key].shift();
}
}
}
/**
2020-05-14 10:25:00 +02:00
* Returns the current rate of a given shape
2020-05-13 18:04:51 +02:00
* @param {enumAnalyticsDataSource} dataSource
* @param {ShapeDefinition} definition
*/
getCurrentShapeRate(dataSource, definition) {
const slices = this.history[dataSource];
return slices[slices.length - 2][definition.getHash()] || 0;
}
/**
2020-05-14 10:25:00 +02:00
* Returns the rate of a given shape, <historyOffset> frames ago
2020-05-13 18:04:51 +02:00
* @param {enumAnalyticsDataSource} dataSource
* @param {ShapeDefinition} definition
* @param {number} historyOffset
*/
getPastShapeRate(dataSource, definition, historyOffset) {
assertAlways(
2020-05-14 10:25:00 +02:00
historyOffset >= 0 && historyOffset < globalConfig.statisticsGraphSlices - 1,
2020-05-13 18:04:51 +02:00
"Invalid slice offset: " + historyOffset
);
const slices = this.history[dataSource];
2020-05-14 10:25:00 +02:00
return slices[slices.length - 2 - historyOffset][definition.getHash()] || 0;
}
/**
* Returns the rates of all shapes
* @param {enumAnalyticsDataSource} dataSource
*/
getCurrentShapeRates(dataSource) {
const slices = this.history[dataSource];
// First, copy current slice
const baseValues = Object.assign({}, slices[slices.length - 2]);
// Add past values
for (let i = 0; i < 10; ++i) {
const pastValues = slices[slices.length - i - 3];
for (const key in pastValues) {
baseValues[key] = baseValues[key] || 0;
}
}
return baseValues;
2020-05-13 18:04:51 +02:00
}
update() {
if (this.root.time.now() - this.lastAnalyticsSlice > globalConfig.analyticsSliceDurationSeconds) {
this.lastAnalyticsSlice = this.root.time.now();
this.startNewSlice();
}
}
}