diff --git a/src/main/java/com/comandante/creeper/Main.java b/src/main/java/com/comandante/creeper/Main.java index fb7e1ef2b1c39a8ddeeb9d79971baf272566f2de..0c53038e980de7cd310081d4a1fe12deee40c8eb 100644 --- a/src/main/java/com/comandante/creeper/Main.java +++ b/src/main/java/com/comandante/creeper/Main.java @@ -92,7 +92,7 @@ public class Main { MapsManager mapsManager = new MapsManager(creeperConfiguration, roomManager); ChannelUtils channelUtils = new ChannelUtils(playerManager, roomManager); EntityManager entityManager = new EntityManager(roomManager, playerManager, db); - GameManager gameManager = new GameManager(creeperConfiguration, roomManager, playerManager, entityManager, mapsManager, channelUtils); + GameManager gameManager = new GameManager(creeperConfiguration, roomManager, playerManager, entityManager, mapsManager, channelUtils, db); startUpMessage("Reading world from disk."); WorldExporter worldExporter = new WorldExporter(roomManager, mapsManager, gameManager.getFloorManager(), entityManager, gameManager); diff --git a/src/main/java/com/comandante/creeper/bot/BotCommandManager.java b/src/main/java/com/comandante/creeper/bot/BotCommandManager.java index 70b1fb57552137b7b7235f953366cd1da008fa81..7840c96fc908ed11b7bb2952a96b8bb7372f0c8a 100644 --- a/src/main/java/com/comandante/creeper/bot/BotCommandManager.java +++ b/src/main/java/com/comandante/creeper/bot/BotCommandManager.java @@ -2,21 +2,34 @@ package com.comandante.creeper.bot; import com.comandante.creeper.managers.GameManager; +import com.comandante.creeper.player.PlayerMetadata; +import com.comandante.creeper.player.PlayerMetadataSerializer; +import org.mapdb.DB; +import org.mapdb.HTreeMap; public class BotCommandManager { + public static final String QUOTE_STORE = "quoteStore"; private final GameManager gameManager; private final WeatherManager weatherManager; private final ChuckNorrisManager chuckNorrisManager; private final DictionaryManager dictionaryManager; private final OmdbManager omdbManager; + private HTreeMap<String, Quote> quoteStore; + private final DB db; - public BotCommandManager(GameManager gameManager) { + public BotCommandManager(DB db, GameManager gameManager) { this.gameManager = gameManager; this.weatherManager = new WeatherManager(gameManager.getCreeperConfiguration()); this.chuckNorrisManager = new ChuckNorrisManager(gameManager.getCreeperConfiguration()); this.dictionaryManager = new DictionaryManager(gameManager.getCreeperConfiguration()); this.omdbManager = new OmdbManager(); + this.db = db; + if (db.exists(QUOTE_STORE)) { + this.quoteStore = db.get(QUOTE_STORE); + } else { + this.quoteStore = db.createHashMap(QUOTE_STORE).valueSerializer(new QuoteSerializer()).make(); + } } public GameManager getGameManager() { diff --git a/src/main/java/com/comandante/creeper/bot/Quote.java b/src/main/java/com/comandante/creeper/bot/Quote.java new file mode 100644 index 0000000000000000000000000000000000000000..43e4ed8b172236db6cead6b5903605867b0f493c --- /dev/null +++ b/src/main/java/com/comandante/creeper/bot/Quote.java @@ -0,0 +1,42 @@ +package com.comandante.creeper.bot; + +public class Quote { + + private String triggerWord; + private String quote; + private long timestamp; + + public static QuoteBuilder builder = new QuoteBuilder(); + + public Quote(String triggerWord, String quote, long timestamp) { + this.triggerWord = triggerWord; + this.quote = quote; + this.timestamp = timestamp; + } + + public static class QuoteBuilder { + private String triggerWord; + private String quote; + private long timestamp; + + public QuoteBuilder triggerWord(String triggerWord) { + this.triggerWord = triggerWord; + return this; + } + + public QuoteBuilder quote(String quote) { + this.quote = quote; + return this; + } + + public QuoteBuilder timestamp(long timestamp) { + this.timestamp = timestamp; + return this; + } + + public Quote build() { + return new Quote(triggerWord, quote, timestamp); + } + } +} + diff --git a/src/main/java/com/comandante/creeper/bot/QuoteSerializer.java b/src/main/java/com/comandante/creeper/bot/QuoteSerializer.java new file mode 100644 index 0000000000000000000000000000000000000000..6dd0e2d330d502daabc2a045d5eb4a3733d7b57f --- /dev/null +++ b/src/main/java/com/comandante/creeper/bot/QuoteSerializer.java @@ -0,0 +1,27 @@ +package com.comandante.creeper.bot; + +import com.comandante.creeper.spells.Effect; +import com.google.gson.GsonBuilder; +import org.mapdb.Serializer; + +import java.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; +import java.io.Serializable; + +public class QuoteSerializer implements Serializer<Quote>, Serializable { + @Override + public void serialize(DataOutput out, Quote value) throws IOException { + out.writeUTF(new GsonBuilder().create().toJson(value, Effect.class)); + } + + @Override + public Quote deserialize(DataInput in, int available) throws IOException { + return new GsonBuilder().create().fromJson(in.readUTF(), Quote.class); + } + + @Override + public int fixedSize() { + return -1; + } +} \ No newline at end of file diff --git a/src/main/java/com/comandante/creeper/bot/commands/QuoteCommand.java b/src/main/java/com/comandante/creeper/bot/commands/QuoteCommand.java new file mode 100644 index 0000000000000000000000000000000000000000..7f87df0bf4669f01802324e8c8eba46d6943163c --- /dev/null +++ b/src/main/java/com/comandante/creeper/bot/commands/QuoteCommand.java @@ -0,0 +1,49 @@ +package com.comandante.creeper.bot.commands; + +import com.comandante.creeper.bot.BotCommandManager; +import com.comandante.creeper.bot.Quote; +import com.google.api.client.util.Lists; +import com.google.common.collect.Sets; +import com.omertron.omdbapi.OMDBException; +import org.apache.log4j.Logger; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Set; + +public class QuoteCommand extends BotCommand { + + static Set<String> triggers = Sets.newHashSet("q", "quote"); + static String helpUsage = "q word"; + static String helpDescription = "Quote things by things.."; + private static final Logger log = Logger.getLogger(QuoteCommand.class); + + public QuoteCommand(BotCommandManager botCommandManager) { + super(botCommandManager, triggers, helpUsage, helpDescription); + } + + @Override + public List<String> process() { + ArrayList<String> resp = Lists.newArrayList(); + if (args.size() >= 2) { + String key = args.get(0); + args.remove(0); + String value = joinArgs(args); + Quote quote = Quote.builder + .triggerWord(key) + .quote(value) + .timestamp(System.currentTimeMillis()) + .build(); + + + + } + try { + resp.addAll(botCommandManager.getOmdbManager().getMovieInfo(argumentString)); + } catch (OMDBException e) { + log.error(e); + } + return resp; + } +} diff --git a/src/main/java/com/comandante/creeper/managers/GameManager.java b/src/main/java/com/comandante/creeper/managers/GameManager.java index 215f741d70c0e6410a7e49ef65a1f532551798ae..8b0d29a8e35aaeea62a5dfe005a37a036424244e 100755 --- a/src/main/java/com/comandante/creeper/managers/GameManager.java +++ b/src/main/java/com/comandante/creeper/managers/GameManager.java @@ -31,6 +31,7 @@ import com.google.common.collect.Interners; import com.google.common.collect.Maps; import org.apache.commons.lang3.text.WordUtils; import org.apache.log4j.Logger; +import org.mapdb.DB; import org.nocrala.tools.texttablefmt.BorderStyle; import org.nocrala.tools.texttablefmt.ShownBorders; import org.nocrala.tools.texttablefmt.Table; @@ -68,7 +69,7 @@ public class GameManager { private final ItemUseHandler itemUseHandler; private final NpcMover npcMover; - public GameManager(CreeperConfiguration creeperConfiguration, RoomManager roomManager, PlayerManager playerManager, EntityManager entityManager, MapsManager mapsManager, ChannelUtils channelUtils) { + public GameManager(CreeperConfiguration creeperConfiguration, RoomManager roomManager, PlayerManager playerManager, EntityManager entityManager, MapsManager mapsManager, ChannelUtils channelUtils, DB db) { this.roomManager = roomManager; this.playerManager = playerManager; this.entityManager = entityManager; @@ -82,7 +83,7 @@ public class GameManager { this.creeperConfiguration = creeperConfiguration; this.forageManager = new ForageManager(this); this.effectsManager = new EffectsManager(this); - this.botCommandManager = new BotCommandManager(this); + this.botCommandManager = new BotCommandManager(db, this); this.botCommandFactory = new BotCommandFactory(botCommandManager); this.statsModifierFactory = new StatsModifierFactory(this); this.gossipCache = new GossipCache(this);