0) { $header = unpack('Csize/Ctype/Creqi/Cdata', socket_read($socket, 4, PHP_BINARY_READ)); if(!$header) die(); $data = socket_read($socket, $header['size'] - 4, PHP_BINARY_READ); switch($header['type']) { case 3: // TINY, keep-alive if($header['reqi'] === 0 && $header['data'] === 0) { socket_write($socket, pack('CCCC', 4, 3, 0, 0)); } break; case 21: // NPL $race[$header['data']] = array('position' => 0, 'lap' => 0, 'etime' => 0, 'gapahead' => 0, 'gapbehind' => 0, 'REMOTE' => 1); $data = unpack('Cptype', substr($data, 1, 1)); if(!($data['ptype'] & 6)) { $follow = $header['data']; // PLID to time for $race[$follow]['remote'] = 0; // init buttons socket_write($socket, pack('CCCCCCCCCCCCa8', 20, 45, $buttonid+1, 0, $buttonid+0, 0, 16, 0, $buttonx-20, $buttony, 15, 5, str_pad('0.00', 8, "\0"))); // top left socket_write($socket, pack('CCCCCCCCCCCCa8', 20, 45, $buttonid+2, 0, $buttonid+1, 0, 20, 0, $buttonx-20, $buttony+5, 15, 5, str_pad('0.00', 8, "\0"))); // bottom left socket_write($socket, pack('CCCCCCCCCCCCa8', 20, 45, $buttonid+3, 0, $buttonid+2, 0, 16, 0, $buttonx+5, $buttony, 15, 5, str_pad('0.00', 8, "\0"))); // top right socket_write($socket, pack('CCCCCCCCCCCCa8', 20, 45, $buttonid+4, 0, $buttonid+3, 0, 20, 0, $buttonx+5, $buttony+5, 15, 5, str_pad('0.00', 8, "\0"))); // bottom right } break; case 23: // PLL unset($race[$header['data']]); if(isset($follow)) { if($header['data'] == $follow) { unset($follow); // clear buttons socket_write($socket, pack('CCCCCCCC', 8, 42, 0 , 1, 0, 0, 0, 0)); } } break; case 24: // LAP $etime = unpack('Ival', substr($data, 4, 4)); $race[$header['data']]['etime'] = $etime['val']; socket_write($socket, pack('CCCC', 4, 3, $header['data'], 16)); break; case 25: // SPX $etime = unpack('Ival', substr($data, 4, 4)); $race[$header['data']]['etime'] = $etime['val']; socket_write($socket, pack('CCCC', 4, 3, $header['data'], 16)); break; case 37: // NLP while(strlen($data) >= 6) { $nlp = unpack('Snode/Slap/Cplid/Cposition', substr($data, 0, 6)); $race[$nlp['plid']]['position'] = $nlp['position']; $race[$nlp['plid']]['lap'] = $nlp['lap']; $data = substr($data, 6, strlen($data)-6); } if(isset($follow)) { if(($header['reqi'] == $follow) && ($race[$follow]['position'] >= 2)) { // gap ahead foreach($race as $racer) { if($racer['position'] == $race[$follow]['position'] -1) { $gap = $race[$follow]['etime'] - $racer['etime']; socket_write($socket, pack('CCCCCCCCCCCCa8', 20, 45, $buttonid+1, 0, $buttonid+0, 0, 16, 0, $buttonx-20, $buttony, 15, 5, str_pad('+'.number_format($gap / 1000, 2), 8, "\0"))); // top left $diff = $gap - $race[$follow]['gapahead']; if($diff <= 0) socket_write($socket, pack('CCCCCCCCCCCCa8', 20, 45, $buttonid+2, 0, $buttonid+1, 0, 20, 0, $buttonx-20, $buttony+5, 15, 5, str_pad(number_format($diff / 1000, 2), 8, "\0"))); // bottom left if($diff > 0) socket_write($socket, pack('CCCCCCCCCCCCa8', 20, 45, $buttonid+2, 0, $buttonid+1, 0, 21, 0, $buttonx-20, $buttony+5, 15, 5, str_pad('+'.number_format($diff / 1000, 2), 8, "\0"))); // bottom left $race[$follow]['gapahead'] = $gap; } } } if($race[$header['reqi']]['position'] == $race[$follow]['position'] +1) { // gap behind $gap = $race[$header['reqi']]['etime'] - $race[$follow]['etime']; socket_write($socket, pack('CCCCCCCCCCCCa8', 20, 45, $buttonid+3, 0, $buttonid+2, 0, 16, 0, $buttonx+5, $buttony, 15, 5, str_pad('+'.number_format($gap / 1000, 2), 8, "\0"))); // top right $diff = $gap - $race[$follow]['gapbehind']; if($diff <= 0) socket_write($socket, pack('CCCCCCCCCCCCa8', 20, 45, $buttonid+4, 0, $buttonid+3, 0, 20, 0, $buttonx+5, $buttony+5, 15, 5, str_pad(number_format($diff / 1000, 2), 8, "\0"))); // bottom right if($diff > 0) socket_write($socket, pack('CCCCCCCCCCCCa8', 20, 45, $buttonid+4, 0, $buttonid+3, 0, 21, 0, $buttonx+5, $buttony+5, 15, 5, str_pad('+'.number_format($diff / 1000, 2), 8, "\0"))); // bottom right $race[$follow]['gapbehind'] = $gap; } } break; case 17: // RST $race = array(); socket_write($socket, pack('CCCCCCCC', 8, 42, 0 , 1, 0, 0, 0, 0)); socket_write($socket, pack('CCCC', 4, 3, 254, 14)); break; default: if($debug) { print_r($header); } break; } } } socket_close($socket); ?>