Created
July 23, 2015 23:02
-
-
Save vemacs/42cb2ca246aad5b5d7af to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package dank.memes.skype.ban; | |
| import com.google.common.collect.HashMultimap; | |
| import com.google.common.collect.Multimap; | |
| import com.samczsun.skype4j.user.User; | |
| import org.yaml.snakeyaml.DumperOptions; | |
| import org.yaml.snakeyaml.Yaml; | |
| import java.io.*; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| import java.util.Map; | |
| public class BanStorage { | |
| private final File banDirectory; | |
| private Multimap<String, Ban> loadedBans; | |
| private static final String BAN_FILE = "bans.yml"; | |
| private static final long PERMABAN_VAL = -1; | |
| private final Yaml yaml; | |
| public BanStorage(File banDirectory) { | |
| this.banDirectory = banDirectory; | |
| DumperOptions options = new DumperOptions(); | |
| options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); | |
| yaml = new Yaml(options); | |
| } | |
| public boolean isBanned(User user) { | |
| List<Ban> invalidBans = new ArrayList<>(); | |
| boolean banned = false; | |
| if (loadedBans.containsKey(user.getUsername())) { | |
| for (Ban b : loadedBans.get(user.getUsername())) { | |
| if (b.getChatName().equals(user.getChat().getIdentity())) { | |
| if (dateApplies(b.getExpirationDate())) { | |
| banned = true; | |
| } else { | |
| invalidBans.add(b); | |
| banned = false; | |
| } | |
| } | |
| } | |
| } | |
| for (Ban b : invalidBans) { | |
| loadedBans.get(b.getUsername()).remove(b); | |
| } | |
| return banned; | |
| } | |
| private boolean dateApplies(long date) { | |
| return date == PERMABAN_VAL || date > System.currentTimeMillis(); | |
| } | |
| public void loadBans() { | |
| File banFile = new File(banDirectory, BAN_FILE); | |
| loadedBans = HashMultimap.create(); | |
| if (!banFile.exists()) { | |
| Ban test1 = new Ban("test1", "testchat1", PERMABAN_VAL); | |
| Ban test2 = new Ban("dank", "memes", System.currentTimeMillis() + 1000 * 5 * 60); | |
| Ban test3 = new Ban("steel", "beams", System.currentTimeMillis() + 1000 * 15 * 60); | |
| loadedBans.put(test1.getUsername(), test1); | |
| loadedBans.put(test2.getUsername(), test2); | |
| loadedBans.put(test3.getUsername(), test3); | |
| return; | |
| } | |
| try (InputStream is = new FileInputStream(banFile)) { | |
| List bans = (List) yaml.load(is); | |
| loadedBans = HashMultimap.create(); | |
| for (Object obj : bans) { | |
| Ban b = (Ban) obj; | |
| if (dateApplies(b.getExpirationDate())) { | |
| loadedBans.get(b.getUsername()).add(b); | |
| System.out.println(b.toString()); | |
| } | |
| } | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| public void saveBans() { | |
| List<Ban> toSave = new ArrayList<>(); | |
| File banFile = new File(banDirectory, BAN_FILE); | |
| for (Map.Entry<String, Ban> entry : loadedBans.entries()) { | |
| toSave.add(entry.getValue()); | |
| } | |
| try (Writer w = new FileWriter(banFile)) { | |
| yaml.dump(toSave, w); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment