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
=== modified file 'MafiaBotV2/Bot.cs'
--- MafiaBotV2/Bot.cs 2009-03-27 11:16:13 +0000
+++ MafiaBotV2/Bot.cs 2009-05-02 09:56:08 +0000
@@ -6,6 +6,7 @@
6using MafiaBotV2.Network;6using MafiaBotV2.Network;
7using MafiaBotV2.Network.Irc;7using MafiaBotV2.Network.Irc;
8using MafiaBotV2.Network.File;8using MafiaBotV2.Network.File;
9using MafiaBotV2.Util;
910
10namespace MafiaBotV211namespace MafiaBotV2
11{12{
@@ -59,6 +60,7 @@
5960
60 public void Run() {61 public void Run() {
61 while (net.ProcessMessage()) {62 while (net.ProcessMessage()) {
63 Scheduler.Instance.Execute();
62 System.Threading.Thread.Sleep(1);64 System.Threading.Thread.Sleep(1);
63 }65 }
64 }66 }
6567
=== modified file 'MafiaBotV2/MafiaBotV2.csproj'
--- MafiaBotV2/MafiaBotV2.csproj 2009-04-13 21:21:56 +0000
+++ MafiaBotV2/MafiaBotV2.csproj 2009-05-02 10:03:48 +0000
@@ -76,6 +76,7 @@
76 <Compile Include="GameManager.cs" />76 <Compile Include="GameManager.cs" />
77 <Compile Include="IGame.cs" />77 <Compile Include="IGame.cs" />
78 <Compile Include="ICommand.cs" />78 <Compile Include="ICommand.cs" />
79 <Compile Include="MafiaGame\Commands\DeadlineCommand.cs" />
79 <Compile Include="MafiaGame\Commands\FactionCommand.cs" />80 <Compile Include="MafiaGame\Commands\FactionCommand.cs" />
80 <Compile Include="MafiaGame\Commands\ModkillCommand.cs" />81 <Compile Include="MafiaGame\Commands\ModkillCommand.cs" />
81 <Compile Include="MafiaGame\Commands\PowerCommand.cs" />82 <Compile Include="MafiaGame\Commands\PowerCommand.cs" />
@@ -140,6 +141,7 @@
140 <Compile Include="Util\Huffman.cs" />141 <Compile Include="Util\Huffman.cs" />
141 <Compile Include="Util\ListFormatter.cs" />142 <Compile Include="Util\ListFormatter.cs" />
142 <Compile Include="Util\ListShuffle.cs" />143 <Compile Include="Util\ListShuffle.cs" />
144 <Compile Include="Util\Scheduler.cs" />
143 <Compile Include="Util\Tree\ComplexSubtree.cs" />145 <Compile Include="Util\Tree\ComplexSubtree.cs" />
144 <Compile Include="Util\Tree\ComplexTree.cs" />146 <Compile Include="Util\Tree\ComplexTree.cs" />
145 <Compile Include="Util\Tree\ComplexTreeNode.cs" />147 <Compile Include="Util\Tree\ComplexTreeNode.cs" />
146148
=== added file 'MafiaBotV2/MafiaGame/Commands/DeadlineCommand.cs'
--- MafiaBotV2/MafiaGame/Commands/DeadlineCommand.cs 1970-01-01 00:00:00 +0000
+++ MafiaBotV2/MafiaGame/Commands/DeadlineCommand.cs 2009-05-02 14:25:01 +0000
@@ -0,0 +1,47 @@
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using MafiaBotV2.Util;
6
7namespace MafiaBotV2.MafiaGame.Commands
8{
9 class DeadlineCommand : ICommand
10 {
11 #region ICommand Members
12
13 public string Name {
14 get { return "deadline"; }
15 }
16
17 public bool AllowedInPublic {
18 get { return true; }
19 }
20
21 MafiaGame game;
22
23 public object Parent {
24 get { return game; }
25 }
26
27 public DeadlineCommand(MafiaGame game) {
28 this.game = game;
29 }
30
31 public string Execute(MafiaBotV2.Network.NetUser from, MafiaBotV2.Network.NetObject source, string[] args) {
32 if(args.Length == 0) {
33 return "Syntax: ##deadline <seconds>";
34 }
35
36 if(from != game.Creator) {
37 return "Only the creator can set deadlines.";
38 }
39
40 DateTime executionTime = DateTime.Now.AddSeconds(Int32.Parse(args[0]));
41 game.SetDeadline(executionTime);
42 return null;
43 }
44
45 #endregion
46 }
47}
048
=== modified file 'MafiaBotV2/MafiaGame/MafiaGame.cs'
--- MafiaBotV2/MafiaGame/MafiaGame.cs 2009-04-13 21:15:12 +0000
+++ MafiaBotV2/MafiaGame/MafiaGame.cs 2009-05-02 14:25:01 +0000
@@ -70,12 +70,38 @@
70 }70 }
71 71
72 Channel.SetTopic(topic);72 Channel.SetTopic(topic);
73
74 if(e.NewPhase.Type == PhaseType.Night) {
75 SetDeadline(DateTime.Now.AddSeconds(120));
76 }
73 }77 }
7478
75 void OnGameOver(object sender, GameOverEventArgs e) {79 void OnGameOver(object sender, GameOverEventArgs e) {
76 Games.Destroy(this);80 Games.Destroy(this);
77 }81 }
7882
83 public void SetDeadline(DateTime executionTime) {
84 Scheduler.Instance.QueueTask(this, executionTime, new ScheduledTaskHandler(ExecuteTask));
85
86 TimeSpan duration = executionTime - DateTime.Now;
87 if (duration.TotalSeconds > 10) {
88 Scheduler.Instance.QueueTask(this, executionTime.Subtract(new TimeSpan(0, 0, 10)), new ScheduledTaskHandler(Warn), "10");
89 if (duration.TotalSeconds > 60) {
90 Scheduler.Instance.QueueTask(this, executionTime.Subtract(new TimeSpan(0, 1, 0)), new ScheduledTaskHandler(Warn), "60");
91 }
92 }
93
94 Channel.SendMessage("Deadline set in " + duration.Minutes + ":" + duration.Seconds.ToString("00"));
95 }
96
97 private void Warn(Scheduler scheduler, object[] args) {
98 this.Channel.SendMessage("Deadline in " + args[0] + " seconds.");
99 }
100
101 private void ExecuteTask(Scheduler scheduler, object[] args) {
102 this.Village.Phase.End();
103 }
104
79 public override void Start() {105 public override void Start() {
80 if (Players.Any(p => p.Name.ToLower() == "nolynch")) {106 if (Players.Any(p => p.Name.ToLower() == "nolynch")) {
81 this.RemoveUser(Players.Find(p => p.Name.ToLower() == "nolynch"));107 this.RemoveUser(Players.Find(p => p.Name.ToLower() == "nolynch"));
@@ -138,6 +164,7 @@
138 if (State != GameState.Forming) {164 if (State != GameState.Forming) {
139 ShowRoles();165 ShowRoles();
140 }166 }
167 Scheduler.Instance.RemoveAll(this);
141168
142 base.Destroy();169 base.Destroy();
143 }170 }
@@ -164,6 +191,7 @@
164 if(user == Creator) {191 if(user == Creator) {
165 this.Creator.Commands.Add(new Commands.ReplaceCommand(this));192 this.Creator.Commands.Add(new Commands.ReplaceCommand(this));
166 this.Creator.Commands.Add(new Commands.ModkillCommand(this));193 this.Creator.Commands.Add(new Commands.ModkillCommand(this));
194 this.Creator.Commands.Add(new Commands.DeadlineCommand(this));
167 }195 }
168 break;196 break;
169 }197 }
170198
=== modified file 'MafiaBotV2/Network/NetObject.cs'
--- MafiaBotV2/Network/NetObject.cs 2009-05-01 18:53:35 +0000
+++ MafiaBotV2/Network/NetObject.cs 2009-05-02 14:25:01 +0000
@@ -29,7 +29,7 @@
29 }29 }
3030
31 protected virtual string CommandPrefix {31 protected virtual string CommandPrefix {
32 get { return "++"; }32 get { return "##"; }
33 }33 }
3434
35 public abstract void SendMessage(string text);35 public abstract void SendMessage(string text);
3636
=== added file 'MafiaBotV2/Util/Scheduler.cs'
--- MafiaBotV2/Util/Scheduler.cs 1970-01-01 00:00:00 +0000
+++ MafiaBotV2/Util/Scheduler.cs 2009-05-02 14:25:01 +0000
@@ -0,0 +1,88 @@
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace MafiaBotV2.Util
6{
7 delegate void ScheduledTaskHandler(Scheduler sender, object[] args);
8
9 class Scheduler
10 {
11 List<Task> tasks;
12
13 private Scheduler() {
14 tasks = new List<Task>();
15 }
16
17 public void Execute() {
18 List<Task> readyTasks = tasks.FindAll(task => DateTime.Now > task.Time);
19 foreach(Task task in readyTasks) {
20 task.Handler.Invoke(this, task.Args);
21 }
22 tasks.RemoveAll(task => readyTasks.Contains(task));
23
24 }
25
26 public void QueueTask(DateTime time, ScheduledTaskHandler task) {
27 QueueTask(null, time, task);
28 }
29
30 public void QueueTask(object key, DateTime time, ScheduledTaskHandler task, params object[] args) {
31 Task item = new Task(key, time, task, args);
32 tasks.Add(item);
33 }
34
35 public void RemoveAll(object key) {
36 tasks.RemoveAll(task => task.Key == key);
37 }
38
39 public void Clear() {
40 tasks.Clear();
41 }
42
43 #region Scheduler Singleton
44 public static Scheduler Instance {
45 get {
46 return NestedScheduler.instance;
47 }
48 }
49
50 class NestedScheduler
51 {
52 // Explicit static constructor to tell C# compiler
53 // not to mark type as beforefieldinit
54 static NestedScheduler() {
55 }
56
57 internal static readonly Scheduler instance = new Scheduler();
58 }
59 #endregion
60
61 class Task
62 {
63 DateTime time;
64 public System.DateTime Time {
65 get { return time; }
66 }
67 ScheduledTaskHandler handler;
68 public MafiaBotV2.Util.ScheduledTaskHandler Handler {
69 get { return handler; }
70 }
71 object[] args;
72 public object[] Args {
73 get { return args; }
74 }
75 object key;
76 public object Key {
77 get { return key; }
78 }
79
80 public Task(object key, DateTime time, ScheduledTaskHandler task, object[] args) {
81 this.key = key;
82 this.time = time;
83 this.handler = task;
84 this.args = args;
85 }
86 }
87 }
88}
089
=== modified file 'MafiaRun/input/awesome.txt'
--- MafiaRun/input/awesome.txt 2009-04-13 13:55:29 +0000
+++ MafiaRun/input/awesome.txt 2009-05-02 14:25:01 +0000
@@ -4,5 +4,6 @@
4A:join #mafia_test4A:join #mafia_test
50:say #mafia_test ##variant awesome50:say #mafia_test ##variant awesome
60:say #mafia_test ##start60:say #mafia_test ##start
70:say #mafia_test ##deadline 0
71:say #mafia_test ##shoot bot281:say #mafia_test ##shoot bot2
83:say #mafia_test ##shoot bot693:say #mafia_test ##shoot bot6
9\ No newline at end of file10\ No newline at end of file

Subscribers

People subscribed via source and target branches

to all changes: