PDA

View Full Version : Re


Hwona
06-05-2014, 11:51 AM
I've been trying to make a stat system for my site, but I can't figure out how to put a random number into an "obedience" column in "owned_adoptables" upon the creation of a pet on the adopt page. Could someone help me? I'm using v.1.3.3

class_ownedadoptables file(tried creating a function on line 43 which is probably completely wrong):
<?php

class OwnedAdoptable extends Adoptable{

protected $aid;
protected $name;
protected $owner;
protected $currentlevel;
protected $totalclicks;
protected $code;
protected $imageurl;
protected $usealternates;
protected $tradestatus;
protected $isfrozen;
protected $gender;
protected $offsprings;
protected $lastbred;
protected $nextlevel;
protected $voters;
protected $obedience;

public function __construct($aid, $owner = ""){
$mysidia = Registry::get("mysidia");
$whereClause = "aid ='{$aid}'";
if(!empty($owner)) $whereClause .= " and owner = '{$owner}'";
$row = $mysidia->db->select("owned_adoptables", array(), $whereClause)->fetchObject();
if(!is_object($row)) throw new AdoptNotfoundException("Adoptable {$adoptinfo} does not exist or does not belong to the owner specified...");

parent::__construct($row->type);
foreach($row as $key => $val){
$this->$key = $val;
}
}

public function getAdoptID(){
return $this->aid;
}

public function getAdoptObedience(){
return $this->obedience;
}

public function setAdoptObedience(){
$query = "INSERT INTO adopts_owned_adoptables
(obedience)
WHERE aid = $this->aid;
VALUES
($obedience)";
$mysidia->db->exec($query);
}

public function getName(){
return $this->name;
}

public function setName($name, $assignMode = ""){
if($assignMode == Model::UPDATE) $this->save("name", $name);
$this->name = $name;
}

public function getOwner($fetchMode = ""){
if($fetchMode == Model::MODEL) return new Member($this->owner);
else return $this->owner;
}

public function setOwner($owner, $assignMode = ""){
if($assignMode == Model::UPDATE) $this->save("owner", $owner);
$this->owner = $owner;
}

public function getCurrentLevel($fetchMode = ""){
if($fetchMode == Model::MODEL) return new AdoptLevel($this->type, $this->currentlevel);
else return $this->currentlevel;
}

public function setCurrentLevel($level, $assignMode = ""){
if($assignMode == Model::UPDATE){
$this->save("currentlevel", $level);
if($this->getAltStatus() == "yes") $this->save("usealternates", "yes");
}
$this->currentlevel = $level;
}

public function getTotalClicks(){
return $this->totalclicks;
}

public function setTotalClicks($clicks, $assignMode = ""){
if($assignMode == Model::UPDATE) $this->save("totalclicks", $clicks);
$this->totalclicks = $clicks;
}

public function getCode(){
return $this->code;
}

public function getImageURL(){
return $this->imageurl;
}

public function useAlternates(){
return $this->usealternates;
}

public function getTradeStatus(){
return $this->tradestatus;
}

public function setTradeStatus($status, $assignMode = ""){
if($assignMode == Model::UPDATE) $this->save("tradestatus", $status);
$this->tradestatus = $status;
}

public function isFrozen(){
return $this->isfrozen;
}

public function setFrozen($frozen = TRUE, $assignMode = ""){
if($assignMode == Model::UPDATE) $this->save("isfrozen", $frozen);
$this->isfrozen = $frozen;
}

public function getGender($fetchMode = ""){
if($fetchMode == Model::GUI) return new Image("picuploads/{$this->gender}.png");
else return $this->gender;
}

public function getOffsprings(){
return $this->offsprings;
}

public function setOffsprings($offsprings = 1, $assignMode = ""){
$this->offsprings = $offsprings;
if($assignMode == Model::UPDATE) $this->save("offsprings", $this->offsprings);
}

public function getLastBred($fetchMode = ""){
if($fetchMode == Model::OBJ) return new DateTime($this->lastbred);
return $this->lastbred;
}

public function setLastBred($lastBred = 0, $assignMode = ""){
if($assignMode == Model::UPDATE) $this->save("lastbred", $lastBred);
$this->lastbred = $lastBred;
}

public function getAltStatus(){
if($this->alternates == "enabled" and $this->currentlevel == $this->altoutlevel){
$rand = mt_rand(1, $this->altchance);
if($rand == 1) return "yes";
}
return "no";
}

public function getImage($fetchMode = ""){
if($this->imageurl) return $this->imageurl;
if($this->currentlevel == 0) return $this->getEggImage($fetchMode);

$mysidia = Registry::get("mysidia");
$level = $this->getCurrentLevel("model");
if($this->useAlternates() == "yes") return $level->getAlternateImage($fetchMode);
else return $level->getPrimaryImage($fetchMode);
}

public function hasNextLevel(){
try{
$this->nextlevel = new AdoptLevel($this->type, $this->currentlevel + 1);
return TRUE;
}
catch(LevelNotfoundException $lne){
return FALSE;
}
}

public function getNextLevel(){
if(!$this->nextlevel) return FALSE;
return $this->nextlevel;
}

public function getLevelupClicks(){
if(!$this->nextlevel) return FALSE;
return $this->nextlevel->getRequiredClicks() - $this->totalclicks;
}

public function getStats(){
$mysidia = Registry::get("mysidia");
$document = $mysidia->frame->getDocument();
$stats = new Division("adoptstats");
$stats->add(new Comment("<br><br><b>Total Clicks: {$this->totalclicks}"));
$stats->add(new Comment("Gender: ", FALSE));
$stats->add(new Image("picuploads/{$this->gender}.png"));

if($this->hasNextLevel()){
$level = $this->getNextLevel();
$levelupClicks = $this->getLevelupClicks();
$nextLevel = $level->getLevel().$mysidia->lang->clicks.$levelupClicks;
}
else $nextLevel = $mysidia->lang->maximum;

$adoptStats = "<br>Trade Status: {$this->tradestatus}<br>
Current Level: {$this->currentlevel}<br>Next Level: {$nextLevel}</b>";
$stats->add(new Comment($adoptStats));
return $stats;
}

public function hasVoter($user, $date = ""){
if(!$date) $date = new DateTime;
$mysidia = Registry::get("mysidia");

if($user instanceof Member){
$whereClause = "adoptableid='{$this->aid}' and username = '{$user->username}' and date = '{$date->format('Y-m-d')}'";
}
else{
$ip = secure($_SERVER['REMOTE_ADDR']);
$whereClause = "adoptableid='{$mysidia->input->get("aid")}' and ip = '{$ip}' and date = '{$date->format('Y-m-d')}'";
}

$void = $mysidia->db->select("vote_voters", array("void"), $whereClause)->fetchColumn();
if(is_numeric($void)) return TRUE;
else return FALSE;
}

protected function save($field, $value){
$mysidia = Registry::get("mysidia");
$mysidia->db->update("owned_adoptables", array($field => $value), "aid='{$this->aid}'");
}
}
?>
adopt file(tweak starts on line 34):
<?php

