server - Handle cokebank errors when dispensing
authorJohn Hodge <[email protected]>
Fri, 21 Oct 2016 07:28:30 +0000 (15:28 +0800)
committerJohn Hodge <[email protected]>
Fri, 21 Oct 2016 07:28:30 +0000 (15:28 +0800)
src/server/dispense.c

index 8ec7c2e..c994e33 100644 (file)
@@ -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);

UCC git Repository :: git.ucc.asn.au