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
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");
}
// If the item isn't availiable for sale, return -1 (so it's skipped)
- if( status )
+ if( status || price >= giUserBalance )
Index = -1;
return Index;
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);
}
}
#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
* \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)
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);
}
// 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
{
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);
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;
}
#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);
/*
* 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;
}
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);
// 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);
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
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;
-
- // KNOWN HACK: Naming a slot "dead" disables it
+ // HACK: Naming a slot "dead" disables it
if( strcmp(Item->Name, "dead") == 0 )
return 1;
int src_acct, price;
char *username, *actualUsername;
- src_acct = Bank_GetAcctByName(COKEBANK_SALES_ACCT);
+ src_acct = _GetSalesAcct(Item);
if( OverridePrice > 0 )
price = OverridePrice;
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_DEBT_ACCT,1), Ammount, ReasonGiven );
if(ret) return 2;
byName = Bank_GetAcctName(ActualUser);
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);
+}
}
// 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);
}
// 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 ;
return ;
}
- uid = Bank_GetAcctByName(username);
+ uid = Bank_GetAcctByName(username, 0);
if( uid == -1 ) {
sendf(Client->Socket, "404 Unknown user\n");
return ;
}
// Get recipient
- uid = Bank_GetAcctByName(recipient);
+ uid = Bank_GetAcctByName(recipient, 0);
if( uid == -1 ) {
sendf(Client->Socket, "404 Invalid target user\n");
return ;
}
// Get recipient
- uid = Bank_GetAcctByName(user);
+ uid = Bank_GetAcctByName(user, 0);
if( uid == -1 ) {
sendf(Client->Socket, "404 Invalid user\n");
return ;
}
// Get recipient
- uid = Bank_GetAcctByName(user);
+ uid = Bank_GetAcctByName(user, 0);
if( uid == -1 ) {
sendf(Client->Socket, "404 Invalid user\n");
return ;
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 ) {
}
// 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 ;