]> jfr.im git - irc/gameservirc.git/blobdiff - gameserv/tcpclient.cpp
implemented thelizno's custom time idea
[irc/gameservirc.git] / gameserv / tcpclient.cpp
index 92f0814fb0783c0b3f4f1c0f8fb8b51f71c4a474..86a0b8446d05bdd8223acbafbfb1bab958556547 100644 (file)
 #include <stdlib.h>
 #include <fcntl.h>
 #include <signal.h>
+
 //#include <sys/types.h>
 //#include <sys/wait.h>
 //#include <errno.h>
 
-using std::ofstream;
-using std::ifstream;
-using std::cerr;
-using std::endl;
+using namespace std;
 
 char *PACKAGE = "GameServ";
-char *VERSION = "1.2.1 +devel";
+char *VERSION = "1.3.0 +devel";
 
 int sock;
-int day;
+long lastrefresh;
+long lastrollover;
 
 List<aClient> clients[U_TABLE_SIZE];
 
-void save_day();
-void load_day();
+void save_lastrefresh();
+void load_lastrefresh();
+void load_lastrollover();
+void save_lastrollover();
 void prettyIntro();
 void check_idles();
 
@@ -102,11 +103,50 @@ int main(int argc, char *argv[])
         perror("Could not turn into a daemon");
         exit(3);
     }
-  init_masters();
+  if (load_items() == 0)
+    {
+      log("Error loading items");
+      goto end;
+    }
+
+  if (load_store() == 0)
+    {
+      log("Error loading store");
+      goto end;
+    }
+  if (load_tavern() == 0)
+    {
+      log("Error loading tavern");
+      goto end;
+    }
+
   load_gs_dbase();
+  loadNews(newsdata, todaysnews);
 
+
+  if (load_masters() == false)
+    {
+      log("Error loading masters");
+      goto end;
+    }
+  
   if (load_monsters() == false)
-       goto end;
+    {
+      log("Error loading monsters");
+      goto end;
+    }
+  
+  if (!load_dragon())
+    {
+      log("Error loading dragon");
+      goto end;
+    }
+  
+  if (load_levels() == false)
+    {
+      log("Error loading levels");
+      goto end;
+    }
 
     shuttingdown = false;
 
@@ -120,24 +160,12 @@ int main(int argc, char *argv[])
        currentserver = strtok(NULL, " ");
     }
 
