Exemple #1
0
        public IActionResult Post([FromBody] NewLobbyRequest parameters)
        {
            if (parameters == null || parameters.token == null)
            {
                return(BadRequest(new Response("Wrong parameters", null)));
            }

            string token = parameters.token;
            string error = "";

            if (!TokenManager.CheckUser(token))
            {
                error = ErrorTypes.UNAUTH_USER_ERROR.ToString();
            }

            Response resp;

            if (error == "")
            {
                Lobby newLobby = new Lobby(parameters.lobbyName);
                Lobbies.AddLobby(newLobby);
                int id = newLobby.Id;
                resp = new Response(error, new NewLobbyResponse(id));
            }
            else
            {
                resp = new Response(error, null);
            }

            IActionResult res = Ok(resp);

            return(res);
        }
Exemple #2
0
        public IActionResult Post([FromBody] TokenRequest parameters)
        {
            if (parameters == null || parameters.token == null)
            {
                return(BadRequest(new Response("Wrong parameters", null)));
            }

            string token = parameters.token;
            string error = "";

            if (!TokenManager.CheckUser(token))
            {
                error = ErrorTypes.UNAUTH_USER_ERROR.ToString();
            }

            Response resp;

            if (error == "")
            {
                LobbiesResponse lobbiesResp = Lobbies.getLobbiesResponse();
                resp = new Response(error, lobbiesResp);
            }
            else
            {
                resp = new Response(error, null);
            }

            IActionResult res = Ok(resp);

            return(res);
        }
Exemple #3
0
        public void TokenManagerTest()
        {
            int    id    = 12;
            string token = TokenManager.RegisterUserToken(id, "s");
            string hash  = Hash.HashId(id);

            Assert.IsTrue(TokenManager.CheckUser(hash));
        }
Exemple #4
0
        public IActionResult Post([FromBody] LobbyRequest parameters, [FromRoute] int id)
        {
            if (parameters == null || parameters.token == null)
            {
                return(BadRequest(new Response("Wrong parameters", null)));
            }

            string token = parameters.token;
            string error = "";

            if (!TokenManager.CheckUser(token))
            {
                error = ErrorTypes.UNAUTH_USER_ERROR.ToString();
            }

            Response resp;

            if (error == "")
            {
                Lobby  curLobby = Lobbies.GetLobby(id);
                Player p        = TokenManager.GetPlayer(token);
                if (curLobby == null)
                {
                    error = ErrorTypes.NONEXIST_LOBBY_ERROR.ToString();
                    resp  = new Response(error, null);
                }
                else if (curLobby.GetPlayer(token) != null)
                {
                    error = ErrorTypes.ALREADY_CONNECT_TO_LOBBY_ERROR.ToString();
                    resp  = new Response(error, null);
                }
                else if (p.Status != PlayerStatus.Default)
                {
                    error = ErrorTypes.ALREADY_CONNECT_TO_DIFF_LOBBY_ERROR.ToString();
                    resp  = new Response(error, null);
                }
                else if (curLobby.Status != LobbyStatus.WaitingForPlayer)
                {
                    error = ErrorTypes.FULL_LOBBY_ERROR.ToString();
                    resp  = new Response(error, null);
                }
                else
                {
                    curLobby.ConnectPlayer(p);
                    resp = new Response(error, new LobbyResponse(curLobby));
                }
            }
            else
            {
                resp = new Response(error, null);
            }

            IActionResult res = Ok(resp);

            return(res);
        }
Exemple #5
0
        public IActionResult Post([FromBody] Auth parameters)
        {
            if (parameters == null || parameters.authParam == null || parameters.authParam.email == null ||
                parameters.authParam.password == null)
            {
                return(BadRequest(new Response("Wrong parameters", null)));
            }

            string        error    = "";
            string        email    = parameters.authParam.email;
            string        password = parameters.authParam.password;
            TokenResponse tr       = dbManager.CheckUser(email, password);
            int           id       = Int32.Parse(tr.token);
            string        username = tr.nickname;

            if (id == -1)
            {
                error += ErrorTypes.AUTH_ERROR.ToString();
            }

            Response resp;

            if (error == "")
            {
                string token = "";
                if (!TokenManager.CheckUser(Hash.HashId(id)))
                {
                    token = TokenManager.RegisterUserToken(id, username);
                }
                else
                {
                    token = Hash.HashId(id);
                }
                TokenResponse tokenResp = new TokenResponse(token, username);
                resp = new Response(error, tokenResp);
            }
            else
            {
                resp = new Response(error, null);
            }

            IActionResult res = Ok(resp);

            return(res);
        }
Exemple #6
0
        public static bool ProcessRequest(WebSocketContext wsContext, string token, string type, object data)
        {
            //Тассов, не стукай
            bool isConnected = TokenManager.CheckUser(token) && LobbyRoutes.ContainsKey(token);

            if (!isConnected)
            {
                return(false);
            }
            switch (type)
            {
            case ("CONNECT"):
                ConnectPlayer(wsContext, token);
                break;

            case ("PING"):
                break;

            case ("PLAYER_STATUS_CHANGED"):
                WSStatusRequest statusReq = JsonConvert.DeserializeObject <WSStatusRequest>(data.ToString());
                ChangePlayerStatus(token, statusReq.status);
                break;

            case ("MOVE_ATTEMPT"):
                WSDirectionRequest dir = JsonConvert.DeserializeObject <WSDirectionRequest>(data.ToString());
                MovePlayer(token, dir.direction);
                break;

            case ("TRAP_PLACE"):
                WSPlaceTrapRequest req = JsonConvert.DeserializeObject <WSPlaceTrapRequest>(data.ToString());
                PlaceTrap(token, req.trap, req.xpos, req.ypos);
                break;

            case ("DISCONNECT"):
                DisconnectPlayer(token);
                break;

            default:
                UnknownCommand();
                break;
            }
            return(true);
        }