Пример #1
0
func (suite *UserGamesQueryTestSuite) TestComputeResult() {
	var (
		playerId      users.Id = "bob"
		query         *userGamesQuery
		activeGames   []game.Id = []game.Id{5, 6, 7}
		finishedGames []game.Id = []game.Id{1, 2, 3, 4}
		gameCreates   []events.Event
		gameStarts    []events.Event
		gameEnds      []events.Event
	)

	for _, id := range activeGames {
		gameCreates = append(gameCreates, events.NewGameCreateEvent(id, playerId, playerId))
		gameStarts = append(gameStarts, events.NewGameStartEvent(id, playerId, playerId))
	}
	for _, id := range finishedGames {
		gameCreates = append(gameCreates, events.NewGameCreateEvent(id, playerId, playerId))
		gameStarts = append(gameStarts, events.NewGameStartEvent(id, playerId, playerId))
		gameEnds = append(gameEnds, events.NewGameEndEvent(id, game.GameEndCheckmate, game.Black, playerId, playerId))
	}

	suite.mockEvents.
		On("EventsOfTypeForPlayer", playerId, events.GameCreateType).
		Return(gameCreates)

	suite.mockEvents.
		On("EventsOfTypeForPlayer", playerId, events.GameStartType).
		Return(gameStarts)

	suite.mockEvents.
		On("EventsOfTypeForPlayer", playerId, events.GameEndType).
		Return(gameEnds)

	query = UserGamesQuery(playerId).(*userGamesQuery)

	assert := assert.New(suite.T())

	assert.Equal(false, query.hasResult())

	query.computeResult(suite.mockSystemQueries)

	for _, id := range activeGames {
		assert.Contains(query.Result, id)
	}

	for _, id := range finishedGames {
		assert.Contains(query.Result, id)
	}

	assert.Equal(len(activeGames)+len(finishedGames), len(query.Result))

	assert.Equal(true, query.hasResult())
}
Пример #2
0
func (f *Fixtures) runGameFixtures() {
	var (
		numGames            int     = 16
		nextMoveProbability float64 = 0.8
	)

	usersService := f.Users.(*users.UsersService)
	users := usersService.GetAll()

	for gameId := 1; gameId <= numGames; gameId++ {
		shuffle := rand.Perm(len(users))
		white := users[shuffle[0]]
		black := users[shuffle[1]]

		log.Info("Creating game between %s and %s", white.Name, black.Name)

		whiteId := white.Uuid
		blackId := black.Uuid

		if rand.Intn(2) == 0 {
			f.Events.Receive(
				events.NewGameCreateEvent(game.Id(gameId), whiteId, ""),
			)
			time.Sleep(100 * time.Millisecond)
		} else {
			f.Events.Receive(
				events.NewGameCreateEvent(game.Id(gameId), "", blackId),
			)
			time.Sleep(100 * time.Millisecond)
		}

		f.Events.Receive(
			events.NewGameStartEvent(game.Id(gameId), whiteId, blackId),
		)
		time.Sleep(100 * time.Millisecond)

		for turnNumber := 1; ; turnNumber++ {
			if rand.Float64() > nextMoveProbability {
				break
			}

			validMoves, _ := f.Queries.ValidMoves(game.Id(gameId))
			if len(validMoves) == 0 {
				break
			}

			log.Info("Turn %d", turnNumber)

			log.Debug("got valid moves: %v", validMoves)
			randomMoveRecord := validMoves[rand.Intn(len(validMoves))]

			f.Events.Receive(
				events.NewMoveEvent(game.Id(gameId), game.TurnNumber(turnNumber), randomMoveRecord.Move),
			)
			time.Sleep(100 * time.Millisecond)
		}

		log.Info("Done")
	}
}