瀏覽代碼

Changing DBMap into STL::Map in C++ for guild storage (#2915)

inhyositsu 7 年之前
父節點
當前提交
fe575ddac8
共有 1 個文件被更改,包括 34 次插入50 次删除
  1. 34 50
      src/map/storage.cpp

+ 34 - 50
src/map/storage.cpp

@@ -3,11 +3,12 @@
 
 #include "storage.hpp"
 
+#include <map>
+
 #include <stdlib.h>
 #include <string.h>
 
 #include "../common/cbasetypes.h"
-#include "../common/db.h"
 #include "../common/nullpo.h"
 #include "../common/malloc.h"
 #include "../common/showmsg.h"
@@ -23,7 +24,9 @@
 #include "log.hpp"
 #include "battle.hpp"
 
-static DBMap* guild_storage_db; ///Databases of guild_storage : int guild_id -> struct guild_storage*
+///Databases of guild_storage : int guild_id -> struct guild_storage
+std::map<int, struct s_storage> guild_storage_db;
+
 struct s_storage_table *storage_db;
 int storage_count;
 
@@ -102,7 +105,6 @@ void storage_sortitem(struct item* items, unsigned int size)
  */
 void do_init_storage(void)
 {
-	guild_storage_db = idb_alloc(DB_OPT_RELEASE_DATA);
 	storage_db = NULL;
 	storage_count = 0;
 }
@@ -114,36 +116,26 @@ void do_init_storage(void)
  */
 void do_final_storage(void)
 {
-	guild_storage_db->destroy(guild_storage_db,NULL);
+	guild_storage_db.clear();
 	if (storage_db)
 		aFree(storage_db);
 	storage_db = NULL;
 	storage_count = 0;
 }
 
-/**
- * Parses storage and saves 'dirty' ones upon reconnect.
- * @author [Skotlex]
- * @see DBApply
- * @return 0
- */
-static int storage_reconnect_sub(DBKey key, DBData *data, va_list ap)
-{
-	struct s_storage *stor = static_cast<s_storage *>(db_data2ptr(data));
-
-	if (stor->dirty && stor->status == 0) //Save closed storages.
-		storage_guild_storagesave(0, stor->id, 0);
-
-	return 0;
-}
-
 /**
  * Function to be invoked upon server reconnection to char. To save all 'dirty' storages
  * @author [Skotlex]
  */
-void do_reconnect_storage(void)
-{
-	guild_storage_db->foreach(guild_storage_db, storage_reconnect_sub);
+void do_reconnect_storage(void){
+	for( auto entry : guild_storage_db ){
+		struct s_storage stor = entry.second;
+
+		// Save closed storages.
+		if( stor.dirty && stor.status == 0 ){
+			storage_guild_storagesave(0, stor.id, 0);
+		}
+	}
 }
 
 /**
@@ -532,24 +524,6 @@ void storage_storage_quit(struct map_session_data* sd, int flag)
 	storage_storagesave(sd);
 }
 
-/**
- * Create a guild_storage stucture and add it into the db
- * @see DBCreateData
- * @param key
- * @param args
- * @return 
- */
-static DBData create_guildstorage(DBKey key, va_list args)
-{
-	struct s_storage *gs = NULL;
-
-	gs = (struct s_storage *) aCalloc(sizeof(struct s_storage), 1);
-	gs->type = TABLE_GUILD_STORAGE;
-	gs->id = key.i;
-
-	return db_ptr2data(gs);
-}
-
 /**
  * Retrieve the guild_storage of a guild
  * will create a new storage if none found for the guild
@@ -558,10 +532,18 @@ static DBData create_guildstorage(DBKey key, va_list args)
  */
 struct s_storage *guild2storage(int guild_id)
 {
-	struct s_storage *gs = NULL;
+	struct s_storage *gs;
+
+	if (guild_search(guild_id) == nullptr)
+		return nullptr;
 
-	if (guild_search(guild_id) != NULL)
-		gs = (struct s_storage *)idb_ensure(guild_storage_db,guild_id,create_guildstorage);
+	gs = guild2storage2(guild_id);
+	
+	if( gs == nullptr ){
+		gs = &guild_storage_db[guild_id];
+		gs->id = guild_id;
+		gs->type = TABLE_GUILD_STORAGE;
+	}
 
 	return gs;
 }
@@ -570,21 +552,23 @@ struct s_storage *guild2storage(int guild_id)
  * See if the guild_storage exist in db and fetch it if it's the case
  * @author : [Skotlex]
  * @param guild_id : guild_id to search the storage
- * @return s_storage or NULL
+ * @return s_storage or nullptr
  */
-struct s_storage *guild2storage2(int guild_id)
-{
-	return (struct s_storage*)idb_get(guild_storage_db,guild_id);
+struct s_storage *guild2storage2(int guild_id){
+	if( guild_storage_db.find(guild_id) != guild_storage_db.end() ){
+		return &guild_storage_db[guild_id];
+	}else{
+		return nullptr;
+	}
 }
 
 /**
  * Delete a guild_storage and remove it from db
  * @param guild_id : guild to remove the storage from
- * @return 0
  */
 void storage_guild_delete(int guild_id)
 {
-	idb_remove(guild_storage_db,guild_id);
+	guild_storage_db.erase(guild_id);
 }
 
 /**