Search in sources :

Example 1 with WrappedStaticStateRequestManager

use of com.minecolonies.coremod.colony.requestsystem.management.manager.wrapped.WrappedStaticStateRequestManager in project minecolonies by Minecolonies.

the class RequestHandler method resolveRequest.

/**
 * Method used to resolve a request.
 * When this method is called the given request has to be assigned.
 *
 * @param manager The manager requesting the resolving.
 * @param request The request about to be resolved.
 * @throws IllegalArgumentException when the request is unknown, not resolved, or cannot be resolved.
 */
@SuppressWarnings({ UNCHECKED, RAWTYPES })
public static void resolveRequest(final IStandardRequestManager manager, final IRequest request) {
    getRequest(manager, request.getToken());
    if (!isAssigned(manager, request.getToken())) {
        throw new IllegalArgumentException("The given request is not resolved");
    }
    if (request.getState() != RequestState.IN_PROGRESS) {
        throw new IllegalArgumentException("The given request is not in the right state. Required: " + RequestState.IN_PROGRESS + " - Found:" + request.getState());
    }
    if (request.hasChildren()) {
        throw new IllegalArgumentException("Cannot resolve request with open Children");
    }
    final IRequestResolver resolver = ResolverHandler.getResolverForRequest(manager, request);
    request.setState(new WrappedStaticStateRequestManager(manager), RequestState.IN_PROGRESS);
    resolver.resolve(manager, request);
}
Also used : IRequestResolver(com.minecolonies.api.colony.requestsystem.resolver.IRequestResolver) WrappedStaticStateRequestManager(com.minecolonies.coremod.colony.requestsystem.management.manager.wrapped.WrappedStaticStateRequestManager)

Example 2 with WrappedStaticStateRequestManager

use of com.minecolonies.coremod.colony.requestsystem.management.manager.wrapped.WrappedStaticStateRequestManager in project minecolonies by Minecolonies.

the class RequestHandler method assignRequestDefault.

/**
 * Method used to assign a given request to a resolver. Does take a given blacklist of resolvers into account.
 * Uses the default assigning strategy: {@link AssigningStrategy#PRIORITY_BASED}
 *
 * @param manager                The manager to modify.
 * @param request                The request to assign.
 * @param resolverTokenBlackList Each resolver that has its token in this blacklist will be skipped when checking for a possible resolver.
 * @return The token of the resolver that has gotten the request assigned, null if none was found.
 *
 * @throws IllegalArgumentException is thrown when the request is unknown to this manager.
 */
