forked from derandark/PhatAC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCharacterDatabase.cpp
132 lines (99 loc) · 3.75 KB
/
CharacterDatabase.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "StdAfx.h"
#include "Database.h"
#include "CharacterDatabase.h"
CCharacterDatabase::CCharacterDatabase(CDatabase *DB)
{
m_DB = DB;
m_hSTMT = DB->m_hSTMT;
}
_CHARDESC *CCharacterDatabase::GetCharacterDesc(DWORD dwGUID, _CHARDESC *pBuffer)
{
char *command = csprintf("SELECT Account, Name, DeletePeriod, Instances, WorldClass FROM Characters WHERE ID=%u;", dwGUID);
SQLPrepare(m_hSTMT, (unsigned char *)command, SQL_NTS);
SQLExecute(m_hSTMT);
pBuffer->dwGUID = dwGUID;
*pBuffer->szAccount = 0;
*pBuffer->szName = 0;
pBuffer->dwDeletePeriod = 0;
pBuffer->dwInstances = 0;
*pBuffer->szWorldClass = 0;
SQLBindCol(m_hSTMT, 1, SQL_C_CHAR, &pBuffer->szAccount, 60, NULL);
SQLBindCol(m_hSTMT, 2, SQL_C_CHAR, &pBuffer->szName, 60, NULL);
SQLBindCol(m_hSTMT, 3, SQL_C_ULONG, &pBuffer->dwDeletePeriod, sizeof(DWORD), NULL);
SQLBindCol(m_hSTMT, 4, SQL_C_ULONG, &pBuffer->dwInstances, sizeof(DWORD), NULL);
SQLBindCol(m_hSTMT, 5, SQL_C_CHAR, &pBuffer->szWorldClass, 40, NULL);
RETCODE retcode = SQLFetch(m_hSTMT);
if (!(retcode == SQL_SUCCESS) && !(retcode == SQL_SUCCESS_WITH_INFO))
pBuffer = NULL;
SQLCloseCursor(m_hSTMT);
SQLFreeStmt(m_hSTMT, SQL_UNBIND);
return pBuffer;
}
_CHARDESC *CCharacterDatabase::GetCharacterDesc(const char* szName, _CHARDESC *pBuffer)
{
char *command = csprintf("SELECT Account, ID, DeletePeriod, Instances, WorldClass FROM Characters WHERE ((Name = \'%s\'));", szName);
SQLPrepare(m_hSTMT, (unsigned char *)command, SQL_NTS);
SQLExecute(m_hSTMT);
*pBuffer->szAccount = 0;
pBuffer->dwGUID = 0;
strncpy(pBuffer->szName, szName, 60);
pBuffer->dwDeletePeriod = 0;
pBuffer->dwInstances = 0;
*pBuffer->szWorldClass = 0;
SQLBindCol(m_hSTMT, 1, SQL_C_CHAR, &pBuffer->szAccount, 60, NULL);
SQLBindCol(m_hSTMT, 2, SQL_C_ULONG, &pBuffer->dwGUID, sizeof(DWORD), NULL);
SQLBindCol(m_hSTMT, 3, SQL_C_ULONG, &pBuffer->dwDeletePeriod, sizeof(DWORD), NULL);
SQLBindCol(m_hSTMT, 4, SQL_C_ULONG, &pBuffer->dwInstances, sizeof(DWORD), NULL);
SQLBindCol(m_hSTMT, 5, SQL_C_CHAR, &pBuffer->szWorldClass, 40, NULL);
RETCODE retcode = SQLFetch(m_hSTMT);
if (!(retcode == SQL_SUCCESS) && !(retcode == SQL_SUCCESS_WITH_INFO))
pBuffer = NULL;
SQLCloseCursor(m_hSTMT);
SQLFreeStmt(m_hSTMT, SQL_UNBIND);
return pBuffer;
}
void CCharacterDatabase::CreateCharacterDesc(const char* szAccount, DWORD dwGUID, const char* szName)
{
char *command;
command = csprintf("INSERT INTO Characters (Account, ID, Name, DeletePeriod, Instances, WorldClass) VALUES ('%s', %lu, '%s', 0, 1, 'human-male');", szAccount, dwGUID, szName);
SQLPrepare(m_hSTMT, (unsigned char *)command, SQL_NTS);
SQLExecute(m_hSTMT);
SQLFreeStmt(m_hSTMT, SQL_UNBIND);
}
DWORD CCharacterDatabase::IncCharacterInstance(DWORD dwGUID, DWORD dwLastInstance)
{
DWORD dwNewInstance = dwLastInstance + 1;
char *command = csprintf("UPDATE Characters SET Instances=%u WHERE ID=%u;", dwNewInstance, dwGUID);
SQLPrepare(m_hSTMT, (unsigned char *)command, SQL_NTS);
SQLExecute(m_hSTMT);
SQLCloseCursor(m_hSTMT);
SQLFreeStmt(m_hSTMT, SQL_UNBIND);
return dwNewInstance;
}
DWORD CCharacterDatabase::GetCharacters(const char *account, DWORD *dwGUIDs)
{
char *accountlwr = _strlwr(_strdup(account));
char *command = csprintf("SELECT ID FROM Characters WHERE (LCase(Account) = \'%s\');", accountlwr);
free(accountlwr);
SQLPrepare(m_hSTMT, (unsigned char *)command, SQL_NTS);
SQLExecute(m_hSTMT);
DWORD dwCount = 0;
DWORD dwGUID;
SQLBindCol(m_hSTMT, 1, SQL_C_ULONG, &dwGUID, sizeof(DWORD), NULL);
RETCODE rc;
while (dwCount < 5)
{
rc = SQLFetch(m_hSTMT);
if (rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
{
dwGUIDs[dwCount] = dwGUID;
dwCount++;
}
else
break;
}
//MsgBox("Chars: %lu", dwCount);
SQLCloseCursor(m_hSTMT);
SQLFreeStmt(m_hSTMT, SQL_UNBIND);
return dwCount;
}