mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-17 23:33:12 +09:00
Merge pull request #733 from clusterio/feature/subspace-storage-space-age
Feature/subspace storage space age
This commit is contained in:
15 files changed
+249
-186
No files matched your search
@@ -2,19 +2,19 @@
|
||||
"name": "clusterio_lib",
|
||||
"variants": [
|
||||
{
|
||||
"version": "0.1.0",
|
||||
"version": "2.0.17",
|
||||
"factorio_version": "0.17"
|
||||
},
|
||||
{
|
||||
"version": "0.1.1",
|
||||
"version": "2.0.18",
|
||||
"factorio_version": "0.18"
|
||||
},
|
||||
{
|
||||
"version": "0.1.2",
|
||||
"version": "2.0.11",
|
||||
"factorio_version": "1.1"
|
||||
},
|
||||
{
|
||||
"version": "0.1.3",
|
||||
"version": "2.0.20",
|
||||
"factorio_version": "2.0"
|
||||
}
|
||||
],
|
||||
|
||||
@@ -416,7 +416,11 @@ local players = {}
|
||||
for _, p in pairs(game.players) do
|
||||
players[p.name] = p.online_time
|
||||
end
|
||||
rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
|
||||
if helpers ~= nil then
|
||||
rcon.print(helpers.table_to_json(players))
|
||||
else
|
||||
rcon.print(game.table_to_json(players))
|
||||
end`.replace(/\r?\n/g, " ");
|
||||
let playerTimes: Record<string, number> = JSON.parse(await this.sendRcon(exportPlayerTimes));
|
||||
let count = 0;
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ import fs from "fs-extra";
|
||||
import * as libFileOps from "./file_ops";
|
||||
import { basicType } from "./helpers";
|
||||
|
||||
export type ItemCountWithQuality = { [quality: string]: number };
|
||||
|
||||
/**
|
||||
* Converts a Map with only string keys to a JavaScript object.
|
||||
@@ -154,6 +155,11 @@ function checkCount(count: unknown): asserts count is number {
|
||||
}
|
||||
}
|
||||
|
||||
function checkQuality(quality: unknown): asserts quality is string {
|
||||
if (typeof quality !== "string") {
|
||||
throw new Error("quality must be a string");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Item database
|
||||
@@ -164,7 +170,7 @@ function checkCount(count: unknown): asserts count is number {
|
||||
* deserialized the content is verified.
|
||||
*/
|
||||
export class ItemDatabase {
|
||||
private _items: Map<string, number> = new Map();
|
||||
private _items: Map<string, ItemCountWithQuality> = new Map();
|
||||
|
||||
/**
|
||||
* Create a new item database
|
||||
@@ -177,11 +183,17 @@ export class ItemDatabase {
|
||||
constructor(serialized?: object) {
|
||||
// Verify the content of the serialized database
|
||||
if (serialized !== undefined) {
|
||||
for (let [name, count] of Object.entries(serialized)) {
|
||||
for (let [name, qualities] of Object.entries(serialized)) {
|
||||
checkName(name);
|
||||
checkCount(count);
|
||||
// Migration from pre-quality database format
|
||||
if (typeof qualities === "number") {
|
||||
qualities = { normal: qualities };
|
||||
}
|
||||
for (const count of Object.values(qualities)) {
|
||||
checkCount(count);
|
||||
}
|
||||
|
||||
this._items.set(name, count);
|
||||
this._items.set(name, qualities);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -195,10 +207,15 @@ export class ItemDatabase {
|
||||
* @returns Serialized representation of the database
|
||||
*/
|
||||
serialize() {
|
||||
let obj: Record<string, number> = {};
|
||||
for (let [name, count] of this._items) {
|
||||
if (count !== 0) {
|
||||
obj[name] = count;
|
||||
let obj: Record<string, ItemCountWithQuality> = {};
|
||||
for (let [name, qualities] of this._items) {
|
||||
for (const quality of Object.getOwnPropertyNames(qualities)) {
|
||||
if (qualities[quality] !== 0) {
|
||||
if (!obj[name]) {
|
||||
obj[name] = {};
|
||||
}
|
||||
obj[name][quality] = qualities[quality];
|
||||
}
|
||||
}
|
||||
}
|
||||
return obj;
|
||||
@@ -222,16 +239,18 @@ export class ItemDatabase {
|
||||
* then 0 is returned.
|
||||
*
|
||||
* @param name - The name of the item to get the count of.
|
||||
* @param quality - The quality of the item to get the count of.
|
||||
* @returns The count of the item stored.
|
||||
*/
|
||||
getItemCount(name: string): number {
|
||||
getItemCount(name: string, quality: string): number {
|
||||
checkName(name);
|
||||
checkQuality(quality);
|
||||
|
||||
if (!this._items.has(name)) {
|
||||
if (!this._items.get(name)?.[quality]) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return this._items.get(name)!;
|
||||
return this._items.get(name)![quality];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -241,16 +260,21 @@ export class ItemDatabase {
|
||||
*
|
||||
* @param name - The name of the item to add.
|
||||
* @param count - The count of item to remove.
|
||||
* @param quality - The quality of the item to add.
|
||||
*/
|
||||
addItem(name: string, count: number) {
|
||||
addItem(name: string, count: number, quality: string) {
|
||||
checkName(name);
|
||||
checkCount(count);
|
||||
checkQuality(quality);
|
||||
|
||||
if (this._items.has(name)) {
|
||||
count += this._items.get(name)!;
|
||||
if (!this._items.has(name)) {
|
||||
this._items.set(name, { [quality]: count });
|
||||
return;
|
||||
}
|
||||
|
||||
this._items.set(name, count);
|
||||
// Nullish coalescing operator is used to avoid NaN when the quality is not present
|
||||
let currentCount = this._items.get(name)![quality] ?? 0;
|
||||
this._items.get(name)![quality] = currentCount + count;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -262,24 +286,17 @@ export class ItemDatabase {
|
||||
*
|
||||
* @param name - The name of the item to remove.
|
||||
* @param count - The count of items to remove.
|
||||
* @param quality - The quality of the item to remove.
|
||||
*/
|
||||
removeItem(name: string, count: number) {
|
||||
checkName(name);
|
||||
checkCount(count);
|
||||
|
||||
count = -count;
|
||||
if (this._items.has(name)) {
|
||||
count += this._items.get(name)!;
|
||||
}
|
||||
|
||||
this._items.set(name, count);
|
||||
removeItem(name: string, count: number, quality: string) {
|
||||
this.addItem(name, -count, quality);
|
||||
};
|
||||
|
||||
/**
|
||||
* Allow to iterate through the items.
|
||||
* @returns array of items.
|
||||
*/
|
||||
getEntries(): IterableIterator<[string, number]> {
|
||||
getEntries(): IterableIterator<[string, ItemCountWithQuality]> {
|
||||
return this._items.entries();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user