Skip to content
This repository has been archived by the owner on Aug 8, 2024. It is now read-only.

Commit

Permalink
refactor: use a much simpler data structure
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanzhoudev committed Sep 10, 2023
1 parent 4bfec68 commit 1e48b2b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@
import com.wynntils.modules.map.managers.GuildResourceManager;
import com.wynntils.modules.map.overlays.renderer.MapInfoUI;
import com.wynntils.webapi.WebManager;
import com.wynntils.webapi.profiles.GuildColorProfile;
import com.wynntils.webapi.profiles.TerritoryProfile;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.util.text.TextFormatting;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class MapTerritory {

private static final CustomColor territoryNameColour = new CustomColor(CommonColors.WHITE);
private static HashMap<String, GuildColorProfile> randomGuildColorsHashMap = new HashMap<>();
private static final HashMap<String, CustomColor> backupGuildColors = new HashMap<>();

ScreenRenderer renderer = null;

Expand Down Expand Up @@ -165,31 +165,26 @@ public void drawScreen(int mouseX, int mouseY, float partialTicks, boolean terri
}

private CustomColor getTerritoryColor(boolean resourceColor) {
if (!resourceColor) {
HashMap<String, GuildColorProfile> guildColorProfileHashMap = WebManager.getGuildColors();
if (guildColorProfileHashMap.isEmpty()) {
Random random = new Random();
CustomColor randomColor = CommonColors.getColors()[random.nextInt(CommonColors.getColors().length)];
randomGuildColorsHashMap.put(String.valueOf(randomGuildColorsHashMap.size()), new GuildColorProfile(territory.getGuild(), territory.getGuildPrefix(), randomColor));
return randomColor;
}
for (GuildColorProfile guildColorProfile : guildColorProfileHashMap.values()) {
if (guildColorProfile.getName().equals(territory.getGuild())) {
return guildColorProfile.getGuildColor();
}
}
for (GuildColorProfile guildColorProfile : randomGuildColorsHashMap.values()) {
if (guildColorProfile.getName().equals(territory.getGuild())) {
return guildColorProfile.getGuildColor();
}
}
if (resourceColor) return resources.getColor();

if (WebManager.getGuildColors().containsKey(territory.getGuild())) {
return WebManager.getGuildColors().get(territory.getGuild());
}

// Guild not found in the list, check backup list and add a random color if not found
if (!backupGuildColors.containsKey(territory.getGuild())) {
Random random = new Random();
CustomColor randomColor = CommonColors.getColors()[random.nextInt(CommonColors.getColors().length)];
randomGuildColorsHashMap.put(String.valueOf(randomGuildColorsHashMap.size()), new GuildColorProfile(territory.getGuild(), territory.getGuildPrefix(), randomColor));
return randomColor;
} else {
return resources.getColor();
backupGuildColors.put(
territory.getGuild(),
CustomColor.fromHSV(
random.nextFloat(),
random.nextFloat(),
0.5f + random.nextFloat() * 0.5f,
1f
)
);
}
return backupGuildColors.get(territory.getGuild());
}

public void postDraw(int mouseX, int mouseY, float partialTicks, int width, int height) {
Expand Down
31 changes: 18 additions & 13 deletions src/main/java/com/wynntils/webapi/WebManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.wynntils.Reference;
import com.wynntils.core.events.custom.WynnGuildWarEvent;
import com.wynntils.core.framework.FrameworkManager;
import com.wynntils.core.framework.rendering.colors.CustomColor;
import com.wynntils.core.utils.Utils;
import com.wynntils.modules.core.enums.UpdateStream;
import com.wynntils.modules.core.overlays.UpdateOverlay;
Expand All @@ -31,14 +32,12 @@
import com.wynntils.webapi.profiles.ServerProfile;
import com.wynntils.webapi.profiles.TerritoryProfile;
import com.wynntils.webapi.profiles.UpdateProfile;
import com.wynntils.webapi.profiles.GuildColorProfile;
import com.wynntils.webapi.profiles.guild.GuildProfile;
import com.wynntils.webapi.profiles.ingredient.IngredientProfile;
import com.wynntils.webapi.profiles.item.IdentificationOrderer;
import com.wynntils.webapi.profiles.item.ItemGuessProfile;
import com.wynntils.webapi.profiles.item.ItemProfile;
import com.wynntils.webapi.profiles.item.enums.ItemType;
import com.wynntils.webapi.profiles.item.objects.IdentificationContainer;
import com.wynntils.webapi.profiles.item.objects.MajorIdentification;
import com.wynntils.webapi.profiles.music.MusicLocationsProfile;
import com.wynntils.webapi.profiles.player.PlayerStatsProfile;
Expand Down Expand Up @@ -69,7 +68,7 @@ public class WebManager {
private static @Nullable WebReader apiUrls;

private static HashMap<String, TerritoryProfile> territories = new HashMap<>();
private static HashMap<String, GuildColorProfile> guildColors = new HashMap<>();
private static HashMap<String, CustomColor> guildColors = new HashMap<>();
private static UpdateProfile updateProfile;
private static boolean ignoringJoinUpdate = false;

Expand Down Expand Up @@ -210,7 +209,7 @@ public static HashMap<String, TerritoryProfile> getTerritories() {
return territories;
}

public static HashMap<String, GuildColorProfile> getGuildColors() {
public static HashMap<String, CustomColor> getGuildColors() {
return guildColors;
}

Expand Down Expand Up @@ -345,15 +344,21 @@ public static void updateGuildColors(RequestHandler handler) {
.cacheTo(new File(API_CACHE_ROOT, "guildColors.json"))
.handleJsonObject(json -> {
if (!json.has("0")) return false;

Type type = new TypeToken<HashMap<String, GuildColorProfile>>() {
}.getType();

GsonBuilder builder = new GsonBuilder();
builder.registerTypeHierarchyAdapter(GuildColorProfile.class, new GuildColorProfile.GuildColorDeserializer());
Gson gson = builder.create();

guildColors = gson.fromJson(json, type);
// json is {"0": {data}}, {"1": {data}}, etc.
// we need to convert it to {data}, {data}, etc.
guildColors = new HashMap<>();
for (Map.Entry<String, JsonElement> entry : json.entrySet()) {
JsonObject data = entry.getValue().getAsJsonObject();
// data is now {"_id":"Kingdom Foxes","prefix":"Fox","color":"#FF8200"}

String colorString = entry.getValue().getAsJsonObject().get("color").getAsString();
if (colorString.length() != 7 && colorString.length() != 4 && colorString.length() != 3) continue;

guildColors.put( // name, CustomColor
entry.getValue().getAsJsonObject().get("_id").getAsString(),
CustomColor.fromString(colorString, 0f)
);
}
return true;
})
);
Expand Down
58 changes: 0 additions & 58 deletions src/main/java/com/wynntils/webapi/profiles/GuildColorProfile.java

This file was deleted.

0 comments on commit 1e48b2b

Please sign in to comment.