More code optimisation

This commit is contained in:
Deon George 2018-12-06 14:09:43 +11:00
parent 9c2304869d
commit 0eaaa57e8a
2 changed files with 246 additions and 288 deletions

View File

@ -60,7 +60,6 @@ class Frame
public function __construct(\App\Models\Frame $o,string $msg=NULL) public function __construct(\App\Models\Frame $o,string $msg=NULL)
{ {
dump(__METHOD__);
$this->frame = $o; $this->frame = $o;
$this->output = $this->hasFlag('clear') ? CLS : HOME; $this->output = $this->hasFlag('clear') ? CLS : HOME;
@ -94,6 +93,25 @@ class Frame
return $this->output; return $this->output;
} }
/**
* Return a list of alternative versions of this frame.
*/
public function alts()
{
return \App\Models\Frame::where('frame',$this->frame())
->where('index',$this->index())
->where('id','<>',$this->frame->id)
->limit(9);
}
/**
* Frame Created Date
*/
public function created()
{
return $this->frame->created_at;
}
/** /**
* Convert the frame from Binary to Output * Convert the frame from Binary to Output
* Look for fields within the frame. * Look for fields within the frame.
@ -385,7 +403,7 @@ class Frame
* @param $text * @param $text
* @return int * @return int
*/ */
function strlenv($text) { function strlenv($text):int {
return strlen($text)-substr_count($text,ESC); return strlen($text)-substr_count($text,ESC);
} }

View File

