Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

Help for database creaton

Initiate Mage
Joined
Oct 27, 2020
Messages
4
Reaction score
0
Hi,

Is it possible to create a database for an MMORPG from this?

#ifndef __DIESELSERVER_DB_H__
#define __DIESELSERVER_DB_H__

#include "stdafx.h"
#include <sql.h>
#include <sqlext.h>

#include <map>
#include <string>
#define MYSQLSUCCESS(rc) ((rc==SQL_SUCCESS)||(rc==SQL_SUCCESS_WITH_INFO))


class CDBConnection
{
private:
enum {
DBSTMT_NEWACCOUNT = 0,
DBSTMT_CHKACCOUNT,

DBSTMT_GETCHAR, // get character list

DBSTMT_NEWCHARSTAT, // create new char - stored proc returning charid
DBSTMT_NEWCHARWEAR,
DBSTMT_DELCHAR,

DBSTMT_LOADCHAR, // loading
DBSTMT_LOADWEAR,
DBSTMT_LOADEQ,
DBSTMT_LOADAVATAR,
DBSTMT_LOADUSE,
DBSTMT_LOADETC,
DBSTMT_LOADEGG,

DBSTMT_SAVECHAR, // save
DBSTMT_SAVEWEAR,
DBSTMT_SAVEEQ,
DBSTMT_SAVEAVATAR,
DBSTMT_SAVEUSE,
DBSTMT_SAVEETC,
DBSTMT_SAVEEGG,

DBSTMT_MAX, // max statements
};
RETCODE rc;
HENV hEnv;
HDBC hdbc;
HSTMT hstmt[DBSTMT_MAX];

std::map<int, std::wstring> m_mapQuery;

SQLINTEGER ntslen;
SQLINTEGER intlen;
SQLINTEGER biglen;
SQLINTEGER shortlen;

SQLINTEGER pwearlen;
SQLINTEGER psexlen;
SQLINTEGER pattrlen;

char account[31];
char apasswd[11];
int acid;

char wear[720];

int cid;
char cname[41];
int srl;
int lvl;
int max_hp,hp,max_sp,sp,max_mp,mp;
int mapid,spaceid,mapx,mapy;
__int64 exp,money;
char attr[32];
char sex;
short eqslot,useslot,etcslot,cashslot,eggslot;
int bonus,homeid,pclass,guild;

union itemdata
{
char p[24];
struct stdata
{
short sProto;
char m_szMod[CItem::ITEM_MODDATA_SIZE];
} data;
} uitem;
void _saveweardata(CPC *pPC)
{
CItem *p;
int nItem;
printf("SAVE:");
for(int i = 0 ; i< MAX_AVATAR_ITEMSLOT; i++) {
nItem = pPC->m_cAvatar.m_nItem;
if(nItem >= 0) {
p = GWorld->m_pItem[nItem];
uitem.data.sProto = p->m_sProtoID;
memcpy( uitem.data.m_szMod, p->m_arrayModData, CItem::ITEM_MODDATA_SIZE);
}
else {
uitem.data.sProto = -1;
memset(uitem.data.m_szMod, -1, CItem::ITEM_MODDATA_SIZE);
}
printf("%d ", uitem.data.sProto);
memcpy((void *)(wear+24*i), uitem.p, 24);
}
printf("\n");

}
void _loadweardata(CPC *pPC)
{
CAvatar *pAvatar = &pPC->m_cAvatar;
printf("LOAD:");

for(int i=0; i<MAX_AVATAR_ITEMSLOT; i++)
{
memcpy( uitem.p, (void *)(wear+24*i), 24);

if( uitem.data.sProto < 0)
{
pAvatar->m_nItem = -1;
}
else
{
int nItemID = GWorld->CreateItem(uitem.data.sProto);
if(nItemID >= 0)
{
memcpy(GWorld->m_pItem[nItemID]->m_arrayModData, uitem.data.m_szMod, CItem::ITEM_MODDATA_SIZE);
pAvatar->m_nItem = nItemID;

}
else pAvatar->m_nItem = -1;
}
printf("%d ", uitem.data.sProto);
}
printf("\n");
}


public:
CDBConnection()
{

hEnv = SQL_NULL_HENV;
hdbc = SQL_NULL_HDBC;
for(int i=0;i<DBSTMT_MAX;i++)
hstmt = SQL_NULL_HSTMT;

ntslen = SQL_NTS;
intlen = sizeof(int);
shortlen = sizeof(short);
biglen = sizeof(__int64);
pattrlen = sizeof(attr);
psexlen = sizeof(sex);
pwearlen = sizeof(wear);

m_mapQuery[DBSTMT_NEWACCOUNT] = std::wstring(L"INSERT INTO ACCOUNT (accountid, passwd) VALUES (?, ?)");
m_mapQuery[DBSTMT_CHKACCOUNT] = std::wstring(L"SELECT usrsrl,passwd FROM ACCOUNT WHERE accountid=?");

//m_mapQuery[DBSTMT_GETCHAR] = std::wstring(L"SELECT charid,charname FROM PLAYER WHERE usrsrl=?");
m_mapQuery[DBSTMT_GETCHAR] = std::wstring(L"SELECT charid FROM PLAYER WHERE charname=?");

// m_mapQuery[DBSTMT_NEWCHARSTAT] = std::wstring(L"{? = CALL CREATECHAR (?, ?, ?, \
// ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)}");
m_mapQuery[DBSTMT_NEWCHARSTAT] = std::wstring(L"{? = CALL CREATECHAR2 (?, ?, ?, \
?, ?, ?, ?, ?, ?, ?, ?, ?, ?)}");
m_mapQuery[DBSTMT_NEWCHARWEAR] = std::wstring(L"INSERT INTO WEAR (cid, item) VALUES (?, ?)");
m_mapQuery[DBSTMT_DELCHAR] = std::wstring(L"{CALL DELCHAR (?)}");

m_mapQuery[DBSTMT_LOADCHAR] = std::wstring(L"SELECT charname,usrsrl,lvl,hp,max_hp,mp,max_mp,sp,max_sp, \
mapid,spaceid,mapx,mapy,exp,money,attr,sex,eqslot,useslot,etcslot,cashslot,eggslot \
bonus,homeid,class,guild FROM PLAYER WHERE charid=?");
m_mapQuery[DBSTMT_LOADWEAR] = std::wstring(L"SELECT item FROM WEAR WHERE cid=?");
m_mapQuery[DBSTMT_LOADEQ] = std::wstring(L"SELECT item FROM EQINVEN WHERE cid=?");
m_mapQuery[DBSTMT_LOADAVATAR] = std::wstring(L"SELECT item FROM CASHINVEN WHERE cid=?");
m_mapQuery[DBSTMT_LOADUSE] = std::wstring(L"SELECT item FROM USEINVEN WHERE cid=?");
m_mapQuery[DBSTMT_LOADETC] = std::wstring(L"SELECT item FROM ETCINVEN WHERE cid=?");
m_mapQuery[DBSTMT_LOADEGG] = std::wstring(L"SELECT item FROM EGGINVEN WHERE cid=?");

m_mapQuery[DBSTMT_SAVECHAR] = std::wstring(L"UPDATE PLAYER SET \
lvl=?,hp=?,max_hp=?,mp=?,max_mp=?,sp=?,max_sp=?,mapid=?,spaceid=?, \
mapx=?,mapy=?,exp=?,money=?,attr=?,sex=?, \
eqslot=?, useslot=?, etcslot=?, cashslot=?, eggslot=?, \
bonus=?,homeid=?,class=?,guild=? WHERE charid=?");
m_mapQuery[DBSTMT_SAVEWEAR] = std::wstring(L"UPDATE WEAR SET item=? WHERE cid=?");
m_mapQuery[DBSTMT_SAVEEQ] = std::wstring(L"{CALL SAVEEQ (?)}");
m_mapQuery[DBSTMT_SAVEAVATAR] = std::wstring(L"{CALL SAVEAVATAR (?)}");
m_mapQuery[DBSTMT_SAVEUSE] = std::wstring(L"{CALL SAVEUSE (?)}");
m_mapQuery[DBSTMT_SAVEETC] = std::wstring(L"{CALL SAVEETC (?)}");
m_mapQuery[DBSTMT_SAVEEGG] = std::wstring(L"{CALL SAVEEGG (?)}");



}

bool Init(const char *pDSN)
{
SQLAllocEnv(&hEnv);
SQLAllocConnect(hEnv, &hdbc);

WCHAR wszDSNName[256];
MultiByteToWideChar( CP_ACP, 0, pDSN, 255 ,wszDSNName, 255);
rc = SQLConnect(hdbc, wszDSNName, SQL_NTS, NULL, 0, NULL, 0);
if(!MYSQLSUCCESS(rc))
{
SQLFreeEnv(hEnv);
SQLFreeConnect(hdbc);
return false;
}

for(int i=0;i<DBSTMT_MAX;i++)
{
rc = SQLAllocStmt(hdbc, &hstmt);
if(!MYSQLSUCCESS(rc)) goto fail;
}


// useful macro only use in this context.
#define MYPREPARE(i) do { rc = SQLPrepare(hstmt, (SQLWCHAR *)m_mapQuery.c_str(), SQL_NTS); \
if(!MYSQLSUCCESS(rc)) goto fail; } while(0)

#define BindInt(i, j, var) do { rc = SQLBindParameter(hstmt, j, SQL_PARAM_INPUT, \
SQL_C_LONG, SQL_INTEGER, 0, 0, &var, sizeof(var), &intlen); \
if(!MYSQLSUCCESS(rc)) goto fail; } while(0)
#define BindStr(i, j, var) do { rc = SQLBindParameter(hstmt, j, SQL_PARAM_INPUT, \
SQL_C_CHAR, SQL_VARCHAR, 0, 0, var, sizeof(var), &ntslen); \
if(!MYSQLSUCCESS(rc)) goto fail; } while(0)
#define BindBig(i, j, var) do { rc = SQLBindParameter(hstmt, j, SQL_PARAM_INPUT, \
SQL_C_SBIGINT, SQL_BIGINT, 0, 0, &var, sizeof(var), &biglen); \
if(!MYSQLSUCCESS(rc)) goto fail; } while(0)
#define BindShort(i, j, var) do { rc = SQLBindParameter(hstmt, j, SQL_PARAM_INPUT, \
SQL_C_SHORT, SQL_SMALLINT, 0, 0, &var, sizeof(var), &shortlen); \
if(!MYSQLSUCCESS(rc)) goto fail; } while(0)

