ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/public/Qzhteln/trunk/TELTest/Scoreboard.cs
Revision: 2
Committed: Wed Feb 11 13:07:01 2026 UTC (8 weeks, 4 days ago) by figdor32
File size: 4157 byte(s)
Log Message:
Initial check-in

File Contents

# Content
1 using System;
2 using System.Collections.Generic;
3 using System.IO;
4 using System.Linq;
5 using System.Text;
6 using System.Threading;
7 using System.Threading.Tasks;
8 using System.Timers;
9
10 namespace TELTest {
11 public class Scoreboard {
12 public Scoreboard(string fileName) {
13 FileName = fileName;
14 if (File.Exists(fileName)) {
15 ReloadAllScores();
16 }
17 t.Interval = 15000;
18 t.Elapsed += this.T_Elapsed;
19 t.AutoReset = false;
20 }
21
22 private void T_Elapsed(Object sender, ElapsedEventArgs e) {
23 Save();
24 }
25
26 public string FileName { get; }
27 private readonly object _lock = new object();
28 private void ReloadAllScores() {
29 string[] lines = null;
30 lock (_lock) lines = File.ReadAllLines(FileName);
31 lock (m_Entries)
32 foreach (var item in lines) {
33 var i = ScoreboardEntry.Deserialize(item);
34 if (i.Name != null) m_Entries.Add(i);
35 }
36 }
37 public void Save() {
38 lock (_lock) {
39 File.WriteAllLines(FileName, m_Entries.Select(x=>x.Serialize()));
40 }
41 }
42 public void Add(ScoreboardEntry entry) {
43 lock (m_Entries)
44 m_Entries.Add(entry);
45 t.Stop();
46 t.Start();
47 }
48 public ScoreboardEntry[] Highscore(int count = 15) {
49 lock(m_Entries)
50 return m_Entries.OrderBy(x => -x.Value).ToList().GetRange(0, count < m_Entries.Count ? count : m_Entries.Count).ToArray();
51 }
52 public ScoreboardEntry[] Lowscore(int count = 15) {
53 lock (m_Entries)
54 return m_Entries.OrderBy(x => x.Value).ToList().GetRange(0, count < m_Entries.Count ? count : m_Entries.Count).ToArray();
55 }
56 System.Timers.Timer t = new System.Timers.Timer();
57
58 private List<ScoreboardEntry> m_Entries = new List<ScoreboardEntry>();
59 public IReadOnlyList<ScoreboardEntry> Entries { get { return m_Entries; } }
60 public struct ScoreboardEntry {
61 public ScoreboardEntry(string name, int value) {
62 this.Date = DateTime.Now;
63 this.Name = name;
64 this.Value = value;
65 }
66 public DateTime Date;
67 public string Name;
68 public int Value;
69 const char SEP_CHAR = '|';
70 public string Serialize() {
71 return Name + SEP_CHAR + Value + SEP_CHAR + Date.ToString("O", System.Globalization.CultureInfo.InvariantCulture);
72 }
73 public static ScoreboardEntry Deserialize(string str) {
74 if (str.IndexOf(SEP_CHAR) == -1) return default;
75 string[] s = str.Split(SEP_CHAR);
76 string name = s[0];
77 int value = int.MinValue;
78 DateTime dt = DateTime.MinValue;
79 if (int.TryParse(s[1], out int p)) {
80 value = p;
81 }
82 if (s.Length > 2) {
83 dt = DateTime.ParseExact(s[2], "O", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.RoundtripKind);
84 }
85 return new ScoreboardEntry(name, value) { Date = dt };
86 }
87 }
88 public static Scoreboard Of(string name) {
89 lock (Scoreboards) {
90 if (!Directory.Exists("Hiscores"))
91 lock (_dirLock)
92 Directory.CreateDirectory("Hiscores");
93 if (Scoreboards.ContainsKey(name)) return Scoreboards[name];
94 Scoreboard s = new Scoreboard(System.IO.Path.Combine("Hiscores", name + ".txt"));
95 Scoreboards.Add(name, s);
96 return s;
97 }
98 }
99 private static object _dirLock = new object();
100 public static Dictionary<string, Scoreboard> Scoreboards { get; set; } = new Dictionary<string, Scoreboard>();
101 }
102 }