Fixing up logging (slot renames and gives)
[tpg/opendispense2.git] / src / server / dispense.c
index 215e580..4c6bd51 100644 (file)
@@ -31,6 +31,10 @@ int DispenseItem(int ActualUser, int User, tItem *Item)
        
        handler = Item->Handler;
        
+       // KNOWN HACK: Naming a slot "dead" disables it
+       if( strcmp(Item->Name, "dead") == 0 )
+               return 1;
+       
        // Check if the dispense is possible
        if( handler->CanDispense ) {
                ret = handler->CanDispense( User, Item->ID );
@@ -44,8 +48,8 @@ int DispenseItem(int ActualUser, int User, tItem *Item)
        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->Name, Item->Handler->Name, Item->ID);
                        free( username );
                        return -1;      // 1: Unknown Error again
                }
@@ -73,6 +77,39 @@ int DispenseItem(int ActualUser, int User, tItem *Item)
        return 0;       // 0: EOK
 }
 
+/**
+ * \brief Refund a dispense
+ */
+int DispenseRefund(int ActualUser, int DestUser, tItem *Item, int OverridePrice)
+{
+        int    ret;
+        int    src_acct, price;
+       char    *username, *actualUsername;
+
+       src_acct = Bank_GetAcctByName(COKEBANK_SALES_ACCT);
+
+       if( OverridePrice > 0 )
+               price = OverridePrice;
+       else
+               price = Item->Price;
+
+       ret = _Transfer( src_acct, DestUser, price, "Refund");
+       if(ret) return ret;
+
+       username = Bank_GetAcctName(DestUser);
+       actualUsername = Bank_GetAcctName(ActualUser);
+       
+       Log_Info("refund '%s' (%s:%i) to %s by %s [cost %i, balance %i]",
+               Item->Name, Item->Handler->Name, Item->ID,
+               username, actualUsername, price, Bank_GetBalance(DestUser)
+               );
+
+       free(username);
+       free(actualUsername);
+
+       return 0;
+}
+
 /**
  * \brief Give money from one user to another
  */
@@ -92,8 +129,8 @@ int DispenseGive(int ActualUser, int SrcUser, int DestUser, int Ammount, const c
        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
                );
@@ -105,6 +142,7 @@ int DispenseGive(int ActualUser, int SrcUser, int DestUser, int Ammount, const c
        return 0;
 }
 
+#if 0 // Dead Code
 /**
  * \brief Move money from one user to another (Admin Only)
  */
@@ -126,8 +164,8 @@ int DispenseTransfer(int ActualUser, int SrcUser, int DestUser, int Ammount, con
        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
                );
@@ -138,6 +176,8 @@ int DispenseTransfer(int ActualUser, int SrcUser, int DestUser, int Ammount, con
        
        return 0;
 }
+#endif
+
 /**
  * \brief Add money to an account
  */
@@ -146,7 +186,11 @@ int DispenseAdd(int ActualUser, int User, int Ammount, const char *ReasonGiven)
         int    ret;
        char    *dstName, *byName;
        
+#if DISPENSE_ADD_BELOW_MIN
        ret = _Transfer( Bank_GetAcctByName(COKEBANK_DEBT_ACCT), User, Ammount, ReasonGiven );
+#else
+       ret = Bank_Transfer( Bank_GetAcctByName(COKEBANK_DEBT_ACCT), User, Ammount, ReasonGiven );
+#endif
        if(ret) return 2;
        
        byName = Bank_GetAcctName(ActualUser);
@@ -208,6 +252,35 @@ int DispenseDonate(int ActualUser, int User, int Ammount, const char *ReasonGive
        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)
 {
@@ -229,11 +302,11 @@ int _GetMinBalance(int Account)
        // - Internal accounts have no lower bound
        if( flags & USER_FLAG_INTERNAL )        return INT_MIN;
        
-       // Admin to -$10
-       //if( flags & USER_FLAG_ADMIN ) return -1000;
+       // Admin to -$50
+//     if( flags & USER_FLAG_ADMIN )   return -5000;
        
-       // Coke to -$5
-       //if( flags & USER_FLAG_COKE )  return -500;
+       // Coke to -$20
+//     if( flags & USER_FLAG_COKE )    return -2000;
        
        // Anyone else, non-negative
        return 0;
@@ -246,12 +319,12 @@ int _CanTransfer(int Source, int Destination, int Ammount)
 {
        if( Ammount > 0 )
        {
-               if( Bank_GetBalance(Source) + Ammount < _GetMinBalance(Source) )
+               if( Bank_GetBalance(Source) - Ammount < _GetMinBalance(Source) )
                        return 0;
        }
        else
        {
-               if( Bank_GetBalance(Destination) - Ammount < _GetMinBalance(Destination) )
+               if( Bank_GetBalance(Destination) + Ammount < _GetMinBalance(Destination) )
                        return 0;
        }
        return 1;

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