]> jfr.im git - irc/gameservirc.git/blobdiff - gameserv/gameserv.cpp
Implemented a player timeout scheme
[irc/gameservirc.git] / gameserv / gameserv.cpp
index c64b01a10c038768bcc0aaa17bc0d173f1b0b9e3..0937cd705e5234bd3097fb750465d32567707a8c 100644 (file)
@@ -10,6 +10,7 @@
 
 using std::ifstream;
 using std::ofstream;
+using std::ios;
 
 #if defined(HAVE_CRYPT_H)
 
@@ -21,7 +22,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 +47,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 +58,11 @@ bool check_password(char *name, char *plaintext); // Finds a password for the gi
 
 /********** GameServ Booleans **********/
 
+bool shuttingdown;
+bool timedOut(Player *p);
+void  updateTS(Player *p);
+void timeOutEvent(Player *p);
+
 bool is_playing(char *u); // True if the given nickname in the clients list is playing.
 bool is_playing(aClient *user);
 
@@ -79,6 +89,7 @@ long int stringtoint(char *number);
 char *spaces(int len, char *seperator);
 void refresh(Player *p);
 void refreshall();
+void updateTS(Player *p);
 void reset(Player *p);
 void init_masters();
 void init_monsters();
@@ -171,7 +182,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 +221,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 +249,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 +541,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 +564,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 +603,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 "\
@@ -606,14 +623,14 @@ void logout(aClient *user)
        temp = new aClient;
        temp->stats = new Player;
        temp->stats->setData(user->stats);
-       user->stats->user = NULL;
+       user->stats->client = NULL;
 
        if (player_fight(user))
            user->stats->battle->stats->battle = NULL;
 
        delete user->stats;
        user->stats = NULL;
-       temp->stats->user = NULL;
+       temp->stats->client = NULL;
        #ifdef P10
            temp->setRealNick("!NULL!");
        #endif
@@ -634,7 +651,7 @@ void logout(aClient *user)
 void do_register(char *u)
 {
     char *password, *name;
-    aClient *user, *p;
+    aClient *user;
     name = strtok(NULL, " ");
     password = strtok(NULL, " ");
 
@@ -660,14 +677,15 @@ void do_register(char *u)
     }
     else if ((user = find(u)))
     {
-       p = findplayer(u);
-        if (!user->stats && !p)
+        if (!user->stats)
         {
            user->stats = new Player(user);
-           user->stats->user = user; // Set the backwards pointer
+           user->stats->client = 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);
+           updateTS(user->stats);
+           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);
@@ -701,7 +719,7 @@ void do_identify(char *u)
     {
        notice(s_GameServ, u, "You are already playing!");
     }
-    else if (p->stats->user != NULL && !isAdmin(user))
+    else if (p->stats->client != NULL && !isAdmin(user))
     {
        notice(s_GameServ, u, "That player has already identified.");
     }
@@ -711,7 +729,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", 
@@ -722,6 +742,8 @@ void do_identify(char *u)
        #ifdef DEBUGMODE
            log("Setting data for identified");
        #endif
+       updateTS(user->stats);
+
        user->stats->setData(p->stats);
 
        #ifdef DEBUGMODE
@@ -754,7 +776,10 @@ void do_stats(char *u)
            return;
        }
        else
+       {
+           updateTS(user->stats);
            showstats(u, user->stats->name);
+       }
     }
     else
        showstats(u, nick);
@@ -923,21 +948,15 @@ void display_monster(char *u)
 
 void display_players(char *u)
 {
-    if (is_playing(u))
+    aClient *user;
+    if (!(user = find(u)))
     {
-       aClient *ni = find(u);
-
-       aClient *battle = ni->stats->battle;
-
-       notice(s_GameServ, u, "Your Hitpoints: \ 2%d\ 2", ni->stats->hp);
-       notice(s_GameServ, u, "%s's Hitpoints: \ 2%d\ 2", battle->stats->name, 
-                                                       battle->stats->hp);
-       notice(s_GameServ, u, "Here are your commands:");
-       notice(s_GameServ, u, "/msg %S attack");
-       notice(s_GameServ, u, "/msg %S run");
-       notice(s_GameServ, u, "What will you do?");
+       log("Fatal error in display_players(): Couldn't find %s", u);
     }
+    else
+       display_players(user);
 }
