| 1 |
figdor32 |
2 |
using System; |
| 2 |
|
|
using System.Collections.Generic; |
| 3 |
|
|
using System.ComponentModel; |
| 4 |
|
|
using System.IO; |
| 5 |
|
|
using System.Linq; |
| 6 |
|
|
using System.Text; |
| 7 |
|
|
using System.Threading.Tasks; |
| 8 |
|
|
|
| 9 |
|
|
namespace TELTest.Gamemodes { |
| 10 |
|
|
internal class Comments : QuizGamemode { |
| 11 |
|
|
static Comments() { |
| 12 |
|
|
bk.MaximumMemoryPressure = 512 * 1024 * 1024; // 512MB |
| 13 |
|
|
UpdateCDB(); |
| 14 |
|
|
} |
| 15 |
|
|
static void UpdateCDB() { |
| 16 |
|
|
tmr.Interval = 15000; |
| 17 |
|
|
tmr.Elapsed += Tmr_Elapsed; |
| 18 |
|
|
tmr.AutoReset = false; |
| 19 |
|
|
} |
| 20 |
|
|
private static void Retime() { |
| 21 |
|
|
tmr.Stop(); |
| 22 |
|
|
tmr.Start(); |
| 23 |
|
|
} |
| 24 |
|
|
public Comments() { |
| 25 |
|
|
|
| 26 |
|
|
} |
| 27 |
|
|
public Comments(QuizContainer container, BaseQuizContext context) : base(container, context) { |
| 28 |
|
|
|
| 29 |
|
|
} |
| 30 |
|
|
private static System.Timers.Timer tmr = new System.Timers.Timer(); |
| 31 |
|
|
private static void Tmr_Elapsed(Object sender, System.Timers.ElapsedEventArgs e) { |
| 32 |
|
|
Program.WriteWithTime("Begin write comments..."); |
| 33 |
|
|
bk.SavePendingChanges(); |
| 34 |
|
|
Program.WriteWithTime("Done. {0} tuples out, ~{1}B memory in use.", bk.LastTuplesOut, bk.CurrentMemoryPressure); |
| 35 |
|
|
} |
| 36 |
|
|
static BasicKeyValue bk = new BasicKeyValue("Comments"); |
| 37 |
|
|
|
| 38 |
|
|
public override String Name => "Komentarze"; |
| 39 |
|
|
|
| 40 |
|
|
public override String SystemName => "Comments"; |
| 41 |
|
|
|
| 42 |
|
|
public override String Description => "Zostaw komentarz pod quizem."; |
| 43 |
|
|
|
| 44 |
|
|
public override QuizGamemode CreateInstance(QuizContainer quiz, BaseQuizContext context) => new Comments(quiz, context); |
| 45 |
|
|
public override async Task Begin() { |
| 46 |
|
|
int score = 0; |
| 47 |
|
|
await WriteLine("Tytul quizu: " + Quiz.Title); |
| 48 |
|
|
await WriteLine("Autor quizu: " + Quiz.Author); |
| 49 |
|
|
await WriteLine(); |
| 50 |
|
|
var g = Quiz.Guid.ToString(); |
| 51 |
|
|
|
| 52 |
|
|
List<string> cms = new List<string>(); |
| 53 |
|
|
recreate = true; |
| 54 |
|
|
int lastx = 0; |
| 55 |
|
|
int lasty = 2; |
| 56 |
|
|
while (true) { |
| 57 |
|
|
if (recreate) { |
| 58 |
|
|
cms.Clear(); |
| 59 |
|
|
DeserializeComs(); |
| 60 |
|
|
cms.Add("Tytul quizu: " + Quiz.Title); |
| 61 |
|
|
cms.Add("Autor quizu: " + Quiz.Author); |
| 62 |
|
|
cms.Add("Powrot..."); |
| 63 |
|
|
cms.Add("Dodaj..."); |
| 64 |
|
|
for (int i = 0; i < comments.Count; i++) { |
| 65 |
|
|
var item = comments[i]; |
| 66 |
|
|
cms.Add("[" + item.Date + "] " + item.Author + ": " + item.Content); |
| 67 |
|
|
} |
| 68 |
|
|
recreate = false; |
| 69 |
|
|
} |
| 70 |
|
|
var outp = await GUIUtilities.Create2DMenu(Context.Handler, Context.Stream, new string[][] { cms.ToArray() }, new[] { "Komentarze" }, null, lastx, lasty); |
| 71 |
|
|
if (outp.Item2 == 2) break; |
| 72 |
|
|
|
| 73 |
|
|
if (outp.Item2 == 3) { |
| 74 |
|
|
await GUIUtilities.AskUsernameIfNeeded(Context.Handler, Context.Stream); |
| 75 |
|
|
await WriteComment(); |
| 76 |
|
|
} |
| 77 |
|
|
lastx = outp.Item1; |
| 78 |
|
|
lasty = outp.Item2; |
| 79 |
|
|
} |
| 80 |
|
|
} |
| 81 |
|
|
private bool recreate = false; |
| 82 |
|
|
protected void DeserializeComs() { |
| 83 |
|
|
comments.Clear(); |
| 84 |
|
|
Byte[] com = null; |
| 85 |
|
|
lock (bk) com = bk[Quiz.Guid.ToString()]; |
| 86 |
|
|
DeserializeCommentsInner(com); |
| 87 |
|
|
comments.Reverse(); |
| 88 |
|
|
} |
| 89 |
|
|
protected void DeserializeCommentsInner(byte[] data) { |
| 90 |
|
|
if (data == null || data.Length < 4) return; |
| 91 |
|
|
using (MemoryStream ms = new MemoryStream(data)) { |
| 92 |
|
|
using (BinaryReader br = new BinaryReader(ms)) { |
| 93 |
|
|
while (true) { |
| 94 |
|
|
var al = br.ReadInt32(); |
| 95 |
|
|
if (al == 0x7FFFFFFF) break; |
| 96 |
|
|
Comment com = new Comment(); |
| 97 |
|
|
var bt = br.ReadBytes(al); |
| 98 |
|
|
com.Author = DecStr(bt); |
| 99 |
|
|
al = br.ReadInt32(); |
| 100 |
|
|
bt = br.ReadBytes(al); |
| 101 |
|
|
com.Content = DecStr(bt); |
| 102 |
|
|
int y = br.ReadInt32(); |
| 103 |
|
|
int m = br.ReadInt32(); |
| 104 |
|
|
int d = br.ReadInt32(); |
| 105 |
|
|
int h = br.ReadInt32(); |
| 106 |
|
|
int mn = br.ReadInt32(); |
| 107 |
|
|
int s = br.ReadInt32(); |
| 108 |
|
|
com.Date = new DateTime(y, m, d, h, mn, s); |
| 109 |
|
|
comments.Add(com); |
| 110 |
|
|
} |
| 111 |
|
|
} |
| 112 |
|
|
} |
| 113 |
|
|
} |
| 114 |
|
|
List<Comment> comments = new List<Comment>(); |
| 115 |
|
|
protected byte[] SerializeComments() { |
| 116 |
|
|
MemoryStream ms = new MemoryStream(); |
| 117 |
|
|
BinaryWriter bw = new BinaryWriter(ms); |
| 118 |
|
|
for (int i = 0; i < comments.Count; i++) { |
| 119 |
|
|
var item = comments[i]; |
| 120 |
|
|
var aut = EncStr(item.Author); |
| 121 |
|
|
bw.Write(aut.Length); |
| 122 |
|
|
bw.Write(aut); |
| 123 |
|
|
var cont = EncStr(item.Content); |
| 124 |
|
|
bw.Write(cont.Length); |
| 125 |
|
|
bw.Write(cont); |
| 126 |
|
|
bw.Write(item.Date.Year); |
| 127 |
|
|
bw.Write(item.Date.Month); |
| 128 |
|
|
bw.Write(item.Date.Day); |
| 129 |
|
|
bw.Write(item.Date.Hour); |
| 130 |
|
|
bw.Write(item.Date.Minute); |
| 131 |
|
|
bw.Write(item.Date.Second); |
| 132 |
|
|
} |
| 133 |
|
|
bw.Write(0x7FFFFFFF); |
| 134 |
|
|
return ms.ToArray(); |
| 135 |
|
|
} |
| 136 |
|
|
protected static byte[] EncStr(string str) { |
| 137 |
|
|
return System.Text.Encoding.UTF8.GetBytes(str); |
| 138 |
|
|
} |
| 139 |
|
|
protected static string DecStr(byte[] str) { |
| 140 |
|
|
return System.Text.Encoding.UTF8.GetString(str); |
| 141 |
|
|
} |
| 142 |
|
|
async Task WriteComment() { |
| 143 |
|
|
var res = await GUIUtilities.Promptfull(Context.Handler, Context.Stream, "Komentarz", null, 96); |
| 144 |
|
|
if (string.IsNullOrWhiteSpace(res)) return; |
| 145 |
|
|
comments.Add(new Comment() { |
| 146 |
|
|
Author = Context.Handler.PlayerName, |
| 147 |
|
|
Content = res, |
| 148 |
|
|
Date = DateTime.Now, |
| 149 |
|
|
}); |
| 150 |
|
|
var b = SerializeComments(); |
| 151 |
|
|
lock (bk) |
| 152 |
|
|
bk[Quiz.Guid.ToString()] = b; |
| 153 |
|
|
Retime(); |
| 154 |
|
|
recreate = true; |
| 155 |
|
|
} |
| 156 |
|
|
struct Comment { |
| 157 |
|
|
public DateTime Date; |
| 158 |
|
|
public string Author; |
| 159 |
|
|
public string Content; |
| 160 |
|
|
} |
| 161 |
|
|
} |
| 162 |
|
|
} |