public static Command highScoreCommand(highScoreBundle[] hsbs, string username) { Command newCommand = new Command(); newCommand.cType = CType.HighScore; newCommand.message = newCommand.cType + ":" + hsbs.Length; for (int i = 0; i < hsbs.Length; i++ ) { newCommand.message += ":" + hsbs[i].username + ":" + hsbs[i].score; } newCommand.message += ":" + GameManager.db.getRank(username) + ":" + GameManager.db.getScore(username) + ";"; return newCommand; }
public highScoreBundle[] getHighScores(int n) { highScoreBundle[] highScores = new highScoreBundle[n]; SQLiteCommand dbcmd = dbcon.CreateCommand(); string Sql = "SELECT username,score FROM scores ORDER BY score DESC LIMIT ?"; dbcmd.CommandText = Sql; SQLiteParameter param = new SQLiteParameter(); param.Value = n; dbcmd.Parameters.Add(param); SQLiteDataReader reader = dbcmd.ExecuteReader(); int s = 0; while (reader.Read()) { highScoreBundle hsb = new highScoreBundle(); hsb.username = reader.GetString(0); hsb.score = reader.GetInt32(1); highScores[s] = hsb; s++; if (s >= n) { break; } } if(s < n) { highScoreBundle[] newHighScore = new highScoreBundle[s]; for (int i = 0; i < s; i++) { newHighScore[i] = highScores[i]; } highScores = newHighScore; } // for security reasons... // close reader reader.Close(); reader = null; // dispose of database commands dbcmd.Dispose(); dbcmd = null; return highScores; }