nitro/apps/frontend/src/api/avatar/CategoryBaseModel.ts

216 lines
5.3 KiB
TypeScript
Raw Normal View History

2023-03-17 00:22:55 +01:00
import {AvatarEditorUtilities} from "./AvatarEditorUtilities";
import {CategoryData} from "./CategoryData";
import {IAvatarEditorCategoryModel} from "./IAvatarEditorCategoryModel";
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
export class CategoryBaseModel implements IAvatarEditorCategoryModel {
protected _categories: Map<string, CategoryData>;
protected _isInitalized: boolean;
protected _maxPaletteCount: number;
private _disposed: boolean;
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
constructor() {
this._isInitalized = false;
this._maxPaletteCount = 0;
}
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
public dispose(): void {
this._categories = null;
this._disposed = true;
}
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
public get disposed(): boolean {
return this._disposed;
}
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
public init(): void {
if (!this._categories) this._categories = new Map();
}
public reset(): void {
this._isInitalized = false;
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
if (this._categories) {
for (const category of this._categories.values()) category && category.dispose();
2023-03-16 23:54:48 +01:00
}
2023-03-17 00:22:55 +01:00
this._categories = new Map();
}
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
protected addCategory(name: string): void {
let existing = this._categories.get(name);
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
if (existing) return;
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
existing = AvatarEditorUtilities.createCategory(this, name);
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
if (!existing) return;
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
this._categories.set(name, existing);
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
this.updateSelectionsFromFigure(name);
}
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
protected updateSelectionsFromFigure(figure: string): void {
const category = this._categories.get(figure);
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
if (!category) return;
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
const setId = AvatarEditorUtilities.CURRENT_FIGURE.getPartSetId(figure);
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
let colorIds = AvatarEditorUtilities.CURRENT_FIGURE.getColorIds(figure);
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
if (!colorIds) colorIds = [];
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
category.selectPartId(setId);
category.selectColorIds(colorIds);
}
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
public hasClubSelectionsOverLevel(level: number): boolean {
if (!this._categories) return false;
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
for (const category of this._categories.values()) {
if (!category) continue;
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
if (category.hasClubSelectionsOverLevel(level)) return true;
2023-03-16 23:54:48 +01:00
}
2023-03-17 00:22:55 +01:00
return false;
}
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
public hasInvalidSelectedItems(ownedItems: number[]): boolean {
if (!this._categories) return false;
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
for (const category of this._categories.values()) {
if (category.hasInvalidSelectedItems(ownedItems)) return true;
2023-03-16 23:54:48 +01:00
}
2023-03-17 00:22:55 +01:00
return false;
}
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
public stripClubItemsOverLevel(level: number): boolean {
if (!this._categories) return false;
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
let didStrip = false;
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
for (const [name, category] of this._categories.entries()) {
let isValid = false;
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
if (category.stripClubItemsOverLevel(level)) isValid = true;
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
if (category.stripClubColorsOverLevel(level)) isValid = true;
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
if (isValid) {
const partItem = category.getCurrentPart();
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
if (partItem && AvatarEditorUtilities.CURRENT_FIGURE) {
AvatarEditorUtilities.CURRENT_FIGURE.savePartData(name, partItem.id, category.getSelectedColorIds(), true);
2023-03-16 23:54:48 +01:00
}
2023-03-17 00:22:55 +01:00
didStrip = true;
}
2023-03-16 23:54:48 +01:00
}
2023-03-17 00:22:55 +01:00
return didStrip;
}
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
public stripInvalidSellableItems(): boolean {
if (!this._categories) return false;
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
let didStrip = false;
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
for (const [name, category] of this._categories.entries()) {
const isValid = false;
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
// if(category._Str_8360(this._Str_2278.manager.inventory)) _local_6 = true;
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
if (isValid) {
const partItem = category.getCurrentPart();
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
if (partItem && AvatarEditorUtilities.CURRENT_FIGURE) {
AvatarEditorUtilities.CURRENT_FIGURE.savePartData(name, partItem.id, category.getSelectedColorIds(), true);
2023-03-16 23:54:48 +01:00
}
2023-03-17 00:22:55 +01:00
didStrip = true;
}
2023-03-16 23:54:48 +01:00
}
2023-03-17 00:22:55 +01:00
return didStrip;
}
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
public selectPart(category: string, partIndex: number): void {
const categoryData = this._categories.get(category);
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
if (!categoryData) return;
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
const selectedPartIndex = categoryData.selectedPartIndex;
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
categoryData.selectPartIndex(partIndex);
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
const partItem = categoryData.getCurrentPart();
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
if (!partItem) return;
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
if (partItem.isDisabled) {
categoryData.selectPartIndex(selectedPartIndex);
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
// open hc window
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
return;
2023-03-16 23:54:48 +01:00
}
2023-03-17 00:22:55 +01:00
this._maxPaletteCount = partItem.maxColorIndex;
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
AvatarEditorUtilities.CURRENT_FIGURE.savePartData(category, partItem.id, categoryData.getSelectedColorIds(), true);
}
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
public selectColor(category: string, colorIndex: number, paletteId: number): void {
const categoryData = this._categories.get(category);
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
if (!categoryData) return;
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
const paletteIndex = categoryData.getCurrentColorIndex(paletteId);
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
categoryData.selectColorIndex(colorIndex, paletteId);
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
const colorItem = categoryData.getSelectedColor(paletteId);
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
if (colorItem.isDisabled) {
categoryData.selectColorIndex(paletteIndex, paletteId);
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
// open hc window
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
return;
2023-03-16 23:54:48 +01:00
}
2023-03-17 00:22:55 +01:00
AvatarEditorUtilities.CURRENT_FIGURE.savePartSetColourId(category, categoryData.getSelectedColorIds(), true);
}
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
public getCategoryData(category: string): CategoryData {
if (!this._isInitalized) this.init();
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
if (!this._categories) return null;
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
return this._categories.get(category);
}
2023-03-16 23:54:48 +01:00
2023-03-17 00:22:55 +01:00
public get categories(): Map<string, CategoryData> {
return this._categories;
}
public get canSetGender(): boolean {
return false;
}
public get maxPaletteCount(): number {
return this._maxPaletteCount || 1;
}
public set maxPaletteCount(count: number) {
this._maxPaletteCount = count;
}
public get name(): string {
return null;
}
2023-03-16 23:54:48 +01:00
}