PageRenderTime 97ms CodeModel.GetById 30ms RepoModel.GetById 11ms app.codeStats 0ms

/Ratings/Ratings.cs

https://github.com/Dracontis/Ratings
C# | 141 lines | 119 code | 14 blank | 8 comment | 5 complexity | f2c783642d61ef4eaab2b3567a6cb7a6 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Xml.Linq;
  5. using System.Xml.XPath;
  6. using System.Text;
  7. using System.Threading;
  8. namespace Ratings
  9. {
  10. class Ratings
  11. {
  12. private static RatingsXMLFile ratingsFile = new RatingsXMLFile();
  13. private static object syncObject = new Object();
  14. public static List<String> GetBySquadId(int id)
  15. {
  16. Merge("squad" + id);
  17. lock(syncObject)
  18. {
  19. List<String> attributeNames = new List<String>();
  20. attributeNames.Add("id");
  21. return ratingsFile.GetDataAndAttribues("squad" + id, attributeNames);
  22. }
  23. }
  24. public static void Merge(String parentElementName)
  25. {
  26. lock (syncObject)
  27. {
  28. var dataWithSameId = ratingsFile.GetListOfElementValuesGroupedById(parentElementName);
  29. // Insert new data
  30. foreach (List<String> dataSet in dataWithSameId)
  31. {
  32. String strToInsert = getFinalResult(dataSet);
  33. ratingsFile.Insert(strToInsert, parentElementName);
  34. }
  35. }
  36. }
  37. private static String getFinalResult(List<String> dataWithSameId)
  38. {
  39. // Retrieve data and sort it
  40. List<TimeAndEvent> splitedDataStorage = new List<TimeAndEvent>();
  41. foreach (String dataStr in dataWithSameId)
  42. {
  43. String[] timeAndEventStrings = dataStr.Split(':');
  44. TimeAndEvent timeAndEvent = new TimeAndEvent();
  45. timeAndEvent.Time = int.Parse(timeAndEventStrings[0]);
  46. timeAndEvent.Event = timeAndEventStrings[1];
  47. splitedDataStorage.Add(timeAndEvent);
  48. }
  49. var sortedTimeAndEventList = splitedDataStorage.OrderBy(tae => tae.Time).ToList();
  50. // Form result string
  51. String result = "";
  52. for (int i = sortedTimeAndEventList.Count - 1; i >= 0; i--)
  53. {
  54. TimeAndEvent timeAndEventItem = sortedTimeAndEventList[i];
  55. if (i == sortedTimeAndEventList.Count - 1)
  56. result += "Закончил на " + timeAndEventItem.Time + " секунде - " + timeAndEventItem.Event + "; ";
  57. else
  58. result += timeAndEventItem.Time + ": " + timeAndEventItem.Event + "; ";
  59. }
  60. return result;
  61. }
  62. private class TimeAndEvent
  63. {
  64. public int Time { set; get; }
  65. public String Event { set; get; }
  66. }
  67. /// <summary>
  68. /// Write record for squad i in XML file
  69. /// </summary>
  70. /// <param name="id">Squad ID. Defferent with IndexId which starts with zero</param>
  71. /// <param name="record">String to write into XML element</param>
  72. public static void WriteRecordBySquadId(int id, String record)
  73. {
  74. ParameterizedThreadStart p = new ParameterizedThreadStart(WriteRecordBySquadId);
  75. Thread writeThread = new Thread(p);
  76. ThreadParams threadParams = new ThreadParams(id, record, syncObject);
  77. writeThread.Start(threadParams);
  78. }
  79. private static int[] groupIds = new int[4];
  80. private static void WriteRecordBySquadId(object threadParams)
  81. {
  82. ThreadParams parameters = threadParams as ThreadParams;
  83. int squadId = parameters.squadId;
  84. lock (parameters.SyncObject)
  85. {
  86. String parentXMLElementName = "squad" + squadId;
  87. if (ratingsFile.ChildrenElementsCount(parentXMLElementName) == 10)
  88. ratingsFile.Delete("squad" + squadId);
  89. List<AttributeContainer> attributes = new List<AttributeContainer>();
  90. AttributeContainer attribute = new AttributeContainer("id", groupIds[squadId - 1]);
  91. attributes.Add(attribute);
  92. ratingsFile.Insert(parameters.Record, parentXMLElementName, "record", attributes);
  93. }
  94. }
  95. public static void SetGroupId(int group, int id)
  96. {
  97. int groupIndex = group - 1;
  98. try
  99. {
  100. groupIds[groupIndex] = id;
  101. }
  102. catch
  103. {
  104. throw new Exception("SetGroupId throw an exception");
  105. }
  106. }
  107. class ThreadParams
  108. {
  109. public int squadId { set; get; }
  110. public string Record { set; get; }
  111. public object SyncObject { set; get; }
  112. public ThreadParams(int id, string record, object syncObject)
  113. {
  114. this.squadId = id;
  115. this.Record = record;
  116. this.SyncObject = syncObject;
  117. }
  118. }
  119. public static void ResetFileAndInstance()
  120. {
  121. lock (syncObject)
  122. {
  123. ratingsFile.ResetFile();
  124. }
  125. }
  126. }
  127. }