From a092739b9ec4551cef4d0f79e5348f40a6e9f692 Mon Sep 17 00:00:00 2001 From: Gianlu Date: Fri, 26 Jun 2020 19:56:57 +0200 Subject: [PATCH] Enabled/disable custom decks + added entry in stats.jsp --- WebContent/stats.jsp | 1 + build.properties.example | 2 ++ .../filtered-resources/WEB-INF/pyx.properties | 1 + src/main/java/net/socialgamer/cah/CahModule.java | 13 +++++++++++++ .../cah/customsets/CustomCardsService.java | 15 ++++++++++++++- .../net/socialgamer/cah/data/GameManagerTest.java | 6 ++++++ 6 files changed, 37 insertions(+), 1 deletion(-) diff --git a/WebContent/stats.jsp b/WebContent/stats.jsp index 371396e..27be726 100644 --- a/WebContent/stats.jsp +++ b/WebContent/stats.jsp @@ -53,4 +53,5 @@ out.println("METRICS_GAME_ENABLED " + props.get("pyx.metrics.game.enabled")); out.println("METRICS_ROUND_ENABLED " + props.get("pyx.metrics.round.enabled")); out.println("METRICS_SESSION_ENABLED " + props.get("pyx.metrics.session.enabled")); out.println("METRICS_USER_ENABLED " + props.get("pyx.metrics.user.enabled")); +out.println("CUSTOM_DECKS_ENABLED " + props.get("pyx.server.custom_decks_enabled")); %> diff --git a/build.properties.example b/build.properties.example index cdc714c..2849903 100644 --- a/build.properties.example +++ b/build.properties.example @@ -16,6 +16,8 @@ pyx.id_code_salt= pyx.admin_addrs=127.0.0.1,0:0:0:0:0:0:0:1 # comma-separated list of strings banned from appearing in nicks. pyx.banned_nicks=xyzzy +# whether the custom decks functionality is enabled +pyx.custom_decks_enabled=true # comma-separated list of url patterns to allow custom decks pyx.allowed_custom_decks_urls=* diff --git a/src/main/filtered-resources/WEB-INF/pyx.properties b/src/main/filtered-resources/WEB-INF/pyx.properties index 2683d27..8a2aa72 100644 --- a/src/main/filtered-resources/WEB-INF/pyx.properties +++ b/src/main/filtered-resources/WEB-INF/pyx.properties @@ -1,4 +1,5 @@ pyx.client.cookie_domain=${pyx.cookie_domain} +pyx.server.custom_decks_enabled=${pyx.custom_decks_enabled} pyx.server.allowed_custom_decks_urls=${pyx.allowed_custom_decks_urls} pyx.server.include_inactive_cardsets=${pyx.include_inactive_cardsets} pyx.server.max_users=${pyx.max_users} diff --git a/src/main/java/net/socialgamer/cah/CahModule.java b/src/main/java/net/socialgamer/cah/CahModule.java index 0010c43..69c2955 100644 --- a/src/main/java/net/socialgamer/cah/CahModule.java +++ b/src/main/java/net/socialgamer/cah/CahModule.java @@ -307,6 +307,14 @@ public class CahModule extends AbstractModule { } } + @Provides + @CustomDecksEnabled + Boolean provideCustomDecksEnabled() { + synchronized (properties) { + return Boolean.valueOf(properties.getProperty("pyx.server.custom_decks_enabled", "true")); + } + } + @Provides @CustomDecksAllowedUrls List provideAllowedCustomDecksUrls() { @@ -315,6 +323,11 @@ public class CahModule extends AbstractModule { } } + @BindingAnnotation + @Retention(RetentionPolicy.RUNTIME) + public @interface CustomDecksEnabled { + } + @BindingAnnotation @Retention(RetentionPolicy.RUNTIME) public @interface CustomDecksAllowedUrls { diff --git a/src/main/java/net/socialgamer/cah/customsets/CustomCardsService.java b/src/main/java/net/socialgamer/cah/customsets/CustomCardsService.java index 181fe17..590e7c7 100644 --- a/src/main/java/net/socialgamer/cah/customsets/CustomCardsService.java +++ b/src/main/java/net/socialgamer/cah/customsets/CustomCardsService.java @@ -28,7 +28,9 @@ import com.google.common.base.Charsets; import com.google.common.io.ByteSource; import com.google.inject.Inject; import com.google.inject.Provider; +import net.socialgamer.cah.CahModule; import net.socialgamer.cah.CahModule.CustomDecksAllowedUrls; +import net.socialgamer.cah.CahModule.CustomDecksEnabled; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.lang3.StringEscapeUtils; import org.apache.log4j.Logger; @@ -73,10 +75,12 @@ public class CustomCardsService { private static final AtomicInteger deckIdCounter = new AtomicInteger(0); private final Bencode bencode = new Bencode(); + private final Provider enabledProvider; private final Provider> allowedUrlsProvider; @Inject - public CustomCardsService(@CustomDecksAllowedUrls Provider> allowedUrlsProvider) { + public CustomCardsService(@CustomDecksEnabled Provider enabledProvider, @CustomDecksAllowedUrls Provider> allowedUrlsProvider) { + this.enabledProvider = enabledProvider; this.allowedUrlsProvider = allowedUrlsProvider; } @@ -98,12 +102,18 @@ public class CustomCardsService { } public CustomDeck loadSet(int customDeckId) { + if (!enabledProvider.get()) + return null; + CacheEntry entry = checkCacheId(customDeckId); if (checkCacheValid(entry, "id", String.valueOf(customDeckId))) return entry.deck; else return null; } public CustomDeck loadSetFromUrl(String url) { + if (!enabledProvider.get()) + return null; + CacheEntry entry = checkCacheUrl(url); if (checkCacheValid(entry, "url", url)) return entry.deck; @@ -125,6 +135,9 @@ public class CustomCardsService { } public CustomDeck loadSetFromJson(String jsonStr, String url) { + if (!enabledProvider.get()) + return null; + JSONObject obj; String hash; try { diff --git a/src/test/java/net/socialgamer/cah/data/GameManagerTest.java b/src/test/java/net/socialgamer/cah/data/GameManagerTest.java index a94510e..5f88ac1 100644 --- a/src/test/java/net/socialgamer/cah/data/GameManagerTest.java +++ b/src/test/java/net/socialgamer/cah/data/GameManagerTest.java @@ -152,6 +152,12 @@ public class GameManagerTest { return "1"; } + @Provides + @CustomDecksEnabled + Boolean provideCustomDecksEnabled() { + return true; + } + @Provides @CustomDecksAllowedUrls List provideAllowedCustomDecksUrls() {