Example #1
0
        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();
                });
            }
        }