Misc changes, have flu so don't remember all of them
authorJohn Hodge <[email protected]>
Sun, 13 Mar 2011 11:42:55 +0000 (19:42 +0800)
committerJohn Hodge <[email protected]>
Sun, 13 Mar 2011 11:42:55 +0000 (19:42 +0800)
src/Makefile
src/client/main.c
src/cokebank.h
src/cokebank_sqlite/main.c
src/server/dispense.c
src/server/server.c

index f577661..99ac060 100644 (file)
@@ -3,13 +3,13 @@
 
 all:
        @make -C cokebank_sqlite all
-       @make -C cokebank_basic all
+#      @make -C cokebank_basic all
        @make -C server all
        @make -C client all
 
 clean:
        @make -C cokebank_sqlite clean
-       @make -C cokebank_basic clean
+#      @make -C cokebank_basic clean
        @make -C server clean
        @make -C client clean
 
index 0995d3b..598e747 100644 (file)
@@ -286,12 +286,25 @@ int main(int argc, char *argv[])
                        case 'n':       // Dry Run / read-only
                                gbDryRun = 1;
                                break;
+                       case '-':
+                               if( strcmp(argv[i], "--help") == 0 ) {
+                                       ShowUsage();
+                                       return 0;
+                               }
+                               else {
+                                       fprintf(stderr, "%s: Unknown switch '%s'\n", argv[0], argv[i]);
+                                       ShowUsage();
+                                       return RV_ARGUMENTS;
+                               }
+                               break;
                        default:
-//                             if( !isdigit(argv[i][0]) ) {
-//                                     fprintf(stderr, "%s: Unknown switch '%s'\n", argv[0], argv[i]);
-//                                     ShowUsage();
-//                                     return RV_ARGUMENTS;
-//                             }
+                               // The first argument is not allowed to begin with 'i'
+                               // (catches most bad flags)
+                               if( text_argc == 0 ) {
+                                       fprintf(stderr, "%s: Unknown switch '%s'\n", argv[0], argv[i]);
+                                       ShowUsage();
+                                       return RV_ARGUMENTS;
+                               }
                                if( text_argc + 1 ==  MAX_TXT_ARGS )
                                {
                                        fprintf(stderr, "ERROR: Too many arguments\n");
@@ -1076,7 +1089,7 @@ int ShowItemAt(int Row, int Col, int Width, int Index, int bHilighted)
        }
        
        // If the item isn't availiable for sale, return -1 (so it's skipped)
-       if( status )
+       if( status || price >= giUserBalance )
                Index = -1;
        
        return Index;
@@ -1990,7 +2003,7 @@ void _PrintUserLine(const char *Line)
                flags[flagsLen] = '\0';
                
                bal = atoi(Line + matches[4].rm_so);
-               printf("%-15s: $%4i.%02i (%s)\n", username, bal/100, abs(bal)%100, flags);
+               printf("%-15s: $%8.02f (%s)\n", username, ((float)bal)/100, flags);
        }
 }
 
index e5a4389..2057a5d 100644 (file)
@@ -17,6 +17,7 @@
 #include <stdlib.h>
 
 #define COKEBANK_SALES_ACCT    ">sales"        //!< Sales made into
+#define COKEBANK_SALES_PREFIX  ">sales:"       //!< Sales made into
 #define COKEBANK_DEBT_ACCT     ">liability"    //!< Credit taken out of
 #define COKEBANK_FREE_ACCT     ">freeitems"    //!< ODay drink costs taken out of
 
@@ -135,7 +136,7 @@ extern char *Bank_GetAcctName(int AcctID);
  * \param Name Name to search for
  * \return ID of the account, or -1 if not found
  */
