bingo.module.ts (1301B)
1 import { Service, Container } from 'typedi'; 2 import { lineByLine } from '@common/line-by-line'; 3 4 import { BingoController } from './bingo.controller'; 5 6 import { Board } from './model/board'; 7 import { Game } from './model/game'; 8 9 @Service() 10 export class BingoModule { 11 12 constructor( 13 private bingoController: BingoController 14 ) { 15 this.initializeDb(); 16 } 17 18 // TODO: move into dedicated file 19 async initializeDb() { 20 const db: {[index:string]:{[index:string]:any}[]} = Container.get('db'); 21 db.game = db.game || []; 22 db.board = db.board || []; 23 24 // Only a single input file exists 25 let game = null; 26 let board = null; 27 await lineByLine(__dirname + '/../../assets/input', async line => { 28 29 // Task defined first line as already-drawn numbers 30 if (!game) { 31 game = new Game({ 32 name : 'Base game', 33 drawn : line.split(',').map(v => parseInt(v)), 34 board : [] 35 }); 36 db.game.push(game); 37 return; 38 } 39 40 // Handle board closure 41 if (!line) { 42 board = new Board({ game }); 43 db.board.push(board); 44 game.board.push(board); 45 return; 46 } 47 48 // Parse the board values line 49 board.values.push(line.split(' ').filter(v => v).map(v => parseInt(v))); 50 }); 51 52 } 53 }