-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHighScoreQuery.cs
70 lines (63 loc) · 2 KB
/
HighScoreQuery.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Linq;
using System.Linq;
namespace CarRacingGame
{
class HighScoreQuery
{
static DataClasses1DataContext dbContext = new DataClasses1DataContext();
Table<HighScore> highscores = dbContext.GetTable<HighScore>();
public IQueryable ShowTableData()
{
var data = (from row in highscores
orderby row.BestScore descending
select new { row.UserName, row.BestScore, row.Date }).Take(10);
return data;
}
public bool IsTopTen(double userScore)
{
var data = (from row in highscores
orderby row.BestScore descending
select new { row.UserName, row.BestScore, row.Date }).Take(10);
List<double> scores = new List<double>();
foreach (var x in data)
{
scores.Add(x.BestScore);
}
//If database contains less than 10 items
if (scores.Count < 10)
{
return true;
}
//If userScore is greater than the minimum score
else if (userScore > scores.Min())
{
return true;
}
else
{
return false;
}
}
public void AddScore(string username, double score)
{
DateTime date = DateTime.Now;
HighScore obj = new HighScore();
obj.UserName = username;
obj.BestScore = Math.Round(score);
obj.Date = date;
try
{
dbContext.GetTable<HighScore>().InsertOnSubmit(obj);
dbContext.SubmitChanges();
Console.WriteLine("Submitted: " + username + " " + score + " " + date);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}