Forráskód Böngészése

BETA: Implementing AccountVariables (Same as PlayerVariables but attached to player's account)

Rumen Nikiforov 12 éve
szülő
commit
fc1e9f1ec3

+ 54 - 30
L2J_Server_BETA/java/com/l2jserver/gameserver/model/actor/instance/L2PcInstance.java

@@ -133,7 +133,6 @@ import com.l2jserver.gameserver.model.PartyMatchRoom;
 import com.l2jserver.gameserver.model.PartyMatchRoomList;
 import com.l2jserver.gameserver.model.PartyMatchWaitingList;
 import com.l2jserver.gameserver.model.PcCondOverride;
-import com.l2jserver.gameserver.model.PlayerVariables;
 import com.l2jserver.gameserver.model.ShortCuts;
 import com.l2jserver.gameserver.model.ShotType;
 import com.l2jserver.gameserver.model.TeleportBookmark;
@@ -232,6 +231,8 @@ import com.l2jserver.gameserver.model.skills.targets.L2TargetType;
 import com.l2jserver.gameserver.model.stats.Env;
 import com.l2jserver.gameserver.model.stats.Formulas;
 import com.l2jserver.gameserver.model.stats.Stats;
+import com.l2jserver.gameserver.model.variables.AccountVariables;
+import com.l2jserver.gameserver.model.variables.PlayerVariables;
 import com.l2jserver.gameserver.model.zone.L2ZoneType;
 import com.l2jserver.gameserver.model.zone.ZoneId;
 import com.l2jserver.gameserver.model.zone.type.L2BossZone;
@@ -7753,6 +7754,12 @@ public final class L2PcInstance extends L2Playable
 		{
 			vars.store();
 		}
+		
+		final AccountVariables aVars = getScript(AccountVariables.class);
+		if (aVars != null)
+		{
+			aVars.store();
+		}
 	}
 	
 	@Override
@@ -8590,6 +8597,21 @@ public final class L2PcInstance extends L2Playable
 		return _henna[slot - 1];
 	}
 	
+	/**
+	 * @return {@code true} if player has at least 1 henna symbol, {@code false} otherwise.
+	 */
+	public boolean hasHennas()
+	{
+		for (L2Henna henna : _henna)
+		{
+			if (henna != null)
+			{
+				return true;
+			}
+		}
+		return false;
+	}
+	
 	/**
 	 * @return the henna holder for this player.
 	 */
@@ -14819,6 +14841,20 @@ public final class L2PcInstance extends L2Playable
 		return false;
 	}
 	
+	@Override
+	public void addOverrideCond(PcCondOverride... excs)
+	{
+		super.addOverrideCond(excs);
+		getVariables().set(COND_OVERRIDE_KEY, Long.toString(_exceptions));
+	}
+	
+	@Override
+	public void removeOverridedCond(PcCondOverride... excs)
+	{
+		super.removeOverridedCond(excs);
+		getVariables().set(COND_OVERRIDE_KEY, Long.toString(_exceptions));
+	}
+	
 	/**
 	 * @return {@code true} if {@link PlayerVariables} instance is attached to current player's scripts, {@code false} otherwise.
 	 */
@@ -14836,6 +14872,23 @@ public final class L2PcInstance extends L2Playable
 		return vars != null ? vars : addScript(new PlayerVariables(getObjectId()));
 	}
 	
+	/**
+	 * @return {@code true} if {@link AccountVariables} instance is attached to current player's scripts, {@code false} otherwise.
+	 */
+	public boolean hasAccountVariables()
+	{
+		return getScript(AccountVariables.class) != null;
+	}
+	
+	/**
+	 * @return {@link AccountVariables} instance containing parameters regarding player.
+	 */
+	public AccountVariables getAccountVariables()
+	{
+		final AccountVariables vars = getScript(AccountVariables.class);
+		return vars != null ? vars : addScript(new AccountVariables(getAccountName()));
+	}
+	
 	/**
 	 * Adds a henna listener
 	 * @param listener
@@ -14998,33 +15051,4 @@ public final class L2PcInstance extends L2Playable
 	{
 		return _eventListeners;
 	}
-	
-	/**
-	 * @return {@code true} if player has at least 1 henna symbol, {@code false} otherwise.
-	 */
-	public boolean hasHennas()
-	{
-		for (L2Henna henna : _henna)
-		{
-			if (henna != null)
-			{
-				return true;
-			}
-		}
-		return false;
-	}
-	
-	@Override
-	public void addOverrideCond(PcCondOverride... excs)
-	{
-		super.addOverrideCond(excs);
-		getVariables().set(COND_OVERRIDE_KEY, Long.toString(_exceptions));
-	}
-	
-	@Override
-	public void removeOverridedCond(PcCondOverride... excs)
-	{
-		super.removeOverridedCond(excs);
-		getVariables().set(COND_OVERRIDE_KEY, Long.toString(_exceptions));
-	}
 }

