Ejemplo n.º 1
0
        private Response AddPackage(AuthDetails user, Dictionary <string, object> payload)
        {
            // Package needs to consists of 5 cards
            if (!(payload["array"] is JArray rawCards) || rawCards.Count != 5)
            {
                return(Response.Status(Status.BadRequest));
            }
            // Get user and check if its an admin account
            var userSchema = db.GetUser(user.Username);

            if (userSchema is null)
            {
                return(Response.Status(Status.BadRequest));
            }
            if (userSchema.Role != Role.Admin)
            {
                return(Response.Status(Status.Forbidden));
            }
            // Parse given cards
            var cards = CardSchema.ParseRequest(rawCards);

            // Check if all cards were correctly parsed
            if (cards.Count != 5)
            {
                return(Response.Status(Status.BadRequest));
            }
            // Add package and return corresponding response
            var result = db.AddPackage(cards);

            return(Response.Status(result ? Status.Created : Status.Conflict));
        }
Ejemplo n.º 2
0
        public Response GetBattleHistory(PathVariable <int> path, AuthDetails?user)
        {
            if (user is null)
            {
                return(Response.Status(Status.BadRequest));
            }
            var userSchema = db.GetUser(user.Username);

            if (userSchema is null)
            {
                return(Response.Status(Status.BadRequest));
            }
            if (userSchema.Role != Role.Admin)
            {
                return(Response.Status(Status.Forbidden));
            }
            var battles    = db.GetBattleHistory(path.Value);
            var response   = new Dictionary <string, object>();
            var battleList = new List <Dictionary <string, object> >();

            foreach (var battle in battles)
            {
                if (battle.IsDraw)
                {
                    battleList.Add(new Dictionary <string, object>()
                    {
                        { "Id", battle.Id }, { "PlayerA", battle.PlayerA }, { "PlayerB", battle.PlayerB },
                        { "Draw", battle.IsDraw }
                    });
                }
                else
                {
                    battleList.Add(new Dictionary <string, object>()
                    {
                        { "Id", battle.Id }, { "PlayerA", battle.PlayerA }, { "PlayerB", battle.PlayerB },
Ejemplo n.º 3
0
        public (bool, string) Register(string username, string password)
        {
            var check = db.GetUser(username);

            if (check != null)
            {
                return(false, "");
            }
            var hashPassword = GenerateHash(password);

            if (!db.AddUser(username.ToLower() != "admin"
                ? new UserSchema(username, hashPassword, "User")
                : new UserSchema(username, hashPassword, "Admin")))
            {
                return(false, "");
            }
            var token = GenerateToken(username);

            AddToken(token);
            return(true, token);
        }