-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.cpp
93 lines (90 loc) · 2.26 KB
/
Player.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
#include <string>
#include "sampgdk.h"
#include <set>
#include "Server.h"
#include "Player.h"
#include "Dialogs.h"
extern set<int> playersOnline;
extern Server server;
void Player::Connect(string playerName, int playerId)
{
name = playerName;
id = playerId;
playersOnline.insert(id);
sql::PreparedStatement *pstmt;
sql::ResultSet *result;
pstmt = server.con->prepareStatement("SELECT * FROM accounts WHERE name = ?");
pstmt->setString(1, name);
result = pstmt->executeQuery();
if (result->next())
{
SendClientMessage(id, -1, "Àêêàóíò çàðåãèñòðèðîâàí");
LoadAccount(result);
Authorization();
}
else
{
SendClientMessage(id, -1, "Àêêàóíò íå çàðåãèñòðèðîâàí"); // ñîîáùèòü îá ýòîì
Registration();
}
delete result;
delete pstmt;
}
void Player::Authorization()
{
ShowPlayerDialog(id, Dialogs::Auth, DIALOG_STYLE_INPUT, "Àâòîðèçàöèÿ", "Äîáðî ïîæàëîâàòü íà ñåðâåð, åáàøüòå ïàðîëü", "Ââîä", "Îòìåíà");
}
void Player::Disconnect()
{
SaveAccount();
Clear();
playersOnline.erase(id);
}
void Player::Clear()
{
id = NULL;
name.clear();
password.clear();
level = NULL;
}
void Player::SaveAccount()
{
sql::PreparedStatement *pstmt;
pstmt = server.con->prepareStatement("UPDATE accounts SET password = ?, level = ? WHERE name = ?");
pstmt->setString(1, password);
pstmt->setInt(2, level);
pstmt->setString(3, name);
pstmt->executeQuery();
delete pstmt;
}
void Player::AddAccount()
{
try
{
sql::PreparedStatement *pstmt;
pstmt = server.con->prepareStatement("INSERT INTO accounts(name, password, level) VALUES(?,?,?)");
pstmt->setString(1, name);
pstmt->setString(2, password);
pstmt->setInt(3, level);
pstmt->execute();
delete pstmt;
}
catch (sql::SQLException &e)
{
sampgdk::logprintf("# ERR: SQLException in %s", __FILE__);
sampgdk::logprintf("(%s) on line %s", __FUNCTION__, __LINE__);
sampgdk::logprintf("# ERR: %s", e.what());
sampgdk::logprintf(" (MySQL error code: %s", e.getErrorCode());
sampgdk::logprintf(", SQLState: %s)", e.getSQLState());
}
}
void Player::LoadAccount(sql::ResultSet *result)
{
password = result->getString(3);
level = result->getInt(4);
}
void Player::Registration()
{
level = 1;
ShowPlayerDialog(id, Dialogs::Register, DIALOG_STYLE_INPUT, "Àâòîðèçàöèÿ", "Äîáðî ïîæàëîâàòü íà ñåðâåð, åáàøüòå ðåãó", "Ðåãà", "Îòìåíà");
}