MYPREPARE(DBSTMT_NEWACCOUNT);
BindStr(DBSTMT_NEWACCOUNT, 1, account);
BindStr(DBSTMT_NEWACCOUNT, 2, apasswd);

MYPREPARE(DBSTMT_CHKACCOUNT);
BindStr(DBSTMT_CHKACCOUNT, 1, account);

MYPREPARE(DBSTMT_GETCHAR);
BindStr(DBSTMT_GETCHAR, 1, cname);

MYPREPARE(DBSTMT_NEWCHARWEAR);
BindInt(DBSTMT_NEWCHARWEAR, 1, cid);
rc = SQLBindParameter(hstmt[DBSTMT_NEWCHARWEAR], 2, SQL_PARAM_INPUT,
SQL_C_BINARY, SQL_BINARY, 0, 0, wear, sizeof(wear), &pwearlen);
if(!MYSQLSUCCESS(rc)) goto fail;

MYPREPARE(DBSTMT_LOADCHAR);
BindInt(DBSTMT_LOADCHAR, 1, cid);

MYPREPARE(DBSTMT_LOADWEAR);
BindInt(DBSTMT_LOADWEAR, 1, cid);

MYPREPARE(DBSTMT_LOADEQ);
BindInt(DBSTMT_LOADEQ, 1, cid);

