1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- export function ParseGameStrings(games: string[]): Game[] {
- const parsedGames: Game[] = [];
- const gameIDRegex = new RegExp(/Game (\d+)/);
- const resultsRegex = new RegExp(/(\d+) (red|blue|green)/, "g");
- for(const gameString of games) {
-
- const [gamePart, setsPart] = gameString.split(": ");
-
- const gameID = gamePart.match(gameIDRegex);
- if (gameID == null) {
- throw new Error(`Failed to parse Game ID\n${gameString}`);
- }
-
- const parsed: Game = {
- ID: Number(gameID[1]),
- Sets: []
- };
-
- const setStrings = setsPart.split(/; /);
-
- for(const setString of setStrings) {
-
- const results = setString.matchAll(resultsRegex);
- if (results == null) {
- throw new Error(`Failed to parse set results\n${setString}`);
- }
- const parsedSet: Set = {
- red: 0,
- green: 0,
- blue: 0
- };
- for (const result of results) {
- const amountPulled = Number(result[1]);
- const colorPulled = result[2];
- parsedSet[colorPulled as keyof Set] = amountPulled;
- }
-
- parsed.Sets.push(parsedSet);
- }
-
- parsedGames.push(parsed);
- }
- return parsedGames;
- }
- export type Game = {
-
- ID: number,
-
- Sets: Set[]
- };
- export type Set = {
-
- red: number,
-
- green: number,
-
- blue: number,
- };
|