} return $app['twig']->render('status.html.twig', array('tammyList' => $tammyList)); }); $app->post("/play", function () use($app) { $tammyList = Tamagotchi::getAll(); foreach ($tammyList as $key => $tammy) { if ($tammy->getName() == $_POST['name']) { $tammy->play(); } } return $app['twig']->render('status.html.twig', array('tammyList' => $tammyList)); }); $app->post("/sleep", function () use($app) { $tammyList = Tamagotchi::getAll(); foreach ($tammyList as $key => $tammy) { if ($tammy->getName() == $_POST['name']) { $tammy->sleep(); } } return $app['twig']->render('status.html.twig', array('tammyList' => $tammyList)); }); $app->post("/passthetime", function () use($app) { $tammyList = Tamagotchi::getAll(); Tamagotchi::passTheTime(); return $app['twig']->render('status.html.twig', array('tammyList' => $tammyList)); }); $app->post("/clear", function () use($app) { Tamagotchi::clear(); return $app['twig']->render('name.html.twig'); }); return $app;
$current_tamagotchi = Tamagotchi::getAll(); $current_tamagotchi[0]->setAttention(10); $food = $current_tamagotchi[0]->getFood(); $sleep = $current_tamagotchi[0]->getSleep(); $attention = $current_tamagotchi[0]->getAttention(); if ($sleep == 0 || $food == 0) { return $app['twig']->render('dead.html.twig'); } else { $current_tamagotchi[0]->setFood($food - 1); $current_tamagotchi[0]->setSleep($sleep - 1); $current_tamagotchi[0]->setAttention($attention + 1); return $app['twig']->render('create_attention.html.twig'); } }); $app->get("/sleep", function () use($app) { $current_tamagotchi = Tamagotchi::getAll(); $current_tamagotchi[0]->setSleep(10); $food = $current_tamagotchi[0]->getFood(); $sleep = $current_tamagotchi[0]->getSleep(); $attention = $current_tamagotchi[0]->getAttention(); if ($sleep == 0 || $food == 0) { return $app['twig']->render('dead.html.twig'); } else { $current_tamagotchi[0]->setFood($food - 1); $current_tamagotchi[0]->setSleep($sleep + 1); $current_tamagotchi[0]->setAttention($attention - 1); return $app['twig']->render('create_sleep.html.twig'); } }); $app->post('/delete_attr', function () use($app) { Tamagotchi::deleteAll();
session_start(); if (empty($_SESSION['list_of_pets'])) { $_SESSION['list_of_pets'] = array(); } $app = new Silex\Application(); $app->register(new Silex\Provider\TwigServiceProvider(), array('twig.path' => __DIR__ . '/../views')); $app->get("/", function () use($app) { return $app['twig']->render('pets.html.twig', array('pets' => Tamagotchi::getAll())); }); $app->post("/pets", function () use($app) { $pet = new Tamagotchi($_POST['name']); $pet->save(); return $app['twig']->render('create_pet.html.twig', array('newpet' => $pet)); }); $app->post("/delete_pets", function () use($app) { Tamagotchi::deleteAll(); return $app['twig']->render('delete_pets.html.twig'); }); //find out how to change so these methods can work on more than the object keyed in array at 0 $app->post("/feeding", function () use($app) { $pet = Tamagotchi::getAll(); if (key($_POST) == "food") { $pet[0]->feed(); } return $app['twig']->render('pets.html.twig', array('pets' => Tamagotchi::getAll())); }); $app->post("/feelings", function () use($app) { $_SESSION['list_of_pets'][0]->setMood($_SESSION['list_of_pets'][0]->getMood() + 5); return $app['twig']->render('pets.html.twig', array('pets' => Tamagotchi::getAll())); }); return $app;
Tamagotchi::deleteAll(); return $app['twig']->render('home.html.twig', array('tamagotchis' => Tamagotchi::getAll())); }); $app->post('/feed', function () use($app) { foreach ($_SESSION['list_of_tamagotchis'] as $key => $tamagotchi) { if ($tamagotchi->getName() == $_POST['name']) { $tamagotchi->feed(); } } Tamagotchi::ageAll(); return $app['twig']->render('home.html.twig', array('tamagotchis' => Tamagotchi::getAll())); }); $app->post('/attend', function () use($app) { foreach ($_SESSION['list_of_tamagotchis'] as $key => $tamagotchi) { if ($tamagotchi->getName() == $_POST['name']) { $tamagotchi->attend(); } } Tamagotchi::ageAll(); return $app['twig']->render('home.html.twig', array('tamagotchis' => Tamagotchi::getAll())); }); $app->post('/sleep', function () use($app) { foreach ($_SESSION['list_of_tamagotchis'] as $key => $tamagotchi) { if ($tamagotchi->getName() == $_POST['name']) { $tamagotchi->sleep(); } } Tamagotchi::ageAll(); return $app['twig']->render('home.html.twig', array('tamagotchis' => Tamagotchi::getAll())); }); return $app;
<?php require_once __DIR__ . "/../vendor/autoload.php"; require_once __DIR__ . "/../src/Tamagotchi.php"; session_start(); if (empty($_SESSION['tamagotchis'])) { $_SESSION['tamagotchis'] = array(); } $app = new Silex\Application(); $app->register(new Silex\Provider\TwigServiceProvider(), array('twig.path' => __DIR__ . '/../views')); $app->get("/", function () use($app) { return $app['twig']->render('create_tamagotchi.html.twig', array('tamas' => Tamagotchi::getAll())); }); $app->post("/tamagotchis_page", function () use($app) { $tama = new Tamagotchi($_POST['name']); $tama->save(); return $app['twig']->render('tamagotchis_page.html.twig', array('newtama' => $tama)); }); $app->post("/add_food", function () use($app) { $tama->addFood(); return $app['twig']->render('tamagotchis_page.html.twig'); }); // $tama = new Tamagotchi($_POST['name']); // $tama->addFood(); // return $app['twig']->render('tamagotchis_page.html.twig', array('newtama' => $tama)); // // $app->post("/delete_tama", function() use ($app) { // Tamagotchi::deleteAll(); // return $app['twig']->render('delete_place.html.twig'); // }); return $app;