Code: Select all
Sub PlayerData()
'routine to load all player games and their details
'written by GeneralFault
'loading all variables used in this subroutine
Dim Url_API, strPages, strPlName, strEvent, strTemp, strState, strDate As String
Dim xmlDoc As MSXML2.DOMDocument60
Dim xNode, yNode As MSXML2.IXMLDOMNode
Dim nrPages, nrGames, nrNodes, nrPlayers, nrEvents, Points As Integer
Dim h, i, j, k, m, PlayerNumber As Integer
Dim realDate As Date
'----------------------------------------Preparation----------------------------------------
'populate all relevant variables
Url_API = "http://www.conquerclub.com/api.php?mode=gamelist&un=generalfault&names=Y&gs=F&events=Y"
'preparing execution by retreiving first xml-batch
Set xmlDoc = New MSXML2.DOMDocument60
With xmlDoc
'Load the xml from CC API
.async = False
.validateOnParse = False
.Load (Url_API)
'select first node to get the number of pages
Set xNode = .FirstChild.SelectSingleNode("page")
With xNode
strPages = xNode.nodeTypedValue
'trim string to number
strPages = Mid(strPages, 5)
nrPages = CInt(strPages)
End With 'xNode pages
'select second node to get the number of games played
Set xNode = .FirstChild.SelectSingleNode("games")
With xNode
nrGames = xNode.Attributes.getNamedItem("total").NodeValue
End With 'xNode games
Worksheets("PlayerData").Select
Cells(1, 1).Value = "Number of games in total: " & nrGames
End With 'xmlDoc
'----------------------------------------Execution----------------------------------------
'setting h as counter through all games
'starting at row 4
h = 3
For i = 1 To nrPages
Url_API = "http://www.conquerclub.com/api.php?mode=gamelist&un=generalfault&names=Y&gs=F&events=Y" & Chr(38) & "page=" & i
'load the correct api in the for-loop
Set xmlDoc = New MSXML2.DOMDocument60
With xmlDoc
.async = False
.validateOnParse = False
.Load (Url_API)
Set xNode = .FirstChild.SelectSingleNode("games")
nrNodes = xNode.ChildNodes.Length
'get all childnodes to populate the details
With xNode.ChildNodes
For j = 0 To nrNodes - 1
h = h + 1
With xNode.ChildNodes(j)
'-------------------- -------------------- --------------------
Cells(h, 1).Value = .SelectSingleNode("game_number").Text
'-------------------- -------------------- --------------------
'skip one to enter player state later
'skip another one to enter player score later
'-------------------- -------------------- --------------------
strTemp = .SelectSingleNode("game_state").Text
'gs - game state (W)aiting, (A)ctive or (F)inished
Select Case strTemp
Case "W"
strTemp = "Waiting"
Case "A"
strTemp = "Active"
Case "F"
strTemp = "Finished"
Case Else
strTemp = "Unknown"
End Select
Cells(h, 4).Value = strTemp
'-------------------- -------------------- --------------------
Cells(h, 5).Value = .SelectSingleNode("tournament").Text
'-------------------- -------------------- --------------------
strTemp = .SelectSingleNode("private").Text
'private - N(Public), Y(Private) , T(Tournament)
Select Case strTemp
Case "N"
strTemp = "Public"
Case "Y"
strTemp = "Private"
Case "T"
strTemp = "Tournament"
Case Else
strTemp = "Unknown"
End Select
Cells(h, 6).Value = strTemp
'-------------------- -------------------- --------------------
strTemp = .SelectSingleNode("speed_game").Text
'speed_game - N(Casual), 1(1min Speed), 2(1min Speed), 3(1min Speed), 4(1min Speed), 5(1min Speed)
'different than the API explanation
Select Case strTemp
Case "N"
strTemp = "Casual"
Case "Y"
strTemp = "Speed"
Case "S"
strTemp = "Speed"
Case "1"
strTemp = "1 min Speed"
Case "2"
strTemp = "2 min Speed"
Case "3"
strTemp = "3 min Speed"
Case "4"
strTemp = "4 min Speed"
Case "5"
strTemp = "5 min Speed"
Case Else
strTemp = "Unknown"
End Select
Cells(h, 7).Value = strTemp
'-------------------- -------------------- --------------------
Cells(h, 8).Value = .SelectSingleNode("map").Text
'-------------------- -------------------- --------------------
strTemp = .SelectSingleNode("game_type").Text
'game_type - S(Standard), C(Terminator), A(Assassin), D(Doubles), T(Triples), Q(Quadruples), P(Polymorphic)
Select Case strTemp
Case "S"
strTemp = "Standard"
Case "C"
strTemp = "Terminator"
Case "A"
strTemp = "Assassin"
Case "D"
strTemp = "Doubles"
Case "T"
strTemp = "Triples"
Case "Q"
strTemp = "Quadruples"
Case "P"
strTemp = "Polymorphic"
Case Else
strTemp = "Unknown"
End Select
Cells(h, 9).Value = strTemp
'-------------------- -------------------- --------------------
strTemp = .SelectSingleNode("initial_troops").Text
'initial_troops - E(Automatic), M(Manual)
Select Case strTemp
Case "E"
strTemp = "Automatic"
Case "M"
strTemp = "Manual"
Case Else
strTemp = "Unknown"
End Select
Cells(h, 10).Value = strTemp
'-------------------- -------------------- --------------------
strTemp = .SelectSingleNode("play_order").Text
'play_order - S(Sequential), F(Freestyle)
Select Case strTemp
Case "S"
strTemp = "Sequential"
Case "F"
strTemp = "Freestyle"
Case Else
strTemp = "Unknown"
End Select
Cells(h, 11).Value = strTemp
'-------------------- -------------------- --------------------
strTemp = .SelectSingleNode("bonus_cards").Text
'bonus_cards - 2 (Escalating) , 3 (Flat Rate) , 1 (No Spoils), 4(Nuclear), 5(Zombie)
Select Case strTemp
Case "1"
strTemp = "No Spoils"
Case "2"
strTemp = "Escalating"
Case "3"
strTemp = "Flat Rate"
Case "4"
strTemp = "Nuclear"
Case "5"
strTemp = "Zombie"
Case Else
strTemp = "Unknown"
End Select
Cells(h, 12).Value = strTemp
'-------------------- -------------------- --------------------
strTemp = .SelectSingleNode("fortifications").Text
'fortifications - C(Chained), O(Adjaecent), M(Unlimited), P(Parachute), N(None)
Select Case strTemp
Case "C"
strTemp = "Chained"
Case "O"
strTemp = "Adjacent"
Case "M"
strTemp = "Unlimited"
Case "P"
strTemp = "Parachute"
Case "N"
strTemp = "None"
Case Else
strTemp = "Unknown"
End Select
Cells(h, 13).Value = strTemp
'-------------------- -------------------- --------------------
strTemp = .SelectSingleNode("war_fog").Text
'war_fog - N(No Fog) or Y(Fog)
Select Case strTemp
Case "N"
strTemp = "No Fog"
Case "Y"
strTemp = "Fog"
Case Else
strTemp = "Unknown"
End Select
Cells(h, 14).Value = strTemp
'-------------------- -------------------- --------------------
strTemp = .SelectSingleNode("trench_warfare").Text
'trench_warfare - (Y)es, (N)o
Select Case strTemp
Case "Y"
strTemp = "Trench"
Case "N"
strTemp = "No Trench"
Case Else
strTemp = "Unknown"
End Select
Cells(h, 15).Value = strTemp
'-------------------- -------------------- --------------------
strTemp = .SelectSingleNode("round_limit").Text
'round_limit - 0, 20, 30, 50, 100
Select Case strTemp
Case "0"
strTemp = "No limit"
Case "20"
strTemp = "20 rounds"
Case "30"
strTemp = "30 rounds"
Case "50"
strTemp = "50 rounds"
Case "100"
strTemp = "100 rounds"
Case Else
strTemp = "Unknown"
End Select
Cells(h, 16).Value = strTemp
'-------------------- -------------------- --------------------
Cells(h, 17).Value = .SelectSingleNode("round").Text
'-------------------- -------------------- --------------------
Cells(h, 18).Value = .SelectSingleNode("poly_slots").Text
'-------------------- -------------------- --------------------
strDate = .SelectSingleNode("time_remaining").Text
If strDate = 0 And Cells(h, 4).Value = "Finished" Then
Cells(h, 18).Value = Finished
Else
'calculate later
End If
'-------------------- -------------------- --------------------
'skip one for date
'-------------------- -------------------- --------------------
'childnode(18) is the collection of players
With xNode.ChildNodes(j).ChildNodes(18)
k = 22
nrPlayers = xNode.ChildNodes(j).ChildNodes(18).ChildNodes.Length
Cells(h, 21).Value = nrPlayers
PlayerNumber = 99
For m = 0 To nrPlayers - 1
strPlName = .ChildNodes(m).nodeTypedValue
If strPlName = "GeneralFault" Then
strState = .ChildNodes(m).Attributes.getNamedItem("state").Text
Cells(h, 2).Value = strState
If PlayerNumber = 99 Then
PlayerNumber = m + 1
Else
'playernumber is already assigned (polymorphic)
End If
Else
'excel cannot handle usernames starting with an =
strTemp = Left(strPlName, 1)
If strTemp = "=" Then
strPlName = " " & strPlName
Else
'do nothing
End If
Cells(h, k).Value = strPlName
k = k + 1
End If
Next
End With
'-------------------- -------------------- --------------------
'childnode(19) is the collection of events
With xNode.ChildNodes(j).ChildNodes(19)
nrEvents = xNode.ChildNodes(j).ChildNodes(19).ChildNodes.Length
For m = 0 To nrEvents - 1
strEvent = xNode.ChildNodes(j).ChildNodes(19).ChildNodes(m).nodeTypedValue
strDate = .ChildNodes(m).Attributes.getNamedItem("timestamp").Text
realDate = DateAdd("s", strDate, "01/01/1970") ' 00:00:00
strTemp = Len(CStr(PlayerNumber))
If strTemp = 1 Then
'playernumber has one digit, select the correct eventstrings
If Left(strEvent, 1) = PlayerNumber Then
If strState = "Won" Then
strTemp = PlayerNumber & " gains "
If strTemp = Left(strEvent, 8) Then
'remove first 8 characters from string and last 7 characters
strEvent = Mid(strEvent, 8)
strEvent = Left(strEvent, Len(strEvent) - 7)
Points = CInt(strEvent)
Cells(h, 3).Value = Points
Cells(h, 20).Value = realDate
Else
'do nothing
End If
Else
'state is lost
strTemp = PlayerNumber & " loses "
If strTemp = Left(strEvent, 8) Then
'remove first 8 characters from string and last 7 characters
strEvent = Mid(strEvent, 8)
strEvent = "-" & Left(strEvent, Len(strEvent) - 7)
Points = CInt(strEvent)
Cells(h, 3).Value = Points
Cells(h, 20).Value = realDate
Else
'do nothing
End If
End If
Else
'eventstring is not relevant for selected player
End If
Else
'playernumber has two digits, select the correct eventstrings
If Left(strEvent, 2) = PlayerNumber Then
If strState = "Won" Then
strTemp = PlayerNumber & " gains "
If strTemp = Left(strEvent, 9) Then
'remove first 9 characters from string and last 7 characters
strEvent = Mid(strEvent, 9)
strEvent = Left(strEvent, Len(strEvent) - 7)
Points = CInt(strEvent)
Cells(h, 3).Value = Points
Cells(h, 20).Value = realDate
Else
'do nothing
End If
Else
'state is lost
strTemp = PlayerNumber & " loses "
If strTemp = Left(strEvent, 9) Then
'remove first 9 characters from string and last 7 characters
strEvent = Mid(strEvent, 9)
strEvent = "-" & Left(strEvent, Len(strEvent) - 7)
Points = CInt(strEvent)
Cells(h, 3).Value = Points
Cells(h, 20).Value = realDate
Else
'do nothing
End If
End If
Else
'eventstring is not relevant for selected player
End If
End If
'change cell with date to mm/dd/yyyy ATTENTION: DUTCH SETTINGS NOW
Cells(h, 20).NumberFormat = "dd/mm/yyyy"
Next
'-------------------- -------------------- --------------------
End With
End With 'xnode.childNodes
Next
End With 'xnode
End With 'xmldoc
Next
MsgBox "done"
End Sub