public void WorkerThread() { bool WhetherDataIsSentToStartGame = false; bool WhetherDataIsSentToFinishGame = false; DateTime timeForSendingLeftTime = DateTime.Now.AddSeconds(10); while (currentStatus == status.on) { Thread.Sleep(30); CheckClients(); if (dataOfThisGame.phaseOfGame == phase.waiting || dataOfThisGame.phaseOfGame == phase.game) { if (DateTime.Compare(DateTime.Now, timeForSendingLeftTime) > 0) { SendTimeLeft(); timeForSendingLeftTime = timeForSendingLeftTime.AddSeconds(10); } } if (DateTime.Now < dataOfThisGame.TimeOfEndingPhaseGame && DateTime.Now > dataOfThisGame.TimeOfEndingThisWaiting && !WhetherDataIsSentToStartGame) { dataOfThisGame.StartGame(); for (int i = 1; i <= ClientCommand.nextID; i++) { if (clients.ContainsKey(i) && clients[i].CurrentStatus == status.on) { clients[i].SendNewCommand(dataOfThisGame.FormCommandOfMapSize() as BaseCommand); //Console.WriteLine(i + " клиент получил сообщение1 " + DateTime.Now); clients[i].SendNewCommand(dataOfThisGame.FormCommandOfPlayerCoords(listOfPlayersAndTheirNickname[i]) as BaseCommand); //Console.WriteLine(i + " клиент получил сообщение2 " + DateTime.Now); clients[i].SendNewCommand(dataOfThisGame.FormCommandOfVisibleObjects(listOfPlayersAndTheirNickname[i]) as BaseCommand); //Console.WriteLine(i + " клиент получил сообщение3 " + DateTime.Now); clients[i].SendNewCommand(dataOfThisGame.FormCommandOfVisiblePlayers(listOfPlayersAndTheirNickname[i]) as BaseCommand); //Console.WriteLine(i + " клиент получил сообщение4 " + DateTime.Now); } } WhetherDataIsSentToStartGame = true; } if ((DateTime.Now > dataOfThisGame.TimeOfEndingPhaseGame && DateTime.Now < dataOfThisGame.TimeOfEndingPhaseResult || dataOfThisGame.phaseOfGame == phase.result) && !WhetherDataIsSentToFinishGame) { dataOfThisGame.FinishGame(); for (int i = 1; i < ClientCommand.nextID; i++) { if (clients.ContainsKey(i) && clients[i].CurrentStatus == status.on) { clients[i].SendNewCommand(dataOfThisGame.FormCommandOfGameOver(listOfPlayersAndTheirNickname[i]) as BaseCommand); } } WhetherDataIsSentToFinishGame = true; } if (DateTime.Now > dataOfThisGame.TimeOfEndingPhaseResult) { FinalizationWorkingServer(); //InitializationServer(); } } }
public void WorkerThread() { Thread workerThread = new Thread(delegate() { bool WhetherDataIsSentToStartGame = true; bool WhetherDataIsSentToFinishGame = true; DateTime timeForSendingLeftTime = DateTime.Now.AddSeconds(10); while (currentStatus == status.on) { Thread.Sleep(30); CheckClients(); if (DateTime.Now < dataOfThisGame.TimeOfEndingThisWaiting) { if (DateTime.Compare(DateTime.Now, timeForSendingLeftTime) > 0) { SendTimeLeft(); timeForSendingLeftTime = timeForSendingLeftTime.AddSeconds(10); } } if (DateTime.Now < dataOfThisGame.TimeOfEndingPhaseGame && DateTime.Now > dataOfThisGame.TimeOfEndingThisWaiting) { if (WhetherDataIsSentToStartGame) { dataOfThisGame.StartGame(); for (int i = 1; i <= ClientCommand.nextID; i++) { if (clients.ContainsKey(i) && clients[i].CurrentStatus == status.on) { clients[i].SendNewCommand(dataOfThisGame.FormCommandOfMapSize() as BaseCommand); clients[i].SendNewCommand(dataOfThisGame.FormCommandOfPlayerCoords(listOfPlayersAndTheirNickname[i]) as BaseCommand); clients[i].SendNewCommand(dataOfThisGame.FormCommandOfVisibleObjects(i - 1) as BaseCommand); clients[i].SendNewCommand(dataOfThisGame.FormCommandOfVisiblePlayers(listOfPlayersAndTheirNickname[i]) as BaseCommand); Thread.Sleep(70); } } WhetherDataIsSentToStartGame = false; } if (DateTime.Compare(DateTime.Now, timeForSendingLeftTime) > 0) { SendTimeLeft(); timeForSendingLeftTime = timeForSendingLeftTime.AddSeconds(10); } } if (DateTime.Now > dataOfThisGame.TimeOfEndingPhaseGame && DateTime.Now < dataOfThisGame.TimeOfEndingPhaseResult) { // if (DateTime.Compare(DateTime.Now, timeForSendingLeftTime) > 0) if (WhetherDataIsSentToFinishGame) { dataOfThisGame.FinishGame(); for (int i = 1; i <= ClientCommand.nextID; i++) { if (clients.ContainsKey(i) && clients[i].CurrentStatus == status.on) { clients[i].SendNewCommand(dataOfThisGame.FormCommandOfGameOver() as BaseCommand); Thread.Sleep(70); } } WhetherDataIsSentToFinishGame = false; } //SendTimeLeft(); //timeForSendingLeftTime = timeForSendingLeftTime.AddSeconds(10); } if (DateTime.Now > dataOfThisGame.TimeOfEndingPhaseResult) { // FinalizationWorkingServer(); //InitializationServer(); } } }); workerThread.Start(); }