Browse Source

Fix V1 for issue 398

Devn00b 2 years ago
parent
commit
de5b160336

+ 18 - 0
EQ2/source/WorldServer/WorldDatabase.cpp

@@ -7934,4 +7934,22 @@ void WorldDatabase::LoadCharacterSpellEffects(int32 char_id, Client* client, int
 
 	}
 
+}
+
+//devn00b: We need to handle non-found factions so the subtraction/addition works on 1st kill. Need to find a better way to handle this, but for now..
+bool WorldDatabase::VerifyFactionID(int32 char_id, int32 faction_id) {
+	DatabaseResult result;
+	database_new.Select(&result, "SELECT COUNT(id) as faction_exists from character_factions where faction_id=%u and char_id=%u", faction_id, char_id);
+
+	if (result.Next() && result.GetInt32Str("faction_exists") == 0) 
+		return false;
+	
+	return true;
+}
+//devn00b: handle adding default faction value to the db when a player discovers it
+void WorldDatabase::AddDefaultFaction(int32 char_id, int32 faction_id, sint32 faction_value) {
+	Query query;
+	//TODO: there is probably a better way to do this rather than writing to the db. I'll figure it out at some point.
+	query.RunQuery2(Q_INSERT, "insert into character_factions (char_id, faction_id, faction_level ) values (%u,%u,%i)",char_id, faction_id, faction_value);
+    //query.RunQuery2(Q_INSERT, "update character_factions set faction_level = %i where char_id=%u and faction_id=%u", faction_value, char_id, faction_id);
 }

+ 2 - 0
EQ2/source/WorldServer/WorldDatabase.h

@@ -222,6 +222,8 @@ public:
 	void	LoadFactionList();
 	bool	LoadPlayerFactions(Client* client);
 	void	SavePlayerFactions(Client* client);
+	bool    VerifyFactionID(int32 char_id, int32 faction_id);
+	void    AddDefaultFaction(int32 char_id, int32 faction_id, sint32 faction_value);
 	void	LoadSpawnScriptData();
 	void	LoadZoneScriptData();
 	int32	LoadSpellScriptData();

+ 21 - 0
EQ2/source/WorldServer/zoneserver.cpp

@@ -4293,6 +4293,17 @@ void ZoneServer::ProcessFaction(Spawn* spawn, Client* client)
 
 		if(player->GetFactions()->ShouldDecrease(spawn->GetFactionID()))
 		{
+			//make sure the player has discovered the faction before we do anything with it, otherwise add it.
+			bool hasfaction = database.VerifyFactionID(player->GetCharacterID(),spawn->GetFactionID());
+			if(hasfaction == 0) {
+				//they do not have the faction. Lets get the default value and feed it in.
+				sint32 defaultfaction = master_faction_list.GetDefaultFactionValue(spawn->GetFactionID());
+				//add the default faction for the player.
+				database.AddDefaultFaction(player->GetCharacterID(), spawn->GetFactionID(), defaultfaction);
+				//load the clients factions. without this it gets reset to -100 on character save.
+				database.LoadPlayerFactions(client);
+			}
+
 			update_result = player->GetFactions()->DecreaseFaction(spawn->GetFactionID());
 			faction = master_faction_list.GetFaction(spawn->GetFactionID());
 
@@ -4333,6 +4344,16 @@ void ZoneServer::ProcessFaction(Spawn* spawn, Client* client)
 			{
 				if(player->GetFactions()->ShouldDecrease(*itr))
 				{
+					bool hasfaction = database.VerifyFactionID(player->GetCharacterID(),spawn->GetFactionID());
+					if(hasfaction == 0) {
+						//they do not have the faction. Lets get the default value and feed it in.
+						sint32 defaultfaction = master_faction_list.GetDefaultFactionValue(spawn->GetFactionID());
+						//add the default faction for the player.
+						database.AddDefaultFaction(player->GetCharacterID(), spawn->GetFactionID(), defaultfaction);
+						//load the clients factions. without this it gets reset to -100 on character save.
+						database.LoadPlayerFactions(client);
+					}
+
 					update_result = player->GetFactions()->DecreaseFaction(*itr);
 					faction = master_faction_list.GetFaction(*itr);