8 * \brief Dispense an item for a user
10 * The core of the dispense system, I kinda like it :)
12 int DispenseItem(int ActualUser, int User, tItem *Item)
16 char *username, *actualUsername;
19 handler = Item->Handler;
21 // Check if the dispense is possible
22 if( handler->CanDispense ) {
23 ret = handler->CanDispense( User, Item->ID );
24 if(ret) return 1; // 1: Unable to dispense
27 // Subtract the balance
28 reason = mkstr("Dispense - %s:%i %s", handler->Name, Item->ID, Item->Name);
29 if( !reason ) reason = Item->Name; // TODO: Should I instead return an error?
30 ret = Bank_Transfer( User, Bank_GetUserID(COKEBANK_SALES_ACCT), Item->Price, reason);
32 if(ret) return 2; // 2: No balance
34 // Get username for debugging
35 username = Bank_GetUserName(User);
37 // Actually do the dispense
38 if( handler->DoDispense ) {
39 ret = handler->DoDispense( User, Item->ID );
41 Log_Error("Dispense failed after deducting cost (%s dispensing %s - %ic)",
42 username, Item->Name, Item->Price);
43 Bank_Transfer( Bank_GetUserID(COKEBANK_SALES_ACCT), User, Item->Price, "rollback" );
45 return -1; // 1: Unkown Error again
49 actualUsername = Bank_GetUserName(ActualUser);
51 // And log that it happened
52 Log_Info("dispense '%s' (%s:%i) for %s by %s [cost %i, balance %i cents]",
53 Item->Name, handler->Name, Item->ID,
54 username, actualUsername, Item->Price, Bank_GetBalance(User)
58 free( actualUsername );
63 * \brief Give money from one user to another
65 int DispenseGive(int ActualUser, int SrcUser, int DestUser, int Ammount, const char *ReasonGiven)
69 char *srcName, *dstName;
71 if( Ammount < 0 ) return 1; // Um... negative give? Not on my watch!
73 ret = Bank_Transfer( SrcUser, DestUser, Ammount, ReasonGiven );
74 if(ret) return 2; // No Balance
77 srcName = Bank_GetUserName(SrcUser);
78 dstName = Bank_GetUserName(DestUser);
79 actualUsername = Bank_GetUserName(ActualUser);
81 Log_Info("give %i to %s from %s by %s (%s) [balances %i, %i]",
82 Ammount, dstName, srcName, actualUsername, ReasonGiven,
83 Bank_GetBalance(SrcUser), Bank_GetBalance(DestUser)
94 * \brief Add money to an account
96 int DispenseAdd(int User, int ByUser, int Ammount, const char *ReasonGiven)
99 char *dstName, *byName;
101 ret = Bank_Transfer( Bank_GetUserID(COKEBANK_DEBT_ACCT), User, Ammount, ReasonGiven );
104 byName = Bank_GetUserName(ByUser);
105 dstName = Bank_GetUserName(User);
107 Log_Info("add %i to %s by %s (%s) [balance %i]",
108 Ammount, dstName, byName, ReasonGiven, Bank_GetBalance(User)