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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
/**
* This file is part of Hercules.
* http://herc.ws - http://github.com/HerculesWS/Hercules
*
* Copyright (C) 2012-2020 Hercules Dev Team
* Copyright (C) Athena Dev Teams
*
* Hercules is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define HERCULES_CORE
#include "int_pet.h"
#include "char/char.h"
#include "char/inter.h"
#include "char/mapif.h"
#include "common/memmgr.h"
#include "common/mmo.h"
#include "common/nullpo.h"
#include "common/showmsg.h"
#include "common/socket.h"
#include "common/sql.h"
#include "common/strlib.h"
#include "common/utils.h"
#include <stdio.h>
#include <stdlib.h>
static struct inter_pet_interface inter_pet_s;
struct inter_pet_interface *inter_pet;
/**
* Saves a pet to the SQL database.
*
* Table structure:
* `pet` (`pet_id`, `class`, `name`, `account_id`, `char_id`, `level`, `egg_id`, `equip`, `intimate`, `hungry`, `rename_flag`, `incubate`, `autofeed`)
*
* @remark In case of newly created pet, the pet ID is not updated to reflect the newly assigned ID. The caller must do so.
*
* @param p The pet data to save.
* @return The ID of the saved pet, or 0 in case of errors.
*
**/
static int inter_pet_tosql(const struct s_pet *p)
{
nullpo_ret(p);
struct SqlStmt *stmt = SQL->StmtMalloc(inter->sql_handle);
if (stmt == NULL) {
SqlStmt_ShowDebug(stmt);
return 0;
}
int pet_id = 0;
if (p->pet_id == 0) { // New pet.
const char *query = "INSERT INTO `%s` "
"(`class`, `name`, `account_id`, `char_id`, `level`, `egg_id`, `equip`, "
"`intimate`, `hungry`, `rename_flag`, `incubate`, `autofeed`) "
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
if (SQL_ERROR == SQL->StmtPrepare(stmt, query, pet_db) ||
SQL_ERROR == SQL->StmtBindParam(stmt, 0, SQLDT_INT32, &p->class_, sizeof(p->class_)) ||
SQL_ERROR == SQL->StmtBindParam(stmt, 1, SQLDT_STRING, &p->name, strnlen(p->name, sizeof(p->name))) ||
SQL_ERROR == SQL->StmtBindParam(stmt, 2, SQLDT_INT32, &p->account_id, sizeof(p->account_id)) ||
SQL_ERROR == SQL->StmtBindParam(stmt, 3, SQLDT_INT32, &p->char_id, sizeof(p->char_id)) ||
SQL_ERROR == SQL->StmtBindParam(stmt, 4, SQLDT_INT16, &p->level, sizeof(p->level)) ||
SQL_ERROR == SQL->StmtBindParam(stmt, 5, SQLDT_INT32, &p->egg_id, sizeof(p->egg_id)) ||
SQL_ERROR == SQL->StmtBindParam(stmt, 6, SQLDT_INT32, &p->equip, sizeof(p->equip)) ||
SQL_ERROR == SQL->StmtBindParam(stmt, 7, SQLDT_INT16, &p->intimate, sizeof(p->intimate)) ||
SQL_ERROR == SQL->StmtBindParam(stmt, 8, SQLDT_INT16, &p->hungry, sizeof(p->hungry)) ||
SQL_ERROR == SQL->StmtBindParam(stmt, 9, SQLDT_INT8, &p->rename_flag, sizeof(p->rename_flag)) ||
SQL_ERROR == SQL->StmtBindParam(stmt, 10, SQLDT_INT8, &p->incubate, sizeof(p->incubate)) ||
SQL_ERROR == SQL->StmtBindParam(stmt, 11, SQLDT_INT32, &p->autofeed, sizeof(p->autofeed)) ||
SQL_ERROR == SQL->StmtExecute(stmt)) {
SqlStmt_ShowDebug(stmt);
SQL->StmtFree(stmt);
return 0;
}
pet_id = (int)SQL->LastInsertId(inter->sql_handle);
} else { // Update pet.
const char *query = "UPDATE `%s` SET "
"`class`=?, `name`=?, `account_id`=?, `char_id`=?, `level`=?, `egg_id`=?, `equip`=?, "
"`intimate`=?, `hungry`=?, `rename_flag`=?, `incubate`=?, `autofeed`=? "
"WHERE `pet_id`=?";
if (SQL_ERROR == SQL->StmtPrepare(stmt, query, pet_db) ||
SQL_ERROR == SQL->StmtBindParam(stmt, 0, SQLDT_INT32, &p->class_, sizeof(p->class_)) ||
SQL_ERROR == SQL->StmtBindParam(stmt, 1, SQLDT_STRING, &p->name, strnlen(p->name, sizeof(p->name))) ||
SQL_ERROR == SQL->StmtBindParam(stmt, 2, SQLDT_INT32, &p->account_id, sizeof(p->account_id)) ||
SQL_ERROR == SQL->StmtBindParam(stmt, 3, SQLDT_INT32, &p->char_id, sizeof(p->char_id)) ||
SQL_ERROR == SQL->StmtBindParam(stmt, 4, SQLDT_INT16, &p->level, sizeof(p->level)) ||
SQL_ERROR == SQL->StmtBindParam(stmt, 5, SQLDT_INT32, &p->egg_id, sizeof(p->egg_id)) ||
SQL_ERROR == SQL->StmtBindParam(stmt, 6, SQLDT_INT32, &p->equip, sizeof(p->equip)) ||
SQL_ERROR == SQL->StmtBindParam(stmt, 7, SQLDT_INT16, &p->intimate, sizeof(p->intimate)) ||
SQL_ERROR == SQL->StmtBindParam(stmt, 8, SQLDT_INT16, &p->hungry, sizeof(p->hungry)) ||
SQL_ERROR == SQL->StmtBindParam(stmt, 9, SQLDT_INT8, &p->rename_flag, sizeof(p->rename_flag)) ||
SQL_ERROR == SQL->StmtBindParam(stmt, 10, SQLDT_INT8, &p->incubate, sizeof(p->incubate)) ||
SQL_ERROR == SQL->StmtBindParam(stmt, 11, SQLDT_INT32, &p->autofeed, sizeof(p->autofeed)) ||
SQL_ERROR == SQL->StmtBindParam(stmt, 12, SQLDT_INT32, &p->pet_id, sizeof(p->pet_id)) ||
SQL_ERROR == SQL->StmtExecute(stmt)) {
SqlStmt_ShowDebug(stmt);
SQL->StmtFree(stmt);
return 0;
}
pet_id = p->pet_id;
}
SQL->StmtFree(stmt);
if (chr->show_save_log)
ShowInfo("Pet saved %d - %s.\n", pet_id, p->name);
return pet_id;
}
/**
* Loads a pet's data from the SQL database.
*
* Table structure:
* `pet` (`pet_id`, `class`, `name`, `account_id`, `char_id`, `level`, `egg_id`, `equip`, `intimate`, `hungry`, `rename_flag`, `incubate`, `autofeed`)
*
* @param pet_id The pet's ID.
* @param p The pet data to save the SQL data in.
* @return Always 0.
*
**/
static int inter_pet_fromsql(int pet_id, struct s_pet *p)
{
nullpo_ret(p);
struct SqlStmt *stmt = SQL->StmtMalloc(inter->sql_handle);
if (stmt == NULL) {
SqlStmt_ShowDebug(stmt);
return 0;
}
#ifdef NOISY
ShowInfo("Loading pet (%d)...\n",pet_id);
#endif
memset(p, 0, sizeof(struct s_pet));
const char *query = "SELECT "
"`class`, `name`, `account_id`, `char_id`, `level`, `egg_id`, `equip`, "
"`intimate`, `hungry`, `rename_flag`, `incubate`, `autofeed` "
"FROM `%s` WHERE `pet_id`=?";
if (SQL_ERROR == SQL->StmtPrepare(stmt, query, pet_db) ||
SQL_ERROR == SQL->StmtBindParam(stmt, 0, SQLDT_INT32, &pet_id, sizeof(pet_id)) ||
SQL_ERROR == SQL->StmtExecute(stmt) ||
SQL_ERROR == SQL->StmtBindColumn(stmt, 0, SQLDT_INT32, &p->class_, sizeof(p->class_), NULL, NULL) ||
SQL_ERROR == SQL->StmtBindColumn(stmt, 1, SQLDT_STRING, &p->name, sizeof(p->name), NULL, NULL) ||
SQL_ERROR == SQL->StmtBindColumn(stmt, 2, SQLDT_INT32, &p->account_id, sizeof(p->account_id), NULL, NULL) ||
SQL_ERROR == SQL->StmtBindColumn(stmt, 3, SQLDT_INT32, &p->char_id, sizeof(p->char_id), NULL, NULL) ||
SQL_ERROR == SQL->StmtBindColumn(stmt, 4, SQLDT_INT16, &p->level, sizeof(p->level), NULL, NULL) ||
SQL_ERROR == SQL->StmtBindColumn(stmt, 5, SQLDT_INT32, &p->egg_id, sizeof(p->egg_id), NULL, NULL) ||
SQL_ERROR == SQL->StmtBindColumn(stmt, 6, SQLDT_INT32, &p->equip, sizeof(p->equip), NULL, NULL) ||
SQL_ERROR == SQL->StmtBindColumn(stmt, 7, SQLDT_INT16, &p->intimate, sizeof(p->intimate), NULL, NULL) ||
SQL_ERROR == SQL->StmtBindColumn(stmt, 8, SQLDT_INT16, &p->hungry, sizeof(p->hungry), NULL, NULL) ||
SQL_ERROR == SQL->StmtBindColumn(stmt, 9, SQLDT_INT8, &p->rename_flag, sizeof(p->rename_flag), NULL, NULL) ||
SQL_ERROR == SQL->StmtBindColumn(stmt, 10, SQLDT_INT8, &p->incubate, sizeof(p->incubate), NULL, NULL) ||
SQL_ERROR == SQL->StmtBindColumn(stmt, 11, SQLDT_INT32, &p->autofeed, sizeof(p->autofeed), NULL, NULL)) {
SqlStmt_ShowDebug(stmt);
SQL->StmtFree(stmt);
return 0;
}
if (SQL->StmtNumRows(stmt) < 1) {
ShowError("inter_pet_fromsql: Requested non-existant pet ID: %d\n", pet_id);
SQL->StmtFree(stmt);
return 0;
}
if (SQL_ERROR == SQL->StmtNextRow(stmt)) {
SqlStmt_ShowDebug(stmt);
SQL->StmtFree(stmt);
return 0;
}
SQL->StmtFree(stmt);
p->pet_id = pet_id;
if (chr->show_save_log)
ShowInfo("Pet loaded %d - %s.\n", pet_id, p->name);
return 0;
}
//----------------------------------------------
static int inter_pet_sql_init(void)
{
//memory alloc
inter_pet->pt = (struct s_pet*)aCalloc(sizeof(struct s_pet), 1);
return 0;
}
static void inter_pet_sql_final(void)
{
if (inter_pet->pt) aFree(inter_pet->pt);
return;
}
//----------------------------------
static int inter_pet_delete(int pet_id)
{
ShowInfo("delete pet request: %d...\n",pet_id);
if( SQL_ERROR == SQL->Query(inter->sql_handle, "DELETE FROM `%s` WHERE `pet_id`='%d'", pet_db, pet_id) )
Sql_ShowDebug(inter->sql_handle);
return 0;
}
//------------------------------------------------------
/**
* Creates a new pet and inserts its data into the `pet` SQL table.
*
* @param account_id The pet's master's account ID.
* @param char_id The pet's master's char ID.
* @param pet_class The pet's class/monster ID.
* @param pet_lv The pet's level.
* @param pet_egg_id The pet's egg's item ID.
* @param pet_equip The pet's equipment's item ID.
* @param intimate The pet's intimacy value.
* @param hungry The pet's hunger value.
* @param rename_flag The pet's rename flag.
* @param incubate The pet's incubate state.
* @param pet_name The pet's name.
* @return The created pet's data struct, or NULL in case of errors.
*
**/
static struct s_pet *inter_pet_create(int account_id, int char_id, int pet_class, int pet_lv, int pet_egg_id,
int pet_equip, short intimate, short hungry, char rename_flag,
char incubate, const char *pet_name)
{
nullpo_retr(NULL, pet_name);
memset(inter_pet->pt, 0, sizeof(struct s_pet));
safestrncpy(inter_pet->pt->name, pet_name, NAME_LENGTH);
inter_pet->pt->account_id = (incubate == 1) ? 0 : account_id;
inter_pet->pt->char_id = (incubate == 1) ? 0 : char_id;
inter_pet->pt->class_ = pet_class;
inter_pet->pt->level = pet_lv;
inter_pet->pt->egg_id = pet_egg_id;
inter_pet->pt->equip = pet_equip;
inter_pet->pt->intimate = cap_value(intimate, PET_INTIMACY_NONE, PET_INTIMACY_MAX);
inter_pet->pt->hungry = cap_value(hungry, PET_HUNGER_STARVING, PET_HUNGER_STUFFED);
inter_pet->pt->rename_flag = rename_flag;
inter_pet->pt->incubate = incubate;
inter_pet->pt->pet_id = 0; // Signal NEW pet.
if ((inter_pet->pt->pet_id = inter_pet->tosql(inter_pet->pt)) != 0)
return inter_pet->pt;
return NULL;
}
static struct s_pet *inter_pet_load(int account_id, int char_id, int pet_id)
{
memset(inter_pet->pt, 0, sizeof(struct s_pet));
inter_pet->fromsql(pet_id, inter_pet->pt);
if(inter_pet->pt!=NULL) {
if (inter_pet->pt->incubate == 1) {
inter_pet->pt->account_id = inter_pet->pt->char_id = 0;
return inter_pet->pt;
} else if (account_id == inter_pet->pt->account_id && char_id == inter_pet->pt->char_id) {
return inter_pet->pt;
} else {
return NULL;
}
}
return NULL;
}
static int inter_pet_parse_frommap(int fd)
{
RFIFOHEAD(fd);
switch(RFIFOW(fd, 0)){
case 0x3080: mapif->parse_CreatePet(fd); break;
case 0x3081: mapif->parse_LoadPet(fd); break;
case 0x3082: mapif->parse_SavePet(fd); break;
case 0x3083: mapif->parse_DeletePet(fd); break;
default:
return 0;
}
return 1;
}
void inter_pet_defaults(void)
{
inter_pet = &inter_pet_s;
inter_pet->pt = NULL;
inter_pet->tosql = inter_pet_tosql;
inter_pet->fromsql = inter_pet_fromsql;
inter_pet->sql_init = inter_pet_sql_init;
inter_pet->sql_final = inter_pet_sql_final;
inter_pet->delete_ = inter_pet_delete;
inter_pet->parse_frommap = inter_pet_parse_frommap;
inter_pet->create = inter_pet_create;
inter_pet->load = inter_pet_load;
}
|