More work

This commit is contained in:
Lordmau5 2023-11-14 23:14:36 +01:00
parent 6afe7675e9
commit 883098fd32
4 changed files with 2238 additions and 18 deletions

View File

@ -1,5 +1,9 @@
import { import {
Exclude,
Expose,
Type, Type,
instanceToPlain,
plainToClass,
plainToInstance plainToInstance
} from 'class-transformer'; } from 'class-transformer';
@ -60,7 +64,9 @@ export class BingoGame {
removeGroup(group: BingoGroup): boolean { removeGroup(group: BingoGroup): boolean {
// Group has parent, don't delete // Group has parent, don't delete
const isParent = this.groups.find(_group => _group.parent && stringCompare(_group.parent.name, group.name)); const isParent = this.groups.find(
_group => _group.parent && stringCompare(_group.parent.name, group.name)
);
if (isParent) { if (isParent) {
console.error('This group is still a parent of:', isParent.name); console.error('This group is still a parent of:', isParent.name);
@ -68,7 +74,9 @@ export class BingoGame {
} }
// One or more goals still use this group, don't delete // One or more goals still use this group, don't delete
const isGoal = this.goals.find(goal => goal.group && stringCompare(goal.group.name, group.name)); const isGoal = this.goals.find(
goal => goal.group && stringCompare(goal.group.name, group.name)
);
if (isGoal) { if (isGoal) {
console.error('This group is still used by:', isGoal.name); console.error('This group is still used by:', isGoal.name);
@ -91,7 +99,13 @@ export class BingoGame {
} }
getGoalsByTags(...tags: string[]): BingoGoal[] { getGoalsByTags(...tags: string[]): BingoGoal[] {
return this.goals.filter(goal => goal.tags.some(_tag => tags.some(tag => stringCompare(_tag, tag)))); return this.goals.filter(
goal => goal.tags.some(
_tag => tags.some(
tag => stringCompare(_tag, tag)
)
)
);
} }
getGoalsByGroup(includeParents: boolean, ...groups: BingoGroup[]): BingoGoal[] { getGoalsByGroup(includeParents: boolean, ...groups: BingoGroup[]): BingoGoal[] {
@ -131,6 +145,10 @@ export class BingoGoal {
name!: string; name!: string;
@Exclude({
toPlainOnly: true
})
@Type(() => BingoGroup)
group!: BingoGroup; group!: BingoGroup;
tags: string[] = []; tags: string[] = [];
@ -149,6 +167,11 @@ export class BingoGoal {
} }
} }
@Expose()
get group_id(): string {
return this.group.name;
}
addTag(tag: string): BingoGoal { addTag(tag: string): BingoGoal {
if (this.tags.includes(tag)) if (this.tags.includes(tag))
return this; return this;
@ -176,13 +199,21 @@ export class BingoGoal {
export class BingoGroup { export class BingoGroup {
name: string; name: string;
@Exclude({
toPlainOnly: true
})
@Type(() => BingoGroup) @Type(() => BingoGroup)
parent?: BingoGroup; parent?: BingoGroup;
constructor(name: string, parent?: BingoGroup) { constructor(name: string, parent?: BingoGroup | undefined) {
this.name = name; this.name = name;
this.parent = parent; this.parent = parent;
} }
@Expose()
get parent_id(): string | undefined {
return this.parent?.name;
}
} }
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
@ -193,30 +224,67 @@ function test(): void {
'The funny game we used for all bingos.', 'The funny game we used for all bingos.',
'Very long text here haha lmao' 'Very long text here haha lmao'
); );
const group1 = new BingoGroup('1');
const group2 = new BingoGroup('2', group1);
const group3 = new BingoGroup('3', group2);
game.addGroup(group1);
game.addGroup(group2);
game.addGroup(group3);
const battle_group = new BingoGroup('Battle'); const battle_group = new BingoGroup('Battle');
const defeat_enemies = new BingoGroup('Defeat enemies on the street', battle_group); const defeat_enemies = new BingoGroup('Defeat enemies on the street', battle_group);
game.addGroup(battle_group); game.addGroup(battle_group);
game.addGroup(defeat_enemies); game.addGroup(defeat_enemies);
const easy = new BingoGoal('Defeat 50 enemies on the street', defeat_enemies).addTag('Easy'); const easy = new BingoGoal('Defeat 50 enemies on the street', group1).addTag('Easy');
const normal = new BingoGoal('Defeat 150 enemies on the street', defeat_enemies).addTag('Normal'); const normal = new BingoGoal('Defeat 150 enemies on the street', group2).addTag('Normal');
const hard = new BingoGoal('Defeat 300 enemies on the street', defeat_enemies).addTag('Hard'); const hard = new BingoGoal('Defeat 300 enemies on the street', group3).addTag('Hard');
game.addGoal(easy); game.addGoal(easy);
game.addGoal(normal); game.addGoal(normal);
game.addGoal(hard); game.addGoal(hard);
// console.log(game.goals[0].group);
// game.groups[0].name = 'not 1';
// game.groups[0].parent = game.groups[1];
// console.log(game.goals[0].group);
// const jsonifiedInstance = instanceToPlain(game); // const jsonifiedInstance = instanceToPlain(game);
// console.log(JSON.stringify(jsonifiedInstance)); // const fixed = JSON.parse(JSON.stringify(jsonifiedInstance));
// console.log(JSON.stringify([ fixed ]));
// console.log(JSON.stringify(game)); // console.log(JSON.stringify(game));
const json_string = '{"id":"yakuza_0","name":"Yakuza 0","description":"The funny game we used for all bingos.","generator":"simple","goals":[{"name":"Defeat 50 enemies on the street","tags":["Easy"],"group":{"name":"Defeat enemies on the street","parent":{"name":"Battle"}},"possible_spaces":[]},{"name":"Defeat 150 enemies on the street","tags":["Normal"],"group":{"name":"Defeat enemies on the street","parent":{"name":"Battle"}},"possible_spaces":[]},{"name":"Defeat 300 enemies on the street","tags":["Hard"],"group":{"name":"Defeat enemies on the street","parent":{"name":"Battle"}},"possible_spaces":[]}],"groups":[{"name":"Battle"},{"name":"Defeat enemies on the street","parent":{"name":"Battle"}}]}'; // const got_group1 = game.getGroupByName('1');
const parsed: JSON = JSON.parse(json_string); // console.log(got_group1);
const parsed_game = plainToInstance(BingoGame, parsed);
console.log(parsed_game); // const got_group3 = game.getGroupByName('3');
// console.log(got_group3);
// console.log(got_group1 === got_group3?.parent?.parent);
// const jsonifiedString = JSON.stringify(jsonifiedInstance);
// const parsedInstance: JSON = JSON.parse(jsonifiedString);
// const parsedgame = plainToClass(BingoGame, parsedInstance);
// console.log(parsedgame);
// console.log(parsedgame === game);
// const got_group1 = parsedgame.getGroupByName('1');
// console.log(got_group1);
// const got_group3 = parsedgame.getGroupByName('3');
// console.log(got_group3);
// console.log(got_group1 === got_group3?.parent?.parent);
// const json_string = '{"id":"yakuza_0","name":"Yakuza 0","description":"The funny game we used for all bingos.","generator":"simple","goals":[{"name":"Defeat 50 enemies on the street","tags":["Easy"],"group":{"name":"Defeat enemies on the street","parent":{"name":"Battle"}},"possible_spaces":[]},{"name":"Defeat 150 enemies on the street","tags":["Normal"],"group":{"name":"Defeat enemies on the street","parent":{"name":"Battle"}},"possible_spaces":[]},{"name":"Defeat 300 enemies on the street","tags":["Hard"],"group":{"name":"Defeat enemies on the street","parent":{"name":"Battle"}},"possible_spaces":[]}],"groups":[{"name":"Battle"},{"name":"Defeat enemies on the street","parent":{"name":"Battle"}}]}';
// const parsed: JSON = JSON.parse(json_string);
// const parsed_game = plainToInstance(BingoGame, parsed);
// console.log(parsed_game);
// console.log(JSON.stringify(other_game) === JSON.stringify(jsonifiedInstance)); // console.log(JSON.stringify(other_game) === JSON.stringify(jsonifiedInstance));
// parsed_game.removeGroup(defeat_enemies); // parsed_game.removeGroup(defeat_enemies);
// console.log(parsed_game.goals); // console.log(parsed_game.goals);
} }
test();

View File

@ -4,15 +4,16 @@ import {
BingoGame, BingoGoal, BingoGroup BingoGame, BingoGoal, BingoGroup
} from '@/js/Bingo.js'; } from '@/js/Bingo.js';
import json from './games.json'; import json from './games.json';
import new_json from './new_games.json';
import { import {
instanceToPlain instanceToPlain, plainToClass, plainToInstance
} from 'class-transformer'; } from 'class-transformer';
import markdown_text from '@/js/testmarkdown.js'; import markdown_text from '@/js/testmarkdown.js';
export const games: Map<string, BingoGame> = new Map; export const games: Map<string, BingoGame> = new Map;
function run() { function run_old_format() {
for (const j_game of json) { for (const j_game of json) {
const game = new BingoGame( const game = new BingoGame(
j_game.game_name, j_game.game_name,
@ -57,4 +58,43 @@ function run() {
// console.log(goals); // console.log(goals);
} }
run(); function run_new_format() {
for (const j_game of new_json) {
const game = new BingoGame(
j_game.id,
j_game.name,
j_game.short_description,
j_game.description
);
const group_map: Map<string, BingoGroup> = new Map;
for (const j_group of j_game.groups) {
let group: BingoGroup;
if (j_group.parent_id)
group = new BingoGroup(j_group.name, group_map.get(
j_group.parent_id
));
else
group = new BingoGroup(j_group.name);
group_map.set(j_group.name, group);
game.addGroup(group);
}
for (const j_goal of j_game.goals) {
const group = group_map.get(j_goal.group_id);
if (!group)
continue;
const goal = new BingoGoal(j_goal.name, group);
goal.possible_spaces = j_goal.possible_spaces;
goal.tags = j_goal.tags;
game.addGoal(goal);
}
games.set(game.id, game);
}
}
// run_new_format();

File diff suppressed because one or more lines are too long

73
src/js/new_games.json Normal file
View File

@ -0,0 +1,73 @@
[
{
"id": "yakuza_0",
"name": "Yakuza 0",
"short_description": "The funny game we used for all bingos.",
"description": "Very long text here haha lmao",
"generator": "simple",
"goals": [
{
"name": "Defeat 50 enemies on the street",
"tags": [
"Easy"
],
"possible_spaces": [
1
],
"group_id": "1"
},
{
"name": "Defeat 150 enemies on the street",
"tags": [
"Normal"
],
"possible_spaces": [
0,
2
],
"group_id": "2"
},
{
"name": "Defeat 300 enemies on the street",
"tags": [
"Hard"
],
"possible_spaces": [
0,
3,
4,
6,
7,
10,
12,
15,
17,
18,
20,
22
],
"group_id": "3"
}
],
"groups": [
{
"name": "1"
},
{
"name": "2",
"parent_id": "1"
},
{
"name": "3",
"parent_id": "2"
},
{
"name": "Battle"
},
{
"name": "Defeat enemies on the street",
"parent_id": "Battle"
}
]
}
]