-
Notifications
You must be signed in to change notification settings - Fork 0
/
persistence.gsc
executable file
·167 lines (124 loc) · 3.53 KB
/
persistence.gsc
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
CREATE TABLE IF NOT EXISTS `players` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`user` varchar(32) DEFAULT NULL,
`pass` varchar(32) DEFAULT NULL,
`money` int(10) DEFAULT NULL,
`xp` int(10) DEFAULT NULL,
`kills` int(10) DEFAULT NULL,
`headshots` int(10) DEFAULT NULL,
`melee_kills` int(10) DEFAULT NULL,
`longest_killstreak` int(10) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
DELIMITER $$
DROP FUNCTION IF EXISTS statsDeltaMoney$$
CREATE FUNCTION statsDeltaMoney (id_ INT, money_ INT)
RETURNS INT
DETERMINISTIC
BEGIN
DECLARE ret INT;
SET ret = NULL;
UPDATE players SET money=IFNULL(money,0)+money_ WHERE id = id_;
SELECT money FROM players WHERE id = id_ INTO ret;
RETURN ret;
END$$
DELIMITER ;
SELECT statsDeltaMoney(5, 10) AS newMoney;
DELIMITER $$
DROP FUNCTION IF EXISTS statsDeltaXP$$
CREATE FUNCTION statsDeltaXP (id_ INT, xp_ INT)
RETURNS INT
DETERMINISTIC
BEGIN
DECLARE ret INT;
SET ret = NULL;
UPDATE players SET xp=IFNULL(xp,0)+xp_ WHERE id = id_;
SELECT xp FROM players WHERE id = id_ INTO ret;
RETURN ret;
END$$
DELIMITER ;
SELECT statsDeltaXP(5, 10) AS newXP;
*/
eventAddGetMoney(name,value)
{
player = self;
// no login = no persistent data
// false = tells event system, that it shall save it normal
if (!isDefined(player.statsID))
return false;
key = name;
ret = mysql_query(level.mysql, "SELECT statsDeltaMoney("+player.statsID+", "+value+") AS newMoney;");
if (!isDefined(ret) || mysql_errno(level.mysql))
{
printf("ERROR: mod\\stats::add("+key+","+value+"): " + mysql_error(level.mysql));
return;
}
result = mysql_store_result(level.mysql);
row = mysql_fetch_row(result);
if (!isDefined(row))
{
printf("ROW NOT DEFINED\n");
return;
}
newvalue = int(row[0]);
mysql_free_result(result);
// default action
player.stats[name] /*+*/= newvalue; // statsDeltaMoney() is addget-like (before i added "value", but need it for money effect)
return true; // continue with events
}
eventAddGetXP(name, value)
{
player = self;
// no login = no persistent data
// false = tells event system, that it shall save it normal
if (!isDefined(player.statsID))
return false;
key = name;
ret = mysql_query(level.mysql, "SELECT statsDeltaXP("+player.statsID+", "+value+") AS newXP;");
if (!isDefined(ret) || mysql_errno(level.mysql))
{
printf("ERROR: mod\\stats::add("+key+","+value+"): " + mysql_error(level.mysql));
return;
}
result = mysql_store_result(level.mysql);
row = mysql_fetch_row(result);
if (!isDefined(row))
{
printf("ROW NOT DEFINED\n");
return;
}
newvalue = int(row[0]);
mysql_free_result(result);
player.stats[name] /*+*/= newvalue;
return true; // continue with events
}
loginUser(user, pass)
{
player = self;
ret = mysql_query(level.mysql, "SELECT id FROM players WHERE user='" + player getGuid() + "'");
if (!isDefined(ret) || ret != 0)
{
// todo: write to log
printf("errno="+mysql_errno(level.mysql) + " error=''"+mysql_error(level.mysql) + "''");
return false;
}
result = mysql_store_result(level.mysql);
row = mysql_fetch_row(result);
mysql_free_result(result);
if (!isDefined(row))
return false;
player.statsID = row[0];
//player iprintln("Welcome, you are logged in! ID="+player.statsID);
return true;
}
// doesnt do anything when user/pass-combination already exists
createUser(user, pass)
{
mysql_query(level.mysql, "INSERT INTO players (user, pass) VALUES ('" + user + "', '" + pass + "')");
//printf("mysql_errno=" + mysql_errno(level.mysql) + "\n");
}
isActive()
{
return isDefined(level.mysql);
}