浏览代码

Restore offline traders on restart. Thx InDev, L2Dream, VlLight & janiii.
Req dp update.

JIV 15 年之前
父节点
当前提交
e470ce25fc

+ 6 - 0
L2_GameServer/java/com/l2jserver/Config.java

@@ -654,6 +654,9 @@ public final class Config
 	public static boolean L2JMOD_ENABLE_WAREHOUSESORTING_PRIVATE;
 	public static boolean L2JMOD_ENABLE_WAREHOUSESORTING_PRIVATE;
 	public static boolean OFFLINE_TRADE_ENABLE;
 	public static boolean OFFLINE_TRADE_ENABLE;
 	public static boolean OFFLINE_CRAFT_ENABLE;
 	public static boolean OFFLINE_CRAFT_ENABLE;
+	public static boolean RESTORE_OFFLINERS;
+	public static int OFFLINE_MAX_DAYS; 
+	public static boolean OFFLINE_DISCONNECT_FINISHED;
 	public static boolean OFFLINE_SET_NAME_COLOR;
 	public static boolean OFFLINE_SET_NAME_COLOR;
 	public static int OFFLINE_NAME_COLOR;
 	public static int OFFLINE_NAME_COLOR;
 	public static boolean OFFLINE_FAME;
 	public static boolean OFFLINE_FAME;
@@ -2220,6 +2223,9 @@ public final class Config
 					OFFLINE_SET_NAME_COLOR = Boolean.parseBoolean(L2JModSettings.getProperty("OfflineSetNameColor", "false"));
 					OFFLINE_SET_NAME_COLOR = Boolean.parseBoolean(L2JModSettings.getProperty("OfflineSetNameColor", "false"));
 					OFFLINE_NAME_COLOR = Integer.decode("0x" + L2JModSettings.getProperty("OfflineNameColor", "808080"));
 					OFFLINE_NAME_COLOR = Integer.decode("0x" + L2JModSettings.getProperty("OfflineNameColor", "808080"));
 					OFFLINE_FAME = Boolean.parseBoolean(L2JModSettings.getProperty("OfflineFame", "true"));
 					OFFLINE_FAME = Boolean.parseBoolean(L2JModSettings.getProperty("OfflineFame", "true"));
+					RESTORE_OFFLINERS = Boolean.parseBoolean(L2JModSettings.getProperty("RestoreOffliners", "false")); 
+					OFFLINE_MAX_DAYS = Integer.parseInt(L2JModSettings.getProperty("OfflineMaxDays", "10"));
+					OFFLINE_DISCONNECT_FINISHED = Boolean.parseBoolean(L2JModSettings.getProperty("OfflineDisconnectFinished", "true"));
 
 
 					L2JMOD_ENABLE_MANA_POTIONS_SUPPORT = Boolean.parseBoolean(L2JModSettings.getProperty("EnableManaPotionSupport", "false"));
 					L2JMOD_ENABLE_MANA_POTIONS_SUPPORT = Boolean.parseBoolean(L2JModSettings.getProperty("EnableManaPotionSupport", "false"));
 
 

+ 5 - 0
L2_GameServer/java/com/l2jserver/gameserver/GameServer.java

@@ -61,6 +61,7 @@ import com.l2jserver.gameserver.datatables.NobleSkillTable;
 import com.l2jserver.gameserver.datatables.NpcBufferTable;
 import com.l2jserver.gameserver.datatables.NpcBufferTable;
 import com.l2jserver.gameserver.datatables.NpcTable;
 import com.l2jserver.gameserver.datatables.NpcTable;
 import com.l2jserver.gameserver.datatables.NpcWalkerRoutesTable;
 import com.l2jserver.gameserver.datatables.NpcWalkerRoutesTable;
+import com.l2jserver.gameserver.datatables.OfflineTradersTable;
 import com.l2jserver.gameserver.datatables.PetDataTable;
 import com.l2jserver.gameserver.datatables.PetDataTable;
 import com.l2jserver.gameserver.datatables.PetSkillsTable;
 import com.l2jserver.gameserver.datatables.PetSkillsTable;
 import com.l2jserver.gameserver.datatables.ResidentialSkillTable;
 import com.l2jserver.gameserver.datatables.ResidentialSkillTable;
@@ -401,6 +402,10 @@ public class GameServer
 		
 		
 		TvTManager.getInstance();
 		TvTManager.getInstance();
 		KnownListUpdateTaskManager.getInstance();
 		KnownListUpdateTaskManager.getInstance();
+		
+		if ((Config.OFFLINE_TRADE_ENABLE || Config.OFFLINE_CRAFT_ENABLE) && Config.RESTORE_OFFLINERS)
+			OfflineTradersTable.restoreOfflineTraders(); 
+		
 		if (Config.DEADLOCK_DETECTOR)
 		if (Config.DEADLOCK_DETECTOR)
 		{
 		{
 			_deadDetectThread = new DeadLockDetector();
 			_deadDetectThread = new DeadLockDetector();

+ 12 - 0
L2_GameServer/java/com/l2jserver/gameserver/Shutdown.java

@@ -15,11 +15,13 @@
 package com.l2jserver.gameserver;
 package com.l2jserver.gameserver;
 
 
 import java.util.Collection;
 import java.util.Collection;
+import java.util.logging.Level;
 import java.util.logging.Logger;
 import java.util.logging.Logger;
 
 
 import com.l2jserver.Config;
 import com.l2jserver.Config;
 import com.l2jserver.L2DatabaseFactory;
 import com.l2jserver.L2DatabaseFactory;
 import com.l2jserver.gameserver.datatables.ClanTable;
 import com.l2jserver.gameserver.datatables.ClanTable;
+import com.l2jserver.gameserver.datatables.OfflineTradersTable;
 import com.l2jserver.gameserver.instancemanager.CastleManorManager;
 import com.l2jserver.gameserver.instancemanager.CastleManorManager;
 import com.l2jserver.gameserver.instancemanager.CursedWeaponsManager;
 import com.l2jserver.gameserver.instancemanager.CursedWeaponsManager;
 import com.l2jserver.gameserver.instancemanager.GrandBossManager;
 import com.l2jserver.gameserver.instancemanager.GrandBossManager;
@@ -202,6 +204,16 @@ public class Shutdown extends Thread
 	{
 	{
 		if (this == SingletonHolder._instance)
 		if (this == SingletonHolder._instance)
 		{
 		{
+			try
+			{
+				if ((Config.OFFLINE_TRADE_ENABLE || Config.OFFLINE_CRAFT_ENABLE) && Config.RESTORE_OFFLINERS)
+					OfflineTradersTable.storeOffliners();
+			}
+			catch (Throwable t)
+			{
+				_log.log(Level.WARNING, "Error saving offline shops.",t);
+			}
+				
 			try
 			try
 			{
 			{
 				disconnectAllCharacters();
 				disconnectAllCharacters();

+ 256 - 0
L2_GameServer/java/com/l2jserver/gameserver/datatables/OfflineTradersTable.java

@@ -0,0 +1,256 @@
+/*
+ * This program is free software: you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later
+ * version.
+ *
+ * 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 General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package com.l2jserver.gameserver.datatables;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.util.Calendar;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import com.l2jserver.Config;
+import com.l2jserver.L2DatabaseFactory;
+import com.l2jserver.gameserver.LoginServerThread;
+import com.l2jserver.gameserver.model.L2ManufactureItem;
+import com.l2jserver.gameserver.model.L2ManufactureList;
+import com.l2jserver.gameserver.model.L2World;
+import com.l2jserver.gameserver.model.TradeList.TradeItem;
+import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
+import com.l2jserver.gameserver.network.L2GameClient;
+import com.l2jserver.gameserver.network.L2GameClient.GameClientState;
+
+public class OfflineTradersTable
+{
+	private static Logger _log = Logger.getLogger(OfflineTradersTable.class.getName());
+	
+	//SQL DEFINITIONS
+	private static final String SAVE_OFFLINE_STATUS = "INSERT INTO character_offline_trade (`charId`,`time`,`type`,`title`) VALUES (?,?,?,?)";
+	private static final String SAVE_ITEMS = "INSERT INTO character_offline_trade_items (`charId`,`item`,`count`,`price`) VALUES (?,?,?,?)";
+	private static final String CLEAR_OFFLINE_TABLE = "DELETE FROM character_offline_trade";
+	private static final String CLEAR_OFFLINE_TABLE_ITEMS = "DELETE FROM character_offline_trade_items";
+	private static final String LOAD_OFFLINE_STATUS = "SELECT * FROM character_offline_trade";
+	private static final String LOAD_OFFLINE_ITEMS = "SELECT * FROM character_offline_trade_items WHERE charId = ?";
+	
+	public static void storeOffliners()
+	{
+		Connection con = null;
+		try
+		{
+			con = L2DatabaseFactory.getInstance().getConnection();
+			PreparedStatement stm = con.prepareStatement(CLEAR_OFFLINE_TABLE);
+			stm.execute();
+			stm.close();
+			stm = con.prepareStatement(CLEAR_OFFLINE_TABLE_ITEMS);
+			stm.execute();
+			stm.close();
+			
+			con.setAutoCommit(false); // avoid halfway done
+			stm = con.prepareStatement(SAVE_OFFLINE_STATUS);
+			PreparedStatement stm_items = con.prepareStatement(SAVE_ITEMS);
+			
+			//TextBuilder items = TextBuilder.newInstance();
+			for (L2PcInstance pc : L2World.getInstance().getAllPlayers().values())
+			{
+				try
+				{
+					if ((pc.getPrivateStoreType() != L2PcInstance.STORE_PRIVATE_NONE) && (pc.getClient() == null || pc.getClient().isDetached()))
+					{
+						stm.setInt(1, pc.getObjectId()); //Char Id
+						stm.setLong(2, pc.getOfflineStartTime());
+						stm.setInt(3, pc.getPrivateStoreType()); //store type
+						String title = null;
+						
+						switch (pc.getPrivateStoreType())
+						{
+							case L2PcInstance.STORE_PRIVATE_BUY:
+								if (!Config.OFFLINE_TRADE_ENABLE)
+									continue;
+								title = pc.getBuyList().getTitle();
+								for (TradeItem i : pc.getBuyList().getItems())
+								{
+									stm_items.setInt(1, pc.getObjectId());
+									stm_items.setInt(2, i.getItem().getItemId());
+									stm_items.setLong(3, i.getCount());
+									stm_items.setLong(4, i.getPrice());
+									stm_items.executeUpdate();
+									stm_items.clearParameters();
+								}
+								break;
+							case L2PcInstance.STORE_PRIVATE_SELL:
+							case L2PcInstance.STORE_PRIVATE_PACKAGE_SELL:
+								if (!Config.OFFLINE_TRADE_ENABLE)
+									continue;
+								title = pc.getSellList().getTitle();
+								for (TradeItem i : pc.getSellList().getItems())
+								{
+									stm_items.setInt(1, pc.getObjectId());
+									stm_items.setInt(2, i.getObjectId());
+									stm_items.setLong(3, i.getCount());
+									stm_items.setLong(4, i.getPrice());
+									stm_items.executeUpdate();
+									stm_items.clearParameters();
+								}
+								break;
+							case L2PcInstance.STORE_PRIVATE_MANUFACTURE:
+								if (!Config.OFFLINE_CRAFT_ENABLE)
+									continue;
+								title = pc.getCreateList().getStoreName();
+								for (L2ManufactureItem i : pc.getCreateList().getList())
+								{
+									stm_items.setInt(1, pc.getObjectId());
+									stm_items.setInt(2, i.getRecipeId());
+									stm_items.setLong(3, 0);
+									stm_items.setLong(4, i.getCost());
+									stm_items.executeUpdate();
+									stm_items.clearParameters();
+								}
+						}
+						stm.setString(4, title);
+						stm.executeUpdate();
+						stm.clearParameters();
+						con.commit(); // flush
+					}
+				}
+				catch (Exception e)
+				{
+					_log.log(Level.WARNING, "OfflineTradersTable[storeTradeItems()]: Error while saving offline trader: " + pc.getObjectId() + " " + e, e);
+				}
+			}
+			stm.close();
+			stm_items.close();
+			_log.info("Offline traders stored.");
+		}
+		catch (Exception e)
+		{
+			_log.log(Level.WARNING,"OfflineTradersTable[storeTradeItems()]: Error while saving offline traders: " + e,e);
+		}
+		finally
+		{
+			L2DatabaseFactory.close(con);
+		}
+	}
+	
+	public static void restoreOfflineTraders()
+	{
+		_log.info("Loading offline traders...");
+		Connection con = null;
+		int nTraders = 0;
+		try
+		{
+			con = L2DatabaseFactory.getInstance().getConnection();
+			PreparedStatement stm = con.prepareStatement(LOAD_OFFLINE_STATUS);
+			ResultSet rs = stm.executeQuery();
+			while (rs.next())
+			{
+				long time = rs.getLong("time");
+				if (Config.OFFLINE_MAX_DAYS > 0)
+				{
+					Calendar cal = Calendar.getInstance();
+					cal.setTimeInMillis(time);
+					cal.roll(Calendar.DAY_OF_YEAR, Config.OFFLINE_MAX_DAYS);
+					if (cal.getTimeInMillis() <= System.currentTimeMillis())
+						continue;
+				}
+				
+				int type = rs.getInt("type");
+				if (type == L2PcInstance.STORE_PRIVATE_NONE)
+					continue;
+				
+				L2PcInstance player = null;
+				
+				try
+				{
+					L2GameClient client = new L2GameClient(null);
+					client.setDetached(true);
+					player = L2PcInstance.load(rs.getInt("charId"));
+					client.setActiveChar(player);
+					client.setAccountName(player.getAccountNamePlayer());
+					client.setState(GameClientState.IN_GAME);
+					player.setClient(client);
+					player.setOfflineStartTime(time);
+					player.spawnMe(player.getX(), player.getY(), player.getZ());
+					LoginServerThread.getInstance().addGameServerLogin(player.getAccountName(), client);
+					PreparedStatement stm_items = con.prepareStatement(LOAD_OFFLINE_ITEMS);
+					stm_items.setInt(1, player.getObjectId());
+					ResultSet items = stm_items.executeQuery();
+					
+					switch (type)
+					{
+						case L2PcInstance.STORE_PRIVATE_BUY:
+							while (items.next())
+							{
+								player.getBuyList().addItemByItemId(items.getInt(2), items.getLong(3), items.getLong(4));
+							}
+							player.getBuyList().setTitle(rs.getString("title"));
+							break;
+						case L2PcInstance.STORE_PRIVATE_SELL:
+						case L2PcInstance.STORE_PRIVATE_PACKAGE_SELL:
+							while (items.next())
+							{
+								player.getSellList().addItem(items.getInt(2), items.getLong(3), items.getLong(4));
+							}
+							player.getSellList().setTitle(rs.getString("title"));
+							player.getSellList().setPackaged(type == L2PcInstance.STORE_PRIVATE_PACKAGE_SELL);
+							break;
+						case L2PcInstance.STORE_PRIVATE_MANUFACTURE:
+							L2ManufactureList createList = new L2ManufactureList();
+							while (items.next())
+							{
+								createList.add(new L2ManufactureItem(items.getInt(2), items.getLong(4)));
+							}
+							player.setCreateList(createList);
+							player.getCreateList().setStoreName(rs.getString("title"));
+							break;
+					}
+					items.close();
+					stm_items.close();
+					
+					player.sitDown();
+					if (Config.OFFLINE_SET_NAME_COLOR)
+						player.getAppearance().setNameColor(Config.OFFLINE_NAME_COLOR);
+					player.setPrivateStoreType(type);
+					player.setOnlineStatus(true);
+					player.restoreEffects();
+					player.broadcastUserInfo();
+					nTraders++;
+				}
+				catch (Exception e)
+				{
+					_log.log(Level.WARNING, "OfflineTradersTable[loadOffliners()]: Error loading trader: ",e);
+					if (player != null)
+						player.logout();
+				}
+			}
+			rs.close();
+			stm.close();
+			_log.info("Loaded: " +nTraders+ " offline trader(s)");
+			stm = con.prepareStatement(CLEAR_OFFLINE_TABLE);
+			stm.execute();
+			stm.close();
+			stm = con.prepareStatement(CLEAR_OFFLINE_TABLE_ITEMS);
+			stm.execute();
+			stm.close();
+		}
+		catch (Exception e)
+		{
+			_log.log(Level.WARNING, "OfflineTradersTable[loadOffliners()]: Error while loading offline traders: ",e);
+		}
+		finally
+		{
+			L2DatabaseFactory.close(con);
+		}
+	}
+}

+ 4 - 0
L2_GameServer/java/com/l2jserver/gameserver/idfactory/IdFactory.java

@@ -66,6 +66,8 @@ public abstract class IdFactory
 	        "UPDATE olympiad_fights        SET charTwoId = ?     WHERE charTwoId = ?",
 	        "UPDATE olympiad_fights        SET charTwoId = ?     WHERE charTwoId = ?",
 	        "UPDATE heroes_diary        SET charId = ?     WHERE charId = ?",
 	        "UPDATE heroes_diary        SET charId = ?     WHERE charId = ?",
 	        "UPDATE olympiad_nobles        SET charId = ?     WHERE charId = ?",
 	        "UPDATE olympiad_nobles        SET charId = ?     WHERE charId = ?",
+	        "UPDATE character_offline_trade SET charId = ?     WHERE charId = ?",
+	        "UPDATE character_offline_trade_items SET charId = ? WHERE charId = ?",
 	        "UPDATE clanhall             SET ownerId = ?       WHERE ownerId = ?" };
 	        "UPDATE clanhall             SET ownerId = ?       WHERE ownerId = ?" };
 	
 	
 	protected static final String[] ID_CHECKS = {
 	protected static final String[] ID_CHECKS = {
@@ -222,6 +224,8 @@ public abstract class IdFactory
 			cleanCount += stmt.executeUpdate("DELETE FROM olympiad_fights WHERE olympiad_fights.charOneId NOT IN (SELECT charId FROM characters);");
 			cleanCount += stmt.executeUpdate("DELETE FROM olympiad_fights WHERE olympiad_fights.charOneId NOT IN (SELECT charId FROM characters);");
 			cleanCount += stmt.executeUpdate("DELETE FROM olympiad_fights WHERE olympiad_fights.charTwoId NOT IN (SELECT charId FROM characters);");
 			cleanCount += stmt.executeUpdate("DELETE FROM olympiad_fights WHERE olympiad_fights.charTwoId NOT IN (SELECT charId FROM characters);");
 			cleanCount += stmt.executeUpdate("DELETE FROM heroes_diary WHERE heroes_diary.charId NOT IN (SELECT charId FROM characters);");
 			cleanCount += stmt.executeUpdate("DELETE FROM heroes_diary WHERE heroes_diary.charId NOT IN (SELECT charId FROM characters);");
+			cleanCount += stmt.executeUpdate("DELETE FROM character_offline_trade WHERE character_offline_trade.charId NOT IN (SELECT charId FROM characters);");
+			cleanCount += stmt.executeUpdate("DELETE FROM character_offline_trade_items WHERE character_offline_trade_items.charId NOT IN (SELECT charId FROM characters);");
 			
 			
 			// If the clan does not exist...
 			// If the clan does not exist...
 			cleanCount += stmt.executeUpdate("DELETE FROM clan_privs WHERE clan_privs.clan_id NOT IN (SELECT clan_id FROM clan_data);");
 			cleanCount += stmt.executeUpdate("DELETE FROM clan_privs WHERE clan_privs.clan_id NOT IN (SELECT clan_id FROM clan_data);");

+ 15 - 0
L2_GameServer/java/com/l2jserver/gameserver/model/actor/instance/L2PcInstance.java

@@ -511,6 +511,8 @@ public final class L2PcInstance extends L2Playable
 	private boolean _inCrystallize;
 	private boolean _inCrystallize;
 	private boolean _inCraftMode;
 	private boolean _inCraftMode;
 	
 	
+	private long _offlineShopStart = 0;
+	
 	private L2Transformation _transformation;
 	private L2Transformation _transformation;
 	private int _transformationId = 0;
 	private int _transformationId = 0;
 	
 	
@@ -6373,6 +6375,9 @@ public final class L2PcInstance extends L2Playable
 	public void setPrivateStoreType(int type)
 	public void setPrivateStoreType(int type)
 	{
 	{
 		_privatestore = type;
 		_privatestore = type;
+		
+		if (Config.OFFLINE_DISCONNECT_FINISHED && _privatestore == STORE_PRIVATE_NONE && (getClient() == null || getClient().isDetached()))
+			logout(); 
 	}
 	}
 
 
 	/**
 	/**
@@ -14703,4 +14708,14 @@ public final class L2PcInstance extends L2Playable
 
 
 		return result;
 		return result;
 	}
 	}
+
+	public long getOfflineStartTime()
+	{
+		return _offlineShopStart;
+	}
+	
+	public void setOfflineStartTime(long time)
+	{
+		_offlineShopStart = time;
+	}
 }
 }

+ 2 - 0
L2_GameServer/java/com/l2jserver/gameserver/network/L2GameClient.java

@@ -633,6 +633,8 @@ public final class L2GameClient extends MMOClient<MMOConnection<L2GameClient>>
 								player.getAppearance().setNameColor(Config.OFFLINE_NAME_COLOR);
 								player.getAppearance().setNameColor(Config.OFFLINE_NAME_COLOR);
 								player.broadcastUserInfo();
 								player.broadcastUserInfo();
 							}
 							}
+							if (player.getOfflineStartTime() == 0)
+								player.setOfflineStartTime(System.currentTimeMillis());
 
 
 							LogRecord record = new LogRecord(Level.INFO, "Entering offline mode");
 							LogRecord record = new LogRecord(Level.INFO, "Entering offline mode");
 					    	record.setParameters(new Object[]{L2GameClient.this});
 					    	record.setParameters(new Object[]{L2GameClient.this});

+ 2 - 0
L2_GameServer/java/com/l2jserver/gameserver/network/clientpackets/Logout.java

@@ -104,6 +104,8 @@ public final class Logout extends L2GameClientPacket
 		{
 		{
 			player.getInventory().updateDatabase();
 			player.getInventory().updateDatabase();
 			player.closeNetConnection(true);
 			player.closeNetConnection(true);
+			if (player.getOfflineStartTime() == 0)
+				player.setOfflineStartTime(System.currentTimeMillis());
 			return;
 			return;
 		}
 		}
 
 

+ 13 - 0
L2_GameServer/java/config/l2jmods.properties

@@ -253,6 +253,19 @@ OfflineNameColor = 808080
 # Enable -> true, Disable -> false
 # Enable -> true, Disable -> false
 OfflineFame = True
 OfflineFame = True
 
 
+#Restore offline traders/crafters after restart/shutdown. Default: false.
+RestoreOffliners = False
+
+#Do not restore offline characters, after OfflineMaxDays days spent from first restore.
+#Require server restart to disconnect expired shops.
+#0 = disabled (always restore).
+#Default: 10
+OfflineMaxDays = 10 
+
+#Disconnect shop after finished selling, buying.
+#Default: True
+OfflineDisconnectFinished = True
+
 # ---------------------------------------------------------------------------
 # ---------------------------------------------------------------------------
 # Mana Drugs/Potions
 # Mana Drugs/Potions
 # ---------------------------------------------------------------------------
 # ---------------------------------------------------------------------------