+
 void display_players(aClient *user)
 {
     char *u = user->getNick();
@@ -958,18 +977,14 @@ bool is_playing(char *u)
 {
     aClient *user;
     if (!(user = find(u)))
-    {
        return false;
-    }
     else
-    {
-       return user->stats != NULL;
-    }
+       return is_playing(user);
 }
 
 bool is_playing(aClient *user)
 {
-    return user->stats != NULL && (stricmp(user->getNick(), "!NULL!") != 0);
+    return user->stats != NULL;
 }
 
 bool is_fighting(char *u)
@@ -977,17 +992,11 @@ bool is_fighting(char *u)
     aClient *user;
 
     if (!(user = find(u)))
-    {
        return false;
-    }
-    else if (user->stats)
-    {
-       return user->stats->fight != NULL || user->stats->battle != NULL 
-               || user->stats->master != NULL;
-    }
     else
-       return false;
+       return is_fighting(user);
 }
+
 bool is_fighting(aClient *user)
 {
     if (!is_playing(user))
@@ -1002,10 +1011,8 @@ bool player_fight(char *u)
 
     if (!(user = find(u)))
        return false;
-    else if (user->stats)
-       return user->stats->battle != NULL;
-    else
-       return false;
+    else 
+       return player_fight(user);
 }
 bool player_fight(aClient *user)
 {
@@ -1021,11 +1028,10 @@ bool master_fight(char *u)
 
     if (!(user = find(u)))
        return false;
-    else if (user->stats)
-       return user->stats->master != NULL;
     else
-       return false;
+       return master_fight(user);
 }
+
 bool master_fight(aClient *user)
 {
     if (!is_playing(user))
@@ -1043,16 +1049,22 @@ void do_fight(char *u)
     if (!nick)
     {
        notice(s_GameServ, u, "SYNTAX: /msg %S FIGHT PLAYER");
+       return;
     }
     else if (!(ni = find(u)))
     {
        notice(s_GameServ, u, "Fatal error. Contact a(n) %S admin. buf: %s", strtok(NULL, ""));
+       return;
     }
     else if (!is_playing(ni))
     {
        notice(s_GameServ, u, "You are not playing!");
+       return;
     }
-    else if (ni->stats->player_fights <= 0)
+
+    updateTS(ni->stats);
+
+    if (ni->stats->player_fights <= 0)
     {
        ni->stats->player_fights = 0; // just to be safe
        notice(s_GameServ, u, "You are out of player fights for the "\
@@ -1133,9 +1145,10 @@ void do_fight(char *u)
         notice(s_GameServ, battle->getNick(), "%s gets to go first "\
                        "because they initiated!", ni->stats->name);
         notice(s_GameServ, battle->getNick(), "Please wait while %s decides what to do.", ni->stats->name);
-        display_players(u);
+        display_players(ni);
     }
 }
+
 void do_use(char *u)
 {
     aClient *user;
@@ -1160,6 +1173,8 @@ void do_use(char *u)
        return;
     }
 
+    updateTS(user->stats);
+
     p = &user->stats->inventory;
 
     if (stricmp(item, "HEALTH") == 0)
@@ -1242,6 +1257,7 @@ void do_run(char *u)
         return;
     }
 
+    updateTS(user->stats);
     p = user->stats;
 
     if (p->battle)
@@ -1388,6 +1404,7 @@ void do_attack(char *u)
        // One has to be !NULL based on the previous else if
        // We wouldn't be here if they were all NULL
     }
+    updateTS(ni->stats);
 
     if (!player_fight(ni))
     {
@@ -1620,7 +1637,7 @@ void do_attack(char *u)
                                              weapons[ni->stats->weapon], hit);
        clearYourTurn(ni->stats);
        setYourTurn(battle->stats);
-        display_players(battle->getNick());
+        display_players(battle);
     }
     else
     {
@@ -1628,7 +1645,7 @@ void do_attack(char *u)
         notice(s_GameServ, battle->getNick(), "%s misses you completely!", ni->stats->name);
        clearYourTurn(ni->stats);
        setYourTurn(battle->stats);
-        display_players(battle->getNick());
+        display_players(battle);
     }
     if (hit >= battle->stats->hp)
     {
@@ -1699,6 +1716,7 @@ void do_heal(char *u)
     if (!amount)
     {
        notice(s_GameServ, u, "SYNTAX: /msg %S HEAL {ALL | #}");
+       return;
     }
     else if (!(ni = find(u)))
     {
@@ -1718,12 +1736,16 @@ void do_heal(char *u)
     else if (is_fighting(ni))
     {
        notice(s_GameServ, u, "You can't heal in battle!");
+       return;
     }
     else if (ni->stats->hp >= ni->stats->maxhp)
     {
         notice(s_GameServ, u, "You don't need healing!");
+       return;
     }
-    else if (stricmp(amount, "ALL") == 0)
+
+    updateTS(ni->stats);
+    if (stricmp(amount, "ALL") == 0)
     {
         price = ni->stats->level * 3;
         if (ni->stats->gold < (ni->stats->maxhp - ni->stats->hp) * price)
@@ -1824,7 +1846,7 @@ long int chartoint(char ch)
 
 int save_gs_dbase()
 {
-    ListNode<aClient> *ptr = players.First();
+    ListNode<aClient> *ptr;
     Player *it;
     ofstream outfile;
 
@@ -1836,6 +1858,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 +1872,7 @@ int save_gs_dbase()
                << ' ' << it->inventory.Strength() << ' ' << it->inventory.Defense() << ' ' << it->inventory.HP() << endl;
        ptr = ptr->Next();
     }
+   }
 outfile.close();
 return 1;
 }
@@ -1914,8 +1940,10 @@ int load_gs_dbase()
        tempname = strtok(NULL, " ");
        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);
