public void UpdatePlayers() { Parallel.ForEach(ListPlayer, (ActivePlayer, loopState) => { if (!ActivePlayer.IsConnected()) { if (ActivePlayer.HasLeftServer()) { ListPlayerToRemove.Add(ActivePlayer); Database.RemovePlayer(ActivePlayer); } } else { lock (ActivePlayer.ListAsyncOnlineScript) { foreach (OnlineScript ActiveScript in ActivePlayer.ListAsyncOnlineScript) { ActiveScript.Execute(ActivePlayer); } ActivePlayer.ListAsyncOnlineScript.Clear(); } } }); while (ListPlayerToRemove.Count > 0) { ListPlayer.Remove(ListPlayerToRemove[0]); ListPlayerToRemove.RemoveAt(0); } Parallel.ForEach(DicLocalRoom.Values, (ActiveGroup, loopState) => { if (ActiveGroup.IsRunningSlow()) { //Send Room to another Server } if (ActiveGroup.CurrentGame != null) { ActiveGroup.CurrentGame.Update(DeltaTime); } for (int P = ActiveGroup.Room.ListOnlinePlayer.Count - 1; P >= 0; --P) { IOnlineConnection ActivePlayer = ActiveGroup.Room.ListOnlinePlayer[P]; if (!ActivePlayer.IsConnected()) { if (ActivePlayer.HasLeftServer()) { string PlayerID = ActiveGroup.Room.ListOnlinePlayer[P].ID; ActiveGroup.Room.RemoveOnlinePlayer(P); Database.UpdatePlayerCountInRoom(ActiveGroup.Room.RoomID, (byte)ActiveGroup.Room.ListOnlinePlayer.Count); if (ActiveGroup.Room.ListOnlinePlayer.Count == 0) { ListLocalRoomToRemove.Add(ActiveGroup.Room.RoomID); } if (ActiveGroup.CurrentGame == null) { foreach (IOnlineConnection OtherPlayer in ActiveGroup.Room.ListOnlinePlayer) { OtherPlayer.Send(new PlayerLeftScriptServer(PlayerID, 0)); } } else { ActiveGroup.CurrentGame.RemoveOnlinePlayer(PlayerID, ActivePlayer); } ActivePlayer.StopReadingScriptAsync(); } } else { lock (ActivePlayer.ListAsyncOnlineScript) { foreach (OnlineScript ActiveScript in ActivePlayer.ListAsyncOnlineScript) { ActiveScript.Execute(ActivePlayer); } ActivePlayer.ListAsyncOnlineScript.Clear(); } } } }); while (ListLocalRoomToRemove.Count > 0) { DicLocalRoom.Remove(ListLocalRoomToRemove[0]); DicAllRoom.Remove(ListLocalRoomToRemove[0]); Database.RemoveRoom(ListLocalRoomToRemove[0]); ListLocalRoomToRemove.RemoveAt(0); } foreach (GameClientGroup ActiveGroup in DicTransferingRoom.Values) { foreach (IOnlineConnection ActivePlayer in ActiveGroup.Room.ListOnlinePlayer) { lock (ActivePlayer.ListAsyncOnlineScript) { foreach (OnlineScript ActiveScript in ActivePlayer.ListAsyncOnlineScript) { ActiveScript.Execute(ActivePlayer); } ActivePlayer.ListAsyncOnlineScript.Clear(); } } if (ActiveGroup.Room.ListOnlinePlayer.Count == ActiveGroup.Room.CurrentPlayerCount && ActiveGroup.CurrentGame != null) { ListTransferingRoomToRemove.Add(ActiveGroup.Room.RoomID); } } while (ListTransferingRoomToRemove.Count > 0) { DicLocalRoom.Add(ListTransferingRoomToRemove[0], DicTransferingRoom[ListTransferingRoomToRemove[0]]); DicTransferingRoom.Remove(ListTransferingRoomToRemove[0]); ListTransferingRoomToRemove.RemoveAt(0); } //TODO: Run task in background if (DateTimeOffset.Now > NextRoomUpdateTime) { NextRoomUpdateTime = NextRoomUpdateTime.AddSeconds(10); List <IRoomInformations> ListRoomUpdates = Database.GetAllRoomUpdatesSinceLastTimeChecked(ServerVersion); if (ListRoomUpdates != null) { foreach (IRoomInformations ActiveRoomUpdate in ListRoomUpdates) { if (ActiveRoomUpdate.IsDead) { DicAllRoom.Remove(ActiveRoomUpdate.RoomID); } else { if (DicAllRoom.ContainsKey(ActiveRoomUpdate.RoomID)) { DicAllRoom[ActiveRoomUpdate.RoomID] = ActiveRoomUpdate; } else { DicAllRoom.Add(ActiveRoomUpdate.RoomID, ActiveRoomUpdate); } } } SharedWriteBuffer.ClearWriteBuffer(); SharedWriteBuffer.WriteScript(new RoomListScriptServer(this, ListRoomUpdates)); Parallel.ForEach(ListPlayer, (ActivePlayer, loopState) => { ActivePlayer.SendWriteBuffer(); }); } } }
public void UpdatePlayers() { Parallel.ForEach(ListPlayerConnection, (ActivePlayer, loopState) => { if (!ActivePlayer.IsConnected()) { if (ActivePlayer.HasLeftServer()) { ListPlayerToRemove.Add(new KeyValuePair <IOnlineConnection, string>(ActivePlayer, null)); Database.RemovePlayer(ActivePlayer); } } else { lock (ActivePlayer.ListAsyncOnlineScript) { foreach (OnlineScript ActiveScript in ActivePlayer.ListAsyncOnlineScript) { ActiveScript.Execute(ActivePlayer); } ActivePlayer.ListAsyncOnlineScript.Clear(); } } }); while (ListPlayerToRemove.Count > 0) { DicCommunicationGroup[ListPlayerToRemove[0].Value].ListGroupMember.Remove(ListPlayerToRemove[0].Key); DicPlayerInfoByName.Remove(ListPlayerToRemove[0].Key.ID); DicPlayerByID.Remove(ListPlayerToRemove[0].Key.ID); ListPlayerToRemove.RemoveAt(0); } Parallel.ForEach(DicCommunicationGroup.Values, (ActiveGroup, loopState) => { if (ActiveGroup.IsRunningSlow()) { //Send Room to another Server } for (int P = ActiveGroup.ListGroupMember.Count - 1; P >= 0; --P) { IOnlineConnection ActivePlayer = ActiveGroup.ListGroupMember[P]; if (!ActivePlayer.IsConnected()) { if (ActivePlayer.HasLeftServer()) { string PlayerID = ActiveGroup.ListGroupMember[P].ID; ActiveGroup.RemoveOnlinePlayer(P); if (ActiveGroup.ListGroupMember.Count == 0) { ListGroupToRemove.Add(ActiveGroup.GroupID); } ActivePlayer.StopReadingScriptAsync(); } } } }); while (ListGroupToRemove.Count > 0) { DicCommunicationGroup.Remove(ListGroupToRemove[0]); ListGroupToRemove.RemoveAt(0); } if (DateTimeOffset.Now > NextPlayerUpdateTime) { NextPlayerUpdateTime = NextPlayerUpdateTime.AddSeconds(10); SharedWriteBuffer.ClearWriteBuffer(); SharedWriteBuffer.WriteScript(new PlayerListScriptServer(GetPlayerNames())); Parallel.ForEach(DicCommunicationGroup["Global"].ListGroupMember, (ActiveMember, loopState) => { ActiveMember.SendWriteBuffer(); }); } }