void mud_update(void) { static long creature_time = 0; static long object_time = 0; static long reset_time = 0; static long mysql_time = 0; static long mudtime_time = 0; static long backup_time = 0; static long heal_time = 0; static long socket_time = 0; if( ++socket_time % (LOOPS_PER_SECOND) == 0 ) socket_update(); if( ++heal_time % (LOOPS_PER_SECOND*5) == 0 ) heal_update(); if( ++creature_time % (LOOPS_PER_SECOND*60) == 0 ) creature_update(); if( ++object_time % (LOOPS_PER_SECOND) == 0 ) object_update(); if( ++reset_time % (LOOPS_PER_SECOND) == 0 ) reset_update(); if( ++mysql_time % (LOOPS_PER_SECOND*60*60) == 0 ) mudlog("Pinging mysql server == %s", mysql_ping(mysql) == 0 ? "Connected" : "Disconnected"); if( ++mudtime_time % (LOOPS_PER_SECOND) == 0 ) { if(mudtime_time % (LOOPS_PER_SECOND*60*15) == 0) // 15 minutes fwrite_time(); mudtime.age++; } if( ++backup_time % (LOOPS_PER_SECOND*60*60) == 0) // check at startup and every hour backup_update(); }
// game boot up stuff void init_mud(long hotreboot) { char buf[MAX_BUFFER]; MSOCKET *socket; extern CREATURE *hash_creature[HASH_KEY]; extern OBJECT *hash_object[HASH_KEY]; extern ROOM *hash_room[HASH_KEY]; extern char **connect_attempts; extern void backup_update(); #ifndef WIN32 struct itimerval itime; struct timeval time; #endif extern char * const months[]; extern char * const days[]; long x; for(x = 0; months[x]; x++) ; mudtime.months = x; for(x = 0; days[x]; x++) ; mudtime.days = x; // init hash tables memset(hash_creature, 0,sizeof(hash_creature)); memset(hash_object, 0,sizeof(hash_object)); memset(hash_room, 0,sizeof(hash_room)); memset(hash_reset, 0,sizeof(hash_reset)); // set up process signals to catch and deal with signal(SIGINT, signal_handler); signal(SIGILL, signal_handler); signal(SIGFPE, signal_handler); signal(SIGSEGV, signal_handler); signal(SIGPIPE, SIG_IGN); // ignore pipes! tx to unix socket faq for help // with this. http://http://www.developerweb.net/forum/ #ifndef WIN32 signal(SIGTRAP, signal_handler); signal(SIGVTALRM, signal_handler); // this code is to check for infinite looping time.tv_sec = 5; time.tv_usec = 0; itime.it_interval = time; itime.it_value = time; if( setitimer(ITIMER_VIRTUAL, &itime, 0) < 0 ) mudlog("Error starting setitimer."); #endif // initialize the random number generator init_rand(); // connect_attempts array, to keep people from spam connecting to slow down the mud connect_attempts = malloc(sizeof(char*)); connect_attempts[0] = 0; mysql = mysql_init(NULL); if (!mysql) { mudlog("Error initializing MySQL: %s", mysql_error(mysql)); abort(); } // connect to MySQL database if (!mysql_real_connect(mysql, DB_HOST, DB_LOGIN, DB_PASSWORD, DB_NAME, 0, NULL, 0)) { mudlog("Error opening mysql database: %s", mysql_error(mysql)); abort(); } if (mysql_select_db(mysql, DB_NAME)) { mudlog("Error opening main database: %s",mysql_error(mysql)); if (mysql) mysql_close(mysql); abort(); } // time of da mud ! fread_time(); // load areas/rooms/objects/creatures load_db(); // check if hotreboot and reconnect everything if(hotreboot) { MSOCKET *socket=0; char buf[MAX_BUFFER]; MYSQL_RES *result; MYSQL_ROW row; mysql_query(mysql, "SELECT * FROM player WHERE online='1'"); result = mysql_store_result(mysql); if (mysql_num_rows(result) < 0) { mudlog("hotreboot: mysql error is: %s",mysql_error(mysql)); exit(1); } while((row = mysql_fetch_row(result))) { strcpy(buf, row[P_NAME]); // check for "host" player.. holds control descriptor if(!strcasecmp("host", row[C_NAME])) { host = atoi(row[P_DESCRIPTOR]); } else { socket = init_socket(); fread_player(socket, buf); str_dup(&socket->host, row[P_HOST]); str_dup(&socket->ip, row[P_IP]); socket->desc = atoi(row[P_DESCRIPTOR]); socket->connected = CON_PLAYING; trans(socket->pc, socket->pc->in_room); sendcrit(socket->pc, "Hotreboot completed."); } } mysql_free_result(result); } // make sure nobody has non-existant connections, in the event of a crash + hotreboot // then go through and update people who are online // this is also a good time to get host address, as players will be expecting lag mysql_query(mysql, "UPDATE player SET online='0', descriptor='0'"); for(socket = socket_list; socket; socket = socket->next) { get_hostname(socket); sprintf(buf,"UPDATE player SET online='1', host=\"%s\" WHERE id='%li'", smash_quotes(socket->host), socket->id); mysql_query(mysql, buf); } // see how many players are in each area, to see if area should be 'alive' for(socket = socket_list; socket; socket = socket->next) { if(IsPlaying(socket->pc)) socket->pc->in_room->area->players++; } // check if we need to backup backup_update(); }