Beispiel #1
0
func (gameWrapper *GameWrapper) MoveUnit(playerId int, request api.MoveRequest) (*api.MoveResponse, error) {
	player, err := gameWrapper.world.GetAndVerifyTurnOwner(playerId)
	if err != nil {
		return nil, err
	}
	locations := make([]game.Location, len(request.Move))
	for i, location := range request.Move {
		locations[i] = game.LocationFromRequest(location)
	}
	validError := gameWrapper.verifyValidMove(request.UnitId, player, locations)
	if validError != nil {
		return nil, validError
	}
	end := len(locations)
	err = gameWrapper.verifiedMoveUnit(request.UnitId, locations[0], locations[end-1])
	if err != nil {
		return nil, err
	}
	return &api.MoveResponse{
		Move:   request.Move,
		UnitId: request.UnitId}, nil
}
Beispiel #2
0
func (gameWrapper *GameWrapper) Attack(playerId int, request api.AttackRequest) (*api.AttackResponse, error) {
	player, err := gameWrapper.world.GetAndVerifyTurnOwner(playerId)
	if err != nil {
		return nil, err
	}
	attackingUnit, err := gameWrapper.world.GetAndVerifyOwnedUnit(player, request.Attacker)
	if err != nil {
		return nil, err
	}
	defendingUnit, err := gameWrapper.world.GetUnitAtLocation(game.LocationFromRequest(request.Target))
	if err != nil {
		return nil, err
	}
	alive, err := game.DamageUnit(attackingUnit, request.AttackIndex, defendingUnit)
	// TODO When a unit is dead mark is as such
	logger.Errorf("unit is %s (alive)", alive)
	if err != nil {
		return nil, err
	}
	return &api.AttackResponse{
		Attacker: request.Attacker, AttackIndex: request.AttackIndex,
		Target: request.Target}, nil
}