Merge lp:~wuntvor/mafiabot/deadlines into lp:mafiabot
- deadlines
- Merge into main
Proposed by
Matthias Schröder
Status: | Merged |
---|---|
Merge reported by: | Matthias Schröder |
Merged at revision: | not available |
Proposed branch: | lp:~wuntvor/mafiabot/deadlines |
Merge into: | lp:mafiabot |
Diff against target: | None lines |
To merge this branch: | bzr merge lp:~wuntvor/mafiabot/deadlines |
Related bugs: | |
Related blueprints: |
Deadlines
(Low)
|
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Matthias Schröder | Approve | ||
Review via email: mp+6119@code.launchpad.net |
Commit message
Merged deadlines.
Description of the change
To post a comment you must log in.
Revision history for this message
Matthias Schröder (wuntvor) : | # |
review:
Approve
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'MafiaBotV2/Bot.cs' | |||
2 | --- MafiaBotV2/Bot.cs 2009-03-27 11:16:13 +0000 | |||
3 | +++ MafiaBotV2/Bot.cs 2009-05-02 09:56:08 +0000 | |||
4 | @@ -6,6 +6,7 @@ | |||
5 | 6 | using MafiaBotV2.Network; | 6 | using MafiaBotV2.Network; |
6 | 7 | using MafiaBotV2.Network.Irc; | 7 | using MafiaBotV2.Network.Irc; |
7 | 8 | using MafiaBotV2.Network.File; | 8 | using MafiaBotV2.Network.File; |
8 | 9 | using MafiaBotV2.Util; | ||
9 | 9 | 10 | ||
10 | 10 | namespace MafiaBotV2 | 11 | namespace MafiaBotV2 |
11 | 11 | { | 12 | { |
12 | @@ -59,6 +60,7 @@ | |||
13 | 59 | 60 | ||
14 | 60 | public void Run() { | 61 | public void Run() { |
15 | 61 | while (net.ProcessMessage()) { | 62 | while (net.ProcessMessage()) { |
16 | 63 | Scheduler.Instance.Execute(); | ||
17 | 62 | System.Threading.Thread.Sleep(1); | 64 | System.Threading.Thread.Sleep(1); |
18 | 63 | } | 65 | } |
19 | 64 | } | 66 | } |
20 | 65 | 67 | ||
21 | === modified file 'MafiaBotV2/MafiaBotV2.csproj' | |||
22 | --- MafiaBotV2/MafiaBotV2.csproj 2009-04-13 21:21:56 +0000 | |||
23 | +++ MafiaBotV2/MafiaBotV2.csproj 2009-05-02 10:03:48 +0000 | |||
24 | @@ -76,6 +76,7 @@ | |||
25 | 76 | <Compile Include="GameManager.cs" /> | 76 | <Compile Include="GameManager.cs" /> |
26 | 77 | <Compile Include="IGame.cs" /> | 77 | <Compile Include="IGame.cs" /> |
27 | 78 | <Compile Include="ICommand.cs" /> | 78 | <Compile Include="ICommand.cs" /> |
28 | 79 | <Compile Include="MafiaGame\Commands\DeadlineCommand.cs" /> | ||
29 | 79 | <Compile Include="MafiaGame\Commands\FactionCommand.cs" /> | 80 | <Compile Include="MafiaGame\Commands\FactionCommand.cs" /> |
30 | 80 | <Compile Include="MafiaGame\Commands\ModkillCommand.cs" /> | 81 | <Compile Include="MafiaGame\Commands\ModkillCommand.cs" /> |
31 | 81 | <Compile Include="MafiaGame\Commands\PowerCommand.cs" /> | 82 | <Compile Include="MafiaGame\Commands\PowerCommand.cs" /> |
32 | @@ -140,6 +141,7 @@ | |||
33 | 140 | <Compile Include="Util\Huffman.cs" /> | 141 | <Compile Include="Util\Huffman.cs" /> |
34 | 141 | <Compile Include="Util\ListFormatter.cs" /> | 142 | <Compile Include="Util\ListFormatter.cs" /> |
35 | 142 | <Compile Include="Util\ListShuffle.cs" /> | 143 | <Compile Include="Util\ListShuffle.cs" /> |
36 | 144 | <Compile Include="Util\Scheduler.cs" /> | ||
37 | 143 | <Compile Include="Util\Tree\ComplexSubtree.cs" /> | 145 | <Compile Include="Util\Tree\ComplexSubtree.cs" /> |
38 | 144 | <Compile Include="Util\Tree\ComplexTree.cs" /> | 146 | <Compile Include="Util\Tree\ComplexTree.cs" /> |
39 | 145 | <Compile Include="Util\Tree\ComplexTreeNode.cs" /> | 147 | <Compile Include="Util\Tree\ComplexTreeNode.cs" /> |
40 | 146 | 148 | ||
41 | === added file 'MafiaBotV2/MafiaGame/Commands/DeadlineCommand.cs' | |||
42 | --- MafiaBotV2/MafiaGame/Commands/DeadlineCommand.cs 1970-01-01 00:00:00 +0000 | |||
43 | +++ MafiaBotV2/MafiaGame/Commands/DeadlineCommand.cs 2009-05-02 14:25:01 +0000 | |||
44 | @@ -0,0 +1,47 @@ | |||
45 | 1 | using System; | ||
46 | 2 | using System.Collections.Generic; | ||
47 | 3 | using System.Linq; | ||
48 | 4 | using System.Text; | ||
49 | 5 | using MafiaBotV2.Util; | ||
50 | 6 | |||
51 | 7 | namespace MafiaBotV2.MafiaGame.Commands | ||
52 | 8 | { | ||
53 | 9 | class DeadlineCommand : ICommand | ||
54 | 10 | { | ||
55 | 11 | #region ICommand Members | ||
56 | 12 | |||
57 | 13 | public string Name { | ||
58 | 14 | get { return "deadline"; } | ||
59 | 15 | } | ||
60 | 16 | |||
61 | 17 | public bool AllowedInPublic { | ||
62 | 18 | get { return true; } | ||
63 | 19 | } | ||
64 | 20 | |||
65 | 21 | MafiaGame game; | ||
66 | 22 | |||
67 | 23 | public object Parent { | ||
68 | 24 | get { return game; } | ||
69 | 25 | } | ||
70 | 26 | |||
71 | 27 | public DeadlineCommand(MafiaGame game) { | ||
72 | 28 | this.game = game; | ||
73 | 29 | } | ||
74 | 30 | |||
75 | 31 | public string Execute(MafiaBotV2.Network.NetUser from, MafiaBotV2.Network.NetObject source, string[] args) { | ||
76 | 32 | if(args.Length == 0) { | ||
77 | 33 | return "Syntax: ##deadline <seconds>"; | ||
78 | 34 | } | ||
79 | 35 | |||
80 | 36 | if(from != game.Creator) { | ||
81 | 37 | return "Only the creator can set deadlines."; | ||
82 | 38 | } | ||
83 | 39 | |||
84 | 40 | DateTime executionTime = DateTime.Now.AddSeconds(Int32.Parse(args[0])); | ||
85 | 41 | game.SetDeadline(executionTime); | ||
86 | 42 | return null; | ||
87 | 43 | } | ||
88 | 44 | |||
89 | 45 | #endregion | ||
90 | 46 | } | ||
91 | 47 | } | ||
92 | 0 | 48 | ||
93 | === modified file 'MafiaBotV2/MafiaGame/MafiaGame.cs' | |||
94 | --- MafiaBotV2/MafiaGame/MafiaGame.cs 2009-04-13 21:15:12 +0000 | |||
95 | +++ MafiaBotV2/MafiaGame/MafiaGame.cs 2009-05-02 14:25:01 +0000 | |||
96 | @@ -70,12 +70,38 @@ | |||
97 | 70 | } | 70 | } |
98 | 71 | 71 | ||
99 | 72 | Channel.SetTopic(topic); | 72 | Channel.SetTopic(topic); |
100 | 73 | |||
101 | 74 | if(e.NewPhase.Type == PhaseType.Night) { | ||
102 | 75 | SetDeadline(DateTime.Now.AddSeconds(120)); | ||
103 | 76 | } | ||
104 | 73 | } | 77 | } |
105 | 74 | 78 | ||
106 | 75 | void OnGameOver(object sender, GameOverEventArgs e) { | 79 | void OnGameOver(object sender, GameOverEventArgs e) { |
107 | 76 | Games.Destroy(this); | 80 | Games.Destroy(this); |
108 | 77 | } | 81 | } |
109 | 78 | 82 | ||
110 | 83 | public void SetDeadline(DateTime executionTime) { | ||
111 | 84 | Scheduler.Instance.QueueTask(this, executionTime, new ScheduledTaskHandler(ExecuteTask)); | ||
112 | 85 | |||
113 | 86 | TimeSpan duration = executionTime - DateTime.Now; | ||
114 | 87 | if (duration.TotalSeconds > 10) { | ||
115 | 88 | Scheduler.Instance.QueueTask(this, executionTime.Subtract(new TimeSpan(0, 0, 10)), new ScheduledTaskHandler(Warn), "10"); | ||
116 | 89 | if (duration.TotalSeconds > 60) { | ||
117 | 90 | Scheduler.Instance.QueueTask(this, executionTime.Subtract(new TimeSpan(0, 1, 0)), new ScheduledTaskHandler(Warn), "60"); | ||
118 | 91 | } | ||
119 | 92 | } | ||
120 | 93 | |||
121 | 94 | Channel.SendMessage("Deadline set in " + duration.Minutes + ":" + duration.Seconds.ToString("00")); | ||
122 | 95 | } | ||
123 | 96 | |||
124 | 97 | private void Warn(Scheduler scheduler, object[] args) { | ||
125 | 98 | this.Channel.SendMessage("Deadline in " + args[0] + " seconds."); | ||
126 | 99 | } | ||
127 | 100 | |||
128 | 101 | private void ExecuteTask(Scheduler scheduler, object[] args) { | ||
129 | 102 | this.Village.Phase.End(); | ||
130 | 103 | } | ||
131 | 104 | |||
132 | 79 | public override void Start() { | 105 | public override void Start() { |
133 | 80 | if (Players.Any(p => p.Name.ToLower() == "nolynch")) { | 106 | if (Players.Any(p => p.Name.ToLower() == "nolynch")) { |
134 | 81 | this.RemoveUser(Players.Find(p => p.Name.ToLower() == "nolynch")); | 107 | this.RemoveUser(Players.Find(p => p.Name.ToLower() == "nolynch")); |
135 | @@ -138,6 +164,7 @@ | |||
136 | 138 | if (State != GameState.Forming) { | 164 | if (State != GameState.Forming) { |
137 | 139 | ShowRoles(); | 165 | ShowRoles(); |
138 | 140 | } | 166 | } |
139 | 167 | Scheduler.Instance.RemoveAll(this); | ||
140 | 141 | 168 | ||
141 | 142 | base.Destroy(); | 169 | base.Destroy(); |
142 | 143 | } | 170 | } |
143 | @@ -164,6 +191,7 @@ | |||
144 | 164 | if(user == Creator) { | 191 | if(user == Creator) { |
145 | 165 | this.Creator.Commands.Add(new Commands.ReplaceCommand(this)); | 192 | this.Creator.Commands.Add(new Commands.ReplaceCommand(this)); |
146 | 166 | this.Creator.Commands.Add(new Commands.ModkillCommand(this)); | 193 | this.Creator.Commands.Add(new Commands.ModkillCommand(this)); |
147 | 194 | this.Creator.Commands.Add(new Commands.DeadlineCommand(this)); | ||
148 | 167 | } | 195 | } |
149 | 168 | break; | 196 | break; |
150 | 169 | } | 197 | } |
151 | 170 | 198 | ||
152 | === modified file 'MafiaBotV2/Network/NetObject.cs' | |||
153 | --- MafiaBotV2/Network/NetObject.cs 2009-05-01 18:53:35 +0000 | |||
154 | +++ MafiaBotV2/Network/NetObject.cs 2009-05-02 14:25:01 +0000 | |||
155 | @@ -29,7 +29,7 @@ | |||
156 | 29 | } | 29 | } |
157 | 30 | 30 | ||
158 | 31 | protected virtual string CommandPrefix { | 31 | protected virtual string CommandPrefix { |
160 | 32 | get { return "++"; } | 32 | get { return "##"; } |
161 | 33 | } | 33 | } |
162 | 34 | 34 | ||
163 | 35 | public abstract void SendMessage(string text); | 35 | public abstract void SendMessage(string text); |
164 | 36 | 36 | ||
165 | === added file 'MafiaBotV2/Util/Scheduler.cs' | |||
166 | --- MafiaBotV2/Util/Scheduler.cs 1970-01-01 00:00:00 +0000 | |||
167 | +++ MafiaBotV2/Util/Scheduler.cs 2009-05-02 14:25:01 +0000 | |||
168 | @@ -0,0 +1,88 @@ | |||
169 | 1 | using System; | ||
170 | 2 | using System.Collections.Generic; | ||
171 | 3 | using System.Text; | ||
172 | 4 | |||
173 | 5 | namespace MafiaBotV2.Util | ||
174 | 6 | { | ||
175 | 7 | delegate void ScheduledTaskHandler(Scheduler sender, object[] args); | ||
176 | 8 | |||
177 | 9 | class Scheduler | ||
178 | 10 | { | ||
179 | 11 | List<Task> tasks; | ||
180 | 12 | |||
181 | 13 | private Scheduler() { | ||
182 | 14 | tasks = new List<Task>(); | ||
183 | 15 | } | ||
184 | 16 | |||
185 | 17 | public void Execute() { | ||
186 | 18 | List<Task> readyTasks = tasks.FindAll(task => DateTime.Now > task.Time); | ||
187 | 19 | foreach(Task task in readyTasks) { | ||
188 | 20 | task.Handler.Invoke(this, task.Args); | ||
189 | 21 | } | ||
190 | 22 | tasks.RemoveAll(task => readyTasks.Contains(task)); | ||
191 | 23 | |||
192 | 24 | } | ||
193 | 25 | |||
194 | 26 | public void QueueTask(DateTime time, ScheduledTaskHandler task) { | ||
195 | 27 | QueueTask(null, time, task); | ||
196 | 28 | } | ||
197 | 29 | |||
198 | 30 | public void QueueTask(object key, DateTime time, ScheduledTaskHandler task, params object[] args) { | ||
199 | 31 | Task item = new Task(key, time, task, args); | ||
200 | 32 | tasks.Add(item); | ||
201 | 33 | } | ||
202 | 34 | |||
203 | 35 | public void RemoveAll(object key) { | ||
204 | 36 | tasks.RemoveAll(task => task.Key == key); | ||
205 | 37 | } | ||
206 | 38 | |||
207 | 39 | public void Clear() { | ||
208 | 40 | tasks.Clear(); | ||
209 | 41 | } | ||
210 | 42 | |||
211 | 43 | #region Scheduler Singleton | ||
212 | 44 | public static Scheduler Instance { | ||
213 | 45 | get { | ||
214 | 46 | return NestedScheduler.instance; | ||
215 | 47 | } | ||
216 | 48 | } | ||
217 | 49 | |||
218 | 50 | class NestedScheduler | ||
219 | 51 | { | ||
220 | 52 | // Explicit static constructor to tell C# compiler | ||
221 | 53 | // not to mark type as beforefieldinit | ||
222 | 54 | static NestedScheduler() { | ||
223 | 55 | } | ||
224 | 56 | |||
225 | 57 | internal static readonly Scheduler instance = new Scheduler(); | ||
226 | 58 | } | ||
227 | 59 | #endregion | ||
228 | 60 | |||
229 | 61 | class Task | ||
230 | 62 | { | ||
231 | 63 | DateTime time; | ||
232 | 64 | public System.DateTime Time { | ||
233 | 65 | get { return time; } | ||
234 | 66 | } | ||
235 | 67 | ScheduledTaskHandler handler; | ||
236 | 68 | public MafiaBotV2.Util.ScheduledTaskHandler Handler { | ||
237 | 69 | get { return handler; } | ||
238 | 70 | } | ||
239 | 71 | object[] args; | ||
240 | 72 | public object[] Args { | ||
241 | 73 | get { return args; } | ||
242 | 74 | } | ||
243 | 75 | object key; | ||
244 | 76 | public object Key { | ||
245 | 77 | get { return key; } | ||
246 | 78 | } | ||
247 | 79 | |||
248 | 80 | public Task(object key, DateTime time, ScheduledTaskHandler task, object[] args) { | ||
249 | 81 | this.key = key; | ||
250 | 82 | this.time = time; | ||
251 | 83 | this.handler = task; | ||
252 | 84 | this.args = args; | ||
253 | 85 | } | ||
254 | 86 | } | ||
255 | 87 | } | ||
256 | 88 | } | ||
257 | 0 | 89 | ||
258 | === modified file 'MafiaRun/input/awesome.txt' | |||
259 | --- MafiaRun/input/awesome.txt 2009-04-13 13:55:29 +0000 | |||
260 | +++ MafiaRun/input/awesome.txt 2009-05-02 14:25:01 +0000 | |||
261 | @@ -4,5 +4,6 @@ | |||
262 | 4 | A:join #mafia_test | 4 | A:join #mafia_test |
263 | 5 | 0:say #mafia_test ##variant awesome | 5 | 0:say #mafia_test ##variant awesome |
264 | 6 | 0:say #mafia_test ##start | 6 | 0:say #mafia_test ##start |
265 | 7 | 0:say #mafia_test ##deadline 0 | ||
266 | 7 | 1:say #mafia_test ##shoot bot2 | 8 | 1:say #mafia_test ##shoot bot2 |
267 | 8 | 3:say #mafia_test ##shoot bot6 | 9 | 3:say #mafia_test ##shoot bot6 |
268 | 9 | \ No newline at end of file | 10 | \ No newline at end of file |