Exemplo n.º 1
0
        public static GamePool GetGamePool(string gameName)
        {
            GamePool gamePool;

            if (GameRegistrations.ContainsKey(gameName))
            {
                gamePool = GameRegistrations[gameName];
            }
            else
            {
                GameRegistrations.TryAdd(gameName, gamePool = new GamePool(gameName));
            }

            return(gamePool);
        }
Exemplo n.º 2
0
        //

        public static void RegisterGameIntent(string gameName, bool spawnNPC = false)
        {
            bool npcOnly = false;             //Some games may not be completed. Use an NPC as a placeholder

            if (spawnNPC)
            {
                string         neatName    = gameName;
                PlayerLocation npcLocation = new PlayerLocation(0.5D, 30D, 16.5D, 180F, 180F, 0F);

                switch (gameName)
                {
                case "murder":
                {
                    neatName    = "§c§lMurder Mystery";
                    npcLocation = new PlayerLocation(260.5, 77, 271.5, 180F, 180F, 0F);
                    break;
                }

                case "build-battle":
                {
                    neatName    = "§e§lBuild Battle";
                    npcLocation = new PlayerLocation(252.5, 77, 271.5, 180F, 180F, 0F);
                    break;
                }

                case "block-hunt":
                {
                    neatName    = PlayerNPC.ComingSoonName;
                    npcLocation = new PlayerLocation(263.5, 77, 269.5, 180F, 180F, 0F);
                    npcOnly     = true;
                    break;
                }

                case "bed-wars":
                {
                    neatName    = PlayerNPC.ComingSoonName;
                    npcLocation = new PlayerLocation(249.5, 77, 269.5, 180F, 180F, 0F);
                    npcOnly     = true;
                    break;
                }
                }

                if (!gameName.Equals("hub"))
                {
                    try
                    {
                        PlayerNPC.SpawnHubNPC(null, neatName, npcLocation, $"GID:{gameName}");
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }
                }
            }

            {
                if (!GameRegistrations.ContainsKey(gameName))
                {
                    //Initialize GamePool
                    GamePool gamePool = GetGamePool(gameName);

                    if (npcOnly)
                    {
                        gamePool.Active = false;
                        return;
                    }
                }
                else
                {
                    return;                     //Already listening, Don't start again.
                }
            }

            ISubscriber subscriber = RedisPool.GetSubscriber();

            /*
             * Channel <game>_info
             * Format:
             * {ip-address}:{port}:{current-players}:{available-servers}
             */
            subscriber.SubscribeAsync($"{GetDevelopmentPrefix()}{gameName}_info", (channel, message) =>
            {
                try
                {
                    string[] messageSplit = ((string)message).Split(':');

                    string hostAddress = messageSplit[0];
                    if (!ushort.TryParse(messageSplit[1], out ushort hostPort))
                    {
                        SkyUtil.log($"Invalid format of port in message {message}");
                        return;
                    }

                    InstanceInfo instanceInfo;
                    if (!GameRegistrations.TryGetValue(gameName, out var gamePool))
                    {
                        string gameHostAddress = (hostAddress + ":" + hostPort).Equals(SkyCoreAPI.Instance.CurrentIp)
                                                        ? "local"
                                                        : hostAddress;

                        instanceInfo = new InstanceInfo {
                            HostAddress = gameHostAddress, HostPort = hostPort
                        };

                        SkyUtil.log($"Game {gameName} missing from GameRegistrations! Re-Registering...");
                        RegisterGame(gameName, instanceInfo);
                    }
                    else
                    {
                        instanceInfo = (hostAddress + ":" + hostPort).Equals(SkyCoreAPI.Instance.CurrentIp)
                                                        ? gamePool.GetLocalInstance()
                                                        : gamePool.GetInstance(hostAddress + ":" + hostPort);
                    }

                    int.TryParse(messageSplit[2], out var instancePlayers);

                    instanceInfo.CurrentPlayers = instancePlayers;

                    instanceInfo.AvailableGames = PopulateGameList(messageSplit[3].Split('|'), new List <GameInfo>());
                    instanceInfo.Update();
                    //SkyUtil.log($"Updated {availableGames.Count} available games on {gameName} ({hostAddress + ":" + hostPort})");
                }
                catch (Exception e)
                {
                    BugSnagUtil.ReportBug(e);
                }
            });
        }