Untitled

mail@pastecode.io avatar
unknown
csharp
2 months ago
9.1 kB
3
Indexable
Never
using System.Collections.Generic;
using Controllers.Fader;
using Managers.Audio;
using Managers.MainMenu;
using Networking.Chat;
using Networking.Properties;
using Networking.Rooms;
using Photon.Pun;
using Photon.Realtime;
using Systems.Language;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using Visual.Loading;

namespace Networking.Lobby {
    public class LobbyCreateRoom : MonoBehaviourPunCallbacks {
        public static LobbyCreateRoom Room;
        [SerializeField] private TMP_Text RoomNameText;
        [SerializeField] private Button CreateRoomButton;
        [SerializeField] private Button CancelRoomCreation;
        [SerializeField] private Button CancelRoom;
        [SerializeField] private Button PlayRoom;
        [SerializeField] private Button ReadyButton;
        [SerializeField] private TMP_InputField ServerNameField;
        [SerializeField] private TMP_Text MaxPlayersText;
        [SerializeField] private TMP_Text PrivateRoomText;

        [HideInInspector] public bool Ready;
        private bool CanStartRoom;
        private bool RoomIsPrivate;
        private int PlayerCount = 4;

        private void Awake() {
            Room = this;
            SetListeners();
        }

        private void Start() {
            CheckStartRoom();
            CheckPlayerIsKingOfTheRoom();
        }
        
        private void SetListeners() {
            Room = this;
            CreateRoomButton.onClick.AddListener(() => CreateRoom());
            CancelRoomCreation.onClick.AddListener(() => MainMenuWindowManager.Window.DisableWindow("Window_LobbyCreator"));
            CancelRoom.onClick.AddListener(() => CancelAndCloseRoom());
            PlayRoom.onClick.AddListener(() => StartRoom());
            ReadyButton.onClick.AddListener(() => ToggleReady());
        }

        private void Update() {
            MaxPlayersText.text = PlayerCount.ToString("F0");
            PrivateRoomText.text = AWLanguageSystem.LangKey("DREAD_STORIES_GENERAL", RoomIsPrivate ? "DS_GENERAL_YES" : "DS_GENERAL_NO");

            if (PhotonNetwork.IsConnected) {
                if (PhotonNetwork.InRoom) {
                    RoomNameText.text = PhotonNetwork.CurrentRoom.Name;
                }

                if (ServerNameField.text == "") {
                    ServerNameField.text = PhotonNetwork.NickName + "'s Room";
                }
            }
        }

        private void CheckPlayerIsKingOfTheRoom() {
            foreach (GameObject Element in OnlyForMasterElements) {
                Element.SetActive(PhotonNetwork.IsMasterClient);
            }
        }

        private void ChangePlayerCount(int InputValue) {
            PlayerCount += InputValue;
            if (PlayerCount > 4) {
                PlayerCount = 4;
            }

            if (PlayerCount < 1) {
                PlayerCount = 1;
            }
        }

        private void ChangeRoomPrivateState() {
            RoomIsPrivate = !RoomIsPrivate;
        }

        private bool CheckRoomCanBeCreated() {
            return PhotonNetwork.IsConnected && (!string.IsNullOrEmpty(ServerNameField.text) && ServerNameField.text.Length >= 5 && PlayerCount != -1);
        }
        
        private void CreateRoom() {
            if (!CheckRoomCanBeCreated()) return;
            
            RoomOptions Options = new RoomOptions {
                MaxPlayers = (byte)PlayerCount,
                IsVisible = !RoomIsPrivate,
                PublishUserId = true,
                BroadcastPropsChangeToAll = true
            };

            PhotonNetwork.CreateRoom(ServerNameField.text, Options, TypedLobby.Default);
            LoadingCircleBar.Loader.StartLoading(AWLanguageSystem.LangKey("DREAD_STORIES_INITIALIZE", "DS_INIT_CREATING_ROOM"), FadeDuration: 0.5f);
        }

        public override void OnCreatedRoom() {
            RoomManager.Room.FetchCurrentRoomPlayers();
            ChatController.Chat.Connect();
            
            LoadingCircleBar.Loader.FinishLoading(0.25f);
            
            MainMenuWindowManager.Window.FadeElement("Window_Main", "FadeOut", 0.5f);
            MainMenuWindowManager.Window.FadeElement("Window_Lobby", "FadeOut", 0.5f);
            MainMenuWindowManager.Window.FadeElement("Window_LobbyCreator", "FadeOut", 0.5f);
            
            this.Invoke(() => MainMenuWindowManager.Window.ChangeWindowState("Window_Main", false), 0.5f);
            this.Invoke(() => MainMenuWindowManager.Window.ChangeWindowState("Window_Lobby", false), 0.5f);
            this.Invoke(() => MainMenuWindowManager.Window.ChangeWindowState("Window_LobbyCreator", false), 0.5f);
            this.Invoke(() => EnableRoomUI(), 0.5f);
            
            AudioManager.Audio.ChangeTrack(1, 0.025f, 1.0f);
        }