class AdoptController extends AppController{

private $view;
private $subController;

public function __construct(){
parent::__construct("member");
$mysidia = Registry::get("mysidia");
if($mysidia->usergroup->getpermission("canadopt") != "yes"){
throw new NoPermissionException("It appears that you are either not logged in, or do not have permission to adopt.");
}
}

public function index(){
$mysidia = Registry::get("mysidia");
$document = $mysidia->frame->getDocument();

if($mysidia->input->post("submit")){
$this->flag = "member";
$this->handleAccess();
if($mysidia->session->fetch("adopt") != 1 or !is_numeric($mysidia->input->post("id"))) throw new InvalidIDException($mysidia->lang->global_id);

$adopt = new Adoptable($mysidia->input->post("id"));
$conditions = $adopt->getConditions();
if(!$conditions->checkConditions()) throw new NoPermissionException("It appears that you do not meet the condition to acquire this adoptable.");

$name = (!$mysidia->input->post("name"))?$adopt->getType():$mysidia->input->post("name");
$alts = $adopt->getAltStatus();
$code = $adopt->getCode();
$adoptobedience = $adopt->setAdoptObedience();
$gender = $adopt->getGender();
$mysidia->db->insert("owned_adoptables", array("aid" => NULL, "type" => $adopt->getType(), "name" => $name, "owner" => $mysidia->user->username, "currentlevel" => 0, "totalclicks" => 0, "code" => $code, "imageurl" => NULL, "usealternates" => $alts, "tradestatus" => 'fortrade', "isfrozen" => 'no', "gender" => $gender, "lastbred" => 0 and "obedience" -> $adoptobedience;
$aid = $mysidia->db->select("owned_adoptables", array("aid"), "code='{$code}', owner='{$mysidia->user->username}'")->fetchColumn();
$document->setTitle("{$name} adopted successfully");

$image = new Image($adopt->getEggImage());
$image->setLineBreak(TRUE);

$document->add($image);
$document->addLangvar("Congratulations! You just adopted {$name}. You can now manage {$name} on the ");
$document->add(new Link("myadopts", "Myadopts Page."));
$document->add(new Comment(""));
$document->add(new Link("myadopts/manage/{$aid}", "Click Here to Manage {$name}"));
$document->add(new Comment(""));
$document->add(new Link("myadopts/bbcode/{$aid}", "Click Here to get BBCodes/HTML Codes for {$name}"));
$document->add(new Comment(""));
$document->addLangvar("Be sure and");
$document->add(new Link("levelup/{$aid}", "feed "));
$document->addLangvar("{$name} with clicks so that they grow!");
return;
}

$mysidia->session->assign("adopt", 1, TRUE);
$default = (!$mysidia->user->isloggedin)?$mysidia->lang->guest:$mysidia->lang->member;
$document->setTitle($mysidia->lang->title);
$document->addLangvar($default);
$ids = $mysidia->db->select("adoptables", array("id"), "shop='none'")->fetchAll(PDO::FETCH_COLUMN);

$adoptForm = new Form("form", "adopt", "post");
$adoptTitle = new Comment("Available Adoptables");
$adoptTitle->setHeading(3);
$adoptForm->add($adoptTitle);
$adoptTable = new Table("table", "", FALSE);
$adoptNum = 0;

foreach($ids as $id){
$adopt = new Adoptable($id);
$conditions = $adopt->getConditions();

if($conditions->checkConditions()){
$row = new TRow;
$idCell = new TCell(new RadioButton("", "id", $id));
$imageCell = new TCell(new Image($adopt->getEggImage(), $adopt->getType()));
$imageCell->setAlign(new Align("center"));

$type = new Comment($adopt->getType());
$type->setBold();
$description = new Comment($adopt->getDescription(), FALSE);
$typeCell = new TCell;
$typeCell->add($type);
$typeCell->add($description);

$row->add($idCell);
$row->add($imageCell);
$row->add($typeCell);
$adoptTable->add($row);
$adoptNum++;
}
}
if($adoptNum == 0) $adoptForm->add(new Comment("There is not an adoptable available at this point, please come back later."));
else $adoptForm->add($adoptTable);

$adoptSubtitle = new Comment("Adopt");
$adoptSubtitle->setHeading(3);
$adoptForm->add($adoptSubtitle);
$adoptForm->add(new Comment("Adoptable Name: ", FALSE));
$adoptForm->add(new TextField("name"));
$adoptForm->add(new Comment(""));
$adoptForm->add(new Button("Adopt Me", "submit", "submit"));
$document->add($adoptForm);
}
}
?>
Thanks a bunch!

IntoRain
06-05-2014, 12:49 PM
Okay a few things I think you are confusing.

1)
This:

public function setAdoptObedience(){
$query = "INSERT INTO adopts_owned_adoptables
(obedience)
WHERE aid = $this->aid;
VALUES
($obedience)";
$mysidia->db->exec($query);
}

Use $mysidia->db->insert("table_name_without_prefix",array("name_of_attribute" => $value_of_attribute,"another" => $more_values)) to insert new objects into tables

Use $mysidia->db->update("table_name_without_prefix",array("name_of_attribute" => $value_of_attribute,"another" => $more_values),"attribute = $value and anotherattribute = $value") to update values in the table. Your adoptables already exist, you just want to update their information. In OwnedAdoptables you don't create a new OwnedAdptable, you just get or set new information, by selecting something from the database or updating it.

2)
To get a random value use rand(min_value,max_value).

