public void Run() { Listen(); MUDTimer.Start(); // Let there be time. Task.Factory.StartNew(DayCycle); // Let there be players. // Let there be player input. while (!Shutdown) { if (mClientQueue.Count > 0) { Console.WriteLine("A challenger has arrived!"); TcpClient client; while (mClientQueue.TryDequeue(out client) != true) { ; } // now we can create the player. There had better be a zone! Task.Factory.StartNew(() => IntroducePlayer(client)); } // being done in ProcessEvents: ProcessPlayerInput (); } }
public void DayCycle() { int hourLength = 1000 * 20; // 1 game hour = 20 seconds int dayLength = hourLength * 24; int sunrise = 6 * hourLength; // 6am = sunrise bool sunriseHappened = false; int sunset = 50; // 6pm = sunset bool sunsetHappened = false; while (!Shutdown) { Task.Delay(500).Wait(); // wait half a second, then do checks if (Math.Abs(MUDTimer.ElapsedMilliseconds - sunrise) < 50 && !sunriseHappened) { foreach (Player player in mPlayers) { if (player != null) { mudEventQueue.Enqueue(new Task( () => { player.ReceiveMessage(player.Zone.Sunrise); } )); } } sunriseHappened = true; } if (Math.Abs(MUDTimer.ElapsedMilliseconds - sunset) < 50 && !sunsetHappened) { foreach (Player player in mPlayers) { if (player != null) { mudEventQueue.Enqueue(new Task( () => { player.ReceiveMessage(player.Zone.Sunset); } )); } } sunsetHappened = true; } if (MUDTimer.ElapsedMilliseconds > dayLength) { MUDTimer.Reset(); DaysPassed++; sunsetHappened = false; sunriseHappened = false; } } }