int _GetMinBalance(int Account);
int _CanTransfer(int Source, int Destination, int Ammount);
int _Transfer(int Source, int Destination, int Ammount, const char *Reason);
+ int _GetSalesAcct(tItem *Item);
// === CODE ===
/**
tHandler *handler;
char *username, *actualUsername;
- salesAcct = Bank_GetAcctByName(COKEBANK_SALES_ACCT);
+ handler = Item->Handler;
+
+ salesAcct = _GetSalesAcct(Item);
// Check if the user can afford it
if( Item->Price && !_CanTransfer(User, salesAcct, Item->Price) )
return 2; // 2: No balance
}
- handler = Item->Handler;
+ // HACK: Naming a slot "dead" disables it
+ if( strcmp(Item->Name, "dead") == 0 )
+ return 1;
// Check if the dispense is possible
if( handler->CanDispense ) {
if( handler->DoDispense ) {
ret = handler->DoDispense( User, Item->ID );
if(ret) {
- Log_Error("Dispense failed (%s dispensing '%s' - %ic)",
- username, Item->Name, Item->Price);
+ Log_Error("Dispense failed (%s dispensing %s:%i '%s')",
+ username, Item->Handler->Name, Item->ID, Item->Name);
free( username );
return -1; // 1: Unknown Error again
}
int src_acct, price;
char *username, *actualUsername;
- src_acct = Bank_GetAcctByName(COKEBANK_SALES_ACCT);
+ src_acct = _GetSalesAcct(Item);
if( OverridePrice > 0 )
price = OverridePrice;
char *actualUsername;
char *srcName, *dstName;
+ // HACK: Naming a slot "dead" disables it (catch for snack)
+ if( strcmp(ReasonGiven, "dead") == 0 )
+ return 1;
+
if( Ammount < 0 ) return 1; // Um... negative give? Not on my watch!
ret = _Transfer( SrcUser, DestUser, Ammount, ReasonGiven );
srcName = Bank_GetAcctName(SrcUser);
dstName = Bank_GetAcctName(DestUser);
- Log_Info("give %i to %s from %s by %s [balances %i, %i] - %s",
- Ammount, dstName, srcName, actualUsername,
+ Log_Info("give %i from %s to %s by %s [balances %i, %i] - %s",
+ Ammount, srcName, dstName, actualUsername,
Bank_GetBalance(SrcUser), Bank_GetBalance(DestUser),
ReasonGiven
);
return 0;
}
+#if 0 // Dead Code
/**
* \brief Move money from one user to another (Admin Only)
*/
srcName = Bank_GetAcctName(SrcUser);
dstName = Bank_GetAcctName(DestUser);
- Log_Info("move %i to %s from %s by %s [balances %i, %i] - %s",
- Ammount, dstName, srcName, actualUsername,
+ Log_Info("move %i from %s to %s by %s [balances %i, %i] - %s",
+ Ammount, srcName, dstName, actualUsername,
Bank_GetBalance(SrcUser), Bank_GetBalance(DestUser),
ReasonGiven
);
return 0;
}
+#endif
+
/**
* \brief Add money to an account
*/
char *dstName, *byName;
#if DISPENSE_ADD_BELOW_MIN
- ret = _Transfer( Bank_GetAcctByName(COKEBANK_DEBT_ACCT), User, Ammount, ReasonGiven );
+ ret = _Transfer( Bank_GetAcctByName(COKEBANK_DEBT_ACCT,1), User, Ammount, ReasonGiven );
#else
- ret = Bank_Transfer( Bank_GetAcctByName(COKEBANK_DEBT_ACCT), User, Ammount, ReasonGiven );
+ ret = Bank_Transfer( Bank_GetAcctByName(COKEBANK_DEBT_ACCT,1), User, Ammount, ReasonGiven );
#endif
if(ret) return 2;
int curBal = Bank_GetBalance(User);
char *byName, *dstName;
- _Transfer( Bank_GetAcctByName(COKEBANK_DEBT_ACCT), User, Balance-curBal, ReasonGiven );
+ _Transfer( Bank_GetAcctByName(COKEBANK_DEBT_ACCT,1), User, Balance-curBal, ReasonGiven );
byName = Bank_GetAcctName(ActualUser);
dstName = Bank_GetAcctName(User);
if( Ammount < 0 ) return 2;
- ret = _Transfer( User, Bank_GetAcctByName(COKEBANK_DEBT_ACCT), Ammount, ReasonGiven );
+ ret = _Transfer( User, Bank_GetAcctByName(COKEBANK_DONATE_ACCT,1), Ammount, ReasonGiven );
if(ret) return 2;
byName = Bank_GetAcctName(ActualUser);
return 0;
}
+int DispenseUpdateItem(int User, tItem *Item, const char *NewName, int NewPrice)
+{
+ char *username;
+
+ // Sanity checks
+ if( NewPrice < 0 ) return 2;
+ if( !Item ) return 2;
+ if( strlen(NewName) < 1 ) return 2;
+
+ // Update the item
+ free(Item->Name);
+ Item->Name = strdup(NewName);
+ Item->Price = NewPrice;
+
+ username = Bank_GetAcctName(User);
+
+ Log_Info("item %s:%i updated to '%s' %i by %s",
+ Item->Handler->Name, Item->ID,
+ NewName, NewPrice, username
+ );
+
+ free(username);
+
+ // Update item file
+ Items_UpdateFile();
+
+ return 0;
+}
+
// --- Internal Functions ---
int _GetMinBalance(int Account)
{
/**
* \brief Check if a transfer is possible
+ * \return Boolean success
*/
int _CanTransfer(int Source, int Destination, int Ammount)
{
+ if( Bank_GetFlags(Source) & USER_FLAG_DISABLED )
+ return 0;
if( Ammount > 0 )
{
if( Bank_GetBalance(Source) - Ammount < _GetMinBalance(Source) )
return 1;
return Bank_Transfer(Source, Destination, Ammount, Reason);
}
+
+int _GetSalesAcct(tItem *Item)
+{
+ char string[sizeof(COKEBANK_SALES_PREFIX)+strlen(Item->Handler->Name)];
+ strcpy(string, COKEBANK_SALES_PREFIX);
+ strcat(string, Item->Handler->Name);
+ return Bank_GetAcctByName(string, 1);
+}