using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; using Player = ExitGames.Client.Photon.LoadBalancing.Player; using Hashtable = ExitGames.Client.Photon.Hashtable; public class PlayerPropertyEntry { /// /// Unique key for . /// public readonly string id; /// /// Make sure is unique. /// /// public PlayerPropertyEntry(string id){ this.id = id; } /// /// Gets value from local player. /// /// public T GetLocal(){ return Get(NetworkManager.net.LocalPlayer); } /// /// Gets value from . /// /// /// public T Get(Player player){ return (T)player.CustomProperties[id]; } /// /// Sets for local player. /// /// public void SetLocal(T value){ Set(NetworkManager.net.LocalPlayer, value); } /// /// Sets for . /// /// /// public void Set(Player player, T value){ var h = new Hashtable(); h.Add(id, value); player.SetCustomProperties(h); } /// /// Sets with (, ). /// /// /// public void Initialilze(Hashtable hashtable, T value){ hashtable.Add(id, value); } // ---------------------------------------------------- // Seems like the best place to put this // dunno // ---------------------------------------------------- /// /// Gets value of in . /// /// public int GetPlayerPrefInt() => PlayerPrefs.GetInt(id, 0); /// /// Sets using into . /// /// public void SetPlayerPrefInt(int value) => PlayerPrefs.SetInt(id, value); } public static class PlayerProperties { /// /// A huge list of a bunch of touhous. /// public static readonly string[] touhous = new[]{ "Reimu", "Marsia", "Rumia", "Daiyousei", "Cirno", "Hong", "Koakuma", "Patchouli", "Sakuya", "Flandre", "Letty", "Chen", "Alice", "Lily", "Lyrica", "Lunasa", "Merlin", "Youmu", "Yuyuko", "Ran", "Yakari", "Suika", "Wriggle", "Mystia", "Keine", "Tewi", "Reisen", "Eirin", "Kaguya", "Mokou", "Aya", "Medicine", "Yuuka", "Komachi", "Eiki", "Shizuha", "Minoriko", "Hina", "Nitori", "Momiji", "Sanae", "Kanako", "Suwako", "Iku", "Tenshi", "Hatate", "Kokoro", "Kisume", "Yamame", "Parsee", "Yuugi", "Satori", "Rin", "Utsuho", "Koishi", "Kyouko", "Yoshika", "Seiga", "Tojiko", "Futo", "Miko", "Mamizou", "Wakasagihime", "Sekibanki", "Kagerou", "Benben", "Yatsuhashi", "Shinmyoumaru", "Raiko", "Sumireko", "Joon", "Shion", "Seiran", "Ringo", "Doremy", "Sagume", "Clownpiece", "Junko", "Hecatia", "Eternity", "Nemuno", "Auun", "Narumi", "Satono", "Mai", "Okina", "Eika", "Urumi", "Kutaka", "Yachie", "Mayumi", "Keiki", "Saki", "Rinnosuke", "Sunny", "Luna", "Star", "Chang'e", "Kasen", "Kosuzu" }; /// /// As name implies, gives a random touhou. /// public static string getRandomTouhou => touhous[Random.Range(0, touhous.Length)]; public static readonly string playerNickname = "nn"; /// /// Player's status in lobby. (ready or not) /// public static readonly PlayerPropertyEntry lobbyStatus = new PlayerPropertyEntry("ls"); /// /// Player's status in loading a scene. (ready or not) /// public static readonly PlayerPropertyEntry gameStatus = new PlayerPropertyEntry("gs"); /// /// Player's selected character. /// public static readonly PlayerPropertyEntry playerCharacter = new PlayerPropertyEntry("pc"); /// /// Player's selected team. /// public static readonly PlayerPropertyEntry playerTeam = new PlayerPropertyEntry("pt"); /// /// Player's selected response. /// public static readonly PlayerPropertyEntry playerResponse = new PlayerPropertyEntry("pr"); public static Player localPlayer { get { return NetworkManager.net.LocalPlayer; } } /// /// Initializes all custom properties for the local player. /// public static void CreatePlayerHashtable(){ var h = new Hashtable(); localPlayer.NickName = GetPlayerNickname(); lobbyStatus.Initialilze(h, false); gameStatus.Initialilze(h, false); playerCharacter.Initialilze(h, playerCharacter.GetPlayerPrefInt()); playerTeam.Initialilze(h, playerCharacter.GetPlayerPrefInt()); playerResponse.Initialilze(h, -1); localPlayer.SetCustomProperties(h); } /// /// Resets a select few custom properties for the local player. /// public static void ResetPlayerHashtable(){ var h = new Hashtable(); lobbyStatus.Initialilze(h, false); playerResponse.Initialilze(h, -1); localPlayer.SetCustomProperties(h); } #region Ready Status /// /// If inside a room, returns if all players are lobby ready. /// If not, returns if the local player is lobby ready. /// /// public static bool GetAllLobbyStatus(){ if (!NetworkManager.inRoom) return lobbyStatus.GetLocal(); var players = NetworkManager.net.CurrentRoom.Players.Values; if (players.Count < 2) return false; foreach(var p in players){ if (!lobbyStatus.Get(p)) return false; } return true; } /// /// If inside a room, returns if all players are game ready. /// If not, returns if the local player is game ready. /// /// public static bool GetAllGameStatus() { if (!NetworkManager.inRoom) return gameStatus.GetLocal(); var players = NetworkManager.net.CurrentRoom.Players.Values; if (players.Count < 2) return false; foreach (var p in players) { if (!gameStatus.Get(p)) return false; } return true; } /// /// Returns the highest value player response from all players. /// Returns -3 if no room is active. /// Returns -2 if there is less than 2 players in the room. /// /// public static int GetAllResponse(){ if (!NetworkManager.inRoom) return -3; var players = NetworkManager.net.CurrentRoom.Players.Values; if (players.Count < 2) return -2; var response = int.MaxValue; foreach (var p in players) { response = Mathf.Min(playerResponse.Get(p), response); } return response; } /// /// If inside a room, returns true if team mode is inactive OR if team mode is active and there is 2+ unique teams. /// If not inside a room, returns true. /// /// public static bool GetAllTeamDifferent(){ if (!NetworkManager.inRoom) return true; if (!RoomProperties.teamStatus.Get()) return true; var uniqueTeams = NetworkManager.net.CurrentRoom.Players.Values.Select(p => playerTeam.Get(p)).Distinct(); return uniqueTeams.Count() > 1; } #endregion #region Nickname /// /// Gets local player's nickname from . /// Returns a random touhou if no nickname is found. /// /// public static string GetPlayerNickname(){ var key = playerNickname; if (PlayerPrefs.HasKey(key)){ return PlayerPrefs.GetString(key); } else { var value = getRandomTouhou; PlayerPrefs.SetString(key, value); return value; } } /// /// Sets into and . /// /// public static void SetPlayerNickname(string nickname){ var key = playerNickname; PlayerPrefs.SetString(key, nickname); localPlayer.NickName = key; } #endregion }