Beispiel #1
0
func TestGetFirstNonExpiredChallenge(t *testing.T) {
	dao := dao.NewLobbyDAO()
	api := NewLobbyAPI(dao)

	challenger := util.UUID()
	challenged := util.UUID()

	api.JoinPlayer(challenger)
	api.JoinPlayer(challenged)

	var firstOne *model.Challenge
	{
		challenge := model.NewChallenge()
		challenge.ChallengeID = util.UUID()
		challenge.ChallengerPlayer = challenger
		challenge.ChallengedPlayer = challenged
		challenge.ChallengeTs = 100

		dao.CreateChallenge(challenge)

	}
	{
		challenge := model.NewChallenge()
		challenge.ChallengeID = util.UUID()
		challenge.ChallengerPlayer = challenger
		challenge.ChallengedPlayer = challenged
		challenge.ChallengeTs = util.Now()

		dao.CreateChallenge(challenge)
		firstOne = challenge
	}
	{
		challenge := model.NewChallenge()
		challenge.ChallengeID = util.UUID()
		challenge.ChallengerPlayer = challenger
		challenge.ChallengedPlayer = challenged
		challenge.ChallengeTs = util.Now()

		dao.CreateChallenge(challenge)

	}
	{
		challenge := model.NewChallenge()
		challenge.ChallengeID = util.UUID()
		challenge.ChallengerPlayer = challenger
		challenge.ChallengedPlayer = challenged
		challenge.ChallengeTs = 100

		dao.CreateChallenge(challenge)
	}

	resolvedChallenge, failure := api.GetChallengeFor(challenged)

	assert.NotNil(t, resolvedChallenge)
	assert.Nil(t, failure)
	assert.Equal(t, firstOne.ChallengeID, resolvedChallenge.ChallengeID)
}
Beispiel #2
0
/**
 * Creates a challenge between two players. If the challenge already exists, then an
 * exception is thrown
 * @param challengerPlayer
 * @param challengedPlayer
 * @return
 */
func (self *LobbyAPI) CreateChallenge(challengerPlayer, challengedPlayer string) (*model.Challenge, error) {
	failure := validatePlayer(self, challengerPlayer)
	if failure != nil {
		return nil, failure
	}

	failure = validatePlayer(self, challengedPlayer)
	if failure != nil {
		return nil, failure
	}

	challengeID := challengerPlayer + "_" + challengedPlayer
	challenge := self.lobbyDAO.GetChallenge(challengeID)
	if challenge != nil {
		// challenge already exists
		return nil, errors.New("Challenge already exists: " + challengeID)
	}

	challenge = model.NewChallenge()
	challenge.ChallengeID = challengeID
	challenge.ChallengedPlayer = challengedPlayer
	challenge.ChallengerPlayer = challengerPlayer
	challenge.ChallengeTs = util.Now()

	self.lobbyDAO.CreateChallenge(challenge)

	return challenge, nil
}
Beispiel #3
0
/**
 * Join a named player to the lobby, indicating that they are ready for a game challenge
 * @param playerName
 * @return
 */
func (self *LobbyAPI) JoinPlayer(playerName string) *model.Player {
	player := self.lobbyDAO.GetPlayer(playerName)
	if player == nil {
		player = model.NewPlayer()
		player.PlayerName = playerName
		player.JoinedTs = util.Now()
		self.lobbyDAO.SavePlayer(player)
	}

	return player
}