-extern int     Bank_GetAcctByName(const char *Name);
+extern int     Bank_GetAcctByName(const char *Name, int bCreate);
 /**
  * \brief Create a new account
  * \param Name Name for the new account (if NULL, an anoymous account is created)
index bf4d3f2..34761b2 100644 (file)
@@ -64,6 +64,7 @@ struct sAcctIterator  // Unused really, just used as a void type
  int   Bank_GetBalance(int AcctID);
 char   *Bank_GetAcctName(int AcctID);
 sqlite3_stmt   *Bank_int_MakeStatemnt(sqlite3 *Database, const char *Query);
+ int   Bank_int_QueryNone(sqlite3 *Database, const char *Query, char **ErrorMessage);
 sqlite3_stmt   *Bank_int_QuerySingle(sqlite3 *Database, const char *Query);
  int   Bank_int_IsValidName(const char *Name);
 
@@ -86,7 +87,7 @@ int Bank_Initialise(const char *Argument)
        }
 
        // Check structure
-       rv = sqlite3_exec(gBank_Database, "SELECT acct_id FROM accounts LIMIT 1", NULL, NULL, &errmsg);
+       rv = Bank_int_QueryNone(gBank_Database, "SELECT acct_id FROM accounts LIMIT 1", &errmsg);
        if( rv == SQLITE_OK )
        {
                // NOP
@@ -95,7 +96,7 @@ int Bank_Initialise(const char *Argument)
        {
                sqlite3_free(errmsg);
                // Create tables
-               rv = sqlite3_exec(gBank_Database, csBank_DatabaseSetup, NULL, NULL, &errmsg);
+               rv = Bank_int_QueryNone(gBank_Database, csBank_DatabaseSetup, &errmsg);
                if( rv != SQLITE_OK ) {
                        fprintf(stderr, "Bank_Initialise - SQLite Error: %s\n", errmsg);
                        sqlite3_free(errmsg);
@@ -127,36 +128,36 @@ int Bank_Transfer(int SourceUser, int DestUser, int Ammount, const char *Reason)
        Reason = "";    // Shut GCC up
        
        // Begin SQL Transaction
-       sqlite3_exec(gBank_Database, "BEGIN TRANSACTION", NULL, NULL, NULL);
+       Bank_int_QueryNone(gBank_Database, "BEGIN TRANSACTION", NULL);
 
        // Take from the source
-       query = mkstr("UPDATE accounts SET acct_balance=acct_balance-%i,acct_last_seen=datetime('now') WHERE acct_id=%i", Ammount, SourceUser);
+       query = mkstr("UPDATE accounts SET acct_balance=acct_balance%+i,acct_last_seen=datetime('now') WHERE acct_id=%i", -Ammount, SourceUser);
 //     printf("query = \"%s\"\n", query);
-       rv = sqlite3_exec(gBank_Database, query, NULL, NULL, &errmsg);
+       rv = Bank_int_QueryNone(gBank_Database, query, &errmsg);
        free(query);
        if( rv != SQLITE_OK )
        {
                fprintf(stderr, "Bank_Transfer - SQLite Error: %s\n", errmsg);
                sqlite3_free(errmsg);
-               sqlite3_exec(gBank_Database, "ROLLBACK", NULL, NULL, NULL);
+               Bank_int_QueryNone(gBank_Database, "ROLLBACK",  NULL);
                return 1;
        }
 
        // Give to the destination
-       query = mkstr("UPDATE accounts SET acct_balance=acct_balance+%i,acct_last_seen=datetime('now') WHERE acct_id=%i", Ammount, DestUser);
+       query = mkstr("UPDATE accounts SET acct_balance=acct_balance%+i,acct_last_seen=datetime('now') WHERE acct_id=%i", Ammount, DestUser);
 //     printf("query = \"%s\"\n", query);
-       rv = sqlite3_exec(gBank_Database, query, NULL, NULL, &errmsg);
+       rv = Bank_int_QueryNone(gBank_Database, query, &errmsg);
        free(query);
        if( rv != SQLITE_OK )
        {
                fprintf(stderr, "Bank_Transfer - SQLite Error: %s\n", errmsg);
                sqlite3_free(errmsg);
-               sqlite3_exec(gBank_Database, "ROLLBACK", NULL, NULL, NULL);
+               Bank_int_QueryNone(gBank_Database, "ROLLBACK", NULL);
                return 1;
        }
 
        // Commit transaction
-       sqlite3_exec(gBank_Database, "COMMIT", NULL, NULL, NULL);
+       Bank_int_QueryNone(gBank_Database, "COMMIT", NULL);
 
        return 0;
 }
@@ -225,7 +226,7 @@ int Bank_SetFlags(int UserID, int Mask, int Value)
        #endif
 
        // Execute Query
-       rv = sqlite3_exec(gBank_Database, query, NULL, NULL, &errmsg);
+       rv = Bank_int_QueryNone(gBank_Database, query, &errmsg);
        if( rv != SQLITE_OK )
        {
                fprintf(stderr, "Bank_SetUserFlags - SQLite Error: %s\n", errmsg);
@@ -286,23 +287,36 @@ char *Bank_GetAcctName(int AcctID)
 /*
  * Get an account ID from a name
  */
