summaryrefslogtreecommitdiff
path: root/src/char/int_mercenary.c
blob: 0c3a7fdf8d5d77ea291028c691aee2d00c021502 (plain) (blame)
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
/**
 * This file is part of Hercules.
 * http://herc.ws - http://github.com/HerculesWS/Hercules
 *
 * Copyright (C) 2012-2015  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_mercenary.h"

#include "char/char.h"
#include "char/inter.h"
#include "char/mapif.h"
#include "common/cbasetypes.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>

struct inter_mercenary_interface inter_mercenary_s;
struct inter_mercenary_interface *inter_mercenary;

bool inter_mercenary_owner_fromsql(int char_id, struct mmo_charstatus *status)
{
	char* data;

	nullpo_ret(status);
	if( SQL_ERROR == SQL->Query(inter->sql_handle, "SELECT `merc_id`, `arch_calls`, `arch_faith`, `spear_calls`, `spear_faith`, `sword_calls`, `sword_faith` FROM `%s` WHERE `char_id` = '%d'", mercenary_owner_db, char_id) )
	{
		Sql_ShowDebug(inter->sql_handle);
		return false;
	}

	if( SQL_SUCCESS != SQL->NextRow(inter->sql_handle) )
	{
		SQL->FreeResult(inter->sql_handle);
		return false;
	}

	SQL->GetData(inter->sql_handle,  0, &data, NULL); status->mer_id = atoi(data);
	SQL->GetData(inter->sql_handle,  1, &data, NULL); status->arch_calls = atoi(data);
	SQL->GetData(inter->sql_handle,  2, &data, NULL); status->arch_faith = atoi(data);
	SQL->GetData(inter->sql_handle,  3, &data, NULL); status->spear_calls = atoi(data);
	SQL->GetData(inter->sql_handle,  4, &data, NULL); status->spear_faith = atoi(data);
	SQL->GetData(inter->sql_handle,  5, &data, NULL); status->sword_calls = atoi(data);
	SQL->GetData(inter->sql_handle,  6, &data, NULL); status->sword_faith = atoi(data);
	SQL->FreeResult(inter->sql_handle);

	return true;
}

bool inter_mercenary_owner_tosql(int char_id, struct mmo_charstatus *status)
{
	nullpo_ret(status);
	if( SQL_ERROR == SQL->Query(inter->sql_handle, "REPLACE INTO `%s` (`char_id`, `merc_id`, `arch_calls`, `arch_faith`, `spear_calls`, `spear_faith`, `sword_calls`, `sword_faith`) VALUES ('%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d')",
		mercenary_owner_db, char_id, status->mer_id, status->arch_calls, status->arch_faith, status->spear_calls, status->spear_faith, status->sword_calls, status->sword_faith) )
	{
		Sql_ShowDebug(inter->sql_handle);
		return false;
	}

	return true;
}

bool inter_mercenary_owner_delete(int char_id)
{
	if( SQL_ERROR == SQL->Query(inter->sql_handle, "DELETE FROM `%s` WHERE `char_id` = '%d'", mercenary_owner_db, char_id) )
		Sql_ShowDebug(inter->sql_handle);

	if( SQL_ERROR == SQL->Query(inter->sql_handle, "DELETE FROM `%s` WHERE `char_id` = '%d'", mercenary_db, char_id) )
		Sql_ShowDebug(inter->sql_handle);

	return true;
}

/**
 * Creates a new mercenary with the given data.
 *
 * @remark
 *   The mercenary ID is expected to be 0, and will be filled with the newly
 *   assigned ID.
 *
 * @param[in,out] merc The new mercenary's data.
 * @retval false in case of errors.
 */
bool mapif_mercenary_create(struct s_mercenary *merc)
{
	nullpo_retr(false, merc);
	Assert_retr(false, merc->mercenary_id == 0);

	if (SQL_ERROR == SQL->Query(inter->sql_handle,
			"INSERT INTO `%s` (`char_id`,`class`,`hp`,`sp`,`kill_counter`,`life_time`) VALUES ('%d','%d','%d','%d','%u','%u')",
			mercenary_db, merc->char_id, merc->class_, merc->hp, merc->sp, merc->kill_count, merc->life_time)) {
		Sql_ShowDebug(inter->sql_handle);
		return false;
	}
	merc->mercenary_id = (int)SQL->LastInsertId(inter->sql_handle);

	return true;
}

/**
 * Saves an existing mercenary.
 *
 * @param merc The mercenary's data.
 * @retval false in case of errors.
 */
bool mapif_mercenary_save(const struct s_mercenary *merc)
{
	nullpo_retr(false, merc);
	Assert_retr(false, merc->mercenary_id > 0);

	if (SQL_ERROR == SQL->Query(inter->sql_handle,
			"UPDATE `%s` SET `char_id` = '%d', `class` = '%d', `hp` = '%d', `sp` = '%d', `kill_counter` = '%u', `life_time` = '%u' WHERE `mer_id` = '%d'",
			mercenary_db, merc->char_id, merc->class_, merc->hp, merc->sp, merc->kill_count, merc->life_time, merc->mercenary_id)) {
		Sql_ShowDebug(inter->sql_handle);
		return false;
	}

	return true;
}

bool mapif_mercenary_load(int merc_id, int char_id, struct s_mercenary *merc)
{
	char* data;

	nullpo_ret(merc);
	memset(merc, 0, sizeof(struct s_mercenary));
	merc->mercenary_id = merc_id;
	merc->char_id = char_id;

	if( SQL_ERROR == SQL->Query(inter->sql_handle, "SELECT `class`, `hp`, `sp`, `kill_counter`, `life_time` FROM `%s` WHERE `mer_id` = '%d' AND `char_id` = '%d'", mercenary_db, merc_id, char_id) )
	{
		Sql_ShowDebug(inter->sql_handle);
		return false;
	}

	if( SQL_SUCCESS != SQL->NextRow(inter->sql_handle) )
	{
		SQL->FreeResult(inter->sql_handle);
		return false;
	}

	SQL->GetData(inter->sql_handle,  0, &data, NULL); merc->class_ = atoi(data);
	SQL->GetData(inter->sql_handle,  1, &data, NULL); merc->hp = atoi(data);
	SQL->GetData(inter->sql_handle,  2, &data, NULL); merc->sp = atoi(data);
	SQL->GetData(inter->sql_handle,  3, &data, NULL); merc->kill_count = atoi(data);
	SQL->GetData(inter->sql_handle,  4, &data, NULL); merc->life_time = atoi(data);
	SQL->FreeResult(inter->sql_handle);
	if( save_log )
		ShowInfo("Mercenary loaded (%d - %d).\n", merc->mercenary_id, merc->char_id);

	return true;
}

bool mapif_mercenary_delete(int merc_id)
{
	if( SQL_ERROR == SQL->Query(inter->sql_handle, "DELETE FROM `%s` WHERE `mer_id` = '%d'", mercenary_db, merc_id) )
	{
		Sql_ShowDebug(inter->sql_handle);
		return false;
	}

	return true;
}

void mapif_mercenary_send(int fd, struct s_mercenary *merc, unsigned char flag)
{
	int size = sizeof(struct s_mercenary) + 5;

	nullpo_retv(merc);
	WFIFOHEAD(fd,size);
	WFIFOW(fd,0) = 0x3870;
	WFIFOW(fd,2) = size;
	WFIFOB(fd,4) = flag;
	memcpy(WFIFOP(fd,5),merc,sizeof(struct s_mercenary));
	WFIFOSET(fd,size);
}

void mapif_parse_mercenary_create(int fd, const struct s_mercenary *merc)
{
	struct s_mercenary merc_;
	bool result;

	memcpy(&merc_, merc, sizeof(merc_));

	result = mapif->mercenary_create(&merc_);
	mapif->mercenary_send(fd, &merc_, result);
}

void mapif_parse_mercenary_load(int fd, int merc_id, int char_id)
{
	struct s_mercenary merc;
	bool result = mapif->mercenary_load(merc_id, char_id, &merc);
	mapif->mercenary_send(fd, &merc, result);
}

void mapif_mercenary_deleted(int fd, unsigned char flag)
{
	WFIFOHEAD(fd,3);
	WFIFOW(fd,0) = 0x3871;
	WFIFOB(fd,2) = flag;
	WFIFOSET(fd,3);
}

void mapif_parse_mercenary_delete(int fd, int merc_id)
{
	bool result = mapif->mercenary_delete(merc_id);
	mapif->mercenary_deleted(fd, result);
}

void mapif_mercenary_saved(int fd, unsigned char flag)
{
	WFIFOHEAD(fd,3);
	WFIFOW(fd,0) = 0x3872;
	WFIFOB(fd,2) = flag;
	WFIFOSET(fd,3);
}

void mapif_parse_mercenary_save(int fd, struct s_mercenary* merc)
{
	bool result = mapif->mercenary_save(merc);
	mapif->mercenary_saved(fd, result);
}

int inter_mercenary_sql_init(void)
{
	return 0;
}
void inter_mercenary_sql_final(void)
{
	return;
}

/*==========================================
 * Inter Packets
 *------------------------------------------*/
int inter_mercenary_parse_frommap(int fd)
{
	unsigned short cmd = RFIFOW(fd,0);

	switch( cmd )
	{
		case 0x3070: mapif->parse_mercenary_create(fd, RFIFOP(fd,4)); break;
		case 0x3071: mapif->parse_mercenary_load(fd, (int)RFIFOL(fd,2), (int)RFIFOL(fd,6)); break;
		case 0x3072: mapif->parse_mercenary_delete(fd, (int)RFIFOL(fd,2)); break;
		case 0x3073: mapif->parse_mercenary_save(fd, RFIFOP(fd,4)); break;
		default:
			return 0;
	}
	return 1;
}

void inter_mercenary_defaults(void)
{
	inter_mercenary = &inter_mercenary_s;

	inter_mercenary->owner_fromsql = inter_mercenary_owner_fromsql;
	inter_mercenary->owner_tosql = inter_mercenary_owner_tosql;
	inter_mercenary->owner_delete = inter_mercenary_owner_delete;

	inter_mercenary->sql_init = inter_mercenary_sql_init;
	inter_mercenary->sql_final = inter_mercenary_sql_final;
	inter_mercenary->parse_frommap = inter_mercenary_parse_frommap;
}