Change stamp storage to a list, insert new stamps at the begining, 'l' loads the first stamp or the previously used stamp, 'k' shows the stamp browser

This commit is contained in:
Simon Robertshaw 2012-06-10 19:52:24 +01:00
parent cdc4b4df86
commit fd572e9da6
6 changed files with 52 additions and 16 deletions

View File

@ -253,12 +253,12 @@ SaveFile * Client::GetStamp(string stampID)
void Client::DeleteStamp(string stampID) void Client::DeleteStamp(string stampID)
{ {
for(int i = 0; i < stampIDs.size(); i++) for (std::list<string>::iterator iterator = stampIDs.begin(), end = stampIDs.end(); iterator != end; ++iterator)
{ {
if(stampIDs[i] == stampID) if((*iterator) == stampID)
{ {
remove(string(STAMPS_DIR PATH_SEP + stampID + ".stm").c_str()); remove(string(STAMPS_DIR PATH_SEP + stampID + ".stm").c_str());
stampIDs.erase(stampIDs.begin()+i); stampIDs.erase(iterator);
return; return;
} }
} }
@ -294,7 +294,7 @@ string Client::AddStamp(GameSave * saveData)
stampStream.write((const char *)gameData, gameDataLength); stampStream.write((const char *)gameData, gameDataLength);
stampStream.close(); stampStream.close();
stampIDs.push_back(saveID.str()); stampIDs.push_front(saveID.str());
updateStamps(); updateStamps();
@ -312,18 +312,35 @@ void Client::updateStamps()
std::ofstream stampsStream; std::ofstream stampsStream;
stampsStream.open(string(STAMPS_DIR PATH_SEP "stamps.def").c_str(), ios::binary); stampsStream.open(string(STAMPS_DIR PATH_SEP "stamps.def").c_str(), ios::binary);
for(int i = 0; i < stampIDs.size(); i++) for (std::list<string>::const_iterator iterator = stampIDs.begin(), end = stampIDs.end(); iterator != end; ++iterator)
{ {
stampsStream.write(stampIDs[i].c_str(), 10); stampsStream.write((*iterator).c_str(), 10);
} }
stampsStream.write("\0", 1); stampsStream.write("\0", 1);
stampsStream.close(); stampsStream.close();
return; return;
} }
vector<string> Client::GetStamps() int Client::GetStampsCount()
{ {
return stampIDs; return stampIDs.size();
}
vector<string> Client::GetStamps(int start, int count)
{
//if(start+count > stampIDs.size()) {
// if(start > stampIDs.size())
// return vector<string>();
// count = stampIDs.size()-start;
//}
vector<string> stampRange;
int index = 0;
for (std::list<string>::const_iterator iterator = stampIDs.begin(), end = stampIDs.end(); iterator != end; ++iterator, ++index) {
if(index>=start && index < start+count)
stampRange.push_back(*iterator);
}
return stampRange;
} }
RequestStatus Client::ExecVote(int saveID, int direction) RequestStatus Client::ExecVote(int saveID, int direction)

View File

@ -3,6 +3,7 @@
#include <queue> #include <queue>
#include <vector> #include <vector>
#include <list>
#include <fstream> #include <fstream>
#include "Config.h" #include "Config.h"
@ -30,7 +31,7 @@ class Client: public Singleton<Client> {
private: private:
std::string lastError; std::string lastError;
vector<string> stampIDs; list<string> stampIDs;
int lastStampTime; int lastStampTime;
int lastStampName; int lastStampName;
@ -58,7 +59,9 @@ public:
SaveFile * GetStamp(string stampID); SaveFile * GetStamp(string stampID);
void DeleteStamp(string stampID); void DeleteStamp(string stampID);
string AddStamp(GameSave * saveData); string AddStamp(GameSave * saveData);
vector<string> GetStamps(); vector<string> GetStamps(int start, int count);
int GetStampsCount();
SaveFile * GetFirstStamp();
unsigned char * GetSaveData(int saveID, int saveDate, int & dataLength); unsigned char * GetSaveData(int saveID, int saveDate, int & dataLength);
LoginStatus Login(string username, string password, User & user); LoginStatus Login(string username, string password, User & user);

View File

@ -109,8 +109,6 @@ public:
cc->gameModel->SetStamp(cc->localBrowser->GetSave()->GetGameSave()); cc->gameModel->SetStamp(cc->localBrowser->GetSave()->GetGameSave());
cc->LoadStamp(); cc->LoadStamp();
} }
else
cc->gameModel->SetStamp(NULL);
} }
}; };

View File

@ -130,6 +130,15 @@ GameModel::GameModel():
{ {
currentUser = Client::Ref().GetAuthUser(); currentUser = Client::Ref().GetAuthUser();
} }
//Set stamp to first stamp in list
vector<string> stamps = Client::Ref().GetStamps(0, 1);
if(stamps.size()>0)
{
SaveFile * stampFile = Client::Ref().GetStamp(stamps[0]);
if(stampFile && stampFile->GetGameSave())
stamp = stampFile->GetGameSave();
}
} }
GameModel::~GameModel() GameModel::~GameModel()
@ -430,7 +439,10 @@ void GameModel::SetStamp(GameSave * save)
{ {
if(stamp) if(stamp)
delete stamp; delete stamp;
stamp = new GameSave(*save); if(save)
stamp = new GameSave(*save);
else
stamp = NULL;
} }
void GameModel::SetPlaceSave(GameSave * save) void GameModel::SetPlaceSave(GameSave * save)

View File

@ -741,6 +741,11 @@ void GameView::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool
} }
break; break;
case 'l': case 'l':
c->LoadStamp();
selectPoint2 = ui::Point(-1, -1);
selectPoint1 = selectPoint2;
break;
case 'k':
selectPoint2 = ui::Point(-1, -1); selectPoint2 = ui::Point(-1, -1);
selectPoint1 = selectPoint2; selectPoint1 = selectPoint2;
c->OpenStamps(); c->OpenStamps();

View File

@ -15,7 +15,8 @@ LocalBrowserModel::LocalBrowserModel():
currentPage(1) currentPage(1)
{ {
// TODO Auto-generated constructor stub // TODO Auto-generated constructor stub
stampIDs = Client::Ref().GetStamps(); //stampIDs = Client::Ref().GetStamps();
stampIDs = Client::Ref().GetStamps(0, 16);
} }
@ -72,9 +73,9 @@ void LocalBrowserModel::UpdateSavesList(int pageNumber)
delete tempStampsList[i]; delete tempStampsList[i];
}*/ }*/
int stampsEnd = pageNumber*20; stampIDs = Client::Ref().GetStamps((pageNumber-1)*20, 20);
for(int i = stampsEnd-20; i<stampIDs.size() && i<stampsEnd; i++) for(int i = 0; i<stampIDs.size(); i++)
{ {
SaveFile * tempSave = Client::Ref().GetStamp(stampIDs[i]); SaveFile * tempSave = Client::Ref().GetStamp(stampIDs[i]);
if(tempSave) if(tempSave)