| 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; |
| 7 |
|
|
using System.Threading.Tasks; |
| 8 |
|
|
using TELTest.Gamemodes; |
| 9 |
|
|
|
| 10 |
|
|
namespace TELTest { |
| 11 |
|
|
internal static class QuizDraftManager { |
| 12 |
|
|
static QuizDraftManager() { |
| 13 |
|
|
tmr.Interval = 15000; |
| 14 |
|
|
tmr.Elapsed += Tmr_Elapsed; |
| 15 |
|
|
tmr.AutoReset = false; |
| 16 |
|
|
//LoadAll(); |
| 17 |
|
|
|
| 18 |
|
|
} |
| 19 |
|
|
static BasicKeyValue bk = new BasicKeyValue("Drafts1"); |
| 20 |
|
|
private static void Tmr_Elapsed(Object sender, System.Timers.ElapsedEventArgs e) { |
| 21 |
|
|
Program.WriteWithTime("Begin write drafts..."); |
| 22 |
|
|
bk.SavePendingChanges(); |
| 23 |
|
|
Program.WriteWithTime("Done. {0} tuples out, ~{1}B memory in use.", bk.LastTuplesOut, bk.CurrentMemoryPressure); |
| 24 |
|
|
} |
| 25 |
|
|
//DumpAllDrafts(); |
| 26 |
|
|
|
| 27 |
|
|
public static bool Contains(string id) { |
| 28 |
|
|
return bk.ContainsKey(id); |
| 29 |
|
|
} |
| 30 |
|
|
private static void Retime() { |
| 31 |
|
|
tmr.Stop(); |
| 32 |
|
|
tmr.Start(); |
| 33 |
|
|
} |
| 34 |
|
|
public static string SaveNewDraft(QuizContainer container) { |
| 35 |
|
|
string id = null; |
| 36 |
|
|
while (true) { |
| 37 |
|
|
id = Utilities.RandomString(15); |
| 38 |
|
|
if (bk.ContainsKey(id)) continue; |
| 39 |
|
|
var bytes = System.Text.Encoding.UTF8.GetBytes(container.SerializeXML()); |
| 40 |
|
|
lock (bk) bk[id] = bytes; |
| 41 |
|
|
break; |
| 42 |
|
|
} |
| 43 |
|
|
Retime(); |
| 44 |
|
|
return id; |
| 45 |
|
|
} |
| 46 |
|
|
public static bool SaveDraft(QuizContainer container, string id) { |
| 47 |
|
|
//lock (AllDrafts) { |
| 48 |
|
|
// if (!AllDrafts.ContainsKey(id)) return false; |
| 49 |
|
|
// AllDrafts[id] = container; |
| 50 |
|
|
//} |
| 51 |
|
|
bk[id] = System.Text.Encoding.UTF8.GetBytes(container.SerializeXML()); |
| 52 |
|
|
Retime(); |
| 53 |
|
|
return true; |
| 54 |
|
|
} |
| 55 |
|
|
public static bool DeleteDraft(string id) { |
| 56 |
|
|
//lock (AllDrafts) { |
| 57 |
|
|
bk.Remove(id); |
| 58 |
|
|
Retime(); |
| 59 |
|
|
//return AllDrafts.Remove(id); |
| 60 |
|
|
|
| 61 |
|
|
return true; |
| 62 |
|
|
//} |
| 63 |
|
|
} |
| 64 |
|
|
public static QuizContainer GetDraft(string id) { |
| 65 |
|
|
if (bk.ContainsKey(id)) { |
| 66 |
|
|
byte[] sr = bk[id]; |
| 67 |
|
|
QuizContainer qc = QuizContainer.FromXml(System.Text.Encoding.UTF8.GetString(sr)); |
| 68 |
|
|
return qc; |
| 69 |
|
|
} |
| 70 |
|
|
return null; |
| 71 |
|
|
//lock (AllDrafts) { |
| 72 |
|
|
// if (AllDrafts.ContainsKey(id)) return AllDrafts[id]; |
| 73 |
|
|
//} |
| 74 |
|
|
//return null; |
| 75 |
|
|
} |
| 76 |
|
|
private static object _dmpLock = new object(); |
| 77 |
|
|
static System.Timers.Timer tmr = new System.Timers.Timer(); |
| 78 |
|
|
|
| 79 |
|
|
} |
| 80 |
|
|
} |