Merge lp:~wuntvor/mafiabot/deadlines into lp:mafiabot

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
Reviewer Review Type Date Requested Status
Matthias Schröder Approve
Review via email: mp+6119@code.launchpad.net

Commit message

Merged deadlines.

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 using MafiaBotV2.Network;
6 using MafiaBotV2.Network.Irc;
7 using MafiaBotV2.Network.File;
8+using MafiaBotV2.Util;
9
10 namespace MafiaBotV2
11 {
12@@ -59,6 +60,7 @@
13
14 public void Run() {
15 while (net.ProcessMessage()) {
16+ Scheduler.Instance.Execute();
17 System.Threading.Thread.Sleep(1);
18 }
19 }
20
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 <Compile Include="GameManager.cs" />
26 <Compile Include="IGame.cs" />
27 <Compile Include="ICommand.cs" />
28+ <Compile Include="MafiaGame\Commands\DeadlineCommand.cs" />
29 <Compile Include="MafiaGame\Commands\FactionCommand.cs" />
30 <Compile Include="MafiaGame\Commands\ModkillCommand.cs" />
31 <Compile Include="MafiaGame\Commands\PowerCommand.cs" />
32@@ -140,6 +141,7 @@
33 <Compile Include="Util\Huffman.cs" />
34 <Compile Include="Util\ListFormatter.cs" />
35 <Compile Include="Util\ListShuffle.cs" />
36+ <Compile Include="Util\Scheduler.cs" />
37 <Compile Include="Util\Tree\ComplexSubtree.cs" />
38 <Compile Include="Util\Tree\ComplexTree.cs" />
39 <Compile Include="Util\Tree\ComplexTreeNode.cs" />
40
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+using System;
46+using System.Collections.Generic;
47+using System.Linq;
48+using System.Text;
49+using MafiaBotV2.Util;
50+
51+namespace MafiaBotV2.MafiaGame.Commands
52+{
53+ class DeadlineCommand : ICommand
54+ {
55+ #region ICommand Members
56+
57+ public string Name {
58+ get { return "deadline"; }
59+ }
60+
61+ public bool AllowedInPublic {
62+ get { return true; }
63+ }
64+
65+ MafiaGame game;
66+
67+ public object Parent {
68+ get { return game; }
69+ }
70+
71+ public DeadlineCommand(MafiaGame game) {
72+ this.game = game;
73+ }
74+
75+ public string Execute(MafiaBotV2.Network.NetUser from, MafiaBotV2.Network.NetObject source, string[] args) {
76+ if(args.Length == 0) {
77+ return "Syntax: ##deadline <seconds>";
78+ }
79+
80+ if(from != game.Creator) {
81+ return "Only the creator can set deadlines.";
82+ }
83+
84+ DateTime executionTime = DateTime.Now.AddSeconds(Int32.Parse(args[0]));
85+ game.SetDeadline(executionTime);
86+ return null;
87+ }
88+
89+ #endregion
90+ }
91+}
92
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 }
98
99 Channel.SetTopic(topic);
100+
101+ if(e.NewPhase.Type == PhaseType.Night) {
102+ SetDeadline(DateTime.Now.AddSeconds(120));
103+ }
104 }
105
106 void OnGameOver(object sender, GameOverEventArgs e) {
107 Games.Destroy(this);
108 }
109
110+ public void SetDeadline(DateTime executionTime) {
111+ Scheduler.Instance.QueueTask(this, executionTime, new ScheduledTaskHandler(ExecuteTask));
112+
113+ TimeSpan duration = executionTime - DateTime.Now;
114+ if (duration.TotalSeconds > 10) {
115+ Scheduler.Instance.QueueTask(this, executionTime.Subtract(new TimeSpan(0, 0, 10)), new ScheduledTaskHandler(Warn), "10");
116+ if (duration.TotalSeconds > 60) {
117+ Scheduler.Instance.QueueTask(this, executionTime.Subtract(new TimeSpan(0, 1, 0)), new ScheduledTaskHandler(Warn), "60");
118+ }
119+ }
120+
121+ Channel.SendMessage("Deadline set in " + duration.Minutes + ":" + duration.Seconds.ToString("00"));
122+ }
123+
124+ private void Warn(Scheduler scheduler, object[] args) {
125+ this.Channel.SendMessage("Deadline in " + args[0] + " seconds.");
126+ }
127+
128+ private void ExecuteTask(Scheduler scheduler, object[] args) {
129+ this.Village.Phase.End();
130+ }
131+
132 public override void Start() {
133 if (Players.Any(p => p.Name.ToLower() == "nolynch")) {
134 this.RemoveUser(Players.Find(p => p.Name.ToLower() == "nolynch"));
135@@ -138,6 +164,7 @@
136 if (State != GameState.Forming) {
137 ShowRoles();
138 }
139+ Scheduler.Instance.RemoveAll(this);
140
141 base.Destroy();
142 }
143@@ -164,6 +191,7 @@
144 if(user == Creator) {
145 this.Creator.Commands.Add(new Commands.ReplaceCommand(this));
146 this.Creator.Commands.Add(new Commands.ModkillCommand(this));
147+ this.Creator.Commands.Add(new Commands.DeadlineCommand(this));
148 }
149 break;
150 }
151
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 }
157
158 protected virtual string CommandPrefix {
159- get { return "++"; }
160+ get { return "##"; }
161 }
162
163 public abstract void SendMessage(string text);
164
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+using System;
170+using System.Collections.Generic;
171+using System.Text;
172+
173+namespace MafiaBotV2.Util
174+{
175+ delegate void ScheduledTaskHandler(Scheduler sender, object[] args);
176+
177+ class Scheduler
178+ {
179+ List<Task> tasks;
180+
181+ private Scheduler() {
182+ tasks = new List<Task>();
183+ }
184+
185+ public void Execute() {
186+ List<Task> readyTasks = tasks.FindAll(task => DateTime.Now > task.Time);
187+ foreach(Task task in readyTasks) {
188+ task.Handler.Invoke(this, task.Args);
189+ }
190+ tasks.RemoveAll(task => readyTasks.Contains(task));
191+
192+ }
193+
194+ public void QueueTask(DateTime time, ScheduledTaskHandler task) {
195+ QueueTask(null, time, task);
196+ }
197+
198+ public void QueueTask(object key, DateTime time, ScheduledTaskHandler task, params object[] args) {
199+ Task item = new Task(key, time, task, args);
200+ tasks.Add(item);
201+ }
202+
203+ public void RemoveAll(object key) {
204+ tasks.RemoveAll(task => task.Key == key);
205+ }
206+
207+ public void Clear() {
208+ tasks.Clear();
209+ }
210+
211+ #region Scheduler Singleton
212+ public static Scheduler Instance {
213+ get {
214+ return NestedScheduler.instance;
215+ }
216+ }
217+
218+ class NestedScheduler
219+ {
220+ // Explicit static constructor to tell C# compiler
221+ // not to mark type as beforefieldinit
222+ static NestedScheduler() {
223+ }
224+
225+ internal static readonly Scheduler instance = new Scheduler();
226+ }
227+ #endregion
228+
229+ class Task
230+ {
231+ DateTime time;
232+ public System.DateTime Time {
233+ get { return time; }
234+ }
235+ ScheduledTaskHandler handler;
236+ public MafiaBotV2.Util.ScheduledTaskHandler Handler {
237+ get { return handler; }
238+ }
239+ object[] args;
240+ public object[] Args {
241+ get { return args; }
242+ }
243+ object key;
244+ public object Key {
245+ get { return key; }
246+ }
247+
248+ public Task(object key, DateTime time, ScheduledTaskHandler task, object[] args) {
249+ this.key = key;
250+ this.time = time;
251+ this.handler = task;
252+ this.args = args;
253+ }
254+ }
255+ }
256+}
257
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 A:join #mafia_test
263 0:say #mafia_test ##variant awesome
264 0:say #mafia_test ##start
265+0:say #mafia_test ##deadline 0
266 1:say #mafia_test ##shoot bot2
267 3:say #mafia_test ##shoot bot6
268\ No newline at end of file

Subscribers

People subscribed via source and target branches

to all changes: