notificationsystem.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. const static float NOTIFICATION_FADE_TIME = 3.0; //! DEPRECATED (moved into NotificationSystem)
  2. enum NotificationType
  3. {
  4. FRIEND_CONNECTED,
  5. INVITE_FAIL_SAME_SERVER,
  6. JOIN_FAIL_GET_SESSION,
  7. CONNECT_FAIL_GENERIC,
  8. DISCONNECTED,
  9. GENERIC_ERROR,
  10. //Please add types before this item
  11. NOTIFICATIONS_END
  12. }
  13. class NotificationRuntimeData
  14. {
  15. ref NotificationData m_StaticData;
  16. float m_NotificationTime;
  17. float m_TimeRemaining;
  18. string m_DetailText;
  19. void NotificationRuntimeData(float time, NotificationData data, string detail_text)
  20. {
  21. m_NotificationTime = time;
  22. m_TimeRemaining = time;
  23. m_StaticData = data;
  24. if (detail_text != "")
  25. m_DetailText = detail_text;
  26. else
  27. m_DetailText = m_StaticData.m_DescriptionText;
  28. }
  29. float GetTime()
  30. {
  31. return m_NotificationTime;
  32. }
  33. float GetRemainingTime()
  34. {
  35. return m_TimeRemaining;
  36. }
  37. string GetIcon()
  38. {
  39. return m_StaticData.m_Icon;
  40. }
  41. string GetTitleText()
  42. {
  43. return m_StaticData.m_TitleText;
  44. }
  45. string GetDetailText()
  46. {
  47. return m_DetailText;
  48. }
  49. void UpdateRemainingTime(float updateInterval)
  50. {
  51. m_TimeRemaining -= updateInterval;
  52. }
  53. //! DEPRECATED
  54. void SetTime(float time);
  55. }
  56. class NotificationSystem
  57. {
  58. const int DEFAULT_TIME_DISPLAYED = 10;
  59. const float NOTIFICATION_FADE_TIME = 3.0;
  60. protected static const string JSON_FILE_PATH = "scripts/data/notifications.json";
  61. protected static const int MAX_NOTIFICATIONS = 5;
  62. private static const float UPDATE_INTERVAL_THRESHOLD = 1.0;
  63. protected static ref NotificationSystem m_Instance;
  64. protected ref array<ref NotificationRuntimeData> m_TimeArray;
  65. protected ref array<ref NotificationRuntimeData> m_DeferredArray;
  66. protected ref map<NotificationType, ref NotificationData> m_DataArray;
  67. protected float m_TimeElapsed;
  68. ref ScriptInvoker m_OnNotificationAdded = new ScriptInvoker();
  69. ref ScriptInvoker m_OnNotificationRemoved = new ScriptInvoker();
  70. static void InitInstance()
  71. {
  72. if (!m_Instance)
  73. {
  74. m_Instance = new NotificationSystem();
  75. m_Instance.LoadNotificationData();
  76. }
  77. }
  78. static void CleanupInstance()
  79. {
  80. m_Instance = null;
  81. }
  82. static NotificationSystem GetInstance()
  83. {
  84. return m_Instance;
  85. }
  86. void NotificationSystem()
  87. {
  88. m_TimeElapsed = 0.0;
  89. m_TimeArray = new array<ref NotificationRuntimeData>();
  90. m_DeferredArray = new array<ref NotificationRuntimeData>();
  91. }
  92. /**
  93. \brief Send custom notification to player from server
  94. @param player the target player to send notification to
  95. @param show_time amount of time this notification is displayed
  96. @param title_text the title text that is displayed in the notification
  97. @param detail_text additional text that can be added to the notification under the title - will not display additional text if not set
  98. @param icon the icon that is displayed in the notification - will use default icon if not set
  99. */
  100. static void SendNotificationToPlayerExtended(Man player, float show_time, string title_text, string detail_text = "", string icon = "")
  101. {
  102. if (player)
  103. {
  104. SendNotificationToPlayerIdentityExtended(player.GetIdentity(), show_time, title_text, detail_text, icon);
  105. }
  106. }
  107. /**
  108. \brief Send custom notification to player identity from server
  109. @param player the target player to send notification to - if null, will send to all players
  110. @param show_time amount of time this notification is displayed
  111. @param title_text the title text that is displayed in the notification
  112. @param detail_text additional text that can be added to the notification under the title - will not display additional text if not set
  113. @param icon the icon that is displayed in the notification - will use default icon if not set
  114. */
  115. static void SendNotificationToPlayerIdentityExtended(PlayerIdentity player, float show_time, string title_text, string detail_text = "", string icon = "")
  116. {
  117. ScriptRPC rpc = new ScriptRPC();
  118. rpc.Write(show_time);
  119. rpc.Write(title_text);
  120. rpc.Write(detail_text);
  121. rpc.Write(icon);
  122. rpc.Send(null, ERPCs.RPC_SEND_NOTIFICATION_EXTENDED, true, player);
  123. }
  124. /**
  125. \brief Send notification from default types to player from server
  126. @param player the target player to send notification to
  127. @param type the type of notification to send - these can be viewed in /Scripts/Data/Notifications.json
  128. @param show_time amount of time this notification is displayed
  129. @param detail_text additional text that can be added to the notification under the title - will not display additional text if not set
  130. */
  131. static void SendNotificationToPlayer(Man player, NotificationType type, float show_time, string detail_text = "")
  132. {
  133. if (player)
  134. {
  135. SendNotificationToPlayerIdentity(player.GetIdentity(), type, show_time, detail_text);
  136. }
  137. }
  138. /**
  139. \brief Send notification from default types to player identity from server
  140. @param player the target player to send notification to - if null, will send to all players
  141. @param type the type of notification to send - these can be viewed in /Scripts/Data/Notifications.json
  142. @param show_time amount of time this notification is displayed
  143. @param detail_text additional text that can be added to the notification under the title - will not display additional text if not set
  144. */
  145. static void SendNotificationToPlayerIdentity( PlayerIdentity player, NotificationType type, float show_time, string detail_text = "" )
  146. {
  147. ScriptRPC rpc = new ScriptRPC();
  148. rpc.Write(type);
  149. rpc.Write(show_time);
  150. rpc.Write(detail_text);
  151. rpc.Send(null, ERPCs.RPC_SEND_NOTIFICATION, true, player);
  152. }
  153. /**
  154. \brief Send notification from default types to local player
  155. @param type the type of notification to send - these can be viewed in /Scripts/Data/Notifications.json
  156. @param show_time amount of time this notification is displayed
  157. @param detail_text additional text that can be added to the notification under the title - will not display additional text if not set
  158. */
  159. static void AddNotification(NotificationType type, float show_time, string detail_text = "")
  160. {
  161. if (m_Instance.m_TimeArray.Count() < MAX_NOTIFICATIONS)
  162. {
  163. NotificationRuntimeData data = new NotificationRuntimeData(show_time, m_Instance.GetNotificationData(type), detail_text);
  164. m_Instance.m_TimeArray.Insert(data);
  165. m_Instance.m_OnNotificationAdded.Invoke(data);
  166. }
  167. else
  168. {
  169. NotificationRuntimeData dataDeferred = new NotificationRuntimeData(show_time, m_Instance.GetNotificationData(type), detail_text);
  170. m_Instance.m_DeferredArray.Insert(dataDeferred);
  171. }
  172. }
  173. /**
  174. \brief Send custom notification from to local player
  175. @param show_time amount of time this notification is displayed
  176. @param title_text the title text that is displayed in the notification
  177. @param detail_text additional text that can be added to the notification under the title - will not display additional text if not set
  178. @param icon the icon that is displayed in the notification - will use default icon if not set
  179. */
  180. static void AddNotificationExtended(float show_time, string title_text, string detail_text = "", string icon = "")
  181. {
  182. if (m_Instance.m_TimeArray.Count() < MAX_NOTIFICATIONS)
  183. {
  184. NotificationData tempData = new NotificationData(icon, title_text);
  185. NotificationRuntimeData data = new NotificationRuntimeData(show_time, tempData, detail_text);
  186. m_Instance.m_TimeArray.Insert(data);
  187. m_Instance.m_OnNotificationAdded.Invoke(data);
  188. }
  189. else
  190. {
  191. NotificationData tempDataDeferred = new NotificationData(icon, title_text);
  192. NotificationRuntimeData dataDeferred = new NotificationRuntimeData(show_time, tempDataDeferred, detail_text);
  193. m_Instance.m_DeferredArray.Insert(dataDeferred);
  194. }
  195. }
  196. static void Update(float timeslice)
  197. {
  198. if (m_Instance)
  199. {
  200. if (g_Game.GetGameState() != DayZGameState.IN_GAME && g_Game.GetGameState() != DayZGameState.MAIN_MENU)
  201. return;
  202. m_Instance.m_TimeElapsed += timeslice;
  203. if (m_Instance.m_TimeElapsed >= UPDATE_INTERVAL_THRESHOLD)
  204. {
  205. array<NotificationRuntimeData> expiredNotifications = new array<NotificationRuntimeData>();
  206. foreach (NotificationRuntimeData visibleNotificationData : m_Instance.m_TimeArray)
  207. {
  208. if (visibleNotificationData.GetRemainingTime() <= 0.0)
  209. expiredNotifications.Insert(visibleNotificationData);
  210. else
  211. visibleNotificationData.UpdateRemainingTime(UPDATE_INTERVAL_THRESHOLD);
  212. }
  213. foreach (NotificationRuntimeData expiredNotificationData : expiredNotifications)
  214. {
  215. m_Instance.m_OnNotificationRemoved.Invoke(expiredNotificationData);
  216. m_Instance.m_TimeArray.RemoveItem(expiredNotificationData);
  217. if (m_Instance.m_DeferredArray.Count() > 0)
  218. {
  219. int count = m_Instance.m_DeferredArray.Count();
  220. NotificationRuntimeData deferredNotificationData = m_Instance.m_DeferredArray.Get(count - 1);
  221. m_Instance.m_TimeArray.Insert(deferredNotificationData);
  222. m_Instance.m_OnNotificationAdded.Invoke(deferredNotificationData);
  223. m_Instance.m_DeferredArray.Remove(count - 1);
  224. }
  225. }
  226. m_Instance.m_TimeElapsed = 0;
  227. }
  228. }
  229. }
  230. protected NotificationData GetNotificationData(NotificationType type)
  231. {
  232. if (m_DataArray.Contains(type))
  233. return m_DataArray.Get(type);
  234. return null;
  235. }
  236. static void LoadNotificationData()
  237. {
  238. map<NotificationType, NotificationData> dataArray;
  239. m_Instance.m_DataArray = new map<NotificationType, ref NotificationData>();
  240. string errorMessage;
  241. if (!JsonFileLoader<map<NotificationType, NotificationData>>.LoadFile(JSON_FILE_PATH, dataArray, errorMessage))
  242. ErrorEx(errorMessage);
  243. m_Instance.m_DataArray.Copy(dataArray);
  244. array<int> notificationTypes = new array<int>();
  245. NotificationType notificationType = 0;
  246. while (notificationType != NotificationType.NOTIFICATIONS_END)
  247. {
  248. notificationTypes.Insert(notificationType);
  249. notificationType++;
  250. }
  251. for (int i = 0; i < m_Instance.m_DataArray.Count(); ++i)
  252. {
  253. notificationTypes.RemoveItem(m_Instance.m_DataArray.GetKey(i));
  254. }
  255. if (notificationTypes.Count() > 0)
  256. {
  257. foreach (NotificationType notificationType2 : notificationTypes)
  258. {
  259. NotificationData notificationData = new NotificationData(
  260. "please_add_an_icon",
  261. "please_add_a_title",
  262. "optional_description",
  263. );
  264. m_Instance.m_DataArray.Insert(notificationType2, notificationData);
  265. }
  266. if (!JsonFileLoader<map<NotificationType, ref NotificationData>>.SaveFile(JSON_FILE_PATH, m_Instance.m_DataArray, errorMessage))
  267. ErrorEx(errorMessage);
  268. }
  269. }
  270. }