Skip to content

Commit

Permalink
Merge branch 'dev' into sparks-match
Browse files Browse the repository at this point in the history
  • Loading branch information
Pablete1234 authored Oct 8, 2024
2 parents a75485f + cb27404 commit b96dbff
Show file tree
Hide file tree
Showing 14 changed files with 298 additions and 205 deletions.
3 changes: 2 additions & 1 deletion core/src/main/java/tc/oc/pgm/api/filter/ReactorFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public interface ReactorFactory<R extends ReactorFactory.Reactor>
extends FilterDefinition, StateHolder<R> {

default void register(Match match, FilterMatchModule fmm) {
match.getFeatureContext().registerState(this, createReactor(match, fmm));
if (!match.getFeatureContext().hasState(this))
match.getFeatureContext().registerState(this, createReactor(match, fmm));
}

/**
Expand Down
181 changes: 75 additions & 106 deletions core/src/main/java/tc/oc/pgm/command/MapPoolCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,18 @@ public void pool(
int resultsPerPage = all ? maps.size() : 8;
int pages = all ? 1 : (maps.size() + resultsPerPage - 1) / resultsPerPage;

Component mapPoolComponent =
TextFormatter.paginate(
text()
.append(translatable("pool.name"))
.append(text(" (", NamedTextColor.DARK_AQUA))
.append(text(mapPool.getName(), NamedTextColor.AQUA))
.append(text(")", NamedTextColor.DARK_AQUA))
.build(),
page,
pages,
NamedTextColor.DARK_AQUA,
NamedTextColor.AQUA,
false);
Component mapPoolComponent = TextFormatter.paginate(
text()
.append(translatable("pool.name"))
.append(text(" (", NamedTextColor.DARK_AQUA))
.append(text(mapPool.getName(), NamedTextColor.AQUA))
.append(text(")", NamedTextColor.DARK_AQUA))
.build(),
page,
pages,
NamedTextColor.DARK_AQUA,
NamedTextColor.AQUA,
false);

Component title =
TextFormatter.horizontalLineHeading(source, mapPoolComponent, NamedTextColor.BLUE, 250);
Expand All @@ -96,7 +95,7 @@ public void pool(
if (chance && votes != null) {
double maxWeight = 0, currWeight;
for (MapInfo map : votes.getMaps()) {
chances.put(map, currWeight = votes.mapPicker.getWeight(null, map, votes.getMapScore(map)));
chances.put(map, currWeight = votes.mapPicker.getWeight(null, map, votes.getVoteData(map)));
maxWeight += currWeight;
}
double finalMaxWeight = maxWeight;
Expand All @@ -106,23 +105,19 @@ public void pool(
int nextPos = mapPool instanceof Rotation ? ((Rotation) mapPool).getNextPosition() : -1;

if (order && votes != null) {
maps =
maps.stream()
.sorted(
Comparator.comparingDouble(chance ? chances::get : votes::getMapScore).reversed())
.collect(Collectors.toList());
maps = maps.stream()
.sorted(Comparator.comparingDouble(chance ? chances::get : votes::getMapScore)
.reversed())
.collect(Collectors.toList());
}

new PrettyPaginatedComponentResults<MapInfo>(title, resultsPerPage) {
@Override
public Component format(MapInfo map, int index) {
index++;
TextComponent.Builder entry =
text()
.append(
text(
index + ". ",
nextPos == index ? NamedTextColor.DARK_AQUA : NamedTextColor.WHITE));
TextComponent.Builder entry = text()
.append(text(
index + ". ", nextPos == index ? NamedTextColor.DARK_AQUA : NamedTextColor.WHITE));
if (votes != null && scores)
entry.append(
text(SCORE_FORMAT.format(votes.getMapScore(map)) + " ", NamedTextColor.YELLOW));
Expand Down Expand Up @@ -156,45 +151,41 @@ public void pools(
int resultsPerPage = 8;
int pages = (mapPools.size() + resultsPerPage - 1) / resultsPerPage;

Component paginated =
TextFormatter.paginate(
translatable("pool.title"),
page,
pages,
NamedTextColor.DARK_AQUA,
NamedTextColor.AQUA,
true);
Component paginated = TextFormatter.paginate(
translatable("pool.title"),
page,
pages,
NamedTextColor.DARK_AQUA,
NamedTextColor.AQUA,
true);

Component formattedTitle =
TextFormatter.horizontalLineHeading(source, paginated, NamedTextColor.BLUE);

new PrettyPaginatedComponentResults<MapPool>(formattedTitle, resultsPerPage) {
@Override
public Component format(MapPool mapPool, int index) {
Component arrow =
text(
"» ",
poolManager.getActiveMapPool().equals(mapPool)
? NamedTextColor.GREEN
: NamedTextColor.WHITE);

Component maps =
text()
.append(text(" (", NamedTextColor.DARK_AQUA))
.append(translatable("map.title", NamedTextColor.DARK_GREEN))
.append(text(": ", NamedTextColor.DARK_GREEN))
.append(text(mapPool.getMaps().size(), NamedTextColor.WHITE))
.append(text(")", NamedTextColor.DARK_AQUA))
.build();

Component players =
text()
.append(text(" (", NamedTextColor.DARK_AQUA))
.append(translatable("match.info.players", NamedTextColor.AQUA))
.append(text(": ", NamedTextColor.AQUA))
.append(text(mapPool.getPlayers(), NamedTextColor.WHITE))
.append(text(")", NamedTextColor.DARK_AQUA))
.build();
Component arrow = text(
"» ",
poolManager.getActiveMapPool().equals(mapPool)
? NamedTextColor.GREEN
: NamedTextColor.WHITE);

Component maps = text()
.append(text(" (", NamedTextColor.DARK_AQUA))
.append(translatable("map.title", NamedTextColor.DARK_GREEN))
.append(text(": ", NamedTextColor.DARK_GREEN))
.append(text(mapPool.getMaps().size(), NamedTextColor.WHITE))
.append(text(")", NamedTextColor.DARK_AQUA))
.build();

Component players = text()
.append(text(" (", NamedTextColor.DARK_AQUA))
.append(translatable("match.info.players", NamedTextColor.AQUA))
.append(text(": ", NamedTextColor.AQUA))
.append(text(mapPool.getPlayers(), NamedTextColor.WHITE))
.append(text(")", NamedTextColor.DARK_AQUA))
.build();

return text()
.append(arrow)
Expand Down Expand Up @@ -223,11 +214,10 @@ public void setPool(
if (newPool == null) throw exception("pool.noPoolMatch");

if (newPool.equals(poolManager.getActiveMapPool())) {
sender.sendMessage(
translatable(
"pool.matching",
NamedTextColor.GRAY,
text(newPool.getName(), NamedTextColor.LIGHT_PURPLE)));
sender.sendMessage(translatable(
"pool.matching",
NamedTextColor.GRAY,
text(newPool.getName(), NamedTextColor.LIGHT_PURPLE)));
return;
}

Expand Down Expand Up @@ -268,17 +258,15 @@ public void skip(

((Rotation) pool).advance(positions);

Component message =
text()
.append(text("[", NamedTextColor.WHITE))
.append(translatable("pool.name", NamedTextColor.GOLD))
.append(text("] [", NamedTextColor.WHITE))
.append(text(pool.getName(), NamedTextColor.AQUA))
.append(text("]", NamedTextColor.WHITE))
.append(
translatable(
"pool.skip", NamedTextColor.GREEN, text(positions, NamedTextColor.AQUA)))
.build();
Component message = text()
.append(text("[", NamedTextColor.WHITE))
.append(translatable("pool.name", NamedTextColor.GOLD))
.append(text("] [", NamedTextColor.WHITE))
.append(text(pool.getName(), NamedTextColor.AQUA))
.append(text("]", NamedTextColor.WHITE))
.append(
translatable("pool.skip", NamedTextColor.GREEN, text(positions, NamedTextColor.AQUA)))
.build();

sender.sendMessage(message);
}
Expand All @@ -291,11 +279,10 @@ public void voteNext(
@Flag(value = "open", aliases = "o") boolean forceOpen,
@Argument("map") @FlagYielding MapInfo map) {
boolean voteResult = poll.toggleVote(map, player);
Component voteAction =
translatable(
voteResult ? "vote.for" : "vote.abstain",
voteResult ? NamedTextColor.GREEN : NamedTextColor.RED,
map.getStyledName(MapNameStyle.COLOR));
Component voteAction = translatable(
voteResult ? "vote.for" : "vote.abstain",
voteResult ? NamedTextColor.GREEN : NamedTextColor.RED,
map.getStyledName(MapNameStyle.COLOR));
player.sendMessage(voteAction);
poll.sendBook(player, forceOpen);
}
Expand All @@ -318,26 +305,12 @@ public void rot(
@Argument("page") @Default("1") @Range(min = "1") int page,
@Flag(value = "all", aliases = "a") boolean all,
String[] rawArgs) {
wrapLegacy(
"pool",
sender,
rawArgs,
() -> {
if (poolManager.getActiveMapPool().getType() != MapPoolType.ORDERED)
throw exception("pool.noRotation");

pool(
sender,
source,
poolManager,
page,
MapPoolType.ORDERED,
null,
false,
false,
false,
all);
});
wrapLegacy("pool", sender, rawArgs, () -> {
if (poolManager.getActiveMapPool().getType() != MapPoolType.ORDERED)
throw exception("pool.noRotation");

pool(sender, source, poolManager, page, MapPoolType.ORDERED, null, false, false, false, all);
});
}

@Command("rots [page]")
Expand Down Expand Up @@ -391,14 +364,10 @@ public void resetRot(
MapPool resetRot =
poolManager.getAppropriateDynamicPool(match).orElseThrow(() -> exception("pool.noDynamic"));

wrapLegacy(
"setpool",
sender,
rawArgs,
() -> {
if (resetRot.getType() != MapPoolType.ORDERED) throw exception("pool.noRotation");
setPool(sender, source, match, poolManager, resetRot, timeLimit, matchLimit);
});
wrapLegacy("setpool", sender, rawArgs, () -> {
if (resetRot.getType() != MapPoolType.ORDERED) throw exception("pool.noRotation");
setPool(sender, source, match, poolManager, resetRot, timeLimit, matchLimit);
});
}

private void wrapLegacy(String replace, Audience sender, String[] rawArgs, Runnable task) {
Expand Down
17 changes: 12 additions & 5 deletions core/src/main/java/tc/oc/pgm/features/MatchFeatureContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,35 @@
import tc.oc.pgm.api.feature.Feature;
import tc.oc.pgm.util.collection.ContextStore;

public class MatchFeatureContext extends ContextStore<Feature> {
public class MatchFeatureContext extends ContextStore<Feature<?>> {

private final Map<StateHolder<?>, Object> states = new IdentityHashMap<>();

public String add(Feature feature) {
public String add(Feature<?> feature) {
super.add(feature.getId(), feature);
return feature.getId();
}

public <T extends Feature> T get(String id, Class<T> type) {
@SuppressWarnings("unchecked")
public <T extends Feature<?>> T get(String id, Class<T> type) {
return (T) this.get(id);
}

public <T> void registerState(StateHolder<T> stateHolder, T state) {
states.put(stateHolder, state);
if (states.putIfAbsent(stateHolder, state) != null) {
throw new IllegalStateException("State already registered: " + stateHolder);
}
}

@SuppressWarnings("unchecked")
public <T> T getState(StateHolder<T> stateHolder) {
//noinspection unchecked
return (T) assertNotNull(states.get(stateHolder), "state");
}

public boolean hasState(StateHolder<?> stateHolder) {
return states.containsKey(stateHolder);
}

public Map<StateHolder<?>, Object> getStates() {
return Collections.unmodifiableMap(states);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import tc.oc.pgm.rotation.MapPoolManager;

public class DisabledMapPool extends MapPool {
DisabledMapPool(MapPoolManager manager, ConfigurationSection section, String name) {
super(null, name, manager, section);
DisabledMapPool(String name, MapPoolManager manager, ConfigurationSection section) {
super(null, name, manager, section, MapParser.parse(name, section));
}

@Override
Expand Down
Loading

0 comments on commit b96dbff

Please sign in to comment.