예제 #1
0
파일: main.go 프로젝트: DanLB/tictactoe
func newRegistration(w http.ResponseWriter, r *http.Request) {
	select {
	case gameId := <-waitingGames:
		gameToPlay := gameMap[gameId]
		gameToPlay.PlayerY = nextPlayerId
		gameMap[gameId] = gameToPlay
		jsonResults, err := json.Marshal(game.Info{
			GameId:     gameToPlay.GameId,
			PlayerId:   gameToPlay.PlayerY,
			OpponentId: gameToPlay.PlayerX,
			PlayerX:    gameToPlay.PlayerX,
		})

		if err != nil {
			handleError(w, err.Error())
		} else {
			nextPlayerId++
			w.Write(jsonResults)
			return
		}
	default:
		newGame := game.NewGame()
		newGame.GameId = generateGameId()
		newGame.PlayerX = nextPlayerId
		nextPlayerId = nextPlayerId + 1
		newGame.TurnId = newGame.PlayerX

		gameMap[newGame.GameId] = newGame
		waitingGames <- newGame.GameId
		newRegistration(w, r)
	}
}
예제 #2
0
파일: main.go 프로젝트: DanLB/tictactoe
func main() {
	initLogging()

	newGame := game.NewGame()
	newGame.GameId = generateGameId()
	newGame.PlayerX = 10000
	newGame.TurnId = newGame.PlayerX

	gameMap[newGame.GameId] = newGame
	waitingGames <- newGame.GameId

	game.Log("Starting the server on port 80")

	http.Handle("/", httpgzip.NewHandler(http.FileServer(http.Dir("./static/"))))
	http.HandleFunc("/tictactoe", index)
	http.HandleFunc("/tictactoe/register", newRegistration)
	http.HandleFunc("/tictactoe/forfeit", forfeit)
	http.HandleFunc("/tictactoe/turn", turnReceived)
	http.ListenAndServe(":80", nil)
}