/*

 BabbleMud - A MUD server

 Author:  Nick Gammon 
          http://www.gammon.com.au/ 

 Date:    22nd July 2004

(C) Copyright Nick Gammon 2004. Permission to copy, use, modify, sell and
distribute this software is granted provided this copyright notice appears
in all copies. This software is provided "as is" without express or implied
warranty, and with no claim as to its suitability for any purpose.
 
*/

// standard library includes ...

#include <iostream>

using namespace std; 

#include "constants.h"
#include "globals.h"
#include "gameutils.h"

#ifdef WIN32
#include <time.h>
#endif

void LoadThings ();
int InitComms ();
void MainLoop ();
void CloseComms ();
   
// main program
int main ()
{

  cout << MUDNAME << " version " << VERSION << endl;
  time_t t = time (NULL);
  cout << "Started up at: " << stringDate (t) << endl;
  serverInfo ().sets ("started_at", stringDate (t));
  serverInfo ().seti ("started_time", t);

  try   // text exceptions
    {
    try
      { // msg (coded) exceptions)
      LoadThings ();    // load stuff
     
      if (InitComms ()) // listen for new connections
        return 1;
          
      Event e ("EventSendToAll", 120000, -1);
      e.sets ("msg", "Something goes 'bing'! (softly)");
      
      eventQueue ().push (e);
          
      gamelog ("Game startup");
      
      MainLoop ();    // handle player input/output
        
      // game over - tell them all
      SendToAll ("game_shut_down", messagemap ());
      
      CloseComms ();  // stop listening
  
      gamelog ("Game shutdown");
      } // end of try
    catch (msg & e)
      {
      // re-throw with code substitution
      throw runtime_error (convertErrorCode (e.what ()));
      }
   }  // end of try
  catch (exception & e)
    {
    cerr << "Exception raised: " << e.what () << endl;
    return 1;
    }
  
  cout << "Game shut down." << endl;  
  cout << "Shut down at: " << stringDate (time (NULL)) << endl;
  return 0;
}   // end of main
