Last active
October 2, 2018 08:17
-
-
Save pphetra/b91609b2b98a8724e2b924a3225aa1c9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from "@nestjs/common"; | |
import { DictionaryService } from "dictionary.service"; | |
import { State, HangmanGame } from "hangman"; | |
import { Observable } from "rxjs"; | |
import { first } from "rxjs/operators"; | |
const games = new Map<string, HangmanGame>() | |
var currentGameId = 1 | |
@Injectable() | |
export class HangmanService { | |
constructor(private readonly dictService: DictionaryService) {} | |
createNewGame(): Observable<State> { | |
const secretWord = this.dictService.randomWord() | |
const gameId = `${currentGameId++}` | |
const game = new HangmanGame(secretWord, gameId) | |
games.set(gameId, game) | |
return this.currentState(gameId) | |
} | |
currentState(gameId: string): Observable<State>{ | |
if (games.has(gameId)) { | |
return games.get(gameId).currentState().pipe(first()) | |
} | |
return null | |
} | |
guess(gameId: string, letter: string): Observable<State>{ | |
if (games.has(gameId)) { | |
const game = games.get(gameId) | |
return game.guess(letter).pipe(first()) | |
} | |
return null | |
} | |
subscribe(gameId: string, cb: (State)=>void) { | |
if (games.has(gameId)) { | |
games.get(gameId).currentState().subscribe(cb) | |
} else { | |
cb({ | |
status: '-' | |
}) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from "@nestjs/common"; | |
@Injectable() | |
export class DictionaryService { | |
words = [ | |
'adventurous', | |
'courageous', | |
'extramundane', | |
'generous', | |
'intransigent', | |
'sympathetic', | |
'vagarious', | |
'witty' | |
] | |
randomWord(): string { | |
const idx = Math.floor((Math.random() * this.words.length)); | |
return this.words[idx] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment