-
Notifications
You must be signed in to change notification settings - Fork 3
/
user.c
196 lines (167 loc) · 3.92 KB
/
user.c
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include "holdem.h"
#include "user.h"
user_t *g_current_user;
user_t *g_users = NULL;
int g_num_users = 0;
user_t *user_create()
{
user_t *user;
if (g_num_users >= MAX_USERS) {
return NULL;
}
user = (user_t *)malloc(sizeof(user_t));
if (user == NULL) {
return NULL;
}
memset(user, 0, sizeof(user_t));
snprintf(user->prompt, sizeof(user->prompt), ">");
g_num_users++;
return user;
}
void user_destroy(user_t *user)
{
if (user) {
g_num_users--;
free(user);
}
}
void send_msg(user_t *user, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
send_msgv(user, fmt, ap);
va_end(ap);
}
void send_msgv(user_t *user, const char *fmt, va_list ap)
{
evbuffer_add_vprintf(bufferevent_get_output(user->bev), fmt, ap);
evbuffer_add_printf(bufferevent_get_output(user->bev), "%s\n", user->prompt);
}
void send_msg_raw(user_t *user, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
send_msgv_raw(user, fmt, ap);
va_end(ap);
}
void send_msgv_raw(user_t *user, const char *fmt, va_list ap)
{
evbuffer_add_vprintf(bufferevent_get_output(user->bev), fmt, ap);
}
void broadcast_global(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
broadcast_globalv(fmt, ap);
va_end(ap);
}
void broadcast_globalv(const char *fmt, va_list ap)
{
user_t *tmp, *user;
va_list cp;
HASH_ITER(hh, g_users, user, tmp) {
va_copy(cp, ap);
send_msgv(user, fmt, cp);
}
}
void broadcast_global_raw(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
broadcast_globalv_raw(fmt, ap);
va_end(ap);
}
void broadcast_globalv_raw(const char *fmt, va_list ap)
{
user_t *tmp, *user;
va_list cp;
HASH_ITER(hh, g_users, user, tmp) {
va_copy(cp, ap);
send_msgv_raw(user, fmt, cp);
}
}
int user_save(user_t *user)
{
int ret;
ret = user_save_password(user);
if (ret < 0) {
return ret;
}
ret = user_save_prompt(user);
if (ret < 0) {
return ret;
}
ret = user_save_money(user);
if (ret < 0) {
return ret;
}
return 0;
}
int user_load(const char *name, user_t *user)
{
char bufk[128], bufv[128];
size_t lenk, lenv;
int ret;
lenk = snprintf(bufk, sizeof(bufk), "%s_password", name);
ret = holdem_db_get(bufk, lenk, bufv, &lenv);
if (ret < 0) {
return ret;
}
memcpy(user->password, bufv, lenv);
lenk = snprintf(bufk, sizeof(bufk), "%s_prompt", name);
ret = holdem_db_get(bufk, lenk, bufv, &lenv);
if (ret < 0) {
return ret;
}
memcpy(user->prompt, bufv, lenv);
lenk = snprintf(bufk, sizeof(bufk), "%s_money", name);
ret = holdem_db_get(bufk, lenk, bufv, &lenv);
if (ret < 0) {
return ret;
}
memcpy(&(user->money), bufv, lenv);
strncpy(user->name, name, sizeof(user->name));
return 0;
}
int user_save_password(user_t *user)
{
char bufk[128], bufv[128];
size_t lenk, lenv;
int ret;
lenk = snprintf(bufk, sizeof(bufk), "%s_password", user->name);
lenv = sizeof(user->password);
memcpy(bufv, user->password, lenv);
ret = holdem_db_put(bufk, lenk, bufv, lenv);
if (ret < 0) {
return ret;
}
return 0;
}
int user_save_prompt(user_t *user)
{
char bufk[128], bufv[128];
size_t lenk, lenv;
int ret;
lenk = snprintf(bufk, sizeof(bufk), "%s_prompt", user->name);
lenv = sizeof(user->prompt);
memcpy(bufv, user->prompt, lenv);
ret = holdem_db_put(bufk, lenk, bufv, lenv);
if (ret < 0) {
return ret;
}
return 0;
}
int user_save_money(user_t *user)
{
char bufk[128], bufv[128];
size_t lenk, lenv;
int ret;
lenk = snprintf(bufk, sizeof(bufk), "%s_money", user->name);
lenv = sizeof(user->money);
memcpy(bufv, &(user->money), lenv);
ret = holdem_db_put(bufk, lenk, bufv, lenv);
if (ret < 0) {
return ret;
}
return 0;
}