7 int _GetMinBalance(int Account);
8 int _Transfer(int Source, int Destination, int Ammount, const char *Reason);
12 * \brief Dispense an item for a user
14 * The core of the dispense system, I kinda like it :)
16 int DispenseItem(int ActualUser, int User, tItem *Item)
20 char *username, *actualUsername;
23 handler = Item->Handler;
25 // Check if the dispense is possible
26 if( handler->CanDispense ) {
27 ret = handler->CanDispense( User, Item->ID );
28 if(ret) return 1; // 1: Unable to dispense
31 // Subtract the balance
32 reason = mkstr("Dispense - %s:%i %s", handler->Name, Item->ID, Item->Name);
33 if( !reason ) reason = Item->Name; // TODO: Should I instead return an error?
34 ret = _Transfer( User, Bank_GetAcctByName(COKEBANK_SALES_ACCT), Item->Price, reason);
36 if(ret) return 2; // 2: No balance
38 // Get username for debugging
39 username = Bank_GetAcctName(User);
41 // Actually do the dispense
42 if( handler->DoDispense ) {
43 ret = handler->DoDispense( User, Item->ID );
45 Log_Error("Dispense failed after deducting cost (%s dispensing %s - %ic)",
46 username, Item->Name, Item->Price);
47 _Transfer( Bank_GetAcctByName(COKEBANK_SALES_ACCT), User, Item->Price, "rollback" );
49 return -1; // 1: Unkown Error again
53 actualUsername = Bank_GetAcctName(ActualUser);
55 // And log that it happened
56 Log_Info("dispense '%s' (%s:%i) for %s by %s [cost %i, balance %i]",
57 Item->Name, handler->Name, Item->ID,
58 username, actualUsername, Item->Price, Bank_GetBalance(User)
62 free( actualUsername );
67 * \brief Give money from one user to another
69 int DispenseGive(int ActualUser, int SrcUser, int DestUser, int Ammount, const char *ReasonGiven)
73 char *srcName, *dstName;
75 if( Ammount < 0 ) return 1; // Um... negative give? Not on my watch!
77 ret = _Transfer( SrcUser, DestUser, Ammount, ReasonGiven );
78 if(ret) return 2; // No Balance
81 actualUsername = Bank_GetAcctName(ActualUser);
82 srcName = Bank_GetAcctName(SrcUser);
83 dstName = Bank_GetAcctName(DestUser);
85 Log_Info("give %i to %s from %s by %s [balances %i, %i] - %s",
86 Ammount, dstName, srcName, actualUsername,
87 Bank_GetBalance(SrcUser), Bank_GetBalance(DestUser),
99 * \brief Add money to an account
101 int DispenseAdd(int ActualUser, int User, int Ammount, const char *ReasonGiven)
104 char *dstName, *byName;
106 ret = _Transfer( Bank_GetAcctByName(COKEBANK_DEBT_ACCT), User, Ammount, ReasonGiven );
109 byName = Bank_GetAcctName(ActualUser);
110 dstName = Bank_GetAcctName(User);
112 Log_Info("add %i to %s by %s [balance %i] - %s",
113 Ammount, dstName, byName, Bank_GetBalance(User), ReasonGiven
123 * \brief Donate money to the club
125 int DispenseDonate(int ActualUser, int User, int Ammount, const char *ReasonGiven)
128 char *srcName, *byName;
130 if( Ammount < 0 ) return 2;
132 ret = _Transfer( User, Bank_GetAcctByName(COKEBANK_DEBT_ACCT), Ammount, ReasonGiven );
135 byName = Bank_GetAcctName(ActualUser);
136 srcName = Bank_GetAcctName(User);
138 Log_Info("donate %i from %s by %s [balance %i] - %s",
139 Ammount, srcName, byName, Bank_GetBalance(User), ReasonGiven
148 // --- Internal Functions ---
149 int _GetMinBalance(int Account)
151 int flags = Bank_GetFlags(Account);
153 // - Internal accounts have no lower bound
154 if( flags & USER_FLAG_INTERNAL ) return INT_MIN;
157 if( flags & USER_FLAG_ADMIN ) return -1000;
160 if( flags & USER_FLAG_COKE ) return -500;
162 // Anyone else, non-negative
166 int _Transfer(int Source, int Destination, int Ammount, const char *Reason)
170 if( Bank_GetBalance(Source) + Ammount < _GetMinBalance(Source) )
175 if( Bank_GetBalance(Destination) - Ammount < _GetMinBalance(Destination) )
179 return Bank_Transfer(Source, Destination, Ammount, Reason);