This commit is contained in:
Chris Chen
2024-02-28 15:17:41 -08:00
parent 42e7ee39be
commit 6301d6008b
124 changed files with 2655 additions and 667 deletions
@@ -0,0 +1,25 @@
<div class="row">
<div class="col-12 col-md-4">
<nb-card>
<nb-card-header>
Treasure Bag
<button nbButton hero status="primary" size="small">Reset</button>
<button nbButton hero status="primary" size="small" (click)="addTreasure(TreasureType.Rare)">Add
Rare</button>
<button nbButton hero status="primary" size="small" (click)="addTreasure(TreasureType.Epic)">Add
Epic</button>
</nb-card-header>
<nb-card-body>
<div>
<ng-template *ngFor="let treasure of treasureBag.drawingItems">
<img src="{{treasure.imageUrl}}"> X {{treasure.unitAmount}}
</ng-template>
</div>
<button nbButton hero status="primary">I feel LUCKY!!!!!</button>
</nb-card-body>
</nb-card>
</div>
</div>
@@ -0,0 +1,25 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { MassiveDarkness2Component } from './massive-darkness2.component';
describe('MassiveDarkness2Component', () => {
let component: MassiveDarkness2Component;
let fixture: ComponentFixture<MassiveDarkness2Component>;
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ MassiveDarkness2Component ]
})
.compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(MassiveDarkness2Component);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
@@ -0,0 +1,143 @@
import { CommonModule } from '@angular/common';
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, OnInit } from '@angular/core';
import { FileService } from '../../services/file.service';
import { MsgBoxService } from '../../services/msg-box.service';
import { ArrayUtils } from '../../utilities/array-utils';
import { ObjectUtils } from '../../utilities/object-utils';
@Component({
selector: 'ngx-massive-darkness2',
templateUrl: './massive-darkness2.component.html',
styleUrls: ['./massive-darkness2.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class MassiveDarkness2Component implements OnInit {
TreasureType = TreasureType;
treasureBag: DrawingBag = new DrawingBag();
constructor(
private fileService: FileService,
private cdRef: ChangeDetectorRef,
private msgBoxService: MsgBoxService
) { }
ngOnInit(): void {
this.resetTreasureBag();
this.detectChanges();
}
detectChanges() {
if (!this.cdRef['destroyed']) {
this.cdRef.detectChanges();
}
}
resetTreasureBag() {
this.treasureBag.ClearAllItems();
this.addTreasure(TreasureType.Common, 15);
}
addTreasure(type: TreasureType, amount: number = 1) {
let item = new DrawingItem(`${TreasureType[type]} Treasure`, `It's a ${TreasureType[type]} Treasure!`, this.fileService.ImageUrl(`TreasureToken/${TreasureType[type]}.png`), amount);
this.treasureBag.AddItem(item);
this.detectChanges();
}
treasureImage(type: TreasureType) {
return this.fileService.ImageUrl(`TreasureToken/${TreasureType[type]}.png`);
}
drawTreasure() {
}
}
export enum TreasureType {
Common,
Rare,
Epic,
Legendary
}
export class DrawingBag {
constructor() {
this.drawingItems = [];
this.removedItems = [];
}
drawingItems: DrawingItem[]
removedItems: DrawingItem[]
public Draw(amount: number): DrawingItem[] {
let drawItems: DrawingItem[] = [];
for (let i = 0; i < amount; i++) {
drawItems.push(this.DrawAndRemove());
}
this.RestoreRemoveItems();
return drawItems;
}
public DrawAndRemove(): DrawingItem {
if (this.drawingItems.length > 0) {
let drawItem = null as DrawingItem;
let drawIndex = Math.random() * this.drawingItems.reduce((sum, current) => sum + current.unitAmount, 0);
let drawCalc = 0;
for (let i = 0; i < this.drawingItems.length; i++) {
const item = this.drawingItems[i];
drawCalc += item.unitAmount;
if (drawCalc >= drawIndex) {
drawItem = ObjectUtils.CloneValue(item);
drawItem.unitAmount = 1;
item.unitAmount -= 1;
break;
}
}
//ObjectUtils.CloneValue
this.RemoveItem(drawItem);
return drawItem;
}
return null;
}
public RestoreRemoveItems() {
for (let i = 0; i < this.removedItems.length; i++) {
const removedItem = this.removedItems[i];
this.AddItem(removedItem);
}
}
public AddItem(item: DrawingItem) {
let existingItem = this.drawingItems.find(i => i.name == item.name);
if (existingItem) {
existingItem.unitAmount += item.unitAmount;
} else {
this.drawingItems.push(item);
}
}
public RemoveItem(item: DrawingItem) {
let existingItem = this.removedItems.find(i => i.name == item.name);
if (existingItem) {
existingItem.unitAmount += item.unitAmount;
} else {
this.removedItems.push(item);
}
}
public ClearAllItems() {
this.drawingItems = [];
this.removedItems = [];
}
}
export class DrawingItem {
constructor(
name: string,
description: string,
imageUrl: string,
unitAmount: number = 1
) {
this.imageUrl = imageUrl
this.name = name
this.description = description
this.unitAmount = unitAmount
}
imageUrl: string
name: string
description: string
unitAmount: number
}