[Ante-cvs] SF.net SVN: ante: [491] trunk/ant/skills/commands/invcontrol.cpp
Brought to you by:
roguestar191
|
From: <rog...@us...> - 2007-08-15 06:29:58
|
Revision: 491
http://ante.svn.sourceforge.net/ante/?rev=491&view=rev
Author: roguestar191
Date: 2007-08-14 23:29:58 -0700 (Tue, 14 Aug 2007)
Log Message:
-----------
inventory control script
Added Paths:
-----------
trunk/ant/skills/commands/invcontrol.cpp
Added: trunk/ant/skills/commands/invcontrol.cpp
===================================================================
--- trunk/ant/skills/commands/invcontrol.cpp (rev 0)
+++ trunk/ant/skills/commands/invcontrol.cpp 2007-08-15 06:29:58 UTC (rev 491)
@@ -0,0 +1,228 @@
+// strhelp has readResponse(), strcpy
+#include "../include/strhelp.h"
+#include "../fcgi/fcgi.h"
+
+#include <sstream>
+#include <fstream>
+std::string username;
+char* ltoa( long long int value, char* result, int base) {
+ // check that the base if valid
+ if (base < 2 || base > 16) { *result = 0; return result; }
+ char* out = result;
+ long long int quotient = value;
+ do {
+ *out = "0123456789abcdef"[ std::abs( quotient % base ) ];
+ ++out;
+ quotient /= base;
+ } while ( quotient );
+ // Only apply negative sign for base 10
+ if ( value < 0 && base == 10) *out++ = '-';
+ std::reverse( result, out );
+ *out = 0;
+ return result;
+}
+
+
+FCGIServer *g_fs;
+int currequest;
+bool isGet(std::string &com) {
+ if(com.find("get") != std::string::npos) return true;
+ return false;
+}
+bool isDrop(std::string &com) {
+ if(com.find("drop") != std::string::npos) return true;
+ return false;
+}
+bool isWear(std::string &com) {
+ if(com.find("wear") != std::string::npos) return true;
+ return false;
+}
+bool isRemove(std::string &com) {
+ if(com.find("remove") != std::string::npos) return true;
+ return false;
+}
+bool isInventory(std::string &com) {
+ if(com.find("inventory") != std::string::npos) return true;
+ return false;
+}
+bool isEquipment(std::string &com) {
+ if(com.find("equipment") != std::string::npos) return true;
+ return false;
+}
+int mode(std::string &com) {
+ if(isGet(com)) return -1;
+ if(isDrop(com)) return 0;
+ if(isWear(com)) return 1;
+ if(isRemove(com)) return 2;
+ if(isInventory(com)) return 3;
+ if(isEquipment(com)) return 4;
+ return -2;
+}
+
+void Do_Get(std::string &arg) {
+ std::string output;
+ if(arg.compare("nonefound") == 0) {
+ output = "%print Get what?<BR>\n";
+ g_fs->Stdout2(output, currequest);
+ return;
+ }
+ output = "%get "; output += arg;
+ g_fs->Stdout2(output, currequest);
+ output = readStdin(g_fs, currequest, true);
+ trim(output);
+ if(output.compare("Failed") == 0) {
+ output = "%print You don't see a ";
+ output += arg;
+ output += " here.<BR>\n";
+ g_fs->Stdout2(output, currequest);
+ } else {
+ output = "%printroom ";
+
+ output += username;
+ output += " get <BR><S-NAME> <CHAN> a ";
+ output += arg;
+ output += "<BR>\n";
+ g_fs->Stdout2(output, currequest);
+ }
+}
+void Do_Wear(std::string &arg) {
+
+}
+void Do_Remove(std::string &arg) {
+
+}
+void Do_Drop(std::string &arg) {
+ std::string output;
+ if(arg.compare("nonefound") == 0) {
+ output = "%print Drop what?<BR>\n";
+ g_fs->Stdout2(output, currequest);
+ return;
+ }
+ output = "%drop "; output += arg;
+ g_fs->Stdout2(output, currequest);
+ output = readStdin(g_fs, currequest, true);
+ trim(output);
+ if(output.compare("Failed") == 0) {
+ output = "%print You don't have a ";
+ output += arg; output += "<BR>\n";
+ g_fs->Stdout2(output, currequest);
+ } else {
+ output = "%printroom ";
+ output += username;
+ output += " drop <BR><S-NAME> <CHAN> a ";
+ output += arg;
+ output += "<BR>\n";
+ g_fs->Stdout2(output, currequest);
+ }
+
+}
+std::string buildItemString(std::vector<std::string> &ivec) {
+ std::string output;
+ std::vector<std::string>::iterator iter = ivec.begin(), end = ivec.end();
+ for(;iter!=end;iter++) {
+ output += (*(iter));
+ output += "<BR>";
+ }
+ return output;
+}
+
+void Do_Inventory(std::string &arg) {
+ std::string output = "%display INVENTORY\n";
+ g_fs->Stdout2(output, currequest);
+ output = readStdin(g_fs, currequest, true);
+ trim(output);
+ if(output.compare("You don't have anything!") == 0) {
+ output = "%print You don't have anything!<BR>\n";
+ g_fs->Stdout2(output, currequest);
+ return;
+ }
+ replaceAll(output, "<QUO>", "\"");
+ replaceAll(output, "\"\"", "\" \"");
+ std::vector<std::string> ivec = strToVec(output);
+ output = "%print You are carrying ";
+ char res[100];
+ memset(res, '\0', 99);
+ output += ltoa( ivec.size(), res, 10);
+ output += " items<BR>";
+ output += buildItemString(ivec);
+ output += "\n";
+ g_fs->Stdout2(output, currequest);
+}
+void Do_Equipment(std::string &arg) {
+
+}
+
+void Do_Request(std::string &com, std::string &argument) {
+ int m = mode(com);
+ if(m == -2) return;
+ if(m == -1) Do_Get(argument);
+ else if(m == 0) Do_Drop(argument);
+ else if(m == 1) Do_Wear(argument);
+ else if(m == 2) Do_Remove(argument);
+ else if(m == 3) Do_Inventory(argument);
+ else if(m == 4) Do_Equipment(argument);
+}
+int main(int argc, const char *argv[]) {
+/* Handle Arguments */
+ const char *port = "6000";
+ if(argc != 3) {
+ goto errexit;
+ } else {
+ if(memcmp(argv[1], "-p", 2) != 0) goto errexit;
+ std::string test = argv[2];
+ for(size_t s = 0, e= test.size(); s != e; s++) {
+ if(!isdigit(test[s])) goto errexit;
+ }
+ port = argv[2];
+ }
+ goto norm;
+ errexit:
+ std::cerr << "Usage: Programname -p PORT#" << std::endl;
+ return 0;
+ norm:
+/* Arguments handled */
+/* StartFCGIServer */
+ FCGIServer *fs = new FCGIServer(port);
+/* Set global fcgi server pointer */
+ g_fs = fs;
+/* Create a vector to store clients*/
+ std::vector<FCGIRet> clients;
+/* Don't ever stop running, until we're killed */
+ while(true) {
+ /* Poll the server for connections */
+ clients = g_fs->Connections();
+ /* If no new connections are found and there's no data on existing connections */
+ if(clients.size() == 0) continue;
+ /* Create iterators to scroll through the clients */
+ std::vector<FCGIRet>::iterator iter = clients.begin(), end = clients.end();
+ /* Scroll through the clients */
+ for(;iter!=end;iter++) {
+ /* Set the current request number */
+ currequest = (*(iter)).requestid;
+ /* If the master server has sent the notification that it's done sending params variables */
+ if((*(iter)).isparams) {
+ std::cerr << "Found something..\n";
+ /* Start the request */
+ {
+ // print out some scripting commands, all will "provoke" the server into giving a response
+ std::string out = "\n%getaccess\n%skargs\n%username\n";
+ g_fs->Stdout2(out,currequest);
+ }
+ std::cerr << "Current Request: " << currequest << std::endl;
+ // read the first response (%getaccess)
+ std::string accessword = readStdin(g_fs, currequest, true);
+ // get rdif of \r\n
+ trim(accessword);
+ std::string argc = readStdin(g_fs, currequest, true);
+ trim(argc);
+ replaceAll(argc, "<QUO>", "\\\"");// This isn't necessary here, but done to show that the engine translates \" to <QUO> so it can easily pass through multiple parsers without the \'s being stripped every time and eventually making the quote disappear
+ username = readStdin(g_fs, currequest, true);
+ trim(username);
+ Do_Request(accessword, argc);
+ g_fs->endRequest(currequest);
+ }// if((*(iter)).isparams) {
+ }// for(;iter!=end;iter++) {
+ }// while(true) {
+ delete fs;
+ return 0;
+};
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|