2011-06-09 22:54:01 +02:00
|
|
|
package com.Acrobot.ChestShop.DB;
|
|
|
|
|
|
|
|
import com.Acrobot.ChestShop.ChestShop;
|
2011-06-11 17:36:55 +02:00
|
|
|
import com.Acrobot.ChestShop.Config.Config;
|
|
|
|
import com.Acrobot.ChestShop.Config.Property;
|
2011-06-09 22:54:01 +02:00
|
|
|
import com.Acrobot.ChestShop.Logging.Logging;
|
|
|
|
import org.bukkit.Material;
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Acrobot
|
|
|
|
*/
|
|
|
|
public class Generator implements Runnable {
|
2011-07-23 21:00:47 +02:00
|
|
|
private static final String filePath = Config.getString(Property.STATISTICS_PAGE_PATH);
|
2011-06-09 22:54:01 +02:00
|
|
|
|
|
|
|
private static double generationTime;
|
|
|
|
|
2011-07-23 21:00:47 +02:00
|
|
|
private static final String header = fileToString("header");
|
|
|
|
private static final String row = fileToString("row");
|
|
|
|
private static final String footer = fileToString("footer");
|
2011-06-09 22:54:01 +02:00
|
|
|
|
|
|
|
private static BufferedWriter buf;
|
|
|
|
|
|
|
|
public void run() {
|
|
|
|
generateStats();
|
|
|
|
}
|
|
|
|
|
2011-07-23 21:00:47 +02:00
|
|
|
private static void fileStart() throws IOException {
|
2011-06-09 22:54:01 +02:00
|
|
|
FileWriter fw = new FileWriter(filePath);
|
|
|
|
fw.write(header);
|
|
|
|
fw.close();
|
|
|
|
}
|
|
|
|
|
2011-07-23 21:00:47 +02:00
|
|
|
private static void fileEnd() throws IOException {
|
2011-06-09 22:54:01 +02:00
|
|
|
FileWriter fw = new FileWriter(filePath, true);
|
|
|
|
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 {
|
|
|
|
File f = new File(ChestShop.folder + "/HTML/" + fileName + ".html");
|
|
|
|
FileReader rd = new FileReader(f);
|
|
|
|
char[] buf = new char[(int) f.length()];
|
|
|
|
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
|
|
|
double amount = 0;
|
|
|
|
List<Transaction> list;
|
2011-08-13 12:08:34 +02:00
|
|
|
if (bought) list = ChestShop.getDB().find(Transaction.class).where().eq("buy", 1).eq("itemID", itemID).findList();
|
2011-07-23 21:00:47 +02:00
|
|
|
else if (sold) list = ChestShop.getDB().find(Transaction.class).where().eq("buy", 0).eq("itemID", itemID).findList();
|
|
|
|
else list = ChestShop.getDB().find(Transaction.class).where().eq("itemID", itemID).findList();
|
2011-08-13 12:08:34 +02:00
|
|
|
|
2011-07-23 21:00:47 +02:00
|
|
|
for (Transaction t : list) amount += t.getAmount();
|
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();
|
|
|
|
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);
|
|
|
|
String matName = material.name().replace("_", " ").toLowerCase();
|
|
|
|
|
|
|
|
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)));
|
|
|
|
}
|
|
|
|
|
2011-07-23 21:00:47 +02:00
|
|
|
private static void generateStats() {
|
2011-06-09 22:54:01 +02:00
|
|
|
try {
|
|
|
|
fileStart();
|
|
|
|
|
|
|
|
buf = new BufferedWriter(new FileWriter(filePath, true));
|
|
|
|
long genTime = System.currentTimeMillis();
|
2011-07-23 21:00:47 +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();
|
|
|
|
|
|
|
|
generationTime = (System.currentTimeMillis() - genTime) / 1000;
|
|
|
|
fileEnd();
|
|
|
|
} catch (Exception e) {
|
|
|
|
Logging.log("Couldn't generate statistics page!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|