2011-06-09 22:54:01 +02:00
|
|
|
package com.Acrobot.ChestShop.DB;
|
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
import com.Acrobot.Breeze.Utils.StringUtil;
|
2011-06-09 22:54:01 +02:00
|
|
|
import com.Acrobot.ChestShop.ChestShop;
|
2012-05-10 16:32:25 +02:00
|
|
|
import com.avaje.ebean.ExpressionList;
|
2011-06-09 22:54:01 +02:00
|
|
|
import org.bukkit.Material;
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Acrobot
|
|
|
|
*/
|
|
|
|
public class Generator implements Runnable {
|
2012-05-10 16:32:25 +02:00
|
|
|
private final File pagePath;
|
2011-06-09 22:54:01 +02:00
|
|
|
|
2012-03-01 22:03:59 +01:00
|
|
|
private static String header;
|
|
|
|
private static String row;
|
|
|
|
private static String footer;
|
2011-06-09 22:54:01 +02:00
|
|
|
|
|
|
|
private static BufferedWriter buf;
|
|
|
|
|
2012-05-10 16:32:25 +02:00
|
|
|
|
|
|
|
public Generator(File pagePath) {
|
|
|
|
this.pagePath = pagePath;
|
|
|
|
}
|
|
|
|
|
2011-06-09 22:54:01 +02:00
|
|
|
public void run() {
|
2012-03-01 22:03:59 +01:00
|
|
|
header = fileToString("header");
|
|
|
|
row = fileToString("row");
|
|
|
|
footer = fileToString("footer");
|
|
|
|
|
2012-05-10 16:32:25 +02:00
|
|
|
if (row.isEmpty()) {
|
|
|
|
ChestShop.getBukkitLogger().severe("You lack the necessary HTML files in your plugins/ChestShop/HTML folder!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-06-09 22:54:01 +02:00
|
|
|
generateStats();
|
|
|
|
}
|
|
|
|
|
2012-05-10 16:32:25 +02:00
|
|
|
private void fileStart() throws IOException {
|
|
|
|
FileWriter fw = new FileWriter(pagePath);
|
2011-06-09 22:54:01 +02:00
|
|
|
fw.write(header);
|
|
|
|
fw.close();
|
|
|
|
}
|
|
|
|
|
2012-05-10 16:32:25 +02:00
|
|
|
private void fileEnd(long generationTime) throws IOException {
|
|
|
|
FileWriter fw = new FileWriter(pagePath, true);
|
2011-06-09 22:54:01 +02:00
|
|
|
fw.write(footer.replace("%time", String.valueOf(generationTime)));
|
|
|
|
fw.close();
|
|
|
|
}
|
|
|
|
|
2011-07-23 21:00:47 +02:00
|
|
|
private static String fileToString(String fileName) {
|
2011-06-09 22:54:01 +02:00
|
|
|
try {
|
2012-05-10 16:32:25 +02:00
|
|
|
File htmlFolder = new File(ChestShop.getFolder(), "HTML");
|
|
|
|
File fileToRead = new File(htmlFolder, fileName + ".html");
|
|
|
|
|
|
|
|
FileReader rd = new FileReader(fileToRead);
|
|
|
|
char[] buf = new char[(int) fileToRead.length()];
|
2011-06-09 22:54:01 +02:00
|
|
|
rd.read(buf);
|
|
|
|
return new String(buf);
|
|
|
|
} catch (Exception e) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-23 21:00:47 +02:00
|
|
|
private static double generateItemTotal(int itemID, boolean bought, boolean sold) {
|
2011-06-09 22:54:01 +02:00
|
|
|
List<Transaction> list;
|
2012-05-10 16:32:25 +02:00
|
|
|
ExpressionList<Transaction> checkIf = ChestShop.getDB().find(Transaction.class).where();
|
|
|
|
|
|
|
|
if (bought || sold) {
|
|
|
|
list = checkIf.eq("buy", bought ? 1 : 0).eq("itemID", itemID).findList();
|
|
|
|
} else {
|
|
|
|
list = checkIf.eq("itemID", itemID).findList();
|
|
|
|
}
|
|
|
|
|
|
|
|
return countTransactionAmount(list);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static double countTransactionAmount(List<Transaction> list) {
|
|
|
|
double amount = 0;
|
|
|
|
|
|
|
|
for (Transaction transaction : list) {
|
|
|
|
amount += transaction.getAmount();
|
|
|
|
}
|
2011-08-13 12:08:34 +02:00
|
|
|
|
2011-06-09 22:54:01 +02:00
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
2011-07-23 21:00:47 +02:00
|
|
|
private static double generateTotalBought(int itemID) {
|
2011-06-09 22:54:01 +02:00
|
|
|
return generateItemTotal(itemID, true, false);
|
|
|
|
}
|
|
|
|
|
2011-07-23 21:00:47 +02:00
|
|
|
private static double generateTotalSold(int itemID) {
|
2011-06-09 22:54:01 +02:00
|
|
|
return generateItemTotal(itemID, false, true);
|
|
|
|
}
|
|
|
|
|
2011-07-23 21:00:47 +02:00
|
|
|
private static double generateItemTotal(int itemID) {
|
2011-06-09 22:54:01 +02:00
|
|
|
return generateItemTotal(itemID, false, false);
|
|
|
|
}
|
|
|
|
|
2011-07-23 21:00:47 +02:00
|
|
|
private static float generateAveragePrice(int itemID) {
|
2011-06-09 22:54:01 +02:00
|
|
|
float price = 0;
|
2011-07-23 21:00:47 +02:00
|
|
|
List<Transaction> prices = ChestShop.getDB().find(Transaction.class).where().eq("itemID", itemID).eq("buy", true).findList();
|
2012-05-10 16:32:25 +02:00
|
|
|
|
|
|
|
for (Transaction t : prices) {
|
|
|
|
price += t.getAveragePricePerItem();
|
|
|
|
}
|
|
|
|
|
2011-06-09 22:54:01 +02:00
|
|
|
float toReturn = price / prices.size();
|
|
|
|
return (!Float.isNaN(toReturn) ? toReturn : 0);
|
|
|
|
}
|
|
|
|
|
2011-07-23 21:00:47 +02:00
|
|
|
private static float generateAverageBuyPrice(int itemID) {
|
|
|
|
return generateAveragePrice(itemID);
|
2011-06-09 22:54:01 +02:00
|
|
|
}
|
|
|
|
|
2011-07-23 21:00:47 +02:00
|
|
|
private static void generateItemStats(int itemID) throws IOException {
|
2011-06-09 22:54:01 +02:00
|
|
|
double total = generateItemTotal(itemID);
|
|
|
|
|
2011-07-23 21:00:47 +02:00
|
|
|
if (total == 0) return;
|
2011-06-09 22:54:01 +02:00
|
|
|
|
|
|
|
double bought = generateTotalBought(itemID);
|
|
|
|
double sold = generateTotalSold(itemID);
|
|
|
|
|
|
|
|
Material material = Material.getMaterial(itemID);
|
2012-06-08 15:28:36 +02:00
|
|
|
String matName = StringUtil.capitalizeFirstLetter(material.name(), '_');
|
2011-06-09 22:54:01 +02:00
|
|
|
|
|
|
|
int maxStackSize = material.getMaxStackSize();
|
|
|
|
|
|
|
|
float buyPrice = generateAverageBuyPrice(itemID);
|
|
|
|
|
|
|
|
buf.write(row.replace("%material", matName)
|
|
|
|
.replace("%total", String.valueOf(total))
|
|
|
|
.replace("%bought", String.valueOf(bought))
|
|
|
|
.replace("%sold", String.valueOf(sold))
|
|
|
|
.replace("%maxStackSize", String.valueOf(maxStackSize))
|
|
|
|
.replace("%pricePerStack", String.valueOf((buyPrice * maxStackSize)))
|
|
|
|
.replace("%pricePerItem", String.valueOf(buyPrice)));
|
|
|
|
}
|
|
|
|
|
2012-05-10 16:32:25 +02:00
|
|
|
private void generateStats() {
|
2011-06-09 22:54:01 +02:00
|
|
|
try {
|
2012-05-10 16:32:25 +02:00
|
|
|
File parentFolder = pagePath.getParentFile();
|
|
|
|
if (!parentFolder.exists()) {
|
|
|
|
parentFolder.mkdir();
|
|
|
|
}
|
2011-12-20 21:39:45 +01:00
|
|
|
|
2011-06-09 22:54:01 +02:00
|
|
|
fileStart();
|
|
|
|
|
2012-05-10 16:32:25 +02:00
|
|
|
buf = new BufferedWriter(new FileWriter(pagePath, true));
|
|
|
|
|
2011-06-09 22:54:01 +02:00
|
|
|
long genTime = System.currentTimeMillis();
|
2012-05-10 16:32:25 +02:00
|
|
|
for (Material m : Material.values()) {
|
|
|
|
generateItemStats(m.getId());
|
|
|
|
}
|
2011-08-13 12:08:34 +02:00
|
|
|
|
2011-06-09 22:54:01 +02:00
|
|
|
buf.close();
|
|
|
|
|
2012-05-10 16:32:25 +02:00
|
|
|
long generationTime = (System.currentTimeMillis() - genTime) / 1000;
|
|
|
|
|
|
|
|
fileEnd(generationTime);
|
2011-06-09 22:54:01 +02:00
|
|
|
} catch (Exception e) {
|
2012-06-08 15:28:36 +02:00
|
|
|
ChestShop.getBukkitLogger().severe("Couldn't generate statistics page!");
|
2011-12-20 21:39:45 +01:00
|
|
|
e.printStackTrace();
|
2011-06-09 22:54:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|