/Ratings/Ratings.cs
C# | 141 lines | 119 code | 14 blank | 8 comment | 5 complexity | f2c783642d61ef4eaab2b3567a6cb7a6 MD5 | raw file
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Xml.Linq;
- using System.Xml.XPath;
- using System.Text;
- using System.Threading;
- namespace Ratings
- {
- class Ratings
- {
- private static RatingsXMLFile ratingsFile = new RatingsXMLFile();
- private static object syncObject = new Object();
-
- public static List<String> GetBySquadId(int id)
- {
- Merge("squad" + id);
- lock(syncObject)
- {
- List<String> attributeNames = new List<String>();
- attributeNames.Add("id");
- return ratingsFile.GetDataAndAttribues("squad" + id, attributeNames);
- }
- }
- public static void Merge(String parentElementName)
- {
- lock (syncObject)
- {
- var dataWithSameId = ratingsFile.GetListOfElementValuesGroupedById(parentElementName);
- // Insert new data
- foreach (List<String> dataSet in dataWithSameId)
- {
- String strToInsert = getFinalResult(dataSet);
- ratingsFile.Insert(strToInsert, parentElementName);
- }
- }
- }
- private static String getFinalResult(List<String> dataWithSameId)
- {
- // Retrieve data and sort it
- List<TimeAndEvent> splitedDataStorage = new List<TimeAndEvent>();
- foreach (String dataStr in dataWithSameId)
- {
- String[] timeAndEventStrings = dataStr.Split(':');
- TimeAndEvent timeAndEvent = new TimeAndEvent();
- timeAndEvent.Time = int.Parse(timeAndEventStrings[0]);
- timeAndEvent.Event = timeAndEventStrings[1];
- splitedDataStorage.Add(timeAndEvent);
- }
- var sortedTimeAndEventList = splitedDataStorage.OrderBy(tae => tae.Time).ToList();
- // Form result string
- String result = "";
- for (int i = sortedTimeAndEventList.Count - 1; i >= 0; i--)
- {
- TimeAndEvent timeAndEventItem = sortedTimeAndEventList[i];
- if (i == sortedTimeAndEventList.Count - 1)
- result += "Закончил на " + timeAndEventItem.Time + " секунде - " + timeAndEventItem.Event + "; ";
- else
- result += timeAndEventItem.Time + ": " + timeAndEventItem.Event + "; ";
- }
- return result;
- }
- private class TimeAndEvent
- {
- public int Time { set; get; }
- public String Event { set; get; }
- }
- /// <summary>
- /// Write record for squad i in XML file
- /// </summary>
- /// <param name="id">Squad ID. Defferent with IndexId which starts with zero</param>
- /// <param name="record">String to write into XML element</param>
- public static void WriteRecordBySquadId(int id, String record)
- {
- ParameterizedThreadStart p = new ParameterizedThreadStart(WriteRecordBySquadId);
- Thread writeThread = new Thread(p);
- ThreadParams threadParams = new ThreadParams(id, record, syncObject);
- writeThread.Start(threadParams);
- }
- private static int[] groupIds = new int[4];
- private static void WriteRecordBySquadId(object threadParams)
- {
- ThreadParams parameters = threadParams as ThreadParams;
- int squadId = parameters.squadId;
- lock (parameters.SyncObject)
- {
- String parentXMLElementName = "squad" + squadId;
- if (ratingsFile.ChildrenElementsCount(parentXMLElementName) == 10)
- ratingsFile.Delete("squad" + squadId);
- List<AttributeContainer> attributes = new List<AttributeContainer>();
- AttributeContainer attribute = new AttributeContainer("id", groupIds[squadId - 1]);
- attributes.Add(attribute);
- ratingsFile.Insert(parameters.Record, parentXMLElementName, "record", attributes);
- }
- }
- public static void SetGroupId(int group, int id)
- {
- int groupIndex = group - 1;
- try
- {
- groupIds[groupIndex] = id;
- }
- catch
- {
- throw new Exception("SetGroupId throw an exception");
- }
- }
- class ThreadParams
- {
- public int squadId { set; get; }
- public string Record { set; get; }
- public object SyncObject { set; get; }
- public ThreadParams(int id, string record, object syncObject)
- {
- this.squadId = id;
- this.Record = record;
- this.SyncObject = syncObject;
- }
- }
- public static void ResetFileAndInstance()
- {
- lock (syncObject)
- {
- ratingsFile.ResetFile();
- }
- }
- }
- }