Skip to content

Commit

Permalink
Added Top Movers and fixed some bugs
Browse files Browse the repository at this point in the history
+ Added Top Movers and config options
+ Fixed some bugs when purchasing items
  • Loading branch information
noahbclarkson committed Dec 23, 2020
1 parent 85168ff commit 82ec677
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Auto-Tune/dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@
</dependencies>
<properties>
<mainClass>${project.groupId}.${project.artifactId}</mainClass>
<api.version>1.6</api.version>
<api.version>1.8</api.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
Expand Down
2 changes: 1 addition & 1 deletion Auto-Tune/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<mainClass>${project.groupId}.${project.artifactId}</mainClass>
<api.version>1.6</api.version>
<api.version>1.8</api.version>
</properties>
<!-- Project information -->
<groupId>unprotesting.com.github</groupId>
Expand Down
6 changes: 6 additions & 0 deletions Auto-Tune/src/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ max-volatility: 0.5
## Minimum Volatility per Time Period for the Fixed Volatility price calculation algorithim in economy units
min-volatility: 0.025

## Send players data about the most significant changes in the economy when they join
send-player-top-movers-on-join: true

## How many items should be displayed for sell + buy (A value of 5 means 10 items as 5 for sell + buy)
top-movers-amount: 5

## -- Data Selection Settings -- ##

## Info: When setting your data selection algorithim use a site such as https://www.desmos.com/calculator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public static void loadGUITRADING(Player player, String itemName, Section sec, b
Gui main = new Gui(4, Config.getMenuTitle());
OutlinePane front = new OutlinePane(1, 1, 7, 2);
if (!autosell) {
front = loadTradingItems(player, itemName, sec, front);
front = loadTradingItems(itemName, sec, front);
}
main.addPane(front);
main.addPane(loadReturnButton(sec, autosell));
Expand All @@ -215,14 +215,15 @@ public static void loadGUITRADING(Player player, String itemName, Section sec, b
main.show((HumanEntity) cSender);
}

