Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
CODE RUNNER 2015 予選Bで使用したソース
<?php
class Coderunner
{
const TOKEN = '8M36KCHD7Y6IMB1L9UT3ZNIE8JKG4OV0';
const BASE_URL = 'https://game.coderunner.jp/';
private $query = [];
private $result = [];
function __construct()
{
$this->query['token'] = self::TOKEN;
}
public function run()
{
var_dump($this->query);
while (true) {
print("\n\n");
print("start\n");
$jsonResult = $this->requestApi('infoJson');
$info = json_decode($jsonResult, true);
$count = 0;
if(is_null($info)){
print("OUT ROOM\n");
$count = 0;
$this->atack();
$this->mySleep();
continue;
}
$power = $info['power'];
print("power is {$power}\n");
$hp = $info['hp'];
$maxhp = $info['maxhp'];
$maxHps = 0;
if(count($info['hps']) != $count){
foreach ($info['hps'] as $key => $value) {
print("hps is {$key}_{$value}\n");
$maxHps = max($maxHps, $value);
$count = count($info['hps']);
}
}
print("maxHps is {$maxHps}\n");
$damage = $info['damage'];
print("My damage is {$damage}\n");
$rank = 1;
foreach ($info['friend'] as $friend) {
if($damage < $friend['damage']){
$rank++;
}
}
print("My rank is {$rank}\n");
if($maxhp == $maxHps){
print("maxhp == maxHps AND {$maxhp} == {$maxHps}\n");
$this->atack();
$this->mySleep();
continue;
}
if($hp < $power && 20334484 < $hp){
print("hp < power AND 25334484 < $hp\n");
$this->atack();
$this->mySleep();
continue;
}
if(30334484 < $hp && $hp/2 < $power){
print("30334484 < hp && hp/2 < power\n");
$this->atack();
$this->mySleep();
continue;
}
usleep(200000);
}
}
private function atack()
{
$apiResult = $this->requestApi('enter');
var_dump($apiResult);
}
private function mySleep()
{
$sleep = $this->getRandomData();
print("sleep is {$sleep}\n");
usleep($sleep);
print("\n");
}
private function requestApi($path)
{
print("*** request ***\n");
$url = $this->getUrl($path);
var_dump($url);
try {
$apiResult = file_get_contents($url);
} catch (Exseption $e){
print("{$e->getMessage()}\n");
}
return $apiResult;
}
private function getUrl($path)
{
$_query = http_build_query($this->query);
return self::BASE_URL . "{$path}?". $_query;
}
private function getRandomData()
{
$random = rand(200000, 400000);
return $random;
}
}
$coderunner = new Coderunner();
$coderunner->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment