2017-03-31 23:30:41 +02:00
|
|
|
package tc.oc.pgm.mutation.types;
|
|
|
|
|
|
|
|
import java.time.Duration;
|
2017-04-03 04:09:35 +02:00
|
|
|
import java.time.Instant;
|
2017-03-31 23:30:41 +02:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import tc.oc.commons.core.stream.Collectors;
|
2017-04-03 04:09:35 +02:00
|
|
|
import tc.oc.commons.core.util.TimeUtils;
|
2017-03-31 23:30:41 +02:00
|
|
|
import tc.oc.pgm.match.Match;
|
|
|
|
import tc.oc.pgm.match.MatchPlayer;
|
2017-04-03 04:09:35 +02:00
|
|
|
import tc.oc.pgm.match.Repeatable;
|
|
|
|
import tc.oc.time.Time;
|
2017-03-31 23:30:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A mutation module that executes a task on random {@link MatchPlayer}s.
|
|
|
|
*/
|
2017-04-03 04:09:35 +02:00
|
|
|
public interface TargetMutation extends MutationModule {
|
2017-03-31 23:30:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute a task on the given randomly selected players.
|
|
|
|
* @param players a list of players, which size is determined by {@link #targets()}.
|
|
|
|
*/
|
2017-04-03 04:09:35 +02:00
|
|
|
void target(List<MatchPlayer> players);
|
2017-03-31 23:30:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine the number of random players to target.
|
2017-04-03 04:09:35 +02:00
|
|
|
*
|
2017-03-31 23:30:41 +02:00
|
|
|
* If there are no enough players on the server, it is possible
|
|
|
|
* that the number of targets is less than expected.
|
|
|
|
* @return number of targets.
|
|
|
|
*/
|
2017-04-03 04:09:35 +02:00
|
|
|
int targets();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the next time {@link #target()} will be run.
|
|
|
|
* @return next target time.
|
|
|
|
*/
|
|
|
|
Instant next();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the next time {@link #target()} will be run.
|
|
|
|
* @param time next target time.
|
|
|
|
*/
|
|
|
|
void next(Instant time);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the frequency that {@link #target()} will be run.
|
|
|
|
* @return frequency between target times.
|
|
|
|
*/
|
|
|
|
Duration frequency();
|
2017-03-31 23:30:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a list of random players.
|
|
|
|
* @return the random players.
|
|
|
|
*/
|
2017-04-03 04:09:35 +02:00
|
|
|
default List<MatchPlayer> search() {
|
|
|
|
return match().participants()
|
|
|
|
.filter(MatchPlayer::isSpawned)
|
|
|
|
.collect(Collectors.toRandomSubList(entropy(), targets()));
|
2017-03-31 23:30:41 +02:00
|
|
|
}
|
|
|
|
|
2017-04-03 04:09:35 +02:00
|
|
|
/**
|
|
|
|
* Execute a task on randomly selected players and reset the
|
|
|
|
* next time the task will be executed.
|
|
|
|
*/
|
|
|
|
default void target() {
|
|
|
|
target(search());
|
|
|
|
next(match().getInstantNow().plus(frequency()));
|
2017-03-31 23:30:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-04-03 04:09:35 +02:00
|
|
|
default void enable() {
|
|
|
|
MutationModule.super.enable();
|
|
|
|
target();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Repeatable(interval = @Time(seconds = 1))
|
|
|
|
default void tick() {
|
|
|
|
Instant now = match().getInstantNow(), next = next();
|
|
|
|
if(next == null) {
|
|
|
|
next(now.plus(frequency()));
|
|
|
|
} else if(TimeUtils.isEqualOrBeforeNow(now, next)) {
|
|
|
|
target();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract class Impl extends MutationModule.Impl implements TargetMutation {
|
|
|
|
|
|
|
|
Duration frequency;
|
|
|
|
Instant next;
|
|
|
|
|
|
|
|
public Impl(Match match, Duration frequency) {
|
|
|
|
super(match);
|
|
|
|
this.frequency = frequency;
|
2017-03-31 23:30:41 +02:00
|
|
|
}
|
2017-04-03 04:09:35 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public Instant next() {
|
|
|
|
return next;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void next(Instant time) {
|
|
|
|
next = time;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Duration frequency() {
|
|
|
|
return frequency;
|
|
|
|
}
|
|
|
|
|
2017-03-31 23:30:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|