Exemple #1
0
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)
	}
}
Exemple #2
0
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)
}