Beispiel #1
0
        private void RefreshPlayerMenus()
        {
            IUser[] users = Game.GetActiveUsers();

            if (users == null || users.Length == 0)
            {
                return;
            }

            foreach (var u in users)
            {
                var menus = PlayerMenuList.Where(m => m.Player != null && m.Player.User != null && m.Player.User.Name == u.Name).ToList();
                if (menus.Count > 1)
                {
                    DebugLogger.DebugOnlyDialogLog("PLAYER " + u.Name + " HAD MORE THAN ONE MENU. DELETING THE OTHER MENUS");
                    menus[1].Player = null;
                    menus[1].Menu.SetText(string.Empty);
                    menus[1].Ready = false;
                }
            }

            if (PlayerList.Count == users.Length)
            {
                return;
            }
            try
            {
                int initialPlayercount = PlayerList.Count;



                if (PlayerList.Count < users.Length)
                {
                    var availableMenus = PlayerMenuList.Where(m => m.Player == null || m.Player.User == null).ToList();
                    for (int i = 0; i < users.Length; i++)
                    {
                        IUser currentUser = null;
                        currentUser = users[i];
                        if (currentUser == null)
                        {
                            continue;
                        }
                        if (currentUser.IsSpectator)
                        {
                            continue;
                        }

                        if (PlayerList.Any(p => p.User.Name.Equals(currentUser.Name)))
                        {
                            continue;
                        }

                        if (currentUser.GetPlayer() == null)
                        {
                            DebugLogger.DebugOnlyDialogLog("SPAWNING BODY FOR PLAYER " + currentUser.Name, CameraPosition);
                            var positionToSpawn = Game.GetSingleObjectByCustomId("StartSpawnPoint").GetWorldPosition();
                            var p = Game.CreatePlayer(positionToSpawn);
                            p.SetTeam(PlayerTeam.Team3);
                            p.SetUser(currentUser);
                            p.SetProfile(currentUser.GetProfile());
                            p.SetWorldPosition(positionToSpawn);
                        }

                        var player = new TPlayer(currentUser);



                        if (availableMenus.Count == 0)
                        {
                            DebugLogger.DebugOnlyDialogLog("NO AVAILABLE MENUS FOR PLAYER " + player.Name);
                            return;
                        }
                        TPlayerMenu menu = null;
                        menu = availableMenus.FirstOrDefault();
                        availableMenus.RemoveAt(availableMenus.IndexOf(menu));
                        if (menu == null)
                        {
                            return;
                        }


                        try
                        {
                            var playerFound = false;
                            if (!string.IsNullOrEmpty(OtherData))
                            {
                                // try to look for player in OtherData
                                var playerList = OtherData.Split(';');

                                for (int p = 0; p < playerList.Length; p++)
                                {
                                    string[] plData = playerList[p].Split(':');
                                    if (plData.Length == 11)
                                    {
                                        List <string> list = new List <string>(plData);
                                        list.RemoveAt(3);
                                        plData = list.ToArray();
                                    }

                                    string name = FormatName(player.Name);
                                    if (name == plData[0])
                                    {
                                        menu.SetPlayer(player);
                                        player.Level      = Math.Min(Convert.ToInt32(plData[1]), LevelList.Count - 1);
                                        player.CurrentExp = Convert.ToInt32(plData[2]);
                                        for (int u = 0; u < menu.Equipment.Count; u++)
                                        {
                                            menu.Equipment[u] = Convert.ToInt32(plData[3 + u]);
                                        }
                                        menu.ValidateEquipment();
                                        menu.Change = true;
                                        menu.Update();
                                        playerFound = true;
                                        DebugLogger.DebugOnlyDialogLog("PLAYER FOUND IN MEMORY DATA. WELCOME BACK!" + player.Name);
                                        break;
                                    }
                                }
                            }

                            if (!playerFound)
                            {
                                DebugLogger.DebugOnlyDialogLog("ASSIGNING MENU TO PLAYER " + player.Name);
                                if (ShowDebugMessages && !string.IsNullOrEmpty(OtherData))
                                {
                                    DebugLogger.DebugOnlyDialogLog("THEY ARE A NEW PLAYER! WELCOME " + player.Name + "!");
                                }
                                menu.SetPlayer(player);
                                menu.Change = true;
                                menu.Update();
                            }
                        }
                        catch (Exception e)
                        {
                            DebugLogger.DebugOnlyDialogLog(e.Message);
                            DebugLogger.DebugOnlyDialogLog(e.StackTrace);
                            DebugLogger.DebugOnlyDialogLog("COULDN'T INSTANTIATE PLAYER " + player.Name);
                        }

                        PlayerList.Add(player);
                    }
                }


                if (PlayerList.Count > users.Length)
                {
                    DebugLogger.DebugOnlyDialogLog("More players in PlayersList than active users");
                    for (int i = PlayerMenuList.Count - 1; i >= 0; i--)
                    {
                        TPlayer player = null;
                        try
                        {
                            player = PlayerList.ElementAtOrDefault(i);
                        }
                        catch (Exception)
                        {
                            DebugLogger.DebugOnlyDialogLog("PlayerList.ElementAtOrDefault(i) was out of range, i:" + (i).ToString(), CameraPosition);
                        }

                        if (player == null)
                        {
                            continue;
                        }
                        TPlayerMenu menu = null;
                        try
                        {
                            menu = PlayerMenuList.Where(m => m.Player != null && m.Player.Name != null && m.Player.Name.Equals(player.Name)).FirstOrDefault();
                        }
                        catch (Exception e)
                        {
                            DebugLogger.DebugOnlyDialogLog("PlayerMenuList.Where(m => m.Player.Name.Equals(m.Player)).FirstOrDefault()" + (i).ToString(), CameraPosition);
                            DebugLogger.DebugOnlyDialogLog(e.Message + (i).ToString(), CameraPosition);
                            DebugLogger.DebugOnlyDialogLog(e.StackTrace + (i).ToString(), CameraPosition);
                        }


                        if (menu == null)
                        {
                            DebugLogger.DebugOnlyDialogLog("MENU FOR PLAYER " + player.Name + " WAS NULL. IF HE LEFT HIS PROGRESS WILL NOT BE SAVED!!");
                            continue;
                        }

                        if (!users.Any(u => u.Name != null && u.Name.Equals(player.User.Name)) && GameState != -3 && GameState != -4)
                        {
                            DebugLogger.DebugOnlyDialogLog("REMOVING MENU FOR: " + player.User.Name + ". LEVEL " + player.Level + ". USER HAS LEFT THE MATCH");
                            var playersInMemoryCountBefore = OtherData.Split(';').Length;
                            var leavingPlayerData          = menu.Save(saveOnlyIfActive: false);
                            OtherData += leavingPlayerData;
                            var playersInMemoryCountAfter = OtherData.Split(';').Length;
                            menu.Dispose();
                            PlayerList.Remove(player);
                            if (player.Body != null)
                            {
                                player.Body.Remove();
                                DebugLogger.DebugOnlyDialogLog("REMOVED BODY FOR PLAYER " + player.User.Name);
                            }
                            else
                            {
                                DebugLogger.DebugOnlyDialogLog("COULDN'T REMOVE BODY FOR PLAYER " + player.User.Name);
                            }
                            DebugLogger.DebugOnlyDialogLog("REMOVED MENU FOR: " + player.User.Name + "SUCCESFULLY. DATA ADDED TO MEMORY PLAYERS");
                            DebugLogger.DebugOnlyDialogLog(playersInMemoryCountBefore + " LOADED IN MEMORY BEFORE AND " + playersInMemoryCountAfter + " AFTER. " + PlayerList.Count + " IN MATCH");
                            DebugLogger.DebugOnlyDialogLog("SAVED DATA FOR LEAVING PLAYER " + leavingPlayerData);
                        }
                    }
                }

                // ONLY LOAD DATA ONCE PER GAME
                if (OtherData == string.Empty)
                {
                    LoadData();
                }
            }
            catch (Exception e)
            {
                DebugLogger.DebugOnlyDialogLog(e.StackTrace, CameraPosition);
                DebugLogger.DebugOnlyDialogLog(e.Source, CameraPosition);
            }
        }