Ejemplo n.º 1
0
        //This also acts as notification of player exiting game.
        public async UniTask NotifyUserDisconnect(IpPortInfo ipPortInfo, int connectionID)
        {
            var request = UnityWebRequest.Post($"{BASE_ADDRESS}matches/notify/disconnect?connectionID={connectionID}", "");

            request.uploadHandler             = new UploadHandlerRaw(System.Text.Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(ipPortInfo)));
            request.uploadHandler.contentType = "application/json";
            request.SetRequestHeader("Content-Type", "application/json");
            await request.SendWebRequest();
        }
Ejemplo n.º 2
0
    public void JoinServer(IpPortInfo ipPortInfo)
    {
        _networkManager.networkAddress = ipPortInfo.IpAddress;
        _telepathyTransport.port       = (ushort)ipPortInfo.DesktopPort;
        _webTransport.port             = (ushort)ipPortInfo.WebsocketPort;

        Debug.Log("Client join server");

        _networkManager.StartClient();
    }
Ejemplo n.º 3
0
        public async UniTask TurnOffServerAsync(IpPortInfo ipPortInfo)
        {
            var request = UnityWebRequest.Delete($"{BASE_ADDRESS}server/turnoff/{ipPortInfo}");

            request.uploadHandler             = new UploadHandlerRaw(System.Text.Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(ipPortInfo)));
            request.uploadHandler.contentType = "application/json";
            request.SetRequestHeader("Content-Type", "application/json");
            await request.SendWebRequest();

            Debug.Log($"Unregister server {ipPortInfo}");
        }
        /// <summary>
        /// Disconnect user from the server. As server doesn't hold user information as it has no UserManager,
        /// only connectionId can be passed.
        /// </summary>
        /// <param name="serverIp"></param>
        /// <param name="connectionID"></param>
        public void DisconnectUser(IpPortInfo serverIp, int connectionID)
        {
            var validServer = Servers.TryGetValue(serverIp, out var server);

            if (validServer == false)
            {
                _logger.LogError($"Tried to disconnect {connectionID} to {serverIp} which does not exist");
            }

            server.DisconnectUser(connectionID);
        }
        public GameServer GetServer(IpPortInfo ipPortInfo)
        {
            var success = Servers.TryGetValue(ipPortInfo, out var server);

            if (success)
            {
                return(server);
            }

            return(null);
        }
        public void NotifyMatchStarted(IpPortInfo serverIp, string matchID)
        {
            var serverExists = Servers.TryGetValue(serverIp, out var server);

            if (serverExists == false)
            {
                _logger.LogError($"Trying to start {matchID} from not existing server {server}");
                return;
            }

            server.NotifyMatchStarted(matchID);
        }
        public void RemoveMatch(IpPortInfo serverIp, string matchID)
        {
            var serverExists = Servers.TryGetValue(serverIp, out var server);

            if (serverExists == false)
            {
                _logger.LogError("Trying to delete match from server which does not exist.");
                return;
            }

            server.DeleteMatch(matchID);
        }
        public void RenameServer(IpPortInfo ipPortInfo, string newName)
        {
            var result = Servers.TryGetValue(ipPortInfo, out var server);

            if (result == false)
            {
                _logger.LogError($"Failed to change name of server {ipPortInfo} as it doesn't exists in MatchManager");
                return;
            }

            server.Name = newName;
        }
        /// <summary>
        /// Relate user to a connection id, generated by user when entering the match
        /// </summary>
        /// <param name="user"></param>
        public bool ConnectUser(IpPortInfo serverIpInfo, GameUser user)
        {
            var validServer = Servers.TryGetValue(serverIpInfo, out var server);

            if (validServer == false)
            {
                _logger.LogError($"Tried to connect {user} to {serverIpInfo} which does not exist");
                return(false);
            }

            return(server.ConnectUser(user));
        }
