Untitled

mail@pastecode.io avatar
unknown
java
2 years ago
11 kB
3
Indexable
Never
package com.rs.game.player.content.dailychallenges;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import com.rs.game.item.ItemIdentifiers;
import com.rs.game.player.Player;
import com.rs.game.player.content.dailychallenges.challenges.DailyChallenge;
import com.rs.game.player.content.dailychallenges.challenges.DailyChallengeState;
import com.rs.game.player.content.dailychallenges.challenges.DailyChallengesHolder;
import com.rs.game.player.content.dailychallenges.map.DailyChallengesMap;
import com.rs.game.player.dialogue.Dialogue;
import com.rs.utils.Colors;

import lombok.Getter;
import lombok.Setter;

public class DailyChallengesManager implements Serializable {

	private static final long serialVersionUID = -9206796684786532738L;

	public static final int RESET_VIS_WAX_AMOUNT = 25;
	public static final int EXTEND_VIS_WAX_AMOUNT = 50;

	public transient DailyChallenge selectedChallenge;
	public transient int selectedChallengeIndex;

	@Getter
	private LinkedHashMap<DailyChallenge, DailyChallengeState> dailyChallenges;

	@Getter
	@Setter
	private transient Player player;

	public DailyChallengesManager() {
		setPlayer(player);
		if (dailyChallenges == null) {
			dailyChallenges = new LinkedHashMap<DailyChallenge, DailyChallengeState>(5);
		}
	}

	public void onLogin() {
		// if(DailyChallengesMap. {
		if (requiresNewChallenges()) {
			DailyChallengesMap.getEntry(player.getUsername()).setNeedNewChallenges(false);
			generateDailyChallenges();
			player.sm(Colors.GREEN + "Your Daily Challenges have been reset!");
		} else {
			player.sm(Colors.CYAN + "There is " + DailyChallengesExecutor.timeUntilNextExecution()
					+ " time left until Daily Challenges reset.");
		}
		// }
	}

	private boolean requiresNewChallenges() {
		if (DailyChallengesMap.getEntry(player.getUsername()) == null) {
			return false;
		}
		return DailyChallengesMap.getEntry(player.getUsername()).needNewChallenges();
	}

	public DailyChallenge getDailyChallengeByIndexInMap(int index) {
		List<DailyChallenge> keys = new ArrayList<DailyChallenge>(dailyChallenges.keySet());
		return keys.get(index);
	}

	public DailyChallenge getDailyChallengeBySkill(int skillId) {
		for (Map.Entry<DailyChallenge, DailyChallengeState> entry : player.getDailyChallenges().getDailyChallenges()
				.entrySet()) {
			int challengeSkill = entry.getKey().getSkillId();
			if (skillId == challengeSkill) {
				return entry.getKey();
			}
		}
		return null;
	}

	public void updateChallengeProgress(DailyChallenge challenge, int amount) {
		if (dailyChallenges.containsKey(challenge)) {
			dailyChallenges.get(challenge).setProgress(dailyChallenges.get(challenge).getProgress() + amount);
		}
	}

	private static LinkedHashMap<DailyChallenge, DailyChallengeState> cloneMap(
			LinkedHashMap<DailyChallenge, DailyChallengeState> mapToClone) {
		Map<DailyChallenge, DailyChallengeState> newMap = new LinkedHashMap<DailyChallenge, DailyChallengeState>(
				mapToClone.size());
		for (Map.Entry<DailyChallenge, DailyChallengeState> entry : mapToClone.entrySet()) {
			newMap.put(entry.getKey(), entry.getValue());
		}
		return (LinkedHashMap<DailyChallenge, DailyChallengeState>) newMap;
	}

	private static DailyChallenge getDailyChallengeByIndexInMap(Map<DailyChallenge, DailyChallengeState> map,
			int index) {
		List<DailyChallenge> keys = new ArrayList<DailyChallenge>(map.keySet());
		return keys.get(index);
	}

	private static DailyChallengeState getDailyChallengeProgressByIndexInMap(
			Map<DailyChallenge, DailyChallengeState> map, int index) {
		List<DailyChallengeState> values = new ArrayList<DailyChallengeState>(map.values());
		return values.get(index);
	}

