1: using System;
2: using System.Linq;
3: using System.Data;
4: using System.Collections;
5: using System.Collections.Generic;
6:
7: namespace Ia.TentPlay.Cl.Model.Memorise
8: {
9: ////////////////////////////////////////////////////////////////////////////
10:
11: /// <summary publish="true">
12: /// Morse code class
13: /// </summary>
14: /// <value>
15: /// https://msdn.microsoft.com/en-us/library/z1hkazw7(v=vs.100).aspx
16: /// </value>
17: /// <remarks>
18: /// Copyright © 2008-2018 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
19: ///
20: /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
21: /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
22: ///
23: /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
24: /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
25: ///
26: /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
27: ///
28: /// Copyright notice: This notice may not be removed or altered from any source distribution.
29: /// </remarks>
30: public class MorseCode : Test
31: {
32: private Dictionary<string, string> internationalMorseCodeDicitonary, reverseInternationalMorseCodeDicitonary;
33:
34: protected override TestTopic testTopic
35: {
36: get
37: {
38: return Ia.TentPlay.Cl.Model.Memorise.Test.TestTopic.MorseCode;
39: }
40: }
41:
42: ////////////////////////////////////////////////////////////////////////////
43:
44: /// <summary>
45: ///
46: /// </summary>
47: public enum DirectionType { MorseCodeToAscii = 1, AsciiToMorseCode = 2 };
48:
49: ////////////////////////////////////////////////////////////////////////////
50:
51: /// <summary>
52: ///
53: /// </summary>
54: public MorseCode() { }
55:
56: ////////////////////////////////////////////////////////////////////////////
57:
58: /// <summary>
59: ///
60: /// </summary>
61: public override void PopulateTestDatabaseTableWithInitialQuestionsIfEmpty(Guid userId)
62: {
63: int count;
64: Ia.TentPlay.Cl.Model.Memorise.Score score;
65:
66: using (var db = new Ia.TentPlay.Db())
67: {
68: count = (from s in db.Scores where s.TestId == (int)Ia.TentPlay.Cl.Model.Memorise.Test.TestTopic.MorseCode select s).Count();
69:
70: if (count == 0)
71: {
72: // MorseCodeToAscii
73: foreach (KeyValuePair<string, string> kvp in InternationalMorseCodeDicitonary)
74: {
75: score = new Ia.TentPlay.Cl.Model.Memorise.Score();
76:
77: score.Question = kvp.Key;
78: score.Answer = kvp.Value;
79: score.TestId = (int)testTopic;
80: score.TypeId = (int)Ia.TentPlay.Cl.Model.Memorise.MorseCode.DirectionType.MorseCodeToAscii;
81: score.Created = score.Updated = score.Viewed = DateTime.UtcNow.AddHours(3);
82: score.UserId = userId;
83:
84: db.Scores.Add(score);
85: }
86:
87: // AsciiToMorseCode
88: foreach (KeyValuePair<string, string> kvp in ReverseInternationalMorseCodeDicitonary)
89: {
90: score = new Ia.TentPlay.Cl.Model.Memorise.Score();
91:
92: score.Question = kvp.Key;
93: score.Answer = kvp.Value;
94: score.TestId = (int)testTopic;
95: score.TypeId = (int)DirectionType.AsciiToMorseCode;
96: score.Created = score.Updated = score.Viewed = DateTime.UtcNow.AddHours(3);
97: score.UserId = userId;
98:
99: db.Scores.Add(score);
100: }
101:
102: db.SaveChanges();
103: }
104: }
105: }
106:
107: ////////////////////////////////////////////////////////////////////////////
108:
109: /// <summary>
110: ///
111: /// </summary>
112: public override int WeightedRandomTypeIdAccordingTypeDistribution(Guid userId)
113: {
114: int countOfAllTypes, countOfTypeId1, countOfTypeId2, typeId;
115: ArrayList countArrayList;
116:
117: using (var db = new Ia.TentPlay.Db())
118: {
119: countOfAllTypes = (from s in db.Scores where s.TestId == (int)testTopic select s).Count();
120: countOfTypeId1 = (from s in db.Scores where s.TestId == (int)testTopic && s.TypeId == (int)DirectionType.AsciiToMorseCode select s).Count();
121: countOfTypeId2 = (from s in db.Scores where s.TestId == (int)testTopic && s.TypeId == (int)DirectionType.MorseCodeToAscii select s).Count();
122:
123: countArrayList = new ArrayList(countOfAllTypes);
124:
125: // below: populate ArrayList with TypeId keys a number of times equal to its count
126: for (int i = 0; i < countOfTypeId1; i++) countArrayList.Add((int)DirectionType.AsciiToMorseCode);
127: for (int i = 0; i < countOfTypeId2; i++) countArrayList.Add((int)DirectionType.MorseCodeToAscii);
128:
129: if (countArrayList.Count > 0)
130: {
131: typeId = (int)countArrayList[random.Next(countOfAllTypes)];
132: }
133: else
134: {
135: typeId = (Ia.Cl.Model.Default.RandomBool) ? (int)DirectionType.AsciiToMorseCode : (int)DirectionType.MorseCodeToAscii;
136: }
137: }
138:
139: return typeId;
140: }
141:
142: ////////////////////////////////////////////////////////////////////////////
143:
144: /// <summary>
145: ///
146: /// <see cref="https://en.wikipedia.org/wiki/Morse_code"/>
147: /// </summary>
148: public Dictionary<string, string> InternationalMorseCodeDicitonary
149: {
150: get
151: {
152: /*
153: * https://en.wikipedia.org/wiki/Morse_code
154: *
155: * International Morse Code
156: *
157: * 1. The length of a dot is one unit
158: * 2. A dash is three units
159: * 3. The space between parts of the same letter is one unit.
160: * 4. The space between letters is three units.
161: * 5. The space between words is seven units.
162: */
163:
164: if (internationalMorseCodeDicitonary == null || internationalMorseCodeDicitonary.Count == 0)
165: {
166: internationalMorseCodeDicitonary = new Dictionary<string, string>(50);
167:
168: internationalMorseCodeDicitonary["A"] = ".-";
169: internationalMorseCodeDicitonary["B"] = "-...";
170: internationalMorseCodeDicitonary["C"] = "-.-.";
171: internationalMorseCodeDicitonary["D"] = "-..";
172: internationalMorseCodeDicitonary["E"] = ".";
173: internationalMorseCodeDicitonary["F"] = "..-.";
174: internationalMorseCodeDicitonary["G"] = "--.";
175: internationalMorseCodeDicitonary["H"] = "....";
176: internationalMorseCodeDicitonary["I"] = "..";
177: internationalMorseCodeDicitonary["J"] = ".---";
178: internationalMorseCodeDicitonary["K"] = "-.-";
179: internationalMorseCodeDicitonary["L"] = ".-..";
180: internationalMorseCodeDicitonary["M"] = "--";
181: internationalMorseCodeDicitonary["N"] = "-.";
182: internationalMorseCodeDicitonary["O"] = "---";
183: internationalMorseCodeDicitonary["P"] = ".--.";
184: internationalMorseCodeDicitonary["Q"] = "--.-";
185: internationalMorseCodeDicitonary["R"] = ".-.";
186: internationalMorseCodeDicitonary["S"] = "...";
187: internationalMorseCodeDicitonary["T"] = "-";
188: internationalMorseCodeDicitonary["U"] = "..-";
189: internationalMorseCodeDicitonary["V"] = "...-";
190: internationalMorseCodeDicitonary["W"] = ".--";
191: internationalMorseCodeDicitonary["X"] = "-..-";
192: internationalMorseCodeDicitonary["Y"] = "-.--";
193: internationalMorseCodeDicitonary["Z"] = "--..";
194:
195: internationalMorseCodeDicitonary["1"] = ".----";
196: internationalMorseCodeDicitonary["2"] = "..---";
197: internationalMorseCodeDicitonary["3"] = "...--";
198: internationalMorseCodeDicitonary["4"] = "....-";
199: internationalMorseCodeDicitonary["5"] = ".....";
200: internationalMorseCodeDicitonary["6"] = "-....";
201: internationalMorseCodeDicitonary["7"] = "--...";
202: internationalMorseCodeDicitonary["8"] = "---..";
203: internationalMorseCodeDicitonary["9"] = "----.";
204: internationalMorseCodeDicitonary["0"] = "-----";
205:
206: }
207: else
208: {
209:
210: }
211:
212: return internationalMorseCodeDicitonary;
213: }
214: }
215:
216: ////////////////////////////////////////////////////////////////////////////
217:
218: /// <summary>
219: ///
220: /// </summary>
221: public Dictionary<string, string> ReverseInternationalMorseCodeDicitonary
222: {
223: get
224: {
225: if (reverseInternationalMorseCodeDicitonary == null || reverseInternationalMorseCodeDicitonary.Count == 0)
226: {
227: reverseInternationalMorseCodeDicitonary = new Dictionary<string, string>(50);
228:
229: foreach (KeyValuePair<string, string> kvp in InternationalMorseCodeDicitonary) reverseInternationalMorseCodeDicitonary[kvp.Value] = kvp.Key;
230: }
231: else
232: {
233:
234: }
235:
236: return reverseInternationalMorseCodeDicitonary;
237: }
238: }
239:
240: ////////////////////////////////////////////////////////////////////////////
241: ////////////////////////////////////////////////////////////////////////////
242: }
243:
244: ////////////////////////////////////////////////////////////////////////////
245: ////////////////////////////////////////////////////////////////////////////
246: }