Skip to content

Commit

Permalink
Fixed top movers not sorting correctly
Browse files Browse the repository at this point in the history
Fixed top movers not sorting correctly
  • Loading branch information
noahbclarkson committed Dec 23, 2020
1 parent 82ec677 commit 46b9940
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 52 deletions.
14 changes: 10 additions & 4 deletions Auto-Tune/src/unprotesting/com/github/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -165,10 +167,10 @@ public final class Main extends JavaPlugin implements Listener {
public static Integer materialListSize;

@Getter
public static TopMover[] topSellers;
public static ArrayList<TopMover> topSellers;

@Getter
public static TopMover[] topBuyers;
public static ArrayList<TopMover> topBuyers;

@Getter
public static ConcurrentHashMap<String, ItemPriceData> itemPrices = new ConcurrentHashMap<String, ItemPriceData>();
Expand Down Expand Up @@ -465,8 +467,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()];
topSellers = new ArrayList<TopMover>();
topBuyers = new ArrayList<TopMover>();
}

public static void closeDataFiles(){
Expand All @@ -482,9 +484,13 @@ public static void closeDataFiles(){
}

public static void loadTopMovers(){
Main.topBuyers.clear();
Main.topSellers.clear();
for (String item : Main.map.keySet()){
TopMover itemMover = new TopMover(item);
}
Collections.sort(Main.topBuyers);
Collections.sort(Main.topSellers);
if (Config.isDebugEnabled()){
Main.debugLog("Top Buyers: ");
for (TopMover mover : topBuyers){
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package unprotesting.com.github.util;

import java.util.Arrays;
import java.util.Collections;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Logger;
Expand Down
77 changes: 29 additions & 48 deletions Auto-Tune/src/unprotesting/com/github/util/TopMover.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package unprotesting.com.github.util;

import java.util.Collections;
import java.util.List;

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

public class TopMover {
public class TopMover implements Comparable< TopMover >{

public double price;
public double percentage_change = 0.0;
Expand All @@ -15,58 +16,28 @@ 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;
if (this.percentage_change != 0.0){
if (this.percentage_change > 0){
if (Main.getTopBuyers().size() < Config.getTopMoversAmount()){
Main.topBuyers.add(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{
Collections.sort(Main.topBuyers);
if (Main.getTopBuyers().get(Config.getTopMoversAmount()-1).percentage_change < this.percentage_change){
Main.topBuyers.remove(Config.getTopMoversAmount()-1);
Main.topBuyers.add(Config.getTopMoversAmount()-1, 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;
if (this.percentage_change < 0){
if (Main.getTopSellers().size() < Config.getTopMoversAmount()){
Main.topSellers.add(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;
else{
Collections.sort(Main.topSellers);
if (Main.getTopSellers().get(Config.getTopMoversAmount()-1).percentage_change > this.percentage_change){
Main.topSellers.remove(Config.getTopMoversAmount()-1);
Main.topSellers.add(Config.getTopMoversAmount()-1, this);
}
}
}
Expand Down Expand Up @@ -98,5 +69,15 @@ public double loadPercentageChange(TopMover mover) {
public String toString(){
return ("Name: " + this.name + " | Price: " + this.price + " | Percentage Change: %" + this.percentage_change);
}

@Override
public int compareTo(TopMover o) {
if (o.percentage_change < 0 && this.percentage_change < 0){
return (int) (this.percentage_change*1000 - o.percentage_change*1000);
}
else{
return (int) (o.percentage_change*1000 - this.percentage_change*1000);
}
}

}

0 comments on commit 46b9940

Please sign in to comment.