	private boolean rerollChallenge(DailyChallenge challenge) {
		if (dailyChallenges.containsKey(challenge) && !isCompleted(challenge)) {
			Map<DailyChallenge, DailyChallengeState> tempMap = cloneMap(dailyChallenges);
			List<DailyChallenge> keys = new ArrayList<DailyChallenge>(dailyChallenges.keySet());
			int indexInMap = keys.indexOf(challenge);
			keys.remove(indexInMap);
			DailyChallengesHolder challengeSkill = randomSkill();
			while (true) {
				challengeSkill = randomSkill();
				if (!existingChallengeSkill(challengeSkill)) {
					break;
				}
			}
			DailyChallenge generatedChallenge = randomChallengeFromSkill(challengeSkill);
			resetDailyChallenges();
			for (int i = 0; i < 5; i++) {
				if (i == indexInMap) {
					dailyChallenges.put(generatedChallenge, new DailyChallengeState(generatedChallenge));
					dailyChallenges.get(generatedChallenge).setReRolled(true);
					continue;
				}
				dailyChallenges.put(getDailyChallengeByIndexInMap(tempMap, i),
						getDailyChallengeProgressByIndexInMap(tempMap, i));
			}
			return true;
		} else {
			player.getDialogueManager().startDialogue("SimpleMessage", Colors.RED + "Error message!<br>"
					+ "This challenge has been either completed, claimed, or doesn't exist in your challenges.");
		}
		return false;
	}

	public void generateDailyChallenges() {
		resetDailyChallenges();
		DailyChallengesHolder challengeSkill = randomSkill();
		while (dailyChallenges.size() < 5) {
			challengeSkill = randomSkill();
			if (existingChallengeSkill(challengeSkill)) {
				continue;
			}
			DailyChallenge challenge = randomChallengeFromSkill(challengeSkill);
			dailyChallenges.put(challenge, new DailyChallengeState(challenge));
		}
		player.sm("Size: " + dailyChallenges.size());
		printMap();
	}

	public void claimReward(DailyChallenge challenge) {
		if (isCompleted(challenge) && !dailyChallenges.get(challenge).isClaimedReward()) {
			// code for reward here.
			dailyChallenges.get(challenge).setClaimedReward(true);
			return;
		}
		player.sm(Colors.RED + "There are no rewards for you to claim.");
	}

	public boolean rewardAwaits() {
		for (Map.Entry<DailyChallenge, DailyChallengeState> entry : player.getDailyChallenges().getDailyChallenges()
				.entrySet()) {
			if (isCompleted(entry.getKey()) && !dailyChallenges.get(entry.getKey()).isClaimedReward()) {
				return true;
			}
		}
		return false;
	}

	public boolean isCompleted(DailyChallenge challenge) {
		for (Map.Entry<DailyChallenge, DailyChallengeState> entry : player.getDailyChallenges().getDailyChallenges()
				.entrySet()) {
			if (entry.getKey().equals(challenge)) {
				Integer value = dailyChallenges.get(entry.getKey()).getProgress();
				if (value >= challenge.getFinishCount()) {
					return true;
				}
			}
		}
		return false;
	}

	private void resetDailyChallenges() {
		dailyChallenges.clear();
	}

	public void printMap() {
		for (Map.Entry<DailyChallenge, DailyChallengeState> entry : player.getDailyChallenges().getDailyChallenges()
				.entrySet()) {
			DailyChallenge key = entry.getKey();
			DailyChallengeState state = dailyChallenges.get(entry.getKey());
			System.out.println("Entry: " + key.getName() + " skill id: " + key.getSkillId() + ", progress: ("
					+ state.getProgress() + "/" + key.getFinishCount() + "), has claimed reward: "
					+ state.isClaimedReward() + ", is rerolled: " + state.isReRolled());
		}
	}

	private boolean existingChallengeSkill(DailyChallengesHolder challengeSkill) {
		for (Map.Entry<DailyChallenge, DailyChallengeState> entry : dailyChallenges.entrySet()) {
			DailyChallenge key = entry.getKey();
			if (key.getName().toLowerCase().equalsIgnoreCase(challengeSkill.toString().toLowerCase())) {
				return true;
			}
		}
		return false;
	}

	private DailyChallengesHolder randomSkill() {
		int pick = new Random().nextInt(DailyChallengesHolder.values().length);
		return DailyChallengesHolder.values()[pick];
	}

	private DailyChallenge randomChallengeFromSkill(DailyChallengesHolder challengeSkill) {
		int pick = new Random().nextInt(challengeSkill.getDailyChallenges().size());
		return challengeSkill.getDailyChallenges().get(pick);
	}