+
+       temp->stats->client = NULL;
+       players[hv].insertAtBack(temp);
        delete temp;
     }
 delete [] buf;
@@ -1972,15 +2000,21 @@ void do_store(char *u)
        notice(s_GameServ, u, "SYNTAX: STORE LIST {ARMOR | WEAPONS}");
        notice(s_GameServ, u, "        \ 2STORE SELL {ARMOR | WEAPON}\ 2");
        notice(s_GameServ, u, "        \ 2STORE BUY {ARMOR | WEAPON} \1fNUMBER\1f\ 2");
+       return;
     }
     else if (!(user = find(u)) || !is_playing(user))
+    {
        notice(s_GameServ, u, "You must be playing to use the store!");
+       return;
+    }
     else if (!isAlive(user->stats))
     {
        notice(s_GameServ, u, "You are dead. Wait until tomorrow to purchase weapons and armor!");
        return;
     }
-    else if (stricmp(cmd, "LIST") == 0)
+    updateTS(user->stats);
+
+    if (stricmp(cmd, "LIST") == 0)
     {
        if (stricmp(item, "WEAPONS") == 0)
        {
@@ -2149,6 +2183,7 @@ void do_inventory(char *u)
        notice(s_GameServ, u, "You must be playing to check your inventory!");
        return;
     }
+    updateTS(user->stats);
     showinventory(user, user);
 }
 void showinventory(aClient *from, aClient *to)
@@ -2189,7 +2224,10 @@ void do_tavern(char *u)
        notice(s_GameServ, u, "You cannot go to the Tavern during a fight!");
        return;
     }
+
+    updateTS(user->stats);
     p = user->stats;
+
     if (!cmd)
     {
        notice(s_GameServ, u, "Welcome to Boot Liquors Mystic Apothecary");
@@ -2304,14 +2342,21 @@ void do_bank(char *u)
        notice (s_GameServ, u, "BANK BALANCE");
        return;
     }
-
-    user = find(u);
-    if (!is_playing(user))
+    else if (!(user = find(u)))
+    {
+       notice(s_GameServ, u, "Fatal Error. Couldn't find your aClient. Contact a(n) %S "\
+                               " admin for help");
+       log("Fatal Error. Couldn't find %s while executing do_bank()", u);
+       return; 
+    }
+    else if (!is_playing(user))
     {
        notice(s_GameServ, u, "You must be playing to use the bank!");
        return;
     }
-    else if (stricmp(cmd, "BALANCE") == 0)
+
+    updateTS(user->stats);
+    if (stricmp(cmd, "BALANCE") == 0)
     {
         showBankBalance(u);
         return;
@@ -2459,7 +2504,8 @@ void do_master(char *u)
        notice(s_GameServ, u, "You must be playing to see your master!");
        return;
     }
-    
+
+    updateTS(user->stats);
     char *cmd = strtok(NULL, " ");
     Player *p = user->stats;
     long int need = 0;
@@ -2588,8 +2634,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 +2644,7 @@ void refreshall()
        refresh(p);
        it = it->Next();
     }
+   }
 }
 
 void refresh(Player *p)
@@ -2671,7 +2719,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 +2729,7 @@ void resetall()
        reset(p);
        it = it->Next();
     }
+   }
 }
 
 void reset(Player *p)
@@ -2689,6 +2740,34 @@ void reset(Player *p)
     p->reset();
 }
 
+void updateTS(Player *p)
+{
+    if (!p)
+       return;
+    p->lastcommand = time(NULL);
+}
+
+bool timedOut(Player *p)
+{
+    if (!p)
+       return false;
+    else
+       return ((time(NULL) - p->lastcommand) >= maxidletime);
+}
+
+void timeOutEvent(Player *p)
+{
+    aClient *user = p->client;
+
+    if (!user) // then they're not playing
+       return;
+
+    char *nick = user->getNick();
+
+    notice(s_GameServ, nick, "You just idle timed out");
+    logout(user);
+}
+
 void do_reset(char *u)
 {
     char *nick = strtok(NULL, " ");
@@ -2887,3 +2966,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