#include "base64.h"
#include <iostream>
#include <vector>
#include "zlib.h"
#include "Logic.h"
#include <fstream>
using namespace std;
static const std::string base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
Cbase::Cbase(int ID)
{
m_nID = ID;
Init();
}
Cbase::~Cbase()
{
}
void Cbase::Init()
{
n6OldPlay = 0;
nOldPlay1_1 = 0;
OnlinePlayCount = 0;
n6DayCreatePlay = 0;
nOnlineTimeZero = 0;
nOnlineTimeZeroOld = 0;
nDayTimeOnline = 0;
m_nLongCount = 0;
m_nLongEggCount = 0;
m_nLordPetCount = 0;
m_nCount = 0;
for(int i=0;i<=11;i++)
{
PetLVClassification.push_back(0);
vectnNewhandCount.push_back(0);
}
for (int i = 0; i < 1000; i++)
{
vectTaskCount.push_back(0);
vectPetCountPeole.push_back(0);
vectRoleOnline.push_back(0);
vectRoleOnlineHour.push_back(0);
PetLVClassificationToday.push_back(0);
PetLVClassificationOld.push_back(0);
vectPetCountPeoleToday.push_back(0);
vectPetCountPeoleOld.push_back(0);
vectRoleOnlineOld.push_back(0);
vectRoleOnlineHourOld.push_back(0);
vectTrain.push_back(0);
vectorDay.push_back(0);
}
}
std::string Cbase::base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) {
std::string ret;
int i = 0;
int j = 0;
unsigned char char_array_3[3];
unsigned char char_array_4[4];
while (in_len--) {
char_array_3[i++] = *(bytes_to_encode++);
if (i == 3) {
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;
for(i = 0; (i <4) ; i++)
ret += base64_chars[char_array_4[i]];
i = 0;
}
}
if (i)
{
for(j = i; j < 3; j++)
char_array_3[j] = '\0';
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;
for (j = 0; (j < i + 1); j++)
ret += base64_chars[char_array_4[j]];
while((i++ < 3))
ret += '=';
}
return ret;
}
std::string Cbase::base64_decode(std::string const& encoded_string) {
int in_len = encoded_string.size();
int i = 0;
int j = 0;
int in_ = 0;
unsigned char char_array_4[4], char_array_3[3];
std::string ret;
while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
char_array_4[i++] = encoded_string[in_]; in_++;
if (i ==4) {
for (i = 0; i <4; i++)
char_array_4[i] = base64_chars.find(char_array_4[i]);
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (i = 0; (i < 3); i++)
ret += char_array_3[i];
i = 0;
}
}
if (i) {
for (j = i; j <4; j++)
char_array_4[j] = 0;
for (j = 0; j <4; j++)
char_array_4[j] = base64_chars.find(char_array_4[j]);
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
}
return ret;
}
void Cbase::StringSplit(std::vector<std::string> &output, const std::string &strInput, const std::string &strSeparate, bool bTrim)
{
int nEnd = 0;
int nBegin = 0;
int nSeparateLen = strSeparate.length();
while (nEnd != -1)
{
nEnd = strInput.find(strSeparate, nBegin);
std::string substr;
if (nEnd == -1)
substr = strInput.substr(nBegin, strInput.length() - nBegin);
else
substr = strInput.substr(nBegin, nEnd - nBegin);
nBegin = nEnd + nSeparateLen;
if(bTrim && substr.empty())
continue;
output.push_back(substr);
}
}
bool Cbase::BaseLoadCompress(string filename,char *pValue, int nVersion, CRole *pRole,int BlobIndex)
{
if (NULL == pValue)
return false;
std::vector<std::string> vectTmpRecords; //所有信息
string strInput = pValue;
string strOut;
strOut = base64_decode(strInput);
char uncom_buf[102400] ={ 0 };
int uncom_len = sizeof(uncom_buf);
int ir = uncompress((Bytef *) uncom_buf, (uLongf*) &uncom_len,(const Bytef *) (strOut.c_str()), strOut.size());
if (ir == Z_OK)
{
StringSplit(vectTmpRecords, uncom_buf, FIELD_SPLIT);
if (vectTmpRecords.size() <= 0)
return false;
string strVersion = vectTmpRecords.front();
std::vector<std::string>::iterator it = vectTmpRecords.begin();
vectTmpRecords.erase(it);
}
std::vector<int> vectAttribute; //详细每一个的信息
int nAttributeCount = 0;
int nDbAttributeCount = 0;
int nCount = 0;
int nSize = vectTmpRecords.size();
for (int i = 0; i < nSize; i++)
{
if (i == 0)
{
nCount = String2int(vectTmpRecords[i]);//取出的数量
}
else if (i == 1)
{
int nAttrCount = String2int(vectTmpRecords[i]);//取出属性数量
nDbAttributeCount = nAttrCount;
int nAllSize = int(nDbAttributeCount * nCount) + 2;
if (nSize != nAllSize)
{
// break;
}
}
else
{
GetAttribute(vectAttribute, vectTmpRecords[i]);
nAttributeCount++;
}
if (nDbAttributeCount > 0 && nAttributeCount == nDbAttributeCount)
{
//break;
if (vectAttribute.size() <= 0)
{
vectAttribute.clear();
nAttributeCount = 0;
continue;
}
int nID = vectAttribute[0];
if (nID <= 0)
{
vectAttribute.clear();
nAttributeCount = 0;
continue;
}
if ( BlobIndex==5)
{
int nTrainType=vectAttribute[0];
int nMaxLayer=vectAttribute[2];
if(nTrainType==2)
vectTrain[nMaxLayer]++;
}
else if ( BlobIndex==0)
{
int nBossID= vectAttribute[0];
if(MapBoss.find(nBossID)==MapBoss.end())
MapBoss[nBossID]=1;
else
MapBoss.find(nBossID)->second++;
}
else if ( BlobIndex==1)
{
int nPetLv = vectAttribute[3];
int nSpeciesID = vectAttribute[2];
if (pRole->m_nRolePetMaxLv < nPetLv)
pRole->m_nRolePetMaxLv = nPetLv;
if(nSpeciesID == 2082 || nSpeciesID == 2083)
pRole->m_nHaveLong=true;
else if(nSpeciesID == 2081)
pRole->m_nLongEgg=true;
else if(1004<=nSpeciesID && nSpeciesID <=1006)
pRole->m_nLordPet1=true;
else if(1013<=nSpeciesID && nSpeciesID <=1015)
pRole->m_nLordPet2=true;
else if(1007<=nSpeciesID && nSpeciesID <=1009)
pRole->m_nLordPet3=true;
}
else if ( BlobIndex==3 || BlobIndex==4)
{
if (BlobIndex==3)
{
if (vectAttribute.size() < 3)
continue;
pRole->m_nTaskState = vectAttribute[3];
pRole->m_nTaskID = vectAttribute[2];
if(pRole->m_nTaskState==2)
{
int n=vectAttribute[5];
int n2=vectAttribute[6];
if(n!=0 || n2!=0)
{
if (MapCurDialogID.find(vectAttribute[5])== MapCurDialogID.end())
MapCurDialogID[vectAttribute[5]] = 1;
else
MapCurDialogID.find(vectAttribute[5])->second++;
if (MapCompleteDlgID.find(vectAttribute[6])== MapCompleteDlgID.end())
MapCompleteDlgID[vectAttribute[6]] = 1;
else
MapCompleteDlgID.find(vectAttribute[6])->second++;
}
}
if(pRole->m_nTaskState!=4)
pRole->m_nTaskID=0;
}
else if (BlobIndex==4)
{
if (vectAttribute.size() < 1)
continue;
pRole->m_nTaskState = vectAttribute[1];
pRole->m_nTaskID = vectAttribute[0];
}
if(0==pRole->m_nTaskID%10)
pRole->m_nTaskID=0;
if(vectTaskID.size()==0)
{
if(0 < pRole->m_nTaskID)
vectTaskI