Misc changes, have flu so don't remember all of them
[tpg/opendispense2.git] / src / server / dispense.c
1 /**
2  */
3 #include "common.h"
4 #include <stdlib.h>
5 #include <limits.h>
6 #include <string.h>
7
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);
12
13 // === CODE ===
14 /**
15  * \brief Dispense an item for a user
16  * 
17  * The core of the dispense system, I kinda like it :)
18  */
19 int DispenseItem(int ActualUser, int User, tItem *Item)
20 {
21          int    ret, salesAcct;
22         tHandler        *handler;
23         char    *username, *actualUsername;
24         
25         handler = Item->Handler;
26         
27         salesAcct = _GetSalesAcct(Item);
28
29         // Check if the user can afford it
30         if( Item->Price && !_CanTransfer(User, salesAcct, Item->Price) )
31         {
32                 return 2;       // 2: No balance
33         }
34         
35         // HACK: Naming a slot "dead" disables it
36         if( strcmp(Item->Name, "dead") == 0 )
37                 return 1;
38         
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
43         }
44         
45         // Get username for debugging
46         username = Bank_GetAcctName(User);
47         
48         // Actually do the dispense
49         if( handler->DoDispense ) {
50                 ret = handler->DoDispense( User, Item->ID );
51                 if(ret) {
52                         Log_Error("Dispense failed (%s dispensing %s:%i '%s')",
53                                 username, Item->Name, Item->Handler->Name, Item->ID);
54                         free( username );
55                         return -1;      // 1: Unknown Error again
56                 }
57         }
58         
59         // Take away money
60         if( Item->Price )
61         {
62                 char    *reason;
63                 reason = mkstr("Dispense - %s:%i %s", handler->Name, Item->ID, Item->Name);
64                 _Transfer( User, salesAcct, Item->Price, reason );
65                 free(reason);
66         }
67         
68         actualUsername = Bank_GetAcctName(ActualUser);
69         
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)
74                 );
75         
76         free( username );
77         free( actualUsername );
78         return 0;       // 0: EOK
79 }
80
81 /**
82  * \brief Refund a dispense
83  */
84 int DispenseRefund(int ActualUser, int DestUser, tItem *Item, int OverridePrice)
85 {
86          int    ret;
87          int    src_acct, price;
88         char    *username, *actualUsername;
89
90         src_acct = _GetSalesAcct(Item);
91
92         if( OverridePrice > 0 )
93                 price = OverridePrice;
94         else
95                 price = Item->Price;
96
97         ret = _Transfer( src_acct, DestUser, price, "Refund");
98         if(ret) return ret;
99
100         username = Bank_GetAcctName(DestUser);
101         actualUsername = Bank_GetAcctName(ActualUser);
102         
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)
106                 );
107
108         free(username);
109         free(actualUsername);
110
111         return 0;
112 }
113
114 /**
115  * \brief Give money from one user to another
116  */
117 int DispenseGive(int ActualUser, int SrcUser, int DestUser, int Ammount, const char *ReasonGiven)
118 {
119          int    ret;
120         char    *actualUsername;
121         char    *srcName, *dstName;
122         
123         if( Ammount < 0 )       return 1;       // Um... negative give? Not on my watch!
124         
125         ret = _Transfer( SrcUser, DestUser, Ammount, ReasonGiven );
126         if(ret) return 2;       // No Balance
127         
128         
129         actualUsername = Bank_GetAcctName(ActualUser);
130         srcName = Bank_GetAcctName(SrcUser);
131         dstName = Bank_GetAcctName(DestUser);
132         
133         Log_Info("give %i from %s to %s by %s [balances %i, %i] - %s",
134                 Ammount, srcName, dstName, actualUsername,
135                 Bank_GetBalance(SrcUser), Bank_GetBalance(DestUser),
136                 ReasonGiven
137                 );
138         
139         free(srcName);
140         free(dstName);
141         free(actualUsername);
142         
143         return 0;
144 }
145
146 #if 0 // Dead Code
147 /**
148  * \brief Move money from one user to another (Admin Only)
149  */
150 int DispenseTransfer(int ActualUser, int SrcUser, int DestUser, int Ammount, const char *ReasonGiven)
151 {
152          int    ret;
153         char    *actualUsername;
154         char    *srcName, *dstName;
155
156         // Make sure the user is an admin
157         if( !(Bank_GetFlags(ActualUser) & USER_FLAG_ADMIN) )
158                 return 1;
159         
160         ret = _Transfer( SrcUser, DestUser, Ammount, ReasonGiven );
161         if(ret) return 2;       // No Balance
162         
163         
164         actualUsername = Bank_GetAcctName(ActualUser);
165         srcName = Bank_GetAcctName(SrcUser);
166         dstName = Bank_GetAcctName(DestUser);
167         
168         Log_Info("move %i from %s to %s by %s [balances %i, %i] - %s",
169                 Ammount, srcName, dstName, actualUsername,
170                 Bank_GetBalance(SrcUser), Bank_GetBalance(DestUser),
171                 ReasonGiven
172                 );
173         
174         free(srcName);
175         free(dstName);
176         free(actualUsername);
177         
178         return 0;
179 }
180 #endif
181
182 /**
183  * \brief Add money to an account
184  */
185 int DispenseAdd(int ActualUser, int User, int Ammount, const char *ReasonGiven)
186 {
187          int    ret;
188         char    *dstName, *byName;
189         
190 #if DISPENSE_ADD_BELOW_MIN
191         ret = _Transfer( Bank_GetAcctByName(COKEBANK_DEBT_ACCT,1), User, Ammount, ReasonGiven );
192 #else
193         ret = Bank_Transfer( Bank_GetAcctByName(COKEBANK_DEBT_ACCT,1), User, Ammount, ReasonGiven );
194 #endif
195         if(ret) return 2;
196         
197         byName = Bank_GetAcctName(ActualUser);
198         dstName = Bank_GetAcctName(User);
199         
200         Log_Info("add %i to %s by %s [balance %i] - %s",
201                 Ammount, dstName, byName, Bank_GetBalance(User), ReasonGiven
202                 );
203         
204         free(byName);
205         free(dstName);
206         
207         return 0;
208 }
209
210 int DispenseSet(int ActualUser, int User, int Balance, const char *ReasonGiven)
211 {
212          int    curBal = Bank_GetBalance(User);
213         char    *byName, *dstName;
214         
215         _Transfer( Bank_GetAcctByName(COKEBANK_DEBT_ACCT,1), User, Balance-curBal, ReasonGiven );
216         
217         byName = Bank_GetAcctName(ActualUser);
218         dstName = Bank_GetAcctName(User);
219         
220         Log_Info("set balance of %s to %i by %s [balance %i] - %s",
221                 dstName, Balance, byName, Bank_GetBalance(User), ReasonGiven
222                 );
223         
224         free(byName);
225         free(dstName);
226         
227         return 0;
228 }
229
230 /**
231  * \brief Donate money to the club
232  */
233 int DispenseDonate(int ActualUser, int User, int Ammount, const char *ReasonGiven)
234 {
235          int    ret;
236         char    *srcName, *byName;
237         
238         if( Ammount < 0 )       return 2;
239         
240         ret = _Transfer( User, Bank_GetAcctByName(COKEBANK_DEBT_ACCT,1), Ammount, ReasonGiven );
241         if(ret) return 2;
242         
243         byName = Bank_GetAcctName(ActualUser);
244         srcName = Bank_GetAcctName(User);
245         
246         Log_Info("donate %i from %s by %s [balance %i] - %s",
247                 Ammount, srcName, byName, Bank_GetBalance(User), ReasonGiven
248                 );
249         
250         free(byName);
251         free(srcName);
252         
253         return 0;
254 }
255
256 int DispenseUpdateItem(int User, tItem *Item, const char *NewName, int NewPrice)
257 {
258         char    *username;
259         
260         // Sanity checks
261         if( NewPrice < 0 )      return 2;
262         if( !Item )     return 2;
263         if( strlen(NewName) < 1 )       return 2;
264         
265         // Update the item
266         free(Item->Name);
267         Item->Name = strdup(NewName);
268         Item->Price = NewPrice;
269         
270         username = Bank_GetAcctName(User);
271         
272         Log_Info("item %s:%i updated to '%s' %i by %s",
273                 Item->Handler->Name, Item->ID,
274                 NewName, NewPrice, username
275                 );
276         
277         free(username);
278         
279         // Update item file
280         Items_UpdateFile();
281         
282         return 0;
283 }
284
285 // --- Internal Functions ---
286 int _GetMinBalance(int Account)
287 {
288          int    flags = Bank_GetFlags(Account);
289         
290         // Evil little piece of HACK:
291         // root's balance cannot be changed by any of the above functions
292         // - Stops dispenses as root by returning insufficent balance.
293         {
294                 char    *username = Bank_GetAcctName(Account);
295                 if( strcmp(username, "root") == 0 )
296                 {
297                         free(username);
298                         return INT_MAX;
299                 }
300                 free(username);
301         }
302         
303         // - Internal accounts have no lower bound
304         if( flags & USER_FLAG_INTERNAL )        return INT_MIN;
305         
306         // Admin to -$50
307 //      if( flags & USER_FLAG_ADMIN )   return -5000;
308         
309         // Coke to -$20
310 //      if( flags & USER_FLAG_COKE )    return -2000;
311         
312         // Anyone else, non-negative
313         return 0;
314 }
315
316 /**
317  * \brief Check if a transfer is possible
318  */
319 int _CanTransfer(int Source, int Destination, int Ammount)
320 {
321         if( Ammount > 0 )
322         {
323                 if( Bank_GetBalance(Source) - Ammount < _GetMinBalance(Source) )
324                         return 0;
325         }
326         else
327         {
328                 if( Bank_GetBalance(Destination) + Ammount < _GetMinBalance(Destination) )
329                         return 0;
330         }
331         return 1;
332 }
333
334 int _Transfer(int Source, int Destination, int Ammount, const char *Reason)
335 {
336         if( !_CanTransfer(Source, Destination, Ammount) )
337                 return 1;
338         return Bank_Transfer(Source, Destination, Ammount, Reason);
339 }
340
341 int _GetSalesAcct(tItem *Item)
342 {
343         char string[sizeof(COKEBANK_SALES_PREFIX)+strlen(Item->Handler->Name)];
344         strcpy(string, COKEBANK_SALES_PREFIX);
345         strcat(string, Item->Handler->Name);
346         return Bank_GetAcctByName(string, 1);
347 }

UCC git Repository :: git.ucc.asn.au