-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathberries.ps1
184 lines (162 loc) · 5.78 KB
/
berries.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
function Get-AllItems {
param(
[cmdletbinding()]
[Parameter(Mandatory=$true, Position=0)]
[string]$StartingUrl
)
$response = Invoke-RestMethod -Uri $StartingUrl
[System.Collections.ArrayList]$items = $response.results
$nextUrl = $response.next
while (-not [string]::IsNullOrEmpty($nextUrl)){
$response = Invoke-RestMethod -Uri $nextUrl
$items.AddRange($response.results)
$nextUrl = $response.next
}
return $items
}
function Get-BerryData {
[cmdletbinding(DefaultParameterSetName='Url')]
param(
[Parameter(ParameterSetName='Url', Mandatory=$true, Position=0)]
[string]$Url,
[Parameter(ParameterSetName='Name', Mandatory=$true, Position=0)]
[string]$name
)
switch($PSCmdlet.ParameterSetName){
'Url' {
$response = Invoke-RestMethod -Uri $Url
}
'Name' {
$response = Invoke-RestMethod -Uri "https://pokeapi.co/api/v2/berry/$name"
}
}
$item = $response.item
$itemResponse = Invoke-RestMethod -Uri $item.url
$pokemonsHoldingBerry = $itemResponse.held_by_pokemon
$pokemons = [System.Collections.ArrayList]::new()
foreach ($pokemon in $pokemonsHoldingBerry){
$pokemonResponse = Get-PokemonData -Url $pokemon.pokemon.url
$pokemons.Add($pokemonResponse) > $null
}
$result = $response | Select-Object -Property name, firmness, flavors, natural_gift_power, natural_gift_type, item, @{N="pokemonsHoldingBerry"; E={$pokemons}}
return $result
}
function Get-BerryVitalDataList{
$berries = Get-AllItems -StartingUrl "https://pokeapi.co/api/v2/berry"
$berriesVital = [System.Collections.ArrayList]::new()
foreach($berry in $berries){
$berryData = Get-BerryData -Url $berry.url
$berriesVital.Add($berryData) > $null
}
return $berriesVital
}
function Get-ContestTypeData {
[cmdletbinding(DefaultParameterSetName='Url')]
param(
[Parameter(ParameterSetName='Url', Mandatory=$true, Position=0)]
[string]$Url,
[Parameter(ParameterSetName='Name', Mandatory=$true, Position=0)]
[string]$name
)
switch($PSCmdlet.ParameterSetName){
'Url' {
$response = Invoke-RestMethod -Uri $Url
}
'Name' {
$response = Invoke-RestMethod -Uri "https://pokeapi.co/api/v2/contest-type/$name"
}
}
$result = $response | Select-Object -Property name, berry_flavor
return $result
}
function Get-ContestVitalDataList{
$contestTypes = Get-AllItems -StartingUrl "https://pokeapi.co/api/v2/contest-type"
$contestTypesVital = [System.Collections.ArrayList]::new()
foreach($contestType in $contestTypes){
$contestTypeData = Get-ContestTypeData -Url $contestType.url
$contestTypesVital.Add($contestTypeData) > $null
}
return $contestTypesVital
}
function Group-BerriesByContestType{
$berries = Get-BerryVitalDataList
$contestTypes = Get-ContestVitalDataList
$result = @{}
foreach($contestType in $contestTypes){
[string]$berry_flavor = $contestType.berry_flavor.name.Trim()
$result[$berry_flavor] = [System.Collections.ArrayList]::new()
}
foreach($berry in $berries){
[string]$berry_best_flavor = ($berry.flavors | Sort-Object -Descending potency)[0].flavor.name.Trim()
$result[$berry_best_flavor].Add($berry) > $null
}
return $result
}
function Sort-BerriesInContestsTypeGroups{
$groupedBerries = Group-BerriesByContestType
$newGroupedBerries = @{}
foreach($entry in $groupedBerries.GetEnumerator()){
$flavor = $entry.Key
$group = $entry.Value
# Condition for sorting but no pokemons holding berries.
$group = $group | Select-Object -Property *, @{N="Potency_total"; E={$_.flavors | Select-Object -ExpandProperty potency | Measure-Object -Sum | Select-Object Sum}}| Sort-Object -Property Potency_total, natural_gift_power, natural_gift_type, pokemonsHoldingBerry -Descending | Select-Object -ExcludeProperty Potency_total
$newGroupedBerries[$flavor] = $group
}
return $newGroupedBerries
}
function Get-PokemonData{
[cmdletbinding(DefaultParameterSetName='Url')]
param(
[Parameter(ParameterSetName='Url', Mandatory=$true, Position=0)]
[string]$Url,
[Parameter(ParameterSetName='Name', Mandatory=$true, Position=0)]
[string]$name
)
switch($PSCmdlet.ParameterSetName){
'Url' {
$response = Invoke-RestMethod -Uri $Url
}
'Name' {
$response = Invoke-RestMethod -Uri "https://pokeapi.co/api/v2/pokemon/$name"
}
}
$result = $response | Select-Object -Property *, @{N="base_stats"; E={$_.stats | Select-Object -Property base_stat, @{N="stat_name"; E={$_.stat.name}}}} -ExcludeProperty stats
return $result
}
function Write-PokemonsHoldingBerry{
param(
[Parameter(Mandatory=$true)]
[System.Collections.ArrayList]$PokemonsHoldingBerry
)
foreach($pokemon in $PokemonsHoldingBerry){
Write-Output "Pokemon Name: $($pokemon.name)"
Write-Output "Base Stats:"
$pokemon.base_stats | Format-Table -AutoSize
}
}
function Write-Summary {
$contestTypes = Sort-BerriesInContestsTypeGroups
foreach($contestType in $contestTypes.getEnumerator()){
$contest = $contestType.Key
$berries = $contestType.Value | Select-Object -First 3
Write-Output "--------------------------------"
Write-Output "Contest Type: $contest"
foreach($berry in $berries){
$summaryItem = [ordered]@{
"Berry Name" = $berry.name
"Berry Firmness" = $berry.firmness.name
"Berry Flavors" = ($berry.flavors | Sort-Object -Descending potency)[0].flavor.name
"Natural Gift Power" = $berry.natural_gift_power
"Natural Gift Type" = $berry.natural_gift_type.name
}
$summaryItem | Format-Table -AutoSize
Write-Output "Pokemons Holding Berry:"
if([string]::IsNullOrEmpty($berry.PokemonsHoldingBerry)){
Write-Output "No Pokemons Holding Berry"
}else{
Write-PokemonsHoldingBerry $berry.PokemonsHoldingBerry
}
}
}
}
Write-Summary