7 int _GetMinBalance(int Account);
8 int _CanTransfer(int Source, int Destination, int Ammount);
9 int _Transfer(int Source, int Destination, int Ammount, const char *Reason);
13 * \brief Dispense an item for a user
15 * The core of the dispense system, I kinda like it :)
17 int DispenseItem(int ActualUser, int User, tItem *Item)
21 char *username, *actualUsername;
23 salesAcct = Bank_GetAcctByName(COKEBANK_SALES_ACCT);
25 // Check if the user can afford it
26 if( Item->Price && !_CanTransfer(User, salesAcct, Item->Price) )
28 return 2; // 2: No balance
31 handler = Item->Handler;
33 // Check if the dispense is possible
34 if( handler->CanDispense ) {
35 ret = handler->CanDispense( User, Item->ID );
36 if(ret) return 1; // 1: Unable to dispense
39 // Get username for debugging
40 username = Bank_GetAcctName(User);
42 // Actually do the dispense
43 if( handler->DoDispense ) {
44 ret = handler->DoDispense( User, Item->ID );
46 Log_Error("Dispense failed (%s dispensing '%s' - %ic)",
47 username, Item->Name, Item->Price);
49 return -1; // 1: Unknown Error again
57 reason = mkstr("Dispense - %s:%i %s", handler->Name, Item->ID, Item->Name);
58 _Transfer( User, salesAcct, Item->Price, reason );
62 actualUsername = Bank_GetAcctName(ActualUser);
64 // And log that it happened
65 Log_Info("dispense '%s' (%s:%i) for %s by %s [cost %i, balance %i]",
66 Item->Name, handler->Name, Item->ID,
67 username, actualUsername, Item->Price, Bank_GetBalance(User)
71 free( actualUsername );
76 * \brief Give money from one user to another
78 int DispenseGive(int ActualUser, int SrcUser, int DestUser, int Ammount, const char *ReasonGiven)
82 char *srcName, *dstName;
84 if( Ammount < 0 ) return 1; // Um... negative give? Not on my watch!
86 ret = _Transfer( SrcUser, DestUser, Ammount, ReasonGiven );
87 if(ret) return 2; // No Balance
90 actualUsername = Bank_GetAcctName(ActualUser);
91 srcName = Bank_GetAcctName(SrcUser);
92 dstName = Bank_GetAcctName(DestUser);
94 Log_Info("give %i to %s from %s by %s [balances %i, %i] - %s",
95 Ammount, dstName, srcName, actualUsername,
96 Bank_GetBalance(SrcUser), Bank_GetBalance(DestUser),
102 free(actualUsername);
108 * \brief Add money to an account
110 int DispenseAdd(int ActualUser, int User, int Ammount, const char *ReasonGiven)
113 char *dstName, *byName;
115 ret = _Transfer( Bank_GetAcctByName(COKEBANK_DEBT_ACCT), User, Ammount, ReasonGiven );
118 byName = Bank_GetAcctName(ActualUser);
119 dstName = Bank_GetAcctName(User);
121 Log_Info("add %i to %s by %s [balance %i] - %s",
122 Ammount, dstName, byName, Bank_GetBalance(User), ReasonGiven
131 int DispenseSet(int ActualUser, int User, int Balance, const char *ReasonGiven)
133 int curBal = Bank_GetBalance(User);
134 char *byName, *dstName;
136 _Transfer( Bank_GetAcctByName(COKEBANK_DEBT_ACCT), User, Balance-curBal, ReasonGiven );
138 byName = Bank_GetAcctName(ActualUser);
139 dstName = Bank_GetAcctName(User);
141 Log_Info("set balance of %s to %i by %s [balance %i] - %s",
142 dstName, Balance, byName, Bank_GetBalance(User), ReasonGiven
152 * \brief Donate money to the club
154 int DispenseDonate(int ActualUser, int User, int Ammount, const char *ReasonGiven)
157 char *srcName, *byName;
159 if( Ammount < 0 ) return 2;
161 ret = _Transfer( User, Bank_GetAcctByName(COKEBANK_DEBT_ACCT), Ammount, ReasonGiven );
164 byName = Bank_GetAcctName(ActualUser);
165 srcName = Bank_GetAcctName(User);
167 Log_Info("donate %i from %s by %s [balance %i] - %s",
168 Ammount, srcName, byName, Bank_GetBalance(User), ReasonGiven
177 // --- Internal Functions ---
178 int _GetMinBalance(int Account)
180 int flags = Bank_GetFlags(Account);
182 // - Internal accounts have no lower bound
183 if( flags & USER_FLAG_INTERNAL ) return INT_MIN;
186 if( flags & USER_FLAG_ADMIN ) return -1000;
189 if( flags & USER_FLAG_COKE ) return -500;
191 // Anyone else, non-negative
196 * \brief Check if a transfer is possible
198 int _CanTransfer(int Source, int Destination, int Ammount)
202 if( Bank_GetBalance(Source) + Ammount < _GetMinBalance(Source) )
207 if( Bank_GetBalance(Destination) - Ammount < _GetMinBalance(Destination) )
213 int _Transfer(int Source, int Destination, int Ammount, const char *Reason)
215 if( !_CanTransfer(Source, Destination, Ammount) )
217 return Bank_Transfer(Source, Destination, Ammount, Reason);