MYPREPARE(DBSTMT_LOADUSE);
BindInt(DBSTMT_LOADUSE, 1, cid);

MYPREPARE(DBSTMT_LOADETC);
BindInt(DBSTMT_LOADETC, 1, cid);

MYPREPARE(DBSTMT_LOADEGG);
BindInt(DBSTMT_LOADEGG, 1, cid);

MYPREPARE(DBSTMT_SAVECHAR);
BindInt(DBSTMT_SAVECHAR, 1, lvl);
BindInt(DBSTMT_SAVECHAR, 2, hp);
BindInt(DBSTMT_SAVECHAR, 3, max_hp);
BindInt(DBSTMT_SAVECHAR, 4, mp);
BindInt(DBSTMT_SAVECHAR, 5, max_mp);
BindInt(DBSTMT_SAVECHAR, 6, sp);
BindInt(DBSTMT_SAVECHAR, 7, max_sp);
BindInt(DBSTMT_SAVECHAR, 8, mapid);
BindInt(DBSTMT_SAVECHAR, 9, spaceid);
BindInt(DBSTMT_SAVECHAR, 10, mapx);
BindInt(DBSTMT_SAVECHAR, 11, mapy);
BindBig(DBSTMT_SAVECHAR, 12, exp);
BindBig(DBSTMT_SAVECHAR, 13, money);
rc = SQLBindParameter(hstmt[DBSTMT_SAVECHAR], 14, SQL_PARAM_INPUT,
SQL_C_BINARY, SQL_BINARY, 0, 0, attr, sizeof(attr), &pattrlen);
if(!MYSQLSUCCESS(rc)) goto fail;
rc = SQLBindParameter(hstmt[DBSTMT_SAVECHAR], 15, SQL_PARAM_INPUT,
SQL_C_CHAR, SQL_CHAR, 0, 0, &sex, sizeof(sex), &psexlen);
if(!MYSQLSUCCESS(rc)) goto fail;
BindShort(DBSTMT_SAVECHAR, 16, eqslot);
BindShort(DBSTMT_SAVECHAR, 17, useslot);
BindShort(DBSTMT_SAVECHAR, 18, etcslot);
BindShort(DBSTMT_SAVECHAR, 19, cashslot);
BindShort(DBSTMT_SAVECHAR, 20, eggslot);
BindInt(DBSTMT_SAVECHAR, 21, bonus);
BindInt(DBSTMT_SAVECHAR, 22, homeid);
BindInt(DBSTMT_SAVECHAR, 23, pclass);
BindInt(DBSTMT_SAVECHAR, 24, guild);
BindInt(DBSTMT_SAVECHAR, 25, cid);

