From: John Hodge Date: Fri, 21 Oct 2016 07:28:30 +0000 (+0800) Subject: server - Handle cokebank errors when dispensing X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=45bee621d08d0471e92dd7b2092a635af8cc7f13;p=tpg%2Fopendispense2.git server - Handle cokebank errors when dispensing --- diff --git a/src/server/dispense.c b/src/server/dispense.c index 8ec7c2e..c994e33 100644 --- a/src/server/dispense.c +++ b/src/server/dispense.c @@ -45,6 +45,23 @@ int DispenseItem(int ActualUser, int User, tItem *Item) // Get username for debugging username = Bank_GetAcctName(User); + // Ordering: Pay, Drop. Worst case requires a refund, other ordering leads to drops when payment fails. + + // Take away money + if( Item->Price ) + { + char *reason; + reason = mkstr("Dispense - %s:%i %s", handler->Name, Item->ID, Item->Name); + if( _Transfer( User, salesAcct, Item->Price, reason ) != 0 ) { + Log_Error("Dispense failed (%s dispensing %s:%i '%s') - Cokebank error!", + username, Item->Handler->Name, Item->ID, Item->Name); + free(reason); + free( username ); + return -1; // -1: Unknown error + } + free(reason); + } + // Actually do the dispense if( handler->DoDispense ) { ret = handler->DoDispense( User, Item->ID ); @@ -52,19 +69,10 @@ int DispenseItem(int ActualUser, int User, tItem *Item) Log_Error("Dispense failed (%s dispensing %s:%i '%s')", username, Item->Handler->Name, Item->ID, Item->Name); free( username ); - return -1; // 1: Unknown Error again + return -1; // -1: Unknown Error } } - // Take away money - if( Item->Price ) - { - char *reason; - reason = mkstr("Dispense - %s:%i %s", handler->Name, Item->ID, Item->Name); - _Transfer( User, salesAcct, Item->Price, reason ); - free(reason); - } - actualUsername = Bank_GetAcctName(ActualUser); // And log that it happened @@ -227,7 +235,8 @@ int DispenseSet(int ActualUser, int User, int Balance, const char *ReasonGiven, int curBal = Bank_GetBalance(User); char *byName, *dstName; - _Transfer( Bank_GetAcctByName(COKEBANK_DEBT_ACCT,1), User, Balance-curBal, ReasonGiven ); + if( Bank_Transfer( Bank_GetAcctByName(COKEBANK_DEBT_ACCT,1), User, Balance-curBal, ReasonGiven ) ) + return -1; byName = Bank_GetAcctName(ActualUser); dstName = Bank_GetAcctName(User);