	public void reRoll(DailyChallenge challenge) {
		player.getDialogueManager().startDialogue(new Dialogue() {

			@Override
			public void start() {
				sendOptionsDialogue("Reroll " + Colors.RED + "[Daily " + challenge.getName() + "]</col> for "
						+ Colors.RED + RESET_VIS_WAX_AMOUNT + " Vis wax?", "Yes, proceed to Reroll.", "No.");
			}

			@Override
			public void run(int interfaceId, int componentId) {
				switch (stage) {
				case -1:
					switch (componentId) {
					case OPTION_1:
						if (player.getInventory().getAmountOf(ItemIdentifiers.VIS_WAX) >= RESET_VIS_WAX_AMOUNT) {
							if (rerollChallenge(challenge)) {
								DailyChallengesInterface.refreshInterface(player);
								sendDialogue("Success.");
								stage = 0;
							}
						} else {
							sendDialogue("You do not have enough Vis wax to do this, requires: " + RESET_VIS_WAX_AMOUNT
									+ " to reroll a challenge.");
							stage = 0;
						}
						break;
					case OPTION_2:
						end();
						break;
					}
					break;
				case 0:
					end();
					break;
				}
			}

			@Override
			public void finish() {
			}

		});
	}

	private enum XP_DROPTABLE {
		// skillLevel, Experience
		LVL_1(1, 500), LVL_2(2, 552), LVL_3(3, 616), LVL_4(4, 680), LVL_5(5, 746), LVL_6(6, 832), LVL_7(7, 984),
		LVL_8(8, 1016), LVL_9(9, 1154), LVL_10(10, 1228), LVL_11(11, 1360), LVL_12(12, 1504), LVL_13(13, 1644),
		LVL_14(14, 1832), LVL_15(15, 2016), LVL_16(16, 2092), LVL_17(17, 2192), LVL_18(18, 2280), LVL_19(19, 2384),
		LVL_20(20, 2480), LVL_21(21, 2596), LVL_22(22, 2696), LVL_23(23, 2816), LVL_24(24, 2940), LVL_25(25, 3072),
		LVL_26(26, 3192), LVL_27(27, 3242), LVL_28(28, 3312), LVL_29(29, 3624), LVL_30(30, 3784), LVL_31(31, 3946),
		LVL_32(32, 4112), LVL_33(33, 4288), LVL_34(34, 4474), LVL_35(35, 4664), LVL_36(36, 4868), LVL_37(37, 5078),
		LVL_38(38, 5296), LVL_39(39, 5532), LVL_40(40, 5764), LVL_41(41, 6016), LVL_42(42, 6276), LVL_43(43, 6544),
		LVL_44(44, 6828), LVL_45(45, 7116), LVL_46(46, 7432), LVL_47(47, 7764), LVL_48(48, 8100), LVL_49(49, 8440),
		LVL_50(50, 8808), LVL_51(51, 9186), LVL_52(52, 9600), LVL_53(53, 9996), LVL_54(54, 10436), LVL_55(55, 10896),
		LVL_56(56, 11376), LVL_57(57, 11880), LVL_58(58, 12366), LVL_59(59, 12929), LVL_60(60, 13474),
		LVL_61(61, 14057), LVL_62(62, 14684), LVL_63(63, 15290), LVL_64(64, 16033), LVL_65(65, 16671),
		LVL_66(66, 17372), LVL_67(67, 18152), LVL_68(68, 19032), LVL_69(69, 19760), LVL_70(70, 20742),
		LVL_71(71, 21543), LVL_72(72, 22474), LVL_73(73, 23572), LVL_74(74, 24655), LVL_75(75, 25709),
		LVL_76(76, 26715), LVL_77(77, 27960), LVL_78(78, 29174), LVL_79(79, 30337), LVL_80(80, 31840),
		LVL_81(81, 33328), LVL_82(82, 34780), LVL_83(83, 36174), LVL_84(84, 38096), LVL_85(85, 39347),
		LVL_86(86, 41196), LVL_87(87, 43003), LVL_88(88, 44739), LVL_89(89, 47380), LVL_90(90, 48972),
		LVL_91(91, 51612), LVL_92(92, 52914), LVL_93(93, 55424), LVL_94(94, 57886), LVL_95(95, 60261),
		LVL_96(96, 64517), LVL_97(97, 66779), LVL_98(98, 68815);

		@Getter
		private int skillLevel, experience;

		XP_DROPTABLE(int skillLevel, int experience) {
			this.skillLevel = skillLevel;
			this.experience = experience;
		}
	}

}