Skip to content

Commit

Permalink
The new and improved stats plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
jorisguffens committed Dec 7, 2022
1 parent aa94f44 commit 63bf99f
Show file tree
Hide file tree
Showing 22 changed files with 520 additions and 255 deletions.
42 changes: 30 additions & 12 deletions api/src/main/java/com/guflimc/brick/stats/api/StatsManager.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,53 @@
package com.guflimc.brick.stats.api;

import com.guflimc.brick.stats.api.container.StatsContainer;
import com.guflimc.brick.stats.api.actor.Actor;
import com.guflimc.brick.stats.api.container.Container;
import com.guflimc.brick.stats.api.container.Record;
import com.guflimc.brick.stats.api.event.SubscriptionBuilder;
import com.guflimc.brick.stats.api.key.StatsKey;
import com.guflimc.brick.stats.api.relation.RelationProvider;
import org.jetbrains.annotations.NotNull;

import java.util.UUID;
import java.util.List;
import java.util.function.IntFunction;

public interface StatsManager {

int read(@NotNull UUID id, @NotNull StatsKey key);
Container find(@NotNull Actor... actors);

int read(@NotNull UUID id, UUID relation, @NotNull StatsKey key);
Container find(@NotNull Actor.ActorSet actors);

void update(@NotNull UUID id, @NotNull StatsKey key, @NotNull IntFunction<Integer> updater);
//

void update(@NotNull UUID id, UUID relation, @NotNull StatsKey key, @NotNull IntFunction<Integer> updater);
/**
* This will also update permutations of the actor set.
*/
void update(@NotNull Actor.ActorSet actors, @NotNull StatsKey key, @NotNull IntFunction<Integer> updater);

default void write(@NotNull UUID id, @NotNull StatsKey key, int value) {
write(id, null, key, value);
/**
* This will first find the full actor set by traversing relations and then {@link #update(Actor.ActorSet, StatsKey, IntFunction)}.
*/
void update(@NotNull Actor actor, @NotNull StatsKey key, @NotNull IntFunction<Integer> updater);

//

List<Record> select(@NotNull String actorType, @NotNull StatsKey key, int limit, boolean asc);

default List<Record> select(@NotNull String actorType, @NotNull StatsKey key, int limit) {
return select(actorType, key, limit, false);
}

default void write(@NotNull UUID id, UUID relation, @NotNull StatsKey key, int value) {
update(id, relation, key, ignored -> value);
default List<Record> select(@NotNull String actorType, @NotNull StatsKey key) {
return select(actorType, key, Integer.MAX_VALUE);
}

StatsContainer readAll(@NotNull UUID id);
//

void registerRelations(@NotNull RelationProvider relationProvider);

void unregisterRelations(@NotNull RelationProvider relationProvider);

void registerRelationProvider(@NotNull RelationProvider relationProvider);
//

SubscriptionBuilder subscribe();

Expand Down
76 changes: 76 additions & 0 deletions api/src/main/java/com/guflimc/brick/stats/api/actor/Actor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.guflimc.brick.stats.api.actor;

import org.jetbrains.annotations.NotNull;

import java.util.*;
import java.util.stream.Stream;

public interface Actor {

UUID id();

String type();

//

static Actor player(@NotNull UUID id) {
return new TempActor(id, "PLAYER");
}

record TempActor(@NotNull UUID id, @NotNull String type) implements Actor {
}

//

class ActorSet implements Iterable<Actor> {

private final Set<Actor> actors;

public ActorSet(@NotNull Collection<? extends Actor> c) {
if ( c.isEmpty() ) {
throw new IllegalArgumentException("ActorSet must contain at least one actor.");
}
this.actors = Set.copyOf(c);
}

public <T extends Actor> ActorSet(@NotNull T... actors) {
this(Set.of(actors));
}

public int size() {
return actors.size();
}

public Actor first() {
return iterator().next();
}

public Stream<Actor> stream() {
return actors.stream();
}

public Set<Actor> copySet() {
return new HashSet<>(actors);
}

@NotNull
@Override
public Iterator<Actor> iterator() {
return actors.iterator();
}

//


@Override
public int hashCode() {
return actors.hashCode();
}

@Override
public boolean equals(Object obj) {
return obj instanceof ActorSet as && actors.equals(as.actors);
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.guflimc.brick.stats.api.container;

import com.guflimc.brick.stats.api.actor.Actor;
import com.guflimc.brick.stats.api.key.StatsKey;
import org.jetbrains.annotations.NotNull;

import java.util.Collection;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.function.IntFunction;

public interface Container {

Actor.ActorSet actors();

Collection<StatsKey> stats();

//

Optional<Record> find(@NotNull StatsKey key);

int read(@NotNull StatsKey key);

void update(@NotNull StatsKey key, @NotNull IntFunction<Integer> updater);

default void write(@NotNull StatsKey key, int value) {
update(key, i -> value);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.guflimc.brick.stats.api.container;

import com.guflimc.brick.stats.api.actor.Actor;
import com.guflimc.brick.stats.api.key.StatsKey;

public interface Record {

Actor.ActorSet actors();

StatsKey key();

int value();

}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.guflimc.brick.stats.api.event;

import com.guflimc.brick.stats.api.container.StatsRecord;
import com.guflimc.brick.stats.api.container.Record;
import com.guflimc.brick.stats.api.key.StatsKey;
import org.jetbrains.annotations.NotNull;

public record Event(@NotNull StatsKey key, @NotNull StatsRecord record, int previousValue) {
public record Event(@NotNull Record record, int previousValue) {
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.guflimc.brick.stats.api.relation;

import com.guflimc.brick.stats.api.actor.Actor;
import com.guflimc.brick.stats.api.key.StatsKey;
import org.jetbrains.annotations.NotNull;

import java.util.Optional;
import java.util.UUID;
import org.jetbrains.annotations.Nullable;

@FunctionalInterface
public interface RelationProvider {

Optional<UUID> relation(@NotNull UUID entityId, @NotNull StatsKey key);
@Nullable
Actor provide(@NotNull Actor actor, @NotNull StatsKey key);

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import com.guflimc.brick.orm.ebean.database.EbeanConfig;
import com.guflimc.brick.orm.ebean.database.EbeanDatabaseContext;
import com.guflimc.brick.orm.ebean.database.EbeanMigrations;
import com.guflimc.brick.stats.common.domain.DStatsRecord;
import com.guflimc.brick.stats.common.domain.DActor;
import com.guflimc.brick.stats.common.domain.DRecord;
import io.ebean.annotation.Platform;

import java.io.IOException;
Expand All @@ -29,7 +30,10 @@ protected Class<?>[] applicableClasses() {
}

private static final Class<?>[] APPLICABLE_CLASSES = new Class[]{
DStatsRecord.class
DRecord.class,

DActor.ActorPK.class,
DActor.class
};

public static void main(String[] args) throws IOException, SQLException {
Expand Down
Loading

0 comments on commit 63bf99f

Please sign in to comment.