From 88dc83a1cdc8df8d74078f1d41726095d5a26d61 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Fri, 4 Mar 2011 17:58:33 +0800 Subject: [PATCH] Hey, `dispense -s` implemented! --- src/client/main.c | 122 ++++++++++++++++++++++++++++++++++++++++++-- src/server/itemdb.c | 13 ++++- src/server/server.c | 2 +- 3 files changed, 130 insertions(+), 7 deletions(-) diff --git a/src/client/main.c b/src/client/main.c index 71f58f9..fd5915d 100644 --- a/src/client/main.c +++ b/src/client/main.c @@ -88,6 +88,7 @@ void PopulateItemList(int Socket); void _PrintUserLine(const char *Line); int Dispense_AddUser(int Socket, const char *Username); int Dispense_SetUserType(int Socket, const char *Username, const char *TypeString); + int Dispense_SetItem(int Socket, const char *Type, int ID, int NewPrice, const char *NewName); // --- Helpers --- char *ReadLine(int Socket); int sendf(int Socket, const char *Format, ...); @@ -138,6 +139,8 @@ void ShowUsage(void) " Alter a account value\n" " dispense refund []\n" " Refund an item to a user (with optional price override)\n" + " dispense slot \n" + " Rename/Re-price a slot\n" " == Dispense administrators ==\n" " dispense acct = \"\"\n" " Set an account balance\n" @@ -263,6 +266,17 @@ int main(int argc, char *argv[]) giDispensePort = atoi(argv[++i]); break; + // Set slot name/price + case 's': + if( text_argc != 0 ) { + fprintf(stderr, "%s: -s must appear before other arguments\n", argv[0]); + ShowUsage(); + return RV_ARGUMENTS; + } + text_args[0] = "slot"; // HACK!! + text_argc ++; + break; + case 'G': // Don't use GUI giUIMode = UI_MODE_BASIC; break; @@ -359,7 +373,7 @@ int main(int argc, char *argv[]) // // `dispense give` // - "Here, have some money." - if( strcmp(text_args[0], "give") == 0 ) + else if( strcmp(text_args[0], "give") == 0 ) { if( text_argc != 4 ) { fprintf(stderr, "`dispense give` takes three arguments\n"); @@ -523,8 +537,64 @@ int main(int argc, char *argv[]) close(sock); return ret; } + // Set slot + else if( strcmp(text_args[0], "slot") == 0 ) + { + regmatch_t matches[3]; + char *item_type, *newname; + int item_id, price; + + // Check arguments + if( text_argc != 4 ) { + fprintf(stderr, "Error: `dispense slot` takes three arguments\n"); + ShowUsage(); + return RV_ARGUMENTS; + } + + // Parse arguments + if( RunRegex(&gUserItemIdentRegex, text_args[1], 3, matches, NULL) != 0 ) { + fprintf(stderr, "Error: Invalid item ID passed (: expected)\n"); + return RV_ARGUMENTS; + } + item_type = text_args[1] + matches[1].rm_so; + text_args[1][ matches[1].rm_eo ] = '\0'; + item_id = atoi( text_args[1] + matches[2].rm_so ); + + // - Price + price = atoi( text_args[2] ); + if( price <= 0 && text_args[2][0] != '0' ) { + fprintf(stderr, "Error: Invalid price passed (must be >= 0)\n"); + return RV_ARGUMENTS; + } + + // - New name + newname = text_args[3]; + // -- Sanity + { + char *pos; + for( pos = newname; *pos; pos ++ ) + { + if( !isalnum(*pos) && *pos != ' ' ) { + fprintf(stderr, "Error: You should only have letters, numbers and spaces in an item name\n"); + return RV_ARGUMENTS; + } + } + } + + // Connect & Authenticate + sock = OpenConnection(gsDispenseServer, giDispensePort); + if( sock < 0 ) return RV_SOCKET_ERROR; + ret = Authenticate(sock); + if(ret) return ret; + // Update the slot + ret = Dispense_SetItem(sock, item_type, item_id, price, newname); + + close(sock); + return ret; + } // Item name / pattern - else { + else + { gsItemPattern = text_args[0]; } @@ -1995,18 +2065,62 @@ int Dispense_SetUserType(int Socket, const char *Username, const char *TypeStrin case 403: printf("Only wheel can modify users\n"); - ret = 1; + ret = RV_PERMISSIONS; break; case 404: printf("User '%s' does not exist\n", Username); - ret = 0; + ret = RV_INVALID_USER; break; case 407: printf("Flag string is invalid\n"); + ret = RV_ARGUMENTS; + break; + + default: + fprintf(stderr, "Unknown response code %i '%s'\n", responseCode, buf); + ret = RV_UNKNOWN_RESPONSE; + break; + } + + free(buf); + + return ret; +} + +int Dispense_SetItem(int Socket, const char *Type, int ID, int NewPrice, const char *NewName) +{ + char *buf; + int responseCode, ret; + + // Check for a dry run + if( gbDryRun ) { + printf("Dry Run - No action\n"); + return 0; + } + + sendf(Socket, "UPDATE_ITEM %s:%i %i %s\n", Type, ID, NewPrice, NewName); + + buf = ReadLine(Socket); + responseCode = atoi(buf); + + switch(responseCode) + { + case 200: + printf("Item %s:%i updated\n", Type, ID); ret = 0; break; + + case 403: + printf("Only coke members can modify the slots\n"); + ret = RV_PERMISSIONS; + break; + + case 406: + printf("Invalid item passed\n"); + ret = RV_BAD_ITEM; + break; default: fprintf(stderr, "Unknown response code %i '%s'\n", responseCode, buf); diff --git a/src/server/itemdb.c b/src/server/itemdb.c index dc188fd..a88fad8 100644 --- a/src/server/itemdb.c +++ b/src/server/itemdb.c @@ -263,6 +263,8 @@ void Items_UpdateFile(void) int num; tHandler *handler; + trim(buffer); + lineNum ++; line_items[lineNum-1] = -1; line_comments[lineNum-1] = NULL; @@ -326,7 +328,8 @@ void Items_UpdateFile(void) fclose(fp); - fp = fopen("items.cfg.new", "w"); // DEBUG: Don't kill the real item file until debugged + //fp = fopen("items.cfg.new", "w"); // DEBUG: Don't kill the real item file until debugged + fp = fopen(gsItemListFile, "w"); // Create new file { @@ -342,8 +345,11 @@ void Items_UpdateFile(void) if( done_items[ line_items[i] ] ) { fprintf(fp, "; DUP -"); } - done_items[ line_items[i] ] = 1; + + if( item->bHidden ) + fprintf(fp, "-"); + fprintf(fp, "%s\t%i\t%i\t%s\t", item->Handler->Name, item->ID, item->Price, item->Name ); @@ -363,6 +369,9 @@ void Items_UpdateFile(void) tItem *item = &gaItems[i]; if( done_items[i] ) continue ; + if( item->bHidden ) + fprintf(fp, "-"); + fprintf(fp, "%s\t%i\t%i\t%s\n", item->Handler->Name, item->ID, item->Price, item->Name ); diff --git a/src/server/server.c b/src/server/server.c index 4404f9e..26c7fb2 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -1318,7 +1318,7 @@ void Server_Cmd_UPDATEITEM(tClient *Client, char *Args) price = atoi(price_str); if( price <= 0 && price_str[0] != '0' ) { - sendf(CLient->Socket, "407 Invalid price set\n"); + sendf(Client->Socket, "407 Invalid price set\n"); } // Update the item -- 2.20.1