-  strcpy(boss.name, "Red Dragon");
-  strcpy(boss.weapon, "Breath of Unholy Fire");
-  boss.strength = 1000;
-  boss.gold = 2000000000;
-  boss.exp = 2000000000;
-  boss.maxhp = 6667;
-  boss.hp = 6667;
-  strcpy(boss.death, "You finally snuff out the deadly murderous "\
-    "dragon's dark flames. You have freed the land of its terror "\
-    "filled reign from above!");
-
-
  // This loop will retry the connection 3 times
  for (int retry = 0; retry < 3 && !shuttingdown; retry++)
  {
     connected = 1;
-    load_day();
-
+    load_lastrefresh();
+    load_lastrollover();
 
     long int loadtime = time(NULL);
     long int currentTime;
@@ -151,7 +179,8 @@ int main(int argc, char *argv[])
     bool loaded = false;
 
     ignore_pipe();
-    sock = make_connection(remoteport, SOCK_STREAM, remoteserver);
+    sock = conn(remoteserver, remoteport, localhost, 0);
+    //    sock = make_connection(remoteport, SOCK_STREAM, remoteserver);
     if (sock == -1) {
        fprintf(stderr,"make_connection failed.\n");
     unload_config_file();
@@ -160,11 +189,10 @@ int main(int argc, char *argv[])
     log("%S socket connected.");
 
 #ifdef UNREAL
-       raw("PROTOCTL NICKv2 VHP");
        raw("PASS :%s", remotepass);
        raw("SERVER %s 1 :%s", servername, servername);
-       raw("NICK %S 1 %d %S %s %s %d +w%s %s :%s v%s", time(NULL), gshost, 
-               servername, time(NULL), (isBOper() ? "o" : ""), gshost, PACKAGE, VERSION);
+       raw("NICK %S 1 %d %S %s %s %d :%s v%s", time(NULL), gshost, 
+               servername, time(NULL), PACKAGE, VERSION);
        raw(":%S JOIN %s", c_Forest);
        raw(":%S MODE %s +tn", c_Forest);
 #elif defined(BAHAMUT)
@@ -179,6 +207,14 @@ int main(int argc, char *argv[])
        raw("NICK %S 1 %d +w%s %s %s %s :GameServ", time(NULL), (isBOper() ? "o" : ""), 
                gsident, gshost, servername);
        raw(":%s SJOIN %ld %s +nt :@%S", servername, time(NULL), c_Forest);
+#elif defined(ULTIMATE2)
+       raw("PASS %s :TS", remotepass);
+       raw("SERVER %s 1 :%s", servername, servername);
+       raw("NICK %S 1 %d %s %s %s 0 :GameServ", 
+               time(NULL), gsident, gshost, servername);
+       if (isBOper())
+           raw(":%S mode %S +o");
+       raw(":%S JOIN %s", c_Forest);
 #elif defined(P10)
        // Server numeric is: []  <-- must be unique
        raw("PASS :%s", remotepass);
@@ -192,12 +228,23 @@ int main(int argc, char *argv[])
        raw("%s T %s :%s", gsnum, c_Forest, c_ForestTopic);
        raw("[] EB");  // End burst
 #else 
-#ifndef HYBRID
-       raw(":%S MODE %s +o %S", c_Forest);
-#endif
+    #ifndef HYBRID
+       #if defined(ULTIMATE2)
+           raw(":%s MODE %s +o %S %ld", servername, c_Forest, 
+               time(NULL));
+       #else
+           raw(":%S MODE %s +o %S", c_Forest);
+       #endif
+    #endif
        raw(":%S TOPIC %s :%s", c_Forest, c_ForestTopic);
 #endif
 
+    #ifndef P10
+    if (isUseNickServ())
+    {
+       raw(":%S PRIVMSG %s :IDENTIFY %s", nsname, nspass);
+    }
+    #endif
   sock_gets(sock,buffer,sizeof(buffer)-1); /* -1 added thanks to
     David Duchene <dave@ltd.com> for pointing out the possible
     buffer overflow resulting from the linefeed added below. */
@@ -249,13 +296,44 @@ int main(int argc, char *argv[])
            }
        }
 
-       // Save the player data every updateperiod seconds
+       // Refresh players and clear news if the time is up
        currentTime = time(NULL);
+
+       if (isRolloverForestFights())
+         {
+           if (currentTime - lastrollover >= rolloverperiod)
+             {
+               rolloverall();
+               lastrollover = currentTime;
+               save_lastrollover();
+               notice(s_GameServ, c_Forest, "Adding %d forest fights to all players!", forestfights);
+             }
+         }
+
+       if (currentTime - lastrefresh >= refreshperiod)
+       {
+           refreshall();
+           clearNews(todaysnews);
+           saveNews(newsdata, todaysnews);
+           lastrefresh = currentTime;
+           save_lastrefresh();
+           notice(s_GameServ, c_Forest, "Refreshing all players "\
+                       "and resetting news!");
+       }
+
+       // Save the player data every updateperiod seconds
        if (currentTime - oldTime >= updateperiod)
        {
            oldTime = currentTime;
            log("Saving to %s", playerdata);
+
            save_gs_dbase();
+           saveNews(newsdata, todaysnews);
+           if (isSavedNotice())
+           {
+               // Send notice to the channel of the update
+               notice(s_GameServ, c_Forest, "%S player data saved");
+           }
        }
 
 
@@ -355,7 +433,7 @@ int main(int argc, char *argv[])
 
                temp = clients[hv].insertAtBack(newuser);
 
-               #if defined(HYBRID) || defined(BAHAMUT)
+               #if defined(HYBRID) || defined(BAHAMUT) || defined(ULTIMATE2)
                    char *nickserver;
                    strtok(NULL, " ");
                    strtok(NULL, " ");
