public async Task <JsonResult> ProcessMessage()
        {
            RestResponse response = new RestResponse();

            byte[] byteJson = await Request.GetRawBodyBytesAsync();

            string             json    = Encoding.Unicode.GetString(byteJson);
            RestRequestMessage request =
                JsonConvert.DeserializeObject <RestRequestMessage>(json, _restDataJsonConverter);

            DeleteOldPLayers();
            Console.WriteLine("NEW MESSAGE");
            response.UserCertificate = Guid.NewGuid().ToByteArray();

            User user;

            switch (request)
            {
            case RestDataRequestMessage message:

                Console.WriteLine($"NEW MESSAGE OF TYPE:${message.MessageType}");

                HandleMessage(message, message.GetMessage(), ref response, request);


                if (message.SessionCredentials != null)
                {
                    response.UserCertificate = message.SessionCredentials.SessionKey.ToByteArray();
                    user = _context.Users.Find(userObj =>
                                               userObj.Id.SessionKey == message.SessionCredentials.SessionKey);
                    if (user != null)
                    {
                        user.LastAlive = DateTime.Now;

                        var hasMessage = user.QueuedMessages.TryDequeue(out var result);
                        if (hasMessage)
                        {
                            response.EnqueueMessage(result);
                        }
                    }
                }

                break;

            case AliveMessage message:
                if (message.SessionCredentials != null)
                {
                    response.UserCertificate = message.SessionCredentials.SessionKey.ToByteArray();
                }

                Console.WriteLine($"NEW MESSAGE OF TYPE:${message.GetType().Name}");
                response.SetSuccessful(true, "Alive");
                if (message.SessionCredentials != null)
                {
                    user = _context.Users.Find(userObj =>
                                               userObj.Id.SessionKey == message.SessionCredentials.SessionKey);
                    if (user != null)
                    {
                        user.LastAlive = DateTime.Now;

                        var hasMessage = user.QueuedMessages.TryDequeue(out var result);
                        if (hasMessage)
                        {
                            response.EnqueueMessage(result);
                        }
                    }
                }

                break;

            case DisconnectMessage message:
                var usertokick = _context.Users.Find(x => message.UserCertificate == x.Id.SessionKey.ToByteArray());
                if (usertokick == null)
                {
                    response.SetSuccessful(true, "Disconnected");
                }
                else
                {
                    _context.Users.Remove(usertokick);
                    user = _context.FindServerHosterBy(usertokick.ConnectedServer);
                    if (user?.HostedServer != null)
                    {
                        user.HostedServer.PlayerCount--;
                    }

                    response.SetSuccessful(true, "Disconnected");
                }

                break;

            case ConnectMessage message:
                Console.WriteLine($"NEW MESSAGE OF TYPE:${message.GetType().Name}");
                response.SetSuccessful(true, "Alive");
                break;

            default:
                Console.WriteLine($"Unhandled Type: ${request.GetType().Name}");
                break;
            }


            //  response.FunctionResult = new RestObjectFunctionResult();new ServerStatus(new ServerStatus(true,true,true,true,new TextObject("hello world")));
            var settings = new JsonSerializerSettings
            {
                Converters = new JsonConverter[]
                {
                    _restDataJsonConverter
                },
                ContractResolver = new DefaultContractResolver {
                    NamingStrategy = new CamelCaseNamingStrategy()
                }
            };

            return(new JsonResult(response, settings));
        }
Example #2
0
        public async Task <JsonResult> ProcessMessage()
        {
            RestResponse response = new RestResponse();

            byte[] byteJson = await Request.GetRawBodyBytesAsync();

            string             json    = Encoding.Unicode.GetString(byteJson);
            RestRequestMessage request =
                JsonConvert.DeserializeObject <RestRequestMessage>(json,
                                                                   new JsonConverter[] { this._restDataJsonConverter });


            Console.WriteLine("NEW MESSAGE");
            response.UserCertificate = Guid.NewGuid().ToByteArray();
            switch (request)
            {
            case RestDataRequestMessage message:
                Console.WriteLine($"NEW MESSAGE OF TYPE:${message.MessageType}");
                switch (message.MessageType)
                {
                case MessageType.Login:
                    Console.Write("New Login");
                    HandleLogins(message, ref response, request);

                    break;

                case MessageType.Message:
                    Console.Write("New Message Message");
                    HandleMessage(message, ref response);

                    break;

                case MessageType.Function:
                    HandleFunctions(message, ref response);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
                if (message.SessionCredentials != null)
                {
                    response.UserCertificate = message.SessionCredentials.SessionKey.ToByteArray();
                    var user = _context.Users.Find(user => user.Id.SessionKey == message.SessionCredentials.SessionKey);
                    if (user != null)
                    {
                        RestResponseMessage result;
                        var hasMessage = user.QueuedMessages.TryDequeue(out result);
                        if (hasMessage)
                        {
                            response.EnqueueMessage(result);
                        }
                    }
                }

                break;

            case AliveMessage message:
                if (message.SessionCredentials != null)
                {
                    response.UserCertificate = message.SessionCredentials.SessionKey.ToByteArray();
                }

                Console.WriteLine($"NEW MESSAGE OF TYPE:${message.GetType().Name}");
                response.SetSuccessful(true, "Alive");
                if (message.SessionCredentials != null)
                {
                    var user = _context.Users.Find(user => user.Id.SessionKey == message.SessionCredentials.SessionKey);
                    if (user != null)
                    {
                        RestResponseMessage result;
                        var hasMessage = user.QueuedMessages.TryDequeue(out result);
                        if (hasMessage)
                        {
                            response.EnqueueMessage(result);
                        }
                    }
                }
                break;

            case ConnectMessage message:

                Console.WriteLine($"NEW MESSAGE OF TYPE:${message.GetType().Name}");
                response.SetSuccessful(true, "Alive");
                break;

            default:
                Console.WriteLine($"Unhandled Type: ${request.GetType().Name}");
                break;
            }



            //  response.FunctionResult = new RestObjectFunctionResult();new ServerStatus(new ServerStatus(true,true,true,true,new TextObject("hello world")));
            var settings = new JsonSerializerSettings
            {
                Converters = new JsonConverter[]
                {
                    this._restDataJsonConverter
                },
                ContractResolver = new DefaultContractResolver {
                    NamingStrategy = new CamelCaseNamingStrategy()
                }
            };

            return(new JsonResult(response, settings));
        }