54 lines
1.4 KiB
TypeScript
Raw Normal View History

2023-11-23 16:33:21 +08:00
import { ITableJson } from "./Core/ITableJson";
import { ITableRow } from "./Core/ITableRow";
export class TableManager {
private static _tableJsons: { [key: string]: ITableJson } = {};
public static AddJsonAssets(jsonAssets: JSON[]) {
if (!jsonAssets) return;
const newAssets: JSON[] = jsonAssets.concat();
for (const jsonAsset of newAssets) {
this.AddJsonAsset(jsonAsset);
}
}
2023-12-06 13:56:43 +08:00
public static AddJsonAsset(jsonAsset: any) {
2023-11-23 16:33:21 +08:00
if (!jsonAsset) {
return;
}
2023-12-06 13:56:43 +08:00
for (let tableName in jsonAsset.json) {
console.log(`TableV3 [${tableName}] json loaded`);
this._tableJsons[tableName] = jsonAsset.json[tableName];
2023-11-23 16:33:21 +08:00
}
}
public static GetTable(name: string): ITableJson {
return this._tableJsons[name];
}
public static InitTable<T extends Array<ITableRow>>(
name: string,
tableType: { new(): T },
rowType: { new(): ITableRow },
): T {
const json = this._tableJsons[name];
if (!json) {
return null;
// throw new Error(`TableV3 [${name}] 尚未載入json檔`);
}
const table = new tableType();
const cols = json.cols;
const colLength = cols.length;
const rows = json.rows;
for (const r of rows) {
const trow = new rowType();
for (let i = 0; i < colLength; i++) {
trow[cols[i]] = r[i];
}
table[trow.Id] = trow;
}
// console.log(`TableV3 [${name}] init done`);
return table;
}
}