Skip to content

Commit

Permalink
Implement SectorsGateway
Browse files Browse the repository at this point in the history
  • Loading branch information
thekabal committed Mar 12, 2018
1 parent feb7fb5 commit 28f1d7c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
2 changes: 1 addition & 1 deletion classes/KabalTrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static function trade(\PDO $pdo_db, array $playerinfo, Reg $tkireg): void
$shipgoods = null;

// Obtain sector information
$sql = "SELECT * FROM ::prefix::universe WHERE sector_id=:sector_id";
$sql = "SELECT * FROM ::prefix::universe WHERE sector_id=:sector_id LIMIT 1";
$stmt = $pdo_db->prepare($sql);
$stmt->bindParam(':sector_id', $playerinfo['sector'], \PDO::PARAM_INT);
$stmt->execute();
Expand Down
44 changes: 44 additions & 0 deletions classes/Sectors/SectorsGateway.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php declare(strict_types = 1);
// The Kabal Invasion - A web-based 4X space game
// Copyright © 2014 The Kabal Invasion development team, Ron Harwood, and the BNT development team
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// File: classes/Sectors/SectorsGateway.php

namespace Tki\Sectors; // Domain Entity organization pattern, Sectors objects

class SectorsGateway // Gateway for SQL calls related to Sectors
{
/** @var \PDO **/
protected $pdo_db; // This will hold a protected version of the pdo_db variable

public function __construct(\PDO $pdo_db) // Create the this->pdo_db object
{
$this->pdo_db = $pdo_db;
}

public function selectSectorInfo(int $sector_id)
{
$sql = "SELECT * FROM ::prefix::universe WHERE sector_id=:sector_id LIMIT 1";
$stmt = $this->pdo_db->prepare($sql);
$stmt->bindParam(':sector_id', $sector_id, \PDO::PARAM_INT);
$stmt->execute();
\Tki\Db::logDbErrors($this->pdo_db, $sql, __LINE__, __FILE__); // Log any errors, if there are any

// A little magic here. If it couldn't select a sector, the following call will return false - which is what we want for "no sector found".
$sectorinfo = $stmt->fetch(\PDO::FETCH_ASSOC);
return $sectorinfo; // FUTURE: Eventually we want this to return a sector object instead, for now, sectorinfo array or false for no user found.
}
}
7 changes: 2 additions & 5 deletions main.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,8 @@


// Pull sector info from database
$sql = "SELECT * FROM ::prefix::universe WHERE sector_id=:sector_id";
$stmt = $pdo_db->prepare($sql);
$stmt->bindParam(':sector_id', $playerinfo['sector'], PDO::PARAM_INT);
$stmt->execute();
$sectorinfo = $stmt->fetch(PDO::FETCH_ASSOC);
$sectors_gateway = new \Tki\Sectors\SectorsGateway($pdo_db); // Build a sector gateway object to handle the SQL calls
$sectorinfo = $sectors_gateway->selectsectorInfo($playerinfo['sector']);

if ($playerinfo['on_planet'] == "Y")
{
Expand Down

0 comments on commit 28f1d7c

Please sign in to comment.