| 1 |
figdor32 |
2 |
using System; |
| 2 |
|
|
using System.Collections.Generic; |
| 3 |
|
|
using System.IO; |
| 4 |
|
|
using System.Linq; |
| 5 |
|
|
using System.Text; |
| 6 |
|
|
using System.Threading.Tasks; |
| 7 |
|
|
|
| 8 |
|
|
namespace TELTest { |
| 9 |
|
|
internal static class QuizListing { |
| 10 |
|
|
static QuizListing() { |
| 11 |
|
|
tmr.Interval = 15000; |
| 12 |
|
|
tmr.Elapsed += Tmr_Elapsed; |
| 13 |
|
|
tmr.AutoReset = false; |
| 14 |
|
|
LoadAll(); |
| 15 |
|
|
} |
| 16 |
|
|
|
| 17 |
|
|
private static void Tmr_Elapsed(Object sender, System.Timers.ElapsedEventArgs e) => DumpAll(); |
| 18 |
|
|
|
| 19 |
|
|
private static System.Timers.Timer tmr = new System.Timers.Timer(); |
| 20 |
|
|
private static void Retime() { |
| 21 |
|
|
tmr.Stop(); |
| 22 |
|
|
tmr.Start(); |
| 23 |
|
|
} |
| 24 |
|
|
public static bool UpdateEntry(string id, string name, Visibility visibility) { |
| 25 |
|
|
lock (Entries) { |
| 26 |
|
|
QuizEntry? qw = null; |
| 27 |
|
|
for (int i = 0; i < Entries.Count; i++) { |
| 28 |
|
|
if (Entries[i].ID == id) { |
| 29 |
|
|
var tmp = Entries[i]; |
| 30 |
|
|
qw = tmp; |
| 31 |
|
|
tmp.Name = name; |
| 32 |
|
|
tmp.Visibility = visibility; |
| 33 |
|
|
Entries[i] = tmp; |
| 34 |
|
|
Retime(); |
| 35 |
|
|
break; |
| 36 |
|
|
} |
| 37 |
|
|
} |
| 38 |
|
|
if (qw != null) { |
| 39 |
|
|
if (qw.Value.Visibility == Visibility.Public) { |
| 40 |
|
|
for (Int32 i = 0; i < CachePublic.Count; i++) { |
| 41 |
|
|
QuizEntry item = CachePublic[i]; |
| 42 |
|
|
if (item.ID == id) { |
| 43 |
|
|
var tmp = CachePublic[i]; |
| 44 |
|
|
tmp.Name = name; |
| 45 |
|
|
tmp.Visibility = visibility; |
| 46 |
|
|
CachePublic[i] = tmp; |
| 47 |
|
|
break; |
| 48 |
|
|
} |
| 49 |
|
|
} |
| 50 |
|
|
} |
| 51 |
|
|
} |
| 52 |
|
|
return false; |
| 53 |
|
|
} |
| 54 |
|
|
} |
| 55 |
|
|
public static bool UpdateVisibility(string id, Visibility visibility) { |
| 56 |
|
|
lock (Entries) { |
| 57 |
|
|
for (int i = 0; i < Entries.Count; i++) { |
| 58 |
|
|
if (Entries[i].ID == id) { |
| 59 |
|
|
var tmp = Entries[i]; |
| 60 |
|
|
tmp.Visibility = visibility; |
| 61 |
|
|
Entries[i] = tmp; |
| 62 |
|
|
Retime(); |
| 63 |
|
|
return true; |
| 64 |
|
|
} |
| 65 |
|
|
} |
| 66 |
|
|
return false; |
| 67 |
|
|
} |
| 68 |
|
|
} |
| 69 |
|
|
public static bool Contains(string id) { |
| 70 |
|
|
lock (Entries) { |
| 71 |
|
|
for (int i = 0; i < Entries.Count; i++) { |
| 72 |
|
|
if (Entries[i].ID == id) { |
| 73 |
|
|
return true; |
| 74 |
|
|
} |
| 75 |
|
|
} |
| 76 |
|
|
return false; |
| 77 |
|
|
} |
| 78 |
|
|
} |
| 79 |
|
|
public static bool Remove(string id) { |
| 80 |
|
|
lock (Entries) { |
| 81 |
|
|
for (int i = 0; i < Entries.Count; i++) { |
| 82 |
|
|
if (Entries[i].ID == id) { |
| 83 |
|
|
Entries.RemoveAt(i); |
| 84 |
|
|
return true; |
| 85 |
|
|
} |
| 86 |
|
|
} |
| 87 |
|
|
return false; |
| 88 |
|
|
} |
| 89 |
|
|
} |
| 90 |
|
|
public static void Insert(string id, QuizContainer container) { |
| 91 |
|
|
Insert(id, container.Title, container.Visibility); |
| 92 |
|
|
} |
| 93 |
|
|
public static void Insert(string id, string name, Visibility visibility) { |
| 94 |
|
|
lock (Entries) { |
| 95 |
|
|
var entry = new QuizEntry() { |
| 96 |
|
|
ID = id, |
| 97 |
|
|
Name = name, |
| 98 |
|
|
Visibility = visibility |
| 99 |
|
|
}; |
| 100 |
|
|
Entries.Add(entry); |
| 101 |
|
|
if (visibility == Visibility.Public) { |
| 102 |
|
|
CachePublic.Add(entry); |
| 103 |
|
|
} |
| 104 |
|
|
Retime(); |
| 105 |
|
|
} |
| 106 |
|
|
} |
| 107 |
|
|
public static void DumpAll() { |
| 108 |
|
|
Program.WriteWithTime("Begin write all listings...."); |
| 109 |
|
|
int tuplesOut = 0; |
| 110 |
|
|
lock (Entries) { |
| 111 |
|
|
using (FileStream fs = File.OpenWrite("Listing.bin")) { |
| 112 |
|
|
fs.Seek(0, SeekOrigin.Begin); |
| 113 |
|
|
BinaryWriter bw = new BinaryWriter(fs, System.Text.Encoding.UTF8, true); |
| 114 |
|
|
foreach (var item in Entries) { |
| 115 |
|
|
byte[] ib = System.Text.Encoding.ASCII.GetBytes(item.ID); |
| 116 |
|
|
bw.Write(ib.Length); |
| 117 |
|
|
bw.Write(ib); |
| 118 |
|
|
|
| 119 |
|
|
byte[] nb = System.Text.Encoding.UTF8.GetBytes(item.Name); |
| 120 |
|
|
bw.Write(nb.Length); |
| 121 |
|
|
bw.Write(nb); |
| 122 |
|
|
|
| 123 |
|
|
bw.Write((int)item.Visibility); |
| 124 |
|
|
tuplesOut++; |
| 125 |
|
|
} |
| 126 |
|
|
bw.Write(0); |
| 127 |
|
|
fs.Flush(); |
| 128 |
|
|
bw.Dispose(); |
| 129 |
|
|
} |
| 130 |
|
|
} |
| 131 |
|
|
Program.WriteWithTime("Done, {0} records out.", tuplesOut); |
| 132 |
|
|
} |
| 133 |
|
|
public static void LoadAll() { |
| 134 |
|
|
if (!File.Exists("Listing.bin")) return; |
| 135 |
|
|
using (FileStream fs = File.OpenRead("Listing.bin")) { |
| 136 |
|
|
BinaryReader bw = new BinaryReader(fs, System.Text.Encoding.UTF8, true); |
| 137 |
|
|
while(true) { |
| 138 |
|
|
QuizEntry qe = new QuizEntry(); |
| 139 |
|
|
int len = bw.ReadInt32(); |
| 140 |
|
|
if (len == 0) break; |
| 141 |
|
|
byte[] ib = bw.ReadBytes(len); |
| 142 |
|
|
qe.ID = System.Text.Encoding.ASCII.GetString(ib); |
| 143 |
|
|
|
| 144 |
|
|
len = bw.ReadInt32(); |
| 145 |
|
|
ib = bw.ReadBytes(len); |
| 146 |
|
|
qe.Name = System.Text.Encoding.UTF8.GetString(ib); |
| 147 |
|
|
|
| 148 |
|
|
qe.Visibility = (Visibility)bw.ReadInt32(); |
| 149 |
|
|
Entries.Add(qe); |
| 150 |
|
|
if (qe.Visibility == Visibility.Public) CachePublic.Add(qe); |
| 151 |
|
|
} |
| 152 |
|
|
bw.Dispose(); |
| 153 |
|
|
} |
| 154 |
|
|
} |
| 155 |
|
|
|
| 156 |
|
|
public static List<QuizEntry> CachePublic = new List<QuizEntry>(); |
| 157 |
|
|
public static List<QuizEntry> Entries = new List<QuizEntry>(); |
| 158 |
|
|
} |
| 159 |
|
|
public struct QuizEntry { |
| 160 |
|
|
public string Name; |
| 161 |
|
|
public Visibility Visibility; |
| 162 |
|
|
public string ID; |
| 163 |
|
|
|
| 164 |
|
|
} |
| 165 |
|
|
} |