public static OutlinePane loadTradingItems(Player player, String itemName, Section sec, OutlinePane front) {
public static OutlinePane loadTradingItems(String itemName, Section sec, OutlinePane front) {
for (int i = 0; i < 14; i++) {
final int finalI = i;
ItemStack iStack;
GuiItem gItem;
if (i < 7) {
iStack = loadTradingItem(itemName, amounts[i], true, sec);
gItem = new GuiItem(iStack, event -> {
Player player = (Player) event.getWhoClicked();
if (event.getClick() == ClickType.LEFT) {
event.setCancelled(true);
ConcurrentHashMap<String, Integer> maxBuyMapRec = Main.maxBuyMap.get(player.getUniqueId());
Expand Down Expand Up @@ -279,6 +280,7 @@ public static OutlinePane loadTradingItems(Player player, String itemName, Secti
} else {
iStack = loadTradingItem(itemName, amounts[i - 7], false, sec);
gItem = new GuiItem(iStack, event -> {
Player player = (Player) event.getWhoClicked();
if (event.getClick() == ClickType.LEFT) {
event.setCancelled(true);
ConcurrentHashMap<String, Integer> maxSellMapRec = Main.maxSellMap.get(player.getUniqueId());
Expand Down
36 changes: 31 additions & 5 deletions Auto-Tune/src/unprotesting/com/github/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -83,6 +85,7 @@
import unprotesting.com.github.util.Section;
import unprotesting.com.github.util.StaticFileHandler;
import unprotesting.com.github.util.TextHandler;
import unprotesting.com.github.util.TopMover;
import unprotesting.com.github.util.TutorialHandler;

public final class Main extends JavaPlugin implements Listener {
Expand Down Expand Up @@ -140,9 +143,9 @@ public final class Main extends JavaPlugin implements Listener {
public static Gui gui;

@Getter
@Setter
public static Economy economy;


public ArrayList<String> itemStringArray;

@Getter
Expand All @@ -161,6 +164,12 @@ public final class Main extends JavaPlugin implements Listener {
@Setter
public static Integer materialListSize;

@Getter
public static TopMover[] topSellers;

@Getter
public static TopMover[] topBuyers;

@Getter
public static ConcurrentHashMap<String, ItemPriceData> itemPrices = new ConcurrentHashMap<String, ItemPriceData>();

Expand Down Expand Up @@ -283,6 +292,7 @@ public void onEnable() {
PriceCalculationHandler.loadItemPriceData();
scheduler.scheduleAsyncRepeatingTask(getINSTANCE(), new PriceCalculationHandler(), Config.getTimePeriod() * 600, Config.getTimePeriod() * 1200);
scheduler.scheduleAsyncRepeatingTask(getINSTANCE(), new EnchantmentPriceHandler(), 900*Config.getTimePeriod(), (Config.getTimePeriod()*2400));
if (Config.isSendPlayerTopMoversOnJoin()){loadTopMovers();};
}

private boolean setupEconomy() {
Expand All @@ -291,6 +301,7 @@ private boolean setupEconomy() {
return false;
}
econ = rsp.getProvider();
setEconomy(econ);
return econ != null;
}

Expand Down Expand Up @@ -454,6 +465,8 @@ public static void setupDataFiles() {
if (tempdatadata.get("GDP")==null){
tempdatadata.put("GDP", 0.0);
}
topSellers = new TopMover[Config.getTopMoversAmount()];
topBuyers = new TopMover[Config.getTopMoversAmount()];
}

public static void closeDataFiles(){
Expand All @@ -468,6 +481,22 @@ public static void closeDataFiles(){
loanDB.close();
}

public static void loadTopMovers(){
for (String item : Main.map.keySet()){
TopMover itemMover = new TopMover(item);
}
if (Config.isDebugEnabled()){
Main.debugLog("Top Buyers: ");
for (TopMover mover : topBuyers){
Main.debugLog(mover.toString());
}
Main.debugLog("Top Sellers: ");
for (TopMover mover : topSellers){
Main.debugLog(mover.toString());
}
}
}

@Deprecated
public boolean onCommand(CommandSender sender, Command testcmd, String trade, String[] help) {
if (sender instanceof Player) {
Expand Down Expand Up @@ -518,11 +547,8 @@ public static void saveplayerdata() {
YamlConfiguration.loadConfiguration(playerdata);
playerDataConfig.save(playerdata);
} catch (IOException e) {
plugin.getLogger().warning("Unable to save " + playerdatafilename); // shouldn't really happen, but save
// throws the
// exception
plugin.getLogger().warning("Unable to save " + playerdatafilename);
}

}

public static void log(String input) {
Expand Down
8 changes: 6 additions & 2 deletions Auto-Tune/src/unprotesting/com/github/util/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public final class Config {
autoSellEnabled,
calculateGlobalGDP,
tutorial,
updatePricesWhenInactive;
updatePricesWhenInactive,
sendPlayerTopMoversOnJoin;

@Getter
@Setter
Expand All @@ -32,7 +33,8 @@ public final class Config {
dynamicInflationUpdatePeriod,
maximumShortTradeLength,
InterestRateUpdateRate,
tutorialMessagePeriod;
tutorialMessagePeriod,
topMoversAmount;

@Getter
@Setter
Expand Down Expand Up @@ -70,6 +72,7 @@ public final class Config {
public static void loadDefaults() {
Config.setUpdatePricesWhenInactive(Main.getMainConfig().getBoolean("update-prices-when-inactive", false));
Config.setSellPriceDifferenceVariationEnabled(Main.getMainConfig().getBoolean("sell-price-difference-variation-enabled", true));
Config.setSendPlayerTopMoversOnJoin(Main.getMainConfig().getBoolean("send-player-top-movers-on-join", true));
Config.setWebServer(Main.getMainConfig().getBoolean("web-server-enabled", true));
Config.setTutorial(Main.getMainConfig().getBoolean("tutorial", true));
Config.setCalculateGlobalGDP(Main.getMainConfig().getBoolean("calculate-global-GDP", true));
Expand All @@ -79,6 +82,7 @@ public static void loadDefaults() {
Config.setDebugEnabled(Main.getMainConfig().getBoolean("debug-enabled", false));
Config.setAutoSellProfitUpdatePeriod(Main.getMainConfig().getInt("auto-sell-profit-update-period", 1200));
Config.setPort(Main.getMainConfig().getInt("port", 8321));
Config.setTopMoversAmount(Main.getMainConfig().getInt("top-movers-amount", 5));
Config.setMaximumShortTradeLength(Main.getMainConfig().getInt("maximum-short-trade-length", 100));
Config.setAutoSellUpdatePeriod(Main.getMainConfig().getInt("auto-sell-update-period", 10));
Config.setTimePeriod(Main.getMainConfig().getInt("time-period", 10));
Expand Down
22 changes: 19 additions & 3 deletions Auto-Tune/src/unprotesting/com/github/util/JoinEventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,24 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Logger;

import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.event.player.PlayerJoinEvent;

import unprotesting.com.github.Main;
import unprotesting.com.github.Commands.AutoTuneGUIShopUserCommand;

public class JoinEventHandler implements Listener {

public static Logger log = Logger.getLogger("Minecraft");

@EventHandler
public void onPlayerJoin(PlayerLoginEvent e) {
public void onPlayerJoin(PlayerJoinEvent e) {
Player p = e.getPlayer();
sendTopMoversMessages(p);
OfflinePlayer player = (OfflinePlayer) p;
UUID uuid = p.getUniqueId();
String name = p.getName();
Expand All @@ -31,9 +34,22 @@ public void onPlayerJoin(PlayerLoginEvent e) {
if (!Main.maxSellMap.containsKey(player.getUniqueId())){
ConcurrentHashMap<String, Integer> cMap2 = Main.loadMaxStrings(Main.map);
Main.maxSellMap.put(player.getUniqueId(), cMap2);
}
}

public void sendTopMoversMessages(Player player){
if (Config.isSendPlayerTopMoversOnJoin()){
player.sendMessage(ChatColor.GREEN + "***** | Top Buyers Today: | ******");
for (TopMover mover : Main.topBuyers){
player.sendMessage(ChatColor.GOLD + mover.name + ": " + ChatColor.GREEN + " %+" + AutoTuneGUIShopUserCommand.df2.format(mover.percentage_change));
}
player.sendMessage(ChatColor.RED + "***** | Top Sellers Today: | ******");
for (TopMover mover : Main.topSellers){
player.sendMessage(ChatColor.GOLD + mover.name + ": " + ChatColor.RED + " %" + AutoTuneGUIShopUserCommand.df2.format(mover.percentage_change));
}
}

}

}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public static void loadItemPricesAndCalculate() throws ParseException, ClientPro
Date date = Calendar.getInstance().getTime();
Date newDate = MathHandler.addMinutesToJavaUtilDate(date, Config.getTimePeriod());
String strDate = Main.dateFormat.format(newDate);
Main.loadTopMovers();
Main.log("Done running item price Algorithim, a new check will occur at: " + strDate);
try {
Main.debugLog("Saving data to data.csv file");
Expand Down
102 changes: 102 additions & 0 deletions Auto-Tune/src/unprotesting/com/github/util/TopMover.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package unprotesting.com.github.util;

import java.util.List;

import unprotesting.com.github.Main;
import unprotesting.com.github.Commands.AutoTuneGUIShopUserCommand;

public class TopMover {

public double price;
public double percentage_change = 0.0;
public String name;

public TopMover(String name){
this.name = name;
this.price = AutoTuneGUIShopUserCommand.getItemPrice(name, false);
this.percentage_change = loadPercentageChange(this);
if (percentage_change != 0.0){
if (percentage_change > 0){
int nullValue = 100000;
for (int k = 0; k < Config.getTopMoversAmount(); k++){
if (Main.topBuyers[k] == null){
nullValue = k;
break;
}
}
if (nullValue < 99999){
Main.topBuyers[nullValue] = this;
}
else {
double lowest_percentage = 100.00;
int pos = 10000;
int i = 0;
for (TopMover topMover : Main.topBuyers){
if (topMover.percentage_change < lowest_percentage){
lowest_percentage = topMover.percentage_change;
pos = i;
}
i++;
}
if (this.percentage_change > lowest_percentage && pos != 10000){
Main.topBuyers[pos] = this;
}
}
}
else if (percentage_change < 0){
int nullValue = 100000;
for (int k = 0; k < Config.getTopMoversAmount(); k++){
if (Main.topSellers[k] == null){
nullValue = k;
break;
}
}
if (nullValue < 99999){
Main.topSellers[nullValue] = this;
}
else {
double highest_percentage = 100.00;
int pos = 10000;
int i = 0;
for (TopMover topMover : Main.topSellers){
if (topMover.percentage_change < highest_percentage){
highest_percentage = topMover.percentage_change;
pos = i;
}
i++;
}
if (this.percentage_change < highest_percentage && pos != 10000){
Main.topSellers[pos] = this;
}
}
}
}
}

public double loadPercentageChange(TopMover mover) {
String item = mover.name;
double currentPrice = mover.price;
float timePeriod = (float) Config.getTimePeriod();
float timePeriodsInADay = (float) (1 / (timePeriod / 1440));
List<Double> newMap = Main.getItemPrices().get(item).prices;
if (newMap.size() <= timePeriodsInADay) {
return 0.0;
}
Integer oneDayOldTP = (int) Math.floor(newMap.size() - timePeriodsInADay);
double oneDayOldPrice = newMap.get(oneDayOldTP);
if (oneDayOldPrice > currentPrice) {
double percent = 100 * ((currentPrice / oneDayOldPrice) - 1);
return -1*Math.abs(percent);
} else if (oneDayOldPrice < currentPrice) {
double percent = 100 * (1 - (oneDayOldPrice / currentPrice));
return Math.abs(percent);
} else {
return 0.0;
}
}

public String toString(){
return ("Name: " + this.name + " | Price: " + this.price + " | Percentage Change: %" + this.percentage_change);
}

}

0 comments on commit 82ec677

Please sign in to comment.