Beispiel #1
0
        public async Task <string> AddOneRecordREST(string PlayerscoreJSON)
        {
            string yn = "0";    // default is failed

            if (String.IsNullOrEmpty(PlayerscoreJSON))
            {
                return(yn);
            }
            try
            {
                JObject     json        = JObject.Parse(PlayerscoreJSON);
                Playerscore playerscore = new Playerscore();
                playerscore.PlayerName = (string)json["PlayerName"];
                playerscore.Score      = Convert.ToInt32((string)json["Score"]);
                playerscore.GameId     = Convert.ToInt32((string)json["GameId"]);

                int result = await _playerscoreManager.AddOnePlayerscoreToTable(playerscore);

                if (result == ErrorCodeModel.Succeeded)
                {
                    yn = "1";
                }
            } catch (Exception ex) {
                string msg = ex.StackTrace.ToString();
                Console.WriteLine("AddOneRecordREST failed:\n" + msg);
            }

            return(yn);
        }
Beispiel #2
0
    void FormatHighscores(string textStream)
    {
        string[] entries = textStream.Split(new char[] { '\n' }, System.StringSplitOptions.RemoveEmptyEntries);
        highscoresList = new Highscore[entries.Length];


        for (int i = 0; i < entries.Length; i++)
        {
            string[] entryInfo = entries[i].Split(new char[] { '|' });
            string   username  = entryInfo[0];
            int      score     = int.Parse(entryInfo[1]);
            int      time      = int.Parse(entryInfo[2]);

            highscoresList[i] = new Highscore(username, score, time);


            print(highscoresList[i].username + ": " + highscoresList[i].score + ": " + highscoresList[i].time);

            if (highscoresList[i].username == ScoreManager.playername)
            {
                int rank = i + 1;
                PlayerscoresList = new Playerscore(rank, username, score, time);
                Debug.Log(PlayerscoresList.username + "is found");
                highscoreDisplay.DisplayPlayerRank(PlayerscoresList);
            }
            else
            {
                // print("Error on finding" );
            }
        }
    }
        /// <summary>
        /// Finds the one playerscore by identifier.
        /// </summary>
        /// <returns>The one playerscore by identifier (Playerscore.Id).</returns>
        /// <param name="id">the id of the playerscore.</param>
        public async Task <Playerscore> FindOnePlayerscoreById(int id)
        {
            Playerscore playerscore = await _context.Playerscore
                                      .Where(x => x.Id == id).SingleOrDefaultAsync();

            return(playerscore);
        }
