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);
11 int _GetSalesAcct(tItem *Item);
15 * \brief Dispense an item for a user
17 * The core of the dispense system, I kinda like it :)
19 int DispenseItem(int ActualUser, int User, tItem *Item)
23 char *username, *actualUsername;
25 handler = Item->Handler;
27 salesAcct = _GetSalesAcct(Item);
29 // Check if the user can afford it
30 if( Item->Price && !_CanTransfer(User, salesAcct, Item->Price) )
32 return 2; // 2: No balance
35 // HACK: Naming a slot "dead" disables it
36 if( strcmp(Item->Name, "dead") == 0 )
39 // Check if the dispense is possible
40 if( handler->CanDispense ) {
41 ret = handler->CanDispense( User, Item->ID );
42 if(ret) return 1; // 1: Unable to dispense
45 // Get username for debugging
46 username = Bank_GetAcctName(User);
48 // Actually do the dispense
49 if( handler->DoDispense ) {
50 ret = handler->DoDispense( User, Item->ID );
52 Log_Error("Dispense failed (%s dispensing %s:%i '%s')",
53 username, Item->Handler->Name, Item->ID, Item->Name);
55 return -1; // 1: Unknown Error again
63 reason = mkstr("Dispense - %s:%i %s", handler->Name, Item->ID, Item->Name);
64 _Transfer( User, salesAcct, Item->Price, reason );
68 actualUsername = Bank_GetAcctName(ActualUser);
70 // And log that it happened
71 Log_Info("dispense '%s' (%s:%i) for %s by %s [cost %i, balance %i]",
72 Item->Name, handler->Name, Item->ID,
73 username, actualUsername, Item->Price, Bank_GetBalance(User)
77 free( actualUsername );
82 * \brief Refund a dispense
84 int DispenseRefund(int ActualUser, int DestUser, tItem *Item, int OverridePrice)
88 char *username, *actualUsername;
90 src_acct = _GetSalesAcct(Item);
92 if( OverridePrice > 0 )
93 price = OverridePrice;
97 ret = _Transfer( src_acct, DestUser, price, "Refund");
100 username = Bank_GetAcctName(DestUser);
101 actualUsername = Bank_GetAcctName(ActualUser);
103 Log_Info("refund '%s' (%s:%i) to %s by %s [cost %i, balance %i]",
104 Item->Name, Item->Handler->Name, Item->ID,
105 username, actualUsername, price, Bank_GetBalance(DestUser)
109 free(actualUsername);
115 * \brief Give money from one user to another
117 int DispenseGive(int ActualUser, int SrcUser, int DestUser, int Ammount, const char *ReasonGiven)
120 char *actualUsername;
121 char *srcName, *dstName;
123 // HACK: Naming a slot "dead" disables it (catch for snack)
124 if( strcmp(ReasonGiven, "dead") == 0 )
127 if( Ammount < 0 ) return 1; // Um... negative give? Not on my watch!
129 ret = _Transfer( SrcUser, DestUser, Ammount, ReasonGiven );
130 if(ret) return 2; // No Balance
133 actualUsername = Bank_GetAcctName(ActualUser);
134 srcName = Bank_GetAcctName(SrcUser);
135 dstName = Bank_GetAcctName(DestUser);
137 Log_Info("give %i from %s to %s by %s [balances %i, %i] - %s",
138 Ammount, srcName, dstName, actualUsername,
139 Bank_GetBalance(SrcUser), Bank_GetBalance(DestUser),
145 free(actualUsername);
152 * \brief Move money from one user to another (Admin Only)
154 int DispenseTransfer(int ActualUser, int SrcUser, int DestUser, int Ammount, const char *ReasonGiven)
157 char *actualUsername;
158 char *srcName, *dstName;
160 // Make sure the user is an admin
161 if( !(Bank_GetFlags(ActualUser) & USER_FLAG_ADMIN) )
164 ret = _Transfer( SrcUser, DestUser, Ammount, ReasonGiven );
165 if(ret) return 2; // No Balance
168 actualUsername = Bank_GetAcctName(ActualUser);
169 srcName = Bank_GetAcctName(SrcUser);
170 dstName = Bank_GetAcctName(DestUser);
172 Log_Info("move %i from %s to %s by %s [balances %i, %i] - %s",
173 Ammount, srcName, dstName, actualUsername,
174 Bank_GetBalance(SrcUser), Bank_GetBalance(DestUser),
180 free(actualUsername);
187 * \brief Add money to an account
189 int DispenseAdd(int ActualUser, int User, int Ammount, const char *ReasonGiven)
192 char *dstName, *byName;
194 #if DISPENSE_ADD_BELOW_MIN
195 ret = _Transfer( Bank_GetAcctByName(COKEBANK_DEBT_ACCT,1), User, Ammount, ReasonGiven );
197 ret = Bank_Transfer( Bank_GetAcctByName(COKEBANK_DEBT_ACCT,1), User, Ammount, ReasonGiven );
201 byName = Bank_GetAcctName(ActualUser);
202 dstName = Bank_GetAcctName(User);
204 Log_Info("add %i to %s by %s [balance %i] - %s",
205 Ammount, dstName, byName, Bank_GetBalance(User), ReasonGiven
214 int DispenseSet(int ActualUser, int User, int Balance, const char *ReasonGiven)
216 int curBal = Bank_GetBalance(User);
217 char *byName, *dstName;
219 _Transfer( Bank_GetAcctByName(COKEBANK_DEBT_ACCT,1), User, Balance-curBal, ReasonGiven );
221 byName = Bank_GetAcctName(ActualUser);
222 dstName = Bank_GetAcctName(User);
224 Log_Info("set balance of %s to %i by %s [balance %i] - %s",
225 dstName, Balance, byName, Bank_GetBalance(User), ReasonGiven
235 * \brief Donate money to the club
237 int DispenseDonate(int ActualUser, int User, int Ammount, const char *ReasonGiven)
240 char *srcName, *byName;
242 if( Ammount < 0 ) return 2;
244 ret = _Transfer( User, Bank_GetAcctByName(COKEBANK_DONATE_ACCT,1), Ammount, ReasonGiven );
247 byName = Bank_GetAcctName(ActualUser);
248 srcName = Bank_GetAcctName(User);
250 Log_Info("donate %i from %s by %s [balance %i] - %s",
251 Ammount, srcName, byName, Bank_GetBalance(User), ReasonGiven
260 int DispenseUpdateItem(int User, tItem *Item, const char *NewName, int NewPrice)
265 if( NewPrice < 0 ) return 2;
266 if( !Item ) return 2;
267 if( strlen(NewName) < 1 ) return 2;
271 Item->Name = strdup(NewName);
272 Item->Price = NewPrice;
274 username = Bank_GetAcctName(User);
276 Log_Info("item %s:%i updated to '%s' %i by %s",
277 Item->Handler->Name, Item->ID,
278 NewName, NewPrice, username
289 // --- Internal Functions ---
290 int _GetMinBalance(int Account)
292 int flags = Bank_GetFlags(Account);
294 // Evil little piece of HACK:
295 // root's balance cannot be changed by any of the above functions
296 // - Stops dispenses as root by returning insufficent balance.
298 char *username = Bank_GetAcctName(Account);
299 if( strcmp(username, "root") == 0 )
307 // - Internal accounts have no lower bound
308 if( flags & USER_FLAG_INTERNAL ) return INT_MIN;
311 // if( flags & USER_FLAG_ADMIN ) return -5000;
314 // if( flags & USER_FLAG_COKE ) return -2000;
316 // Anyone else, non-negative
321 * \brief Check if a transfer is possible
322 * \return Boolean success
324 int _CanTransfer(int Source, int Destination, int Ammount)
326 if( Bank_GetFlags(Source) & USER_FLAG_DISABLED )
330 if( Bank_GetBalance(Source) - Ammount < _GetMinBalance(Source) )
335 if( Bank_GetBalance(Destination) + Ammount < _GetMinBalance(Destination) )
341 int _Transfer(int Source, int Destination, int Ammount, const char *Reason)
343 if( !_CanTransfer(Source, Destination, Ammount) )
345 return Bank_Transfer(Source, Destination, Ammount, Reason);
348 int _GetSalesAcct(tItem *Item)
350 char string[sizeof(COKEBANK_SALES_PREFIX)+strlen(Item->Handler->Name)];
351 strcpy(string, COKEBANK_SALES_PREFIX);
352 strcat(string, Item->Handler->Name);
353 return Bank_GetAcctByName(string, 1);