Worked these changes into the client & server (read more)

The changes were fairly easy, and I was actually able to find and fix an
out-of-sequence bug: The destructors for the AccountManager and the
CharacterManager were using SQL code after the call to SQLite3_close_v2(),
so I added and called UnloadAll() for both of them.
This commit is contained in:
Kayne Ruse
2014-06-14 03:38:13 +10:00
parent 793737e1ed
commit b418ad713d
10 changed files with 51 additions and 36 deletions
+10 -7
View File
@@ -128,9 +128,9 @@ void InWorld::RenderFrame() {
} }
void InWorld::Render(SDL_Surface* const screen) { void InWorld::Render(SDL_Surface* const screen) {
//draw the map //draw the map0
for (auto it = regionPager.GetContainer()->begin(); it != regionPager.GetContainer()->end(); it++) { for (std::list<Region>::iterator it = regionPager.GetContainer()->begin(); it != regionPager.GetContainer()->end(); it++) {
tileSheet.DrawRegionTo(screen, *it, camera.x, camera.y); tileSheet.DrawRegionTo(screen, &(*it), camera.x, camera.y);
} }
//draw characters //draw characters
@@ -343,6 +343,9 @@ void InWorld::HandleRegionContent(RegionPacket* const argPacket) {
//replace existing regions //replace existing regions
regionPager.UnloadRegion(argPacket->x, argPacket->y); regionPager.UnloadRegion(argPacket->x, argPacket->y);
regionPager.PushRegion(argPacket->region); regionPager.PushRegion(argPacket->region);
//clean up after the serial code
delete argPacket->region;
argPacket->region = nullptr; argPacket->region = nullptr;
} }
@@ -431,13 +434,13 @@ void InWorld::UpdateMap() {
int yEnd = snapToBase(REGION_HEIGHT, (camera.y+camera.height)/tileSheet.GetTileH()) + REGION_HEIGHT; int yEnd = snapToBase(REGION_HEIGHT, (camera.y+camera.height)/tileSheet.GetTileH()) + REGION_HEIGHT;
//prune distant regions //prune distant regions
for (auto it = regionPager.GetContainer()->begin(); it != regionPager.GetContainer()->end(); /* EMPTY */) { for (std::list<Region>::iterator it = regionPager.GetContainer()->begin(); it != regionPager.GetContainer()->end(); /* EMPTY */) {
//check if the region is outside off this area //check if the region is outside off this area
if ((*it)->GetX() < xStart || (*it)->GetX() > xEnd || (*it)->GetY() < yStart || (*it)->GetY() > yEnd) { if (it->GetX() < xStart || it->GetX() > xEnd || it->GetY() < yStart || it->GetY() > yEnd) {
//clunky, but the alternative was time consuming //clunky, but the alternative was time consuming
int tmpX = (*it)->GetX(); int tmpX = it->GetX();
int tmpY = (*it)->GetY(); int tmpY = it->GetY();
++it; ++it;
regionPager.UnloadRegion(tmpX, tmpY); regionPager.UnloadRegion(tmpX, tmpY);
+2 -2
View File
@@ -23,7 +23,7 @@
#define INWORLD_HPP_ #define INWORLD_HPP_
//maps //maps
#include "region_pager.hpp" #include "region_pager_base.hpp"
//networking //networking
#include "udp_network_utility.hpp" #include "udp_network_utility.hpp"
@@ -110,7 +110,7 @@ protected:
TileSheet tileSheet; TileSheet tileSheet;
//map //map
RegionPager regionPager; RegionPagerBase regionPager;
//UI //UI
Button disconnectButton; Button disconnectButton;
+5
View File
@@ -54,6 +54,11 @@ Region* RegionPagerBase::FindRegion(int x, int y) {
return it != regionList.end() ? &(*it) : nullptr; return it != regionList.end() ? &(*it) : nullptr;
} }
Region* RegionPagerBase::PushRegion(Region* const ptr) {
regionList.push_front(*ptr);
return &regionList.front();
}
Region* RegionPagerBase::LoadRegion(int x, int y) { Region* RegionPagerBase::LoadRegion(int x, int y) {
//TODO: load the region if possible //TODO: load the region if possible
return nullptr; return nullptr;
+1
View File
@@ -38,6 +38,7 @@ public:
//region manipulation //region manipulation
virtual Region* GetRegion(int x, int y); virtual Region* GetRegion(int x, int y);
virtual Region* FindRegion(int x, int y); virtual Region* FindRegion(int x, int y);
virtual Region* PushRegion(Region* const);
virtual Region* LoadRegion(int x, int y); virtual Region* LoadRegion(int x, int y);
virtual Region* SaveRegion(int x, int y); virtual Region* SaveRegion(int x, int y);
+7 -10
View File
@@ -36,16 +36,6 @@ static const char* DELETE_USER_ACCOUNT = "DELETE FROM Accounts WHERE uid = ?;";
//Define the public methods //Define the public methods
//------------------------- //-------------------------
AccountManager::AccountManager() {
//
}
AccountManager::~AccountManager() {
for (auto& it : accountMap) {
SaveAccount(it.first);
}
}
int AccountManager::CreateAccount(std::string username, int clientIndex) { int AccountManager::CreateAccount(std::string username, int clientIndex) {
//create this user account, failing if it exists, leave this account in memory //create this user account, failing if it exists, leave this account in memory
sqlite3_stmt* statement = nullptr; sqlite3_stmt* statement = nullptr;
@@ -202,6 +192,13 @@ void AccountManager::DeleteAccount(int uid) {
accountMap.erase(uid); accountMap.erase(uid);
} }
void AccountManager::UnloadAll() {
for (auto& it : accountMap) {
SaveAccount(it.first);
}
accountMap.clear();
}
//------------------------- //-------------------------
//Define the accessors and mutators //Define the accessors and mutators
//------------------------- //-------------------------
+4 -2
View File
@@ -30,8 +30,8 @@
class AccountManager { class AccountManager {
public: public:
AccountManager(); AccountManager() = default;
~AccountManager(); ~AccountManager() { UnloadAll(); };
//public access methods //public access methods
int CreateAccount(std::string username, int clientIndex); int CreateAccount(std::string username, int clientIndex);
@@ -40,6 +40,8 @@ public:
void UnloadAccount(int uid); void UnloadAccount(int uid);
void DeleteAccount(int uid); void DeleteAccount(int uid);
void UnloadAll();
//accessors and mutators //accessors and mutators
AccountData* GetAccount(int uid); AccountData* GetAccount(int uid);
std::map<int, AccountData>* GetContainer(); std::map<int, AccountData>* GetContainer();
+7 -10
View File
@@ -58,16 +58,6 @@ static const char* DELETE_CHARACTER = "DELETE FROM Characters WHERE uid = ?;";
//Define the methods //Define the methods
//------------------------- //-------------------------
CharacterManager::CharacterManager() {
//
}
CharacterManager::~CharacterManager() {
for (auto& it : characterMap) {
SaveCharacter(it.first);
}
}
//TODO: default stats as a parameter? This would be good for differing beggining states or multiple classes //TODO: default stats as a parameter? This would be good for differing beggining states or multiple classes
int CharacterManager::CreateCharacter(int owner, std::string handle, std::string avatar) { int CharacterManager::CreateCharacter(int owner, std::string handle, std::string avatar) {
//Create the character, failing if it exists //Create the character, failing if it exists
@@ -294,6 +284,13 @@ void CharacterManager::UnloadCharacterIf(std::function<bool(std::map<int, Charac
} }
} }
void CharacterManager::UnloadAll() {
for (auto& it : characterMap) {
SaveCharacter(it.first);
}
characterMap.clear();
}
//------------------------- //-------------------------
//Define the accessors and mutators //Define the accessors and mutators
//------------------------- //-------------------------
+4 -2
View File
@@ -31,8 +31,8 @@
class CharacterManager { class CharacterManager {
public: public:
CharacterManager(); CharacterManager() = default;
~CharacterManager(); ~CharacterManager() { UnloadAll(); };
//public access methods //public access methods
int CreateCharacter(int owner, std::string handle, std::string avatar); int CreateCharacter(int owner, std::string handle, std::string avatar);
@@ -43,6 +43,8 @@ public:
void UnloadCharacterIf(std::function<bool(std::map<int, CharacterData>::iterator)> f); void UnloadCharacterIf(std::function<bool(std::map<int, CharacterData>::iterator)> f);
void UnloadAll();
//accessors and mutators //accessors and mutators
CharacterData* GetCharacter(int uid); CharacterData* GetCharacter(int uid);
std::map<int, CharacterData>* GetContainer(); std::map<int, CharacterData>* GetContainer();
+2 -2
View File
@@ -23,7 +23,7 @@
#define ROOMDATA_HPP_ #define ROOMDATA_HPP_
//map system //map system
#include "region_pager.hpp" #include "region_pager_lua.hpp"
struct RoomData { struct RoomData {
enum class RoomType { enum class RoomType {
@@ -35,7 +35,7 @@ struct RoomData {
}; };
//members //members
RegionPager pager; RegionPagerLua pager;
RoomType type; RoomType type;
//TODO: collision map //TODO: collision map
+8
View File
@@ -151,6 +151,14 @@ void ServerApplication::Proc() {
void ServerApplication::Quit() { void ServerApplication::Quit() {
std::cout << "Shutting down" << std::endl; std::cout << "Shutting down" << std::endl;
//close the managers
clientMap.clear();
accountMgr.UnloadAll();
characterMgr.UnloadAll();
//TODO: unload combats
//TODO: unload enemies
//TODO: unload rooms
//APIs //APIs
lua_close(luaState); lua_close(luaState);
sqlite3_close_v2(database); sqlite3_close_v2(database);