Свернуть ↑
package ru.catssoftware.gameserver.datatables;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import javolution.util.FastMap;
import javolution.util.FastTable;
import org.apache.log4j.Logger;
import ru.catssoftware.L2DatabaseFactory;
import ru.catssoftware.gameserver.Shutdown;
import ru.catssoftware.util.StatsSet;
public class ServerData {
private static Logger _log = Logger.getLogger(ServerData.class);
private static ServerData _instance = null;
public static FastMap<Integer, FastTable<DonateSkill>> _cachedSkills = new FastMap();
public static FastMap<Integer, FastTable<DonateSkill>> _donateSkills = new FastMap();
public static ServerData getInstance() {
if(_instance==null)
_instance = new ServerData();
return _instance;
}
private StatsSet _serverData = new StatsSet();
public static void init() throws SQLException {
_instance = new ServerData();
_instance.load();
}
private void load() throws SQLException
{
cacheSoldSkills();
}
private ServerData() {
try {
Connection con = L2DatabaseFactory.getInstance().getConnection();
PreparedStatement stm = con.prepareStatement("select * from server_data");
ResultSet rs = stm.executeQuery();
while(rs.next()) {
_serverData.set(rs.getString(1), rs.getString(2));
}
rs.close();
stm.close();
con.close();
Shutdown.getInstance().registerShutdownHandler(new Runnable() {
public void run() {
try {
save();
load();
} catch(SQLException e) {
_log.error("AddDonateSkill: ERROR",e);
}
}
});
} catch(SQLException e) {
_log.error("ServerData: Unable to retrive server data, exiting...",e);
System.exit(1);
}
}
public StatsSet getData() {
return _serverData;
}
public void save() {
try {
Connection con = L2DatabaseFactory.getInstance().getConnection();
PreparedStatement update = con.prepareStatement("update server_data set valueData=? where valueName=?");
PreparedStatement insert = con.prepareStatement("insert into server_data values(?,?)");
for(String s : _serverData.getSet().keySet()) {
update.setString(2, s);
update.setString(1, _serverData.getString(s));
if(update.executeUpdate()==0) {
insert.setString(1, s);
insert.setString(2, _serverData.getString(s));
insert.execute();
}
}
insert.close();
update.close();
con.close();
} catch(SQLException e) {
_log.error("ServerData: Unable to save server data!",e);
}
}
public FastTable<DonateSkill> getDonateSkills(int charId) {
return (FastTable)_cachedSkills.get(Integer.valueOf(charI d));
}
public static class DonateSkill
{
public int cls;
public int id;
public int lvl;
public long expire;
public int priceId;
public int priceCount;
public String priceName;
public String icon;
public String info;
public DonateSkill(int cls, int id, int lvl, long expire)
{
this.cls = cls;
this.id = id;
this.lvl = lvl;
this.expire = expire;
}
public DonateSkill(int cls, int id, int lvl, long expire, int priceId, int priceCount, String priceName, String icon, String info) {
this.cls = cls;
this.id = id;
this.lvl = lvl;
this.expire = expire;
this.priceId = priceId;
this.priceCount = priceCount;
this.priceName = priceName;
this.icon = icon;
this.info = info;
}
}
public void addDonateSkill(int charId, int cls, int id, int lvl, long expire) {
DonateSkill ds = new DonateSkill(cls, id, lvl, expire);
if (_cachedSkills.get(Integer.valueOf(charId)) == null) {
FastTable dst = new FastTable();
dst.add(ds);
_cachedSkills.put(Integer.valueOf(charId), dst);
} else {
((FastTable)_cachedSkills.get(Integer.valueOf(char Id))).add(ds);
}
Connection con = null;
PreparedStatement st = null;
try {
con = L2DatabaseFactory.getInstance().getConnection();
st = con.prepareStatement("REPLACE INTO `z_donate_skills` (`char_id`, `class_id`, `skill_id`, `skill_lvl`, `expire`) VALUES (?, ?, ?, ?, ?)");
st.setInt(1, charId);
st.setInt(2, cls);
st.setInt(3, id);
st.setInt(4, lvl);
st.setLong(5, expire);
st.execute();
} catch (Exception e) {
_log.error("ServerData [ERROR]: addDonateSkill() ->" + e);
} finally {
{
try {
con.close();
st.close();
} catch(SQLException e) {
_log.error("AddDonateSkill: ERROR",e);
}
}
}
}
private void cacheSoldSkills()
{
Connection con = null;
PreparedStatement st = null;
ResultSet rs = null;
try {
con = L2DatabaseFactory.getInstance().getConnection();
con.setTransactionIsolation(1);
st = con.prepareStatement("DELETE FROM z_donate_skills WHERE expire > ? AND expire < ?");
st.setInt(1, 1);
st.setLong(2, System.currentTimeMillis());
st.execute();
st.close();
FastTable dst = new FastTable();
st = con.prepareStatement("SELECT char_id, class_id, skill_id, skill_lvl, expire FROM `z_donate_skills`");
rs = st.executeQuery();
while (rs.next()) {
int charId = rs.getInt("char_id");
int classId = rs.getInt("class_id");
int skillId = rs.getInt("skill_id");
int skillLvl = rs.getInt("skill_lvl");
long expire = rs.getLong("expire");
DonateSkill ds = new DonateSkill(classId, skillId, skillLvl, expire);
if (_cachedSkills.get(Integer.valueOf(charId)) == null) {
dst.clear();
dst.add(ds);
_cachedSkills.put(Integer.valueOf(charId), dst);
} else {
((FastTable)_cachedSkills.get(Integer.valueOf(char Id))).add(ds);
}
}
} catch (Exception e) {
_log.warn("CustomServerData: cacheDonateSkills() error: " + e);
} finally {
try {
con.close();
st.close();
rs.close();
} catch(SQLException e) {
_log.error("AddDonateSkill: ERROR",e);
}
}
}
}