3)
Your setObedience function returns no value at all, so in adopt.php doing variable = setObedience() doesn't store anything.

4)
The new adopt is only added to the database after the line mysidia->db->insert("owned_adoptables",...). Before that line, he's an Adoptable, so the adopt->setObedience() function doesn't exist for an Adoptable, since you created it for an OwnedAdoptable.

5) In fact, in adopt.php the line mysidia->db->insert("owned_adoptables",...) creates the adopt, you can't use a query before the adopt actually existing on the table. You can just get a random variable before the insert and in the insert add the obedience value. Like:


...
$adoptobedience = rand(min_value,max_value);
$gender = $adopt->getGender();
$mysidia->db->insert("owned_adoptables", array("aid" => NULL, "type" => $adopt->getType(), "name" => $name, "owner" => $mysidia->user->username, "currentlevel" => 0, "totalclicks" => 0, "code" => $code, "imageurl" => NULL, "usealternates" => $alts, "tradestatus" => 'fortrade', "isfrozen" => 'no', "gender" => $gender, "lastbred" => 0, "obedience" => $adoptobedience));

Hwona
06-05-2014, 02:00 PM
Thank you! Everything works, however, I'm getting this weird error upon adoption(free only): Database error 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' owner='Dream Beauty'' at line 2!
However, the pet still gets adopted. Do you know how to fix this? :3
Otherwise, everything works perfectly and I figured how to do it for bred and shop adoptables - thanks a bunch! Woud this count as mod by any chance? If so, would you mind if I share it? :3