X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=src%2Fserver%2Fdispense.c;h=c994e335f037c8e5a1a54a38109821b91f55ab34;hb=45bee621d08d0471e92dd7b2092a635af8cc7f13;hp=52d978cdfb32deb0ae529c0b3f9e4a7ac2e16345;hpb=df71d48cc0082a5333203384cfbce040cf54a155;p=tpg%2Fopendispense2.git diff --git a/src/server/dispense.c b/src/server/dispense.c index 52d978c..c994e33 100644 --- a/src/server/dispense.c +++ b/src/server/dispense.c @@ -3,10 +3,12 @@ #include "common.h" #include #include +#include int _GetMinBalance(int Account); int _CanTransfer(int Source, int Destination, int Ammount); int _Transfer(int Source, int Destination, int Ammount, const char *Reason); + int _GetSalesAcct(tItem *Item); // === CODE === /** @@ -20,7 +22,9 @@ int DispenseItem(int ActualUser, int User, tItem *Item) tHandler *handler; char *username, *actualUsername; - salesAcct = Bank_GetAcctByName(COKEBANK_SALES_ACCT); + handler = Item->Handler; + + salesAcct = _GetSalesAcct(Item); // Check if the user can afford it if( Item->Price && !_CanTransfer(User, salesAcct, Item->Price) ) @@ -28,7 +32,9 @@ int DispenseItem(int ActualUser, int User, tItem *Item) return 2; // 2: No balance } - handler = Item->Handler; + // HACK: Naming a slot "dead" disables it + if( strcmp(Item->Name, "dead") == 0 ) + return 1; // Check if the dispense is possible if( handler->CanDispense ) { @@ -39,39 +45,91 @@ int DispenseItem(int ActualUser, int User, tItem *Item) // Get username for debugging username = Bank_GetAcctName(User); - // Actually do the dispense - if( handler->DoDispense ) { - ret = handler->DoDispense( User, Item->ID ); - if(ret) { - Log_Error("Dispense failed (%s dispensing '%s' - %ic)", - username, Item->Name, Item->Price); - free( username ); - return -1; // 1: Unknown Error again - } - } + // 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); - _Transfer( User, salesAcct, Item->Price, reason ); + 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 ); + if(ret) { + Log_Error("Dispense failed (%s dispensing %s:%i '%s')", + username, Item->Handler->Name, Item->ID, Item->Name); + free( username ); + return -1; // -1: Unknown Error + } + } + actualUsername = Bank_GetAcctName(ActualUser); // And log that it happened - Log_Info("dispense '%s' (%s:%i) for %s by %s [cost %i, balance %i]", - Item->Name, handler->Name, Item->ID, - username, actualUsername, Item->Price, Bank_GetBalance(User) - ); + if( gbNoCostMode ) + { + // Special format for zero cost dispenses + Log_Info("test dispense '%s' (%s:%i) for %s by %s [no change]", + Item->Name, handler->Name, Item->ID, + username, actualUsername + ); + } + else + { + Log_Info("dispense '%s' (%s:%i) for %s by %s [cost %i, balance %i]", + Item->Name, handler->Name, Item->ID, + username, actualUsername, Item->Price, Bank_GetBalance(User) + ); + } free( username ); free( actualUsername ); return 0; // 0: EOK } +/** + * \brief Refund a dispense + */ +int DispenseRefund(int ActualUser, int DestUser, tItem *Item, int OverridePrice) +{ + int ret; + int src_acct, price; + char *username, *actualUsername; + + src_acct = _GetSalesAcct(Item); + + if( OverridePrice > 0 ) + price = OverridePrice; + else + price = Item->Price; + + ret = _Transfer( src_acct, DestUser, price, "Refund"); + if(ret) return ret; + + username = Bank_GetAcctName(DestUser); + actualUsername = Bank_GetAcctName(ActualUser); + + Log_Info("refund '%s' (%s:%i) to %s by %s [cost %i, balance %i]", + Item->Name, Item->Handler->Name, Item->ID, + username, actualUsername, price, Bank_GetBalance(DestUser) + ); + + free(username); + free(actualUsername); + + return 0; +} + /** * \brief Give money from one user to another */ @@ -81,6 +139,10 @@ int DispenseGive(int ActualUser, int SrcUser, int DestUser, int Ammount, const c char *actualUsername; char *srcName, *dstName; + // HACK: Naming a slot "dead" disables it (catch for snack) + if( strcmp(ReasonGiven, "dead") == 0 ) + return 1; + if( Ammount < 0 ) return 1; // Um... negative give? Not on my watch! ret = _Transfer( SrcUser, DestUser, Ammount, ReasonGiven ); @@ -91,8 +153,8 @@ int DispenseGive(int ActualUser, int SrcUser, int DestUser, int Ammount, const c srcName = Bank_GetAcctName(SrcUser); dstName = Bank_GetAcctName(DestUser); - Log_Info("give %i to %s from %s by %s [balances %i, %i] - %s", - Ammount, dstName, srcName, actualUsername, + Log_Info("give %i from %s to %s by %s [balances %i, %i] - %s", + Ammount, srcName, dstName, actualUsername, Bank_GetBalance(SrcUser), Bank_GetBalance(DestUser), ReasonGiven ); @@ -104,6 +166,42 @@ int DispenseGive(int ActualUser, int SrcUser, int DestUser, int Ammount, const c return 0; } +#if 0 // Dead Code +/** + * \brief Move money from one user to another (Admin Only) + */ +int DispenseTransfer(int ActualUser, int SrcUser, int DestUser, int Ammount, const char *ReasonGiven) +{ + int ret; + char *actualUsername; + char *srcName, *dstName; + + // Make sure the user is an admin + if( !(Bank_GetFlags(ActualUser) & USER_FLAG_ADMIN) ) + return 1; + + ret = _Transfer( SrcUser, DestUser, Ammount, ReasonGiven ); + if(ret) return 2; // No Balance + + + actualUsername = Bank_GetAcctName(ActualUser); + srcName = Bank_GetAcctName(SrcUser); + dstName = Bank_GetAcctName(DestUser); + + Log_Info("move %i from %s to %s by %s [balances %i, %i] - %s", + Ammount, srcName, dstName, actualUsername, + Bank_GetBalance(SrcUser), Bank_GetBalance(DestUser), + ReasonGiven + ); + + free(srcName); + free(dstName); + free(actualUsername); + + return 0; +} +#endif + /** * \brief Add money to an account */ @@ -112,7 +210,11 @@ int DispenseAdd(int ActualUser, int User, int Ammount, const char *ReasonGiven) int ret; char *dstName, *byName; - ret = _Transfer( Bank_GetAcctByName(COKEBANK_DEBT_ACCT), User, Ammount, ReasonGiven ); +#if DISPENSE_ADD_BELOW_MIN + ret = _Transfer( Bank_GetAcctByName(COKEBANK_ADDSRC_ACCT,1), User, Ammount, ReasonGiven ); +#else + ret = Bank_Transfer( Bank_GetAcctByName(COKEBANK_ADDSRC_ACCT,1), User, Ammount, ReasonGiven ); +#endif if(ret) return 2; byName = Bank_GetAcctName(ActualUser); @@ -128,20 +230,22 @@ int DispenseAdd(int ActualUser, int User, int Ammount, const char *ReasonGiven) return 0; } -int DispenseSet(int ActualUser, int User, int Balance, const char *ReasonGiven) +int DispenseSet(int ActualUser, int User, int Balance, const char *ReasonGiven, int *OrigBalance) { int curBal = Bank_GetBalance(User); char *byName, *dstName; - _Transfer( Bank_GetAcctByName(COKEBANK_DEBT_ACCT), 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); - Log_Info("set balance of %s to %i by %s [balance %i] - %s", - dstName, Balance, byName, Bank_GetBalance(User), ReasonGiven + Log_Info("set balance of %s to %i by %s [was %i, balance %i] - %s", + dstName, Balance, byName, curBal, Bank_GetBalance(User), ReasonGiven ); + *OrigBalance = curBal; free(byName); free(dstName); @@ -158,7 +262,7 @@ int DispenseDonate(int ActualUser, int User, int Ammount, const char *ReasonGive if( Ammount < 0 ) return 2; - ret = _Transfer( User, Bank_GetAcctByName(COKEBANK_DEBT_ACCT), Ammount, ReasonGiven ); + ret = _Transfer( User, Bank_GetAcctByName(COKEBANK_DONATE_ACCT,1), Ammount, ReasonGiven ); if(ret) return 2; byName = Bank_GetAcctName(ActualUser); @@ -174,19 +278,61 @@ int DispenseDonate(int ActualUser, int User, int Ammount, const char *ReasonGive return 0; } +int DispenseUpdateItem(int User, tItem *Item, const char *NewName, int NewPrice) +{ + char *username; + + // Sanity checks + if( NewPrice < 0 ) return 2; + if( !Item ) return 2; + if( strlen(NewName) < 1 ) return 2; + + // Update the item + free(Item->Name); + Item->Name = strdup(NewName); + Item->Price = NewPrice; + + username = Bank_GetAcctName(User); + + Log_Info("item %s:%i updated to '%s' %i by %s", + Item->Handler->Name, Item->ID, + NewName, NewPrice, username + ); + + free(username); + + // Update item file + Items_UpdateFile(); + + return 0; +} + // --- Internal Functions --- int _GetMinBalance(int Account) { int flags = Bank_GetFlags(Account); + // Evil little piece of HACK: + // root's balance cannot be changed by any of the above functions + // - Stops dispenses as root by returning insufficent balance. + { + char *username = Bank_GetAcctName(Account); + if( strcmp(username, "root") == 0 ) + { + free(username); + return INT_MAX; + } + free(username); + } + // - Internal accounts have no lower bound if( flags & USER_FLAG_INTERNAL ) return INT_MIN; - // Admin to -$10 - if( flags & USER_FLAG_ADMIN ) return -1000; + // Admin to -$50 +// if( flags & USER_FLAG_ADMIN ) return -5000; - // Coke to -$5 - if( flags & USER_FLAG_COKE ) return -500; + // Coke to -$20 +// if( flags & USER_FLAG_COKE ) return -2000; // Anyone else, non-negative return 0; @@ -194,17 +340,20 @@ int _GetMinBalance(int Account) /** * \brief Check if a transfer is possible + * \return Boolean success */ int _CanTransfer(int Source, int Destination, int Ammount) { +// if( Bank_GetFlags(Source) & USER_FLAG_DISABLED ) +// return 0; if( Ammount > 0 ) { - if( Bank_GetBalance(Source) + Ammount < _GetMinBalance(Source) ) + if( Bank_GetBalance(Source) - Ammount < _GetMinBalance(Source) ) return 0; } else { - if( Bank_GetBalance(Destination) - Ammount < _GetMinBalance(Destination) ) + if( Bank_GetBalance(Destination) + Ammount < _GetMinBalance(Destination) ) return 0; } return 1; @@ -216,3 +365,11 @@ int _Transfer(int Source, int Destination, int Ammount, const char *Reason) return 1; return Bank_Transfer(Source, Destination, Ammount, Reason); } + +int _GetSalesAcct(tItem *Item) +{ + char string[sizeof(COKEBANK_SALES_PREFIX)+strlen(Item->Handler->Name)]; + strcpy(string, COKEBANK_SALES_PREFIX); + strcat(string, Item->Handler->Name); + return Bank_GetAcctByName(string, 1); +}