public static void RegisterInternalGame(string gameName) { new Thread(delegate() { int threadTick = -1; while (!SkyCoreAPI.IsDisabled) { //Enforce game registration every 15 seconds if (++threadTick % 15 == 0) { //Temp - Sending server is gameName RedisPool.GetSubscriber().PublishAsync($"{GetDevelopmentPrefix()}game_register", $"{gameName}:{gameName}:{CurrentHostAddress}"); } Thread.Sleep(1000); // Update every 1 second InstanceInfo instanceInfo = GameRegistrations[gameName].GetLocalInstance(); if (instanceInfo == null) { SkyUtil.log($"Game not registered under 'local'. {GameRegistrations[gameName].GetAllInstances()}"); continue; //Game not registered? } string availableGameConcat = ""; foreach (GameInfo gameInfo in instanceInfo.AvailableGames) { availableGameConcat += gameInfo.GameId + "," + gameInfo.CurrentPlayers + "," + gameInfo.MaxPlayers + "|"; } string messageContents = SkyCoreAPI.Instance.CurrentIp + ":" + instanceInfo.CurrentPlayers + ":" + availableGameConcat; //SkyUtil.log($"Sending update on {gameName}_info as {messageContents}"); RedisPool.GetSubscriber().PublishAsync($"{GetDevelopmentPrefix()}{gameName}_info", messageContents); } }).Start(); RegisterGame(gameName, new InstanceInfo { HostAddress = "local" }); RedisPool.GetSubscriber().SubscribeAsync($"{GetDevelopmentPrefix()}{gameName}_join", (channel, message) => { string[] messageSplit = ((string)message).Split(':'); if (GameRegistrations.TryGetValue(messageSplit[1], out var gamePool)) { foreach (GameInfo gameInfo in gamePool.GetLocalInstance().AvailableGames) { if (gameInfo.GameId.Equals(messageSplit[2])) { if (IncomingPlayers.ContainsKey(messageSplit[0])) { IncomingPlayers[messageSplit[0]] = gameInfo; } else if (!IncomingPlayers.TryAdd(messageSplit[0], gameInfo)) { return; //Cannot process? } foreach (GameLevel gameLevel in SkyCoreAPI.Instance.GameModes[messageSplit[1]].GameLevels.Values) { if (gameLevel.GameId.Equals(messageSplit[2])) { gameLevel.AddIncomingPlayer(messageSplit[0]); break; } } return; } } } }); }
public async Task Connect() { _hubConnection = new HubConnectionBuilder().WithUrl(_hubUri).Build(); _hubConnection.On(GameEvents.GameSessionStarted, () => GameSessionStarted?.Invoke()); _hubConnection.On <bool>(nameof(IsSessionOpen), (isOpen) => IsSessionOpen?.Invoke(isOpen)); _hubConnection.On(GameEvents.PlayerCreated, () => PlayerCreated?.Invoke()); _hubConnection.On <ICollection <string> >(nameof(IncomingPlayers), (players) => IncomingPlayers?.Invoke(players)); _hubConnection.On(GameEvents.GameStarted, () => GameStarted?.Invoke()); _hubConnection.On <int>(GameEvents.TimeElapsed, countDownSeconds => TimeElapsed?.Invoke(countDownSeconds)); _hubConnection.On(GameEvents.GameStopped, () => GameStopped?.Invoke()); _hubConnection.On <int>(GameEvents.RightAnswer, newScore => SentRightAnswer?.Invoke(newScore)); _hubConnection.On <int>(GameEvents.WrongAnswer, newScore => SentWrongAnswer?.Invoke(newScore)); _hubConnection.On(GameEvents.RoundEnded, () => RoundEnded?.Invoke()); _hubConnection.On <string>(GameEvents.Question, question => QuestionSent?.Invoke(question)); _hubConnection.On <AnswerAndPlayers>(GameEvents.AnswerAndPlayers, answerAndPlayers => GotAnswerAndPlayers?.Invoke(answerAndPlayers)); _hubConnection.On(GameEvents.NextRoundStarted, () => NextRoundStarted?.Invoke()); await _hubConnection.StartAsync(); }