1 #region Copyright & License Information
2 /*
3  * Copyright 2007-2020 The OpenRA Developers (see AUTHORS)
4  * This file is part of OpenRA, which is free software. It is made
5  * available to you under the terms of the GNU General Public License
6  * as published by the Free Software Foundation, either version 3 of
7  * the License, or (at your option) any later version. For more
8  * information, see COPYING.
9  */
10 #endregion
11 
12 using System;
13 using System.Collections.Generic;
14 using System.Linq;
15 using OpenRA.Mods.Common.Widgets;
16 using OpenRA.Primitives;
17 using OpenRA.Traits;
18 using OpenRA.Widgets;
19 
20 namespace OpenRA.Mods.Common.Traits
21 {
22 	[Desc("This trait allows setting a time limit on matches. Attach this to the World actor.")]
23 	public class TimeLimitManagerInfo : ITraitInfo, ILobbyOptions, IRulesetLoaded
24 	{
25 		[Desc("Label that will be shown for the time limit option in the lobby.")]
26 		public readonly string TimeLimitLabel = "Time Limit";
27 
28 		[Desc("Tooltip description that will be shown for the time limit option in the lobby.")]
29 		public readonly string TimeLimitDescription = "Player or team with the highest score after this time wins";
30 
31 		[Desc("Time Limit options that will be shown in the lobby dropdown. Values are in minutes.")]
32 		public readonly int[] TimeLimitOptions = { 0, 10, 20, 30, 40, 60, 90 };
33 
34 		[Desc("List of remaining minutes of game time when a text and optional speech notification should be made to players.")]
35 		public readonly Dictionary<int, string> TimeLimitWarnings = new Dictionary<int, string>
36 		{
37 			{ 1, null },
38 			{ 2, null },
39 			{ 3, null },
40 			{ 4, null },
41 			{ 5, null },
42 			{ 10, null },
43 		};
44 
45 		[Desc("Default selection for the time limit option in the lobby. Needs to use one of the TimeLimitOptions.")]
46 		public readonly int TimeLimitDefault = 0;
47 
48 		[Desc("Prevent the time limit option from being changed in the lobby.")]
49 		public readonly bool TimeLimitLocked = false;
50 
51 		[Desc("Whether to display the options dropdown in the lobby.")]
52 		public readonly bool TimeLimitDropdownVisible = true;
53 
54 		[Desc("Display order for the time limit dropdown in the lobby.")]
55 		public readonly int TimeLimitDisplayOrder = 0;
56 
57 		[Desc("Notification text for time limit warnings. The string '{0}' will be replaced by the remaining time in minutes, '{1}' is used for the plural form.")]
58 		public readonly string Notification = "{0} minute{1} remaining.";
59 
60 		[Desc("ID of the LabelWidget used to display a text ingame that will be updated every second.")]
61 		public readonly string CountdownLabel = null;
62 
63 		[Desc("Text to be shown using the CountdownLabel. The string '{0}' will be replaced by the time in hh:mm:ss format.")]
64 		public readonly string CountdownText = null;
65 
66 		[Desc("Will prevent showing/playing the built-in time limit warnings when set to true.")]
67 		public readonly bool SkipTimeRemainingNotifications = false;
68 
69 		[Desc("Will prevent showing/playing the built-in timer expired notification when set to true.")]
70 		public readonly bool SkipTimerExpiredNotification = false;
71 
RulesetLoaded(Ruleset rules, ActorInfo info)72 		void IRulesetLoaded<ActorInfo>.RulesetLoaded(Ruleset rules, ActorInfo info)
73 		{
74 			if (!TimeLimitOptions.Contains(TimeLimitDefault))
75 				throw new YamlException("TimeLimitDefault must be a value from TimeLimitOptions");
76 		}
77 
ILobbyOptions.LobbyOptions(Ruleset rules)78 		IEnumerable<LobbyOption> ILobbyOptions.LobbyOptions(Ruleset rules)
79 		{
80 			var timelimits = TimeLimitOptions.ToDictionary(c => c.ToString(), c =>
81 			{
82 				if (c == 0)
83 					return "No limit";
84 				else
85 					return c.ToString() + " minute{0}".F(c > 1 ? "s" : null);
86 			});
87 
88 			yield return new LobbyOption("timelimit", TimeLimitLabel, TimeLimitDescription, TimeLimitDropdownVisible, TimeLimitDisplayOrder,
89 				new ReadOnlyDictionary<string, string>(timelimits), TimeLimitDefault.ToString(), TimeLimitLocked);
90 		}
91 
Create(ActorInitializer init)92 		public object Create(ActorInitializer init) { return new TimeLimitManager(init.Self, this); }
93 	}
94 
95 	public class TimeLimitManager : INotifyTimeLimit, ITick, IWorldLoaded
96 	{
97 		readonly TimeLimitManagerInfo info;
98 		MapOptions mapOptions;
99 		LabelWidget countdownLabel;
100 		CachedTransform<int, string> countdown;
101 		int ticksRemaining;
102 
103 		public int TimeLimit;
104 		public string Notification;
105 
TimeLimitManager(Actor self, TimeLimitManagerInfo info)106 		public TimeLimitManager(Actor self, TimeLimitManagerInfo info)
107 		{
108 			this.info = info;
109 			Notification = info.Notification;
110 
111 			var tl = self.World.LobbyInfo.GlobalSettings.OptionOrDefault("timelimit", info.TimeLimitDefault.ToString());
112 			if (!int.TryParse(tl, out TimeLimit))
113 				TimeLimit = info.TimeLimitDefault;
114 
115 			// Convert from minutes to ticks
116 			TimeLimit *= 60 * (1000 / self.World.Timestep);
117 		}
118 
IWorldLoaded.WorldLoaded(World w, OpenRA.Graphics.WorldRenderer wr)119 		void IWorldLoaded.WorldLoaded(World w, OpenRA.Graphics.WorldRenderer wr)
120 		{
121 			mapOptions = w.WorldActor.Trait<MapOptions>();
122 			if (string.IsNullOrWhiteSpace(info.CountdownLabel) || string.IsNullOrWhiteSpace(info.CountdownText))
123 				return;
124 
125 			countdownLabel = Ui.Root.GetOrNull<LabelWidget>(info.CountdownLabel);
126 			if (countdownLabel != null)
127 			{
128 				countdown = new CachedTransform<int, string>(t =>
129 					info.CountdownText.F(WidgetUtils.FormatTime(t, true, w.IsReplay ? mapOptions.GameSpeed.Timestep : w.Timestep)));
130 				countdownLabel.GetText = () => countdown.Update(ticksRemaining);
131 			}
132 		}
133 
ITick.Tick(Actor self)134 		void ITick.Tick(Actor self)
135 		{
136 			if (TimeLimit <= 0)
137 				return;
138 
139 			var ticksPerSecond = 1000 / (self.World.IsReplay ? mapOptions.GameSpeed.Timestep : self.World.Timestep);
140 			ticksRemaining = TimeLimit - self.World.WorldTick;
141 
142 			if (ticksRemaining == 0)
143 			{
144 				foreach (var ntl in self.TraitsImplementing<INotifyTimeLimit>())
145 					ntl.NotifyTimerExpired(self);
146 
147 				foreach (var p in self.World.Players)
148 					foreach (var ntl in p.PlayerActor.TraitsImplementing<INotifyTimeLimit>())
149 						ntl.NotifyTimerExpired(p.PlayerActor);
150 
151 				return;
152 			}
153 
154 			if (ticksRemaining < 0 || info.SkipTimeRemainingNotifications)
155 				return;
156 
157 			foreach (var m in info.TimeLimitWarnings.Keys)
158 			{
159 				if (ticksRemaining == m * 60 * ticksPerSecond)
160 				{
161 					Game.AddSystemLine(Notification.F(m, m > 1 ? "s" : null));
162 
163 					var faction = self.World.LocalPlayer == null ? null : self.World.LocalPlayer.Faction.InternalName;
164 					Game.Sound.PlayNotification(self.World.Map.Rules, self.World.LocalPlayer, "Speech", info.TimeLimitWarnings[m], faction);
165 				}
166 			}
167 		}
168 
INotifyTimeLimit.NotifyTimerExpired(Actor self)169 		void INotifyTimeLimit.NotifyTimerExpired(Actor self)
170 		{
171 			if (countdownLabel != null)
172 				countdownLabel.GetText = () => null;
173 
174 			if (!info.SkipTimerExpiredNotification)
175 				Game.AddSystemLine("Time limit has expired.");
176 		}
177 	}
178 }
179