Beispiel #1
0
func (suite *SystemQueriesTestSuite) TestCompute() {
	assert := assert.New(suite.T())

	var (
		gameId game.Id = 5
		query  Query   = TurnNumberQuery(gameId)
	)

	suite.mockQueriesCache.
		On("Get", query).
		Return(false)

	fakeMoves := []events.Event{
		events.NewMoveEvent(gameId, 1, ""),
		events.NewMoveEvent(gameId, 2, ""),
		events.NewMoveEvent(gameId, 3, ""),
	}
	expected := game.TurnNumber(len(fakeMoves))
	suite.mockEvents.On("EventsOfTypeForGame", gameId, events.MoveType).Return(fakeMoves).Once()

	suite.mockQueriesCache.On("Store", query).Return().Once()

	actual := suite.systemQueries.AnswerQuery(query).(game.TurnNumber)
	assert.Equal(expected, actual)
}
Beispiel #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")
	}
}
Beispiel #3
0
func (suite *SystemQueriesTestSuite) TestQueryStorage() {
	var (
		gameId game.Id = 5
		query  Query   = TurnNumberQuery(gameId)
	)

	suite.mockQueriesCache.
		On("Get", query).
		Return(false)

	fakeMoves := []events.Event{
		events.NewMoveEvent(gameId, 1, ""),
		events.NewMoveEvent(gameId, 2, ""),
		events.NewMoveEvent(gameId, 3, ""),
	}
	suite.mockEvents.On("EventsOfTypeForGame", gameId, events.MoveType).Return(fakeMoves).Once()

	suite.mockQueriesCache.On("Store", query).Return().Once()

	suite.systemQueries.AnswerQuery(query)
	suite.mockQueriesCache.AssertCalled(suite.T(), "Store", query)
}
func (suite *TurnNumberQueryTestSuite) TestComputeResult() {
	var (
		gameId game.Id = 1
		query  *turnNumberQuery
	)

	query = TurnNumberQuery(gameId).(*turnNumberQuery)

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

	// case 1

	suite.mockEvents.
		On("EventsOfTypeForGame", gameId, events.MoveType).
		Return([]events.Event{}).
		Once()

	query.computeResult(suite.mockSystemQueries)

	assert.Equal(game.TurnNumber(0), query.Result)

	// case 2

	fakeMoves := []events.Event{
		events.NewMoveEvent(gameId, 1, ""),
		events.NewMoveEvent(gameId, 2, ""),
		events.NewMoveEvent(gameId, 3, ""),
	}
	suite.mockEvents.
		On("EventsOfTypeForGame", gameId, events.MoveType).
		Return(fakeMoves).
		Once()

	query.computeResult(suite.mockSystemQueries)
	assert.Equal(game.TurnNumber(3), query.Result)
}
func (suite *MoveAtTurnQueryTestSuite) TestComputeResult() {
	var (
		gameId     game.Id            = 7
		turnNumber game.TurnNumber    = 9
		move       game.AlgebraicMove = "Na2"

		query *moveAtTurnQuery
		event events.Event
	)

	event = events.NewMoveEvent(gameId, turnNumber, move)
	query = MoveAtTurnQuery(gameId, turnNumber).(*moveAtTurnQuery)

	assert := assert.New(suite.T())
	suite.mockEvents.
		On("MoveEventForGameAtTurn", gameId, turnNumber).
		Return(event)

	query.computeResult(suite.mockSystemQueries)
	assert.Equal(true, query.hasResult())
	assert.Equal(move, query.Result)
}