RuneHive-Game
Loading...
Searching...
No Matches
ClanRepository.java
Go to the documentation of this file.
1package com.runehive.content.clanchannel;
2
3import com.runehive.content.clanchannel.channel.ClanChannel;
4import com.runehive.content.clanchannel.content.ClanViewer;
5import com.runehive.util.GsonUtils;
6
7import java.io.File;
8import java.io.FileWriter;
9import java.nio.file.Path;
10import java.nio.file.Paths;
11import java.util.*;
12
13/**
14 * The repository containing all the clans and their corresponding data.
15 *
16 * @author Daniel
17 */
18public class ClanRepository {
19
20 /** Map of all clan chat channels. */
21 private static Map<String, ClanChannel> CHANNELS = new HashMap<>();
22
23 /** Set of all active clan chat channels. */
24 public static Set<ClanChannel> ACTIVE_CHANNELS = new HashSet<>();
25
26 /** Set of all active clan chat tags. */
27 public static Set<String> ACTIVE_TAGS = new HashSet<>();
28
29 /** Set of all active clan chat names. */
30 public static Set<String> ACTIVE_NAMES = new HashSet<>();
31
32 /** Set of all active clan chat names. */
33 public static Set<ClanChannel> ALLTIME = new TreeSet<>();
34
35 /** Set of all active clan chat names. */
36 private static Set<ClanChannel> TOP_PVP = new TreeSet<>();
37
38 /** Set of all active clan chat names. */
39 private static Set<ClanChannel> TOP_PVM = new TreeSet<>();
40
41 /** Set of all active clan chat names. */
42 private static Set<ClanChannel> TOP_SKILLING = new TreeSet<>();
43
44 /** Set of all active clan chat names. */
45 private static Set<ClanChannel> TOP_IRON_MAN = new TreeSet<>();
46
47 /** Returns the channel. */
48 public static ClanChannel getChannel(String channel) {
49 return CHANNELS.get(channel.toLowerCase().trim());
50 }
51
52 /** Adds the channel. */
53 public static void addChannel(ClanChannel channel) {
54 CHANNELS.put(channel.getOwner().toLowerCase().trim(), channel);
55 }
56
57 /** Handles removing a channel from the clan lists. */
58 public static void removeChannel(ClanChannel channel) {
59 CHANNELS.remove(channel.getOwner().toLowerCase().trim());
62 }
63
64 public static void setActive(ClanChannel channel) {
66 }
67
68 public static void setInactive(ClanChannel channel) {
69 if (ACTIVE_CHANNELS.remove(channel)) {
71 }
72 }
73
74 public static boolean nameExist(String name) {
75 return ACTIVE_TAGS.contains(name.toLowerCase().trim());
76 }
77
78 public static boolean tagExist(String tag) {
79 for (ClanChannel channel : CHANNELS.values()) {
80 if (channel.getTag().equalsIgnoreCase(tag))
81 return true;
82 }
83 return false;
84 }
85
86 public static void saveAllActiveClans() {
88 if (channel.activeSize() <= 0) {
89 continue;
90 }
92 }
93 }
94
95 private static void saveClan(ClanChannel channel) {
96 Thread.startVirtualThread(() -> {
97 final File dir = Paths.get("data", "content", "clan").toFile();
98
99 if (!dir.exists()) {
100 dir.mkdirs();
101 }
102
103 try (FileWriter fw = new FileWriter(dir.toPath().resolve(channel.getOwner() + ".json").toFile())) {
104 fw.write(GsonUtils.JSON_PRETTY_NO_NULLS.get().toJson(channel.toJson()));
105 } catch (final Exception e) {
106 e.printStackTrace();
107 }
108 });
109 }
110
111 /** Loads all clans and puts them into the map. */
112 public static void loadChannels() {
113 Path path = Paths.get("./data/content/clan/");
114 File[] files = path.toFile().listFiles();
115
116 if (files == null) {
117 System.out.println("No clan files were found.");
118 return;
119 }
120
121 for (File file : files) {
122 String fileName = file.getName();
123 if (!fileName.toLowerCase().endsWith(".json")) continue;
124 /*ClanChannel channel = ClanChannel.fromJson(file.getName().replace(".json", ""));
125 if (channel != null) {
126 if (!channel.getTag().isEmpty())
127 ACTIVE_TAGS.add(channel.getTag());
128 if (!channel.getName().isEmpty())
129 ACTIVE_NAMES.add(channel.getName());
130 ALLTIME.add(channel);
131 CHANNELS.put(channel.getOwner().toLowerCase().trim(), channel);
132 }*/
133
134 ClanChannel.load(file.getName().replace(".json", ""));
135 }
136 }
137
138 public static Optional<Set<ClanChannel>> getTopChanels(ClanType type) {
139 if (type.equals(ClanType.PVP)) {
140 return Optional.of(TOP_PVP);
141 }
142 if (type.equals(ClanType.PVM)) {
143 return Optional.of(TOP_PVM);
144 }
145 if (type.equals(ClanType.SKILLING)) {
146 return Optional.of(TOP_SKILLING);
147 }
148 if (type.equals(ClanType.IRON_MAN)) {
149 return Optional.of(TOP_IRON_MAN);
150 }
151 return Optional.empty();
152 }
153
154 public static Optional<Set<ClanChannel>> getTopChanels(ClanViewer.Filter filter) {
155 if (filter.equals(ClanViewer.Filter.ALL_TIME)) {
156 return Optional.of(ALLTIME);
157 }
158 if (filter.equals(ClanViewer.Filter.TOP_PVM)) {
159 return Optional.of(TOP_PVM);
160 }
161 if (filter.equals(ClanViewer.Filter.TOP_PVP)) {
162 return Optional.of(TOP_PVP);
163 }
164 if (filter.equals(ClanViewer.Filter.TOP_SKILLING)) {
165 return Optional.of(TOP_SKILLING);
166 }
167 if (filter.equals(ClanViewer.Filter.TOP_IRON_MAN)) {
168 return Optional.of(TOP_IRON_MAN);
169 }
170 return Optional.empty();
171 }
172}
The repository containing all the clans and their corresponding data.
static Set< ClanChannel > ALLTIME
Set of all active clan chat names.
static Set< ClanChannel > ACTIVE_CHANNELS
Set of all active clan chat channels.
static void saveClan(ClanChannel channel)
static Map< String, ClanChannel > CHANNELS
Map of all clan chat channels.
static void addChannel(ClanChannel channel)
Adds the channel.
static void setInactive(ClanChannel channel)
static void setActive(ClanChannel channel)
static Optional< Set< ClanChannel > > getTopChanels(ClanType type)
static Optional< Set< ClanChannel > > getTopChanels(ClanViewer.Filter filter)
static void loadChannels()
Loads all clans and puts them into the map.
static Set< ClanChannel > TOP_IRON_MAN
Set of all active clan chat names.
static Set< String > ACTIVE_NAMES
Set of all active clan chat names.
static Set< ClanChannel > TOP_PVM
Set of all active clan chat names.
static Set< ClanChannel > TOP_PVP
Set of all active clan chat names.
static Set< ClanChannel > TOP_SKILLING
Set of all active clan chat names.
static Set< String > ACTIVE_TAGS
Set of all active clan chat tags.
static ClanChannel getChannel(String channel)
Returns the channel.
static void removeChannel(ClanChannel channel)
Handles removing a channel from the clan lists.
PVM
Clans that are focused on PVM.
Definition ClanType.java:13
IRON_MAN
Clans that are focused on iron man accounts.
Definition ClanType.java:22
PVP
Clans that are focused on PVP.
Definition ClanType.java:10
SKILLING
Clans that are focused on skilling.
Definition ClanType.java:16
static final ThreadLocal< Gson > JSON_PRETTY_NO_NULLS