-int Bank_GetAcctByName(const char *Name)
+int Bank_GetAcctByName(const char *Name, int bCreate)
 {
        char    *query;
        sqlite3_stmt    *statement;
         int    ret;
        
-       if( !Bank_int_IsValidName(Name) )       return -1;
+//     printf("Bank_GetAcctByName: (Name='%s',bCreate=%i)\n", Name, bCreate);
+
+       if( !Bank_int_IsValidName(Name) ) {
+//             printf("RETURN: -1 (Bad name)");
+               return -1;
+       }
        
        query = mkstr("SELECT acct_id FROM accounts WHERE acct_name='%s' LIMIT 1", Name);
        statement = Bank_int_QuerySingle(gBank_Database, query);
        free(query);
-       if( !statement )        return -1;
+       if( !statement ) {
+//             printf("User not found\n");
+               if( bCreate )   return Bank_CreateAcct(Name);
+               return -1;
+       }
        
        ret = sqlite3_column_int(statement, 0);
        sqlite3_finalize(statement);
        
-       if( ret == 0 )  return -1;
+//     printf("ret = %i\n", ret);
+
+       if( ret == 0 ) {
+               return -1;
+       }
        return ret;
 }
 
@@ -325,7 +339,7 @@ int Bank_CreateAcct(const char *Name)
                query = strdup("INSERT INTO accounts (acct_name) VALUES (NULL)");
        }
                