Beispiel #4
0
 private void UpdateScore(bool PlayerWon)
 {
     if (PlayerWon)
     {
         Playerscore++;
         label6.Text   = "Win";
         ourscore.Text = Playerscore.ToString();
     }
     else
     {
         Enemyscore++;
         label6.Text          = "Lose";
         enemyscorelabel.Text = Enemyscore.ToString();
     }
 }
        /// <summary>
        /// Deletes the one playerscore by identifier.
        /// </summary>
        /// <returns>Return the error code.</returns>
        /// <param name="id">Identifier.</param>
        public async Task <int> DeleteOnePlayerscoreById(int id)
        {
            int result = ErrorCodeModel.ErrorBecauseBugs;

            if (id == 0)
            {
                // its a bug, the id of playerscore cannot be 0
                result = ErrorCodeModel.ErrorBecauseBugs;
                return(result);
            }

            Playerscore orgPlayerscore = await FindOnePlayerscoreById(id);

            if (orgPlayerscore == null)
            {
                // the original playerscore does not exist any more
                result = ErrorCodeModel.OriginalPlayerscoreNotExist;
            }
            else
            {
                using (var dbTransaction = _context.Database.BeginTransaction())
                {
                    try
                    {
                        _context.Playerscore.Remove(orgPlayerscore);
                        await _context.SaveChangesAsync();

                        dbTransaction.Commit();
                        result = ErrorCodeModel.Succeeded; // succeeded to update
                    }
                    catch (DbUpdateException ex)
                    {
                        string msg = ex.ToString();
                        Console.WriteLine("Failed to delete one playerscore. Please see log file.\n" + msg);
                        dbTransaction.Rollback();
                        result = ErrorCodeModel.DatabaseError;
                    }
                }
            }

            return(result);
        }
 private void UpdateStateOfRequest(StateOfRequest mState, Playerscore firstPlayerscore, int pageNo, int pageSize, int totalRecords, int totalPages, bool isFind = false)
 {
     mState.CurrentPageNo = pageNo;
     mState.PageSize      = pageSize;
     mState.TotalRecords  = totalRecords;
     mState.TotalPages    = totalPages;
     if (firstPlayerscore != null)
     {
         mState.FirstId = firstPlayerscore.Id;
         if (!isFind)
         {
             // mState.OrgId = mState.FirstId;
         }
     }
     else
     {
         mState.OrgId   = 0;
         mState.OrgNo   = "";
         mState.FirstId = 0;
     }
 }
        /// <summary>
        /// Adds the one playerscore to table.
        /// </summary>
        /// <returns>Return the error code.</returns>
        /// <param name="playerscore">Playerscore.</param>
        public async Task <int> AddOnePlayerscoreToTable(Playerscore playerscore)
        {
            int result = ErrorCodeModel.ErrorBecauseBugs;

            if (playerscore == null)
            {
                // the data for updating is empty
                result = ErrorCodeModel.PlayerscoreIsNull;
                return(result);
            }
            if (string.IsNullOrEmpty(playerscore.PlayerName))
            {
                // the playerscore no that input by user is empty
                result = ErrorCodeModel.PlayerNameIsEmpty;
                return(result);
            }

            using (var dbTransaction = _context.Database.BeginTransaction())
            {
                try
                {
                    _context.Add(playerscore);
                    await _context.SaveChangesAsync();

                    dbTransaction.Commit();
                    result = ErrorCodeModel.Succeeded;
                }
                catch (DbUpdateException ex)
                {
                    string errorMsg = ex.ToString();
                    Console.WriteLine("Failed to add one playerscore: \n" + errorMsg);
                    dbTransaction.Rollback();
                    result = ErrorCodeModel.DatabaseError;
                }
            }

            return(result);
        }
 public void DisplayPlayerRank(Playerscore PlayerscoresList)
 {
     playerscoreField.text = "Your are on " + PlayerscoresList.rank + " Place :" + PlayerscoresList.username + " -  " + PlayerscoresList.score + " -  " + convertsecondtoformat(PlayerscoresList.time);
 }
        /// <summary>
        /// Updates the one playerscore by identifier.
        /// </summary>
        /// <returns>Return the error code</returns>
        /// <param name="id">Identifier.</param>
        /// <param name="playerscore">Playerscore.</param>
        public async Task <int> UpdateOnePlayerscoreById(int id, Playerscore playerscore)
        {
            int result = ErrorCodeModel.ErrorBecauseBugs;

            if (id == 0)
            {
                // its a bug, id of playerscore cannot be 0
                result = ErrorCodeModel.ErrorBecauseBugs;
                return(result);
            }
            if (playerscore == null)
            {
                // the data for updating is empty
                result = ErrorCodeModel.PlayerscoreIsNull;
                return(result);
            }
            if (string.IsNullOrEmpty(playerscore.PlayerName))
            {
                // the playerscore name that input by user is empty
                result = ErrorCodeModel.PlayerNameIsEmpty;
                return(result);
            }

            Playerscore orgPlayerscore = await FindOnePlayerscoreById(id);

            if (orgPlayerscore == null)
            {
                // the original playerscore does not exist any more
                result = ErrorCodeModel.OriginalPlayerscoreNotExist;
                return(result);
            }
            else
            {
                orgPlayerscore.CopyFrom(playerscore);

                // check if entry state changed
                if ((_context.Entry(orgPlayerscore).State) == EntityState.Modified)
                {
                    using (var dbTransaction = _context.Database.BeginTransaction())
                    {
                        try
                        {
                            await _context.SaveChangesAsync();

                            dbTransaction.Commit();
                            result = ErrorCodeModel.Succeeded; // succeeded to update
                        }
                        catch (DbUpdateException ex)
                        {
                            string msg = ex.ToString();
                            Console.WriteLine("Failed to update Playerscore table: \n" + msg);
                            dbTransaction.Rollback();
                            result = ErrorCodeModel.DatabaseError;
                        }
                    }
                }
                else
                {
                    result = ErrorCodeModel.PlayerscoreNotChanged; // no changed
                }
            }

            return(result);
        }
        /// <summary>
        /// Finds the one playerscore by playerscore no.
        /// </summary>
        /// <returns>The one playerscore by playerscore no.</returns>
        /// <param name="playerName">Playerscore no.</param>
        public async Task <Playerscore> FindOnePlayerscoreByPlayerName(string playerName)
        {
            Playerscore playerscore = await _context.Playerscore.Where(x => x.PlayerName == playerName).SingleOrDefaultAsync();

            return(playerscore);
        }