From 61aaf6fb28f2f0173ef98782549a1da2976ac6f3 Mon Sep 17 00:00:00 2001 From: Pingex Date: Thu, 26 May 2016 13:11:33 +0200 Subject: [PATCH] League Of Discord, initial commit. --- build.gradle | 3 + config.example.ini | 10 +- .../pingex/dbotm/LeagueOfDiscordModule.java | 122 ++++++++++++++++++ 3 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 src/main/java/net/pingex/dbotm/LeagueOfDiscordModule.java diff --git a/build.gradle b/build.gradle index 897d6a6..4070336 100644 --- a/build.gradle +++ b/build.gradle @@ -30,6 +30,9 @@ dependencies { compile "org.slf4j:slf4j-simple:1.7.9" compile "org.apache.commons:commons-lang3:3.0" compile "com.google.code.gson:gson:2.6.2" + + // LeagueOfDiscordModule + compile "com.robrua:orianna:2.4.3" } jar { diff --git a/config.example.ini b/config.example.ini index 22c6f98..7c3c4ac 100644 --- a/config.example.ini +++ b/config.example.ini @@ -21,4 +21,12 @@ enable = net.pingex.discordbot.HelloWorldModule [permissions] ; The location of the permissions file, defaulting to `permissions.json` if not specified -file = permissions.json \ No newline at end of file +file = permissions.json + +[leagueofdiscord] +; LoL Region to focus requests on. Invalid region will fallback on NA. +region = NA + +; The API key Riot gave you. Empty key will disable the module. +; This key serves as an example and is invalid. You can get a key by visiting https://developer.riotgames.com (LoL account required) +apikey = a3cd6695-2174-4ea8-ac97-414f6dfc8826 \ No newline at end of file diff --git a/src/main/java/net/pingex/dbotm/LeagueOfDiscordModule.java b/src/main/java/net/pingex/dbotm/LeagueOfDiscordModule.java new file mode 100644 index 0000000..e40525a --- /dev/null +++ b/src/main/java/net/pingex/dbotm/LeagueOfDiscordModule.java @@ -0,0 +1,122 @@ +package net.pingex.dbotm; + +import com.robrua.orianna.api.core.RiotAPI; +import com.robrua.orianna.type.core.champion.ChampionStatus; +import com.robrua.orianna.type.core.common.Region; +import com.robrua.orianna.type.core.staticdata.Champion; +import net.pingex.discordbot.AbstractModule; +import net.pingex.discordbot.Command; +import net.pingex.discordbot.Configuration; +import net.pingex.discordbot.Controllable; +import sx.blah.discord.api.IDiscordClient; +import sx.blah.discord.handle.impl.events.MessageReceivedEvent; +import sx.blah.discord.util.DiscordException; +import sx.blah.discord.util.HTTP429Exception; +import sx.blah.discord.util.MissingPermissionsException; +import javax.imageio.ImageIO; +import java.awt.*; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; +import java.net.URL; +import java.util.ArrayList; +import java.util.Map; + +/** + * League of Discord, all the stuff related to League of Legends ! + * @version 0.1-dev + * @author Raphael "Pingex" NAAS + */ +@Controllable(name = "lol") +public class LeagueOfDiscordModule extends AbstractModule +{ + /** + * Loading Splace Image Unit Width + */ + private static final int LSI_UNIT_WIDTH = 308; + + /** + * Loading Splace Image Unit Height + */ + private static final int LSI_UNIT_HEIGHT = 560; + + /** + * Constructor doing all the basic stuff, like registering as a Listener to Discord, getting a logger, etc. + * @param client Discord Client instance used to register self. + */ + public LeagueOfDiscordModule(IDiscordClient client) + { + super(client); + + + // Region config + Region region; + try + { + region = Region.valueOf(Configuration.getValue("leagueofdiscord", "region")); + logger.info("Selected region: " + region); + } + catch (IllegalArgumentException | NullPointerException e) + { + region = Region.NA; + logger.warning("Invalid region selected. Defaulting to NA !"); + } + RiotAPI.setRegion(region); + + // API Key config + if(Configuration.exists("leagueofdiscord", "apikey")) + RiotAPI.setAPIKey(Configuration.getValue("leagueofdiscord", "apikey")); + else + { + logger.severe("API Key not specified. Module shutdown."); + shutdown(); + } + } + + @Command(description = "Returns this week F2P champions.") + public String fwr(MessageReceivedEvent event) + { + Map champs = RiotAPI.getChampionStatuses(true); + + StringBuffer toReturn = new StringBuffer("This week F2P champions:\n"); + ArrayList picMatrix = new ArrayList<>(); + + for(Map.Entry i : champs.entrySet()) + { + toReturn.append("* ").append(i.getKey().getName()).append(" - ").append(i.getKey().getTitle()).append("\n"); + picMatrix.add("http://ddragon.leagueoflegends.com/cdn/img/champion/loading/" + i.getKey().getKey() + "_0.jpg"); + } + + // PICTURE PROCESSING + + int lines = (int)Math.floor(picMatrix.size()/5.1+1); + BufferedImage result = new BufferedImage(LSI_UNIT_WIDTH*5, LSI_UNIT_HEIGHT*lines, BufferedImage.TYPE_INT_RGB); + Graphics g = result.createGraphics(); + + for(int i = 0; i < picMatrix.size(); i++) + { + int linepos = (int)Math.floor(i/5.0); + BufferedImage ins = null; + + try { ins = ImageIO.read(new URL(picMatrix.get(i))); } + catch (IOException e) { toReturn.append("IO Error while processing the picture.").toString(); } + + g.drawImage(ins, (i-linepos*5)*LSI_UNIT_WIDTH, linepos*LSI_UNIT_HEIGHT, null); + } + + try + { + File targetImage = File.createTempFile("DiscordBot", ".jpg");ImageIO.write(result, "jpg", targetImage); + client.getChannelByID(event.getMessage().getChannel().getID()).sendFile(targetImage); + } + catch (IOException e) + { + toReturn.append("IO Error while processing the picture.").toString(); + } + catch (DiscordException | HTTP429Exception | MissingPermissionsException e) + { + toReturn.append("Error with Discord while processing the picture.").toString(); + } + return toReturn.toString(); + } +} \ No newline at end of file