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