-       rv = sqlite3_exec(gBank_Database, query, NULL, NULL, &errmsg);
+       rv = Bank_int_QueryNone(gBank_Database, query, &errmsg);
        if( rv != SQLITE_OK )
        {
                fprintf(stderr, "Bank_CreateAcct - SQLite Error: '%s'\n", errmsg);
@@ -506,7 +520,7 @@ int Bank_AddAcctCard(int AcctID, const char *CardID)
        // Insert card
        query = mkstr("INSERT INTO cards (acct_id,card_name) VALUES (%i,'%s')",
                AcctID, CardID);
-       rv = sqlite3_exec(gBank_Database, query, NULL, NULL, &errmsg);
+       rv = Bank_int_QueryNone(gBank_Database, query, &errmsg);
        if( rv == SQLITE_CONSTRAINT )
        {
                sqlite3_free(errmsg);
@@ -543,6 +557,14 @@ sqlite3_stmt *Bank_int_MakeStatemnt(sqlite3 *Database, const char *Query)
        return ret;
 }
 
+int Bank_int_QueryNone(sqlite3 *Database, const char *Query, char **ErrorMessage)
+{
+       #if DEBUG
+       printf("Bank_int_QueryNone: (Query='%s')\n", Query);
+       #endif
+       return sqlite3_exec(Database, Query, NULL, NULL, ErrorMessage);
+}
+
 /*
  * Create a SQLite statement and query it for the first row
  * Returns NULL if the the set is empty
index 4c6bd51..7061670 100644 (file)
@@ -8,6 +8,7 @@
  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 ===
 /**
@@ -21,7 +22,9 @@ int DispenseItem(int ActualUser, int User, tItem *Item)
        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) )
@@ -29,9 +32,7 @@ int DispenseItem(int ActualUser, int User, tItem *Item)
                return 2;       // 2: No balance
        }
        
-       handler = Item->Handler;
-       
-       // KNOWN HACK: Naming a slot "dead" disables it
+       // HACK: Naming a slot "dead" disables it
        if( strcmp(Item->Name, "dead") == 0 )
                return 1;
        
@@ -86,7 +87,7 @@ int DispenseRefund(int ActualUser, int DestUser, tItem *Item, int OverridePrice)
         int    src_acct, price;
        char    *username, *actualUsername;
 
-       src_acct = Bank_GetAcctByName(COKEBANK_SALES_ACCT);
+       src_acct = _GetSalesAcct(Item);
 
        if( OverridePrice > 0 )
                price = OverridePrice;
@@ -187,9 +188,9 @@ int DispenseAdd(int ActualUser, int User, int Ammount, const char *ReasonGiven)
        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;
        
@@ -211,7 +212,7 @@ int DispenseSet(int ActualUser, int User, int Balance, const char *ReasonGiven)
         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);
@@ -236,7 +237,7 @@ int DispenseDonate(int ActualUser, int User, int Ammount, const char *ReasonGive
        
        if( Ammount < 0 )       return 2;
        
-       ret = _Transfer( User, Bank_GetAcctByName(COKEBANK_DEBT_ACCT), Ammount, ReasonGiven );
+       ret = _Transfer( User, Bank_GetAcctByName(COKEBANK_DEBT_ACCT,1), Ammount, ReasonGiven );
        if(ret) return 2;
        
        byName = Bank_GetAcctName(ActualUser);
@@ -336,3 +337,11 @@ int _Transfer(int Source, int Destination, int Ammount, const char *Reason)
                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);
+}
index 158726c..dd8a294 100644 (file)
@@ -479,7 +479,7 @@ void Server_Cmd_AUTOAUTH(tClient *Client, char *Args)
        }
        
        // Get UID
-       Client->UID = Bank_GetAcctByName( username );   
+       Client->UID = Bank_GetAcctByName( username, 0 );        
        if( Client->UID < 0 ) {
                if(giDebugLevel)
                        Debug(Client, "Unknown user '%s'", username);
@@ -539,7 +539,7 @@ void Server_Cmd_SETEUSER(tClient *Client, char *Args)
        }
        
        // Set id
-       Client->EffectiveUID = Bank_GetAcctByName(username);
+       Client->EffectiveUID = Bank_GetAcctByName(username, 0);
        if( Client->EffectiveUID == -1 ) {
                sendf(Client->Socket, "404 User not found\n");
                return ;
@@ -748,7 +748,7 @@ void Server_Cmd_REFUND(tClient *Client, char *Args)
                return ;
        }
 
-       uid = Bank_GetAcctByName(username);
+       uid = Bank_GetAcctByName(username, 0);
        if( uid == -1 ) {
                sendf(Client->Socket, "404 Unknown user\n");
                return ;
@@ -790,7 +790,7 @@ void Server_Cmd_GIVE(tClient *Client, char *Args)
        }
 
        // Get recipient
-       uid = Bank_GetAcctByName(recipient);
+       uid = Bank_GetAcctByName(recipient, 0);
        if( uid == -1 ) {
                sendf(Client->Socket, "404 Invalid target user\n");
                return ;
@@ -901,7 +901,7 @@ void Server_Cmd_ADD(tClient *Client, char *Args)
        }
 
        // Get recipient
-       uid = Bank_GetAcctByName(user);
+       uid = Bank_GetAcctByName(user, 0);
        if( uid == -1 ) {
                sendf(Client->Socket, "404 Invalid user\n");
                return ;
@@ -962,7 +962,7 @@ void Server_Cmd_SET(tClient *Client, char *Args)
        }
 
        // Get recipient
-       uid = Bank_GetAcctByName(user);
+       uid = Bank_GetAcctByName(user, 0);
        if( uid == -1 ) {
                sendf(Client->Socket, "404 Invalid user\n");
                return ;
@@ -1175,7 +1175,7 @@ void Server_Cmd_USERINFO(tClient *Client, char *Args)
        if( giDebugLevel )      Debug(Client, "User Info '%s'", user);
        
        // Get recipient
-       uid = Bank_GetAcctByName(user);
+       uid = Bank_GetAcctByName(user, 0);
        
        if( giDebugLevel >= 2 ) Debug(Client, "uid = %i", uid);
        if( uid == -1 ) {
@@ -1270,7 +1270,7 @@ void Server_Cmd_USERFLAGS(tClient *Client, char *Args)
        }
        
        // Get UID
-       uid = Bank_GetAcctByName(username);
+       uid = Bank_GetAcctByName(username, 0);
        if( uid == -1 ) {
                sendf(Client->Socket, "404 User '%s' not found\n", username);
                return ;

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