+ 100 - 0
L2J_Server_BETA/java/com/l2jserver/gameserver/model/variables/AbstractVariables.java

@@ -0,0 +1,100 @@
+/*
+ * Copyright (C) 2004-2013 L2J Server
+ * 
+ * This file is part of L2J Server.
+ * 
+ * L2J Server 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.
+ * 
+ * L2J Server 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.model.variables;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import com.l2jserver.gameserver.model.StatsSet;
+
+/**
+ * @author UnAfraid
+ */
+public abstract class AbstractVariables extends StatsSet
+{
+	private final AtomicBoolean _hasChanges = new AtomicBoolean(false);
+	
+	protected abstract void load();
+	
+	public abstract void store();
+	
+	/**
+	 * Overriding following methods to prevent from doing useless database operations if there is no changes since player's login.
+	 */
+	
+	@Override
+	public final void set(String name, boolean value)
+	{
+		_hasChanges.compareAndSet(false, true);
+		super.set(name, value);
+	}
+	
+	@Override
+	public final void set(String name, double value)
+	{
+		_hasChanges.compareAndSet(false, true);
+		super.set(name, value);
+	}
+	
+	@Override
+	public final void set(String name, Enum<?> value)
+	{
+		_hasChanges.compareAndSet(false, true);
+		super.set(name, value);
+	}
+	
+	@Override
+	public final void set(String name, int value)
+	{
+		_hasChanges.compareAndSet(false, true);
+		super.set(name, value);
+	}
+	
+	@Override
+	public final void set(String name, long value)
+	{
+		_hasChanges.compareAndSet(false, true);
+		super.set(name, value);
+	}
+	
+	@Override
+	public final void set(String name, String value)
+	{
+		_hasChanges.compareAndSet(false, true);
+		super.set(name, value);
+	}
+	
+	/**
+	 * @return {@code true} if changes are made since last load/save.
+	 */
+	public final boolean hasChanges()
+	{
+		return _hasChanges.get();
+	}
+	
+	/**
+	 * Atomically sets the value to the given updated value if the current value {@code ==} the expected value.
+	 * @param expect
+	 * @param update
+	 * @return {@code true} if successful. {@code false} return indicates that the actual value was not equal to the expected value.
+	 */
+	public final boolean compareAndSetChanges(boolean expect, boolean update)
+	{
+		return _hasChanges.compareAndSet(expect, update);
+	}
+}

+ 117 - 0
L2J_Server_BETA/java/com/l2jserver/gameserver/model/variables/AccountVariables.java

@@ -0,0 +1,117 @@
+/*
+ * Copyright (C) 2004-2013 L2J DataPack
+ * 
+ * This file is part of L2J DataPack.
+ * 
+ * L2J DataPack 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.
+ * 
+ * L2J DataPack 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.model.variables;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.Map.Entry;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import com.l2jserver.L2DatabaseFactory;
+
+/**
+ * @author UnAfraid
+ */
+public class AccountVariables extends AbstractVariables
+{
+	private static final Logger _log = Logger.getLogger(AccountVariables.class.getName());
+	
+	// SQL Queries.
+	private static final String SELECT_QUERY = "SELECT * FROM account_gsdata WHERE account_name = ?";
+	private static final String DELETE_QUERY = "DELETE FROM account_gsdata WHERE account_name = ?";
+	private static final String INSERT_QUERY = "INSERT INTO account_gsdata (account_name, var, value) VALUES (?, ?, ?)";
+	
+	private final String _accountName;
+	
+	public AccountVariables(String accountName)
+	{
+		_accountName = accountName;
+		load();
+	}
+	
+	@Override
+	protected void load()
+	{
+		// Restore previous variables.
+		try (Connection con = L2DatabaseFactory.getInstance().getConnection();
+			PreparedStatement st = con.prepareStatement(SELECT_QUERY))
+		{
+			st.setString(1, _accountName);
+			try (ResultSet rset = st.executeQuery())
+			{
+				while (rset.next())
+				{
+					set(rset.getString("var"), rset.getString("value"));
+				}
+			}
+		}
+		catch (SQLException e)
+		{
+			_log.log(Level.WARNING, getClass().getSimpleName() + ": Couldn't restore variables for: " + _accountName, e);
+		}
+		finally
+		{
+			compareAndSetChanges(true, false);
+		}
+	}
+	
+	@Override
+	public void store()
+	{
+		// No changes, nothing to store.
+		if (!hasChanges())
+		{
+			return;
+		}
+		
+		try (Connection con = L2DatabaseFactory.getInstance().getConnection())
+		{
+			// Clear previous entries.
+			try (PreparedStatement st = con.prepareStatement(DELETE_QUERY))
+			{
+				st.setString(1, _accountName);
+				st.execute();
+			}
+			
+			// Insert all variables.
+			try (PreparedStatement st = con.prepareStatement(INSERT_QUERY))
+			{
+				st.setString(1, _accountName);
+				for (Entry<String, Object> entry : getSet().entrySet())
+				{
+					st.setString(2, entry.getKey());
+					st.setString(3, String.valueOf(entry.getValue()));
+					st.addBatch();
+				}
+				st.executeBatch();
+			}
+		}
+		catch (SQLException e)
+		{
+			_log.log(Level.WARNING, getClass().getSimpleName() + ": Couldn't update variables for: " + _accountName, e);
+		}
+		finally
+		{
+			compareAndSetChanges(true, false);
+		}
+	}
+}

+ 13 - 54
L2J_Server_BETA/java/com/l2jserver/gameserver/model/PlayerVariables.java → L2J_Server_BETA/java/com/l2jserver/gameserver/model/variables/PlayerVariables.java

@@ -16,24 +16,24 @@
  * 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.model;
+package com.l2jserver.gameserver.model.variables;
 
 import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.Map.Entry;
-import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
 import com.l2jserver.L2DatabaseFactory;
+import com.l2jserver.gameserver.model.L2World;
 import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
 
 /**
  * @author UnAfraid
  */
-public class PlayerVariables extends StatsSet
+public class PlayerVariables extends AbstractVariables
 {
 	private static final Logger _log = Logger.getLogger(PlayerVariables.class.getName());
 	
@@ -43,7 +43,6 @@ public class PlayerVariables extends StatsSet
 	private static final String INSERT_QUERY = "INSERT INTO character_variables (charId, var, val) VALUES (?, ?, ?)";
 	
 	private final int _objectId;
-	private final AtomicBoolean _hasChanges = new AtomicBoolean(false);
 	
 	public PlayerVariables(int objectId)
 	{
@@ -51,7 +50,8 @@ public class PlayerVariables extends StatsSet
 		load();
 	}
 	
-	private void load()
+	@Override
+	protected void load()
 	{
 		// Restore previous variables.
 		try (Connection con = L2DatabaseFactory.getInstance().getConnection();
@@ -62,7 +62,7 @@ public class PlayerVariables extends StatsSet
 			{
 				while (rset.next())
 				{
-					super.set(rset.getString("var"), rset.getString("val"));
+					set(rset.getString("var"), rset.getString("val"));
 				}
 			}
 		}
@@ -70,12 +70,17 @@ public class PlayerVariables extends StatsSet
 		{
 			_log.log(Level.WARNING, getClass().getSimpleName() + ": Couldn't restore variables for: " + getPlayer(), e);
 		}
+		finally
+		{
+			compareAndSetChanges(true, false);
+		}
 	}
 	
+	@Override
 	public void store()
 	{
 		// No changes, nothing to store.
-		if (!_hasChanges.get())
+		if (!hasChanges())
 		{
 			return;
 		}
@@ -108,56 +113,10 @@ public class PlayerVariables extends StatsSet
 		}
 		finally
 		{
-			_hasChanges.compareAndSet(true, false);
+			compareAndSetChanges(true, false);
 		}
 	}
 	
-	/**
-	 * Overriding following methods to prevent from doing useless database operations if there is no changes since player's login.
-	 */
-	
-	@Override
-	public void set(String name, boolean value)
-	{
-		_hasChanges.compareAndSet(false, true);
-		super.set(name, value);
-	}
-	
-	@Override
-	public void set(String name, double value)
-	{
-		_hasChanges.compareAndSet(false, true);
-		super.set(name, value);
-	}
-	
-	@Override
-	public void set(String name, Enum<?> value)
-	{
-		_hasChanges.compareAndSet(false, true);
-		super.set(name, value);
-	}
-	
-	@Override
-	public void set(String name, int value)
-	{
-		_hasChanges.compareAndSet(false, true);
-		super.set(name, value);
-	}
-	
-	@Override
-	public void set(String name, long value)
-	{
-		_hasChanges.compareAndSet(false, true);
-		super.set(name, value);
-	}
-	
-	@Override
-	public void set(String name, String value)
-	{
-		_hasChanges.compareAndSet(false, true);
-		super.set(name, value);
-	}
-	
 	public L2PcInstance getPlayer()
 	{
 		return L2World.getInstance().getPlayer(_objectId);