Skip to content

Instantly share code, notes, and snippets.

@mfukar
Last active February 2, 2022 03:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mfukar/623ac05dafa9c8015f3fb5302f777a81 to your computer and use it in GitHub Desktop.
Save mfukar/623ac05dafa9c8015f3fb5302f777a81 to your computer and use it in GitHub Desktop.
[Tyrant Unleashed] Download your cards into a ownedcards.txt file, and your decks into a currentdecks.txt, for use with the optimiser. From your browser, get the response from the api.php?message=init request into a file named json.txt in the /data/ directory of the optimiser, along with this script. Reqs Powershell 7. Will create two files in t…
$stopwatch = [Diagnostics.Stopwatch]::StartNew()
if (-not (Test-Path "json.txt")) {
Write-Host "[-] To update owned cards, supply the response to api.php?message=init in json.txt"
exit 1
}
$account_details = Get-Content "json.txt" | ConvertFrom-JSON -AsHashtable
if (-not (Test-Path "cards_section*.xml")) {
Write-Host "[-] Run this script inside the optimiser's /data/ directory"
exit 2
}
$card_definition_XMLs = Get-ChildItem "cards_section*.xml"
# Hash the XMLs
$checksums = @()
$card_definition_XMLs | ForEach-Object -Begin { $counter = 0 } -Process {
$fname = $_
$checksums += (Get-FileHash -Path $fname.Name -Algorithm SHA256).Hash
$counter += 1
Write-Progress -Activity "Updating" -Status "Verifying card definitions" -PercentComplete ($counter / $card_definition_XMLs.Count * 100)
}
# Compare hashes of cards_section*.xml - only update if modifications exist:
if ((Test-Path "xml-checksums.txt") -and ((Compare-Object -ReferenceObject (Import-Clixml -Path "xml-checksums.txt") -DifferenceObject $checksums) -eq $null)) {
Write-Host "[+] cards_section*.xml are identical, not reparsing.."
$are_cards_updated = false
} else {
Write-Host "[!] cards_section*.xml have changed. Reparsing.."
$checksums | Export-Clixml -Path "xml-checksums.txt"
$are_cards_updated = true
}
# Import card data:
$carddata = Import-Clixml -Path all-card-data.xml
if ($are_cards_updated -or $carddata -eq $null) {
$xmlcardslist = @()
$card_definition_XMLs | ForEach-Object -Begin { $counter = 0 } -Process {
$xmlcardslist += [xml](Get-Content -ReadCount 0 $_)
$counter += 1
Write-Progress -Activity "Updating" -Status "Loading card definitions" -PercentComplete ($counter / $card_definition_XMLs.Count * 100)
}
$carddata = @{}
$xmlcardslist | ForEach-Object -Begin { $counter = 0 } -Process {
$list = $_
foreach ($card in $list.root.unit) {
$name = $card.name
$carddata.add([int]$card.id, $name + "-1")
if (($card.selectnodes("upgrade") | measure).count -gt 0) {
foreach ($upg in $card.upgrade) {
$carddata.add([int]$upg.card_id, ($name + "-" + $upg.level))
}
}
}
$counter += 1
Write-Progress -Activity "Updating" -Status "Generating cards" -PercentComplete ($counter / $xmlcardslist.Count * 100)
}
# Store card data:
$carddata | Export-Clixml -Path all-card-data.xml
}
# Build & make a request:
$request = $account_details.request
$fields = New-Object System.Collections.Specialized.NameValueCollection
$fields.Add("password", $request.password)
$fields.Add("user_id", $request.user_id)
$fields.Add("hash", $request.hash)
$fields.Add("syncode", $request.syncode)
$fields.Add("kong_id", $request.kong_id)
$fields.Add("kong_token", $request.kong_token)
$fields.Add("kong_name", $request.kong_name)
############################################################
# If you are behind a proxy, un-comment the following lines:
# $proxy = new-object System.Net.WebProxy "myproxy:port"
# $webclient.UseDefaultCredentials = $true
# $proxy.Credentials = $webclient.Credentials
# $webclient.proxy=$proxy
############################################################
$url = "https://mobile.tyrantonline.com/api.php?message=init&user_id=" + $request.user_id
$webclient = New-Object System.Net.WebClient
try {
$req_stopwatch = [Diagnostics.Stopwatch]::StartNew()
$req = $webclient.UploadValues($url, $fields)
$req_stopwatch.stop()
Write-Host $([string]::Format("[?] Request finished in {0:ss\.fff} sec", $req_stopwatch.elapsed))
}
catch [system.net.WebException] {
Write-Host "[-] Unable to open stream or no response."
exit 3
}
$x = (New-Object Text.UTF8Encoding).GetString($req)
$x | Out-File -Encoding "ASCII" "json.txt"
# Create a temporary file, mark it with date in case it needs to be debugged:
Set-Content -Path "./_ownedcards.txt" -Value "// $(Get-Date)"
$response_dict = ConvertFrom-JSON -AsHashtable -InputObject $x
$cards = $response_dict.user_cards
Write-Host "[?] Response has $($cards.Count) cards"
$text = ""
$cards.GetEnumerator() | ForEach-Object -Begin { $counter = 0 } -Process {
$id = $_.name
if ($cards[$id].num_owned -lt 1) {
$counter += 1
Write-Progress -Activity "Updating" -Status "Saving cards" -PercentComplete ($counter / $cards.Count * 100)
return
}
# The card definitions are keyed with int instead of string:
$text += $carddata.get_item([int]$id)
# The optimiser accepts multiples of a card in a deck as #N,
# but in ownedcards.txt as (N) ...
if ($cards[$id].num_owned -gt 1) {
$text += " (" + $cards[$id].num_owned + ")"
}
# The game XML uses "-N" for maxed cards,
# but the optimiser wants no suffix...
$text = $text.replace("-6", "")
$text += "`r`n"
$counter += 1
Write-Progress -Activity "Updating" -Status "Saving cards" -PercentComplete ($counter / $cards.Count * 100)
} -End {
Write-Host -NoNewline "[+] Ready to write $($counter) cards..."
}
Out-File -Encoding "ASCII" -InputObject $text -Append "_ownedcards.txt"
# Create or replace the content of ownedcards.txt if it exists:
Get-Content _ownedcards.txt | Set-Content ownedcards.txt
Remove-Item "_ownedcards.txt"
Write-Host "done."
# Create a file for decks, mark it with date in case it needs to be debugged:
Set-Content -Path "./currentdecks.txt" -Value "// $(Get-Date)"
Write-Host "[?] Response has $($response_dict.user_decks.Count) decks"
$decks = $response_dict.user_decks
$text = ""
$decks.GetEnumerator() | ForEach-Object -Begin { $counter = 0 } -Process {
$id = $_.name
$deck = $decks[$id]
# Commander:
$text += $carddata.get_item([int]$deck.commander_id)
# Dominion:
$text += ","
$text += $carddata.get_item([int]$deck.dominion_id)
foreach ($card_id in $deck.cards.Keys) {
# Card
$text += ","
$text += $carddata.get_item([int]$card_id)
if ($deck.cards[$card_id] -gt 1) {
$text += " #$($deck.cards[$card_id])"
}
}
$text += "`r`n"
$counter += 1
Write-Progress -Activity "Updating" -Status "Saving decks" -PercentComplete ($counter / $response_dict.user_decks.Count * 100)
} -End {
Write-Host -NoNewline "[+] Ready to write $($counter) decks..."
}
Out-File -Encoding "ASCII" -InputObject $text -Append "currentdecks.txt"
Write-Host "done."
$stopwatch.stop()
Write-Host $([string]::Format("[?] Finished in {0:ss\.fff} :-)", $stopwatch.elapsed))
Write-Progress -Activity "Updating" -Status "Done" -PercentComplete 100
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment