X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;ds=sidebyside;f=src%2Fserver%2Fdispense.c;h=81d49dca8ef5dd23ea8c583055d1616b2a95e5f5;hb=2402457fd4ea286febae34182d9a9f3b63cb6565;hp=77f07accd1b8ec5dc08ea5145c08cfca919595f2;hpb=ca788d1ed62b100c213fa9de432f969d85b136e9;p=tpg%2Fopendispense2.git diff --git a/src/server/dispense.c b/src/server/dispense.c index 77f07ac..81d49dc 100644 --- a/src/server/dispense.c +++ b/src/server/dispense.c @@ -2,6 +2,10 @@ */ #include "common.h" #include +#include + + int _GetMinBalance(int Account); + int _Transfer(int Source, int Destination, int Ammount, const char *Reason); // === CODE === /** @@ -9,11 +13,11 @@ * * The core of the dispense system, I kinda like it :) */ -int DispenseItem(int User, tItem *Item) +int DispenseItem(int ActualUser, int User, tItem *Item) { int ret; tHandler *handler; - char *username; + char *username, *actualUsername; char *reason; handler = Item->Handler; @@ -27,12 +31,12 @@ int DispenseItem(int User, tItem *Item) // Subtract the balance reason = mkstr("Dispense - %s:%i %s", handler->Name, Item->ID, Item->Name); if( !reason ) reason = Item->Name; // TODO: Should I instead return an error? - ret = Transfer( User, GetUserID(COKEBANK_SALES_ACCT), Item->Price, reason); + ret = _Transfer( User, Bank_GetAcctByName(COKEBANK_SALES_ACCT), Item->Price, reason); free(reason); if(ret) return 2; // 2: No balance // Get username for debugging - username = GetUserName(User); + username = Bank_GetAcctName(User); // Actually do the dispense if( handler->DoDispense ) { @@ -40,51 +44,137 @@ int DispenseItem(int User, tItem *Item) if(ret) { Log_Error("Dispense failed after deducting cost (%s dispensing %s - %ic)", username, Item->Name, Item->Price); - Transfer( GetUserID(COKEBANK_SALES_ACCT), User, Item->Price, "rollback" ); + _Transfer( Bank_GetAcctByName(COKEBANK_SALES_ACCT), User, Item->Price, "rollback" ); free( username ); return -1; // 1: Unkown Error again } } + actualUsername = Bank_GetAcctName(ActualUser); + // And log that it happened - Log_Info("dispense %s (%s:%i) by %s [cost %i, balance %i cents]", + Log_Info("dispense '%s' (%s:%i) for %s by %s [cost %i, balance %i]", Item->Name, handler->Name, Item->ID, - username, Item->Price, GetBalance(User) + username, actualUsername, Item->Price, Bank_GetBalance(User) ); free( username ); + free( actualUsername ); return 0; // 0: EOK } /** * \brief Give money from one user to another */ -int DispenseGive(int SrcUser, int DestUser, int Ammount, const char *ReasonGiven) +int DispenseGive(int ActualUser, int SrcUser, int DestUser, int Ammount, const char *ReasonGiven) { int ret; + char *actualUsername; + char *srcName, *dstName; + if( Ammount < 0 ) return 1; // Um... negative give? Not on my watch! - ret = Transfer( SrcUser, DestUser, Ammount, ReasonGiven ); + ret = _Transfer( SrcUser, DestUser, Ammount, ReasonGiven ); if(ret) return 2; // No Balance - Log_Info("give %i to %s from %s (%s)", - Ammount, GetUserName(DestUser), GetUserName(SrcUser), ReasonGiven + + actualUsername = Bank_GetAcctName(ActualUser); + 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, + Bank_GetBalance(SrcUser), Bank_GetBalance(DestUser), + ReasonGiven + ); + + free(srcName); + free(dstName); + free(actualUsername); + + return 0; +} + +/** + * \brief Add money to an account + */ +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(ret) return 2; + + byName = Bank_GetAcctName(ActualUser); + dstName = Bank_GetAcctName(User); + + Log_Info("add %i to %s by %s [balance %i] - %s", + Ammount, dstName, byName, Bank_GetBalance(User), ReasonGiven ); + free(byName); + free(dstName); + return 0; } -int DispenseAdd(int User, int ByUser, int Ammount, const char *ReasonGiven) +/** + * \brief Donate money to the club + */ +int DispenseDonate(int ActualUser, int User, int Ammount, const char *ReasonGiven) { int ret; + char *srcName, *byName; - ret = Transfer( GetUserID(COKEBANK_DEBT_ACCT), User, Ammount, ReasonGiven ); + if( Ammount < 0 ) return 2; + ret = _Transfer( User, Bank_GetAcctByName(COKEBANK_DEBT_ACCT), Ammount, ReasonGiven ); if(ret) return 2; - Log_Info("add %i to %s by %s (%s)", - Ammount, GetUserName(User), GetUserName(ByUser), ReasonGiven + byName = Bank_GetAcctName(ActualUser); + srcName = Bank_GetAcctName(User); + + Log_Info("donate %i from %s by %s [balance %i] - %s", + Ammount, srcName, byName, Bank_GetBalance(User), ReasonGiven ); + free(byName); + free(srcName); + return 0; } + +// --- Internal Functions --- +int _GetMinBalance(int Account) +{ + int flags = Bank_GetFlags(Account); + + // - Internal accounts have no lower bound + if( flags & USER_FLAG_INTERNAL ) return INT_MIN; + + // Admin to -$10 + if( flags & USER_FLAG_ADMIN ) return -1000; + + // Coke to -$10 + if( flags & USER_FLAG_COKE ) return -500; + + // Anyone else, non-negative + return 0; +} + +int _Transfer(int Source, int Destination, int Ammount, const char *Reason) +{ + if( Ammount > 0 ) + { + if( Bank_GetBalance(Source) + Ammount < _GetMinBalance(Source) ) + return 1; + } + else + { + if( Bank_GetBalance(Destination) - Ammount < _GetMinBalance(Destination) ) + return 1; + } + + return Bank_Transfer(Source, Destination, Ammount, Reason); +}