Ejemplo n.º 1
0
 private void addUser(string name)
 {
     User newUser = new User();
     newUser.name = name;
     newUser.scores = new List<Score>();
     users.Add(newUser);
 }
Ejemplo n.º 2
0
        public string handleRequest(string request)
        {
            string response = "ERROR";
            
            switch (state)
            {
                case State.WAIT:
                    if (Regex.Match(request, @"^ADD_USER,[A-Za-z]+$").Success)
                    {
                        state = State.ADD_USERS;

                        string[] elements = request.Split(',');
                        User user = new User();
                        user.name = elements[1];
                        user.scores = new List<Score>();
                        users.Add(user);

                        response = "DONE";
                    }
                    else if (Regex.Match(request, @"^ADD_SCORE,[A-Za-z]+,[0-9]+$").Success)
                    {
                        string[] elements = request.Split(',');
                        string name = elements[1];

                        if (UserExists(name))
                        {
                            state = State.ADD_SCORES;

                            int moveCount = int.Parse(elements[2]);
                            Score score = new Score();
                            score.date = DateTime.Now;
                            score.moveCount = moveCount;

                            int i = findUserIndex(name);
                            users[i].scores.Add(score);
                            response = "DONE";
                        }
                        else
                        {
                            response = "ERROR_UNKNOWN_USER";
                        }
                    }
                    break;
                case State.ADD_USERS:
                    if (Regex.Match(request, @"^ADD_USER,[A-Za-z]+$").Success)
                    {
                        state = State.ADD_USERS;
                        string[] elements = request.Split(',');
                        User user = new User();
                        user.name = elements[1];
                        user.scores = new List<Score>();
                        users.Add(user);

                        response = "DONE";
                    }
                    else if (request == "END_ADD_USERS")
                    {
                        state = State.WAIT;
                        response = "OKAY";
                    }
                    break;
                case State.ADD_SCORES:
                    if (Regex.Match(request, @"^ADD_SCORE,[A-Za-z]+,[0-9]+$").Success)
                    {
                        string[] elements = request.Split(',');
                        string name = elements[1];

                        if (UserExists(name))
                        {
                            int moveCount = int.Parse(elements[2]);
                            Score score = new Score();
                            score.date = DateTime.Now;
                            score.moveCount = moveCount;

                            int i = findUserIndex(name);
                            users[i].scores.Add(score);

                            response = "DONE";
                        }
                        else
                        {
                            response = "ERROR_UNKNOWN_USER";
                        }
                    } else if (request == "END_ADD_SCORES") {
                        state = State.WAIT;
                        response = "OKAY";
                    }
                    break;
            }

            return response;
        }