        private void EnableRoomUI() {
            MainMenuWindowManager.Window.ChangeWindowState("Window_Room", true);
            MainMenuWindowManager.Window.FadeElement("Window_Room", "FadeIn", 0.5f);
        }

        private void CancelAndCloseRoom() {
            PhotonNetwork.LeaveRoom();
            MainMenuWindowManager.Window.FadeElement("Window_Room", "FadeOut", 0.5f);
            
            LoadingCircleBar.Loader.FinishLoading();
            LoadingCircleBar.Loader.StartLoading(AWLanguageSystem.LangKey("DREAD_STORIES_INITIALIZE", "DS_INIT_LEAVING_ROOM"));
            this.Invoke(() => BackFromRoom(), 6.0f);
        }
        
        private void BackFromRoom() {
            MainMenuWindowManager.Window.ChangeWindowState("Window_Main", true);
            this.Invoke(() => MainMenuWindowManager.Window.FadeElement("Window_Main", "FadeIn", 0.5f), 0.5f);
            this.Invoke(() => MainMenuWindowManager.Window.ChangeWindowState("Window_Room", false), 0.5f);
            
            AudioManager.Audio.ChangeTrack(0, 0.025f, 1.0f);
        }

        private void ToggleReady() {
            Ready = !Ready;
            if (Ready) ReadyButton.transform.GetChild(0).GetComponent<TMP_Text>().text = AWLanguageSystem.LangKey("DREAD_STORIES_LOBBY", "DS_LOBBY_PLAYER_NOT_READY");
            if (!Ready) ReadyButton.transform.GetChild(0).GetComponent<TMP_Text>().text = AWLanguageSystem.LangKey("DREAD_STORIES_LOBBY", "DS_LOBBY_PLAYER_READY");
            
            PlayerProperties.Properties.PlayerCustomProps["PlayerReady"] = Ready;
            PlayerProperties.Properties.SendProperties();
        }

        private void CheckStartRoom() {
            if (PhotonNetwork.IsMasterClient) {
                var AllPlayersReady = true;
                var AllPlayers = PhotonNetwork.CurrentRoom.Players;

                if (!Ready) AllPlayersReady = false;
                
                foreach (KeyValuePair<int, Player> player in AllPlayers) {
                    if (player.Value.CustomProperties.ContainsKey("PlayerReady")) {
                        if (player.Value.CustomProperties["PlayerReady"].Equals(false)) {
                            AllPlayersReady = false;
                        }
                    }
                }
                
                CanStartRoom = AllPlayersReady;
                PlayRoom.interactable = CanStartRoom;
            }
        }
        
        private void StartRoom() {
            if (PhotonNetwork.IsMasterClient) {
                PhotonNetwork.CurrentRoom.IsOpen = false;
                PhotonNetwork.CurrentRoom.IsVisible = false;
                this.Invoke(() => PhotonNetwork.LoadLevel(1), 25.0f);
            }

            CallStartRoom();
            base.photonView.RPC("CallStartRoom", RpcTarget.AllViaServer);
        }

        [PunRPC]
        private void CallStartRoom() {
            MainMenuWindowManager.Window.FadeElement("Window_Room", "FadeOut", 0.5f);
            MainMenuWindowManager.Window.ChangeWindowState("Window_LoadingScreen", true);
            MainMenuWindowManager.Window.FadeElement("Window_LoadingScreen", "FadeIn", 0.5f);
            
            LoadingCircleBar.Loader.StartLoading(AWLanguageSystem.LangKey("DREAD_STORIES_LOADING", "DS_LOADING_INITALIZING"));
            this.Invoke(() => LoadingCircleBar.Loader.StartLoading(AWLanguageSystem.LangKey("DREAD_STORIES_LOADING", "DS_LOADING_LOADING")), 1.25f);
            this.Invoke(() => LoadingCircleBar.Loader.StartLoading(AWLanguageSystem.LangKey("DREAD_STORIES_LOADING", "DS_LOADING_SERIALIZING")), 20.5f);
            this.Invoke(() => LoadingCircleBar.Loader.StartLoading(AWLanguageSystem.LangKey("DREAD_STORIES_LOADING", "DS_LOADING_WAITING")), 21.5f);
            
            this.Invoke(() => FadeController.Fader.FadeIn(AutoFadeOut: true, AutoFadeOutDelay: 3.0f), 24.0f);

            this.Invoke(() => AudioManager.Audio.ChangeTrack(2, 0.05f, 1.0f), 1.0f);
            this.Invoke(() => MainMenuWindowManager.Window.ChangeWindowState("Window_Room", false), 0.5f);
        }

        public override void OnCreateRoomFailed(short returnCode, string message) {
            Debug.Log("Room Creation Failed: " + message, this);
        }

        public override void OnMasterClientSwitched(Player player) {
            CancelAndCloseRoom();
        }
    }
}
Leave a Comment