]> jfr.im git - irc/gameservirc.git/blobdiff - gameserv/gameserv.cpp
Added a loop that makes gameserv reload if disconnected. It gives 3 attempts until...
[irc/gameservirc.git] / gameserv / gameserv.cpp
index c64b01a10c038768bcc0aaa17bc0d173f1b0b9e3..2239ce7fb41c64e6a2d05f711e49055cc89cc6cc 100644 (file)
@@ -21,7 +21,12 @@ using std::ofstream;
 
 #endif
 
-List<aClient> players;
+// this will be hash.cpp start
+// thank you wcampbel
+unsigned long HASH(const unsigned char *name, int size_of_table);
+List<aClient> players[U_TABLE_SIZE];
+// this will be hash.cpp end
+
 
 Monster *monsters[LEVELS][MONSTERS];   // Monsters per level. Total = MONSTERS * LEVELS
 Monster boss;                          // The boss monster
@@ -41,7 +46,6 @@ int stricmp(const char *s1, const char *s2);
 int strnicmp(const char *s1, const char *s2, size_t len);
 // String Functions
 
-
 /********** Password functions **********/
 
 bool passcmp(char *encrypted, char *plaintext); // Compares an encrypted pass with a plain text one
@@ -53,6 +57,7 @@ bool check_password(char *name, char *plaintext); // Finds a password for the gi
 
 /********** GameServ Booleans **********/
 
+bool shuttingdown;
 bool is_playing(char *u); // True if the given nickname in the clients list is playing.
 bool is_playing(aClient *user);
 
