From 904f5ea5d1a476b02feb3a6e788b061f1ae5c893 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Sun, 12 Sep 2021 17:38:52 +0200 Subject: [PATCH] Added player config related methods in IOffPlayer --- .../lib/core/players/IOffPlayer.java | 33 +++++++ .../lib/core/players/SQLPlayerConfig.java | 90 +++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 Core/src/main/java/fr/pandacube/lib/core/players/SQLPlayerConfig.java diff --git a/Core/src/main/java/fr/pandacube/lib/core/players/IOffPlayer.java b/Core/src/main/java/fr/pandacube/lib/core/players/IOffPlayer.java index a648be2..4df00dd 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/players/IOffPlayer.java +++ b/Core/src/main/java/fr/pandacube/lib/core/players/IOffPlayer.java @@ -298,5 +298,38 @@ public interface IOffPlayer { + + + + + /* + * Player config + */ + + public default String getConfig(String key) throws DBException { + return SQLPlayerConfig.get(getUniqueId(), key); + } + + public default String getConfig(String key, String deflt) throws DBException { + return SQLPlayerConfig.get(getUniqueId(), key, deflt); + } + + public default void setConfig(String key, String value) throws DBException { + SQLPlayerConfig.set(getUniqueId(), key, value); + } + + public default void unsetConfig(String key) throws DBException { + SQLPlayerConfig.unset(getUniqueId(), key); + } + + public default boolean isWelcomeQuizzDone() { + try { + return Boolean.valueOf(getConfig("welcome.quizz.done", "false")); + } catch (DBException e) { + Log.severe("Error knowing if player has already done the quizz. Assuming they did for now.", e); + return true; + } + } + } diff --git a/Core/src/main/java/fr/pandacube/lib/core/players/SQLPlayerConfig.java b/Core/src/main/java/fr/pandacube/lib/core/players/SQLPlayerConfig.java new file mode 100644 index 0000000..c643f4b --- /dev/null +++ b/Core/src/main/java/fr/pandacube/lib/core/players/SQLPlayerConfig.java @@ -0,0 +1,90 @@ +package fr.pandacube.lib.core.players; + +import java.util.UUID; + +import fr.pandacube.lib.core.db.DB; +import fr.pandacube.lib.core.db.DBException; +import fr.pandacube.lib.core.db.SQLElement; +import fr.pandacube.lib.core.db.SQLElementList; +import fr.pandacube.lib.core.db.SQLFKField; +import fr.pandacube.lib.core.db.SQLField; + +public class SQLPlayerConfig extends SQLElement { + + public SQLPlayerConfig() { + super(); + } + + public SQLPlayerConfig(int id) { + super(id); + } + + /* + * Nom de la table + */ + @Override + protected String tableName() { + return "player_config"; + } + + /* + * Champs de la table + */ + public static final SQLFKField playerId = foreignKey(false, SQLPlayer.class, SQLPlayer.playerId); + public static final SQLField key = field(VARCHAR(255), false); + public static final SQLField value = field(VARCHAR(8192), false); + + + + public static String get(UUID p, String k, String deflt) throws DBException { + SQLPlayerConfig res = DB.getFirst(SQLPlayerConfig.class, playerId.eq(p).and(key.eq(k))); + return res == null ? deflt : res.get(value); + } + + public static String get(UUID p, String k) throws DBException { + return get(p, k, null); + } + + public static void set(UUID p, String k, String v) throws DBException { + if (v == null) { + unset(p, k); + return; + } + + SQLPlayerConfig entry = DB.getFirst(SQLPlayerConfig.class, playerId.eq(p).and(key.eq(k))); + + if (entry == null) { + entry = new SQLPlayerConfig(); + entry.set(playerId, p); + entry.set(key, k); + } + + entry.set(value, v); + entry.save(); + } + + public static void unset(UUID p, String k) throws DBException { + + SQLPlayerConfig entry = DB.getFirst(SQLPlayerConfig.class, playerId.eq(p).and(key.eq(k))); + + if (entry != null) + entry.delete(); + } + + + public static SQLElementList getAllFromPlayer(UUID p, String likeQuery) throws DBException { + return DB.getAll(SQLPlayerConfig.class, playerId.eq(p).and(key.like(likeQuery))); + } + + public static SQLElementList getAllWithKeys(String likeQuery) throws DBException { + return DB.getAll(SQLPlayerConfig.class, key.like(likeQuery)); + } + + public static SQLElementList getAllWithKeyValue(String k, String v) throws DBException { + return DB.getAll(SQLPlayerConfig.class, key.eq(k).and(value.eq(v))); + } + + + + +}