HLMOD.HU Forrás Megtekintés - www.hlmod.hu
  1. #include <amxmisc>
  2. #include <engine>
  3. #include <fakemeta>
  4. #include <geoip>
  5.  
  6. #define PLUGIN "Mutasd az időt!"
  7. #define VERSION "1.1"
  8. #define AUTHOR "Doondook, RaZ_HU"
  9.  
  10. #define SetUserBot(%1) gIsBot |= 1<<(%1 & (MAX_PLAYERS - 1))
  11. #define ClearUserBot(%1) gIsBot &= ~(1<<(%1 & (MAX_PLAYERS - 1)))
  12. #define IsUserBot(%1) gIsBot & 1<<(%1 & (MAX_PLAYERS - 1))
  13.  
  14. #define SetUserConnected(%1) gIsConnected |= 1<<(%1 & (MAX_PLAYERS - 1))
  15. #define ClearUserConnected(%1) gIsConnected &= ~(1<<(%1 & (MAX_PLAYERS - 1)))
  16. #define IsUserConnected(%1) gIsConnected & 1<<(%1 & (MAX_PLAYERS - 1))
  17.  
  18. //#define ReplaceTimer // Felül írja-e vagy sem a köridő jelzőt
  19.  
  20. #if AMXX_VERSION_NUM < 183
  21. #define MAX_PLAYERS 32
  22. #endif
  23.  
  24. new gIsBot, gIsConnected
  25.  
  26. #if defined ReplaceTimer
  27. new gMsgRoundTime
  28. #else
  29. new gMsgHudSync
  30. #endif
  31.  
  32. new gMaxPlayers
  33.  
  34. new gUserUTC[MAX_PLAYERS + 1]
  35.  
  36. new Trie:gTrieUTC
  37.  
  38. new gTimer, gIterations
  39.  
  40. public plugin_init() {
  41. register_plugin(PLUGIN, VERSION, AUTHOR)
  42.  
  43.  
  44. #if AMXX_VERSION_NUM < 183
  45. gMaxPlayers = get_maxplayers()
  46. #else
  47. gMaxPlayers = MaxClients
  48. #endif
  49.  
  50. #if defined ReplaceTimer
  51. gMsgRoundTime = get_user_msgid("RoundTime")
  52. #endif
  53.  
  54. // Create timezones trie map of UTC offsets.
  55. getUTCbase()
  56.  
  57. // Get server's UTC offset.
  58. // If server's timezone is not recognized by <geoip> databases,
  59. // you can set it manually, +/- integer in minutes:
  60. // - for UTC 3:00
  61. // gUserUTC[0] = 180
  62. // - for UTC -1:30
  63. // gUserUTC[0] = -90
  64. // Comment the line below if you set it manually.
  65. gUserUTC[0] = getUserUTC(0)
  66.  
  67. // Get server's current time without UTC offset.
  68. new szTime[9], szHours[3], szMinutes[3], szSeconds[3]
  69. get_time("%H %M %S", szTime, charsmax(szTime))
  70. parse(szTime, szHours, charsmax(szHours), szMinutes, charsmax(szMinutes), szSeconds, charsmax(szSeconds))
  71.  
  72. // Using timer counter instead of calling get_time().
  73. gTimer = str_to_num(szHours) * 60 + str_to_num(szMinutes) - gUserUTC[0]
  74.  
  75. // Amount of the timer entity iterations to increment minutes.
  76. // Double up current seconds, because the timer entity thinks every 0.5 seconds.
  77. gIterations = str_to_num(szSeconds) * 2
  78.  
  79. // Create entity for using as permanent task.
  80. new szEntity = create_entity("info_target")
  81.  
  82. if(szEntity) {
  83. register_think("Timer", "showTime")
  84. set_pev(szEntity, pev_classname, "Timer")
  85. set_pev(szEntity, pev_nextthink, get_gametime() + 0.01)
  86. } else {
  87. set_fail_state("Cannot create timer entity.")
  88. }
  89.  
  90. #if !defined ReplaceTimer
  91. // Create sync object for hudmessage
  92. gMsgHudSync = CreateHudSyncObj()
  93. #endif
  94. }
  95.  
  96. public showTime(szEntity) {
  97. new szTime
  98. for(new id = 1; id <= gMaxPlayers; id++) {
  99. if(~IsUserConnected(id) || IsUserBot(id)) {
  100. continue
  101. }
  102.  
  103. szTime = gTimer + gUserUTC[id]
  104.  
  105. // Check for midnight limit
  106. if(szTime >= 1440) {
  107. szTime -= 1440
  108. } else if (szTime < 0) {
  109. szTime += 1440
  110. }
  111.  
  112. #if defined ReplaceTimer
  113. // Replace round time with server's UTC 0 time + user's UTC offset = user's time in his timezone.
  114. message_begin(MSG_ONE_UNRELIABLE, gMsgRoundTime, _, id)
  115. write_short(szTime + 1) // +1 because of round timer implementation (60 will be 0:59)
  116. message_end()
  117. #else
  118. // HUD üzenet kiíratása
  119. displayTime(id, szTime)
  120. #endif
  121. }
  122.  
  123. // Count iterations of the entity think
  124. gIterations++
  125.  
  126. // Increment timer every 120 iterations (1 minute)
  127. if(gIterations == 120) {
  128. gTimer++
  129. gIterations = 0
  130. }
  131.  
  132. // Set counter to zero at midnight
  133. if(gTimer >= 1440) {
  134. gTimer = 0
  135. }
  136.  
  137. // Lets make it infinite
  138. set_pev(szEntity, pev_nextthink, get_gametime() + 0.5)
  139. }
  140.  
  141. #if !defined ReplaceTimer
  142. public displayTime(id, theTime) {
  143. new iMinutes = theTime % 60
  144. new iHours = floatround( ( theTime - iMinutes ) / 60.0 )
  145. new iTimeText[8]
  146.  
  147. // Szépítés
  148. if(iMinutes < 10)
  149. formatex(iTimeText, charsmax(iTimeText), "%d: 0%d", iHours, iMinutes)
  150. else
  151. formatex(iTimeText, charsmax(iTimeText), "%d: %d", iHours, iMinutes)
  152.  
  153. // Az idő kijelzése a HUD-ra
  154. set_hudmessage(30, 30, 30, 0.488, 0.94, 0, 0.3, 1.0, 0.0, 0.0, -1)
  155. ShowSyncHudMsg(id, gMsgHudSync, iTimeText )
  156. }
  157. #endif
  158.  
  159. getUserUTC(id) {
  160. // Get user's timezone by IP.
  161. new szTimezone[64], szIP[20]
  162. get_user_ip(id, szIP, charsmax(szIP), 1)
  163.  
  164. // Helyi szerver esetén használjon egyénileg megadott időzónát
  165. // Példákhoz lásd a GeoTimezones.txt tartalmát!
  166. if( equal(szIP, "192",3) || equal(szIP, "127", 3) )
  167. {
  168. formatex(szTimezone, charsmax(szTimezone), "Europe/Budapest")
  169. }
  170. else
  171. {
  172. get_user_ip(id, szIP, charsmax(szIP), 1)
  173. geoip_timezone(szIP, szTimezone, charsmax(szTimezone))
  174. }
  175.  
  176. // Check for determined timezone in trie map
  177. if(TrieKeyExists(gTrieUTC, szTimezone)) {
  178. // Get UTC offset by user's timezone
  179. new szUTC
  180. TrieGetCell(gTrieUTC, szTimezone, szUTC)
  181.  
  182. return szUTC
  183. }
  184.  
  185. return 0
  186. }
  187.  
  188. getUTCbase() {
  189. // Convert file's information to trie map
  190. gTrieUTC = TrieCreate()
  191.  
  192. new szPath[128]
  193. get_datadir(szPath, charsmax(szPath))
  194. formatex(szPath, charsmax(szPath), "%s/%s", szPath, "GeoTimezones.txt")
  195.  
  196. if (!file_exists(szPath)) {
  197. log_amx("Cannot open %s", szPath)
  198. } else {
  199. new szLine[81], szKey[64], szValue[16]
  200. new szFile = fopen(szPath, "r")
  201.  
  202. while(!feof(szFile)) {
  203. fgets(szFile, szLine, charsmax(szLine))
  204.  
  205. strtok(szLine, szKey, charsmax(szKey), szValue, charsmax(szValue), '@')
  206.  
  207. // Convert UTC offset value to seconds and save it in trie map
  208. new szHours[4], szMinutes[3]
  209. strtok(szValue, szHours, charsmax(szHours), szMinutes, charsmax(szMinutes), ':')
  210.  
  211. TrieSetCell(gTrieUTC, szKey, str_to_num(szHours) * 60 + str_to_num(szMinutes))
  212. }
  213.  
  214. fclose(szFile)
  215. }
  216. }
  217.  
  218. public client_putinserver(id) {
  219. SetUserConnected(id)
  220.  
  221. if(is_user_bot(id) || is_user_hltv(id)) {
  222. SetUserBot(id)
  223. }
  224. else
  225. gUserUTC[id] = getUserUTC(id)
  226. }
  227.  
  228. #if AMXX_VERSION_NUM < 183
  229. public client_disconnect(id) {
  230. #else
  231. public client_disconnected(id) {
  232. #endif
  233. ClearUserBot(id)
  234. ClearUserConnected(id)
  235. }
  236.  
  237. public plugin_end() {
  238. TrieDestroy(gTrieUTC)
  239. }