@@ -171,7 +176,7 @@ void gameserv(char *source, char *buf)
        ts = strtok(NULL, "\1");
         notice(s_GameServ, source, "\1PING %s\1", ts);
     } else if (stricmp(cmd, "\1VERSION\1") == 0) {
-       notice(s_GameServ, source, "\1VERSION %s %s +devel\1", PACKAGE, VERSION);
+       notice(s_GameServ, source, "\1VERSION %s %s\1", PACKAGE, VERSION);
     } else if (stricmp(cmd, "SEARCH") == 0) {
        cmd = strtok(NULL, " ");
 
@@ -210,13 +215,6 @@ void gameserv(char *source, char *buf)
        do_list(source);
     } else if (stricmp(cmd, "LOGOUT") == 0) {
        do_logout(source);
-    #ifdef DEBUGMODE
-    } else if (stricmp(cmd, "PRINT") == 0) {
-       cout << "Printing the clients list:" << endl;
-       clients.print();
-       cout << "\nPrinting the players list:" << endl;
-       players.print();
-    #endif
     } else if (stricmp(cmd, "REGISTER") == 0) {
        do_register(source);
     } else if (stricmp(cmd, "IDENTIFY") == 0) {
@@ -245,6 +243,7 @@ void gameserv(char *source, char *buf)
            #else
                raw("SQUIT %s :leaving: %s used the Shutdown command.", servername, source);
            #endif
+           shuttingdown = true;
        }
     } else if (stricmp(cmd, "SAVE") == 0) {
        aClient *user;
@@ -536,10 +535,17 @@ char *strtok(char *str, const char *delim)
 void do_list(char *u)
 {
     ListNode<aClient> *temp;
-    temp = players.First();
-    if (!players.isEmpty())
+    bool header = false;
+  for (unsigned long x = 0; x < U_TABLE_SIZE; x++)
+  {
+    temp = players[x].First();
+    if (!players[x].isEmpty())
     {
-       notice(s_GameServ, u, "People Playing:");
+       if (!header)
+       {
+           notice(s_GameServ, u, "People Playing:");
+           header = true;
+       }
        while(temp)
        {
            #ifdef P10
@@ -552,10 +558,13 @@ void do_list(char *u)
 
            temp = temp->Next();
        }
-       notice(s_GameServ, u, "End of List");
     }
-    else
+   }
+    if (!header)
        notice(s_GameServ, u, "No one is playing");
+    else
+       notice(s_GameServ, u, "End of List");
+
 }
 
 void do_logout(char *u)
@@ -588,7 +597,9 @@ void logout(aClient *user)
     {
        ListNode<aClient> *it;
        aClient *temp;
-       it = players.Find(user);
+       unsigned long hv = HASH((unsigned char *) user->stats->name, 
+                                U_TABLE_SIZE);
+       it = players[hv].Find(user);
         if (!it)
         {
             notice(s_GameServ, user->getNick(), "Fatal error. Contact "\
@@ -667,7 +678,8 @@ void do_register(char *u)
            user->stats->user = user; // Set the backwards pointer
            strcpy(user->stats->password, crypt(password, salt));
            strcpy(user->stats->name, name);
-           players.insertAtBack(user);
+           unsigned long hv = HASH((unsigned char *) name, U_TABLE_SIZE);
+           players[hv].insertAtBack(user);
            notice(s_GameServ, u, "Player %s registered with password %s.", user->stats->name, password);
            notice(s_GameServ, u, "Write this password down. If you lose it, there is no way to retrieve it!");
            log("Nickname %s registered player %s.", u, user->stats->name);
@@ -711,7 +723,9 @@ void do_identify(char *u)
     }
     else {
        ListNode<aClient> *temp;
-       temp = players.Find(p);
+       unsigned long hv = HASH((unsigned char *) p->stats->name, 
+                               U_TABLE_SIZE);
+       temp = players[hv].Find(p);
        if (!temp)
        {
            notice(s_GameServ, u, "Fatal error. Contact %S Admin. Buf: %s", 
@@ -1824,7 +1838,7 @@ long int chartoint(char ch)
 
 int save_gs_dbase()
 {
-    ListNode<aClient> *ptr = players.First();
+    ListNode<aClient> *ptr;
     Player *it;
     ofstream outfile;
 
@@ -1836,6 +1850,9 @@ int save_gs_dbase()
        return 0;
     }
 
+   for (unsigned long x = 0; x < U_TABLE_SIZE; x++)
+   {
+    ptr = players[x].First();
     while(ptr)
     {
        it = ptr->getData()->stats;
@@ -1847,6 +1864,7 @@ int save_gs_dbase()
                << ' ' << it->inventory.Strength() << ' ' << it->inventory.Defense() << ' ' << it->inventory.HP() << endl;
        ptr = ptr->Next();
     }
+   }
 outfile.close();
 return 1;
 }
@@ -1915,7 +1933,8 @@ int load_gs_dbase()
        if (tempname)
            p->inventory.setHP(stringtoint(tempname));
        temp->stats->user = NULL;
-       players.insertAtBack(temp);
+       unsigned long hv = HASH((unsigned char *) temp->stats->name, U_TABLE_SIZE);
+       players[hv].insertAtBack(temp);
        delete temp;
     }
 delete [] buf;
@@ -2588,8 +2607,9 @@ void refreshall()
 {
     ListNode <aClient> *it;
     Player *p;
-
-    it = players.First();
+   for (unsigned long x = 0; x < U_TABLE_SIZE; x++)
+   {
+    it = players[x].First();
 
     while (it)
     {
@@ -2597,6 +2617,7 @@ void refreshall()
        refresh(p);
        it = it->Next();
     }
+   }
 }
 
 void refresh(Player *p)
@@ -2671,7 +2692,9 @@ void resetall()
     ListNode <aClient> *it;
     Player *p;
 
-    it = players.First();
+   for (unsigned long x = 0; x < U_TABLE_SIZE; x++)
+   {
+    it = players[x].First();
 
     while (it)
     {
@@ -2679,6 +2702,7 @@ void resetall()
        reset(p);
        it = it->Next();
     }
+   }
 }
 
 void reset(Player *p)
@@ -2887,3 +2911,25 @@ bool load_monsters()
     delete [] buf;
 return true;
 }
+
+// this will be hash.cpp start
+// thank you wcampbel
+unsigned long HASH(const unsigned char *name, int size_of_table)
+{
+  unsigned long h = 0, g;
+
+  while (*name)
+  {
+    #ifdef P10
+       h = (h << 4) + (*name++); // Case sensitive for numerics
+    #else
+       h = (h << 4) + tolower(*name++);
+    #endif
+    if ((g = (h & 0xF0000000)))
+      h ^= g >> 24;
+    h &= ~g;
+  }
+  return h % size_of_table;
+}
+
+// this will be hash.cpp end