54 lines
1.4 KiB
TypeScript
54 lines
1.4 KiB
TypeScript
|
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);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static AddJsonAsset(jsonAsset: JSON) {
|
||
|
if (!jsonAsset) {
|
||
|
return;
|
||
|
}
|
||
|
for (const tableName in jsonAsset) {
|
||
|
console.debug(`TableV3 [${ tableName }] json loaded`);
|
||
|
this._tableJsons[tableName] = jsonAsset[tableName];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
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;
|
||
|
}
|
||
|
}
|