コード例 #1
0
ファイル: game.go プロジェクト: kir-dev/torpedo
// 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)
}
コード例 #2
0
ファイル: player.go プロジェクト: kir-dev/torpedo
// 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
}
コード例 #3
0
ファイル: web.go プロジェクト: kir-dev/torpedo
// 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)
	}
}