PHP Code:
/*
This file is part of the OdinMS Maple Story Server
Copyright (C) 2008 ~ 2010 Patrick Huy <patrick.huy@frz.cc>
Matthias Butz <matze@odinms.de>
Jan Christian Meyer <vimes@odinms.de>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License version 3
as published by the Free Software Foundation. You may not use, modify
or distribute this program under any other version of the
GNU Affero General Public License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package server;
import database.DatabaseConnection;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import java.util.Map.Entry;
import provider.MapleData;
import provider.MapleDataProvider;
import provider.MapleDataProviderFactory;
import provider.MapleDataTool;
import server.CashItemInfo.CashModInfo;
public class CashItemFactory {
private final static CashItemFactory instance = new CashItemFactory();
private final static int[] bestItems = new int[]{5450000, 1012126, 5230000, 5510000, 5520000};
private final Map<Integer, CashItemInfo> itemStats = new HashMap<>();
private final Map<Integer, List<Integer>> itemPackage = new HashMap<>();
private final Map<Integer, CashModInfo> itemMods = new HashMap<>();
private final Map<Integer, List<Integer>> openBox = new HashMap<>();
private final MapleDataProvider data = MapleDataProviderFactory.getDataProvider(new File("Wz/Etc.wz"));
private static List<Integer> blacklist = new ArrayList<>();
public static CashItemFactory getInstance() {
return instance;
}
public final void initialize() {
try {
itemMods.clear();
itemStats.clear();
itemPackage.clear();
openBox.clear();
try {
BufferedReader reader = new BufferedReader(new FileReader("CBlackList.ini"));
String line = reader.readLine();
while (line != null) {
try {
if (!line.isEmpty() && !line.split(",")[0].startsWith("#")) {
getBlacklist().add(Integer.parseInt(line.split(",")[0]));
}
} catch (Exception ex) {
// System.err.println("Error while loading Black listed Cash Item.\r\n" + ex.getMessage());
}
line = reader.readLine();
}
} catch (IOException | NumberFormatException ex) {
System.err.println("Error while loading cash black list.\r\n" + ex.getMessage());
}
Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("SELECT * FROM cashshop_modified_items");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
CashModInfo modded = new CashModInfo(rs.getInt("serial"), rs.getInt("discount_price"),
rs.getInt("mark"), rs.getInt("showup") > 0, rs.getInt("itemid"),
rs.getInt("priority"), rs.getInt("package") > 0, rs.getInt("period"),
rs.getInt("gender"), rs.getInt("count"), rs.getInt("meso"), rs.getInt("unk_1"),
rs.getInt("unk_2"), rs.getInt("unk_3"), rs.getInt("extra_flags"));
itemMods.put(modded.sn, modded);
if (modded.showUp) {
final CashItemInfo cc = itemStats.get(Integer.valueOf(modded.sn));
if (cc != null) {
modded.toCItem(cc); //init
}
}
}
rs.close();
ps.close();
con.close();
final List<MapleData> cccc = data.getData("Commodity.img").getChildren();
for (MapleData field : cccc) {
final int SN = MapleDataTool.getIntConvert("SN", field, 0);
int ID = MapleDataTool.getIntConvert("ItemId", field, 0);
final int Bonus = MapleDataTool.getIntConvert("Bonus", field, 0);
final int Period = MapleDataTool.getIntConvert("Period", field, 0);
final int Priority = MapleDataTool.getIntConvert("Priority", field, 0);
final int Count = MapleDataTool.getIntConvert("Count", field, 1);
final int Price = MapleDataTool.getIntConvert("Price", field, 0);
final int Gender = MapleDataTool.getIntConvert("Gender", field, 2);
final boolean OnSale = MapleDataTool.getIntConvert("OnSale", field, 0) > 0 && Price > 0;
if (getBlacklist().contains(ID) || ((Period == 0 && OnSale))) { // Block black listed item from CS
if (!itemMods.containsKey(SN)) {
itemMods.put(SN, new CashModInfo(SN, 0, -1, false, ID, 100, false, 0, Gender, 0, 0, 0, 0, 0, 40000));
}
}
final CashItemInfo stats = new CashItemInfo(ID, Count, Price, SN, Period, Gender, OnSale);
if (SN > 0) {
itemStats.put(SN, stats);
}
}
final MapleData b = data.getData("CashPackage.img");
for (MapleData c : b.getChildren()) {
if (c.getChildByPath("SN") == null) {
continue;
}
final int packageID = Integer.parseInt(c.getName());
final List<Integer> packageItems = new ArrayList<>();
for (MapleData d : c.getChildByPath("SN").getChildren()) {
packageItems.add(MapleDataTool.getIntConvert(d));
}
for (int pi : packageItems) { // Block Cash Package if contain item in black list
if (getBlacklist().contains((itemStats.containsKey(pi) ? itemStats.get(pi).getId() : 0))) {
for (int _SN : getSN(packageID)) {
if (!itemMods.containsKey(_SN)) {
itemMods.put(_SN, new CashModInfo(_SN, 0, -1, false, 0, 100, false, 0, 2, 0, 0, 0, 0, 0, 40000));
}
}
}
}
itemPackage.put(packageID, packageItems);
}
for (int i = 0; i < getBlacklist().size(); i++) { // Block Cash Packages
if (getBlacklist().get(i) / 100000 == 9) { // Package only
for (int _SN : getSN(getBlacklist().get(i))) {
if (!itemMods.containsKey(_SN)) {
itemMods.put(_SN, new CashModInfo(_SN, 0, -1, false, 0, 100, false, 0, 2, 0, 0, 0, 0, 0, 40000));
}
}
}
}
} catch (SQLException | NumberFormatException e) {
System.err.println("[Error] An error has occured during init CashItemFactory.\r\n" + e.getMessage());
}
}
private List<Integer> getSN(int ItemId) {
final List<Integer> SN = new ArrayList<>();
for (Entry<Integer, CashItemInfo> item : itemStats.entrySet()) {
if (ItemId == item.getValue().getId()) {
SN.add(item.getKey());
}
}
return SN;
}
public final CashItemInfo getSimpleItem(int sn) {
return itemStats.get(sn);
}
public final CashItemInfo getItem(int sn) {
final CashItemInfo stats = itemStats.get(Integer.valueOf(sn));
final CashModInfo z = getModInfo(sn);
if (z != null && z.showUp) {
return z.toCItem(stats); //null doesnt matter
}
if (stats == null || !stats.onSale()) {
return null;
}
//hmm
return stats;
}
public final List<Integer> getPackageItems(int packageID) {
return itemPackage.get(packageID);
}
public final CashModInfo getModInfo(int sn) {
return itemMods.get(sn);
}
public final Collection<CashModInfo> getAllModInfo() {
return itemMods.values();
}
public final Map<Integer, List<Integer>> getRandomItemInfo() {
return openBox;
}
public final int[] getBestItems() {
return bestItems;
}
public static List<Integer> getBlacklist() {
return blacklist;
}
public static void setBlacklist(List<Integer> aBlacklist) {
blacklist = aBlacklist;
}
}