Ejemplo n.º 10
0
        public async UniTask <bool> RegisterServerAsync(string serverName, IpPortInfo ipPortInfo, int maxMatches = 5)
        {
            var jsonContent = JsonConvert.SerializeObject(ipPortInfo);
            var request     = UnityWebRequest.Post($"{BASE_ADDRESS}server/register/{serverName}?maxMatches={maxMatches}", "");

            request.uploadHandler             = new UploadHandlerRaw(System.Text.Encoding.UTF8.GetBytes(jsonContent));
            request.uploadHandler.contentType = "application/json";
            request.SetRequestHeader("Content-Type", "application/json");

            var response = await request.SendWebRequest();

            return(!response.isNetworkError && !response.isHttpError);
        }
        public MatchJoinResult JoinMatch(IpPortInfo serverIp, string matchID, GameUser user)
        {
            var serverExists = Servers.TryGetValue(serverIp, out var server);

            if (serverExists == false)
            {
                return(new MatchJoinResult
                {
                    JoinFailReason = $"Server-{serverIp} does not exists",
                    JoinSucceeded = false,
                });
            }

            return(server.JoinMatch(matchID, user));
        }
Ejemplo n.º 12
0
        //TODO: chagne all signitures
        public async UniTask MakeHostAsync(IpPortInfo ipPortInfo, GameUser user)
        {
            var serverUser = new ServerUserDTO
            {
                User       = user,
                IpPortInfo = ipPortInfo,
            };
            var serverUserJson = JsonConvert.SerializeObject(serverUser);
            var request        = UnityWebRequest.Post($"{BASE_ADDRESS}matches/makehost", "");

            request.uploadHandler             = new UploadHandlerRaw(System.Text.Encoding.UTF8.GetBytes(serverUserJson));
            request.uploadHandler.contentType = "application/json";
            request.SetRequestHeader("Content-Type", "application/json");

            await request.SendWebRequest();
        }
        public async Task RegisterGameServerAsync(string name, int maxMatches, IpPortInfo ipPortInfo)
        {
            if (Servers.ContainsKey(ipPortInfo))
            {
                _logger.LogError($"Trying to register duplicate server => {ipPortInfo}");

                return;
            }

            var serverModel = await _dbContext.Servers.TryFindGameServerModel(ipPortInfo);

            if (serverModel != null)
            {
                //Turn on server
                _logger.LogWarning($"Already has {serverModel.IpPortInfo} in the database. Turning on the server.");
                var newServer = new GameServer(serverModel, _loggerFactory, serverModel.MaxMatches);
                Servers.TryAdd(ipPortInfo, newServer);
                return;
            }

            //Register new servver
            serverModel = new GameServerModel
            {
                IpPortInfo = ipPortInfo,
                MaxMatches = maxMatches,
                Name       = name,
                State      = ServerState.Running,
            };

            _dbContext.Servers.Add(serverModel);
            await _dbContext.SaveChangesAsync();

            var server = new GameServer(serverModel, _loggerFactory);

            server.MaxMatches = maxMatches;

            var result = Servers.TryAdd(ipPortInfo, server);

            if (result == false)
            {
                _logger.LogError($"Failed to add {server} as it already exists");

                return;
            }

            _logger.LogInformation($"Registerd server {server}");
        }
Ejemplo n.º 14
0
        public async UniTask NotifyMatchStarted(IpPortInfo ipPortInfo, string matchID)
        {
            var serverMatchDTO = new ServerMatchDTO
            {
                IpPortInfo = ipPortInfo,
                MatchID    = matchID
            };

            var jsonValue = JsonConvert.SerializeObject(serverMatchDTO);
            var request   = UnityWebRequest.Post($"{BASE_ADDRESS}matches/start", "");

            request.uploadHandler             = new UploadHandlerRaw(System.Text.Encoding.UTF8.GetBytes(jsonValue));
            request.uploadHandler.contentType = "application/json";
            request.SetRequestHeader("Content-Type", "application/json");

            await request.SendWebRequest();
        }