@SuppressWarnings({ UNCHECKED, RAWTYPES })
public static IToken<?> assignRequestDefault(final IStandardRequestManager manager, final IRequest request, final Collection<IToken<?>> resolverTokenBlackList) {
    // Check if the request is registered
    getRequest(manager, request.getToken());
    LogHandler.log("Starting resolver assignment search for request: " + request);
    request.setState(new WrappedStaticStateRequestManager(manager), RequestState.ASSIGNING);
    final Set<TypeToken> requestTypes = ReflectionUtils.getSuperClasses(request.getRequestType());
    requestTypes.remove(TypeConstants.OBJECT);
    final List<TypeToken> typeIndexList = new LinkedList<>(requestTypes);
    final Set<IRequestResolver<?>> resolvers = requestTypes.stream().filter(typeToken -> manager.getRequestableTypeRequestResolverAssignmentDataStore().getAssignments().containsKey(typeToken)).flatMap(type -> manager.getRequestableTypeRequestResolverAssignmentDataStore().getAssignments().get(type).stream().map(iToken -> ResolverHandler.getResolver(manager, iToken))).filter(iRequestResolver -> typeIndexList.contains(iRequestResolver.getRequestType())).sorted(Comparator.comparingInt((IRequestResolver<?> r) -> -1 * r.getPriority()).thenComparingInt((IRequestResolver<?> r) -> typeIndexList.indexOf(r.getRequestType()))).collect(Collectors.toCollection(LinkedHashSet::new));
    for (final IRequestResolver<?> resolver : resolvers) {
        // Skip when the resolver is in the blacklist.
        if (resolverTokenBlackList.contains(resolver.getRequesterId())) {
            continue;
        }
        // Skip if preliminary check fails
        if (!resolver.canResolve(manager, request)) {
            continue;
        }
        @Nullable final List<IToken<?>> attemptResult = resolver.attemptResolve(new WrappedBlacklistAssignmentRequestManager(manager, resolverTokenBlackList), request);
        // Skip if attempt failed (aka attemptResult == null)
        if (attemptResult == null) {
            continue;
        }
        // Successfully found a resolver. Registering
        LogHandler.log("Finished resolver assignment search for request: " + request + " successfully");
        ResolverHandler.addRequestToResolver(manager, resolver, request);
        for (final IToken<?> childRequestToken : attemptResult) {
            @SuppressWarnings(RAWTYPES) final IRequest childRequest = RequestHandler.getRequest(manager, childRequestToken);
            childRequest.setParent(request.getToken());
            request.addChild(childRequest.getToken());
            if (!isAssigned(manager, childRequestToken)) {
                assignRequest(manager, childRequest, resolverTokenBlackList);
            }
        }
        if (request.getState().ordinal() < RequestState.IN_PROGRESS.ordinal()) {
            request.setState(new WrappedStaticStateRequestManager(manager), RequestState.IN_PROGRESS);
            if (!request.hasChildren()) {
                resolveRequest(manager, request);
            }
        }
        return resolver.getRequesterId();
    }
    return null;
}
Also used : RequestMappingHandler(com.minecolonies.api.colony.requestsystem.manager.RequestMappingHandler) IRequest(com.minecolonies.api.colony.requestsystem.request.IRequest) IRequester(com.minecolonies.api.colony.requestsystem.requester.IRequester) java.util(java.util) UNCHECKED(com.minecolonies.api.util.constant.Suppression.UNCHECKED) RequestState(com.minecolonies.api.colony.requestsystem.request.RequestState) ImmutableCollection(com.google.common.collect.ImmutableCollection) IToken(com.minecolonies.api.colony.requestsystem.token.IToken) TypeToken(com.google.common.reflect.TypeToken) IRequestable(com.minecolonies.api.colony.requestsystem.requestable.IRequestable) Collectors(java.util.stream.Collectors) WrappedStaticStateRequestManager(com.minecolonies.coremod.colony.requestsystem.management.manager.wrapped.WrappedStaticStateRequestManager) Nullable(org.jetbrains.annotations.Nullable) IRequestResolver(com.minecolonies.api.colony.requestsystem.resolver.IRequestResolver) TypeConstants(com.minecolonies.api.util.constant.TypeConstants) ImmutableList(com.google.common.collect.ImmutableList) MineColonies(com.minecolonies.coremod.MineColonies) RAWTYPES(com.minecolonies.api.util.constant.Suppression.RAWTYPES) ReflectionUtils(com.minecolonies.api.util.ReflectionUtils) NotNull(org.jetbrains.annotations.NotNull) AssigningStrategy(com.minecolonies.api.colony.requestsystem.manager.AssigningStrategy) WrappedBlacklistAssignmentRequestManager(com.minecolonies.coremod.colony.requestsystem.management.manager.wrapped.WrappedBlacklistAssignmentRequestManager) IStandardRequestManager(com.minecolonies.coremod.colony.requestsystem.management.IStandardRequestManager) IRequestResolver(com.minecolonies.api.colony.requestsystem.resolver.IRequestResolver) WrappedBlacklistAssignmentRequestManager(com.minecolonies.coremod.colony.requestsystem.management.manager.wrapped.WrappedBlacklistAssignmentRequestManager) IToken(com.minecolonies.api.colony.requestsystem.token.IToken) TypeToken(com.google.common.reflect.TypeToken) IRequest(com.minecolonies.api.colony.requestsystem.request.IRequest) Nullable(org.jetbrains.annotations.Nullable) WrappedStaticStateRequestManager(com.minecolonies.coremod.colony.requestsystem.management.manager.wrapped.WrappedStaticStateRequestManager)

Example 3 with WrappedStaticStateRequestManager

use of com.minecolonies.coremod.colony.requestsystem.management.manager.wrapped.WrappedStaticStateRequestManager in project minecolonies by Minecolonies.

the class RequestHandler method onRequestCancelled.

/**
 * Method used to handle requests that were overruled or cancelled.
 * Cancels all children first, handles the creation of clean up requests.
 *
 * @param manager The manager that got notified of the cancellation or overruling.
 * @param token   The token of the request that got cancelled or overruled
 */
@SuppressWarnings(UNCHECKED)
public static void onRequestCancelled(final IStandardRequestManager manager, final IToken<?> token) {
    @SuppressWarnings(RAWTYPES) final IRequest request = RequestHandler.getRequest(manager, token);
    if (request == null) {
        return;
    }
    request.setState(new WrappedStaticStateRequestManager(manager), RequestState.CANCELLED);
    processInternalCancellation(manager, token);
    // Notify the requester.
    final IRequester requester = request.getRequester();
    requester.onRequestCancelled(manager, token);
    cleanRequestData(manager, token);
}
Also used : IRequest(com.minecolonies.api.colony.requestsystem.request.IRequest) IRequester(com.minecolonies.api.colony.requestsystem.requester.IRequester) WrappedStaticStateRequestManager(com.minecolonies.coremod.colony.requestsystem.management.manager.wrapped.WrappedStaticStateRequestManager)

Example 4 with WrappedStaticStateRequestManager

use of com.minecolonies.coremod.colony.requestsystem.management.manager.wrapped.WrappedStaticStateRequestManager in project minecolonies by Minecolonies.

