Show GameController.php syntax highlighted
<?php
/**
* Astrum Futura: Open Source Space Strategy Game
*
* LICENSE
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* 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 General Public License for more details.
*
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@astrumfutura.com so we can send you a copy immediately.
*
* @category Astrum
* @package Astrum_Controller
* @copyright Copyright (c) 2006 Pádraic Brady (http://blog.quantum-star.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
* @version $Id$
*/
/**
* Game Controller
*
* Handles viewing game details and joining a new game.
*
* @category Astrum
* @package Astrum_Controller
* @copyright Copyright (c) 2006 Pádraic Brady (http://blog.quantum-star.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
*/
class GameController extends Astrum_Controller_Action
{
/**
* Regex allowing a string containing alphanumeric characters,
* spaces and underscores. String must start with an alnum
* character.
*/
const REGEX_NAME = '/^[[:alnum:]\ \_]/';
/**
* Display details of the current game, and request confirmation of
* a) JOIN or b) ENTRY depending on whether a current player or not.
*
* @access public
* @todo Implement access control
*/
public function indexAction()
{
if($this->_session->authenticated != 1 || !isset($this->_session->user))
{
$this->forward('index', 'index');
return; // All forwards are not committed until method returns!
}
/*
* Display game information
*/
require_once 'Astrum/Game.php';
$game = new Astrum_Game;
$game->getByPk(1);
$this->_view->game = $game->asArray();
$this->_view->user = $this->_session->user;
/*
* Setup the response
*/
$this->getResponse()->appendBody(
$this->_view->render('game_index.tpl.html')
);
}
public function enterAction()
{
$this->forward('sector','index');
return;
}
public function joinAction()
{
/*
* Check request type, and redisplay form if not POST
*/
if($this->getRequest()->getMethod() !== 'POST' || !isset($this->_post))
{
$this->_forward('signup', 'index');
return;
}
/*
* Get filtered input data or else exit current action
* The associated errorHandler will have setup
* additional instructions for handling bad input
*/
if(!$clean = $this->validateProcessInput())
{
return;
}
/*
* Process new player
*/
require_once 'Astrum/Player.php';
$player = new Astrum_Player;
$player->name = $clean['user'];
$player->user_id = $this->_session->user['id'];
$player->sector_id = 100;
$player->credits = 100000;
$player->save();
/*
* Process new fleet
*/
require_once 'Astrum/Fleet.php';
$fleet = new Astrum_Fleet;
$fleet->name = $clean['fleet'];
$fleet->sector_id = $player->sector_id;
$fleet->player_id = $player->id;
$fleet->save();
/*
* Add fleet_id to player data
*/
$player->fleet_id = $fleet->id;
$player->save();
/*
* Process new ship (need to add ship attributes later)
*/
require_once 'Astrum/Ship.php';
$ship = new Astrum_Ship;
$ship->fleet_id = $fleet->id;
$ship->player_id = $player->id;
$ship->name = $fleet->name . ' #1';
$ship->save();
/*
* Set is_player flag for User account
*/
require_once 'Astrum/User.php';
$user = new Astrum_User($this->_session->user);
$user->setExists(); // user already exists on database - we're just updating
$user->is_player = 1;
$user->save();
/*
* Append data to session
*/
$this->_session->player = $player->asArray();
$this->_session->user = $user->asArray();
/*
* Display confirmation
*/
$this->_view->player = $player->asArray();
$this->_view->fleet = $fleet->asArray();
$this->getResponse()->appendBody(
$this->_view->render('game_join.tpl.html')
);
}
/**
* Validate input data passed to processAction method.
*
* @access private
* @return mixed Boolean false if invalid data, or clean input array
*/
private function validateProcessInput()
{
$clean = array();
/*
* Validate all expected input values
*/
if(!$clean['user'] = $this->_post->testRegex('astrum_form_game_name', self::REGEX_NAME))
{
return $this->handleError('astrum_form_game_name');
}
if(!$clean['fleet'] = $this->_post->testRegex('astrum_form_game_fleet', self::REGEX_NAME))
{
return $this->handleError('astrum_form_game_fleet');
}
return $clean;
}
/**
* Handle errors from validateProcessInput method in POST
* Currently appends an error message to Response, and
* forward to the indexAction
*
* @access private
* @return bool Returns false after setting forward action
*/
private function handleError($type)
{
$this->getResponse()->appendBody('<strong>' . $type . ' field is invalid</strong><br/>');
$this->_forward('game', 'index');
return false;
}
}
See more files for this project here