@@ -363,7 +441,6 @@ int main(int argc, char *argv[])
                    if (nickserver[0] == '+')
                        strtok(NULL, " ");
                    strtok(NULL, " ");
-
                    nickserver = strtok(NULL, " ");
                    for (int x = 0; x < 32; x++)
                    {
@@ -469,7 +546,7 @@ int main(int argc, char *argv[])
            rest = strtok(NULL, "");
            if (strnicmp(dest, s_GameServ, strlen(s_GameServ)) == 0)
                gameserv(source, rest);
-           else if (stricmp(dest, c_Forest) == 0)
+           else if (stricmp(dest, c_Forest) == 0 && isListenOnCF())
                forest(source, rest);
       #endif
       #if defined(P10)
@@ -492,7 +569,7 @@ int main(int argc, char *argv[])
            {
                #ifdef DEBUGMODE
                    log("Player %s (IRC: %s) joined %s", 
-                       joiner->stats->name, 
+                       joiner->stats->name.c_str()
                        #ifdef P10
                            joiner->getRealNick(),
                        #else
@@ -543,10 +620,10 @@ int main(int argc, char *argv[])
   end:
 
   save_gs_dbase();
-  save_day();
+  save_dragon();
+  saveNews(newsdata, todaysnews);
 
   delete_monsters();
-  delete_masters();
 
   #ifdef DEBUGMODE
       log("<CLOSED>");
@@ -635,7 +712,7 @@ aClient *findIRCplayer(const char *nick)
     if (!is_playing(p))
        return NULL;
 
-    unsigned long hv = iHASH((unsigned char *) p->stats->name);
+    unsigned long hv = iHASH((unsigned char *) p->stats->name.c_str());
 
     for (newPtr = players[hv].First(); newPtr; newPtr = newPtr->Next())
     {
@@ -659,7 +736,7 @@ aClient *findplayer(const char *name)
     for (newPtr = players[hv].First(); newPtr; newPtr = newPtr->Next())
     {
        p = newPtr->getData()->stats;
-       if (stricmp(p->name, name) == 0)
+       if (stricmp(p->name.c_str(), name) == 0)
            return newPtr->getData();
        p = NULL;
     }
@@ -676,6 +753,26 @@ void check_idles()
        for (newPtr = players[x].First(); newPtr; newPtr = newPtr->Next())
        {
            p = newPtr->getData()->stats;
+           switch(p->level)
+           {
+               case 1:
+               if ((time(NULL) - p->lastlogin) / 86400 >= level1expire)
+               {
+                   logout(newPtr->getData());
+                   players[x].remove(newPtr->getData());
+                   return;
+               }
+                   break;
+
+               default:
+               if ((time(NULL) - p->lastlogin) / 86400 >= defaultexpire)
+               {
+                   logout(newPtr->getData());
+                   players[x].remove(newPtr->getData());
+                   return;
+               }
+                   break;
+           }
            if (timedOut(p))
            {
                timeOutEvent(p);
@@ -712,56 +809,6 @@ aClient *findbynick(const char *nick)
     return client;    
 }
 
-void load_day()
-{
-    ifstream infile;
-
-    infile.open(".gsday");
-
-    if (infile.fail())
-    {
-       #ifdef DEBUGMODE
-           log("Error opening .gsday");
-       #endif
-
-       generate:
-        #ifdef DEBUGMODE
-           log("Generating new day");
-       #endif
-       struct tm *tm;
-       time_t ti;
-       time(&ti);
-       tm = localtime(&ti);
-
-       day = tm->tm_mday;
-
-        save_day();
-       return;
-    }
-
-    infile >> day;
-    infile.close();
-    if (day < 1 || day > 31)
-       goto generate;
-}
-
-void save_day()
-{
-    ofstream outfile;
-
-    outfile.open(".gsday");
-
-    if (outfile.fail())
-    {
-       log("Error creating new file .gsday");
-       return;
-    }
-
-    outfile << day << endl;
-
-    outfile.close();
-}
-
 /* daemon() - detach process from user and disappear into the background
  * returns -1 on failure, but you can't do much except exit in that case
  * since we may already have forked. This is based on the BSD version,
@@ -827,12 +874,106 @@ void closeall(int fd)
 
 void prettyIntro()
 {
-cout << endl;
-cout << "  GGGG     AAA   MM    MM EEEEEEE  SSSSS  EEEEEEE RRRRRR  VV     VV " << endl;
-cout << " GG  GG   AAAAA  MMM  MMM EE      SS      EE      RR   RR VV     VV " << endl;
-cout << "GG       AA   AA MM MM MM EEEEE    SSSSS  EEEEE   RRRRRR   VV   VV  " << endl;
-cout << "GG   GGG AAAAAAA MM    MM EE           SS EE      RR  RR    VV VV   " << endl;
-cout << "G     G  AA   AA MM    MM EEEEEEE  SSSSS  EEEEEEE RR   RR    VVV" << endl;
-cout << " GGGGG                                                        V\n\n" << endl;
-cout << "Version: " << VERSION << endl;
+  cout << endl;
+  cout << "  GGGG     AAA   MM    MM EEEEEEE  SSSSS  EEEEEEE RRRRRR  VV     VV " << endl;
+  cout << " GG  GG   AAAAA  MMM  MMM EE      SS      EE      RR   RR VV     VV " << endl;
+  cout << "GG       AA   AA MM MM MM EEEEE    SSSSS  EEEEE   RRRRRR   VV   VV  " << endl;
+  cout << "GG   GGG AAAAAAA MM    MM EE           SS EE      RR  RR    VV VV   " << endl;
+  cout << "G     G  AA   AA MM    MM EEEEEEE  SSSSS  EEEEEEE RR   RR    VVV" << endl;
+  cout << " GGGGG                                                        V\n\n" << endl;
+  cout << "Version: " << VERSION << endl;
+  cout << "http://www.gameserv.us - http://www.sourceforge.net/projects/gameservirc" << endl;
+}
+
+void load_lastrefresh()
+{
+    ifstream infile;
+    infile.open(".gsrefresh");
+    if (infile.fail())
+    {
+       #ifdef DEBUGMODE
+           log("Error opening .gsrefresh");
+       #endif
+
+       generate:
+       long mytime = time(NULL);
+       #ifdef DEBUGMODE
+           log("Generating new refresh time");
+       #endif
+
+       // Just a safety measure... tho no one should
+       // get anywhere near the actual time as their refreshperiod
+       if (refreshperiod >= mytime)
+         {
+           log("Refresh period is greater than or equal to the actual time... setting it to 86400");
+           refreshperiod = 86400;
+         }
+       
+       lastrefresh = mytime - (mytime % refreshperiod);
+
+       refreshall();
+       save_lastrefresh();
+       return;
+    }
+    infile >> lastrefresh;
+
+    infile.close();
+    if (lastrefresh < 0)
+       goto generate;
+}
+
+void load_lastrollover()
+{
+  ifstream infile;
+  infile.open(".gsrollover");
+  if (infile.fail())
+    {
+#ifdef DEBUGMODE
+      log("Error opening .gsrollover");
+#endif
+      
+    generate:
+      long mytime = time(NULL);
+#ifdef DEBUGMODE
+      log("Generating new rollover time");
+#endif
+      lastrollover = mytime;
+      return;
+    }
+  infile >> lastrollover;
+  
+  infile.close();
+  if (lastrollover < 0)
+    goto generate;
+}
+
+void save_lastrefresh()
+{
+    ofstream outfile;
+
+    outfile.open(".gsrefresh");
+
+    if (outfile.fail())
+    {
+       log("Error creating new file .gsrefresh");
+       return;
+    }
+    outfile << lastrefresh << endl << lastrollover;
+
+    outfile.close();
+}
+
+void save_lastrollover()
+{
+    ofstream outfile;
+
+    outfile.open(".gsrollover");
+
+    if (outfile.fail())
+    {
+       log("Error creating new file .gsrollover");
+       return;
+    }
+    outfile << lastrollover << endl;
+    outfile.close();
 }