• Welcome to Valhalla Legends Archive.
 

Queue System

Started by H0CKEY, February 01, 2004, 07:24 PM

Previous topic - Next topic

H0CKEY

How would one go about adding a effiecient queue system to a vb or c++ bot. If you could I would like examples of what would make a good queue system or examples of a good queue system that exists.

Thanks for your time

Newby

Use an array and a timer.

That's about as general as I can get since you didn't specify C++ or VB. You said both. :P
- Newby

Quote[17:32:45] * xar sets mode: -oooooooooo algorithm ban chris cipher newby stdio TehUser tnarongi|away vursed warz
[17:32:54] * xar sets mode: +o newby
[17:32:58] <xar> new rule
[17:33:02] <xar> me and newby rule all

Quote<TehUser> Man, I can't get Xorg to work properly.  This sucks.
<torque> you should probably kill yourself
<TehUser> I think I will.  Thanks, torque.

H0CKEY


DarkMinion


K

#4
For C++ I use a std::priority_queue<BnMsg> with a comparison object that gives certain commands (/ban, /kick) higher priority so that they are executed first.


#include <string>
#include <queue>

struct BnMsg
{
public:
   std::string Text;

   BnMsg();
   BnMsg(const BnMsg& b);
   BnMsg(const std::string& s);
   BnMsg(const char* c);
   BnMsg& operator=(const std::string& s);
   BnMsg& operator=(const BnMsg& b);
   BnMsg& operator=(const char* c);
   
   ~BnMsg();

  friend bool operator<(const BnMsg& a, const BnMsg& b);

private:
   int GetPriority() const
   { // check if Text is a certain command, simple chat, or what... };
   
};


class ChatBot : public kBinaryBot
{
public:
   std::priority_queue<BnMsg> queue;

// etc;

};


Edit: I did this from memory -- I may have implemented the comparison wrong.
Edit2: I did.  I fixed it.

Kp

Quote from: K on February 02, 2004, 01:02 PM
For C++ I use a std::priority_queue<BnMsg> with a comparison object that gives certain commands (/ban, /kick) higher priority so that they are executed first.

From the look of your code, you reanalyze the priority of the messages every time you compare them.  While this works, it seems a bit wasteful when you could just keep an unsigned or enum member in the class that specifies the priority.  Assign the enum a value from the GetPriority function when you create the class, then just query the enum's value when you need to sequence the messages.  It'll save you from recomparing against all the other text strings at run time.  Not a big optimization since it's not likely you'll often have lots of messages, but it's a fairly simple change. :)
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

K

Quote from: Kp on February 02, 2004, 07:29 PM
Quote from: K on February 02, 2004, 01:02 PM
For C++ I use a std::priority_queue<BnMsg> with a comparison object that gives certain commands (/ban, /kick) higher priority so that they are executed first.

From the look of your code, you reanalyze the priority of the messages every time you compare them.  While this works, it seems a bit wasteful when you could just keep an unsigned or enum member in the class that specifies the priority.  Assign the enum a value from the GetPriority function when you create the class, then just query the enum's value when you need to sequence the messages.  It'll save you from recomparing against all the other text strings at run time.  Not a big optimization since it's not likely you'll often have lots of messages, but it's a fairly simple change. :)

I realized that after I posted.  Since this is just an example, the under-the-hood implementation is up to whoever wants to use it.   Your point is a valid one, though -- If I were actually using this code I would make the std:::string Text variable a private member, and only recalculate priority when the assignment operator is used to assign a different string to it.