@ -16,6 +16,9 @@ use Sock\{SocketClient,SocketServer,SocketException};
class Server extends Command class Server extends Command
{ {
// Size of Bottom Line Pollution
private $blp = 0;
/** /**
* The name and signature of the console command. * The name and signature of the console command.
* *
@ -59,15 +62,11 @@ class Server extends Command
throw new SocketException(SocketException::CANT_ACCEPT,'Missing pcntl extension'); throw new SocketException(SocketException::CANT_ACCEPT,'Missing pcntl extension');
} }
// @todo Deprecate this function
define('MSG_TIMEWARP_ON', WHITE . 'TIMEWARP ON' . GREEN . 'VIEW INFO WITH *02'); define('MSG_TIMEWARP_ON', WHITE . 'TIMEWARP ON' . GREEN . 'VIEW INFO WITH *02');
define('MSG_TIMEWARP_OFF', WHITE . 'TIMEWARP OFF' . GREEN . 'VIEWING DATE IS FIXED'); define('MSG_TIMEWARP_OFF', WHITE . 'TIMEWARP OFF' . GREEN . 'VIEWING DATE IS FIXED');
define('MSG_TIMEWARP_TO', GREEN . 'TIMEWARP TO %s'); define('MSG_TIMEWARP_TO', GREEN . 'TIMEWARP TO %s');
define('MSG_TIMEWARP', WHITE . 'OTHER VERSIONS EXIST' . GREEN . 'KEY *02 TO VIEW'); define('MSG_TIMEWARP', WHITE . 'OTHER VERSIONS EXIST' . GREEN . 'KEY *02 TO VIEW');
// @todo Deprecate this
include_once('classes/vvdatabase.class.php');
$server = new SocketServer(config('app.port'),config('app.bind')); $server = new SocketServer(config('app.port'),config('app.bind'));
$server->init(); $server->init();
$server->setConnectionHandler([$this,'onConnect']); $server->setConnectionHandler([$this,'onConnect']);
@ -82,10 +81,6 @@ class Server extends Command
function onConnect(SocketClient $client) { function onConnect(SocketClient $client) {
Log::info('Connection from: ',['client'=>$client->getAddress()]); Log::info('Connection from: ',['client'=>$client->getAddress()]);
// @todo To Deprecate in favour of config()
global $config;
include ('config/config.php');
$pid = pcntl_fork(); $pid = pcntl_fork();
if ($pid == -1) if ($pid == -1)
@ -109,35 +104,9 @@ class Server extends Command
$client->send(CLS); $client->send(CLS);
// @todo Deprecate and have an exception handler handle this.
// like throw new FatalClientException(CLS.UP.ERR_DATABASE,500,NULL,$client);
// @todo Get the login/start page, and if it is not available, throw the ERR_DATEBASE error.
$db = new \vvdb();
// Connect to database. Returns error message if unable to connect.
$r = $db->connect($config['dbserver'], $config['database'], $config['dbuser'], $config['dbpass']);
/*
if (!empty($r)) {
http_response_code(500);
$client->send(CLS . UP . ERR_DATABASE);
die ($r);
}
*/
// $user will eventually contain validated user details.
$user = array('systel' => '019990001',
'username' => 'DEMONSTRATION DATA USER',
'address1' => '8 HERBAL HILL',
'address2' => 'LONDON',
'address3' => 'EC1R 5EJ',
'address4' => '',
'address5' => '',
'CUGS' => array(7800, 15500), // Closed User Groups this user has access to
);
// Setup VARS // Setup VARS
$timewarp = FALSE; // Is timewarp active. $timewarp = FALSE; // Is timewarp active.
$timewarpalt = FALSE; // Alternative timewarp frame to get
$history = collect(); // Page history for going backwards $history = collect(); // Page history for going backwards
$action = ACTION_GOTO; // Initial action. $action = ACTION_GOTO; // Initial action.
$cmd = ''; // Current *command being typed in $cmd = ''; // Current *command being typed in
@ -146,9 +115,10 @@ class Server extends Command
$current = []; // Attributes about the current page $current = []; // Attributes about the current page
// field/fieldnum indexes are for fields on the active page // field/fieldnum indexes are for fields on the active page
$current['fieldreset'] = FALSE; // Flag to reset position (used in fields) $current['fieldreset'] = FALSE; // Flag to reset position (used in fields)
$current['fieldpos'] = 0; // For current field, position within.
$current['prevmode'] = FALSE; // Previous mode - in case we need to go back to MODE_FIELD
$prevmode = false; // previous mode // @todo Get the login/start page, and if it is not available, throw the ERR_DATEBASE error.
if (isset($config['loginpage'])) { if (isset($config['loginpage'])) {
$page = ['frame'=>$config['loginpage'],'index'=>'a']; $page = ['frame'=>$config['loginpage'],'index'=>'a'];
} else if (!empty($service['start_page'])) { } else if (!empty($service['start_page'])) {
@ -157,29 +127,6 @@ class Server extends Command
$page = ['frame'=>'98','index'=>'a']; // next page $page = ['frame'=>'98','index'=>'a']; // next page
} }
$curfp = 0; // current field, position within.
$blp = 0; // botton line polluted (by this no. of characters)
if (!isset($config['varient_id']))
$config['varient_id'] = NULL;
$service = $db->getServiceById($config['service_id']);
//dd($service);
$varient = $db->getAllVarients($config['service_id'], $config['varient_id']);
if ($varient === false) {
die ("no varient");
}
$varient = reset($varient);
$matches = array();
if (preg_match('@' . $service['page_format'] . '@', $service['start_frame'], $matches)) {
$page['frame'] = $matches[1];
$page['index'] = $matches[2];
echo " Using start page " . $page['frame'].$page['index'] . "\n";
}
// $start = $service['start_frame'];
// where to start from
while ($action != ACTION_TERMINATE) { while ($action != ACTION_TERMINATE) {
// Read a character from the client session // Read a character from the client session
$read = $client->read(1); $read = $client->read(1);
@ -260,30 +207,27 @@ class Server extends Command
switch ($mode) { switch ($mode) {
// Key presses during field input. // Key presses during field input.
case MODE_FIELD: case MODE_FIELD:
dump(sprintf('** Processing Keypress in MODE_FIELD [%s (%s)]. Last POS [%s]',$read,ord($read),$curfp)); dump(sprintf('** Processing Keypress in MODE_FIELD [%s (%s)]. Last POS [%s]',$read,ord($read),$current['fieldpos']));
$cmd = ''; $cmd = '';
$action = FALSE; $action = FALSE;
switch ($fo->type()) { switch ($fo->type()) {
// Input frame. // Response frame.
case 'a': case 'a':
switch ($read) { switch ($read) {
// End of field entry. // End of field entry.
case HASH: case HASH:
// Next Field // Next Field
$current['fieldnum']++; $current['fieldnum']++;
$curfp = 0; $current['fieldpos'] = 0;
if ($current['fieldnum'] < $fo->fields->count()) { // skip past non-editable fields if ($current['fieldnum'] < $fo->fields->count()) {
$current['fieldnum'] = $fo->getFieldId('edit',$current['fieldnum']); $current['fieldnum'] = $fo->getFieldId('edit',$current['fieldnum']);
if ($current['fieldnum'] !== FALSE) { if ($current['fieldnum'] !== FALSE) {
$current['field'] = $fo->getField($current['fieldnum']); $current['field'] = $fo->getField($current['fieldnum']);
$fielddata[$current['fieldnum']] = ''; $fielddata[$current['fieldnum']] = '';
dump(['Working field'=>$current['field']]);
dump(['line'=>__LINE__,'field'=>$current['field'],'x'=>$current['field']->x]);
$client->send($this->outputPosition($current['field']->x,$current['field']->y).CON); $client->send($this->outputPosition($current['field']->x,$current['field']->y).CON);
$mode = MODE_FIELD; $mode = MODE_FIELD;
$fielddate[$current['fieldnum']] = ''; $fielddate[$current['fieldnum']] = '';
@ -294,53 +238,59 @@ class Server extends Command
} }
} else { } else {
// done them all editable fields. // Finished all editable fields.
$action = ACTION_SUBMITRF; $action = ACTION_SUBMITRF;
} }
break; break;
case STAR: case STAR:
$prevmode = MODE_FIELD; $current['prevmode'] = MODE_FIELD;
$action = ACTION_STAR; $action = ACTION_STAR;
$curfp = 0; $current['fieldpos'] = 0;
$fielddata[$current['fieldnum']] = ''; $fielddata[$current['fieldnum']] = '';
break; break;
case KEY_LEFT: case KEY_LEFT:
if ($curfp--) { if ($current['fieldpos']--)
$client->send(LEFT); $client->send(LEFT);
};
break; break;
case KEY_RIGHT: case KEY_RIGHT:
if ($curfp++ < $current['field']->length) { if ($current['fieldpos']++ < $current['field']->length)
$client->send(RIGHT); $client->send(RIGHT);
};
break; break;
case KEY_DOWN: case KEY_DOWN:
if ($curfp + 40 < $current['field']->length) { if ($current['fieldpos'] + 40 < $current['field']->length) {
$client->send(DOWN); $client->send(DOWN);
$curfp = $curfp + 40; $current['fieldpos'] = $current['fieldpos'] + 40;
}; };
break; break;
case KEY_UP: case KEY_UP:
if ($curfp - 40 >= 0) { if ($current['fieldpos'] - 40 >= 0) {
$client->send($read); $client->send($read);
$curfp = $curfp - 40; $current['fieldpos'] = $current['fieldpos'] - 40;
}; };
break; break;
case CR: case CR:
if ($curfp + $current['field']->x > 40) { // on second or later line of a field // On second or later line of a field
if ($current['fieldpos'] + $current['field']->x > 40) {
$client->send($read); $client->send($read);
$curfp = (($curfp + $current['field']->x) % 40) * 40; $current['fieldpos'] = (($current['fieldpos'] + $current['field']->x) % 40) * 40;
} else { } else {
dump(['line'=>__LINE__,'field'=>$current['field']]);
$client->send($this->outputPosition($current['field']->x,$current['field']->y).CON); $client->send($this->outputPosition($current['field']->x,$current['field']->y).CON);
$curfp = 0; $current['fieldpos'] = 0;
} }
break; break;
case ESC: case ESC:
@ -348,9 +298,9 @@ class Server extends Command
// Record Data Entry // Record Data Entry
default: default:
if (ord($read) > 31 && $curfp < $current['field']->length) { if (ord($read) > 31 && $current['fieldpos'] < $current['field']->length) {
$fielddata[$current['fieldnum']]{$curfp} = $read; $fielddata[$current['fieldnum']]{$current['fieldpos']} = $read;
$curfp++; $current['fieldpos']++;
$client->send($read); $client->send($read);
} }
} }
@ -360,9 +310,10 @@ class Server extends Command
// Other Frame Types - Shouldnt get here. // Other Frame Types - Shouldnt get here.
default: default:
$client->close(); $client->close();
throw new \Exception('Shouldnt get here', 500); throw new \Exception('Shouldnt get here', 500);
} // switch frame types }
break; break;
@ -371,51 +322,54 @@ class Server extends Command
switch ($read) { switch ($read) {
// @todo Input received, process it. // @todo Input received, process it.
case '1': case '1':
dump(['line'=>__LINE__,'s' => $service, 'f' => $fielddata]); // dump(['line'=>__LINE__,'s' => $service, 'f' => $fielddata]);
// @todo if send successful or not // @todo if send successful or not
if (TRUE) { if (TRUE) {
sendBaseline($client, $blp, MSG_SENT); $this->sendBaseline($client,MSG_SENT);
$mode = MODE_RFSENT; $mode = MODE_RFSENT;
} else { } else {
sendBaseline($client, $blp, ERR_NOTSENT); $this->sendBaseline($client,ERR_NOTSENT);
$mode = MODE_RFERROR; $mode = MODE_RFERROR;
} }
break;
case '2':
sendBaseline($client, $blp, MSG_NOTSENT);; break;
case '2':
$this->sendBaseline($client,MSG_NOTSENT);;
$mode = MODE_RFNOTSENT; $mode = MODE_RFNOTSENT;
break; break;
case STAR: case STAR:
$action = ACTION_STAR; $action = ACTION_STAR;
break;
break;
} }
break; break;
// Response form after Sent processing // Response form after Sent processing
// @todo To fix.
case MODE_RFSENT: case MODE_RFSENT:
$client->send(COFF); $client->send(COFF);
if ($read == HASH) { if ($read == HASH) {
if (!empty($pagedata['route1'])) { if (! empty($pagedata['route1'])) {
$action = ACTION_GOTO; $action = ACTION_GOTO;
$page['frame'] = $pagedata['route1']; $page['frame'] = $pagedata['route1'];
$page['index'] = 'a'; $page['index'] = 'a';
} else if (\App\Models\Frame::where('frame',$fo->frame())->where('index',$fo->index_next())->exists()) { } elseif (\App\Models\Frame::where('frame',$fo->frame())->where('index',$fo->index_next())->exists()) {
$action = ACTION_GOTO; $action = ACTION_GOTO;
$page['frame'] = array_get($current,'page.frame'); $page['frame'] = array_get($current,'page.frame');
$page['index'] = chr(1 + ord($page['index'])); $page['index'] = chr(1 + ord($page['index']));
} else if (!empty($pagedata['route0'])) { } elseif (! empty($pagedata['route0'])) {
$action = ACTION_GOTO; $action = ACTION_GOTO;
$page['frame'] = $pagedata['route0']; $page['frame'] = $pagedata['route0'];
$page['index'] = 'a'; $page['index'] = 'a';
// No further routes defined, go home. // No further routes defined, go home.
} else { } else {
$action = ACTION_GOTO; $action = ACTION_GOTO;
$page['frame'] = '0'; $page['frame'] = '0';
@ -433,35 +387,34 @@ class Server extends Command
// Response form after NOT sending // Response form after NOT sending
case MODE_RFNOTSENT: case MODE_RFNOTSENT:
// Response form ERROR // Response form ERROR
// @todo To fix
case MODE_RFERROR: case MODE_RFERROR:
$client->send(COFF); $client->send(COFF);
if ($read == HASH) { if ($read == HASH) {
if (!empty($pagedata['route2'])) { if (! empty($pagedata['route2'])) {
$action = ACTION_GOTO; $action = ACTION_GOTO;
$page['frame'] = $pagedata['route2']; $page['frame'] = $pagedata['route2'];
$page['index'] = 'a'; $page['index'] = 'a';
} else if (\App\Models\Frame::where('frame',$fo->frame())->where('index',$fo->index_next())->exists()) { } elseif (\App\Models\Frame::where('frame',$fo->frame())->where('index',$fo->index_next())->exists()) {
$action = ACTION_GOTO; $action = ACTION_GOTO;
$page['frame'] = $fo->frame(); $page['frame'] = $fo->frame();
$page['index'] = $fo->index_next(); $page['index'] = $fo->index_next();
} else if (!empty($pagedata['route0'])) { } elseif (! empty($pagedata['route0'])) {
$action = ACTION_GOTO; $action = ACTION_GOTO;
$page['frame'] = $pagedata['route0']; $page['frame'] = $pagedata['route0'];
$page['index'] = 'a'; $page['index'] = 'a';
// No further routes defined, go home. // No further routes defined, go home.
} else { } else {
$action = ACTION_GOTO; $action = ACTION_GOTO;
$page['frame'] = '0'; $page['frame'] = '0';
$page['index'] = 'a'; $page['index'] = 'a';
} }
} else if ($read == STAR) { } elseif ($read == STAR) {
$action = ACTION_STAR; $action = ACTION_STAR;
break; break;
@ -469,26 +422,20 @@ class Server extends Command
break; break;
/* // List of alternate frames has been presented
List of alternate options has been presented case MODE_WARPTO:
*/ if (is_numeric($read) AND $read) {
$timewarpalt = $alts->get($read-1)->id;
$action = ACTION_GOTO;
case MODE_WARPTO: // expecting a timewarp selection } elseif ($read === '0') {
if (isset($alts[$read - 1])) { $action = ACTION_RELOAD;
$v = $db->getAllVarients($config['service_id'], $alts[$read - 1]['varient_id']);
if (!empty($v)) {
$varient = reset($v);
$page = array_get($current,'page');
$action = ACTION_GOTO;
break;
}
} }
// if wasn't a valid warpto keypress,
//drop into break;
// Not doing anything in particular. // Not doing anything in particular.
case MODE_COMPLETE: case MODE_COMPLETE:
case FALSE: case FALSE:
Log::debug('Idle', ['pid' => $pid]); Log::debug('Idle', ['pid' => $pid]);
$cmd = ''; $cmd = '';
@ -517,8 +464,7 @@ class Server extends Command
$action = ACTION_GOTO; $action = ACTION_GOTO;
} else { } else {
dump(['line'=>__LINE__,'blp was'=>$blp]); $this->sendBaseline($client,ERR_ROUTE);
sendBaseline($client, $blp, ERR_ROUTE);
$mode = $action = FALSE; $mode = $action = FALSE;
} }
@ -527,111 +473,127 @@ class Server extends Command
break; break;
/* // Currently accepting baseline input after a * was received
currently accepting baseline imput after a * was received case MODE_BL:
*/
case MODE_BL: // entering a baseline command
echo "was waiting for page number\n"; echo "was waiting for page number\n";
// if it's a number, continue entry // if it's a number, continue entry
if (strpos('0123456789', $read) !== false) { // numeric if (strpos('0123456789', $read) !== FALSE) {
$cmd .= $read; $cmd .= $read;
$client->send($read); $client->send($read);
$blp++; $this->blp++;
dump(['line'=>__LINE__,'blp is now'=>$blp]);
} }
// if we hit a special numeric command, deal with it. // if we hit a special numeric command, deal with it.
if ($cmd === '00') { // refresh page // Refresh page
if ($cmd === '00') {
$client->send(COFF); $client->send(COFF);
$action = ACTION_RELOAD; $action = ACTION_RELOAD;
$cmd = ''; $cmd = '';
break;
}
if ($cmd === '09') { // reload page
$client->send(COFF);
$action = ACTION_GOTO;
$cmd = '';
break;
break;
} }
if ($cmd === '02') { // new for emulator
// Toggle Timewarp Mode
if ($cmd === '01') {
$client->send(COFF);
$timewarp = !$timewarp;
$this->sendBaseline($client,($timewarp ? MSG_TIMEWARP_ON : MSG_TIMEWARP_OFF));
$cmd = '';
$action = $mode = FALSE;
break;
}
// Present Timewarp Frames
if ($cmd === '02') {
$client->send(COFF); $client->send(COFF);
$action = ACTION_INFO; $action = ACTION_INFO;
$cmd = ''; $cmd = '';
break; break;
} }
if (($cmd === '01')) { // new for emulator
// Reload page
if ($cmd === '09') {
$client->send(COFF); $client->send(COFF);
$timewarp = !$timewarp; $action = ACTION_GOTO;
sendBaseline($client, $blp,
($timewarp ? MSG_TIMEWARP_ON : MSG_TIMEWARP_OFF));
$cmd = '';
$action = $mode = false;
}
// another star aborts the command.
if ($read === "*") { // abort command or reset input field.
$action = false;
sendBaseline($client, $blp, '');
dump(['line'=>__LINE__,'blp is now'=>$blp]);
$cmd = ''; $cmd = '';
if ($prevmode == MODE_FIELD) { break;
$mode = $prevmode; }
$prevmode = false;
dump(['line'=>__LINE__,'field'=>$current['field']]); // Another star aborts the command.
if ($read === STAR) {
$action = FALSE;
$this->sendBaseline($client,'');
$cmd = '';
if ($current['prevmode'] == MODE_FIELD) {
$mode = $current['prevmode'];
$current['prevmode'] = FALSE;
$client->send($this->outputPosition($current['field']->x,$current['field']->y).CON); $client->send($this->outputPosition($current['field']->x,$current['field']->y).CON);
$client->send(str_repeat('.', $current['field']->length)); $client->send(str_repeat('.', $current['field']->length));
// tood reset stored entered text
$current['fieldreset'] = TRUE; $current['fieldreset'] = TRUE;
} else { } else {
$mode = false; $mode = false;
} }
break; break;
} }
// user hit # to complete request // Complete request
if ($read === '_') { // really the # key, if ($read === HASH) {
$client->send(COFF); $client->send(COFF);
if ($cmd === '') { // nothing typed between * and # $timewarpalt = FALSE;
// Nothing typed between * and #
if ($cmd === '') {
$action = ACTION_BACKUP; $action = ACTION_BACKUP;
} else { // *# means go back
// *# means go back
} else {
$page['frame'] = $cmd; $page['frame'] = $cmd;
$page['index'] = 'a'; $page['index'] = 'a';
$action = ACTION_GOTO; $action = ACTION_GOTO;
} }
$cmd = ''; // finished with this now
// Clear the command, we are finished processing
$cmd = '';
break; break;
} }
break; break;
default: default:
echo "not sure what we were doing\n"; Log::debug('Not sure what we were doing?');
} // switch $mode }
} // something in $read }
/* // This section performs some action if it is deemed necessary
This section performs some action if it is deemed necessary
*/
if ($action) { if ($action) {
echo "Performing action $action\n"; echo "Performing action $action\n";
} }
switch ($action) { switch ($action) {
case ACTION_STAR: case ACTION_STAR:
echo " star command started\n"; echo " star command started\n";
sendBaseline($client, $blp, GREEN . '*', true); $this->sendBaseline($client,GREEN.STAR,true);
$client->send(CON); $client->send(CON);
$action = false; $action = false;
$mode = MODE_BL; $mode = MODE_BL;
break;
break;
case ACTION_SUBMITRF: case ACTION_SUBMITRF:
$action = false; $action = false;
sendBaseline($client, $bpl, MSG_SENDORNOT); $client->send(COFF);
$this->sendBaseline($client,MSG_SENDORNOT);
$mode = MODE_SUBMITRF; $mode = MODE_SUBMITRF;
break;
break;
// GO Backwards // GO Backwards
case ACTION_BACKUP: case ACTION_BACKUP:
@ -648,57 +610,54 @@ class Server extends Command
// We need this extra test in case we come from ACTION_BACKUP // We need this extra test in case we come from ACTION_BACKUP
if ($action == ACTION_NEXT) if ($action == ACTION_NEXT)
{ {
dump(['current.page.index'=>$current['page']['index'],'fo'=>$fo->index()]);
$current['page']['index'] = $fo->index(); $current['page']['index'] = $fo->index();
$page['index'] = $fo->index_next(); $page['index'] = $fo->index_next();
} }
// Look for requested page
case ACTION_GOTO: case ACTION_GOTO:
// $client->send(HOME . UP . GREEN . "Searching for page $page['frame']"); // If we wanted a "Searching..." message, this is where to put it.
// $blp = 20 + strlenv($page['frame']);
// look for requested page
try { try {
$fo = (new \App\Models\Frame)->fetch($page['frame'],$page['index']); $fo = $timewarpalt
? new \App\Classes\Frame(\App\Models\Frame::findOrFail($timewarpalt))
: (new \App\Models\Frame)->fetch($page['frame'],$page['index']);
} catch (ModelNotFoundException $e) { } catch (ModelNotFoundException $e) {
sendBaseline($client, $blp, ERR_PAGE); $this->sendBaseline($client,ERR_PAGE);
$mode = $action = FALSE; $mode = $action = FALSE;
break; break;
} }
dump(['m' => __METHOD__, 'fo'=>$fo->id(),'service' => $service['service_id'], 'varient' => $varient['varient_id'], 'frame' => $fo->page(), 'type' => $fo->type()]);
// validate if we have access top it // validate if we have access top it
/* if (isset($m['access']) && $m['access'] == 'n') { /* if (isset($m['access']) && $m['access'] == 'n') {
sendBaseline($client, $blp, ERR_PAGE); $this->sendBaseline($client,ERR_PAGE);
$mode = $action = false; $mode = $action = false;
break; break;
} }
if (isset($m['cug']) && is_numeric($m['cug']) && $m['cug'] && !in_array($m['cug'],$usercugs)) { if (isset($m['cug']) && is_numeric($m['cug']) && $m['cug'] && !in_array($m['cug'],$usercugs)) {
sendBaseline($client, $blp, ERR_PRIVATE); $this->sendBaseline($client,ERR_PRIVATE);
$mode = $action = false; $mode = $action = false;
break; break;
} }
*/ */
// we have access... // we have access...
if ($r['varient_id'] != $varient['varient_id']) { //if ($r['varient_id'] != $varient['varient_id']) {
/* if ($timewarpalt) {
if (empty($v['varient_date'])) { if (empty($v['varient_date'])) {
sendBaseline($client, $blp, sprintf(MSG_TIMEWARP_TO, 'unknown date')); $this->sendBaseline($client,sprintf(MSG_TIMEWARP_TO, 'unknown date'));
} else { } else {
sendBaseline($client, $blp, sprintf(MSG_TIMEWARP_TO, date('j f Y', strtotime($v['varient_date'])))); $this->sendBaseline($client,sprintf(MSG_TIMEWARP_TO, date('j f Y', strtotime($v['varient_date']))));
} }
$varient = array_merge($varient, array_intersect_key($r, $varient)); //$varient = array_merge($varient, array_intersect_key($r, $varient));
$msg = "TIMEWARP";
dump(['line'=>__LINE__,'write'=>'TIMEWARP MESSAGE']);
} }
//var_dump(['B','r'=>$r,'m'=>$m]); */
//$pagedata = array_merge($r, $m);
//$varient = $v;
$current['page'] = $fo->page(TRUE); $current['page'] = $fo->page(TRUE);
$curfp = 0; $current['fieldpos'] = 0;
//$pageflags = $db->getFrameTypeFlags($pagedata['type']); // method missing.
$pageflags = [];
// Only if new location, not going backwards // Only if new location, not going backwards
if (($history->last() != $page) AND ($action == ACTION_GOTO || $action == ACTION_NEXT)) { if (($history->last() != $page) AND ($action == ACTION_GOTO || $action == ACTION_NEXT)) {
@ -707,62 +666,39 @@ class Server extends Command
// drop into // drop into
case ACTION_RELOAD: case ACTION_RELOAD:
// @todo Move the $output into the object. // @todo Move this $output into the object.
if ($fo->hasFlag('clear')) if ($fo->hasFlag('clear'))
{ {
$blp = 0; $this->blp = 0;
$output = CLS; $output = CLS;
} else { } else {
$output = HOME; $output = HOME
;
// Clear the baseline. // Clear the baseline.
$blp = sendBaseline($client,$blp,''); $this->sendBaseline($client,'');
} }
$output .= (string)$fo; $output .= (string)$fo;
if ($timewarpalt) {
$this->sendBaseline($client,sprintf(MSG_TIMEWARP_TO,$fo->created() ? $fo->created()->format('Y-m-d H:i:s') : 'UNKNOWN'));
}
switch ($fo->type()) { switch ($fo->type()) {
default: default:
case 'i': // standard frame // Standard Frame
if ($timewarp && 1 < count( case 'i':
$alts = $db->getAlternateFrames($service['service_id'], $varient['varient_id'], array_get($current,'page.frame'), array_get($current,'page.index'))
)) {
$msg = MSG_TIMEWARP;
} else {
$msg = '';
}
dump(['line'=>__LINE__,'blp was'=>$blp,'will be'=>strlenv($msg)]);
$blp = strlenv($msg);
$client->send($output); $client->send($output);
$mode = $action = false; $mode = $action = false;
break; break;
case 'a': // active frame. Prestel uses this for Response Framea. // Active Frame. Prestel uses this for a Response Frame.
/* case 'a':
if ($timewarp && 1 < count( $client->send($output);
$alts = $db->getAlternateFrames($service['service_id'],$varient['varient_id'],array_get($current,'page.frame'), array_get($current,'page.index'))
)) {
$msg = MSG_TIMEWARP;
} else {
$msg = '';
}
*/
// this is a glorious hack to fix three out of 30,000 pages but might
// break lots more.
//$pagedata = str_replace(chr(12),chr(27),$pagedata);
// holds data entered by user. // holds data entered by user.
$fielddata = []; $fielddata = [];
//$msg = '';
//dump(['line'=>__LINE__,'blp was'=>$blp,'will be'=>strlenv($msg)]);
//$blp = strlenv($msg);
$client->send($output);
// dump($fo->fields,count($fo->fields));
// @todo If there are no editable fields, it seems the frame is hung.
if (count($fo->fields)) { if (count($fo->fields)) {
// Get our first editable field. // Get our first editable field.
$current['fieldnum'] = $fo->getFieldId('edit',0); $current['fieldnum'] = $fo->getFieldId('edit',0);
@ -778,68 +714,66 @@ class Server extends Command
$client->send(COFF); $client->send(COFF);
} }
$curfp = 0; $current['fieldpos'] = 0;
} else {
$mode = FALSE;
} }
break; break;
case 't': // terminate // Terminate Frame
//$output = getOutputx(array_get($current,'page.frame'), array_get($current,'page.index'), [], $pageflags); case 't':
$client->send($output); $client->send($output);
$action = ACTION_TERMINATE; $action = ACTION_TERMINATE;
break; break;
} // switch }
break; break;
// Timewarp Mode
case ACTION_INFO: // special emulator command case ACTION_INFO:
$mode = false; $mode = $action = FALSE;
$cmd = ''; $cmd = '';
$action = false; $y = 0;
// @todo Can we use $fo->page() where? $output = $this->outputPosition(0, $y++) . WHITE . NEWBG . RED . 'TIMEWARP INFO FOR Pg.' . BLUE . $fo->page(). WHITE;
$output = $this->outputPosition(0, 0) . WHITE . NEWBG . RED . 'TIMEWARP INFO FOR Pg.' . BLUE . array_get($current,'page.frame').array_get($current,'page.index'). WHITE; //$output .= $this->outputPosition(0, $y++) . WHITE . NEWBG . BLUE . 'Service : ' . substr($service['service_name'] . str_repeat(' ', 27), 0, 27);
$output .= $this->outputPosition(0, 1) . WHITE . NEWBG . BLUE . 'Service : ' . substr($service['service_name'] . str_repeat(' ', 27), 0, 27); //$output .= $this->outputPosition(0, $y++) . WHITE . NEWBG . BLUE . 'Varient : ' . substr($varient['varient_name'] . str_repeat(' ', 27), 0, 27);
$output .= $this->outputPosition(0, 2) . WHITE . NEWBG . BLUE . 'Varient : ' . substr($varient['varient_name'] . str_repeat(' ', 27), 0, 27); $output .= $this->outputPosition(0, $y++) . WHITE . NEWBG . BLUE . 'Dated : ' .substr(($fo->created() ? $fo->created()->format('j F Y') : 'Unknown').str_repeat(' ', 27), 0, 27);
$output .= $this->outputPosition(0, 3) . WHITE . NEWBG . BLUE . 'Dated : ' . substr(date('j F Y', strtotime($varient['varient_date'])) . str_repeat(' ', 27), 0, 27);
$alts = $fo->alts()->get();
$alts = $db->getAlternateFrames($service['service_id'], $varient['varient_id'], array_get($current,'page.frame'), array_get($current,'page.index'));
if (count($alts) > 1) { if (count($alts) > 1) {
$n = 1; $n = 1;
$output .= $this->outputPosition(0, 4) . WHITE . NEWBG . RED . 'ALTERNATIVE VERSIONS:' . str_repeat(' ', 16);
$y = 5; $output .= $this->outputPosition(0, $y++) . WHITE . NEWBG . RED . 'ALTERNATIVE VERSIONS:' . str_repeat(' ', 16);
foreach ($alts as $ss) {
// if (is_numeric($ss['varient_date'])) { foreach ($alts as $o) {
$date = date('d M Y', strtotime($ss['varient_date'])); $date = $o->created_at->format('d M Y');
// } else {
// $date = 'unknown'; $line = WHITE.NEWBG;
// }
$line = WHITE . NEWBG;
if ($timewarp) { if ($timewarp) {
$line .= RED . $n; $line .= RED.$n++;
} }
$line .= BLUE . $date . ' ' . $ss['varient_name'];
$line .= BLUE.$date.' '.$o->note;
$output .= $this->outputPosition(0, $y) . $line . str_repeat(' ', 40 - strlenv($line)); $output .= $this->outputPosition(0,$y++).$line.str_repeat(' ',40-$this->strlenv($line));
$y++;
$n++;
} }
if ($timewarp) { if ($timewarp) {
$mode = MODE_WARPTO; $mode = MODE_WARPTO;
} }
} }
$client->send($output); $client->send($output);
break; break;
}
default:
;
} // switch $action
// We need to reposition the cursor to the current field. // We need to reposition the cursor to the current field.
if ($current['fieldreset'] !== FALSE) { if ($current['fieldreset'] !== FALSE) {
@ -858,9 +792,6 @@ class Server extends Command
// Something bad happened. We'll log it and then disconnect. // Something bad happened. We'll log it and then disconnect.
} catch (\Exception $e) { } catch (\Exception $e) {
Log::emergency($e->getMessage()); Log::emergency($e->getMessage());
//@todo TEMP
throw new \Exception($e);
} }
$client->close(); $client->close();
@ -885,27 +816,36 @@ class Server extends Command
: str_repeat(LEFT,40-$x)); : str_repeat(LEFT,40-$x));
} }
} }
}
function strlenv($text){
return strlen($text) - substr_count($text, ESC);
}
// @todo Not clearing to end of line.
// @todo Remove blp by reference, change return to return blp
function sendBaseline($client,&$blp,$text,$reposition=FALSE) {
dump(['method'=>__METHOD__,'blp'=>$blp,'text'=>$text,'re'=>$reposition]);
/**
* Send a message to the base line
*
* @note "$this->blp" remembers how many chars are there, so that they can be replace with next call
* @param $client
* @param $text
* @param bool $reposition
*/
function sendBaseline($client,$text,$reposition=FALSE) {
$client->send(HOME.UP.$text. $client->send(HOME.UP.$text.
($blp > strlenv($text) ($this->blp > $this->strlenv($text)
? str_repeat(' ',$blp-strlenv($text)). ? str_repeat(' ',$this->blp-$this->strlenv($text)).
($reposition ? HOME.UP.str_repeat(RIGHT,strlenv($text)) : '') ($reposition ? HOME.UP.str_repeat(RIGHT,$this->strlenv($text)) : '')
: '') : '')
); );
$blp = strlenv($text); $this->blp = $this->strlenv($text);
}
dump(['metho'=>__METHOD__,'blp is now'=>$blp]); /**
* Calculate the length of text
return $blp; *
} * ESC characters are two chars, and need to be counted as one.
*
* @param $text
* @return int
* @todo Implement this as a helper
*/
function strlenv($text):int {
return strlen($text)-substr_count($text,ESC);
}
}