Created
October 2, 2018 07:50
-
-
Save pphetra/90fff157216718a3e1ff543a09d4edc3 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 { Controller, Post, Get, Param, Put, Res } from "@nestjs/common"; | |
import { HangmanService } from "hangman.service"; | |
@Controller('hangman') | |
export class HangmanController { | |
constructor(private readonly hangmanService: HangmanService) {} | |
@Post() | |
newGame() { | |
return this.hangmanService.createNewGame() | |
} | |
@Get(':id') | |
currentState(@Param('id') gameId) { | |
return this.hangmanService.currentState(gameId) | |
} | |
@Put(':id/:letter') | |
guess(@Param('id') gameId, @Param('letter') letter) { | |
return this.hangmanService.guess(gameId, letter) | |
} | |
@Get(':id/timer') | |
stream(@Param('id') gameId, @Res() res) { | |
this.hangmanService.subscribe(gameId, s => { | |
if (s.status === 'in-progress') { | |
res.write(`{ data: {lifeLeft: ${s.lifeLeft}, timeLeft: ${s.timeLeft}, status: ${s.status}}}\n`) | |
} else { | |
res.end() | |
} | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment