Clone of PhatAC @ https://github.com/floaterxk/PhatAC

CharacterDatabase.cpp 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include "StdAfx.h"
  2. #include "Database.h"
  3. #include "CharacterDatabase.h"
  4. CCharacterDatabase::CCharacterDatabase(CDatabase *DB)
  5. {
  6. m_DB = DB;
  7. m_hSTMT = DB->m_hSTMT;
  8. }
  9. _CHARDESC *CCharacterDatabase::GetCharacterDesc(DWORD dwGUID, _CHARDESC *pBuffer)
  10. {
  11. char *command = csprintf("SELECT Account, Name, DeletePeriod, Instances, WorldClass FROM Characters WHERE ID=%u;", dwGUID);
  12. SQLPrepare(m_hSTMT, (unsigned char *)command, SQL_NTS);
  13. SQLExecute(m_hSTMT);
  14. pBuffer->dwGUID = dwGUID;
  15. *pBuffer->szAccount = 0;
  16. *pBuffer->szName = 0;
  17. pBuffer->dwDeletePeriod = 0;
  18. pBuffer->dwInstances = 0;
  19. *pBuffer->szWorldClass = 0;
  20. SQLBindCol(m_hSTMT, 1, SQL_C_CHAR, &pBuffer->szAccount, 60, NULL);
  21. SQLBindCol(m_hSTMT, 2, SQL_C_CHAR, &pBuffer->szName, 60, NULL);
  22. SQLBindCol(m_hSTMT, 3, SQL_C_ULONG, &pBuffer->dwDeletePeriod, sizeof(DWORD), NULL);
  23. SQLBindCol(m_hSTMT, 4, SQL_C_ULONG, &pBuffer->dwInstances, sizeof(DWORD), NULL);
  24. SQLBindCol(m_hSTMT, 5, SQL_C_CHAR, &pBuffer->szWorldClass, 40, NULL);
  25. RETCODE retcode = SQLFetch(m_hSTMT);
  26. if (!(retcode == SQL_SUCCESS) && !(retcode == SQL_SUCCESS_WITH_INFO))
  27. pBuffer = NULL;
  28. SQLCloseCursor(m_hSTMT);
  29. SQLFreeStmt(m_hSTMT, SQL_UNBIND);
  30. return pBuffer;
  31. }
  32. _CHARDESC *CCharacterDatabase::GetCharacterDesc(const char* szName, _CHARDESC *pBuffer)
  33. {
  34. char *command = csprintf("SELECT Account, ID, DeletePeriod, Instances, WorldClass FROM Characters WHERE ((Name = \'%s\'));", szName);
  35. SQLPrepare(m_hSTMT, (unsigned char *)command, SQL_NTS);
  36. SQLExecute(m_hSTMT);
  37. *pBuffer->szAccount = 0;
  38. pBuffer->dwGUID = 0;
  39. strncpy(pBuffer->szName, szName, 60);
  40. pBuffer->dwDeletePeriod = 0;
  41. pBuffer->dwInstances = 0;
  42. *pBuffer->szWorldClass = 0;
  43. SQLBindCol(m_hSTMT, 1, SQL_C_CHAR, &pBuffer->szAccount, 60, NULL);
  44. SQLBindCol(m_hSTMT, 2, SQL_C_ULONG, &pBuffer->dwGUID, sizeof(DWORD), NULL);
  45. SQLBindCol(m_hSTMT, 3, SQL_C_ULONG, &pBuffer->dwDeletePeriod, sizeof(DWORD), NULL);
  46. SQLBindCol(m_hSTMT, 4, SQL_C_ULONG, &pBuffer->dwInstances, sizeof(DWORD), NULL);
  47. SQLBindCol(m_hSTMT, 5, SQL_C_CHAR, &pBuffer->szWorldClass, 40, NULL);
  48. RETCODE retcode = SQLFetch(m_hSTMT);
  49. if (!(retcode == SQL_SUCCESS) && !(retcode == SQL_SUCCESS_WITH_INFO))
  50. pBuffer = NULL;
  51. SQLCloseCursor(m_hSTMT);
  52. SQLFreeStmt(m_hSTMT, SQL_UNBIND);
  53. return pBuffer;
  54. }
  55. void CCharacterDatabase::CreateCharacterDesc(const char* szAccount, DWORD dwGUID, const char* szName)
  56. {
  57. char *command;
  58. command = csprintf("INSERT INTO Characters (Account, ID, Name, DeletePeriod, Instances, WorldClass) VALUES ('%s', %lu, '%s', 0, 1, 'human-male');", szAccount, dwGUID, szName);
  59. SQLPrepare(m_hSTMT, (unsigned char *)command, SQL_NTS);
  60. SQLExecute(m_hSTMT);
  61. SQLFreeStmt(m_hSTMT, SQL_UNBIND);
  62. }
  63. DWORD CCharacterDatabase::IncCharacterInstance(DWORD dwGUID, DWORD dwLastInstance)
  64. {
  65. DWORD dwNewInstance = dwLastInstance + 1;
  66. char *command = csprintf("UPDATE Characters SET Instances=%u WHERE ID=%u;", dwNewInstance, dwGUID);
  67. SQLPrepare(m_hSTMT, (unsigned char *)command, SQL_NTS);
  68. SQLExecute(m_hSTMT);
  69. SQLCloseCursor(m_hSTMT);
  70. SQLFreeStmt(m_hSTMT, SQL_UNBIND);
  71. return dwNewInstance;
  72. }
  73. DWORD CCharacterDatabase::GetCharacters(const char *account, DWORD *dwGUIDs)
  74. {
  75. char *accountlwr = _strlwr(_strdup(account));
  76. char *command = csprintf("SELECT ID FROM Characters WHERE (LCase(Account) = \'%s\');", accountlwr);
  77. free(accountlwr);
  78. SQLPrepare(m_hSTMT, (unsigned char *)command, SQL_NTS);
  79. SQLExecute(m_hSTMT);
  80. DWORD dwCount = 0;
  81. DWORD dwGUID;
  82. SQLBindCol(m_hSTMT, 1, SQL_C_ULONG, &dwGUID, sizeof(DWORD), NULL);
  83. RETCODE rc;
  84. while (dwCount < 5)
  85. {
  86. rc = SQLFetch(m_hSTMT);
  87. if (rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
  88. {
  89. dwGUIDs[dwCount] = dwGUID;
  90. dwCount++;
  91. }
  92. else
  93. break;
  94. }
  95. //MsgBox("Chars: %lu", dwCount);
  96. SQLCloseCursor(m_hSTMT);
  97. SQLFreeStmt(m_hSTMT, SQL_UNBIND);
  98. return dwCount;
  99. }