summaryrefslogtreecommitdiff
path: root/src/char/int_auction.c
blob: aa01872f93e70dcc3e7a9fab7826c60126d9e72d (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
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
/**
 * 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_auction.h"

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

#include <stdio.h>
#include <stdlib.h>

static struct inter_auction_interface inter_auction_s;
struct inter_auction_interface *inter_auction;

static int inter_auction_count(int char_id, bool buy)
{
	int i = 0;
	struct auction_data *auction;
	struct DBIterator *iter = db_iterator(inter_auction->db);

	for( auction = dbi_first(iter); dbi_exists(iter); auction = dbi_next(iter) )
	{
		if ((buy && auction->buyer_id == char_id) || (!buy && auction->seller_id == char_id))
			i++;
	}
	dbi_destroy(iter);

	return i;
}

static void inter_auction_save(struct auction_data *auction)
{
	int j;
	StringBuf buf;
	struct SqlStmt *stmt;

	if( !auction )
		return;

	StrBuf->Init(&buf);
	StrBuf->Printf(&buf, "UPDATE `%s` SET `seller_id` = '%d', `seller_name` = ?, `buyer_id` = '%d', `buyer_name` = ?, `price` = '%d', `buynow` = '%d', `hours` = '%d', `timestamp` = '%lu', `nameid` = '%d', `item_name` = ?, `type` = '%d', `refine` = '%d', `attribute` = '%d'",
		auction_db, auction->seller_id, auction->buyer_id, auction->price, auction->buynow, auction->hours, (unsigned long)auction->timestamp, auction->item.nameid, auction->type, auction->item.refine, auction->item.attribute);
	for (j = 0; j < MAX_SLOTS; j++)
		StrBuf->Printf(&buf, ", `card%d` = '%d'", j, auction->item.card[j]);
	for (j = 0; j < MAX_ITEM_OPTIONS; j++)
		StrBuf->Printf(&buf, ", `opt_idx%d` = '%d', `opt_val%d` = '%d'", j, auction->item.option[j].index, j, auction->item.option[j].value);
	StrBuf->Printf(&buf, " WHERE `auction_id` = '%u'", auction->auction_id);

	stmt = SQL->StmtMalloc(inter->sql_handle);
	if( SQL_SUCCESS != SQL->StmtPrepareStr(stmt, StrBuf->Value(&buf))
	||  SQL_SUCCESS != SQL->StmtBindParam(stmt, 0, SQLDT_STRING, auction->seller_name, strnlen(auction->seller_name, NAME_LENGTH))
	||  SQL_SUCCESS != SQL->StmtBindParam(stmt, 1, SQLDT_STRING, auction->buyer_name, strnlen(auction->buyer_name, NAME_LENGTH))
	||  SQL_SUCCESS != SQL->StmtBindParam(stmt, 2, SQLDT_STRING, auction->item_name, strnlen(auction->item_name, ITEM_NAME_LENGTH))
	||  SQL_SUCCESS != SQL->StmtExecute(stmt) )
	{
		SqlStmt_ShowDebug(stmt);
	}

	SQL->StmtFree(stmt);
	StrBuf->Destroy(&buf);
}

static unsigned int inter_auction_create(struct auction_data *auction)
{
	int j;
	StringBuf buf;
	struct SqlStmt *stmt;

	nullpo_ret(auction);

	auction->timestamp = time(NULL) + (auction->hours * 3600);

	StrBuf->Init(&buf);
	StrBuf->Printf(&buf, "INSERT INTO `%s` (`seller_id`,`seller_name`,`buyer_id`,`buyer_name`,`price`,`buynow`,`hours`,`timestamp`,`nameid`,`item_name`,`type`,`refine`,`attribute`,`unique_id`", auction_db);
	for (j = 0; j < MAX_SLOTS; j++)
		StrBuf->Printf(&buf, ",`card%d`", j);
	for (j = 0; j < MAX_ITEM_OPTIONS; j++)
		StrBuf->Printf(&buf, ", `opt_idx%d`, `opt_val%d`", j, j);
	StrBuf->Printf(&buf, ") VALUES ('%d',?,'%d',?,'%d','%d','%d','%lu','%d',?,'%d','%d','%d','%"PRIu64"'",
		auction->seller_id, auction->buyer_id, auction->price, auction->buynow, auction->hours, (unsigned long)auction->timestamp, auction->item.nameid, auction->type, auction->item.refine, auction->item.attribute, auction->item.unique_id);
	for (j = 0; j < MAX_SLOTS; j++)
		StrBuf->Printf(&buf, ",'%d'", auction->item.card[j]);
	for (j = 0; j < MAX_ITEM_OPTIONS; j++)
		StrBuf->Printf(&buf, ",'%d','%d'", auction->item.option[j].index, auction->item.option[j].value);

	StrBuf->AppendStr(&buf, ")");

	stmt = SQL->StmtMalloc(inter->sql_handle);
	if (SQL_SUCCESS != SQL->StmtPrepareStr(stmt, StrBuf->Value(&buf))
	||  SQL_SUCCESS != SQL->StmtBindParam(stmt, 0, SQLDT_STRING, auction->seller_name, strnlen(auction->seller_name, NAME_LENGTH))
	||  SQL_SUCCESS != SQL->StmtBindParam(stmt, 1, SQLDT_STRING, auction->buyer_name, strnlen(auction->buyer_name, NAME_LENGTH))
	||  SQL_SUCCESS != SQL->StmtBindParam(stmt, 2, SQLDT_STRING, auction->item_name, strnlen(auction->item_name, ITEM_NAME_LENGTH))
	||  SQL_SUCCESS != SQL->StmtExecute(stmt))
	{
		SqlStmt_ShowDebug(stmt);
		auction->auction_id = 0;
	} else {
		struct auction_data *auction_;
		int64 tick = (int64)auction->hours * 3600000;

		auction->item.amount = 1;
		auction->item.identify = 1;
		auction->item.expire_time = 0;

		auction->auction_id = (unsigned int)SQL->StmtLastInsertId(stmt);
		auction->auction_end_timer = timer->add( timer->gettick() + tick , inter_auction->end_timer, auction->auction_id, 0);
		ShowInfo("New Auction %u | time left %"PRId64" ms | By %s.\n", auction->auction_id, tick, auction->seller_name);

		CREATE(auction_, struct auction_data, 1);
		memcpy(auction_, auction, sizeof(struct auction_data));
		idb_put(inter_auction->db, auction_->auction_id, auction_);
	}

	SQL->StmtFree(stmt);
	StrBuf->Destroy(&buf);

	return auction->auction_id;
}

static int inter_auction_end_timer(int tid, int64 tick, int id, intptr_t data)
{
	struct auction_data *auction;
	if( (auction = (struct auction_data *)idb_get(inter_auction->db, id)) != NULL )
	{
		if( auction->buyer_id )
		{
			inter_mail->sendmail(0, "Auction Manager", auction->buyer_id, auction->buyer_name, "Auction", "Thanks, you won the auction!.", 0, &auction->item);
			mapif->auction_message(auction->buyer_id, 6); // You have won the auction
			inter_mail->sendmail(0, "Auction Manager", auction->seller_id, auction->seller_name, "Auction", "Payment for your auction!.", auction->price, NULL);
		}
		else
			inter_mail->sendmail(0, "Auction Manager", auction->seller_id, auction->seller_name, "Auction", "No buyers have been found for your auction.", 0, &auction->item);

		ShowInfo("Auction End: id %u.\n", auction->auction_id);

		auction->auction_end_timer = INVALID_TIMER;
		inter_auction->delete_(auction);
	}

	return 0;
}

static void inter_auction_delete(struct auction_data *auction)
{
	unsigned int auction_id;
	nullpo_retv(auction);

	auction_id = auction->auction_id;

	if( SQL_ERROR == SQL->Query(inter->sql_handle, "DELETE FROM `%s` WHERE `auction_id` = '%u'", auction_db, auction_id) )
		Sql_ShowDebug(inter->sql_handle);

	if( auction->auction_end_timer != INVALID_TIMER )
		timer->delete(auction->auction_end_timer, inter_auction->end_timer);

	idb_remove(inter_auction->db, auction_id);
}

static void inter_auctions_fromsql(void)
{
	int i;
	struct auction_data *auction;
	char *data;
	StringBuf buf;
	int64 tick = timer->gettick(), endtick;
	time_t now = time(NULL);

	StrBuf->Init(&buf);
	StrBuf->AppendStr(&buf, "SELECT `auction_id`,`seller_id`,`seller_name`,`buyer_id`,`buyer_name`,"
		"`price`,`buynow`,`hours`,`timestamp`,`nameid`,`item_name`,`type`,`refine`,`attribute`,`unique_id`");
	for (i = 0; i < MAX_SLOTS; i++)
		StrBuf->Printf(&buf, ",`card%d`", i);
	for (i = 0; i < MAX_ITEM_OPTIONS; i++)
		StrBuf->Printf(&buf, ", `opt_idx%d`, `opt_val%d`", i, i);
	StrBuf->Printf(&buf, " FROM `%s` ORDER BY `auction_id` DESC", auction_db);

	if (SQL_ERROR == SQL->QueryStr(inter->sql_handle, StrBuf->Value(&buf)))
		Sql_ShowDebug(inter->sql_handle);

	StrBuf->Destroy(&buf);

	while (SQL_SUCCESS == SQL->NextRow(inter->sql_handle)) {
		struct item *item;
		CREATE(auction, struct auction_data, 1);
		SQL->GetData(inter->sql_handle, 0, &data, NULL); auction->auction_id = atoi(data);
		SQL->GetData(inter->sql_handle, 1, &data, NULL); auction->seller_id = atoi(data);
		SQL->GetData(inter->sql_handle, 2, &data, NULL); safestrncpy(auction->seller_name, data, NAME_LENGTH);
		SQL->GetData(inter->sql_handle, 3, &data, NULL); auction->buyer_id = atoi(data);
		SQL->GetData(inter->sql_handle, 4, &data, NULL); safestrncpy(auction->buyer_name, data, NAME_LENGTH);
		SQL->GetData(inter->sql_handle, 5, &data, NULL); auction->price = atoi(data);
		SQL->GetData(inter->sql_handle, 6, &data, NULL); auction->buynow = atoi(data);
		SQL->GetData(inter->sql_handle, 7, &data, NULL); auction->hours = atoi(data);
		SQL->GetData(inter->sql_handle, 8, &data, NULL); auction->timestamp = atoi(data);

		item = &auction->item;
		SQL->GetData(inter->sql_handle, 9, &data, NULL); item->nameid = atoi(data);
		SQL->GetData(inter->sql_handle,10, &data, NULL); safestrncpy(auction->item_name, data, ITEM_NAME_LENGTH);
		SQL->GetData(inter->sql_handle,11, &data, NULL); auction->type = atoi(data);

		SQL->GetData(inter->sql_handle,12, &data, NULL); item->refine = atoi(data);
		SQL->GetData(inter->sql_handle,13, &data, NULL); item->attribute = atoi(data);
		SQL->GetData(inter->sql_handle,14, &data, NULL); item->unique_id = strtoull(data, NULL, 10);

		item->identify = 1;
		item->amount = 1;
		item->expire_time = 0;
		/* Card Slots */
		for (i = 0; i < MAX_SLOTS; i++) {
			SQL->GetData(inter->sql_handle, 15 + i, &data, NULL);
			item->card[i] = atoi(data);
		}
		/* Item Options */
		for (i = 0; i < MAX_ITEM_OPTIONS; i++) {
			SQL->GetData(inter->sql_handle, 15 + MAX_SLOTS + i * 2, &data, NULL);
			item->option[i].index = atoi(data);
			SQL->GetData(inter->sql_handle, 16 + MAX_SLOTS + i * 2, &data, NULL);
			item->option[i].value = atoi(data);
		}

		if (auction->timestamp > now)
			endtick = ((int64)(auction->timestamp - now) * 1000) + tick;
		else
			endtick = tick + 10000; // 10 seconds to process ended auctions

		auction->auction_end_timer = timer->add(endtick, inter_auction->end_timer, auction->auction_id, 0);
		idb_put(inter_auction->db, auction->auction_id, auction);
	}

	SQL->FreeResult(inter->sql_handle);
}

/*==========================================
 * Packets From Map Server
 *------------------------------------------*/
static int inter_auction_parse_frommap(int fd)
{
	switch(RFIFOW(fd,0))
	{
		case 0x3050: mapif->parse_auction_requestlist(fd); break;
		case 0x3051: mapif->parse_auction_register(fd); break;
		case 0x3052: mapif->parse_auction_cancel(fd); break;
		case 0x3053: mapif->parse_auction_close(fd); break;
		case 0x3055: mapif->parse_auction_bid(fd); break;
		default:
			return 0;
	}
	return 1;
}

static int inter_auction_sql_init(void)
{
	inter_auction->db = idb_alloc(DB_OPT_RELEASE_DATA);
	inter_auction->fromsql();

	return 0;
}

static void inter_auction_sql_final(void)
{
	inter_auction->db->destroy(inter_auction->db,NULL);

	return;
}

void inter_auction_defaults(void)
{
	inter_auction = &inter_auction_s;

	inter_auction->db = NULL; // int auction_id -> struct auction_data*

	inter_auction->count = inter_auction_count;
	inter_auction->save = inter_auction_save;
	inter_auction->create = inter_auction_create;
	inter_auction->end_timer = inter_auction_end_timer;
	inter_auction->delete_ = inter_auction_delete;
	inter_auction->fromsql = inter_auctions_fromsql;
	inter_auction->parse_frommap = inter_auction_parse_frommap;
	inter_auction->sql_init = inter_auction_sql_init;
	inter_auction->sql_final = inter_auction_sql_final;
}