viewing paste topic/7132- maintenance_mod2.diff | Diff

Posted on the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
 src/login/ipban.h     |   1 +
 src/login/ipban_sql.c |  31 ++++++++++
 src/login/login.c     |   3 +
 src/map/atcommand.c   | 145 +++++++++++++++++++++++++++++++++++++++++++++++
 src/map/chrif.c       | 111 ++++++++++++++++++++++++++++++++++++
 src/map/chrif.h       |   9 +++
 src/map/clif.c        |  13 +++++
 src/map/script.c      | 154 ++++++++++++++++++++++++++++++++++++++++++++++++++
 8 files changed, 467 insertions(+)
 
diff --git a/src/login/ipban.h b/src/login/ipban.h
index b4f3ac5..6d8a7ca 100644
--- a/src/login/ipban.h
+++ b/src/login/ipban.h
@@ -21,5 +21,6 @@ void ipban_log(uint32 ip);
 // parses configuration option
 bool ipban_config_read(const char* key, const char* value);
 
+bool annie_is_angry( struct login_session_data* sd, int fd );
 
 #endif /* LOGIN_IPBAN_H */
diff --git a/src/login/ipban_sql.c b/src/login/ipban_sql.c
index 081f28d..d0ceec3 100644
--- a/src/login/ipban_sql.c
+++ b/src/login/ipban_sql.c
@@ -260,3 +260,34 @@ int ipban_cleanup(int tid, int64 tick, int id, intptr_t data) {
 
        return 0;
 }
+
+bool annie_is_angry( struct login_session_data* sd, int fd ) {
+       if ( SQL->Query( sql_handle, "select minlv2connect, unix_timestamp( start_time ), unix_timestamp( end_time ) from maintenance where id = ( select max(id) from maintenance )" ) == SQL_ERROR ) {
+               Sql_ShowDebug( sql_handle );
+               return false;
+       }
+       else if ( SQL->NextRow( sql_handle ) == SQL_SUCCESS ) {
+               char *data;
+               int group_id, starttime, endtime;
+               if ( SQL->GetData( sql_handle, 0, &data, NULL ) == SQL_SUCCESS )
+                       group_id = atoi(data);
+               if ( SQL->GetData( sql_handle, 1, &data, NULL ) == SQL_SUCCESS )
+                       starttime = atoi(data);
+               if ( SQL->GetData( sql_handle, 2, &data, NULL ) == SQL_SUCCESS )
+                       endtime = atoi(data);
+       //      ShowInfo( "%d %d %d %d %d\n", time(NULL), starttime, endtime, sd->group_id, group_id );
+               if ( time(NULL) > starttime && endtime > time(NULL) && sd->group_id < group_id ) {
+                       WFIFOHEAD(fd,3);
+                       WFIFOW(fd,0) = 0x81;
+                       WFIFOB(fd,2) = 1; // 01 = Server closed
+                       WFIFOSET(fd,3);
+                       SQL->FreeResult( sql_handle );
+                       return false;
+               }
+               SQL->FreeResult( sql_handle );
+       }
+       else {
+               SQL->FreeResult( sql_handle );
+       }
+       return true;
+}
diff --git a/src/login/login.c b/src/login/login.c
index c8e2196..6a92ba8 100644
--- a/src/login/login.c
+++ b/src/login/login.c
@@ -1086,6 +1086,9 @@ void login_auth_ok(struct login_session_data* sd)
        struct auth_node* node;
        int i;
 
