Exemplo n.º 1
0
        public string UpdateScore(int scoreid, int addScore, GameResult result)
        {
            if (result == GameResult.Ended)
            {
                return(string.Empty);
            }
            GameScore newscore = null;
            GameScore oldscore = null;

            newscore = GetScoreOfUser(scoreid);
            bool isNewRecord = false;

            if (newscore == null)
            {
                newscore         = new GameScore();
                newscore.ScoreID = scoreid;
                isNewRecord      = true;
            }
            else
            {
                oldscore = (GameScore)newscore.Clone();
            }
            newscore.ModifyTime = DateTime.Now;
            //Modify data
            newscore.Score += addScore;
            switch (result)
            {
            case GameResult.Drawn: ++newscore.Drawn; break;

            case GameResult.Win: ++newscore.Win; break;

            case GameResult.Fail: ++newscore.Fail; break;

            default: return(string.Empty);
            }
            //Update to database
            string cmdString = isNewRecord ? newscore.GetInsert("scores") : oldscore.GetUpdate(newscore, "scores");

            System.Diagnostics.Debug.WriteLine(cmdString);
            using (var connection = GetConnection())
            {
                connection.Open();
                var cmd = new SQLiteCommand(cmdString, connection);
                System.Diagnostics.Debug.WriteLine($"Update local score : {cmd.ExecuteNonQuery()}");
            }
            return(cmdString);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Login to online server
        /// </summary>
        /// <param name="userName">User name</param>
        /// <param name="password">Password</param>
        /// <returns></returns>
        public ConnectResult Login(string userName, string password, out string errorMsg)
        {
            //Check data
            if (string.IsNullOrEmpty(password) || string.IsNullOrEmpty(userName))
            {
                errorMsg = "Empty userName/password!";
                return(ConnectResult.EmptyUserNameOrPsw);
            }
            HasConnected   = false;
            LoginedAccount = null;
            OnlineAccount account = null;
            GameScore     score   = null;

            errorMsg = string.Empty;
            try
            {
                //Get salt and verify if user exist.
                string salt = string.Empty;
                using (SQLiteConnection conn = GetConnection())
                {
                    SQLiteCommand cmd = new SQLiteCommand($"select * from users where username = '******'", conn);
                    conn.Open();
                    var reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        salt = Convert.ToString(reader["salt"]);
                    }
                }
                if (string.IsNullOrEmpty(salt))
                {
                    errorMsg = "User not exist!";
                    return(ConnectResult.UserNotExist);
                }
                //Verify password
                string hashString = GenerateHashString(password, salt);
                ExecuteReader($"select * from users where username = '******' and password = '******';", reader =>
                {
                    while (reader.Read())
                    {
                        account          = new OnlineAccount();
                        account.State    = AccountState.Online;
                        account.UserName = userName;
                        account.UserID   = Convert.ToInt32(reader["id"]);
                        account.NickName = reader["nickname"].ToString();
                    }
                });
                //Check if login successed
                if (account == null)
                {
                    errorMsg = "Wrong password";
                    return(ConnectResult.WrongPassword);
                }
                //Check record in scores
                ExecuteReader($"select * from scores where userid = '{account.UserID}';", reader =>
                {
                    while (reader.Read())
                    {
                        score = new GameScore()
                        {
                            ScoreID    = Convert.ToInt32(reader["scoreid"]),
                            UserID     = account.UserID,
                            NickName   = Convert.ToString(reader["nickname"]),
                            Score      = Convert.ToInt32(reader["score"]),
                            Win        = Convert.ToInt32(reader["win"]),
                            Fail       = Convert.ToInt32(reader["fail"]),
                            Drawn      = Convert.ToInt32(reader["drawn"]),
                            ModifyTime = Convert.ToDateTime(reader["modifytime"])
                        };
                    }
                });
                //If record zero, create it
                if (score == null)
                {
                    System.Diagnostics.Debug.WriteLine($"score is null");
                    var newscore = new GameScore()
                    {
                        UserID     = account.UserID,
                        NickName   = account.NickName,
                        ModifyTime = DateTime.Now
                    };
                    if (ExecuteNonQuery(newscore.GetInsert("scores")) > 0)
                    {
                        score = newscore;
                        System.Diagnostics.Debug.WriteLine($"Insert record successed");
                    }
                    else
                    {
                        System.Diagnostics.Debug.WriteLine($"Insert record to scores fail");
                        errorMsg = "Insert record to scores fail";
                        return(ConnectResult.GenerateRecordFail);
                    }
                }
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine($"StackTrace:\n {e.StackTrace} \nMessage:\n{e.Message}");
                errorMsg = e.Message;
                return(ConnectResult.OtherError);
            }
            if (score != null)
            {
                LoginedAccount       = account;
                LoginedAccount.Score = score;
                return(ConnectResult.Success);
            }
            return(ConnectResult.OtherError);
        }