#if defined _EventNotifier_included #endinput #endif #define _EventNotifier_included #define EVENT_STR_LEN 128 #define EVENT_MAPS_LEN 1024 enum struct EventInfo { int number; int start; // unix timestamp (UTC), 0 if unknown int end; // unix timestamp (UTC), 0 if unknown char name[EVENT_STR_LEN]; char server[EVENT_STR_LEN]; char rewards[EVENT_STR_LEN]; char leader[EVENT_STR_LEN]; char kickers[EVENT_STR_LEN]; char maps[EVENT_MAPS_LEN]; // ", "-joined list } #if !defined EVENTNOTIFIER_CORE /** * Number of events in the last successful fetch (0 before the first fetch). * * @return Event count. */ native int EventNotifier_GetEventCount(); /** * Whether at least one fetch has succeeded since the plugin loaded. * * @return True once the event list has been fetched. */ native bool EventNotifier_HasFetched(); /** * Finds an event by its event number. * * @param number Event number. * @return Index into the event list, or -1 if not found. */ native int EventNotifier_FindEventByNumber(int number); /** * Copies an event into the caller's EventInfo. * * @param index Index into the event list. * @param ev EventInfo to fill. * @param size Size of ev, must be sizeof(EventInfo). * @return True on success, false if index is out of range. * @error Buffer smaller than sizeof(EventInfo). */ native bool EventNotifier_GetEvent(int index, any[] ev, int size = sizeof(EventInfo)); /** * Starts a fetch of the event list now. No-op if a fetch is already running. */ native void EventNotifier_Refresh(); /** * Called after every successful fetch, once the event list has been replaced. */ forward void EventNotifier_OnEventsUpdated(); #endif // EVENTNOTIFIER_CORE // --------------------------------------------------------------------------- // Display helpers // --------------------------------------------------------------------------- /** * Formats a duration in seconds as "1d 2h 3m", "2h 3m" or "3m". */ stock void EventNotifier_FormatDuration(int seconds, char[] buffer, int maxlen) { int days = seconds / 86400; int hours = (seconds % 86400) / 3600; int minutes = (seconds % 3600) / 60; if (days > 0) Format(buffer, maxlen, "%dd %dh %dm", days, hours, minutes); else if (hours > 0) Format(buffer, maxlen, "%dh %dm", hours, minutes); else Format(buffer, maxlen, "%dm", minutes); } /** * Formats an event's status relative to now as a "Status: ..." line. */ stock void EventNotifier_FormatRelativeTime(int start, int end, char[] buffer, int maxlen) { int now = GetTime(); if (end > 0 && now >= start && now < end) { strcopy(buffer, maxlen, "Status: In progress now!"); return; } char span[32]; if (now >= start) { EventNotifier_FormatDuration(now - start, span, sizeof(span)); Format(buffer, maxlen, "Status: Started %s ago", span); } else { EventNotifier_FormatDuration(start - now, span, sizeof(span)); Format(buffer, maxlen, "Status: Starts in %s", span); } } #if !defined EVENTNOTIFIER_CORE public SharedPlugin __pl_EventNotifier = { name = "EventNotifier", file = "EventNotifier.smx", #if defined REQUIRE_PLUGIN required = 1 #else required = 0 #endif }; #if !defined REQUIRE_PLUGIN public void __pl_EventNotifier_SetNTVOptional() { MarkNativeAsOptional("EventNotifier_GetEventCount"); MarkNativeAsOptional("EventNotifier_HasFetched"); MarkNativeAsOptional("EventNotifier_FindEventByNumber"); MarkNativeAsOptional("EventNotifier_GetEvent"); MarkNativeAsOptional("EventNotifier_Refresh"); } #endif #endif // EVENTNOTIFIER_CORE