Beispiel #1
0
// Gets the next player in the game. Order based on first came first serve.
func (g *Game) getNextPlayer(playerId string) (*Player, error) {
	for i := range g.Players {
		if g.Players[i].Id == playerId {
			// when we reach the last player, select the first one again
			if len(g.Players) == i+1 {
				return g.Players[0], nil
			} else {
				return g.Players[i+1], nil
			}
		}
	}

	return nil, util.Errorf("No such player: %s", playerId)
}
Beispiel #2
0
// Adds a player to the registry.
func (player *Player) Join(game *Game) error {
	util.LogInfo("New player (with name: %s) attempts to join the game.", player.Name)
	if game.hasAlreadyJoined(player) {
		return util.Errorf("Player with name %s has already joined the game.", player.Name)
	}

	err := game.Board.placeShips(game.Players, player)
	if err != nil {
		return err
	}

	game.addPlayer(player)

	util.LogInfo("Player with name %s (id: %s) has joined the game.", player.Name, player.Id)
	return nil
}
Beispiel #3
0
// join handler
func joinHandler(rw http.ResponseWriter, req *http.Request) {
	if !canJoin(req) {
		renderError(rw, util.Errorf("You are already playing! You cannot join twice."))
		return
	}

	player := engine.NewPlayer(req.FormValue("username"))
	player.IsBot = covertCheckboxValueToBool(req.FormValue("is_robot"))

	err := player.Join(currentGame)
	if err != nil {
		util.LogWarn("Player could not join. Cause: %s", err.Error())
		renderTemplate(rw, ERROR_TEMPLATE, errorView{
			// TODO: move to properties file
			"Játékos nem tudott csatlakozni.",
			err.Error(),
			util.IsDev(),
		})
	} else {
		http.SetCookie(rw, &http.Cookie{Name: PLAYER_ID_COOKIE, Value: player.Id, HttpOnly: true})
		http.Redirect(rw, req, "/shoot", http.StatusFound)
	}
}