Index: cashshop.c
===================================================================
--- cashshop.c (revision 17301)
+++ cashshop.c (working copy)
@@ -15,13 +15,59 @@
#include <string.h> // memset
#include <stdlib.h> // atoi
-static int cashshop_parse_dbrow( char** str, const char* source, int line );
-
struct cash_item_db cash_shop_items[CASHSHOP_TAB_SEARCH];
extern char item_cash_db_db[32];
extern char item_cash_db2_db[32];
+/*
+ * Read one line of db and assign it to ram
+ * return
+ * 0 = failure
+ * 1 = succes
+ */
+static int cashshop_parse_dbrow( char** str, const char* source, int line ){
+ uint32 nameid = atoi( str[1] );
+
+ if( itemdb_exists( nameid ) ){
+ uint16 tab = atoi( str[0] );
+ uint32 price = atoi( str[2] );
+ struct cash_item_data* cid;
+ int j;
+
+ if( tab > CASHSHOP_TAB_SEARCH ){
+ ShowWarning( "cashshop_parse_dbrow: Invalid tab %d in line %d of \"%s\", skipping.\n", tab, line, source );
+ return 0;
+ }else if( price < 1 ){
+ ShowWarning( "cashshop_parse_dbrow: Invalid price %d in line %d of \"%s\", skipping.\n", price, line, source );
+ return 0;
+ }
+
+ ARR_FIND( 0, cash_shop_items[tab].count, j, nameid == cash_shop_items[tab].item[j]->nameid );
+
+ if( j == cash_shop_items[tab].count ){
+ RECREATE( cash_shop_items[tab].item, struct cash_item_data *, ++cash_shop_items[tab].count );
+ CREATE( cash_shop_items[tab].item[ cash_shop_items[tab].count - 1], struct cash_item_data, 1 );
+ cid = cash_shop_items[tab].item[ cash_shop_items[tab].count - 1];
+ }else{
+ cid = cash_shop_items[tab].item[j];
+ }
+
+ cid->nameid = nameid;
+ cid->price = price;
+
+ return 1;
+ }else{
+ ShowWarning( "cashshop_parse_dbrow: Invalid id %d in line %d of \"%s\", skipping.\n", nameid, line, source );
+ }
+
+ return 0;
+}
+
+/*
+ * Read database from txt format,
+ * parse line and send them to parse_dbrow
+ */
static void cashshop_read_db_txt( void ){
const char* filename[] = { DBPATH"item_cash_db.txt", "item_cash_db2.txt" };
int fi;
@@ -88,6 +134,10 @@
}
}
+/*
+ * Read database from sql format,
+ * parse line and send them to parse_dbrow
+ */
static int cashshop_read_db_sql( void ){
const char* cash_db_name[] = { item_cash_db_db, item_cash_db2_db };
int fi;
@@ -128,6 +178,11 @@
return 0;
}
+/*
+ * Read cashshop database main
+ * choose wich reader to use depending of config :
+ * conf/map_athena.conf db_use_sqldbs
+ */
static void cashshop_read_db( void ){
if( db_use_sqldbs ){
cashshop_read_db_sql();
@@ -136,44 +191,12 @@
}
}
-static int cashshop_parse_dbrow( char** str, const char* source, int line ){
- uint32 nameid = atoi( str[1] );
-
- if( itemdb_exists( nameid ) ){
- uint16 tab = atoi( str[0] );
- uint32 price = atoi( str[2] );
- struct cash_item_data* cid;
- int j;
-
- if( tab > CASHSHOP_TAB_SEARCH ){
- ShowWarning( "cashshop_parse_dbrow: Invalid tab %d in line %d of \"%s\", skipping.\n", tab, line, source );
- return 0;
- }else if( price < 1 ){
- ShowWarning( "cashshop_parse_dbrow: Invalid price %d in line %d of \"%s\", skipping.\n", price, line, source );
- return 0;
- }
-
- ARR_FIND( 0, cash_shop_items[tab].count, j, nameid == cash_shop_items[tab].item[j]->nameid );
-
- if( j == cash_shop_items[tab].count ){
- RECREATE( cash_shop_items[tab].item, struct cash_item_data *, ++cash_shop_items[tab].count );
- CREATE( cash_shop_items[tab].item[ cash_shop_items[tab].count - 1], struct cash_item_data, 1 );
- cid = cash_shop_items[tab].item[ cash_shop_items[tab].count - 1];
- }else{
- cid = cash_shop_items[tab].item[j];
- }
-
- cid->nameid = nameid;
- cid->price = price;
-
- return 1;
- }else{
- ShowWarning( "cashshop_parse_dbrow: Invalid id %d in line %d of \"%s\", skipping.\n", nameid, line, source );
- }
-
- return 0;
-}
-
+/*
+ * Attempt to buy a cashshop item from list
+ * check if valid transaction and if user have enough place to receive item
+ * if yes take cashpoint and give else
+ * else return clif_error
+ */
void cashshop_buylist( struct map_session_data* sd, uint32 kafrapoints, int n, uint16* item_list ){
uint32 totalcash = 0;
uint32 totalweight = 0;
@@ -227,10 +250,7 @@
totalweight += itemdb_weight( nameid ) * quantity;
}
- if( ( totalcash - kafrapoints ) > sd->cashPoints || kafrapoints > sd->kafraPoints ){
- clif_cashshop_result( sd, 0, CASHSHOP_RESULT_ERROR_SHORTTAGE_CASH );
- return;
- }else if( ( totalweight + sd->weight ) > sd->max_weight ){
+ if( ( totalweight + sd->weight ) > sd->max_weight ){
clif_cashshop_result( sd, 0, CASHSHOP_RESULT_ERROR_INVENTORY_WEIGHT );
return;
}else if( pc_inventoryblank( sd ) < new_ ){
@@ -238,7 +258,10 @@
return;
}
- pc_paycash( sd, totalcash, kafrapoints, LOG_TYPE_CASH );
+ if(pc_paycash( sd, totalcash, kafrapoints, LOG_TYPE_CASH ) < 0){
+ clif_cashshop_result( sd, 0, CASHSHOP_RESULT_ERROR_SHORTTAGE_CASH );
+ return;
+ }
for( i = 0; i < n; ++i ){
uint32 nameid = *( item_list + i * 5 );
@@ -273,11 +296,19 @@
clif_cashshop_result( sd, 0, CASHSHOP_RESULT_SUCCESS );
}
+/*
+ * Reload database of cashshop
+ * by destroying and read it again
+ */
void cashshop_reloaddb( void ){
do_final_cashshop();
do_init_cashshop();
}
+/*
+ * Destroy cashshop class
+ * Close all and cleanup
+ */
int do_final_cashshop( void ){
int tab, i;
@@ -285,15 +316,18 @@
for( i = 0; i < cash_shop_items[tab].count; i++ ){
aFree( cash_shop_items[tab].item[i] );
}
-
aFree( cash_shop_items[tab].item );
}
-
memset( cash_shop_items, 0, sizeof( cash_shop_items ) );
return 0;
}
+/*
+ * Initialise cashshop class
+ * return
+ * 0 : success
+ */
int do_init_cashshop( void ){
cashshop_read_db();