示例#1
0
func (s *ClientQueryService) GameHistory(gameId game.Id) ([]game.MoveRecord, bool) {
	var (
		history []game.MoveRecord = []game.MoveRecord{}
	)

	gameStatus := s.SystemQueries.AnswerQuery(GameQuery(gameId)).(GameStatus)
	if gameStatus == GameStatusNull {
		return history, false
	}

	turnNumberQ := TurnNumberQuery(gameId)
	turnNumber := s.SystemQueries.AnswerQuery(turnNumberQ).(game.TurnNumber)

	for i := 0; i <= int(turnNumber); i++ {
		stateQ := BoardAtTurnQuery(gameId, game.TurnNumber(i))
		state := s.SystemQueries.AnswerQuery(stateQ).(game.FEN)

		var move game.AlgebraicMove
		if i == 0 {
			move = ""
		} else {
			moveQ := MoveAtTurnQuery(gameId, game.TurnNumber(i))
			move = s.SystemQueries.AnswerQuery(moveQ).(game.AlgebraicMove)
		}

		record := game.MoveRecord{Move: move, ResultingBoardState: state}

		history = append(history, record)
	}

	return history, true
}
示例#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")
	}
}
示例#3
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)
}
示例#4
0
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)
}
示例#5
0
func (suite *ClientQueriesTestSuite) TestGameHistory() {
	assert := assert.New(suite.T())
	var (
		gameId game.Id = 11

		moves []game.AlgebraicMove = []game.AlgebraicMove{
			"move1",
			"move2",
			"move3",
		}

		states []game.FEN = []game.FEN{
			"start",
			"turn1",
			"turn2",
			"turn3",
		}
	)

	suite.mockSystemQueries.
		On("AnswerQuery", GameQuery(gameId)).
		Return(GameStatusStarted)
	suite.mockSystemQueries.
		On("AnswerQuery", TurnNumberQuery(gameId)).
		Return(game.TurnNumber(len(moves)))

	for i, move := range moves {
		query := MoveAtTurnQuery(gameId, game.TurnNumber(i+1))

		suite.mockSystemQueries.
			On("AnswerQuery", query).
			Return(move)
	}

	for i, state := range states {
		query := BoardAtTurnQuery(gameId, game.TurnNumber(i))

		suite.mockSystemQueries.
			On("AnswerQuery", query).
			Return(state)
	}

	history, found := suite.clientQueries.GameHistory(gameId)

	assert.Equal(true, found)
	assert.Equal(len(states), len(history))

	for i, record := range history {
		var expectedMove game.AlgebraicMove
		if i == 0 {
			expectedMove = ""
		} else {
			expectedMove = moves[i-1]
		}

		expectedState := states[i]

		assert.Equal(expectedMove, record.Move)
		assert.Equal(expectedState, record.ResultingBoardState)
	}
}
示例#6
0
func (q *turnNumberQuery) computeResult(queries SystemQueries) {
	moves := queries.getEvents().EventsOfTypeForGame(q.GameId, events.MoveType)
	q.Result = game.TurnNumber(len(moves))
}