-
Notifications
You must be signed in to change notification settings - Fork 1
/
Set-FplLineup.ps1
165 lines (140 loc) · 6.31 KB
/
Set-FplLineup.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
function Set-FplLineup {
<#
.SYNOPSIS
Set your team lineup for the upcoming gameweek
.DESCRIPTION
Set your team lineup for the upcoming gameweek
.PARAMETER PlayersIn
The players which you wish to bring in to the starting XI.
Alternatively, if you are just swapping the order of players on your bench then use PlayersIn for one bench player and PlayersOut for the other
This parameter takes multiple types of input:
It can be passed as a string
`'Salah'`
It can be passed as a player ID
`253`
It can be passed as a hashtable of properties i.e.
`@{Name = 'Salah'; Club = 'Liverpool'; Position = 'Midfeilder'; PlayerID = 253}`
The only allowed properties are Name, Club, Position, PlayerID
It can be the output of Get-FplPlayer or Get-FplLineup
.PARAMETER PlayersOut
The players you wish to remove from the starting XI.
Alternatively, if you are just swapping the order of players on your bench then use
PlayersIn for one bench player and PlayersOut for the other.
This parameter takes multiple types of input:
It can be passed as a string
`'Salah'`
It can be passed as a player ID
`253`
It can be passed as a hashtable of properties i.e.
`@{Name = 'Salah'; Club = 'Liverpool'; Position = 'Midfeilder'; PlayerID = 253}`
The only allowed properties are Name, Club, Position, PlayerID
It can be the output of Get-FplPlayer or Get-FplLineup
.PARAMETER Captain
The player who you wish to be Captain of your team
.PARAMETER ViceCaptain
The player who you wish to be ViceCaptain of your team
.EXAMPLE
Set-FplLineup -PlayersIn Son -PlayersOut Pogba
This will remove Pogba from the starting XI and swap him with Son
.EXAMPLE
Set-FplLineup -PlayersIn Son, Rashford, Alexander-Arnold -PlayersOut Ings, Diop, Digne
This will remove Ings, Diop and Digne from the starting XI and swap them with Son, Rashford and Alexander-Arnold
.EXAMPLE
Set-FplLineup -PlayersIn @{Name = 'Sterling'; Club = 'Man City'} -PlayersOut Mane
You can use a hashtable to identify players if you have two players with the same name in your team.
.EXAMPLE
Set-FplLineup -Captain Salah
This will set your captain for the upcoming gameweek to Salah
.EXAMPLE
Set-FplLineup -ViceCaptain Sterling
This will set your vice captain for the upcoming gameweek to Sterling
.EXAMPLE
Set-FplLinup -PlayersIn Sane -PlayersOut Diop -Captain Sane -ViceCaptain Pogba
This will swap out Diop for Sane in your starting XI and then set Sane to be the captain and Pogba to be the vice captain
.LINK
https://psfpl.readthedocs.io/en/master/functions/Set-FplLineup
.LINK
https://github.com/sk82jack/PSFPL/blob/master/PSFPL/Public/Set-FplLineup.ps1
#>
[CmdletBinding()]
param (
[Parameter()]
[ValidateCount(0, 4)]
[PlayerTransformAttribute()]
[PSObject[]]
$PlayersIn,
[Parameter()]
[ValidateCount(0, 4)]
[PlayerTransformAttribute()]
[PSObject[]]
$PlayersOut,
[Parameter()]
[PlayerTransformAttribute()]
$Captain,
[Parameter()]
[PlayerTransformAttribute()]
$ViceCaptain
)
if (-not ($PlayersIn -or $PlayersOut -or $Captain -or $ViceCaptain)) {
Write-Error -Message 'Please specify a change to make' -ErrorAction 'Stop'
}
if ($PlayersIn.Count -ne $PlayersOut.Count) {
Write-Error 'You must provide the same number of players coming into the starting lineup as there are players going out' -ErrorAction 'Stop'
}
if ((-not $Script:FplSessionData) -or (-not $Script:FplSessionData['FplSession'])) {
Write-Warning 'No existing connection found'
$Credential = Get-Credential -Message 'Please enter your FPL login details'
Connect-Fpl -Credential $Credential
}
$Lineup = Get-FplLineup
$Players = [PSCustomObject]@{
In = Find-FplPlayer -PlayerTransform $PlayersIn -FplPlayerCollection $Lineup
Out = Find-FplPlayer -PlayerTransform $PlayersOut -FplPlayerCollection $Lineup
Captain = Find-FplPlayer -PlayerTransform $Captain -FplPlayerCollection $Lineup
ViceCaptain = Find-FplPlayer -PlayerTransform $ViceCaptain -FplPlayerCollection $Lineup
}
$PlayerCollection = @($Players.In) + @($Players.Out) + @($Players.Captain, $Players.ViceCaptain) | Where-Object {$_}
foreach ($Player in $PlayerCollection) {
if ($Player.PlayerId -notin $Lineup.PlayerId) {
$Message = 'There is no player with the name "{0}" in your team' -f $Player.Name
Write-Error -Message $Message -ErrorAction 'Stop'
}
}
if ($Players.In -and $Players.Out) {
$Lineup = Invoke-FplLineupSwap -Lineup $Lineup -PlayersIn $Players.In -PlayersOut $Players.Out
}
if ($Captain -or $ViceCaptain) {
$Lineup = Set-FplLineupCaptain -Lineup $Lineup -Captain $Players.Captain -ViceCaptain $Players.ViceCaptain
}
Assert-FplLineup -Lineup $Lineup.Where{-not $_.IsSub}
$Body = [PSCustomObject]@{
picks = $Lineup.Foreach{
@{
element = $_.PlayerId
position = $_.PositionNumber
is_captain = $_.IsCaptain
is_vice_captain = $_.IsViceCaptain
}
}
}
$TeamId = $Script:FplSessionData['TeamID']
$Params = @{
Uri = "https://fantasy.premierleague.com/api/my-team/$TeamId/"
UseBasicParsing = $true
WebSession = $FplSessionData['FplSession']
Method = 'Post'
Body = ($Body | ConvertTo-Json)
Headers = @{
'Content-Type' = 'application/json; charset=UTF-8'
'X-Requested-With' = 'XMLHttpRequest'
'Referer' = 'https://fantasy.premierleague.com/a/team/my'
}
}
try {
$Response = Invoke-RestMethod @Params -ErrorAction 'Stop'
}
catch {
$Response = Get-ErrorResponsePayload -ErrorObject $_ | ConvertFrom-Json
Write-FplError -FplError $Response
}
}