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(); }); } } }
protected override void OnJoinRoomLocal(IOnlineConnection Sender, string RoomID, GameClientGroup ActiveGroup) { RoomInformations JoinedRoom = (RoomInformations)ActiveGroup.Room; List <Player> ListJoiningPlayerInfo = JoinedRoom.GetOnlinePlayer(Sender); foreach (IOnlineConnection ActivePlayer in ActiveGroup.Room.ListOnlinePlayer) { if (ActivePlayer == Sender) { continue; } ActivePlayer.Send(new PlayerJoinedScriptServer(ListJoiningPlayerInfo)); } Dictionary <string, OnlineScript> DicNewScript = OnlineHelper.GetRoomScriptsServer(JoinedRoom, Owner); if (JoinedRoom.RoomType == RoomInformations.RoomTypeMission) { MissionRoomInformations MissionRoom = (MissionRoomInformations)JoinedRoom; DicNewScript.Add(AskStartGameMissionScriptServer.ScriptName, new AskStartGameMissionScriptServer(MissionRoom, (TripleThunderClientGroup)ActiveGroup, Owner)); DicNewScript.Add(AskChangeRoomExtrasMissionScriptServer.ScriptName, new AskChangeRoomExtrasMissionScriptServer(MissionRoom)); } else if (JoinedRoom.RoomType == RoomInformations.RoomTypeBattle) { BattleRoomInformations BattleRoom = (BattleRoomInformations)JoinedRoom; DicNewScript.Add(AskStartGameMissionScriptServer.ScriptName, new AskStartGameBattleScriptServer(BattleRoom, (TripleThunderClientGroup)ActiveGroup, Owner)); DicNewScript.Add(AskChangeRoomExtrasBattleScriptServer.ScriptName, new AskChangeRoomExtrasBattleScriptServer(BattleRoom)); } Sender.AddOrReplaceScripts(DicNewScript); if (ActiveGroup.CurrentGame != null) { FightingZone CurrentGame = (FightingZone)ActiveGroup.CurrentGame; foreach (Player ActivePlayer in ListJoiningPlayerInfo) { CurrentGame.AddLocalCharacter(ActivePlayer); ActivePlayer.OnlineClient = Sender; int LayerIndex; CurrentGame.AddPlayerFromSpawn(ActivePlayer, CurrentGame.NextRobotID + (uint.MaxValue - 100), true, out LayerIndex); //Add Game Specific scripts DicNewScript = OnlineHelper.GetTripleThunderScriptsServer((TripleThunderClientGroup)ActiveGroup, ActivePlayer); Sender.AddOrReplaceScripts(DicNewScript); foreach (IOnlineConnection OtherPlayer in ActiveGroup.Room.ListOnlinePlayer) { if (OtherPlayer == Sender) { continue; } OtherPlayer.Send(new CreatePlayerScriptServer(ActivePlayer, LayerIndex, false)); } } } Sender.Send(new JoinRoomLocalScriptServer(RoomID, JoinedRoom.CurrentDifficulty, ListJoiningPlayerInfo, ActiveGroup)); }