root/src/char_sql/int_auction.c @ 17

Revision 1, 16.1 kB (checked in by jinshiro, 17 years ago)
Line 
1// Copyright (c) Athena Dev Teams - Licensed under GNU GPL
2// For more information, see LICENCE in the main folder
3
4#include "../common/mmo.h"
5#include "../common/malloc.h"
6#include "../common/db.h"
7#include "../common/showmsg.h"
8#include "../common/socket.h"
9#include "../common/strlib.h"
10#include "../common/sql.h"
11#include "../common/timer.h"
12#include "char.h"
13#include "inter.h"
14#include "int_mail.h"
15
16#include <stdio.h>
17#include <string.h>
18#include <stdlib.h>
19
20static DBMap* auction_db_ = NULL; // int auction_id -> struct auction_data*
21
22void auction_delete(struct auction_data *auction);
23static int auction_end_timer(int tid, unsigned int tick, int id, intptr data);
24
25static int auction_count(int char_id, bool buy)
26{
27        int i = 0;
28        struct auction_data *auction;
29        DBIterator* iter;
30        DBKey key;
31
32        iter = auction_db_->iterator(auction_db_);
33        for( auction = (struct auction_data*)iter->first(iter,&key); iter->exists(iter); auction = (struct auction_data*)iter->next(iter,&key) )
34        {
35                if( (buy && auction->buyer_id == char_id) || (!buy && auction->seller_id == char_id) )
36                        i++;
37        }
38        iter->destroy(iter);
39
40        return i;
41}
42
43void auction_save(struct auction_data *auction)
44{
45        int j;
46        StringBuf buf;
47        SqlStmt* stmt;
48
49        if( !auction )
50                return;
51
52        StringBuf_Init(&buf);
53        StringBuf_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'",
54                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);
55        for( j = 0; j < MAX_SLOTS; j++ )
56                StringBuf_Printf(&buf, ", `card%d` = '%d'", j, auction->item.card[j]);
57        StringBuf_Printf(&buf, " WHERE `auction_id` = '%d'", auction->auction_id);
58
59        stmt = SqlStmt_Malloc(sql_handle);
60        if( SQL_SUCCESS != SqlStmt_PrepareStr(stmt, StringBuf_Value(&buf))
61        ||  SQL_SUCCESS != SqlStmt_BindParam(stmt, 0, SQLDT_STRING, auction->seller_name, strnlen(auction->seller_name, NAME_LENGTH))
62        ||  SQL_SUCCESS != SqlStmt_BindParam(stmt, 1, SQLDT_STRING, auction->buyer_name, strnlen(auction->buyer_name, NAME_LENGTH))
63        ||  SQL_SUCCESS != SqlStmt_BindParam(stmt, 2, SQLDT_STRING, auction->item_name, strnlen(auction->item_name, ITEM_NAME_LENGTH))
64        ||  SQL_SUCCESS != SqlStmt_Execute(stmt) )
65        {
66                SqlStmt_ShowDebug(stmt);
67        }
68
69        SqlStmt_Free(stmt);
70        StringBuf_Destroy(&buf);
71}
72
73unsigned int auction_create(struct auction_data *auction)
74{
75        int j;
76        StringBuf buf;
77        SqlStmt* stmt;
78
79        if( !auction )
80                return false;
81
82        auction->timestamp = time(NULL) + (auction->hours * 3600);
83
84        StringBuf_Init(&buf);
85        StringBuf_Printf(&buf, "INSERT INTO `%s` (`seller_id`,`seller_name`,`buyer_id`,`buyer_name`,`price`,`buynow`,`hours`,`timestamp`,`nameid`,`item_name`,`type`,`refine`,`attribute`", auction_db);
86        for( j = 0; j < MAX_SLOTS; j++ )
87                StringBuf_Printf(&buf, ",`card%d`", j);
88        StringBuf_Printf(&buf, ") VALUES ('%d',?,'%d',?,'%d','%d','%d','%lu','%d',?,'%d','%d','%d'",
89                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);
90        for( j = 0; j < MAX_SLOTS; j++ )
91                StringBuf_Printf(&buf, ",'%d'", auction->item.card[j]);
92        StringBuf_AppendStr(&buf, ")");
93       
94        stmt = SqlStmt_Malloc(sql_handle);
95        if( SQL_SUCCESS != SqlStmt_PrepareStr(stmt, StringBuf_Value(&buf))
96        ||  SQL_SUCCESS != SqlStmt_BindParam(stmt, 0, SQLDT_STRING, auction->seller_name, strnlen(auction->seller_name, NAME_LENGTH))
97        ||  SQL_SUCCESS != SqlStmt_BindParam(stmt, 1, SQLDT_STRING, auction->buyer_name, strnlen(auction->buyer_name, NAME_LENGTH))
98        ||  SQL_SUCCESS != SqlStmt_BindParam(stmt, 2, SQLDT_STRING, auction->item_name, strnlen(auction->item_name, ITEM_NAME_LENGTH))
99        ||  SQL_SUCCESS != SqlStmt_Execute(stmt) )
100        {
101                SqlStmt_ShowDebug(stmt);
102                auction->auction_id = 0;
103        }
104        else
105        {
106                struct auction_data *auction_;
107                unsigned int tick = auction->hours * 3600000;
108
109                auction->item.amount = 1;
110                auction->item.identify = 1;
111
112                auction->auction_id = (unsigned int)SqlStmt_LastInsertId(stmt);
113                auction->auction_end_timer = add_timer( gettick() + tick , auction_end_timer, auction->auction_id, 0);
114                ShowInfo("New Auction %u | time left %u ms | By %s.\n", auction->auction_id, tick, auction->seller_name);
115
116                CREATE(auction_, struct auction_data, 1);
117                memcpy(auction_, auction, sizeof(struct auction_data));
118                idb_put(auction_db_, auction_->auction_id, auction_);
119        }
120
121        SqlStmt_Free(stmt);
122        StringBuf_Destroy(&buf);
123
124        return auction->auction_id;
125}
126
127static void mapif_Auction_message(int char_id, unsigned char result)
128{
129        unsigned char buf[74];
130       
131        WBUFW(buf,0) = 0x3854;
132        WBUFL(buf,2) = char_id;
133        WBUFL(buf,6) = result;
134        mapif_sendall(buf,7);
135}
136
137static int auction_end_timer(int tid, unsigned int tick, int id, intptr data)
138{
139        struct auction_data *auction;
140        if( (auction = (struct auction_data *)idb_get(auction_db_, id)) != NULL )
141        {
142                if( auction->buyer_id )
143                {
144                        mail_sendmail(0, "Auction Manager", auction->buyer_id, auction->buyer_name, "Auction", "Thanks, you won the auction!.", 0, &auction->item);
145                        mapif_Auction_message(auction->buyer_id, 6); // You have won the auction
146                        mail_sendmail(0, "Auction Manager", auction->seller_id, auction->seller_name, "Auction", "Payment for your auction!.", auction->price, NULL);
147                }
148                else
149                        mail_sendmail(0, "Auction Manager", auction->seller_id, auction->seller_name, "Auction", "No buyers have been found for your auction.", 0, &auction->item);
150               
151                ShowInfo("Auction End: id %u.\n", auction->auction_id);
152
153                auction->auction_end_timer = -1;
154                auction_delete(auction);
155        }
156
157        return 0;
158}
159
160void auction_delete(struct auction_data *auction)
161{
162        unsigned int auction_id = auction->auction_id;
163
164        if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `auction_id` = '%d'", auction_db, auction_id) )
165                Sql_ShowDebug(sql_handle);
166
167        if( auction->auction_end_timer != -1 )
168                delete_timer(auction->auction_end_timer, auction_end_timer);
169
170        idb_remove(auction_db_, auction_id);
171}
172
173void inter_auctions_fromsql(void)
174{
175        int i;
176        struct auction_data *auction;
177        struct item *item;
178        char *data;
179        StringBuf buf;
180        unsigned int tick = gettick(), endtick;
181        time_t now = time(NULL);
182
183        StringBuf_Init(&buf);
184        StringBuf_AppendStr(&buf, "SELECT `auction_id`,`seller_id`,`seller_name`,`buyer_id`,`buyer_name`,"
185                "`price`,`buynow`,`hours`,`timestamp`,`nameid`,`item_name`,`type`,`refine`,`attribute`");
186        for( i = 0; i < MAX_SLOTS; i++ )
187                StringBuf_Printf(&buf, ",`card%d`", i);
188        StringBuf_Printf(&buf, " FROM `%s` ORDER BY `auction_id` DESC", auction_db);
189
190        if( SQL_ERROR == Sql_Query(sql_handle, StringBuf_Value(&buf)) )
191                Sql_ShowDebug(sql_handle);
192
193        StringBuf_Destroy(&buf);
194
195        while( SQL_SUCCESS == Sql_NextRow(sql_handle) )
196        {
197                CREATE(auction, struct auction_data, 1);
198                Sql_GetData(sql_handle, 0, &data, NULL); auction->auction_id = atoi(data);
199                Sql_GetData(sql_handle, 1, &data, NULL); auction->seller_id = atoi(data);
200                Sql_GetData(sql_handle, 2, &data, NULL); safestrncpy(auction->seller_name, data, NAME_LENGTH);
201                Sql_GetData(sql_handle, 3, &data, NULL); auction->buyer_id = atoi(data);
202                Sql_GetData(sql_handle, 4, &data, NULL); safestrncpy(auction->buyer_name, data, NAME_LENGTH);
203                Sql_GetData(sql_handle, 5, &data, NULL); auction->price = atoi(data);
204                Sql_GetData(sql_handle, 6, &data, NULL); auction->buynow = atoi(data);
205                Sql_GetData(sql_handle, 7, &data, NULL); auction->hours = atoi(data);
206                Sql_GetData(sql_handle, 8, &data, NULL); auction->timestamp = atoi(data);
207
208                item = &auction->item;
209                Sql_GetData(sql_handle, 9, &data, NULL); item->nameid = atoi(data);
210                Sql_GetData(sql_handle,10, &data, NULL); safestrncpy(auction->item_name, data, ITEM_NAME_LENGTH);
211                Sql_GetData(sql_handle,11, &data, NULL); auction->type = atoi(data);
212
213                Sql_GetData(sql_handle,12, &data, NULL); item->refine = atoi(data);
214                Sql_GetData(sql_handle,13, &data, NULL); item->attribute = atoi(data);
215
216                item->identify = 1;
217                item->amount = 1;
218
219                for( i = 0; i < MAX_SLOTS; i++ )
220                {
221                        Sql_GetData(sql_handle, 14 + i, &data, NULL);
222                        item->card[i] = atoi(data);
223                }
224
225                if( auction->timestamp > now )
226                        endtick = ((unsigned int)(auction->timestamp - now) * 1000) + tick;
227                else
228                        endtick = tick + 10000; // 10 Second's to process ended auctions
229
230                auction->auction_end_timer = add_timer(endtick, auction_end_timer, auction->auction_id, 0);
231                idb_put(auction_db_, auction->auction_id, auction);
232        }
233
234        Sql_FreeResult(sql_handle);
235}
236
237static void mapif_Auction_sendlist(int fd, int char_id, short count, short pages, unsigned char *buf)
238{
239        int len = (sizeof(struct auction_data) * count) + 12;
240
241        WFIFOHEAD(fd, len);
242        WFIFOW(fd,0) = 0x3850;
243        WFIFOW(fd,2) = len;
244        WFIFOL(fd,4) = char_id;
245        WFIFOW(fd,8) = count;
246        WFIFOW(fd,10) = pages;
247        memcpy(WFIFOP(fd,12), buf, len - 12);
248        WFIFOSET(fd,len);
249}
250
251static void mapif_parse_Auction_requestlist(int fd)
252{
253        char searchtext[NAME_LENGTH];
254        int char_id = RFIFOL(fd,4), len = sizeof(struct auction_data);
255        int price = RFIFOL(fd,10);
256        short type = RFIFOW(fd,8), page = max(1,RFIFOW(fd,14));
257        unsigned char buf[5 * sizeof(struct auction_data)];
258        DBIterator* iter;
259        DBKey key;
260        struct auction_data *auction;
261        short i = 0, j = 0, pages = 1;
262
263        memcpy(searchtext, RFIFOP(fd,16), NAME_LENGTH);
264
265        iter = auction_db_->iterator(auction_db_);
266        for( auction = (struct auction_data*)iter->first(iter,&key); iter->exists(iter); auction = (struct auction_data*)iter->next(iter,&key) )
267        {
268                if( (type == 0 && auction->type != IT_ARMOR && auction->type != IT_PETARMOR) || 
269                        (type == 1 && auction->type != IT_WEAPON) ||
270                        (type == 2 && auction->type != IT_CARD) ||
271                        (type == 3 && auction->type != IT_ETC) ||
272                        (type == 4 && !strstr(auction->item_name, searchtext)) ||
273                        (type == 5 && auction->price > price) ||
274                        (type == 6 && auction->seller_id != char_id) ||
275                        (type == 7 && auction->buyer_id != char_id) )
276                        continue;
277
278                i++;
279                if( i > 5 )
280                { // Counting Pages of Total Results (5 Results per Page)
281                        pages++;
282                        i = 1; // First Result of This Page
283                }
284
285                if( page != pages )
286                        continue; // This is not the requested Page
287
288                memcpy(WBUFP(buf, j * len), auction, len);
289                j++; // Found Results
290        }
291        iter->destroy(iter);
292
293        mapif_Auction_sendlist(fd, char_id, j, pages, buf);
294}
295
296static void mapif_Auction_register(int fd, struct auction_data *auction)
297{
298        int len = sizeof(struct auction_data) + 4;
299
300        WFIFOHEAD(fd,len);
301        WFIFOW(fd,0) = 0x3851;
302        WFIFOW(fd,2) = len;
303        memcpy(WFIFOP(fd,4), auction, sizeof(struct auction_data));
304        WFIFOSET(fd,len);
305}
306
307static void mapif_parse_Auction_register(int fd)
308{
309        struct auction_data auction;
310        if( RFIFOW(fd,2) != sizeof(struct auction_data) + 4 )
311                return;
312
313        memcpy(&auction, RFIFOP(fd,4), sizeof(struct auction_data));
314        if( auction_count(auction.seller_id, false) < 5 )
315                auction.auction_id = auction_create(&auction);
316
317        mapif_Auction_register(fd, &auction);
318}
319
320static void mapif_Auction_cancel(int fd, int char_id, unsigned char result)
321{
322        WFIFOHEAD(fd,7);
323        WFIFOW(fd,0) = 0x3852;
324        WFIFOL(fd,2) = char_id;
325        WFIFOB(fd,6) = result;
326        WFIFOSET(fd,7);
327}
328
329static void mapif_parse_Auction_cancel(int fd)
330{
331        int char_id = RFIFOL(fd,2), auction_id = RFIFOL(fd,6);
332        struct auction_data *auction;
333
334        if( (auction = (struct auction_data *)idb_get(auction_db_, auction_id)) == NULL )
335        {
336                mapif_Auction_cancel(fd, char_id, 1); // Bid Number is Incorrect
337                return;
338        }
339
340        if( auction->seller_id != char_id )
341        {
342                mapif_Auction_cancel(fd, char_id, 2); // You cannot end the auction
343                return;
344        }
345
346        if( auction->buyer_id > 0 )
347        {
348                mapif_Auction_cancel(fd, char_id, 3); // An auction with at least one bidder cannot be canceled
349                return;
350        }
351
352        mail_sendmail(0, "Auction Manager", auction->seller_id, auction->seller_name, "Auction", "Auction canceled.", 0, &auction->item);
353        auction_delete(auction);
354
355        mapif_Auction_cancel(fd, char_id, 0); // The auction has been canceled
356}
357
358static void mapif_Auction_close(int fd, int char_id, unsigned char result)
359{
360        WFIFOHEAD(fd,7);
361        WFIFOW(fd,0) = 0x3853;
362        WFIFOL(fd,2) = char_id;
363        WFIFOB(fd,6) = result;
364        WFIFOSET(fd,7);
365}
366
367static void mapif_parse_Auction_close(int fd)
368{
369        int char_id = RFIFOL(fd,2), auction_id = RFIFOL(fd,6);
370        struct auction_data *auction;
371
372        if( (auction = (struct auction_data *)idb_get(auction_db_, auction_id)) == NULL )
373        {
374                mapif_Auction_close(fd, char_id, 2); // Bid Number is Incorrect
375                return;
376        }
377
378        if( auction->buyer_id == 0 )
379        {
380                mapif_Auction_close(fd, char_id, 1); // You cannot end the auction
381                return;
382        }
383
384        // Send Money to Seller
385        mail_sendmail(0, "Auction Manager", auction->seller_id, auction->seller_name, "Auction", "Auction closed.", auction->price, NULL);
386        // Send Item to Buyer
387        mail_sendmail(0, "Auction Manager", auction->buyer_id, auction->buyer_name, "Auction", "Auction winner.", 0, &auction->item);
388        mapif_Auction_message(auction->buyer_id, 6); // You have won the auction
389        auction_delete(auction);
390
391        mapif_Auction_close(fd, char_id, 0); // You have ended the auction
392}
393
394static void mapif_Auction_bid(int fd, int char_id, int bid, unsigned char result)
395{
396        WFIFOHEAD(fd,11);
397        WFIFOW(fd,0) = 0x3855;
398        WFIFOL(fd,2) = char_id;
399        WFIFOL(fd,6) = bid; // To Return Zeny
400        WFIFOB(fd,10) = result;
401        WFIFOSET(fd,11);
402}
403
404static void mapif_parse_Auction_bid(int fd)
405{
406        int char_id = RFIFOL(fd,4), bid = RFIFOL(fd,12);
407        unsigned int auction_id = RFIFOL(fd,8);
408        struct auction_data *auction;
409
410        if( (auction = (struct auction_data *)idb_get(auction_db_, auction_id)) == NULL || auction->price >= bid || auction->seller_id == char_id )
411        {
412                mapif_Auction_bid(fd, char_id, bid, 0); // You have failed to bid in the auction
413                return;
414        }
415
416        if( auction_count(char_id, true) > 4 && bid < auction->buynow && auction->buyer_id != char_id )
417        {
418                mapif_Auction_bid(fd, char_id, bid, 9); // You cannot place more than 5 bids at a time
419                return;
420        }
421
422        if( auction->buyer_id > 0 )
423        { // Send Money back to the previous Buyer
424                if( auction->buyer_id != char_id )
425                {
426                        mail_sendmail(0, "Auction Manager", auction->buyer_id, auction->buyer_name, "Auction", "Someone has placed a higher bid.", auction->price, NULL);
427                        mapif_Auction_message(auction->buyer_id, 7); // You have failed to win the auction
428                }
429                else
430                        mail_sendmail(0, "Auction Manager", auction->buyer_id, auction->buyer_name, "Auction", "You have placed a higher bid.", auction->price, NULL);
431        }
432
433        auction->buyer_id = char_id;
434        safestrncpy(auction->buyer_name, (char*)RFIFOP(fd,16), NAME_LENGTH);
435        auction->price = bid;
436
437        if( bid >= auction->buynow )
438        { // Automatic won the auction
439                mapif_Auction_bid(fd, char_id, bid - auction->buynow, 1); // You have successfully bid in the auction
440
441                mail_sendmail(0, "Auction Manager", auction->buyer_id, auction->buyer_name, "Auction", "You have won the auction.", 0, &auction->item);
442                mapif_Auction_message(char_id, 6); // You have won the auction
443                mail_sendmail(0, "Auction Manager", auction->seller_id, auction->seller_name, "Auction", "Payment for your auction!.", auction->buynow, NULL);
444
445                auction_delete(auction);
446                return;
447        }
448
449        auction_save(auction);
450
451        mapif_Auction_bid(fd, char_id, 0, 1); // You have successfully bid in the auction
452}
453
454/*==========================================
455 * Packets From Map Server
456 *------------------------------------------*/
457int inter_auction_parse_frommap(int fd)
458{
459        switch(RFIFOW(fd,0))
460        {
461                case 0x3050: mapif_parse_Auction_requestlist(fd); break;
462                case 0x3051: mapif_parse_Auction_register(fd); break;
463                case 0x3052: mapif_parse_Auction_cancel(fd); break;
464                case 0x3053: mapif_parse_Auction_close(fd); break;
465                case 0x3055: mapif_parse_Auction_bid(fd); break;
466                default:
467                        return 0;
468        }
469        return 1;
470}
471
472int inter_auction_sql_init(void)
473{
474        auction_db_ = idb_alloc(DB_OPT_RELEASE_DATA);
475        inter_auctions_fromsql();
476
477        return 0;
478}
479
480void inter_auction_sql_final(void)
481{
482        auction_db_->destroy(auction_db_,NULL);
483
484        return;
485}
Note: See TracBrowser for help on using the browser.