From: Keith F. <ven...@us...> - 2003-10-21 01:15:17
|
Update of /cvsroot/planeshift/planeshift/src/server In directory sc8-pr-cvs1:/tmp/cvs-serv22526 Modified Files: usermanager.h usermanager.cpp Log Message: Committing patch by Kronon for /clear command and for /advisor and /advice commands. Not tested yet but will test after resynching with new CS cvs. Index: usermanager.h =================================================================== RCS file: /cvsroot/planeshift/planeshift/src/server/usermanager.h,v retrieving revision 1.20 retrieving revision 1.21 diff -C2 -d -r1.20 -r1.21 *** usermanager.h 18 Oct 2003 21:25:38 -0000 1.20 --- usermanager.h 20 Oct 2003 22:53:58 -0000 1.21 *************** *** 38,41 **** --- 38,50 ---- class AdminManager; + struct AdvisorStruct + { + int id; + int connection_id; + int client_id; + csString request; + bool ready; + }; + class UserManager : public iNetSubscriber { *************** *** 84,87 **** --- 93,102 ---- void StopAttack(psUserCmdMessage& msg,Client *client,int clientnum); void HandleLoot(Client *client); + void Advisor(int userId, int connectionId, psUserCmdMessage& msg); + void AddAdvisor(int id, int connectionId); + void RemoveAdvisor(int id, int connectionId); + void GetAdvice(int request_id, const char* message); + void GiveAdvice(int id, const char* message); + bool LogAdvice(int playerID, const char* advice, const char* request); void HandleTraining(Client *client); *************** *** 103,106 **** --- 118,124 ---- AdminManager *adminmanager; csTicks nextUserStatRegeneration; + csArray<AdvisorStruct> advisors; + int advisorPos; + int advisorsNr; }; Index: usermanager.cpp =================================================================== RCS file: /cvsroot/planeshift/planeshift/src/server/usermanager.cpp,v retrieving revision 1.79 retrieving revision 1.80 diff -C2 -d -r1.79 -r1.80 *** usermanager.cpp 18 Oct 2003 21:25:38 -0000 1.79 --- usermanager.cpp 20 Oct 2003 22:53:58 -0000 1.80 *************** *** 75,78 **** --- 75,81 ---- entitymanager = NULL; adminmanager = NULL; + advisors.Empty(); + advisorsNr = 0; + advisorPos = 0; msghandler->Subscribe(this,MSGTYPEUSERCMD); *************** *** 218,222 **** Assist( msg, client, me->clientnum ); } ! else { psSystemMessage newmsg(me->clientnum,MSG_ERROR,"Command not supported by server yet."); --- 221,233 ---- Assist( msg, client, me->clientnum ); } ! else if (msg.command == "/advisor") ! { ! Advisor(client->GetPlayerID(), me->clientnum, msg); ! } ! else if (msg.command == "/advice") ! { ! GiveAdvice(me->clientnum, msg.filter+8); ! } ! else { psSystemMessage newmsg(me->clientnum,MSG_ERROR,"Command not supported by server yet."); *************** *** 227,232 **** Bug2("Could not create valid psSystemMessage for client %u.\n",me->clientnum); } - } } --- 238,243 ---- Bug2("Could not create valid psSystemMessage for client %u.\n",me->clientnum); } } + } *************** *** 920,923 **** --- 931,935 ---- } + void UserManager::HandleTraining(Client *client) { *************** *** 962,965 **** --- 974,1171 ---- } + /*Some functions for the advisor system + */ + + void UserManager::Advisor(int userId, int connectionId, psUserCmdMessage& msg) + { + //NOTE: filter is miss used to carry the whole command line + + if( msg.filter == "/advisor on") + { + AddAdvisor(userId, connectionId); + } + else if( msg.filter == "/advisor off") + { + RemoveAdvisor(userId, connectionId); + } + else if( msg.filter != "/advisor" && msg.filter != "/advisor ") + { + GetAdvice(connectionId, msg.filter+9); + } + } + + void UserManager::AddAdvisor(int id, int connectionId) + { + for(int i = 0; i < advisorsNr; i++) + { + if( id == advisors[i].id) + return; + } + AdvisorStruct advisor; + advisor.id = id; + advisor.client_id = connectionId; + advisor.connection_id = 0; + advisor.ready = true; + + advisors.Push(advisor); + advisorsNr++; + //Send message to advisor that he is an advisor + psSystemMessage newmsg(connectionId,MSG_INFO,"Your request to become an advisor has been granted."); + if (newmsg.valid) + msghandler->SendMessage(newmsg.msg); + else + Bug2("Could not create valid psSystemMessage::AddAdvisor for client %u.\n",connectionId); + + printf("Advisor has been added\n"); + } + + void UserManager::RemoveAdvisor(int id, int connectionId) + { + //Remove an advisor + for(int i = 0; i < advisorsNr; i++) + { + if(advisors[i].id = id) + { + advisors.DeleteIndex(i); + advisorsNr--; + } + } + //Send message to non-advisor that he isn't an advisor any more + psSystemMessage newmsg(connectionId,MSG_INFO,"You have just layed down your advisor role."); + if (newmsg.valid) + msghandler->SendMessage(newmsg.msg); + else + Bug2("Could not create valid psSystemMessage::RemoveAdvisor for client %u.\n",connectionId); + printf("Advisor has been removed\n"); + } + + void UserManager::GetAdvice(int requestId, const char* message) + { + printf("User asked for advice\n"); + + if( advisorsNr == 0) //No advisor is online + { + //Send back an message that there is no advisor online + psSystemMessage newmsg(requestId,MSG_INFO,"The world has no advisor at the moment. You are now allowed to shout you question to other players. You can do it this way \"/shout <question here>\"."); + if (newmsg.valid) + msghandler->SendMessage(newmsg.msg); + else + Bug2("Could not create valid psSystemMessage::GetAdvice for client %u.\n",requestId); + return; + } + else //An advisor is online + { + bool ready = false; //We found an advisor or all advisors are taken + bool busy = false; //If true all advisors are taken + int loopCount = 0; //To look if we have had all advisors + + while(!ready) + { + if(advisorPos >= advisorsNr) //Keep advisorPos go in a loop + advisorPos = 0; + if(advisors[advisorPos].ready == false) //Go to the next advisor in the hope he isn't busy + { + if(loopCount >= advisorsNr) //Every one is busy, so stop looking + { + busy = true; + ready = true; + printf("Every one is busy\n"); + //Send this message to the client + psSystemMessage allbusymsg(requestId,MSG_INFO,"All advisors are busy, please wait a while and ask again."); + if (allbusymsg.valid) + msghandler->SendMessage(allbusymsg.msg); + else + Bug2("Could not create valid psSystemMessage::GetAdvice for client %u.\n",requestId); + } + advisorPos++; + loopCount++; + continue; + } + else + { + advisors[advisorPos].connection_id = requestId; + advisors[advisorPos].request = message; + advisors[advisorPos].ready = false; + //send advisors[advisorPos].id the request message + psSystemMessage amsg(advisors[advisorPos].client_id,MSG_INFO,"You have received a message with a request for help:"); + if (amsg.valid) + msghandler->SendMessage(amsg.msg); + else + Bug2("Could not create valid psSystemMessage::GiveAdvice for client %u.\n",advisors[advisorPos].client_id); + psSystemMessage msg(advisors[advisorPos].client_id,MSG_INFO,message); + if (msg.valid) + msghandler->SendMessage(msg.msg); + else + Bug2("Could not create valid psSystemMessage::GiveAdvice for client %u.\n",advisors[advisorPos].client_id); + //Send a message to the client that the request has been send. + psSystemMessage newmsg(requestId,MSG_INFO,"A messanger has been send with a request for help."); + if (newmsg.valid) + msghandler->SendMessage(newmsg.msg); + else + Bug2("Could not create valid psSystemMessage::GetAdvice for client %u.\n",requestId); + ready = true; + } + advisorPos++; + loopCount++; + } + } + } + + void UserManager::GiveAdvice(int id, const char* message) + { + bool isRequest = false; + int advisor = 0; + //Search loop that will found the right advisor nr in the advisors array and use it as an id + for(int i = 0; i < advisorsNr; i++) + { + if((advisors[i].client_id == id) && (advisors[i].ready == false)) + { + advisor = i; + isRequest = true; + } + } + if( !isRequest ) + return; + + //Send message to advisors[id].connection_id + psSystemMessage msg(advisors[advisor].connection_id,MSG_INFO,"A messanger arrived with the following message:"); + if (msg.valid) + msghandler->SendMessage(msg.msg); + else + Bug2("Could not create valid psSystemMessage::GiveAdvice for client %u.\n",id); + psSystemMessage newmsg(advisors[advisor].connection_id,MSG_INFO,message); + if (newmsg.valid) + msghandler->SendMessage(newmsg.msg); + else + Bug2("Could not create valid psSystemMessage::GiveAdvice for client %u.\n",id); + + //Add message to database + if (!LogAdvice(advisors[advisor].id, message, advisors[advisor].request)) + { + psSystemMessage error(id,MSG_ERROR, "SQL Error: %s", database->GetLastError()); + if (error.valid) + msghandler->SendMessage(error.msg); + return; + } + + advisors[advisor].ready = true; + } + + bool UserManager::LogAdvice(int playerID, const char* advice, const char* request) + { + /* The columns in the table NOT included in this command + * have default values and thus we do not need to put them in + * the INSERT statement + */ + char adviceEsc[513]; + db->Escape(adviceEsc, advice); + char requestEsc[513]; + db->Escape(requestEsc, request); + int result = db->Command("INSERT INTO petitions " + "(player,petition,created_date,status,resolution,escalation_level,category) " + "VALUES (%d,\"%s\",Now(),\"Closed\",\"%s\",\"0\",\"advice\")",playerID, requestEsc,adviceEsc); + + return (result != -1); + } |