forked from TheCurle/Camelot
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a stats module. Track trick uses
- Loading branch information
1 parent
cd7057f
commit 7885660
Showing
10 changed files
with
129 additions
and
3 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
config/src/main/groovy/net/neoforged/camelot/config/module/Statistics.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package net.neoforged.camelot.config.module | ||
|
||
import groovy.transform.CompileStatic | ||
|
||
/** | ||
* The module tracking different statistics. | ||
*/ | ||
@CompileStatic | ||
class Statistics extends ModuleConfiguration { | ||
/** | ||
* Track trick statistics (number of uses, and type). | ||
*/ | ||
boolean tricks = true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/main/java/net/neoforged/camelot/db/transactionals/StatsDAO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package net.neoforged.camelot.db.transactionals; | ||
|
||
import org.jdbi.v3.sqlobject.customizer.Bind; | ||
import org.jdbi.v3.sqlobject.statement.SqlQuery; | ||
import org.jdbi.v3.sqlobject.statement.SqlUpdate; | ||
import org.jdbi.v3.sqlobject.transaction.Transactional; | ||
|
||
/** | ||
* Transactionals used to interact with statistics. | ||
*/ | ||
public interface StatsDAO { | ||
/** | ||
* Trick statistics | ||
*/ | ||
interface Tricks extends Transactional<Tricks>, StatsDAO { | ||
@SqlUpdate("insert into trick_stats (trick, prefix_uses, slash_uses) values(:trick, 1, 0) on conflict(trick) do update set prefix_uses = prefix_uses + 1") | ||
void incrementPrefixUses(@Bind("trick") int trickId); | ||
@SqlUpdate("insert into trick_stats (trick, prefix_uses, slash_uses) values(:trick, 0, 1) on conflict(trick) do update set slash_uses = slash_uses + 1") | ||
void incrementSlashUses(@Bind("trick") int trickId); | ||
|
||
default int getPrefixUses(int trickId) { | ||
return getHandle().createQuery("select prefix_uses from trick_stats where trick = ?") | ||
.bind(0, trickId) | ||
.execute((statementSupplier, _) -> statementSupplier.get().getResultSet().getInt("prefix_uses")); | ||
} | ||
|
||
default int getSlashUses(int trickId) { | ||
return getHandle().createQuery("select slash_uses from trick_stats where trick = ?") | ||
.bind(0, trickId) | ||
.execute((statementSupplier, _) -> statementSupplier.get().getResultSet().getInt("slash_uses")); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/main/java/net/neoforged/camelot/module/StatsModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package net.neoforged.camelot.module; | ||
|
||
import com.google.auto.service.AutoService; | ||
import net.neoforged.camelot.Database; | ||
import net.neoforged.camelot.config.module.Statistics; | ||
import net.neoforged.camelot.db.transactionals.StatsDAO; | ||
import org.jdbi.v3.core.extension.ExtensionConsumer; | ||
|
||
/** | ||
* The module used to track statistics. | ||
*/ | ||
@AutoService(CamelotModule.class) | ||
public class StatsModule extends CamelotModule.Base<Statistics> { | ||
public StatsModule() { | ||
super(Statistics.class); | ||
} | ||
|
||
@Override | ||
public String id() { | ||
return "stats"; | ||
} | ||
|
||
/** | ||
* Use the extension of the given {@code type}. | ||
*/ | ||
public <T extends StatsDAO> void use(Class<T> type, ExtensionConsumer<T, RuntimeException> dao) { | ||
if (type == StatsDAO.Tricks.class && !config().isTricks()) return; | ||
Database.stats().useExtension(type, dao); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
create table trick_stats | ||
( | ||
-- the trick these stats are associated with | ||
trick integer not null primary key, | ||
|
||
prefix_uses integer, | ||
slash_uses integer | ||
) without rowid; |