You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

196 lines
6.3 KiB

<?php namespace App\Controllers;
/******************************************************************************************************************
* RPSLS - Rock, Paper, Scissors, Lizard, Spock Game
*
* @file app/Controllers/SetupController.php
* @package App\Controllers
* @description
* Sets up the DB on first run if it does not exist.
* @author Rick Hays
* @date 11/15/2019
* @license MIT
* @copyright Copyright © 2019 - Rick Hays, All Rights Reserved.
*
* Revisions:
* YYYY-MM-DD XX Description
******************************************************************************************************************/
class SetupController extends BaseController
{
private $forge;
private $Error_Flag = FALSE;
/**
* SetupController constructor.
*/
public function __construct()
{
$this->forge = \Config\Database::forge();
}
/**
* @method setup
*
* @description
* Class Page Call for setting up DB.
* This will attempt to create DB, tables and indexes for the Game using SQLite.
* Note: Make sure permissions are set correct on the app/Database directory so program
* can write DB to path.
*/
public function setup()
{
// Create new DB ///////////////////////////////////////////////////////////////////////////////////////////
if ($this->forge->createDatabase('game'))
echo 'Game Database created. <br>';
else
{
echo 'Error Creating Game DB! <br>';
$this->Error_Flag = TRUE;
}
// Build Log Table /////////////////////////////////////////////////////////////////////////////////////////
if (is_object($this->BuildLogTable()))
echo '(Log) table was created in DB. <br>';
else
{
echo 'Error Creating (Log) Table! <br>';
$this->Error_Flag = TRUE;
}
// Build Play Table ////////////////////////////////////////////////////////////////////////////////////////
if (is_object($this->BuildPlayTable()))
echo '(Play) table was created in DB. <br>';
else
{
echo 'Error Creating (Play) Table! <br>';
$this->Error_Flag = TRUE;
}
// Build USER Table ////////////////////////////////////////////////////////////////////////////////////////
if (is_object($this->BuildUserTable()))
echo '(User) table was created in DB. <br>';
else
{
echo 'Error Creating (User) Table! <br>';
$this->Error_Flag = TRUE;
}
if ($this->Error_Flag)
{
echo 'Errors while trying to create DB';
exit;
}
else
echo 'Starting Game Page.';
return redirect('/', 'refresh');
}
/***************************************************************************************************************
* START OF PRIVATE FUNCTIONS
**************************************************************************************************************/
/**
* @method BuildLogTable
* @description
* Pulls in Log Table Fields & Indexes, then attempts to create Log Table
* @return mixed
*/
private function BuildLogTable()
{
$this->forge->addField($this->LogFields());
return $this->forge->createTable('Log', TRUE);
}
/**
* @method BuildPlayTable
* @description
* Pulls in Play Table Fields & Indexes, then attempts to create Play Table
* @return mixed
*/
private function BuildPlayTable()
{
$this->forge->addField($this->PlayFields());
return $this->forge->createTable('plays', TRUE);
}
/**
* @method BuildUserTable
* @description
* Pulls in User Table Fields & Indexes, then attempts to create User Table
* @return mixed
*/
private function BuildUserTable()
{
$this->forge->addField($this->UserFields());
$this->forge->addKey('UserName', FALSE, TRUE);
return $this->forge->createTable('users', TRUE);
}
/**
* @method LogFields
* @description
* Fields and Indexes, Structure for the Log Table
* @return array
*/
private function LogFields()
{
return $fields =
[
'id' => ['type' => 'int', 'auto_increment' => TRUE, ],
'IP' => ['type' => 'text', ],
'City' => ['type' => 'text', ],
'State' => ['type' => 'text', ],
'Country' => ['type' => 'text', ],
'DateTime' => ['type' => 'date', 'null' => TRUE, ],
];
}
/**
* @method PlayFields
* @description
* Fields and Indexes, Structure for the Play Table
* @return array
*/
private function PlayFields()
{
return $fields =
[
'id' => ['type' => 'int', 'auto_increment' => TRUE, ],
'UserID' => ['type' => 'text', ],
'UserName' => ['type' => 'text', ],
'IP' => ['type' => 'text', ],
'PlayerPick' => ['type' => 'text', ],
'ComputerPick' => ['type' => 'text', ],
'Result' => ['type' => 'text', ],
'DateTime' => ['type' => 'date', 'null' => TRUE, ],
];
}
/**
* @method UserFields
* @description
* Fields and Indexes, Structure for the User Table
* @return array
*/
private function UserFields()
{
return $fields =
[
'id' => ['type' => 'int', 'auto_increment' => TRUE, ],
'UserName' => ['type' => 'text', ],
'Wins' => ['type' => 'int', 'default' => 0, ],
'Loses' => ['type' => 'int', 'default' => 0, ],
'Ties' => ['type' => 'int', 'default' => 0, ],
'IP' => ['type' => 'text', ],
'City' => ['type' => 'text', ],
'State' => ['type' => 'text', ],
'Country' => ['type' => 'text', ],
'CountryCode' => ['type' => 'text', ],
'FlagURL' => ['type' => 'text', ],
'DateCreated' => ['type' => 'date', 'null' => TRUE, ],
'DateUpdated' => ['type' => 'date', 'null' => TRUE, ],
'DateDeleted' => ['type' => 'date', 'null' => TRUE, ],
];
}
}