/** * Execute the command. * * @return void */ public function handle() { Log::info('Trying to handle battle'); $subjectBefore = unserialize(serialize($this->subject)); $targetBefore = unserialize(serialize($this->target)); $this->attackArmy($this->subject, $this->target); $targetBefore->units; $subjectBefore->units; $response = json_encode(array('targetBefore' => $targetBefore, 'subjectBefore' => $subjectBefore, 'target' => $this->target, 'subject' => $this->subject, 'report' => array('turns' => $this->report))); $log = new \App\Log(); $log->message = $response; $log->save(); $notificationBody = array('battleId' => $log->id, 'winner' => $this->getWinnerName()); $notification = new \App\Notification(); $notification->type = "battle_result"; $notification->message = json_encode($notificationBody); $this->saveArmy($this->subject); $this->saveArmy($this->target); $this->subject->player->notifications()->save($notification); return $this->winner; }
use Illuminate\Events\Dispatcher; use Illuminate\Container\Container; $capsule = new Capsule(); $capsule->addConnection(['driver' => 'mysql', 'host' => DB_HOST, 'database' => DB_NAME, 'username' => DB_USER, 'password' => DB_PASSWORD, 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '']); // Set the event dispatcher used by Eloquent models... (optional) $capsule->setEventDispatcher(new Dispatcher(new Container())); // Make this Capsule instance available globally via static methods... (optional) $capsule->setAsGlobal(); // Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher()) $capsule->bootEloquent(); // Listen for Query Events for Debug $events = new Dispatcher(); $events->listen('illuminate.query', function ($query, $bindings, $time, $name) { // Format binding data for sql insertion foreach ($bindings as $i => $binding) { if ($binding instanceof \DateTime) { $bindings[$i] = $binding->format('\'Y-m-d H:i:s\''); } else { if (is_string($binding)) { $bindings[$i] = "'{$binding}'"; } } } // Insert bindings into query $query = str_replace(array('%', '?'), array('%%', '%s'), $query); $query = vsprintf($query, $bindings); // Debug SQL queries App\Log::msg('SQL: [' . $query . ']'); }); $capsule->setEventDispatcher($events); // Eloquent Databse init End
/** * The constructor * @throws \Exception * @param array $config */ function __construct($config = []) { if (self::$instance === null) { if (version_compare(phpversion(), '5.6.0', '<')) { ini_set('default_charset', 'UTF-8'); ini_set('mbstring.internal_encoding', 'UTF-8'); } ini_set('mbstring.func_overload', 7); ini_set("pcre.backtrack_limit", 100000000); ini_set("pcre.recursion_limit", 100000000); self::$instance =& $this; } else { throw new \Exception('App already constructed'); } $this->config = new \App\Config($config); if ($this->config->handleErrors) { // @codeCoverageIgnoreStart error_reporting(E_ALL | E_STRICT); ini_set('display_errors', 0); ini_set('display_startup_errors', 0); $handleError = function ($message, $file, $line, $trace) { $data = "Error:"; $data .= "\nMessage: " . $message; $data .= "\nFile: " . $file; $data .= "\nLine: " . $line; $data .= "\nTrace: " . $trace; $data .= "\nGET: " . print_r($_GET, true); $data .= "\nPOST: " . print_r($_POST, true); $data .= "\nSERVER: " . print_r($_SERVER, true); if ($this->config->logErrors && strlen($this->config->logsDir) > 0 && strlen($this->config->errorLogFilename) > 0) { try { $this->log->write($this->config->errorLogFilename, $data); } catch (\Exception $e) { } } if ($this->config->displayErrors) { ob_clean(); $response = new \App\Response\TemporaryUnavailable($data); $response->disableHooks = true; } else { $response = new \App\Response\TemporaryUnavailable(); } $this->respond($response); }; set_exception_handler(function ($exception) use($handleError) { $handleError($exception->getMessage(), $exception->getFile(), $exception->getLine(), $exception->getTraceAsString()); }); register_shutdown_function(function () use($handleError) { $errorData = error_get_last(); if (is_array($errorData)) { $messageParts = explode(' in ' . $errorData['file'] . ':' . $errorData['line'], $errorData['message'], 2); $handleError(trim($messageParts[0]), $errorData['file'], $errorData['line'], isset($messageParts[1]) ? trim(str_replace('Stack trace:', '', $messageParts[1])) : ''); } }); set_error_handler(function ($errorNumber, $errorMessage, $errorFile, $errorLine) { throw new \ErrorException($errorMessage, 0, $errorNumber, $errorFile, $errorLine); }, E_ALL | E_STRICT); // @codeCoverageIgnoreEnd } spl_autoload_register(function ($class) { $this->classes->load($class); }); $this->request = new \App\Request(); if (isset($_SERVER)) { if (isset($_SERVER['REQUEST_METHOD'])) { $this->request->method = $_SERVER['REQUEST_METHOD']; } $path = isset($_SERVER['REQUEST_URI']) && strlen($_SERVER['REQUEST_URI']) > 0 ? urldecode($_SERVER['REQUEST_URI']) : '/'; $position = strpos($path, '?'); if ($position !== false) { $this->request->query = new \App\Request\Query(substr($path, $position + 1)); $path = substr($path, 0, $position); } $basePath = ''; if (isset($_SERVER['SCRIPT_NAME'])) { $scriptName = $_SERVER['SCRIPT_NAME']; if (strpos($path, $scriptName) === 0) { $basePath = $scriptName; $path = substr($path, strlen($scriptName)); } else { $pathInfo = pathinfo($_SERVER['SCRIPT_NAME']); $dirName = $pathInfo['dirname']; if ($dirName === DIRECTORY_SEPARATOR) { $basePath = ''; $path = $path; } else { $basePath = $dirName; $path = substr($path, strlen($dirName)); } } } $scheme = isset($_SERVER['REQUEST_SCHEME']) && $_SERVER['REQUEST_SCHEME'] === 'https' || isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https' || isset($_SERVER['HTTP_X_FORWARDED_PROTOCOL']) && $_SERVER['HTTP_X_FORWARDED_PROTOCOL'] === 'https' || isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off' ? 'https' : 'http'; $host = isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'unknownhost'; $this->request->path = new \App\Request\Path(isset($path[0]) ? $path : '/'); $this->request->base = $scheme . '://' . $host . $basePath; } $this->routes = new \App\Routes(); $this->log = new \App\Log(); $this->components = new \App\Components(); $this->addons = new \App\Addons(); $this->hooks = new \App\Hooks(); $this->assets = new \App\Assets(); $this->data = new \App\Data(); $this->cache = new \App\Cache(); $this->classes = new \App\Classes(); }