/[LeafOK_CVS]/lbbs/utils/game/money.c
ViewVC logotype

Contents of /lbbs/utils/game/money.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.14 - (show annotations)
Sat Jan 3 10:27:14 2026 UTC (2 months, 1 week ago) by sysadm
Branch: MAIN
CVS Tags: HEAD
Changes since 1.13: +1 -1 lines
Content type: text/x-csrc
Update copyright info

1 /* SPDX-License-Identifier: GPL-3.0-or-later */
2 /*
3 * money
4 * - basic operations of user money
5 *
6 * Copyright (C) 2004-2026 Leaflet <leaflet@leafok.com>
7 */
8
9 #ifdef HAVE_CONFIG_H
10 #include "config.h"
11 #endif
12
13 #include "database.h"
14 #include "log.h"
15 #include "money.h"
16 #include "user_priv.h"
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <mysql.h>
20
21 int BBS_user_money = 0;
22
23 int money_balance()
24 {
25 return BBS_user_money;
26 }
27
28 int money_deposit(int money)
29 {
30 MYSQL *db;
31 MYSQL_RES *rs;
32 MYSQL_ROW row;
33 char sql[SQL_BUFFER_LEN];
34 int ret = 0;
35
36 if (money <= 0)
37 {
38 return 0;
39 }
40
41 db = db_open();
42 if (db == NULL)
43 {
44 return -1;
45 }
46
47 // Begin transaction
48 if (mysql_query(db, "SET autocommit=0") != 0)
49 {
50 log_error("SET autocommit=0 error: %s", mysql_error(db));
51 return -1;
52 }
53
54 if (mysql_query(db, "BEGIN") != 0)
55 {
56 log_error("Begin transaction error: %s", mysql_error(db));
57 return -1;
58 }
59
60 snprintf(sql, sizeof(sql),
61 "SELECT game_money FROM user_pubinfo WHERE UID = %d FOR UPDATE",
62 BBS_priv.uid);
63
64 if (mysql_query(db, sql) != 0)
65 {
66 log_error("Query user_pubinfo error: %s", mysql_error(db));
67 return -1;
68 }
69 if ((rs = mysql_store_result(db)) == NULL)
70 {
71 log_error("Get user money failed");
72 return -1;
73 }
74 if ((row = mysql_fetch_row(rs)))
75 {
76 BBS_user_money = atoi(row[0]);
77 }
78 mysql_free_result(rs);
79
80 if (BBS_user_money + money < USER_MONEY_MAX)
81 {
82 ret = money;
83 BBS_user_money += money;
84 }
85 else if (BBS_user_money < USER_MONEY_MAX)
86 {
87 ret = USER_MONEY_MAX - BBS_user_money;
88 BBS_user_money = USER_MONEY_MAX;
89 }
90 else // Balance exceeds 1 billion !!!
91 {
92 mysql_close(db);
93 return -2;
94 }
95
96 snprintf(sql, sizeof(sql),
97 "UPDATE user_pubinfo SET game_money = %d WHERE UID = %d",
98 BBS_user_money, BBS_priv.uid);
99
100 if (mysql_query(db, sql) != 0)
101 {
102 log_error("Update user_pubinfo error: %s", mysql_error(db));
103 return -1;
104 }
105
106 // Commit transaction
107 if (mysql_query(db, "COMMIT") != 0)
108 {
109 log_error("Commit transaction error: %s", mysql_error(db));
110 return -1;
111 }
112
113 mysql_close(db);
114
115 return ret;
116 }
117
118 int money_withdraw(int money)
119 {
120 MYSQL *db;
121 MYSQL_RES *rs;
122 MYSQL_ROW row;
123 char sql[SQL_BUFFER_LEN];
124 int ret = 0;
125
126 if (money <= 0)
127 {
128 return 0;
129 }
130
131 db = db_open();
132 if (db == NULL)
133 {
134 return -1;
135 }
136
137 // Begin transaction
138 if (mysql_query(db, "SET autocommit=0") != 0)
139 {
140 log_error("SET autocommit=0 error: %s", mysql_error(db));
141 return -1;
142 }
143
144 if (mysql_query(db, "BEGIN") != 0)
145 {
146 log_error("Begin transaction error: %s", mysql_error(db));
147 return -1;
148 }
149
150 snprintf(sql, sizeof(sql),
151 "SELECT game_money FROM user_pubinfo WHERE UID = %d FOR UPDATE",
152 BBS_priv.uid);
153
154 if (mysql_query(db, sql) != 0)
155 {
156 log_error("Query user_pubinfo error: %s", mysql_error(db));
157 return -1;
158 }
159 if ((rs = mysql_store_result(db)) == NULL)
160 {
161 log_error("Get user money failed");
162 return -1;
163 }
164 if ((row = mysql_fetch_row(rs)))
165 {
166 BBS_user_money = atoi(row[0]);
167 }
168 mysql_free_result(rs);
169
170 if (BBS_user_money >= money)
171 {
172 ret = money;
173 BBS_user_money -= money;
174 }
175 else // No enough balance
176 {
177 mysql_close(db);
178 return -2;
179 }
180
181 snprintf(sql, sizeof(sql),
182 "UPDATE user_pubinfo SET game_money = %d WHERE UID = %d",
183 BBS_user_money, BBS_priv.uid);
184
185 if (mysql_query(db, sql) != 0)
186 {
187 log_error("Update user_pubinfo error: %s", mysql_error(db));
188 return -1;
189 }
190
191 // Commit transaction
192 if (mysql_query(db, "COMMIT") != 0)
193 {
194 log_error("Commit transaction error: %s", mysql_error(db));
195 return -1;
196 }
197
198 mysql_close(db);
199
200 return ret;
201 }
202
203 int money_refresh(void)
204 {
205 MYSQL *db;
206 MYSQL_RES *rs;
207 MYSQL_ROW row;
208 char sql[SQL_BUFFER_LEN];
209
210 db = db_open();
211 if (db == NULL)
212 {
213 return -1;
214 }
215
216 snprintf(sql, sizeof(sql),
217 "SELECT game_money FROM user_pubinfo WHERE UID = %d FOR UPDATE",
218 BBS_priv.uid);
219
220 if (mysql_query(db, sql) != 0)
221 {
222 log_error("Query user_pubinfo error: %s", mysql_error(db));
223 return -1;
224 }
225 if ((rs = mysql_store_result(db)) == NULL)
226 {
227 log_error("Get user money failed");
228 return -1;
229 }
230 if ((row = mysql_fetch_row(rs)))
231 {
232 BBS_user_money = atoi(row[0]);
233 }
234 mysql_free_result(rs);
235
236 mysql_close(db);
237
238 return 0;
239 }

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1