Code optimisation and minor function fixes

This commit is contained in:
Deon George
2018-12-05 21:50:38 +11:00
parent 3651a6508a
commit 9c2304869d
5 changed files with 372 additions and 239 deletions

View File

@@ -17,10 +17,12 @@ use Illuminate\Support\Facades\Log;
* + Header is on line 1.
* + Input field is on Line 24.
* + 'i' Frames are info frames, no looking for fields. (Lines 2-23)
* + 'ip' Frames are Information Provider frames - no header added. (Lines 1-23)
* + 'a' Frames have active frames with responses.
* + 't' Frames terminate the session
*
* + Frame types:
* + 'ip' Frames are Information Provider frames - no header added. (Lines 1-23)
*
* To Consider
* + 'x' External frames - living in another viewdata server
*
@@ -38,32 +40,55 @@ class Frame
private $cost_length = 7; // 9 (prefixed with a color, suffixed with unit)
private $cost_unit = 'u';
private $fieldmap = ['s'=>'systel','n'=>'username','a'=>'address#','d'=>'%date'];
public $fields = NULL; // The fields in this frame.
// Magic Fields that are pre-filled
private $fieldmap = [
'a'=>'address#',
'd'=>'%date',
];
// Fields that are editable
private $fieldoptions = [
'a'=>['edit'=>TRUE], // Address
'p'=>['edit'=>TRUE], // Password
'u'=>['edit'=>TRUE], // User
];
// @todo Move this to the database
private $header = RED.'T'.BLUE.'E'.GREEN.'S'.YELLOW.'T'.MAGENTA.'!';
public function __construct(\App\Models\Frame $o,string $msg=NULL)
{
dump(__METHOD__);
$this->frame = $o;
$this->output = $this->hasFlag('clear') ? CLS : HOME;
// If we have a message to display on the bottom line.
if ($msg)
$this->output .= UP.$msg.HOME;
$startline = 0;
if (! $this->hasFlag('ip')) {
// Set the page header: CUG/Site Name | Page # | Cost
$this->output .= $this->render_header($this->header).
$this->render_page($this->frame->frame,$this->frame->index).
$this->render_cost($this->frame->cost);
$startline = 1;
}
// Calculate fields and render output.
$this->fields();
$this->fields($startline);
}
/**
* Render the frame
*
* @return null|string
*/
public function __toString()
{
return $this->output;
@@ -75,10 +100,12 @@ class Frame
*
* @param int $startline
*/
public function fields($startline=0)
public function fields($startline=0,$fieldchar='.')
{
$infield = FALSE;
$this->fields = collect();
$infield = FALSE; // In a field
$fieldtype = NULL; // Type of field
$fieldlength = 0; // Length of field
$this->fields = collect(); // Fields in this frame.
if ($startline)
$this->output .= str_repeat(DOWN,$startline);
@@ -88,28 +115,30 @@ class Frame
// Scan the frame for a field start
for ($y=$startline;$y<=$this->frame_length;$y++)
{
// Fields can only be on a single line
$fieldx = $fieldy = FALSE;
for ($x=0;$x<$this->frame_width;$x++)
{
$posn = $y*40+$x;
// If the frame is not big enough, fill it with spaces.
$byte = ord(isset($this->frame->content{$posn}) ? $this->frame->content{$posn} : ' ')%128;
// dump(sprintf('Y: %s,X: %s, POSN: %s, BYTE: %s',$y,$x,$posn,$byte));
// Check for start-of-field
if ($byte == ord(ESC)) { // Esc designates start of field (Esc-K is end of edit)
$infield = TRUE;
$fieldlength = 0;
$fieldlength = 1;
$fieldtype = ord(substr($this->frame->content,$posn+1,1))%128;
$byte = ord(' '); // Replace ESC with space.
$this->output .= $fieldchar;
} else {
if ($infield) {
if ($byte == $fieldtype) {
$fieldlength++;
$byte = ord(' '); // Replace field with space.
$byte = ord($fieldchar); // Replace field with $fieldchar.
if ($fieldx === false) {
if ($fieldx === FALSE) {
$fieldx = $x;
$fieldy = $y;
}
@@ -131,12 +160,15 @@ class Frame
// Replace field with Date
if ($field == '%date') {
if ($fieldlength == 1)
// Drop the last dot and replace it.
if ($fieldlength == 2) {
$datetime = date('D d M H:ia');
$this->output = rtrim($this->output,$fieldchar);
$this->output .= $datetime{0};
}
if ($fieldlength <= strlen($datetime))
if ($fieldlength > 1 AND $fieldlength <= strlen($datetime))
$byte = ord($datetime{$fieldlength-1});
}
// @todo user data
@@ -147,19 +179,18 @@ class Frame
} /*else // pre-load field contents. PAM or *00 ?
if (isset($fields[what]['value'])) {
} */
*/
}
} else {
Log::debug(sprintf('Frame: %s%s, Field found at [%s,%s], Type: %s, Length: %s','TBA','TBA',$fieldx,$fieldy,$fieldtype,$fieldlength));
$this->fields->push([
$this->fields->push(new FrameFields([
'type'=>chr($fieldtype),
'length'=>$fieldlength,
'x'=>$fieldx,
'x'=>$fieldx-1, // Adjust for the ESC char
'y'=>$fieldy,
]);
]));
Log::debug(sprintf('Frame: %s, Field found at [%s,%s], Type: %s, Length: %s',$this->page(),$fieldx-1,$fieldy,$fieldtype,$fieldlength));
$infield = FALSE;
$fieldx = $fieldy = FALSE;
@@ -167,23 +198,47 @@ class Frame
}
}
// truncate end of lines
// truncate end of lines @todo havent validated this code or used it?
if (isset($pageflags['tru']) && substr($this->frame->content,$posn,40-$x) === str_repeat(' ',40-$x)) {
$this->output .= CR . LF;
break;
}
$this->output .= ($byte < 32) ? ESC.chr($byte+64) : chr($byte);
if (! $infield OR $fieldlength > 1)
$this->output .= ($byte < 32) ? ESC.chr($byte+64) : chr($byte);
}
}
}
/**
* Return the Frame Number
* Returns the current frame.
*/
public function framenum()
public function frame()
{
return $this->frame->frame.$this->frame->index;
return $this->frame->frame;
}
/**
* Return the current field configuration
*/
public function getField(int $id)
{
return $this->fields->get($id);
}
/**
* Get a specific key of the field options that passes a filter test
*
* @param string $type
* @param int $after
* @return mixed
*/
public function getFieldId($type='edit',$after=0)
{
return $this->fields
->search(function($item,$key) use ($type,$after) {
return $key >= $after AND $this->isFieldEditable($item->type);
});
}
/**
@@ -194,11 +249,69 @@ class Frame
* @param $flag
* @return bool
*/
private function hasFlag($flag)
public function hasFlag($flag)
{
return $this->frame->hasFlag($flag);
}
/**
* Return the frame DB id
*
* @return mixed
*/
public function id()
{
return $this->frame->id;
}
/**
* Current frame index
*
* @return mixed
*/
public function index()
{
return $this->frame->index;
}
/**
* Return the next index
*/
public function index_next()
{
return chr(ord($this->frame->index)+1);
}
/**
* Determine if a field is editable
*
* @param string $field
* @return mixed
*/
public function isFieldEditable(string $field)
{
return array_get(array_get($this->fieldoptions,$field),'edit',FALSE);
}
/**
* Return the Page Number
*/
public function page(bool $as_array=FALSE)
{
return $as_array ? ['frame'=>$this->frame->frame,'index'=>$this->frame->index] : $this->frame->page;
}
/**
* Return the next page number.
*
* @param bool $as_array
* @return mixed
*/
public function pagenext(bool $as_array=FALSE)
{
return $as_array ? ['frame'=>$this->frame->frame,'index'=>$this->index_next()] : $this->frame->frame.$this->index_next();
}
/**
* Render the cost of the frame
*
@@ -292,10 +405,10 @@ class Frame
R_WHITE.'999999999a'.R_RED.sprintf('%07.0f',999).'u';
$o->content .= str_repeat('+-',18).' '.R_RED.'01';
$o->content .= 'Date: '.ESC.str_repeat('d',25).str_repeat('+-',4);
$o->content .= 'Name: '.ESC.str_repeat('u',5).str_repeat('+-',14);
$o->content .= 'Address: '.ESC.str_repeat('a',20).''.str_repeat('+-',5);
$o->content .= ' : '.ESC.str_repeat('a',20).''.str_repeat('+-',5);
$o->content .= 'Date: '.ESC.str_repeat('d',25).str_repeat('+-',4);
$o->content .= 'Address: '.ESC.str_repeat('a',19).' '.str_repeat('+-',5);
$o->content .= ' : '.ESC.str_repeat('a',19).' '.str_repeat('+-',5);
return $o;
}

View File

@@ -0,0 +1,18 @@
<?php
namespace App\Classes;
class FrameFields
{
private $fields = [];
public function __construct(array $fields)
{
$this->fields = $fields;
}
public function __get($key)
{
return array_get($this->fields,$key);
}
}