the class StandardRequestManager method updateRequestState.

/**
 * Method to update the state of a given request.
 *
 * @param token The token that represents a given request to update.
 * @param state The new state of that request.
 * @throws IllegalArgumentException when the token is unknown to this manager.
 */
@Override
public void updateRequestState(@NotNull final IToken<?> token, @NotNull final RequestState state) {
    final IRequest<?> request = RequestHandler.getRequest(this, token);
    LogHandler.log("Updating request state from:" + token + ". With original state: " + request.getState() + " to : " + state);
    request.setState(new WrappedStaticStateRequestManager(this), state);
    if (colony != null) {
        colony.markDirty();
    }
    switch(request.getState()) {
        case COMPLETED:
            LogHandler.log("Request completed: " + token + ". Notifying parent and requester...");
            RequestHandler.onRequestSuccessful(this, token);
            return;
        case OVERRULED:
            LogHandler.log("Request overruled: " + token + ". Notifying parent, children and requester...");
            RequestHandler.onRequestOverruled(this, token);
            break;
        case CANCELLED:
            LogHandler.log("Request cancelled: " + token + ". Notifying parent, children and requester...");
            RequestHandler.onRequestCancelled(this, token);
            return;
        case RECEIVED:
            LogHandler.log("Request received: " + token + ". Removing from system...");
            RequestHandler.cleanRequestData(this, token);
            return;
        default:
    }
}
Also used : WrappedStaticStateRequestManager(com.minecolonies.coremod.colony.requestsystem.management.manager.wrapped.WrappedStaticStateRequestManager)

Example 5 with WrappedStaticStateRequestManager

use of com.minecolonies.coremod.colony.requestsystem.management.manager.wrapped.WrappedStaticStateRequestManager in project minecolonies by Minecolonies.

the class ResolverHandler method addRequestToResolver.

/**
 * Method used to add a request to a resolver.
 * <p>
 * <p>
 * Is only used internally.
 * The method modifies the resolverRequestMap that is used to track which resolver handles which request.
 * </p>
 *
 * @param manager  The manager to modify
 * @param resolver The resolver to add the request to.
 * @param request  The request to add to the resolver.
 */
public static void addRequestToResolver(final IStandardRequestManager manager, final IRequestResolver<?> resolver, final IRequest<?> request) {
    if (!manager.getRequestResolverRequestAssignmentDataStore().getAssignments().containsKey(resolver.getRequesterId())) {
        manager.getRequestResolverRequestAssignmentDataStore().getAssignments().put(resolver.getRequesterId(), new HashSet<>());
    }
    LogHandler.log("Adding request: " + request + " to resolver: " + resolver);
    manager.getRequestResolverRequestAssignmentDataStore().getAssignments().get(resolver.getRequesterId()).add(request.getToken());
    request.setState(new WrappedStaticStateRequestManager(manager), RequestState.ASSIGNED);
}
Also used : WrappedStaticStateRequestManager(com.minecolonies.coremod.colony.requestsystem.management.manager.wrapped.WrappedStaticStateRequestManager)

Aggregations

WrappedStaticStateRequestManager (com.minecolonies.coremod.colony.requestsystem.management.manager.wrapped.WrappedStaticStateRequestManager)5 IRequest (com.minecolonies.api.colony.requestsystem.request.IRequest)2 IRequester (com.minecolonies.api.colony.requestsystem.requester.IRequester)2 IRequestResolver (com.minecolonies.api.colony.requestsystem.resolver.IRequestResolver)2 ImmutableCollection (com.google.common.collect.ImmutableCollection)1 ImmutableList (com.google.common.collect.ImmutableList)1 TypeToken (com.google.common.reflect.TypeToken)1 AssigningStrategy (com.minecolonies.api.colony.requestsystem.manager.AssigningStrategy)1 RequestMappingHandler (com.minecolonies.api.colony.requestsystem.manager.RequestMappingHandler)1 RequestState (com.minecolonies.api.colony.requestsystem.request.RequestState)1 IRequestable (com.minecolonies.api.colony.requestsystem.requestable.IRequestable)1 IToken (com.minecolonies.api.colony.requestsystem.token.IToken)1 ReflectionUtils (com.minecolonies.api.util.ReflectionUtils)1 RAWTYPES (com.minecolonies.api.util.constant.Suppression.RAWTYPES)1 UNCHECKED (com.minecolonies.api.util.constant.Suppression.UNCHECKED)1 TypeConstants (com.minecolonies.api.util.constant.TypeConstants)1 MineColonies (com.minecolonies.coremod.MineColonies)1 IStandardRequestManager (com.minecolonies.coremod.colony.requestsystem.management.IStandardRequestManager)1 WrappedBlacklistAssignmentRequestManager (com.minecolonies.coremod.colony.requestsystem.management.manager.wrapped.WrappedBlacklistAssignmentRequestManager)1 java.util (java.util)1