Note: This file is out of my hands. I cannot change the format or type of file I have to parse.
Here is some sample data that I'm trying to parse. This is information for just one player:
[0] (com.riotgames.platform.gameclient.domain::PlayerParticipantStatsSummary)#8
_profileIconId = 4
elo = 0
eloChange = 0
gameId = 82736631
gameItems = (null)
inChat = false
leaver = false
leaves = 1
level = 5
losses = 2
profileIconId = 4
skinName = "Vladimir"
statistics = (mx.collections::ArrayCollection)#9
filterFunction = (null)
length = 26
list = (mx.collections::ArrayList)#10
length = 26
source = (Array)#11
[0] (com.riotgames.platform.gameclient.domain::RawStatDTO)#12
displayName = (null)
statTypeName = "PHYSICAL_DAMAGE_TAKEN"
value = 11156
[1] (com.riotgames.platform.gameclient.domain::RawStatDTO)#13
displayName = (null)
statTypeName = "TOTAL_DAMAGE_TAKEN"
value = 20653
[2] (com.riotgames.platform.gameclient.domain::RawStatDTO)#14
displayName = (null)
statTypeName = "ITEM2"
value = 3158
[3] (com.riotgames.platform.gameclient.domain::RawStatDTO)#15
displayName = (null)
statTypeName = "ITEM4"
value = 3089
[4] (com.riotgames.platform.gameclient.domain::RawStatDTO)#16
displayName = (null)
statTypeName = "WIN"
value = 1
[5] (com.riotgames.platform.gameclient.domain::RawStatDTO)#17
displayName = (null)
statTypeName = "PHYSICAL_DAMAGE_DEALT_PLAYER"
value = 18413
[6] (com.riotgames.platform.gameclient.domain::RawStatDTO)#18
displayName = (null)
statTypeName = "TOTAL_HEAL"
value = 16877
[7] (com.riotgames.platform.gameclient.domain::RawStatDTO)#19
displayName = (null)
statTypeName = "ITEM0"
value = 3083
[8] (com.riotgames.platform.gameclient.domain::RawStatDTO)#20
displayName = (null)
statTypeName = "LARGEST_CRITICAL_STRIKE"
value = 173
[9] (com.riotgames.platform.gameclient.domain::RawStatDTO)#21
displayName = (null)
statTypeName = "ITEM3"
value = 3116
[10] (com.riotgames.platform.gameclient.domain::RawStatDTO)#22
displayName = (null)
statTypeName = "ITEM1"
value = 0
[11] (com.riotgames.platform.gameclient.domain::RawStatDTO)#23
displayName = (null)
statTypeName = "GOLD_EARNED"
value = 11123
[12] (com.riotgames.platform.gameclient.domain::RawStatDTO)#24
displayName = (null)
statTypeName = "ASSISTS"
value = 8
[13] (com.riotgames.platform.gameclient.domain::RawStatDTO)#25
displayName = (null)
statTypeName = "LARGEST_MULTI_KILL"
value = 2
[14] (com.riotgames.platform.gameclient.domain::RawStatDTO)#26
displayName = (null)
statTypeName = "MAGIC_DAMAGE_DEALT_PLAYER"
value = 103124
[15] (com.riotgames.platform.gameclient.domain::RawStatDTO)#27
displayName = (null)
statTypeName = "BARRACKS_KILLED"
value = 0
[16] (com.riotgames.platform.gameclient.domain::RawStatDTO)#28
displayName = (null)
statTypeName = "LARGEST_KILLING_SPREE"
value = 5
[17] (com.riotgames.platform.gameclient.domain::RawStatDTO)#29
displayName = (null)
statTypeName = "ITEM5"
value = 0
[18] (com.riotgames.platform.gameclient.domain::RawStatDTO)#30
displayName = (null)
statTypeName = "MINIONS_KILLED"
value = 176
[19] (com.riotgames.platform.gameclient.domain::RawStatDTO)#31
displayName = (null)
statTypeName = "CHAMPIONS_KILLED"
value = 11
[20] (com.riotgames.platform.gameclient.domain::RawStatDTO)#32
displayName = (null)
statTypeName = "MAGIC_DAMAGE_TAKEN"
value = 8581
[21] (com.riotgames.platform.gameclient.domain::RawStatDTO)#33
displayName = (null)
statTypeName = "NEUTRAL_MINIONS_KILLED"
value = 12
[22] (com.riotgames.platform.gameclient.domain::RawStatDTO)#34
displayName = (null)
statTypeName = "TOTAL_TIME_SPENT_DEAD"
value = 189
[23] (com.riotgames.platform.gameclient.domain::RawStatDTO)#35
displayName = (null)
statTypeName = "TURRETS_KILLED"
value = 0
[24] (com.riotgames.platform.gameclient.domain::RawStatDTO)#36
displayName = (null)
statTypeName = "NUM_DEATHS"
value = 5
[25] (com.riotgames.platform.gameclient.domain::RawStatDTO)#37
displayName = (null)
statTypeName = "TOTAL_DAMAGE_DEALT"
value = 121538
uid = "B6F2F234-2E37-D979-E896-AA7614FCE9CB"
sort = (null)
source = (Array)#11
summonerName = "WeFearTheSun"
teamId = 100
userId = 21672484
wins = 6
Now there are ten players in a game, meaning ten of that snippet above, each one with an increasing counter, [1],[2],[3], and so on.
As you can see inside of this data there are further numbers, ie. another [0].
If the numbers were unique I could just parse the [0], then the [1], then the [2], etc.
Right now I'm parsing the files like so:
private IEnumerable<Player> GetGamePlayers(string content)
{
List<Player> Players = new List<Player>();
var location = content.IndexOf("com.riotgames.platform.gameclient.domain::EndOfGameStats");
var cutContent = content.Substring(location, 65000);
var playerOneLocation = cutContent.IndexOf("[0]");
//var playerOneContent = cutContent.Substring(playerOneLocation, 6231);
var playerOneContent = cutContent.Substring(playerOneLocation, 6255);
Players.Add(ParsePlayer(playerOneContent));
cutContent = cutContent.Substring(playerOneLocation + 6229, cutContent.Length - playerOneLocation - 6229);
var playerTwoLocation = cutContent.IndexOf("[1]");
var playerTwoContent = cutContent.Substring(playerTwoLocation, 6255);
Players.Add(ParsePlayer(playerTwoContent));
cutContent = cutContent.Substring(playerTwoLocation + 6229, cutContent.Length - playerTwoLocation - 6229);
var playerThreeLocation = cutContent.IndexOf("[2]");
var playerThreeContent = cutContent.Substring(playerThreeLocation, 6255);
Players.Add(ParsePlayer(playerThreeContent));
cutContent = cutContent.Substring(playerThreeLocation + 6229, cutContent.Length - playerThreeLocation - 6229);
var playerFourLocation = cutContent.IndexOf("[3]");
var playerFourContent = cutContent.Substring(playerFourLocation, 6255);
Players.Add(ParsePlayer(playerFourContent));
cutContent = cutContent.Substring(playerFourLocation + 6229, cutContent.Length - playerFourLocation - 6229);
var playerFiveLocation = cutContent.IndexOf("[4]");
var playerFiveContent = cutContent.Substring(playerFiveLocation, 6255);
Players.Add(ParsePlayer(playerFiveContent));
location = cutContent.IndexOf("teamPlayerParticipantStats");
cutContent = cutContent.Substring(location, 32000);
var playerSixLocation = cutContent.IndexOf("[0]");
var playerSixContent = cutContent.Substring(playerSixLocation, 6255);
Players.Add(ParsePlayer(playerSixContent));
cutContent = cutContent.Substring(playerSixLocation + 6229, cutContent.Length - playerSixLocation - 6229);
var playerSevenLocation = cutContent.IndexOf("[1]");
var playerSevenContent = cutContent.Substring(playerSevenLocation, 6255);
Players.Add(ParsePlayer(playerSevenContent));
cutContent = cutContent.Substring(playerSevenLocation + 6229, cutContent.Length - playerSevenLocation - 6229);
var playerEightLocation = cutContent.IndexOf("[2]");
var playerEightContent = cutContent.Substring(playerEightLocation, 6255);
Players.Add(ParsePlayer(playerEightContent));
cutContent = cutContent.Substring(playerEightLocation + 6229, cutContent.Length - playerEightLocation - 6229);
var playerNineLocation = cutContent.IndexOf("[3]");
var playerNineContent = cutContent.Substring(playerNineLocation, 6255);
Players.Add(ParsePlayer(playerNineContent));
cutContent = cutContent.Substring(playerNineLocation + 6229, cutContent.Length - playerNineLocation - 6229);
var playerTenLocation = cutContent.IndexOf("[4]");
var playerTenContent = cutContent.Substring(playerTenLocation, 6255);
Players.Add(ParsePlayer(playerTenContent));
return Players;
}
I can tell the code is horrible, and there is a lot of room for improvement. How would I divide the huge content into single blocks for each individual player?
I need to get the string content:
[0] (com.riotgames.platform.gameclient.domain::PlayerParticipantStatsSummary)#8
to
[1] (com.riotgames.platform.gameclient.domain::PlayerParticipantStatsSummary)#8
then:
[1] (com.riotgames.platform.gameclient.domain::PlayerParticipantStatsSummary)#8
to
[2] (com.riotgames.platform.gameclient.domain::PlayerParticipantStatsSummary)#8
List<T>object, FxCop will be very upset if you do. Also, you should check out this article by Dave Donaldson and from StackOverflow that discuss why it is a bad practice to return aList<T>. I don't disagree that returning the most specific type you can is a good practice in general, but List would not be a good candidate to return. \$\endgroup\$