MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame^] | 1 | #pragma strong_types, save_types |
| 2 | #pragma no_clone, no_shadow |
| 3 | |
| 4 | #include "/p/daemon/ch.h" |
| 5 | #include "/p/daemon/uptime_master.h" |
| 6 | |
| 7 | /// Prototypen /// |
| 8 | |
| 9 | void create(); |
| 10 | void reset(); |
| 11 | |
| 12 | void SaveMaxUptime(); |
| 13 | mapping QuerMaxUptime(); |
| 14 | |
| 15 | /// Globale Variablen /// |
| 16 | static int next_save_uptime; |
| 17 | static int max_uptime_reached; |
| 18 | static int last_record_boot; |
| 19 | |
| 20 | /// Funktionen /// |
| 21 | |
| 22 | void create() |
| 23 | { |
| 24 | seteuid(getuid(this_object())); |
| 25 | reset(); |
| 26 | } |
| 27 | |
| 28 | void reset() |
| 29 | { |
| 30 | call_out("SaveMaxUptime",1); |
| 31 | set_next_reset(300); |
| 32 | } |
| 33 | |
| 34 | // Ist eine Rekord-Uptime erreicht, wird diese hier gespeichert. |
| 35 | void SaveMaxUptime() |
| 36 | { |
| 37 | int max_shut, max_boot; |
| 38 | |
| 39 | // Lohnt es, schon wieder zu pruefen? |
| 40 | if (time()>next_save_uptime) |
| 41 | { |
| 42 | // Initialisieren |
| 43 | if (next_save_uptime==0) |
| 44 | { |
| 45 | DEBUG("Masterinit"); |
| 46 | |
| 47 | // Zeit berechnen, ab der ein Rekord aufgestellt ist. |
| 48 | // Vorher braucht nixhts mehr geprueft werden |
| 49 | sscanf(read_file(MAX_UPTIME)||"0 0","%d %d",last_record_boot, max_shut); |
| 50 | next_save_uptime=last_reboot_time()+(max_shut-last_record_boot)+1; |
| 51 | |
| 52 | } else { |
| 53 | DEBUG("Rekord neu speichern."); |
| 54 | |
| 55 | // Neuen Rekord in Datei schreiben. |
| 56 | catch(rm(MAX_UPTIME);publish); |
| 57 | write_file(MAX_UPTIME, last_reboot_time()+" "+time()); |
| 58 | |
| 59 | // Jetzt checken, ob die Uptime grade geknackt wurde ... |
| 60 | // Wenn ja, dann eine Meldung an den Master |
| 61 | if(last_reboot_time()!=last_record_boot) |
| 62 | { |
| 63 | DEBUG("Rekordmeldung. MB:"+last_record_boot+" LU:"+last_reboot_time()); |
| 64 | |
| 65 | // Nur einmal pro Rekord Uptime melden ... |
| 66 | last_record_boot=last_reboot_time(); |
| 67 | |
| 68 | // Hurraaah :) |
| 69 | CHMASTER->join(RECORD_CHANNEL,this_object()); |
| 70 | CHMASTER->send(RECORD_CHANNEL,this_object(), |
| 71 | "[<MasteR>:Lars] Wow, eine Rekord-Uptime.\n" |
| 72 | "[<MasteR>:Merlin] Das erlebt man auch nicht alle Tage ...\n" |
| 73 | "[<MasteR>:Der GameDriver] Dann wird es wohl Zeit fuer einen Reboot, oder?\n" |
| 74 | "[<MasteR>:Merlin] Faules Stueck, arbeite weiter!\n" |
| 75 | "[<MasteR>:Der GameDriver] Och menno!", |
| 76 | MSG_EMPTY); |
| 77 | CHMASTER->leave(RECORD_CHANNEL,this_object()); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | // Gibt Daten zur bisher maximalen Uptime zurueck: (299 Ticks) |
| 84 | // 1.) Wann wurde gebootet |
| 85 | // 2.) Wann endete die Uptime |
| 86 | // 3.) Wann wird dieser Rekord gebrochen? |
| 87 | // 4.) Dauer der Uptime als lesbarer String. |
| 88 | mapping QueryMaxUptime() |
| 89 | { |
| 90 | int t, tmp, max_boot, max_shut; |
| 91 | string s; |
| 92 | |
| 93 | sscanf(read_file(MAX_UPTIME)||"0 0","%d %d",max_boot, max_shut); |
| 94 | |
| 95 | t=max_shut-max_boot; |
| 96 | |
| 97 | s=""; |
| 98 | if (t>=86400) |
| 99 | s+=sprintf("%d Tag%s, ",tmp=t/86400,(tmp==1?"":"e")); |
| 100 | if (t>=3600) |
| 101 | s+=sprintf("%d Stunde%s, ",tmp=(t=t%86400)/3600,(tmp==1?"":"n")); |
| 102 | if (t>60) |
| 103 | s+=sprintf("%d Minute%s und ",tmp=(t=t%3600)/60,(tmp==1?"":"n")); |
| 104 | s+=sprintf("%d Sekunde%s",t=t%60,(t==1?"":"n")); |
| 105 | |
| 106 | return (["beginn":max_boot, |
| 107 | "ende":max_shut, |
| 108 | "naechster rekord":last_reboot_time()+(max_shut-max_boot), |
| 109 | "rekord als string":s]); |
| 110 | } |