void AddToLeaderBoard(LeaderBoardEntry entry) { if (!LeaderBoard.Any()) { LeaderBoard.AddFirst(entry); } else { if (LeaderBoard.First.Value.Score < entry.Score) { LeaderBoard.AddFirst(entry); return; } var current = LeaderBoard.Last; while (current != null) { if (current.Value.Score >= entry.Score) { LeaderBoard.AddAfter(current, entry); } current = current.Previous; } } }
public int?SubmitWord(string playerName, string word) { var score = null as int?; var wordLookup = word?.ToLookup(w => w); // only proceed if we are not working with nulls if (wordLookup != null && lettersLookup != null) { // does the starting set contain all the letters of the word? var contains = wordLookup.All(xs => xs.Count() <= lettersLookup[xs.Key].Count()); if (contains) { // is this a valid word? if (this.validWords.Contains(word)) { score = word.Length; // word has not already been played if (!wordsAlreadyPlayed.Contains(word)) { // Does the leaderboard already contain this work? if (!leaderBoard.Any(l => l.Word == word)) { var newLeaderBoardEntry = new LeaderBoardEntry() { PlayerName = playerName, Word = word, Score = score.Value }; // calculating leaderboard insertion point var entry = leaderBoard.LastOrDefault(l => l.Score >= score); var insertionIndex = 0; if (entry != null) { insertionIndex = leaderBoard.IndexOf(entry) + 1; } leaderBoard.Insert(insertionIndex, newLeaderBoardEntry); // confining leaderboard to top 10 leaderBoard = leaderBoard.Take(10).ToList(); } } } } } return(score); }