Browse Source

BETA: Olympiad rework:
* Unnecessary float cast/math round.
* Writing to static field from instanced method.
* Wrong usage of objects and maps, if you get a reference from an object in a map and update it (the object, for example using a method to update a parameter), '''is not required to remove and then put the object again''', __unless you have created a new object instance__, where '''also is not required to remove'''! put(..) ''method will replace/update given the proper key and the old value will be returned, if not existent return null.''
* CompetitionType class formatting.
* Some other minor fixes.

This fixes unexpected endless loops and data inconsistency and improves performance.

'''Note:''' Report bugs at [http://www.l2jserver.com/forum/viewtopic.php?f=69&t=24536 Olympiad rework] topic.

Zoey76 13 years ago
parent
commit
01c0e5bcf9

+ 3 - 5
L2J_Server_BETA/java/com/l2jserver/gameserver/model/olympiad/CompetitionType.java

@@ -15,9 +15,7 @@
 package com.l2jserver.gameserver.model.olympiad;
 
 /**
- * 
  * @author DS
- *
  */
 public enum CompetitionType
 {
@@ -25,14 +23,14 @@ public enum CompetitionType
 	NON_CLASSED("non-classed"),
 	TEAMS("teams"),
 	OTHER("other");
-
+	
 	private final String _name;
-
+	
 	private CompetitionType(String name)
 	{
 		_name = name;
 	}
-
+	
 	@Override
 	public final String toString()
 	{

+ 70 - 35
L2J_Server_BETA/java/com/l2jserver/gameserver/model/olympiad/Olympiad.java

@@ -57,7 +57,7 @@ public class Olympiad
 	
 	private static final Map<Integer, StatsSet> _nobles = new FastMap<>();
 	protected static L2FastList<StatsSet> _heroesToBe;
-	private static TIntIntHashMap _noblesRank;
+	private static final TIntIntHashMap _noblesRank = new TIntIntHashMap();
 	
 	private static final String OLYMPIAD_DATA_FILE = "config/olympiad.properties";
 	public static final String OLYMPIAD_HTML_PATH = "data/html/olympiad/";
@@ -78,7 +78,41 @@ public class Olympiad
 	private static final String OLYMPIAD_MONTH_CREATE = "INSERT INTO olympiad_nobles_eom SELECT charId, class_id, olympiad_points, competitions_done, competitions_won, competitions_lost, competitions_drawn FROM olympiad_nobles";
 	private static final int[] HERO_IDS =
 	{
-		88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 131, 132, 133, 134
+		88,
+		89,
+		90,
+		91,
+		92,
+		93,
+		94,
+		95,
+		96,
+		97,
+		98,
+		99,
+		100,
+		101,
+		102,
+		103,
+		104,
+		105,
+		106,
+		107,
+		108,
+		109,
+		110,
+		111,
+		112,
+		113,
+		114,
+		115,
+		116,
+		117,
+		118,
+		131,
+		132,
+		133,
+		134
 	};
 	
 	private static final int COMP_START = Config.ALT_OLY_START_TIME; // 6PM
@@ -244,13 +278,12 @@ public class Olympiad
 		try
 		{
 			con = L2DatabaseFactory.getInstance().getConnection();
-			PreparedStatement statement = con.prepareStatement(OLYMPIAD_LOAD_NOBLES);
-			ResultSet rset = statement.executeQuery();
-			
+			final PreparedStatement statement = con.prepareStatement(OLYMPIAD_LOAD_NOBLES);
+			final ResultSet rset = statement.executeQuery();
+			StatsSet statData;
 			while (rset.next())
 			{
-				StatsSet statData = new StatsSet();
-				int charId = rset.getInt(CHAR_ID);
+				statData = new StatsSet();
 				statData.set(CLASS_ID, rset.getInt(CLASS_ID));
 				statData.set(CHAR_NAME, rset.getString(CHAR_NAME));
 				statData.set(POINTS, rset.getInt(POINTS));
@@ -264,7 +297,7 @@ public class Olympiad
 				statData.set(COMP_DONE_WEEK_TEAM, rset.getInt(COMP_DONE_WEEK_TEAM));
 				statData.set("to_save", false);
 				
-				_nobles.put(charId, statData);
+				addNobleStats(rset.getInt(CHAR_ID), statData);
 			}
 			
 			rset.close();
@@ -301,13 +334,13 @@ public class Olympiad
 				milliToEnd = getMillisToValidationEnd();
 			}
 			
-			_log.info("Olympiad System: " + Math.round(milliToEnd / 60000) + " minutes until period ends");
+			_log.info("Olympiad System: " + (milliToEnd / 60000) + " minutes until period ends");
 			
 			if (_period == 0)
 			{
 				milliToEnd = getMillisToWeekChange();
 				
-				_log.info("Olympiad System: Next weekly change is in " + Math.round(milliToEnd / 60000) + " minutes");
+				_log.info("Olympiad System: Next weekly change is in " + (milliToEnd / 60000) + " minutes");
 			}
 		}
 		
@@ -317,7 +350,7 @@ public class Olympiad
 	
 	public void loadNoblesRank()
 	{
-		_noblesRank = new TIntIntHashMap();
+		_noblesRank.clear();
 		TIntIntHashMap tmpPlace = new TIntIntHashMap();
 		
 		Connection con = null;
@@ -460,12 +493,6 @@ public class Olympiad
 		return _nobles.get(playerId);
 	}
 	
-	protected static synchronized void updateNobleStats(int playerId, StatsSet stats)
-	{
-		_nobles.remove(playerId);
-		_nobles.put(playerId, stats);
-	}
-	
 	private void updateCompStatus()
 	{
 		// _compStarted = false;
@@ -693,14 +720,12 @@ public class Olympiad
 			return;
 		}
 		
-		for (Entry<Integer, StatsSet> entry : _nobles.entrySet())
+		int currentPoints;
+		for (StatsSet nobleInfo : _nobles.values())
 		{
-			final StatsSet nobleInfo = entry.getValue();
-			int currentPoints = nobleInfo.getInteger(POINTS);
+			currentPoints = nobleInfo.getInteger(POINTS);
 			currentPoints += WEEKLY_POINTS;
 			nobleInfo.set(POINTS, currentPoints);
-			
-			updateNobleStats(entry.getKey(), nobleInfo);
 		}
 	}
 	
@@ -714,15 +739,12 @@ public class Olympiad
 			return;
 		}
 		