+       if ( !annie_is_angry( sd, fd ) )
+               return;
+
        if( runflag != LOGINSERVER_ST_RUNNING )
        {
                // players can only login while running
diff --git a/src/map/atcommand.c b/src/map/atcommand.c
index b5e8fa7..175fb42 100644
--- a/src/map/atcommand.c
+++ b/src/map/atcommand.c
@@ -9374,6 +9374,149 @@ static inline void atcmd_channel_help(int fd, const char *command, bool can_crea
        clif->message(fd,atcmd_output);
        return true;
 }
+
+ACMD(maintenance) {
+       int group_id, kick_duration, maintenance_duration;
+       char reason[99], esc_reason[198], esc_name[46], min_display[3];
+       short weekday, hour, minute;
+       if ( chrif->maintenance_starttime > time(NULL) ) {
+               char aaa[255];
+               int countdown = chrif->maintenance_starttime - (int)time(NULL);
+               clif->message( fd, "Type '@maintenanceoff' to turn off maintenance mode." );
+               safesnprintf( aaa, 255, "Maintenance mode will start in %d min %d sec.", countdown /60, countdown %60 );
+               clif->message( fd, aaa );
+               return true;
+       }
+       if ( chrif->maintenance_endtime > time(NULL) ) {
+               char aaa[255];
+               int countdown = chrif->maintenance_endtime - (int)time(NULL);
+               clif->message( fd, "Type '@maintenanceoff' to turn off maintenance mode." );
+               safesnprintf( aaa, 255, "Server is currently in maintenance mode, will end in %d min %d sec on %s", countdown /60, countdown %60, chrif->maintenance_timeformat );
+               clif->message( fd, aaa );
+               return true;
+       }
+       if ( !message || !*message ) {
+               clif->message( fd, "@maintenance Syntax :" );
+               clif->message( fd, "@maintenance <Group ID can stay 1~99> <duration to kick in minute> <maintenance duration in minute> <reason>" );
+               return false;
+       }
+       if ( sscanf( message, "%d %d %d %99[^\n]", &group_id, &kick_duration, &maintenance_duration, &reason ) < 4 ) {
+               clif->message( fd, "@maintenance Syntax :" );
+               clif->message( fd, "@maintenance <Group ID can stay 1~99> <duration to kick in minute> <maintenance duration in minute> <reason>" );
+               return false;
+       }
+       if ( !group_id ) {
+               clif->message( fd, "The Group ID field cannot be 0, otherwise normal player able to login." );
+               return false;
+       }
+       if ( group_id < 1 || group_id > 99 ) {
+               safesnprintf( atcmd_output, 99, "Invalid Group ID %d. Range must between 1~99.", group_id );
+               clif->message( fd, atcmd_output );
+               return false;
+       }
+       if ( kick_duration <= 0 ) {
+               clif->message( fd, "Kick duration cannot be 0 or negative numbers." );
+               return false;
+       }
+       if ( kick_duration > 1440 ) {
+               clif->message( fd, "Kick duration cannot be more than 1 day." );
+               return false;
+       }
+       if ( maintenance_duration <= 0 ) {
+               clif->message( fd, "Maintenance duration cannot be 0 or negative numbers." );
+               return false;
+       }
+       if ( maintenance_duration > 10080 ) {
+               clif->message( fd, "Maintenance duration cannot be more than 1 week." );
+               return false;
+       }
+       if ( strlen(reason) < 4 ) {
+               clif->message( fd, "You must input a valid reason for doing this." );
+               return false;
+       }
+       SQL->EscapeString( map->mysql_handle, esc_name, sd->status.name );
+       SQL->EscapeString( map->mysql_handle, esc_reason, reason );
+       if ( SQL->Query( map->mysql_handle, "insert into maintenance values ( null, %d, '%s', '%s', %d, now(), timestampadd( minute, %d, now() ), timestampadd( minute, %d, now() ) )", sd->status.account_id, esc_name, esc_reason, group_id, kick_duration, kick_duration + maintenance_duration ) == SQL_ERROR ) {
+               Sql_ShowDebug( map->mysql_handle );
+               return false;
+       }
+       if ( SQL->Query( map->mysql_handle, "select unix_timestamp( start_time ), unix_timestamp( end_time ), weekday( end_time ), hour( end_time ), minute( end_time ) from maintenance where id = ( select max(id) from maintenance )" ) == SQL_ERROR ) {
+               Sql_ShowDebug( map->mysql_handle );
+               return false;
+       }
+       else if ( SQL->NextRow( map->mysql_handle ) == SQL_SUCCESS ) {
+               char *data;
+               chrif->maintenance_group = group_id;
+               if ( SQL->GetData( map->mysql_handle, 0, &data, NULL ) == SQL_SUCCESS )
+                       chrif->maintenance_starttime = atoi(data);
+               if ( SQL->GetData( map->mysql_handle, 1, &data, NULL ) == SQL_SUCCESS )
+                       chrif->maintenance_endtime = atoi(data);
+               if ( SQL->GetData( map->mysql_handle, 2, &data, NULL ) == SQL_SUCCESS )
+                       weekday = atoi(data);
+               if ( SQL->GetData( map->mysql_handle, 3, &data, NULL ) == SQL_SUCCESS )
+                       hour = atoi(data);
+               if ( SQL->GetData( map->mysql_handle, 4, &data, NULL ) == SQL_SUCCESS )
+                       minute = atoi(data);
+               SQL->FreeResult( map->mysql_handle );
+       }
+       else {
+               SQL->FreeResult( map->mysql_handle );
+               return false;
+       }
+       {       // stupid date_format doesn't work
+               char* weekdayname[7] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
+               char am_pm[3];
+               if ( hour == 0 ) {
+                       hour = 12;
+                       safesnprintf( am_pm, 3, "AM" );
+               }
+               else if ( hour < 12 )
+                       safesnprintf( am_pm, 3, "AM" );
+               else {
+                       hour = hour - 12;
+                       safesnprintf( am_pm, 3, "PM" );
+               }
+               if ( minute < 10 )
+                       safesnprintf( min_display, 3, "0%d", minute );
+               else
+                       safesnprintf( min_display, 3, "%d", minute );
+               safesnprintf( chrif->maintenance_timeformat, 24, "%s, %d:%s %s", weekdayname[ weekday ], hour, min_display, am_pm );
+               safesnprintf( atcmd_output, 255, "Maintenance mode will be commence in %d minutes. Players are adviced to log out right now. Maintenance last %d minutes. Server will come back up on %s", kick_duration, maintenance_duration, chrif->maintenance_timeformat );
+               intif->broadcast( atcmd_output , strlen(atcmd_output)+1, 0xFFFF00 );
+       }
+       chrif->maintenance_countid = timer->add( timer->gettick() +( ( kick_duration == 1 )? 1000 : 60000 ), chrif->maintenance_countdown, 0, 0 );
+       ShowStatus( CL_YELLOW "Maintenance will start in %d min by " CL_GREEN "%s" CL_RESET ".\n", kick_duration, sd->status.name );
+       return true;
+}
+
+ACMD(maintenanceoff) {
+       char esc_name[46];
+       if ( chrif->maintenance_endtime <= time(NULL) ) {
+               clif->message( fd, "The server is currently in not in maintenance mode." );
+               return true;
+       }
+       SQL->EscapeString( map->mysql_handle, esc_name, sd->status.name );
+       if ( SQL->Query( map->mysql_handle, "insert into maintenance values ( null, %d, '%s', '   @maintenanceoff', 0, null, now(), now() )", sd->status.account_id, esc_name ) == SQL_ERROR ) {
+               Sql_ShowDebug( map->mysql_handle );
+               return false;
+       }
+       chrif->maintenance_group = 0;
+       chrif->maintenance_starttime = (int)time(NULL);
+       chrif->maintenance_endtime = (int)time(NULL);
+       if ( chrif->maintenance_countid != INVALID_TIMER ) {
+               timer->delete( chrif->maintenance_countid, chrif->maintenance_countdown );
+               chrif->maintenance_countid = INVALID_TIMER;
+       }
+       if ( chrif->maintenance_timerid != INVALID_TIMER ) {
+               timer->delete( chrif->maintenance_timerid, chrif->maintenance_progress );
+               chrif->maintenance_timerid = INVALID_TIMER;
+       }
+       safesnprintf( atcmd_output, 255, "%s ended the Maintenance mode. Players are able to login now.", sd->status.name );
+       intif->broadcast( atcmd_output , strlen(atcmd_output)+1, 0xFFFF00 );
+       ShowStatus( CL_YELLOW "Maintenance has ended by " CL_GREEN "%s" CL_RESET ".\n", sd->status.name );
+       return true;
+}
+
 /**
  * Fills the reference of available commands in atcommand DBMap
  **/
@@ -9384,6 +9527,8 @@ void atcommand_basecommands(void) {
         * Command reference list, place the base of your commands here
         **/
        AtCommandInfo atcommand_base[] = {
+               ACMD_DEF(maintenance),
+               ACMD_DEF(maintenanceoff),
                ACMD_DEF2("warp", mapmove),
                ACMD_DEF(where),
                ACMD_DEF(jumpto),
diff --git a/src/map/chrif.c b/src/map/chrif.c
index ebdace2..64669bd 100644
--- a/src/map/chrif.c
+++ b/src/map/chrif.c
@@ -413,6 +413,63 @@ bool chrif_changemapserverack(int account_id, int login_id1, int login_id2, int
        return (!login_id1)?false:true; // Is this the best approach here?
 }
 
+int chrif_maintenance_countdown( int tid, int64 tick, int id, intptr data ) {
+       char aaa[255];
+       int countdown = chrif->maintenance_starttime - (int)time(NULL);
+       if ( countdown > 90 ) {
+               safesnprintf( aaa, 255, "Maintenance will start in %d minutes", countdown /60 );
+               intif->broadcast( aaa , strlen(aaa)+1, 0xFFFF00 );
+               timer->delete( chrif->maintenance_countid, chrif->maintenance_countdown );
+               if ( ( countdown % 60 ) > 0 ) // fine tune the timer
+                       chrif->maintenance_countid = timer->add( timer->gettick() + ( ( countdown % 60 ) * 1000 ), chrif->maintenance_countdown, 0, 0 );
+               else
+                       chrif->maintenance_countid = timer->add( timer->gettick() + 60000, chrif->maintenance_countdown, 0, 0 );
+       }
+       else if ( countdown > 15 ) {
+               safesnprintf( aaa, 255, "Maintenance will start in %d seconds", countdown );
+               intif->broadcast( aaa , strlen(aaa)+1, 0xFFFF00 );
+               timer->delete( chrif->maintenance_countid, chrif->maintenance_countdown );
+               if ( ( countdown % 10 ) > 0 ) // fine tune the timer
+                       chrif->maintenance_countid = timer->add( timer->gettick() + ( ( countdown % 10 ) * 1000 ), chrif->maintenance_countdown, 0, 0 );
+               else
+                       chrif->maintenance_countid = timer->add( timer->gettick() + 10000, chrif->maintenance_countdown, 0, 0 );
+       }
+       else if ( countdown > 0 ) {
+               safesnprintf( aaa, 255, "Maintenance will start in %d seconds", countdown );
+               intif->broadcast( aaa , strlen(aaa)+1, 0xFFFF00 );
+               timer->delete( chrif->maintenance_countid, chrif->maintenance_countdown );
+               chrif->maintenance_countid = timer->add( timer->gettick() + 1000, chrif->maintenance_countdown, 0, 0 );
+       }
+       else {
+               struct s_mapiterator* iter = mapit->alloc( MAPIT_NORMAL, BL_PC );
+               TBL_PC *sd;
+               safesnprintf( aaa, 255, "Maintenance starts now. Every player will be kick out." );
+               intif->broadcast( aaa , strlen(aaa)+1, 0xFFFF00 );
+               for ( sd = (TBL_PC*)mapit->first(iter); mapit->exists(iter); sd = (TBL_PC*)mapit->next(iter) )
+                       if ( sd->group_id < chrif->maintenance_group )
+                               clif->authfail_fd( sd->fd, 1 );
+               mapit->free(iter);
+               timer->delete( chrif->maintenance_countid, chrif->maintenance_countdown );
+               chrif->maintenance_countid = INVALID_TIMER;
+               chrif->maintenance_timerid = timer->add( timer->gettick() + ( ( chrif->maintenance_endtime - chrif->maintenance_starttime )*1000 ), chrif->maintenance_progress, 0, 0 );
+               ShowStatus( CL_YELLOW "Maintenance has started." CL_RESET "\n" );
+       }
+       return false;
+}
+
+int chrif_maintenance_progress( int tid, int64 tick, int id, intptr data ) {
+       char aaa[255];
+       timer->delete( chrif->maintenance_timerid, chrif->maintenance_progress );
+       chrif->maintenance_timerid = INVALID_TIMER;
+       safesnprintf( aaa, 255, "Maintenance mode has ended. Players are able to login now." );
+       intif->broadcast( aaa , strlen(aaa)+1, 0xFFFF00 );
+       chrif->maintenance_group = 0;
+       chrif->maintenance_starttime = (int)time(NULL);
+       chrif->maintenance_endtime = (int)time(NULL);
+       ShowStatus( CL_YELLOW "Maintenance has ended." CL_RESET "\n" );
+       return false;
+}
+
 /*==========================================
  *
  *------------------------------------------*/
@@ -439,6 +496,57 @@ void chrif_connectack(int fd) {
       
        sockt->datasync(fd, true);
        chrif->skillid2idx(fd);
+       if ( SQL->Query( map->mysql_handle, "select minlv2connect, unix_timestamp( start_time ), unix_timestamp( end_time ), weekday( end_time ), hour( end_time ), minute( end_time ) from maintenance where id = ( select max(id) from maintenance )" ) == SQL_ERROR )
+               Sql_ShowDebug( map->mysql_handle );
+       else if ( SQL->NextRow( map->mysql_handle ) == SQL_SUCCESS ) {
+               char *data;
+               short weekday, hour, minute;
+               char* weekdayname[7] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
+               char am_pm[3], min_display[3];
+               size_t len;
+               if ( SQL->GetData( map->mysql_handle, 0, &data, NULL ) == SQL_SUCCESS )
+                       chrif->maintenance_group = atoi(data);
+               if ( SQL->GetData( map->mysql_handle, 1, &data, &len ) == SQL_SUCCESS )
+                       chrif->maintenance_starttime = atoi(data);
+               else
+                       chrif->maintenance_starttime = 0;
+               if ( SQL->GetData( map->mysql_handle, 2, &data, NULL ) == SQL_SUCCESS )
+                       chrif->maintenance_endtime = atoi(data);
+               if ( SQL->GetData( map->mysql_handle, 3, &data, NULL ) == SQL_SUCCESS )
+                       weekday = atoi(data);
+               if ( SQL->GetData( map->mysql_handle, 4, &data, NULL ) == SQL_SUCCESS )
+                       hour = atoi(data);
+               if ( SQL->GetData( map->mysql_handle, 5, &data, NULL ) == SQL_SUCCESS )
+                       minute = atoi(data);
+               if ( hour == 0 ) {
+                       hour = 12;
+                       safesnprintf( am_pm, 3, "AM" );
+               }
+               else if ( hour < 12 )
+                       safesnprintf( am_pm, 3, "AM" );
+               else {
+                       hour = hour - 12;
+                       safesnprintf( am_pm, 3, "PM" );
+               }
+               if ( minute < 10 )
+                       safesnprintf( min_display, 3, "0%d", minute );
+               else
+                       safesnprintf( min_display, 3, "%d", minute );
+               safesnprintf( chrif->maintenance_timeformat, 24, "%s, %d:%s %s", weekdayname[ weekday ], hour, min_display, am_pm );
+       }
+       if ( chrif->maintenance_starttime > time(NULL) ) {
+               int countdown = chrif->maintenance_starttime - (int)time(NULL);
+               if ( countdown > 60 )
+                       chrif->maintenance_countid = timer->add( timer->gettick() + ( ( countdown % 60 )*1000 ), chrif->maintenance_countdown, 0, 0 );
+               else
+                       chrif->maintenance_countid = timer->add( timer->gettick() + 1000, chrif->maintenance_countdown, 0, 0 );
+               ShowStatus( CL_YELLOW "Maintenance will start in %d min %d sec." CL_RESET "\n", countdown /60, countdown %60 );
+       }
+       else if ( chrif->maintenance_endtime > time(NULL) ) {
+               int countdown = chrif->maintenance_endtime - (int)time(NULL);
+               chrif->maintenance_timerid = timer->add( timer->gettick() + ( countdown *1000 ), chrif->maintenance_progress, 0, 0 );
+               ShowStatus( CL_YELLOW "Maintenance will end in %d min %d sec." CL_RESET "\n", countdown/60, countdown %60 );
+       }
 }
 
 /**
@@ -1753,4 +1861,7 @@ void chrif_defaults(void) {
        chrif->parse = chrif_parse;
        chrif->save_scdata_single = chrif_save_scdata_single;
        chrif->del_scdata_single = chrif_del_scdata_single;
+
+       chrif->maintenance_countdown = chrif_maintenance_countdown;
+       chrif->maintenance_progress = chrif_maintenance_progress;
 }
diff --git a/src/map/chrif.h b/src/map/chrif.h
index 11baaf5..278d88d 100644
--- a/src/map/chrif.h
+++ b/src/map/chrif.h
@@ -146,6 +146,15 @@ struct chrif_interface {
        int (*parse) (int fd);
        void (*save_scdata_single) (int account_id, int char_id, short type, struct status_change_entry *sce);
        void (*del_scdata_single) (int account_id, int char_id, short type);
+       int (*maintenance_countdown) ( int tid, int64 tick, int id, intptr data );
+       int (*maintenance_progress) ( int tid, int64 tick, int id, intptr data );
+
+       int maintenance_group;
+       int maintenance_starttime;
+       int maintenance_endtime;
+       int maintenance_countid;
+       int maintenance_timerid;
+       char maintenance_timeformat[24];
 };
 
 struct chrif_interface *chrif;
diff --git a/src/map/clif.c b/src/map/clif.c
index d9acf07..1c2975b 100644
--- a/src/map/clif.c
+++ b/src/map/clif.c
@@ -9276,6 +9276,19 @@ void clif_parse_LoadEndAck(int fd,struct map_session_data *sd) {
 #endif
        bool first_time = false;
 
+       if ( sd->state.connect_new ) {
+               if ( chrif->maintenance_starttime > time(NULL) ) {
+                       char aaa[255];
+                       safesnprintf( aaa, 255, "Maintenance starts in %d min %d sec, last %d minutes. Server up by %s", ( chrif->maintenance_starttime - (int)time(NULL) ) /60, ( chrif->maintenance_starttime - (int)time(NULL) ) %60, ( chrif->maintenance_endtime - chrif->maintenance_starttime ) /60, chrif->maintenance_timeformat );
+                       clif->message( sd->fd, aaa );
+               }
+               else if ( chrif->maintenance_endtime > time(NULL) ) {
+                       char aaa[255];
+                       safesnprintf( aaa, 255, "Server is currently in maintenance mode, will end in %d min %d sec on %s", ( chrif->maintenance_endtime - (int)time(NULL) ) /60, ( chrif->maintenance_endtime - (int)time(NULL) ) %60, chrif->maintenance_timeformat );
+                       clif->message( sd->fd, aaa );
+               }
+       }
+
        if(sd->bl.prev != NULL)
                return;
 
diff --git a/src/map/script.c b/src/map/script.c
index 2c89321..0e4c78c 100644
--- a/src/map/script.c
+++ b/src/map/script.c
@@ -18735,10 +18735,164 @@ bool script_hp_add(char *name, char *args, bool (*func)(struct script_state *st)
        return script->add_builtin(&buildin, true);
 }
 
+//     maintenance <Group ID can stay 1~99>, <duration to kick in minute>, <maintenance duration in minute> { , <reason> };
+BUILDIN(maintenance) {
+       int group_id, kick_duration, maintenance_duration;
+       char reason[99], esc_reason[198], min_display[3];
+       short weekday, hour, minute;
+       if ( chrif->maintenance_endtime > time(NULL) ) {
+               ShowError( "buildin_maintenance: Maintenance is currently ON.\n" );
+               return false;
+       }
+       group_id = script_getnum(st,2);
+       kick_duration = script_getnum(st,3);
+       maintenance_duration = script_getnum(st,4);
+       if ( !group_id ) {
+               ShowError( "buildin_maintenance: Group ID field cannot be 0.\n" );
+               return false;
+       }
+       if ( group_id < 1 || group_id > 99 ) {
+               ShowError( "buildin_maintenance: Invalid Group ID %d. Range must between 1~99.\n", group_id );
+               return false;
+       }
+       if ( kick_duration <= 0 ) {
+               ShowError( "buildin_maintenance: Kick duration cannot be 0 or negative numbers.\n" );
+               return false;
+       }
+       if ( kick_duration > 1440 ) {
+               ShowError( "buildin_maintenance: Kick duration cannot be more than 1 day.\n" );
+               return false;
+       }
+       if ( maintenance_duration <= 0 ) {
+               ShowError( "buildin_maintenance: Maintenance duration cannot be 0 or negative numbers.\n" );
+               return false;
+       }
+       if ( maintenance_duration > 10080 ) {
+               ShowError( "buildin_maintenance: Maintenance duration cannot be more than 1 week.\n" );
+               return false;
+       }
+       safesnprintf( reason, 99, script_hasdata(st,5)? script_getstr(st,5) : "   *Regular server maintenance*" );
+       SQL->EscapeString( map->mysql_handle, esc_reason, reason );
+       if ( SQL->Query( map->mysql_handle, "insert into maintenance values ( null, 0, 'NPC', '%s', %d, now(), timestampadd( minute, %d, now() ), timestampadd( minute, %d, now() ) )", esc_reason, group_id, kick_duration, kick_duration + maintenance_duration ) == SQL_ERROR ) {
+               Sql_ShowDebug( map->mysql_handle );
+               return false;
+       }
+       if ( SQL->Query( map->mysql_handle, "select unix_timestamp( start_time ), unix_timestamp( end_time ), weekday( end_time ), hour( end_time ), minute( end_time ) from maintenance where id = ( select max(id) from maintenance )" ) == SQL_ERROR ) {
+               Sql_ShowDebug( map->mysql_handle );
+               return false;
+       }
+       else if ( SQL->NextRow( map->mysql_handle ) == SQL_SUCCESS ) {
+               char *data;
+               chrif->maintenance_group = group_id;
+               if ( SQL->GetData( map->mysql_handle, 0, &data, NULL ) == SQL_SUCCESS )
+                       chrif->maintenance_starttime = atoi(data);
+               if ( SQL->GetData( map->mysql_handle, 1, &data, NULL ) == SQL_SUCCESS )
+                       chrif->maintenance_endtime = atoi(data);
+               if ( SQL->GetData( map->mysql_handle, 2, &data, NULL ) == SQL_SUCCESS )
+                       weekday = atoi(data);
+               if ( SQL->GetData( map->mysql_handle, 3, &data, NULL ) == SQL_SUCCESS )
+                       hour = atoi(data);
+               if ( SQL->GetData( map->mysql_handle, 4, &data, NULL ) == SQL_SUCCESS )
+                       minute = atoi(data);
+               SQL->FreeResult( map->mysql_handle );
+       }
+       else {
+               SQL->FreeResult( map->mysql_handle );
+               return false;
+       }
+       {       // stupid date_format doesn't work
+               char* weekdayname[7] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
+               char am_pm[3], aaa[255];
+               if ( hour == 0 ) {
+                       hour = 12;
+                       safesnprintf( am_pm, 3, "AM" );
+               }
+               else if ( hour < 12 )
+                       safesnprintf( am_pm, 3, "AM" );
+               else {
+                       hour = hour - 12;
+                       safesnprintf( am_pm, 3, "PM" );
+               }
+               if ( minute < 10 )
+                       safesnprintf( min_display, 3, "0%d", minute );
+               else
+                       safesnprintf( min_display, 3, "%d", minute );
+               safesnprintf( chrif->maintenance_timeformat, 24, "%s, %d:%s %s", weekdayname[ weekday ], hour, min_display, am_pm );
+               safesnprintf( aaa, 255, "Maintenance mode will be commence in %d minutes. Players are adviced to log out right now. Maintenance last %d minutes. Server will come back up on %s", kick_duration, maintenance_duration, chrif->maintenance_timeformat );
+               intif->broadcast( aaa , strlen(aaa)+1, 0xFFFF00 );
+       }
+       chrif->maintenance_countid = timer->add( timer->gettick() +( ( kick_duration == 1 )? 1000 : 60000 ), chrif->maintenance_countdown, 0, 0 );
+       ShowStatus( CL_YELLOW "Maintenance will start in %d min" CL_RESET ".\n", kick_duration );
+       return true;
+}
+
+BUILDIN(maintenanceoff) {
+       char reason[99], esc_reason[198];
+       if ( chrif->maintenance_endtime <= time(NULL) ) {
+               ShowError( "buildin_maintenanceoff: Maintenance is currently OFF.\n" );
+               return false;
+       }
+       safesnprintf( reason, 99, script_hasdata(st,2)? script_getstr(st,2) : "   *maintenance off*" );
+       SQL->EscapeString( map->mysql_handle, esc_reason, reason );
+       if ( SQL->Query( map->mysql_handle, "insert into maintenance values ( null, 0, 'NPC', '%s', 0, null, now(), now() )", esc_reason ) == SQL_ERROR ) {
+               Sql_ShowDebug( map->mysql_handle );
+               return false;
+       }
+       chrif->maintenance_group = 0;
+       chrif->maintenance_starttime = (int)time(NULL);
+       chrif->maintenance_endtime = (int)time(NULL);
+       if ( chrif->maintenance_countid != INVALID_TIMER ) {
+               timer->delete( chrif->maintenance_countid, chrif->maintenance_countdown );
+               chrif->maintenance_countid = INVALID_TIMER;
+       }
+       if ( chrif->maintenance_timerid != INVALID_TIMER ) {
+               timer->delete( chrif->maintenance_timerid, chrif->maintenance_progress );
+               chrif->maintenance_timerid = INVALID_TIMER;
+       }
+       intif->broadcast( "Maintenance mode has ended. Players are able to login now." , 255, 0xFFFF00 );
+       ShowStatus( CL_YELLOW "Maintenance has ended" CL_RESET ".\n" );
+       return true;
+}
+
+BUILDIN(maintenancecheck) {
+       int type = script_hasdata(st,2)? script_getnum(st,2) : 0;
+       switch( type ) {
+       default:
+               if ( chrif->maintenance_starttime > time(NULL) )
+                       script_pushint(st, 1);
+               else if ( chrif->maintenance_endtime > time(NULL) )
+                       script_pushint(st, 2);
+               else
+                       script_pushint(st, 0);
+               return true;
+       case 1:
+               script_pushint(st, chrif->maintenance_group);
+               return true;
+       case 2:
+               script_pushint(st, chrif->maintenance_starttime);
+               return true;
+       case 3:
+               script_pushint(st, chrif->maintenance_endtime);
+               return true;
+       case 4:
+               script_pushint(st, chrif->maintenance_countid);
+               return true;
+       case 5:
+               script_pushint(st, chrif->maintenance_timerid);
+               return true;
+//     case 6: // for some reason this cause memory leak
+//             script_pushstr(st, chrif->maintenance_timeformat );
+//             return true;
+       }
+}
+
 #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args }
 #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args }
 void script_parse_builtin(void) {
        struct script_function BUILDIN[] = {
+               BUILDIN_DEF(maintenance,"iii?"),
+               BUILDIN_DEF(maintenanceoff,"?"),
+               BUILDIN_DEF(maintenancecheck,"?"),
                // NPC interaction
                BUILDIN_DEF(mes,"s*"),
                BUILDIN_DEF(next,""),
 
Viewed 849 times, submitted by AnnieRuru.