Ejemplo n.º 15
0
        //TODO: chagne all signitures
        public async UniTask <MatchJoinResultDTO> JoinMatchAsync(IpPortInfo ipPortInfo, string matchID, GameUser user)
        {
            var serverUser = new ServerUserDTO
            {
                User       = user,
                IpPortInfo = ipPortInfo,
            };
            var serverUserJson = JsonConvert.SerializeObject(serverUser);
            var request        = UnityWebRequest.Post($"{BASE_ADDRESS}matches/join?matchID={matchID}", "");

            request.uploadHandler             = new UploadHandlerRaw(System.Text.Encoding.UTF8.GetBytes(serverUserJson));
            request.uploadHandler.contentType = "application/json";
            request.SetRequestHeader("Content-Type", "application/json");

            var response = await request.SendWebRequest();

            var matchJoinResultString = response.downloadHandler.text;

            return(JsonConvert.DeserializeObject <MatchJoinResultDTO>(matchJoinResultString));
        }
        public async Task TurnOffServerAsync(IpPortInfo ipPortInfo)
        {
            var serverModel = await _dbContext.Servers.TryFindGameServerModel(ipPortInfo);

            if (serverModel == null)
            {
                return;
            }

            //TODO : 없애지 말고 상태를 off로 해
            var result = Servers.TryRemove(ipPortInfo, out var server);

            if (result == false)
            {
                _logger.LogError($"Error occured while turning off server {ipPortInfo}");
            }

            serverModel.State = ServerState.Off;
            await _dbContext.SaveChangesAsync();

            _logger.LogInformation($"Turned off {server}");
        }
        public async Task UnRegisterGameServerAsync(IpPortInfo ipPortInfo)
        {
            var serverModel = await _dbContext.Servers.TryFindGameServerModel(ipPortInfo);

            if (serverModel == null)
            {
                _logger.LogError($"{ipPortInfo} doesn't exist in the database");
            }
            else
            {
                _dbContext.Servers.Remove(serverModel);
                await _dbContext.SaveChangesAsync();
            }

            var result = Servers.TryRemove(ipPortInfo, out var _);

            if (result == false)
            {
                _logger.LogError($"Error occured while turning off server {ipPortInfo}");
                return;
            }

            _logger.LogInformation($"Removed server => {ipPortInfo}");
        }
Ejemplo n.º 18
0
 public void ConfigureMatchInfo(MatchDTO match)
 {
     Match      = match;
     IpPortInfo = match.IpPortInfo;
 }
        public static async Task <GameServerModel> TryFindGameServerModel(this DbSet <GameServerModel> servers, IpPortInfo ipPortInfo)
        {
            var server = await servers.FirstOrDefaultAsync(x =>
                                                           x.IpPortInfo.IpAddress == ipPortInfo.IpAddress &&
                                                           x.IpPortInfo.DesktopPort == ipPortInfo.DesktopPort &&
                                                           x.IpPortInfo.WebsocketPort == ipPortInfo.WebsocketPort);

            return(server);
        }
Ejemplo n.º 20
0
 public async Task UnregisterServerAsync(IpPortInfo ipAddress)
 {
     await _matchManager.UnRegisterGameServerAsync(ipAddress);
 }
Ejemplo n.º 21
0
 public async Task TurnOffServerAsync(IpPortInfo ipAddress)
 {
     await _matchManager.TurnOffServerAsync(ipAddress);
 }
Ejemplo n.º 22
0
        public async Task <ActionResult> RegisterServerAsync(string name, [FromQuery] int maxMatches, [FromBody] IpPortInfo ipPortInfo)
        {
            await _matchManager.RegisterGameServerAsync(name, maxMatches, ipPortInfo);

            return(Ok());
        }
Ejemplo n.º 23
0
 public void ResetMatchInfo()
 {
     Match      = null;
     IpPortInfo = null;
 }