-
Notifications
You must be signed in to change notification settings - Fork 7
/
randomEater.ps1
62 lines (53 loc) · 1.59 KB
/
randomEater.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
[CmdletBinding()]
param(
[Parameter(HelpMessage = "host and port of server")]
[string]$url = "http://localhost:5291",
[Parameter(HelpMessage = "your player name")]
[string]$name = [DateTime]::Now.Ticks % 10000
)
function randomDirection {
$directions = "up", "down", "right", "left"
return $directions[(get-random -max 4)]
}
$client = [System.Net.Http.HttpClient]::new()
function call($path) {
$clientResult = $client.GetStringAsync($path).
GetAwaiter().
GetResult()
return $clientResult
}
function get-gameState {
$r = call "$url/state"
return $r
}
$token = call "$url/join?userName=$name"
write-host "$name joined game w/token $token"
$gameState = get-gameState
while ( $gameState -eq "Joining") {
write-verbose "Game state is $gameState...sleeping"
Start-Sleep -Seconds 2
$gameState = get-gameState
}
$direction = randomDirection;
$reqCount = 0;
while ($true) {
$reqCount++;
try {
$moveResult = (call "$url/move/$direction/?token=$token") | ConvertFrom-Json
if ($null -eq $moveResult -or $moveResult.ateAPill -eq $false) {
$newDirection = randomDirection
Write-verbose "$name didn't eat a token going $direction, so I'll try going $newDirection"
$direction = $newDirection;
}
}
catch {
write-error "$name got an exception. I'm out.`n Exception details: `n$_"
break;
}
if ($reqCount -gt 0) {
if ((get-gameState) -eq "GameOver") {
Write-Host "Game over. Player $name ends."
break;
}
}
}