-		for (Entry<Integer, StatsSet> entry : _nobles.entrySet())
+		for (StatsSet nobleInfo : _nobles.values())
 		{
-			StatsSet nobleInfo = entry.getValue();
 			nobleInfo.set(COMP_DONE_WEEK, 0);
 			nobleInfo.set(COMP_DONE_WEEK_CLASSED, 0);
 			nobleInfo.set(COMP_DONE_WEEK_NON_CLASSED, 0);
 			nobleInfo.set(COMP_DONE_WEEK_TEAM, 0);
-			
-			updateNobleStats(entry.getKey(), nobleInfo);
 		}
 	}
 	
@@ -789,8 +811,6 @@ public class Olympiad
 					statement.setInt(11, compDoneWeekTeam);
 					
 					nobleInfo.set("to_save", false);
-					
-					updateNobleStats(charId, nobleInfo);
 				}
 				else
 				{
@@ -909,10 +929,10 @@ public class Olympiad
 		if (_nobles != null)
 		{
 			_logResults.info("Noble,charid,classid,compDone,points");
-			
+			StatsSet nobleInfo;
 			for (Entry<Integer, StatsSet> entry : _nobles.entrySet())
 			{
-				final StatsSet nobleInfo = entry.getValue();
+				nobleInfo = entry.getValue();
 				if (nobleInfo == null)
 				{
 					continue;
@@ -927,7 +947,10 @@ public class Olympiad
 				record = new LogRecord(Level.INFO, charName);
 				record.setParameters(new Object[]
 				{
-					charId, classId, compDone, points
+					charId,
+					classId,
+					compDone,
+					points
 				});
 				_logResults.log(record);
 			}
@@ -967,7 +990,8 @@ public class Olympiad
 						record = new LogRecord(Level.INFO, "Hero " + hero.getString(CHAR_NAME));
 						record.setParameters(new Object[]
 						{
-							hero.getInteger(CHAR_ID), hero.getInteger(CLASS_ID)
+							hero.getInteger(CHAR_ID),
+							hero.getInteger(CLASS_ID)
 						});
 						_logResults.log(record);
 						_heroesToBe.add(hero);
@@ -994,7 +1018,8 @@ public class Olympiad
 					record = new LogRecord(Level.INFO, "Hero " + hero.getString(CHAR_NAME));
 					record.setParameters(new Object[]
 					{
-						hero.getInteger(CHAR_ID), hero.getInteger(CLASS_ID)
+						hero.getInteger(CHAR_ID),
+						hero.getInteger(CLASS_ID)
 					});
 					_logResults.log(record);
 					_heroesToBe.add(hero);
@@ -1051,7 +1076,8 @@ public class Olympiad
 					record = new LogRecord(Level.INFO, "Hero " + hero.getString(CHAR_NAME));
 					record.setParameters(new Object[]
 					{
-						hero.getInteger(CHAR_ID), hero.getInteger(CLASS_ID)
+						hero.getInteger(CHAR_ID),
+						hero.getInteger(CLASS_ID)
 					});
 					_logResults.log(record);
 					_heroesToBe.add(hero);
@@ -1162,7 +1188,6 @@ public class Olympiad
 		if (clear)
 		{
 			noble.set(POINTS, 0);
-			updateNobleStats(objId, noble);
 		}
 		points *= Config.ALT_OLY_GP_PER_POINT;
 		return points;
@@ -1361,6 +1386,16 @@ public class Olympiad
 		_nobles.clear();
 	}
 	
+	/**
+	 * @param charId the noble object Id.
+	 * @param data the stats set data to add.
+	 * @return the old stats set if the noble is already present, null otherwise.
+	 */
+	protected static StatsSet addNobleStats(int charId, StatsSet data)
+	{
+		return _nobles.put(Integer.valueOf(charId), data);
+	}
+	
 	@SuppressWarnings("synthetic-access")
 	private static class SingletonHolder
 	{

+ 0 - 9
L2J_Server_BETA/java/com/l2jserver/gameserver/model/olympiad/OlympiadGameNormal.java

@@ -35,7 +35,6 @@ import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
 import com.l2jserver.util.Rnd;
 
 /**
- * 
  * @author GodKratos, Pere, DS
  */
 abstract public class OlympiadGameNormal extends AbstractOlympiadGame
@@ -310,7 +309,6 @@ abstract public class OlympiadGameNormal extends AbstractOlympiadGame
 					{
 						points = Math.min(playerOnePoints / 3, Config.ALT_OLY_MAX_POINTS);
 						removePointsFromParticipant(_playerOne, points);
-						_playerOne.updateNobleStats();
 						
 						if (Config.ALT_OLY_LOG_FIGHTS)
 						{
@@ -330,7 +328,6 @@ abstract public class OlympiadGameNormal extends AbstractOlympiadGame
 					{
 						points = Math.min(playerTwoPoints / 3, Config.ALT_OLY_MAX_POINTS);
 						removePointsFromParticipant(_playerTwo, points);
-						_playerTwo.updateNobleStats();
 						
 						if (Config.ALT_OLY_LOG_FIGHTS)
 						{
@@ -424,9 +421,6 @@ abstract public class OlympiadGameNormal extends AbstractOlympiadGame
 				_playerTwo.updateStat(COMP_DONE_WEEK, 1);
 				_playerOne.updateStat(getWeeklyMatchType(), 1);
 				_playerTwo.updateStat(getWeeklyMatchType(), 1);
-				
-				_playerOne.updateNobleStats();
-				_playerTwo.updateNobleStats();
 				return;
 			}
 			catch (Exception e)
@@ -532,9 +526,6 @@ abstract public class OlympiadGameNormal extends AbstractOlympiadGame
 			_playerOne.updateStat(getWeeklyMatchType(), 1);
 			_playerTwo.updateStat(getWeeklyMatchType(), 1);
 			
-			_playerOne.updateNobleStats();
-			_playerTwo.updateNobleStats();
-			
 			if (Config.ALT_OLY_LOG_FIGHTS)
 			{
 				LogRecord record = new LogRecord(Level.INFO, winner);

+ 0 - 7
L2J_Server_BETA/java/com/l2jserver/gameserver/model/olympiad/OlympiadGameTeams.java

@@ -32,7 +32,6 @@ import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
 import com.l2jserver.util.Rnd;
 
 /**
- * 
  * @author Pere, DS
  */
 public class OlympiadGameTeams extends AbstractOlympiadGame
@@ -505,7 +504,6 @@ public class OlympiadGameTeams extends AbstractOlympiadGame
 					{
 						par = _teamOne[i];
 						removePointsFromParticipant(par, Math.min(par.stats.getInteger(POINTS) / 3, Config.ALT_OLY_MAX_POINTS));
-						par.updateNobleStats();
 					}
 				}
 				if (_teamTwoDefaulted)
@@ -514,7 +512,6 @@ public class OlympiadGameTeams extends AbstractOlympiadGame
 					{
 						par = _teamTwo[i];
 						removePointsFromParticipant(par, Math.min(par.stats.getInteger(POINTS) / 3, Config.ALT_OLY_MAX_POINTS));
-						par.updateNobleStats();
 					}
 				}
 			}
@@ -684,7 +681,6 @@ public class OlympiadGameTeams extends AbstractOlympiadGame
 					par.updateStat(COMP_DONE, 1);
 					par.updateStat(COMP_DONE_WEEK, 1);
 					par.updateStat(getWeeklyMatchType(), 1);
-					par.updateNobleStats();
 				}
 
 				for (int i = _teamTwoSize; --i >= 0;)
@@ -693,7 +689,6 @@ public class OlympiadGameTeams extends AbstractOlympiadGame
 					par.updateStat(COMP_DONE, 1);
 					par.updateStat(COMP_DONE_WEEK, 1);
 					par.updateStat(getWeeklyMatchType(), 1);
-					par.updateNobleStats();
 				}
 			}
 			catch (Exception e)
@@ -816,7 +811,6 @@ public class OlympiadGameTeams extends AbstractOlympiadGame
 				par.updateStat(COMP_DONE, 1);
 				par.updateStat(COMP_DONE_WEEK, 1);
 				par.updateStat(getWeeklyMatchType(), 1);
-				par.updateNobleStats();
 			}
 
 			for (int i = _teamTwoSize; --i >= 0;)
@@ -825,7 +819,6 @@ public class OlympiadGameTeams extends AbstractOlympiadGame
 				par.updateStat(COMP_DONE, 1);
 				par.updateStat(COMP_DONE_WEEK, 1);
 				par.updateStat(getWeeklyMatchType(), 1);
-				par.updateNobleStats();
 			}
 		}
 		catch (Exception e)

+ 16 - 14
L2J_Server_BETA/java/com/l2jserver/gameserver/model/olympiad/OlympiadManager.java

@@ -38,9 +38,9 @@ import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  */
 public class OlympiadManager
 {
-	private List<Integer> _nonClassBasedRegisters;
-	private Map<Integer, List<Integer>> _classBasedRegisters;
-	private List<List<Integer>> _teamsBasedRegisters;
+	private final List<Integer> _nonClassBasedRegisters;
+	private final Map<Integer, List<Integer>> _classBasedRegisters;
+	private final List<List<Integer>> _teamsBasedRegisters;
 
 	private OlympiadManager()
 	{
@@ -218,7 +218,8 @@ public class OlympiadManager
 			return false;
 		}
 		
-		if (Olympiad.getInstance().getRemainingWeeklyMatches(player.getObjectId()) < 1)
+		final int charId = player.getObjectId();
+		if (Olympiad.getInstance().getRemainingWeeklyMatches(charId) < 1)
 		{
 			player.sendPacket(SystemMessageId.MAX_OLY_WEEKLY_MATCHES_REACHED);
 			return false;
@@ -231,7 +232,7 @@ public class OlympiadManager
 				if (!checkNoble(player, player))
 					return false;
 				
-				if (Olympiad.getInstance().getRemainingWeeklyMatchesClassed(player.getObjectId()) < 1)
+				if (Olympiad.getInstance().getRemainingWeeklyMatchesClassed(charId) < 1)
 				{
 					player.sendPacket(SystemMessageId.MAX_OLY_WEEKLY_MATCHES_REACHED_60_NON_CLASSED_30_CLASSED_10_TEAM);
 					return false;
@@ -239,11 +240,11 @@ public class OlympiadManager
 
 				List<Integer> classed = _classBasedRegisters.get(player.getBaseClass());
 				if (classed != null)
-					classed.add(player.getObjectId());
+					classed.add(charId);
 				else
 				{
 					classed = new FastList<Integer>().shared();
-					classed.add(player.getObjectId());
+					classed.add(charId);
 					_classBasedRegisters.put(player.getBaseClass(), classed);
 				}
 				
@@ -255,13 +256,13 @@ public class OlympiadManager
 				if (!checkNoble(player, player))
 					return false;
 				
-				if (Olympiad.getInstance().getRemainingWeeklyMatchesNonClassed(player.getObjectId()) < 1)
+				if (Olympiad.getInstance().getRemainingWeeklyMatchesNonClassed(charId) < 1)
 				{
 					player.sendPacket(SystemMessageId.MAX_OLY_WEEKLY_MATCHES_REACHED_60_NON_CLASSED_30_CLASSED_10_TEAM);
 					return false;
 				}
 
-				_nonClassBasedRegisters.add(player.getObjectId());
+				_nonClassBasedRegisters.add(charId);
 				player.sendPacket(SystemMessageId.YOU_HAVE_BEEN_REGISTERED_IN_A_WAITING_LIST_OF_NO_CLASS_GAMES);
 				break;
 			}
@@ -454,8 +455,9 @@ public class OlympiadManager
 			player.sendPacket(sm);
 			return false;
 		}
-
-		if (TvTEvent.isPlayerParticipant(noble.getObjectId()))
+		
+		final int charId = noble.getObjectId();
+		if (TvTEvent.isPlayerParticipant(charId))
 		{
 			player.sendMessage("You can't join olympiad while participating on TvT Event.");
 			return false;
@@ -467,7 +469,7 @@ public class OlympiadManager
 		if (isInCompetition(noble, player, true))
 			return false;
 
-		StatsSet statDat = Olympiad.getNobleStats(noble.getObjectId());
+		StatsSet statDat = Olympiad.getNobleStats(charId);
 		if (statDat == null)
 		{
 			statDat = new StatsSet();
@@ -483,10 +485,10 @@ public class OlympiadManager
 			statDat.set(Olympiad.COMP_DONE_WEEK_NON_CLASSED, 0);
 			statDat.set(Olympiad.COMP_DONE_WEEK_TEAM, 0);
 			statDat.set("to_save", true);
-			Olympiad.updateNobleStats(noble.getObjectId(), statDat);
+			Olympiad.addNobleStats(charId, statDat);
 		}
 
-		final int points = Olympiad.getInstance().getNoblePoints(noble.getObjectId());
+		final int points = Olympiad.getInstance().getNoblePoints(charId);
 		if (points <= 0)
 		{
 			NpcHtmlMessage message = new NpcHtmlMessage(0);

+ 0 - 7
L2J_Server_BETA/java/com/l2jserver/gameserver/model/olympiad/Participant.java

@@ -19,9 +19,7 @@ import com.l2jserver.gameserver.model.StatsSet;
 import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
 
 /**
- * 
  * @author DS
- *
  */
 public final class Participant
 {
@@ -64,9 +62,4 @@ public final class Participant
 	{
 		stats.set(statName, Math.max(stats.getInteger(statName) + increment, 0));
 	}
-
-	public final void updateNobleStats()
-	{
-		Olympiad.updateNobleStats(objectId, stats);
-	}
 }