Refactor methods to reduce its Cognitive Complexity

This commit is contained in:
brenoepic 2022-04-28 23:44:24 -03:00
parent 952ba6e172
commit 7a8a2f42ee
16 changed files with 68 additions and 77 deletions

View File

@ -4,7 +4,7 @@ import com.eu.habbo.Emulator;
import com.eu.habbo.core.*; import com.eu.habbo.core.*;
import com.eu.habbo.habbohotel.achievements.AchievementManager; import com.eu.habbo.habbohotel.achievements.AchievementManager;
import com.eu.habbo.habbohotel.bots.BotManager; import com.eu.habbo.habbohotel.bots.BotManager;
import com.eu.habbo.habbohotel.campaign.calendar.CalendarManager; import com.eu.habbo.habbohotel.campaign.CalendarManager;
import com.eu.habbo.habbohotel.catalog.CatalogManager; import com.eu.habbo.habbohotel.catalog.CatalogManager;
import com.eu.habbo.habbohotel.commands.CommandHandler; import com.eu.habbo.habbohotel.commands.CommandHandler;
import com.eu.habbo.habbohotel.crafting.CraftingManager; import com.eu.habbo.habbohotel.crafting.CraftingManager;

View File

@ -1,10 +1,9 @@
package com.eu.habbo.habbohotel.campaign.calendar; package com.eu.habbo.habbohotel.campaign;
import gnu.trove.map.hash.THashMap; import gnu.trove.map.hash.THashMap;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Map; import java.util.Map;
public class CalendarCampaign { public class CalendarCampaign {
@ -12,26 +11,26 @@ public class CalendarCampaign {
private final String name; private final String name;
private final String image; private final String image;
private Map<Integer , CalendarRewardObject> rewards = new THashMap<>(); private Map<Integer , CalendarRewardObject> rewards = new THashMap<>();
private final Integer start_timestamp; private final Integer startTimestamp;
private final int total_days; private final int totalDays;
private final boolean lock_expired; private final boolean lockExpired;
public CalendarCampaign(ResultSet set) throws SQLException { public CalendarCampaign(ResultSet set) throws SQLException {
this.id = set.getInt("id"); this.id = set.getInt("id");
this.name = set.getString("name"); this.name = set.getString("name");
this.image = set.getString("image"); this.image = set.getString("image");
this.start_timestamp = set.getInt("start_timestamp"); this.startTimestamp = set.getInt("start_timestamp");
this.total_days = set.getInt("total_days"); this.totalDays = set.getInt("total_days");
this.lock_expired = set.getInt("lock_expired") == 1; this.lockExpired = set.getInt("lock_expired") == 1;
} }
public CalendarCampaign(int id, String name, String image, Integer start_timestamp, int total_days, boolean lock_expired) { public CalendarCampaign(int id, String name, String image, Integer startTimestamp, int totalDays, boolean lockExpired) {
this.id = id; this.id = id;
this.name = name; this.name = name;
this.image = image; this.image = image;
this.start_timestamp = start_timestamp; this.startTimestamp = startTimestamp;
this.total_days = total_days; this.totalDays = totalDays;
this.lock_expired = lock_expired; this.lockExpired = lockExpired;
} }
public int getId() { public int getId() {
@ -47,12 +46,12 @@ public class CalendarCampaign {
} }
public Integer getStartTimestamp() { public Integer getStartTimestamp() {
return this.start_timestamp; return this.startTimestamp;
} }
public int getTotalDays() { return this.total_days; } public int getTotalDays() { return this.totalDays; }
public boolean getLockExpired() { return this.lock_expired; } public boolean getLockExpired() { return this.lockExpired; }
public Map<Integer, CalendarRewardObject> getRewards() { return rewards; } public Map<Integer, CalendarRewardObject> getRewards() { return rewards; }

View File

@ -1,4 +1,4 @@
package com.eu.habbo.habbohotel.campaign.calendar; package com.eu.habbo.habbohotel.campaign;
import com.eu.habbo.Emulator; import com.eu.habbo.Emulator;
import com.eu.habbo.habbohotel.users.Habbo; import com.eu.habbo.habbohotel.users.Habbo;
@ -9,16 +9,15 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.sql.*; import java.sql.*;
import java.util.*; import java.time.temporal.ChronoUnit;
import java.util.Date; import java.util.Date;
import java.util.*;
import static java.time.temporal.ChronoUnit.DAYS;
public class CalendarManager { public class CalendarManager {
private static final Logger LOGGER = LoggerFactory.getLogger(CalendarCampaign.class); private static final Logger LOGGER = LoggerFactory.getLogger(CalendarManager.class);
final private static Map<Integer, CalendarCampaign> calendarCampaigns = new THashMap<>(); private static final Map<Integer, CalendarCampaign> calendarCampaigns = new THashMap<>();
public static double HC_MODIFIER; public static double HC_MODIFIER;
public CalendarManager() { public CalendarManager() {
@ -58,7 +57,7 @@ public class CalendarManager {
return false; return false;
} }
HC_MODIFIER = Emulator.getConfig().getDouble("hotel.calendar.pixels.hc_modifier", 2.0); CalendarManager.HC_MODIFIER = Emulator.getConfig().getDouble("hotel.calendar.pixels.hc_modifier", 2.0);
return true; return true;
} }
@ -107,35 +106,29 @@ public class CalendarManager {
public CalendarCampaign getCalendarCampaign(String campaignName) { public CalendarCampaign getCalendarCampaign(String campaignName) {
return calendarCampaigns.values().stream().filter(cc -> Objects.equals(cc.getName(), campaignName)).findFirst().orElse(null); return calendarCampaigns.values().stream().filter(cc -> Objects.equals(cc.getName(), campaignName)).findFirst().orElse(null);
} }
public Map<Integer, CalendarCampaign> getCalendarCampaigns() { public Map<Integer, CalendarCampaign> getCalendarCampaigns() {
return calendarCampaigns; return calendarCampaigns;
} }
public void claimCalendarReward(Habbo habbo, String campaignName, int day, boolean force) { public void claimCalendarReward(Habbo habbo, String campaignName, int day, boolean force) {
CalendarCampaign campaign = calendarCampaigns.values().stream().filter(cc -> Objects.equals(cc.getName(), campaignName)).findFirst().orElse(null); CalendarCampaign campaign = calendarCampaigns.values().stream().filter(cc -> Objects.equals(cc.getName(), campaignName)).findFirst().orElse(null);
if(campaign == null) return; if (campaign == null || campaign.getRewards().isEmpty() || (habbo.getHabboStats().calendarRewardsClaimed.stream().anyMatch(claimed -> claimed.getCampaignId() == campaign.getId() && claimed.getDay() == day)))
if (habbo.getHabboStats().calendarRewardsClaimed.stream().noneMatch(claimed -> claimed.getCampaignId() == campaign.getId() && claimed.getDay() == day)) { return;
List<CalendarRewardObject> rewards = new ArrayList<>(campaign.getRewards().values());
CalendarRewardObject object = rewards.get(Emulator.getRandom().nextInt(rewards.size() - 1 + 1));
Set<Integer> keys = campaign.getRewards().keySet();
Map<Integer, Integer> rewards = new THashMap<>();
if(keys.isEmpty()) return;
keys.forEach(key -> rewards.put(rewards.size() + 1, key));
int rand = Emulator.getRandom().nextInt(rewards.size() - 1 + 1) + 1;
int random = rewards.get(rand);
CalendarRewardObject object = campaign.getRewards().get(random);
if (object == null) return; if (object == null) return;
int daysBetween = (int) DAYS.between(new Timestamp(campaign.getStartTimestamp() * 1000L).toInstant(), new Date().toInstant()); long daysBetween = ChronoUnit.DAYS.between(new Timestamp(campaign.getStartTimestamp() * 1000L).toInstant(), new Date().toInstant());
if(daysBetween >= 0 && daysBetween <= campaign.getTotalDays()) { if (((daysBetween >= 0 && daysBetween <= campaign.getTotalDays()) && (((daysBetween - day <= 2 || !campaign.getLockExpired()) && daysBetween - day >= 0)) || (force && habbo.hasPermission("acc_calendar_force")))) {
int diff = (daysBetween - day);
if ((((diff <= 2 || !campaign.getLockExpired()) && diff >= 0) || (force && habbo.hasPermission("acc_calendar_force")))) {
if (Emulator.getPluginManager().fireEvent(new UserClaimRewardEvent(habbo, campaign, day, object, force)).isCancelled()) { if (Emulator.getPluginManager().fireEvent(new UserClaimRewardEvent(habbo, campaign, day, object, force)).isCancelled()) {
return; return;
} }
habbo.getHabboStats().calendarRewardsClaimed.add(new CalendarRewardClaimed(habbo.getHabboInfo().getId(), campaign.getId(), day, object.getId(), new Timestamp(System.currentTimeMillis()))); habbo.getHabboStats().calendarRewardsClaimed.add(new CalendarRewardClaimed(habbo.getHabboInfo().getId(), campaign.getId(), day, object.getId(), new Timestamp(System.currentTimeMillis())));
habbo.getClient().sendResponse(new CampaignCalendarDoorOpenedMessageComposer(true, object, habbo)); habbo.getClient().sendResponse(new CampaignCalendarDoorOpenedMessageComposer(true, object, habbo));
object.give(habbo); object.give(habbo);
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("INSERT INTO calendar_rewards_claimed (user_id, campaign_id, day, reward_id, timestamp) VALUES (?, ?, ?, ?, ?)")) { try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("INSERT INTO calendar_rewards_claimed (user_id, campaign_id, day, reward_id, timestamp) VALUES (?, ?, ?, ?, ?)")) {
statement.setInt(1, habbo.getHabboInfo().getId()); statement.setInt(1, habbo.getHabboInfo().getId());
statement.setInt(2, campaign.getId()); statement.setInt(2, campaign.getId());
@ -149,6 +142,4 @@ public class CalendarManager {
} }
} }
} }
}
}

View File

@ -1,4 +1,4 @@
package com.eu.habbo.habbohotel.campaign.calendar; package com.eu.habbo.habbohotel.campaign;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;

View File

@ -1,4 +1,4 @@
package com.eu.habbo.habbohotel.campaign.calendar; package com.eu.habbo.habbohotel.campaign;
import com.eu.habbo.Emulator; import com.eu.habbo.Emulator;
import com.eu.habbo.habbohotel.items.Item; import com.eu.habbo.habbohotel.items.Item;

View File

@ -1,7 +1,7 @@
package com.eu.habbo.habbohotel.commands; package com.eu.habbo.habbohotel.commands;
import com.eu.habbo.Emulator; import com.eu.habbo.Emulator;
import com.eu.habbo.habbohotel.campaign.calendar.CalendarCampaign; import com.eu.habbo.habbohotel.campaign.CalendarCampaign;
import com.eu.habbo.habbohotel.gameclients.GameClient; import com.eu.habbo.habbohotel.gameclients.GameClient;
import com.eu.habbo.messages.outgoing.events.calendar.CampaignCalendarDataMessageComposer; import com.eu.habbo.messages.outgoing.events.calendar.CampaignCalendarDataMessageComposer;
import com.eu.habbo.messages.outgoing.habboway.nux.InClientLinkMessageComposer; import com.eu.habbo.messages.outgoing.habboway.nux.InClientLinkMessageComposer;

View File

@ -4168,7 +4168,7 @@ public class Room implements Comparable<Room>, ISerialize, Runnable {
} }
public void giveEffect(RoomUnit roomUnit, int effectId, int duration) { public void giveEffect(RoomUnit roomUnit, int effectId, int duration) {
if (roomUnit == null) return; if (roomUnit == null || roomUnit.getRoom() == null) return;
Habbo habbo = roomUnit.getRoom().getHabbo(roomUnit); Habbo habbo = roomUnit.getRoom().getHabbo(roomUnit);

View File

@ -1,7 +1,7 @@
package com.eu.habbo.habbohotel.users; package com.eu.habbo.habbohotel.users;
import com.eu.habbo.Emulator; import com.eu.habbo.Emulator;
import com.eu.habbo.habbohotel.campaign.calendar.CalendarRewardClaimed; import com.eu.habbo.habbohotel.campaign.CalendarRewardClaimed;
import com.eu.habbo.habbohotel.gameclients.GameClient; import com.eu.habbo.habbohotel.gameclients.GameClient;
import com.eu.habbo.habbohotel.achievements.Achievement; import com.eu.habbo.habbohotel.achievements.Achievement;
import com.eu.habbo.habbohotel.achievements.AchievementManager; import com.eu.habbo.habbohotel.achievements.AchievementManager;

View File

@ -9,6 +9,8 @@ import com.eu.habbo.messages.incoming.achievements.GetAchievementsEvent;
import com.eu.habbo.messages.incoming.ambassadors.AmbassadorAlertEvent; import com.eu.habbo.messages.incoming.ambassadors.AmbassadorAlertEvent;
import com.eu.habbo.messages.incoming.ambassadors.FollowFriendEvent; import com.eu.habbo.messages.incoming.ambassadors.FollowFriendEvent;
import com.eu.habbo.messages.incoming.camera.*; import com.eu.habbo.messages.incoming.camera.*;
import com.eu.habbo.messages.incoming.campaign.OpenCampaignCalendarDoorAsStaffEvent;
import com.eu.habbo.messages.incoming.campaign.OpenCampaignCalendarDoorEvent;
import com.eu.habbo.messages.incoming.catalog.*; import com.eu.habbo.messages.incoming.catalog.*;
import com.eu.habbo.messages.incoming.catalog.marketplace.*; import com.eu.habbo.messages.incoming.catalog.marketplace.*;
import com.eu.habbo.messages.incoming.catalog.recycler.PresentOpenEvent; import com.eu.habbo.messages.incoming.catalog.recycler.PresentOpenEvent;
@ -16,8 +18,6 @@ import com.eu.habbo.messages.incoming.catalog.recycler.RecycleItemsEvent;
import com.eu.habbo.messages.incoming.catalog.recycler.GetRecyclerStatusEvent; import com.eu.habbo.messages.incoming.catalog.recycler.GetRecyclerStatusEvent;
import com.eu.habbo.messages.incoming.catalog.recycler.GetRecyclerPrizesEvent; import com.eu.habbo.messages.incoming.catalog.recycler.GetRecyclerPrizesEvent;
import com.eu.habbo.messages.incoming.crafting.*; import com.eu.habbo.messages.incoming.crafting.*;
import com.eu.habbo.messages.incoming.events.calendar.OpenCampaignCalendarDoorEvent;
import com.eu.habbo.messages.incoming.events.calendar.OpenCampaignCalendarDoorAsStaffEvent;
import com.eu.habbo.messages.incoming.floorplaneditor.GetOccupiedTilesEvent; import com.eu.habbo.messages.incoming.floorplaneditor.GetOccupiedTilesEvent;
import com.eu.habbo.messages.incoming.floorplaneditor.GetRoomEntryTileEvent; import com.eu.habbo.messages.incoming.floorplaneditor.GetRoomEntryTileEvent;
import com.eu.habbo.messages.incoming.floorplaneditor.UpdateFloorPropertiesEvent; import com.eu.habbo.messages.incoming.floorplaneditor.UpdateFloorPropertiesEvent;

View File

@ -232,8 +232,8 @@ public class Incoming {
public static final int NewNavigatorSearchEvent = 249; public static final int NewNavigatorSearchEvent = 249;
public static final int NewUserExperienceGetGiftsEvent = 1822; public static final int NewUserExperienceGetGiftsEvent = 1822;
public static final int NewUserExperienceScriptProceedEvent = 1299; public static final int NewUserExperienceScriptProceedEvent = 1299;
public static final int OpenCampaignCalendarDoorAsStaffEvent = 2257; public static final int OpenCampaignCalendarDoorEvent = 8809;
public static final int OpenCampaignCalendarDoorEvent = 3889; public static final int OpenCampaignCalendarDoorAsStaffEvent = 2507;
public static final int OpenFlatConnectionEvent = 2312; public static final int OpenFlatConnectionEvent = 2312;
public static final int OpenPetPackageEvent = 3698; public static final int OpenPetPackageEvent = 3698;
public static final int OpenTradingEvent = 1481; public static final int OpenTradingEvent = 1481;

View File

@ -1,4 +1,4 @@
package com.eu.habbo.messages.incoming.events.calendar; package com.eu.habbo.messages.incoming.campaign;
import com.eu.habbo.Emulator; import com.eu.habbo.Emulator;
import com.eu.habbo.messages.incoming.MessageHandler; import com.eu.habbo.messages.incoming.MessageHandler;
@ -9,6 +9,7 @@ public class OpenCampaignCalendarDoorAsStaffEvent extends MessageHandler {
String campaignName = this.packet.readString(); String campaignName = this.packet.readString();
int day = this.packet.readInt(); int day = this.packet.readInt();
Emulator.getGameEnvironment().getCalendarManager().claimCalendarReward(this.client.getHabbo(), campaignName, day, false); Emulator.getGameEnvironment().getCalendarManager().claimCalendarReward(this.client.getHabbo(), campaignName, day, true);
} }
} }

View File

@ -1,4 +1,4 @@
package com.eu.habbo.messages.incoming.events.calendar; package com.eu.habbo.messages.incoming.campaign;
import com.eu.habbo.Emulator; import com.eu.habbo.Emulator;
import com.eu.habbo.messages.incoming.MessageHandler; import com.eu.habbo.messages.incoming.MessageHandler;
@ -9,6 +9,6 @@ public class OpenCampaignCalendarDoorEvent extends MessageHandler {
String campaignName = this.packet.readString(); String campaignName = this.packet.readString();
int day = this.packet.readInt(); int day = this.packet.readInt();
Emulator.getGameEnvironment().getCalendarManager().claimCalendarReward(this.client.getHabbo(), campaignName, day, true); Emulator.getGameEnvironment().getCalendarManager().claimCalendarReward(this.client.getHabbo(), campaignName, day, false);
} }
} }

View File

@ -2,7 +2,7 @@ package com.eu.habbo.messages.incoming.handshake;
import com.eu.habbo.Emulator; import com.eu.habbo.Emulator;
import com.eu.habbo.habbohotel.achievements.AchievementManager; import com.eu.habbo.habbohotel.achievements.AchievementManager;
import com.eu.habbo.habbohotel.campaign.calendar.CalendarCampaign; import com.eu.habbo.habbohotel.campaign.CalendarCampaign;
import com.eu.habbo.habbohotel.catalog.TargetOffer; import com.eu.habbo.habbohotel.catalog.TargetOffer;
import com.eu.habbo.habbohotel.messenger.Messenger; import com.eu.habbo.habbohotel.messenger.Messenger;
import com.eu.habbo.habbohotel.modtool.ModToolSanctionItem; import com.eu.habbo.habbohotel.modtool.ModToolSanctionItem;

View File

@ -1,6 +1,6 @@
package com.eu.habbo.messages.outgoing.events.calendar; package com.eu.habbo.messages.outgoing.events.calendar;
import com.eu.habbo.habbohotel.campaign.calendar.CalendarRewardClaimed; import com.eu.habbo.habbohotel.campaign.CalendarRewardClaimed;
import com.eu.habbo.messages.ServerMessage; import com.eu.habbo.messages.ServerMessage;
import com.eu.habbo.messages.outgoing.MessageComposer; import com.eu.habbo.messages.outgoing.MessageComposer;
import com.eu.habbo.messages.outgoing.Outgoing; import com.eu.habbo.messages.outgoing.Outgoing;

View File

@ -1,7 +1,7 @@
package com.eu.habbo.messages.outgoing.events.calendar; package com.eu.habbo.messages.outgoing.events.calendar;
import com.eu.habbo.habbohotel.campaign.calendar.CalendarManager; import com.eu.habbo.habbohotel.campaign.CalendarManager;
import com.eu.habbo.habbohotel.campaign.calendar.CalendarRewardObject; import com.eu.habbo.habbohotel.campaign.CalendarRewardObject;
import com.eu.habbo.habbohotel.users.Habbo; import com.eu.habbo.habbohotel.users.Habbo;
import com.eu.habbo.messages.ServerMessage; import com.eu.habbo.messages.ServerMessage;
import com.eu.habbo.messages.outgoing.MessageComposer; import com.eu.habbo.messages.outgoing.MessageComposer;

View File

@ -1,7 +1,7 @@
package com.eu.habbo.plugin.events.users.calendar; package com.eu.habbo.plugin.events.users.calendar;
import com.eu.habbo.habbohotel.campaign.calendar.CalendarCampaign; import com.eu.habbo.habbohotel.campaign.CalendarCampaign;
import com.eu.habbo.habbohotel.campaign.calendar.CalendarRewardObject; import com.eu.habbo.habbohotel.campaign.CalendarRewardObject;
import com.eu.habbo.habbohotel.users.Habbo; import com.eu.habbo.habbohotel.users.Habbo;
import com.eu.habbo.plugin.events.users.UserEvent; import com.eu.habbo.plugin.events.users.UserEvent;