Skip to content

Commit d8652f4

Browse files
committed
Add messagecache
- Add MessageCache - Add MemoryMessageCache - Add transform parameter to applyGame extension methods - Add transform parameter to GameAnimator - Improve documentation of GameAnimatorBuilder - Add structure for command module
1 parent 82284bc commit d8652f4

10 files changed

Lines changed: 538 additions & 35 deletions

File tree

common/src/main/java/me/schlaubi/regnumutils/common/builder/GameAnimatorBuilder.java

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@
3636
import java.util.concurrent.Executors;
3737
import java.util.concurrent.ScheduledExecutorService;
3838
import java.util.concurrent.TimeUnit;
39+
import java.util.function.BiConsumer;
3940
import java.util.function.Consumer;
41+
import java.util.function.Function;
4042

4143
/**
4244
* Builder for {@link GameAnimator}.
@@ -50,10 +52,11 @@ public class GameAnimatorBuilder {
5052

5153
private ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(new DefaultThreadFactory("GameAnimator"));
5254
private List<cc.hawkbot.regnum.client.core.discord.GameAnimator.Game> games = new ArrayList<>();
53-
private Consumer<cc.hawkbot.regnum.client.core.discord.GameAnimator.Game> applier;
55+
private BiConsumer<cc.hawkbot.regnum.client.core.discord.GameAnimator.Game, Function<String, String>> applier;
5456
private long interval = 30;
5557
private long initialDelay = 0;
5658
private TimeUnit unit = TimeUnit.SECONDS;
59+
private Function<String, String> transform = it -> it;
5760

5861
/**
5962
* Returns the currently selected scheduler.
@@ -128,8 +131,7 @@ public GameAnimatorBuilder addGames(Collection<cc.hawkbot.regnum.client.core.dis
128131
*
129132
* @return the {@link Consumer}
130133
*/
131-
@NotNull
132-
public Consumer<cc.hawkbot.regnum.client.core.discord.GameAnimator.Game> getApplier() {
134+
public BiConsumer<cc.hawkbot.regnum.client.core.discord.GameAnimator.Game, Function<String, String>> getApplier() {
133135
return applier;
134136
}
135137

@@ -140,7 +142,7 @@ public Consumer<cc.hawkbot.regnum.client.core.discord.GameAnimator.Game> getAppl
140142
* @return the {@link GameAnimatorBuilder}
141143
*/
142144
@NotNull
143-
public GameAnimatorBuilder setApplier(@NotNull Consumer<cc.hawkbot.regnum.client.core.discord.GameAnimator.Game> applier) {
145+
public GameAnimatorBuilder setApplier(BiConsumer<cc.hawkbot.regnum.client.core.discord.GameAnimator.Game, Function<String, String>> applier) {
144146
this.applier = applier;
145147
return this;
146148
}
@@ -158,6 +160,7 @@ public long getInterval() {
158160
* Sets the interval in which the games are going to be changed.
159161
*
160162
* @param interval the interval as a long
163+
* @throws IllegalArgumentException when the interval is not grater than 0
161164
* @return the {@link GameAnimatorBuilder}
162165
*/
163166
@NotNull
@@ -172,6 +175,7 @@ public GameAnimatorBuilder setInterval(long interval) {
172175
*
173176
* @param interval the interval
174177
* @param unit the {@link TimeUnit} or {@code null} if you don't want to change it
178+
* @throws IllegalArgumentException when the interval is not grater than 0
175179
* @return the {@link GameAnimatorBuilder}
176180
*/
177181
@NotNull
@@ -196,6 +200,7 @@ public long getInitialDelay() {
196200
* Sets the amount of time till the first animation
197201
*
198202
* @param initialDelay the amount of time as a long
203+
* @throws IllegalArgumentException when the initialDelay is negative
199204
* @return the {@link GameAnimatorBuilder}
200205
*/
201206
@NotNull
@@ -210,6 +215,7 @@ public GameAnimatorBuilder setInitialDelay(long initialDelay) {
210215
*
211216
* @param initialDelay the amount of time as a long
212217
* @param unit the {@link TimeUnit} or {@code null} if you don't want to change it
218+
* @throws IllegalArgumentException when the initialDelay is negative
213219
* @return the {@link GameAnimatorBuilder}
214220
*/
215221
@NotNull
@@ -253,23 +259,43 @@ public GameAnimatorBuilder setTimeUnit(@NotNull TimeUnit unit) {
253259
*
254260
* @param jda the {@link JDA} instance
255261
* @return the {@link GameAnimatorBuilder}
256-
* @see GameAnimatorBuilder#setApplier(Consumer)
262+
* @see GameAnimatorBuilder#setApplier(BiConsumer)
257263
*/
258264
@NotNull
259265
public GameAnimatorBuilder setJDA(JDA jda) {
260-
return setApplier((game) -> JDAExtensions.applyGame(jda, game));
266+
return setApplier((game, transform) -> JDAExtensions.applyGame(jda, game, transform));
261267
}
262268

263269
/**
264270
* Uses an {@link ShardManager} instance for the Applier.
265271
*
266272
* @param shardManager the {@link ShardManager} instance
267273
* @return the {@link GameAnimatorBuilder}
268-
* @see GameAnimatorBuilder#setApplier(Consumer)
274+
* @see GameAnimatorBuilder#setApplier(BiConsumer)
269275
*/
270276
@NotNull
271277
public GameAnimatorBuilder setShardManager(ShardManager shardManager) {
272-
return setApplier((game) -> JDAExtensions.applyGame(shardManager, game));
278+
return setApplier((game, transform) -> JDAExtensions.applyGame(shardManager, game, transform));
279+
}
280+
281+
/**
282+
* Returns the function that transfroms every game before applying it
283+
* @return the {@link Function}
284+
*/
285+
@NotNull
286+
public Function<String, String> getTransform() {
287+
return transform;
288+
}
289+
290+
/**
291+
* Sets the function that transfroms every game before applying it
292+
* @param transform the {@link Function}
293+
* @return the {@link GameAnimatorBuilder}
294+
*/
295+
@NotNull
296+
public GameAnimatorBuilder setTransform(Function<String, String> transform) {
297+
this.transform = transform;
298+
return this;
273299
}
274300

275301
/**
@@ -279,13 +305,15 @@ public GameAnimatorBuilder setShardManager(ShardManager shardManager) {
279305
*/
280306
@NotNull
281307
public GameAnimator build() {
308+
Preconditions.checkState(games.size() >= 2, "You have to register at least two games!");
282309
return new GameAnimator(
283310
scheduler,
284311
ImmutableList.copyOf(games),
285312
applier,
286313
interval,
287314
initialDelay,
288-
unit
315+
unit,
316+
transform
289317
);
290318
}
291319
}

common/src/main/java/me/schlaubi/regnumutils/common/constants/RegnumUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
/**
2323
* Some information about the Regnum utils library.
2424
*/
25-
@SuppressWarnings("unused")
25+
@SuppressWarnings({"unused"})
2626
public class RegnumUtils {
2727

2828
public static final String AUTHOR = "Michael \"Schlaubi\" Rittmeister";

common/src/main/kotlin/me/schlaubi/regnumutils/common/GameAnimator.kt

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ import java.util.concurrent.ScheduledExecutorService
2525
import java.util.concurrent.ScheduledFuture
2626
import java.util.concurrent.ThreadLocalRandom
2727
import java.util.concurrent.TimeUnit
28+
import java.util.function.BiConsumer
2829
import java.util.function.Consumer
30+
import java.util.function.Function
2931

3032
/**
3133
* Animates the bots presence according to the specified [interval] and [TimeUnit][unit].
@@ -39,21 +41,13 @@ import java.util.function.Consumer
3941
class GameAnimator internal constructor(
4042
private val scheduler: ScheduledExecutorService,
4143
private val games: List<GameAnimator.Game>,
42-
private val applier: Consumer<GameAnimator.Game>,
44+
private val applier: BiConsumer<GameAnimator.Game, Function<String, String>>,
4345
private val interval: Long = 30,
4446
private val initialDelay: Long = 0,
45-
private val unit: TimeUnit = TimeUnit.SECONDS
47+
private val unit: TimeUnit = TimeUnit.SECONDS,
48+
private val transform: Function<String, String>
4649
) {
4750

48-
companion object {
49-
/**
50-
* Creates a new [GameAnimatorBuilder].
51-
* @return the [GameAnimatorBuilder]
52-
*/
53-
@JvmStatic
54-
fun builder() = GameAnimatorBuilder()
55-
}
56-
5751
/**
5852
* Starts the animator.
5953
* @return a [ScheduledFuture] representing the task
@@ -66,6 +60,15 @@ class GameAnimator internal constructor(
6660
*/
6761
fun stop(): List<Runnable> = scheduler.shutdownNow()
6862

69-
private fun animate() = applier.accept(games[ThreadLocalRandom.current().nextInt(games.size - 1)])
63+
private fun animate() = applier.accept(games[ThreadLocalRandom.current().nextInt(games.size - 1)], transform)
64+
65+
companion object {
66+
/**
67+
* Creates a new [GameAnimatorBuilder].
68+
* @return the [GameAnimatorBuilder]
69+
*/
70+
@JvmStatic
71+
fun builder() = GameAnimatorBuilder()
72+
}
7073
}
7174

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Regnum - A Discord bot clustering system made for Hawk
3+
*
4+
* Copyright (C) 2019 Michael Rittmeister
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see https://www.gnu.org/licenses/.
18+
*/
19+
20+
package me.schlaubi.regnumutils.common.cache
21+
22+
import net.dv8tion.jda.api.entities.Message
23+
24+
class MemoryMessageCache private constructor() : MessageCache {
25+
26+
private val storage = mutableMapOf<Long, Message>()
27+
28+
override val keys: MutableSet<Long>
29+
get() = storage.keys
30+
31+
override val values: MutableCollection<Message>
32+
get() = storage.values
33+
34+
override val size: Int
35+
get() = storage.size
36+
37+
override fun contains(id: Long) = storage.containsKey(id)
38+
39+
override fun isEmpty() = storage.isEmpty()
40+
41+
override fun get(key: Long) = storage[key]
42+
43+
override fun put(key: Long, value: Message) = storage.put(key, value)
44+
45+
override fun remove(key: Long) = storage.remove(key)
46+
47+
override fun clear() = storage.clear()
48+
49+
companion object {
50+
/**
51+
* @see MessageCache.activate
52+
*/
53+
internal fun create() = MemoryMessageCache()
54+
}
55+
}

0 commit comments

Comments
 (0)