Jump to content

Understanding where to put logic in CakePHP for my game?


Recommended Posts

ETA: Title said login, I meant logic. Not tackling login yet ;)

 

Hey guys. For fun I decided to try to make a web game using CakePHP. For now I'm completely ignoring the user login and just trying to build it as if one user is logged in.

 

So the user has a Map they can see (it will be a 5x5 grid, with them in the center). When they load their map page, I want it to generate some random things onto the map (so placing them in random spots in the 5x5 grid). Then save that into the session so that the whole time they are logged in, those items are in those spots.

 

Right now this is my maps_controller.php

<?php
class MapsController extends AppController {

var $name = 'Maps';

function index() {
	$user = $this->Session->read('User');
	$map = $this->Map->findById($user['User']['map_id']);
	$this->set('map', $map);
}
}
?>

 

The logic I need to add is that after retrieving the Map (which is just an ID and name right now), to check if I have already generated the items. If not, do so, then save it into the session. THEN I need to set the whole thing into the view using $this->set().

 

Can someone help point me in the right direction? I am not sure where I'd put this logic...in the afterFind() maybe?

I decided to make a table called "Map Spots" and every hour will generate new items on the map, and it will be communal. So I added a relationship with Maps and Map Spots

	var $hasMany = array(
	'MapSpot' => array(
		'className' => 'MapSpot',
		'foreignKey' => 'map_id',
		'dependent' => false,
		'conditions' => '',
		'fields' => '',
		'order' => '',
		'limit' => '',
		'offset' => '',
		'exclusive' => '',
		'finderQuery' => '',
		'counterQuery' => ''
	)
);

 

And added recursive to my user so when the user is found, their map and all map spots are as well.

<?php
class User extends AppModel {
var $name = 'User';
var $recursive = 2;

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.