8 int _GetMinBalance(int Account);
9 int _CanTransfer(int Source, int Destination, int Ammount);
10 int _Transfer(int Source, int Destination, int Ammount, const char *Reason);
14 * \brief Dispense an item for a user
16 * The core of the dispense system, I kinda like it :)
18 int DispenseItem(int ActualUser, int User, tItem *Item)
22 char *username, *actualUsername;
24 salesAcct = Bank_GetAcctByName(COKEBANK_SALES_ACCT);
26 // Check if the user can afford it
27 if( Item->Price && !_CanTransfer(User, salesAcct, Item->Price) )
29 return 2; // 2: No balance
32 handler = Item->Handler;
34 // Check if the dispense is possible
35 if( handler->CanDispense ) {
36 ret = handler->CanDispense( User, Item->ID );
37 if(ret) return 1; // 1: Unable to dispense
40 // Get username for debugging
41 username = Bank_GetAcctName(User);
43 // Actually do the dispense
44 if( handler->DoDispense ) {
45 ret = handler->DoDispense( User, Item->ID );
47 Log_Error("Dispense failed (%s dispensing '%s' - %ic)",
48 username, Item->Name, Item->Price);
50 return -1; // 1: Unknown Error again
58 reason = mkstr("Dispense - %s:%i %s", handler->Name, Item->ID, Item->Name);
59 _Transfer( User, salesAcct, Item->Price, reason );
63 actualUsername = Bank_GetAcctName(ActualUser);
65 // And log that it happened
66 Log_Info("dispense '%s' (%s:%i) for %s by %s [cost %i, balance %i]",
67 Item->Name, handler->Name, Item->ID,
68 username, actualUsername, Item->Price, Bank_GetBalance(User)
72 free( actualUsername );
77 * \brief Give money from one user to another
79 int DispenseGive(int ActualUser, int SrcUser, int DestUser, int Ammount, const char *ReasonGiven)
83 char *srcName, *dstName;
85 if( Ammount < 0 ) return 1; // Um... negative give? Not on my watch!
87 ret = _Transfer( SrcUser, DestUser, Ammount, ReasonGiven );
88 if(ret) return 2; // No Balance
91 actualUsername = Bank_GetAcctName(ActualUser);
92 srcName = Bank_GetAcctName(SrcUser);
93 dstName = Bank_GetAcctName(DestUser);
95 Log_Info("give %i to %s from %s by %s [balances %i, %i] - %s",
96 Ammount, dstName, srcName, actualUsername,
97 Bank_GetBalance(SrcUser), Bank_GetBalance(DestUser),
103 free(actualUsername);
109 * \brief Move money from one user to another (Admin Only)
111 int DispenseTransfer(int ActualUser, int SrcUser, int DestUser, int Ammount, const char *ReasonGiven)
114 char *actualUsername;
115 char *srcName, *dstName;
117 // Make sure the user is an admin
118 if( !(Bank_GetFlags(ActualUser) & USER_FLAG_ADMIN) )
121 ret = _Transfer( SrcUser, DestUser, Ammount, ReasonGiven );
122 if(ret) return 2; // No Balance
125 actualUsername = Bank_GetAcctName(ActualUser);
126 srcName = Bank_GetAcctName(SrcUser);
127 dstName = Bank_GetAcctName(DestUser);
129 Log_Info("move %i to %s from %s by %s [balances %i, %i] - %s",
130 Ammount, dstName, srcName, actualUsername,
131 Bank_GetBalance(SrcUser), Bank_GetBalance(DestUser),
137 free(actualUsername);
142 * \brief Add money to an account
144 int DispenseAdd(int ActualUser, int User, int Ammount, const char *ReasonGiven)
147 char *dstName, *byName;
149 ret = _Transfer( Bank_GetAcctByName(COKEBANK_DEBT_ACCT), User, Ammount, ReasonGiven );
152 byName = Bank_GetAcctName(ActualUser);
153 dstName = Bank_GetAcctName(User);
155 Log_Info("add %i to %s by %s [balance %i] - %s",
156 Ammount, dstName, byName, Bank_GetBalance(User), ReasonGiven
165 int DispenseSet(int ActualUser, int User, int Balance, const char *ReasonGiven)
167 int curBal = Bank_GetBalance(User);
168 char *byName, *dstName;
170 _Transfer( Bank_GetAcctByName(COKEBANK_DEBT_ACCT), User, Balance-curBal, ReasonGiven );
172 byName = Bank_GetAcctName(ActualUser);
173 dstName = Bank_GetAcctName(User);
175 Log_Info("set balance of %s to %i by %s [balance %i] - %s",
176 dstName, Balance, byName, Bank_GetBalance(User), ReasonGiven
186 * \brief Donate money to the club
188 int DispenseDonate(int ActualUser, int User, int Ammount, const char *ReasonGiven)
191 char *srcName, *byName;
193 if( Ammount < 0 ) return 2;
195 ret = _Transfer( User, Bank_GetAcctByName(COKEBANK_DEBT_ACCT), Ammount, ReasonGiven );
198 byName = Bank_GetAcctName(ActualUser);
199 srcName = Bank_GetAcctName(User);
201 Log_Info("donate %i from %s by %s [balance %i] - %s",
202 Ammount, srcName, byName, Bank_GetBalance(User), ReasonGiven
211 // --- Internal Functions ---
212 int _GetMinBalance(int Account)
214 int flags = Bank_GetFlags(Account);
216 // Evil little piece of HACK:
217 // root's balance cannot be changed by any of the above functions
218 // - Stops dispenses as root by returning insufficent balance.
220 char *username = Bank_GetAcctName(Account);
221 if( strcmp(username, "root") == 0 )
229 // - Internal accounts have no lower bound
230 if( flags & USER_FLAG_INTERNAL ) return INT_MIN;
233 //if( flags & USER_FLAG_ADMIN ) return -1000;
236 //if( flags & USER_FLAG_COKE ) return -500;
238 // Anyone else, non-negative
243 * \brief Check if a transfer is possible
245 int _CanTransfer(int Source, int Destination, int Ammount)
249 if( Bank_GetBalance(Source) + Ammount < _GetMinBalance(Source) )
254 if( Bank_GetBalance(Destination) - Ammount < _GetMinBalance(Destination) )
260 int _Transfer(int Source, int Destination, int Ammount, const char *Reason)
262 if( !_CanTransfer(Source, Destination, Ammount) )
264 return Bank_Transfer(Source, Destination, Ammount, Reason);