add guard against improperly structured json
This commit is contained in:
		
							parent
							
								
									b7c8f011dc
								
							
						
					
					
						commit
						328d697873
					
				
							
								
								
									
										20
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										20
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							| @ -1,17 +1,18 @@ | ||||
| { | ||||
|   "name": "notmudae", | ||||
|   "name": "2023collectabot", | ||||
|   "version": "1.0.0", | ||||
|   "lockfileVersion": 2, | ||||
|   "requires": true, | ||||
|   "packages": { | ||||
|     "": { | ||||
|       "name": "notmudae", | ||||
|       "name": "2023collectabot", | ||||
|       "version": "1.0.0", | ||||
|       "license": "MIT", | ||||
|       "dependencies": { | ||||
|         "discord.js": "^14.8.0", | ||||
|         "sequelize": "^6.30.0", | ||||
|         "sqlite3": "^5.1.6" | ||||
|         "sqlite3": "^5.1.6", | ||||
|         "zod": "^3.21.4" | ||||
|       }, | ||||
|       "devDependencies": { | ||||
|         "@types/node": "^18.15.11", | ||||
| @ -1600,6 +1601,14 @@ | ||||
|       "version": "4.0.0", | ||||
|       "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", | ||||
|       "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==" | ||||
|     }, | ||||
|     "node_modules/zod": { | ||||
|       "version": "3.21.4", | ||||
|       "resolved": "https://registry.npmjs.org/zod/-/zod-3.21.4.tgz", | ||||
|       "integrity": "sha512-m46AKbrzKVzOzs/DZgVnG5H55N1sv1M8qZU3A8RIKbs3mrACDNeIOeilDymVb2HdmP8uwshOCF4uJ8uM9rCqJw==", | ||||
|       "funding": { | ||||
|         "url": "https://github.com/sponsors/colinhacks" | ||||
|       } | ||||
|     } | ||||
|   }, | ||||
|   "dependencies": { | ||||
| @ -2770,6 +2779,11 @@ | ||||
|       "version": "4.0.0", | ||||
|       "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", | ||||
|       "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==" | ||||
|     }, | ||||
|     "zod": { | ||||
|       "version": "3.21.4", | ||||
|       "resolved": "https://registry.npmjs.org/zod/-/zod-3.21.4.tgz", | ||||
|       "integrity": "sha512-m46AKbrzKVzOzs/DZgVnG5H55N1sv1M8qZU3A8RIKbs3mrACDNeIOeilDymVb2HdmP8uwshOCF4uJ8uM9rCqJw==" | ||||
|     } | ||||
|   } | ||||
| } | ||||
|  | ||||
| @ -17,6 +17,7 @@ | ||||
|   "dependencies": { | ||||
|     "discord.js": "^14.8.0", | ||||
|     "sequelize": "^6.30.0", | ||||
|     "sqlite3": "^5.1.6" | ||||
|     "sqlite3": "^5.1.6", | ||||
|     "zod": "^3.21.4" | ||||
|   } | ||||
| } | ||||
|  | ||||
| @ -1,5 +1,6 @@ | ||||
| import charactersJSON from './characters.json' | ||||
| import { Collection } from 'discord.js' | ||||
| import charactersJSON from './characters.json'; | ||||
| import { z } from 'zod' | ||||
| 
 | ||||
| export enum Target { | ||||
|     Self = "self", | ||||
| @ -10,41 +11,53 @@ export enum Target { | ||||
|     TrueRandomOpponent = "trueRandomOpponent" | ||||
| } | ||||
| 
 | ||||
| export interface PotencyStatus { | ||||
|     duration: number | ||||
|     potency: number | ||||
| } | ||||
| const TargetEnum = z.nativeEnum(Target); | ||||
| type TargetEnum = z.infer<typeof TargetEnum>; | ||||
| 
 | ||||
| interface Effect { | ||||
|     target: Target | ||||
|     accuracy?: number | ||||
|     damage?: number | ||||
|     heal?: number | ||||
|     poison?: number | ||||
|     regeneration?: number | ||||
|     burn?: number | ||||
|     confusion?: number | ||||
|     stun?: number | ||||
|     resistanceChange?: PotencyStatus | ||||
|     accuracyChange?: PotencyStatus | ||||
|     speedChange?: PotencyStatus | ||||
|     function?: string | ||||
| } | ||||
| const PotencyStatus = z.object({ | ||||
|     duration: z.number().int(), | ||||
|     potency: z.number().int() | ||||
| }); | ||||
| export type PotencyStatus = z.infer<typeof PotencyStatus>; | ||||
| 
 | ||||
| interface Skill { | ||||
|     name: string | ||||
|     accuracy?: number | ||||
|     effects: Effect[] | ||||
| } | ||||
| const Effect = z.object({ | ||||
|     target: TargetEnum, | ||||
|     accuracy: z.number().int(), | ||||
|     damage: z.optional(z.number().int()), | ||||
|     heal: z.optional(z.number().int()), | ||||
|     poison: z.optional(z.number().int()), | ||||
|     regeneration: z.optional(z.number().int()), | ||||
|     burn: z.optional(z.number().int()), | ||||
|     confusion: z.optional(z.number().int()), | ||||
|     stun: z.optional(z.number().int()), | ||||
|     resistanceChange: z.optional(z.number().int()), | ||||
|     accuracyChange: z.optional(z.number().int()), | ||||
|     speedChange: z.optional(z.number().int()), | ||||
|     function: z.string() | ||||
| }); | ||||
| type Effect = z.infer<typeof Effect>; | ||||
| 
 | ||||
| export interface Character { | ||||
|     id: number | ||||
|     name: string | ||||
|     nameShort: string | ||||
|     img: string | ||||
|     health: number | ||||
|     speed: number | ||||
|     skills: [Skill, Skill, Skill] | ||||
| } | ||||
| const Skill = z.object({ | ||||
|     name: z.string(), | ||||
|     accuracy: z.number().int(), | ||||
|     effects: z.array(Effect) | ||||
| }); | ||||
| type Skill = z.infer<typeof Skill>; | ||||
| 
 | ||||
| export const CHARACTERS: Collection<number, Character> = new Collection(charactersJSON.map((element, index) => {return [index, element as Character]})); | ||||
| const Character = z.object({ | ||||
|     id: z.number().int(), | ||||
|     name: z.string(), | ||||
|     nameShort: z.string(), | ||||
|     img: z.string(), | ||||
|     health: z.number().int(), | ||||
|     speed: z.number().int(), | ||||
|     skills: z.array(Skill) | ||||
| }); | ||||
| export type Character = z.infer<typeof Character>; | ||||
| 
 | ||||
| const CharacterJSON = z.array(Character); | ||||
| type CharacterJSON = z.infer<typeof CharacterJSON>; | ||||
| 
 | ||||
| export const CHARACTERS: Collection<number, Character> = (() => { | ||||
|     return new Collection(CharacterJSON.parse(charactersJSON).map((element, index) => {return [index, element]})); | ||||
| })(); | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user