MYPREPARE(DBSTMT_SAVEWEAR);
rc = SQLBindParameter(hstmt[DBSTMT_SAVEWEAR], 1, SQL_PARAM_INPUT,
SQL_C_BINARY, SQL_BINARY, 0, 0, wear, sizeof(wear), &pwearlen);
BindInt(DBSTMT_SAVEWEAR, 2, cid);
if(!MYSQLSUCCESS(rc)) goto fail;

return true;

fail:
return false;
}
void Fin()
{
for(int i=0; i < DBSTMT_MAX; i++)
if( hstmt != SQL_NULL_HSTMT)
{
SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
hstmt = SQL_NULL_HSTMT;
}
SQLDisconnect(hdbc);
if( hdbc != SQL_NULL_HDBC) {
SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
hdbc = SQL_NULL_HDBC;
}
if( hEnv != SQL_NULL_HENV) {
SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
hEnv = SQL_NULL_HENV;
}
}

bool CreateAccount( char *name, char *passwd)
{
strcpy(account, name);
strcpy(apasswd, passwd);

rc = SQLExecute(hstmt[DBSTMT_NEWACCOUNT]);
if(!MYSQLSUCCESS(rc)) return false;

SQLLEN row;
rc = SQLRowCount(hstmt[DBSTMT_NEWACCOUNT], &row);

if(!MYSQLSUCCESS(rc) || row!=1) return false;


}
bool LoginAccount( char *name, char *passwd)
{
strcpy(account, name);

int racid;
SQLINTEGER racidlen;
char rapasswd[11];
SQLINTEGER rapsslen;

rc = SQLExecute(hstmt[DBSTMT_CHKACCOUNT]);
if(!MYSQLSUCCESS(rc)) return false;
rc = SQLBindCol(hstmt[DBSTMT_CHKACCOUNT], 1, SQL_C_LONG, &racid, sizeof(racid), &racidlen);
if(!MYSQLSUCCESS(rc)) return false;
rc = SQLBindCol(hstmt[DBSTMT_CHKACCOUNT], 2, SQL_C_CHAR, &rapasswd, sizeof(rapasswd), &rapsslen);
if(!MYSQLSUCCESS(rc)) return false;

if(SQLFetch(hstmt[DBSTMT_CHKACCOUNT]) == SQL_SUCCESS)
{ // check passwd routine
SQLCloseCursor(hstmt[DBSTMT_CHKACCOUNT]);
return true;
}
SQLCloseCursor(hstmt[DBSTMT_CHKACCOUNT]);
return false;
}

int GetCharList(int usrl, std::map<int, std::string> &rmap)
{
srl = usrl;

rc = SQLExecute(hstmt[DBSTMT_GETCHAR]);
if(!MYSQLSUCCESS(rc)) return -1;

SQLINTEGER rcidlen, rcnamelen;

rc = SQLBindCol(hstmt[DBSTMT_GETCHAR], 1, SQL_C_LONG, &cid, sizeof(cid), &rcidlen);
if(!MYSQLSUCCESS(rc)) return -1;
rc = SQLBindCol(hstmt[DBSTMT_GETCHAR], 2, SQL_C_CHAR, &cname, sizeof(cname), &rcnamelen);
if(!MYSQLSUCCESS(rc)) return -1;

int nCount = 0;
while(SQLFetch(hstmt[DBSTMT_GETCHAR]) == SQL_SUCCESS)
{
cname[40] = 0x00;
rmap[cid] = std::string(cname);
nCount++;
}
SQLCloseCursor(hstmt[DBSTMT_GETCHAR]);
return nCount;
}

int GetCharID(const char *pName)
{
strcpy(cname, pName);

rc = SQLExecute(hstmt[DBSTMT_GETCHAR]);
if(!MYSQLSUCCESS(rc)) return -1;

SQLINTEGER rcidlen;

rc = SQLBindCol(hstmt[DBSTMT_GETCHAR], 1, SQL_C_LONG, &cid, sizeof(cid), &rcidlen);
if(!MYSQLSUCCESS(rc)) return -1;

if(SQLFetch(hstmt[DBSTMT_GETCHAR]) == SQL_SUCCESS)
{
SQLCloseCursor(hstmt[DBSTMT_GETCHAR]);
return cid;
}
SQLCloseCursor(hstmt[DBSTMT_GETCHAR]);
return -1;
}

int CreateChar(CPC *pPC)
{
// set-up data
short sSlots = 128;
srl = pPC->m_nUSRL;
strcpy(cname, pPC->m_szCharName);
hp = pPC->m_nHPMax;
mp = pPC->m_nMPMax;
sp = pPC->m_nSPMax;
money = pPC->m_nMoney;
memcpy(attr, pPC->m_arrayData, MOB_EXT_SIZE);
if(pPC->m_cAvatar.m_cSex == 0)
sex = '0';
else
sex = '1';
bonus = pPC->m_sBonus;
homeid = pPC->m_nHomeID;
pclass = pPC->m_cClass;
guild = 0;

int rcid;
SQLINTEGER rcidlen = sizeof(int);
rc = SQLBindParameter(hstmt[DBSTMT_NEWCHARSTAT], 1, SQL_PARAM_OUTPUT,
SQL_C_LONG, SQL_INTEGER, 0, 0, &rcid, sizeof(rcid), &rcidlen);
if(!MYSQLSUCCESS(rc)) goto fail;
BindInt(DBSTMT_NEWCHARSTAT, 2, srl);
BindStr(DBSTMT_NEWCHARSTAT, 3, cname);
BindInt(DBSTMT_NEWCHARSTAT, 4, hp);
BindInt(DBSTMT_NEWCHARSTAT, 5, mp);
BindInt(DBSTMT_NEWCHARSTAT, 6, sp);
BindBig(DBSTMT_NEWCHARSTAT, 7, money);
rc = SQLBindParameter(hstmt[DBSTMT_NEWCHARSTAT], 8, SQL_PARAM_INPUT,
SQL_C_BINARY, SQL_BINARY, 0, 0, attr, sizeof(attr), &pattrlen);
if(!MYSQLSUCCESS(rc)) goto fail;
rc = SQLBindParameter(hstmt[DBSTMT_NEWCHARSTAT], 9, SQL_PARAM_INPUT,
SQL_C_CHAR, SQL_CHAR, 0, 0, &sex, sizeof(sex), &psexlen);
if(!MYSQLSUCCESS(rc)) goto fail;
BindShort(DBSTMT_NEWCHARSTAT, 10, sSlots);
BindInt(DBSTMT_NEWCHARSTAT, 11, bonus);
BindInt(DBSTMT_NEWCHARSTAT, 12, homeid);
BindInt(DBSTMT_NEWCHARSTAT, 13, pclass);
BindInt(DBSTMT_NEWCHARSTAT, 14, guild);

rc = SQLExecDirect(hstmt[DBSTMT_NEWCHARSTAT], const_cast<wchar_t *>(m_mapQuery[DBSTMT_NEWCHARSTAT].c_str()), SQL_NTS);
if(!MYSQLSUCCESS(rc)) goto fail;

cid = rcid;
// prepare wear
_saveweardata(pPC);
SQLExecute(hstmt[DBSTMT_NEWCHARWEAR]);
return rcid;
fail:
return -1;
}

bool LoadChar(int tcid, CPC *pPC)
{
cid = tcid;
rc = SQLExecute(hstmt[DBSTMT_LOADCHAR]);
if(!MYSQLSUCCESS(rc)) return false;

SQLINTEGER rcnamelen,rusrlen,rlvllen;
SQLINTEGER rhplen,rmax_hplen,rsplen,rmax_splen,rmplen,rmax_mplen;
SQLINTEGER rmidlen,rspidlen,rmxlen,rmylen,rexplen,rmoneylen;
SQLINTEGER rmnylen,rattrlen,rsexlen;

rc = SQLBindCol(hstmt[DBSTMT_LOADCHAR], 1, SQL_C_CHAR, &cname, sizeof(cname), &rcnamelen);
if(!MYSQLSUCCESS(rc)) return false;
rc = SQLBindCol(hstmt[DBSTMT_LOADCHAR], 2, SQL_C_LONG, &srl, sizeof(srl), &rusrlen);
if(!MYSQLSUCCESS(rc)) return false;
rc = SQLBindCol(hstmt[DBSTMT_LOADCHAR], 3, SQL_C_LONG, &lvl, sizeof(lvl), &rlvllen);
if(!MYSQLSUCCESS(rc)) return false;
rc = SQLBindCol(hstmt[DBSTMT_LOADCHAR], 4, SQL_C_LONG, &hp, sizeof(hp), &rhplen);
if(!MYSQLSUCCESS(rc)) return false;
rc = SQLBindCol(hstmt[DBSTMT_LOADCHAR], 5, SQL_C_LONG, &max_hp, sizeof(max_hp), &rmax_hplen);
if(!MYSQLSUCCESS(rc)) return false;
rc = SQLBindCol(hstmt[DBSTMT_LOADCHAR], 6, SQL_C_LONG, &mp, sizeof(mp), &rmplen);
if(!MYSQLSUCCESS(rc)) return false;
rc = SQLBindCol(hstmt[DBSTMT_LOADCHAR], 7, SQL_C_LONG, &max_mp, sizeof(max_mp), &rmax_mplen);
if(!MYSQLSUCCESS(rc)) return false;
rc = SQLBindCol(hstmt[DBSTMT_LOADCHAR], 8, SQL_C_LONG, &sp, sizeof(sp), &rsplen);
if(!MYSQLSUCCESS(rc)) return false;
rc = SQLBindCol(hstmt[DBSTMT_LOADCHAR], 9, SQL_C_LONG, &max_sp, sizeof(max_sp), &rmax_splen);
if(!MYSQLSUCCESS(rc)) return false;
rc = SQLBindCol(hstmt[DBSTMT_LOADCHAR], 10, SQL_C_LONG, &mapid, sizeof(mapid), &rmidlen);
if(!MYSQLSUCCESS(rc)) return false;
rc = SQLBindCol(hstmt[DBSTMT_LOADCHAR], 11, SQL_C_LONG, &spaceid, sizeof(spaceid), &rspidlen);
if(!MYSQLSUCCESS(rc)) return false;
rc = SQLBindCol(hstmt[DBSTMT_LOADCHAR], 12, SQL_C_LONG, &mapx, sizeof(mapx), &rmxlen);
if(!MYSQLSUCCESS(rc)) return false;
rc = SQLBindCol(hstmt[DBSTMT_LOADCHAR], 13, SQL_C_LONG, &mapy, sizeof(mapy), &rmylen);
if(!MYSQLSUCCESS(rc)) return false;
rc = SQLBindCol(hstmt[DBSTMT_LOADCHAR], 14, SQL_C_SBIGINT, &exp, sizeof(exp), &rexplen);
if(!MYSQLSUCCESS(rc)) return false;
rc = SQLBindCol(hstmt[DBSTMT_LOADCHAR], 15, SQL_C_SBIGINT, &money, sizeof(money), &rmoneylen);
if(!MYSQLSUCCESS(rc)) return false;
rc = SQLBindCol(hstmt[DBSTMT_LOADCHAR], 16, SQL_C_BINARY, attr, sizeof(attr), &rattrlen);
if(!MYSQLSUCCESS(rc)) return false;
rc = SQLBindCol(hstmt[DBSTMT_LOADCHAR], 17, SQL_C_CHAR, &sex, sizeof(sex), &rsexlen);
if(!MYSQLSUCCESS(rc)) return false;

SQLINTEGER reqslotlen,ruseslotlen,retcslotlen,rcashslotlen,reggslotlen;
SQLINTEGER rbonuslen,rhomeidlen,rclasslen,rguildlen;

rc = SQLBindCol(hstmt[DBSTMT_LOADCHAR], 18, SQL_C_SHORT, &eqslot, sizeof(eqslot), &reqslotlen);
if(!MYSQLSUCCESS(rc)) return false;
rc = SQLBindCol(hstmt[DBSTMT_LOADCHAR], 19, SQL_C_SHORT, &useslot, sizeof(useslot), &ruseslotlen);
if(!MYSQLSUCCESS(rc)) return false;
rc = SQLBindCol(hstmt[DBSTMT_LOADCHAR], 20, SQL_C_SHORT, &etcslot, sizeof(etcslot), &retcslotlen);
if(!MYSQLSUCCESS(rc)) return false;
rc = SQLBindCol(hstmt[DBSTMT_LOADCHAR], 21, SQL_C_SHORT, &cashslot, sizeof(cashslot), &rcashslotlen);
if(!MYSQLSUCCESS(rc)) return false;
rc = SQLBindCol(hstmt[DBSTMT_LOADCHAR], 22, SQL_C_SHORT, &eggslot, sizeof(eggslot), &reggslotlen);
if(!MYSQLSUCCESS(rc)) return false;
rc = SQLBindCol(hstmt[DBSTMT_LOADCHAR], 23, SQL_C_LONG, &bonus, sizeof(bonus), &rbonuslen);
if(!MYSQLSUCCESS(rc)) return false;
rc = SQLBindCol(hstmt[DBSTMT_LOADCHAR], 24, SQL_C_LONG, &homeid, sizeof(homeid), &rhomeidlen);
if(!MYSQLSUCCESS(rc)) return false;
rc = SQLBindCol(hstmt[DBSTMT_LOADCHAR], 25, SQL_C_LONG, &pclass, sizeof(pclass), &rclasslen);
if(!MYSQLSUCCESS(rc)) return false;
rc = SQLBindCol(hstmt[DBSTMT_LOADCHAR], 26, SQL_C_LONG, &guild, sizeof(guild), &rguildlen);
if(!MYSQLSUCCESS(rc)) return false;

rc = SQLFetch(hstmt[DBSTMT_LOADCHAR]);
if(MYSQLSUCCESS(rc))
{
SQLCloseCursor(hstmt[DBSTMT_LOADCHAR]);

pPC->m_nUSRL = srl;
pPC->m_nLevel = lvl;
pPC->m_nHP = hp;
pPC->m_nHPMax = max_hp;
pPC->m_nMP = mp;
pPC->m_nMPMax = max_mp;
pPC->m_nSP = sp;
pPC->m_nSPMax = max_sp;
if(pPC->m_sSpaceID == spaceid && pPC->m_sMapID == mapid) {
pPC->m_nMapPosX = mapx;
pPC->m_nMapPosY = mapy;
} else {
pPC->m_nMapPosX = 20*5;
pPC->m_nMapPosY = 20*13;
}
pPC->m_nExp = exp;
pPC->m_nMoney = money;
memcpy(pPC->m_arrayData, attr, MOB_EXT_SIZE);
if( sex == '0')
pPC->m_cAvatar.m_cSex = 0;
else
pPC->m_cAvatar.m_cSex = 1;
memset(pPC->m_cAvatar.m_nItem, 0xffffffff, sizeof(int)*MAX_AVATAR_ITEMSLOT);

pPC->m_cInvenEquipMax = eqslot;
pPC->m_cInvenUseMax = useslot;
pPC->m_cInvenETCMax = etcslot;
pPC->m_cInvenCashMax = cashslot;
pPC->m_cInvenEggMax = eggslot;
pPC->m_sBonus = bonus;
pPC->m_cClass = pclass;

// loading item
rc = SQLExecute(hstmt[DBSTMT_LOADWEAR]);
if( MYSQLSUCCESS(rc)) {
SQLINTEGER rwearlen;

rc = SQLBindCol(hstmt[DBSTMT_LOADWEAR], 1, SQL_C_BINARY, wear, sizeof(wear), &rwearlen);
if(MYSQLSUCCESS(rc))
{
rc = SQLFetch(hstmt[DBSTMT_LOADWEAR]);
if(MYSQLSUCCESS(rc)) _loadweardata(pPC);
SQLCloseCursor(hstmt[DBSTMT_LOADWEAR]);
}

}

return true;
}
SQLCloseCursor(hstmt[DBSTMT_LOADCHAR]);
return false;

}
bool SaveChar(CPC *pPC)
{
cid = pPC->m_nCUSRL;
lvl = pPC->m_nLevel;
hp = pPC->m_nHP;
max_hp = pPC->m_nHPMax;
mp = pPC->m_nMP;
max_mp = pPC->m_nMPMax;
sp = pPC->m_nSP;
max_sp = pPC->m_nSPMax;
mapid = pPC->m_sMapID;
spaceid = pPC->m_sSpaceID;
mapx = pPC->m_nMapPosX;
mapy = pPC->m_nMapPosY;
exp = pPC->m_nExp;
money = pPC->m_nMoney;
memset(attr, 0, sizeof(attr));
memcpy(attr, pPC->m_arrayData, MOB_EXT_SIZE);
if( pPC->m_cAvatar.m_cSex == 0)
sex = '0';
else sex = '1';
eqslot = pPC->m_cInvenEquipMax;
useslot = pPC->m_cInvenUseMax;
etcslot = pPC->m_cInvenETCMax;
cashslot = pPC->m_cInvenCashMax;
eggslot = pPC->m_cInvenEggMax;
bonus = pPC->m_sBonus;
homeid = pPC->m_nHomeID;
pclass = pPC->m_cClass;
guild = 0;
rc = SQLExecute(hstmt[DBSTMT_SAVECHAR]);
if(!MYSQLSUCCESS(rc)) return false;

// set-up item
_saveweardata(pPC);
rc = SQLExecute(hstmt[DBSTMT_SAVEWEAR]);
return true;
}
};

// ŔĚ°ÍŔş °ˇ¶ó Ŭ·ˇ˝şŔÔ´Ď´Ů. Äł¸ŻĹÍ Ľ±ĹĂ ą× »ýĽş âŔĚ żĎĽşµÇ¸é ąŮ˛îľîľß ÇŐ´Ď´Ů.
class IDBHandler
{
private:

public:
IDBHandler() {}
virtual ~IDBHandler() {}

virtual int CreateAccount(const char *pName, const char *pPass)=0;
virtual int CheckAccount(const char *pName, const char *pPass)=0;

virtual int CheckChar(int uid, const char *pName)=0;

virtual int CreateChar(CPC *pPC)=0;
virtual bool LoadChar(int cid, CPC *pPC)=0;
virtual bool SaveChar(CPC *pPC)=0;
};


// ÁřÁ¤ °ˇ¶ó Ŭ·ˇ˝ş. Äł¸ŻĹÍ »ýĽş¸¸ µÇ´Â ÇüĹ·Î
class CBaseDBHandler : public IDBHandler
{
private:
public:
CBaseDBHandler() {}
virtual ~CBaseDBHandler() {}

virtual int CreateAccount(const char *pName, const char *pPass)
{
return 0;
}
virtual int CheckAccount(const char *pName, const char *pPass)
{
return 0;
}

virtual int CheckChar(int uid, const char *pName)
{
return -1;
}


virtual int CreateChar(CPC *pPC) { return 0; }
virtual bool LoadChar(int cid, CPC *pPC) { return true; }
virtual bool SaveChar(CPC *pPC) { return true; }
};

// DB ż¬°áŔş µÇÁö¸¸ °ˇ¶óŔÔ´Ď´Ů.
class CGuraDBHandler : public IDBHandler
{
private:
CDBConnection *pDBCon;
public:
CGuraDBHandler()
{
pDBCon = NULL;
pDBCon = new CDBConnection;
pDBCon->Init("pie");
}
virtual ~CGuraDBHandler() { if(pDBCon) { pDBCon->Fin(); delete pDBCon; } }

virtual int CreateAccount(const char *pName, const char *pPass)
{
return 0;
}
virtual int CheckAccount(const char *pName, const char *pPass)
{
return 0;
}

virtual int CheckChar(int uid, const char *pName)
{
return pDBCon->GetCharID(pName);
}
virtual int CreateChar(CPC *pPC)
{
int cid = pDBCon->CreateChar(pPC);
pPC->m_nCUSRL = cid;
return cid;
}
virtual bool LoadChar(int cid, CPC *pPC)
{
if(pDBCon->LoadChar(cid, pPC)) return true;
return false;
}
virtual bool SaveChar(CPC *pPC)
{
return pDBCon->SaveChar(pPC);
}

};

#endif
 
Back
Top