Exemplo n.º 1
0
func TestRemovePlayerChallenges(t *testing.T) {
	dao := NewLobbyDAO()

	playerName := "aron"

	{
		challenge := model.NewChallenge()
		challenge.ChallengeID = "abc123"
		challenge.ChallengedPlayer = playerName
		dao.CreateChallenge(challenge)
	}
	{
		challenge := model.NewChallenge()
		challenge.ChallengeID = "abc456"
		challenge.ChallengedPlayer = playerName
		dao.CreateChallenge(challenge)
	}
	challenges := dao.GetChallengesFor(playerName)
	assert.True(t, len(challenges) == 2, "Should be 2 challenges")
	fmt.Println(len(challenges), challenges)

	dao.RemoveChallenge("abc123")

	challenges = dao.GetChallengesFor(playerName)
	fmt.Println(challenges)
	assert.True(t, len(challenges) == 1, "Should be 1 challenge")
}
Exemplo n.º 2
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)
}
Exemplo n.º 3
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
}
Exemplo n.º 4
0
func TestRemoveChallenge(t *testing.T) {
	dao := NewLobbyDAO()

	challenge := model.NewChallenge()
	challenge.ChallengeID = "abc123"

	dao.CreateChallenge(challenge)
	dao.RemoveChallenge(challenge.ChallengeID)

	savedChallenge := dao.GetChallenge(challenge.ChallengeID)

	assert.Nil(t, savedChallenge, "The original challenge should be destroyed")
}
Exemplo n.º 5
0
func TestCreateSaveChallenge(t *testing.T) {
	dao := NewLobbyDAO()

	challenge := model.NewChallenge()
	challenge.ChallengeID = "abc123"

	dao.CreateChallenge(challenge)

	savedChallenge := dao.GetChallenge(challenge.ChallengeID)
	assert.NotNil(t, savedChallenge, "The original challenge should be rehydratable")

	assert.Equal(t, savedChallenge.ChallengeID, savedChallenge.ChallengeID, "The rehydrated challenge should have the same ID")
}
Exemplo n.º 6
0
func TestGetSetChallengeMatchID(t *testing.T) {
	dao := NewLobbyDAO()

	challenge := model.NewChallenge()
	challenge.ChallengeID = "abc123"
	challenge.ChallengedPlayer = "aron"
	dao.CreateChallenge(challenge)

	dao.SetChallengeMatchID(challenge.ChallengeID, "match123")
	persistedMatchID := dao.GetMatchIDForChallenge(challenge.ChallengeID)
	fmt.Println(persistedMatchID)
	assert.Equal(t, "match123", persistedMatchID, "Failed to find matchID for challenge")
}
Exemplo n.º 7
0
func TestGetExpiredChallenge(t *testing.T) {
	dao := dao.NewLobbyDAO()
	api := NewLobbyAPI(dao)

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

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

	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.Nil(t, failure)
	assert.Nil(t, resolvedChallenge)
}