Ejemplo n.º 1
0
 public function action_index()
 {
     $result = DB::select('points', 'name')->from('user_fortune')->join('user')->on('user.id', '=', 'user_fortune.id')->order_by('user_fortune.points', 'desc')->limit(10)->execute()->as_array();
     $ranking = ['points' => $result];
     $exp = ['components' => [View_Twig::forge('game/component/status'), View_Twig::forge('game/component/map'), View_Twig::forge('game/component/ranking', $ranking), View_Twig::forge('game/component/station_change_list'), View_Twig::forge('game/component/station_history'), View_Twig::forge('game/component/chat')], 'js' => [View_Twig::forge('game/js/map_position'), View_Twig::forge('game/js/train_position'), View_Twig::forge('game/js/my_position'), View_Twig::forge('game/js/station_change_list'), View_Twig::forge('game/js/station_history'), View_Twig::forge('game/js/chat')]];
     $this->template->content = View_Twig::forge('game/ride', $exp);
 }
Ejemplo n.º 2
0
 public function action_battle()
 {
     //マニュアル バトルの仕方
     $exports = [];
     $this->template->title = 'Metro Royal Manual';
     $this->template->navigation = View_Twig::forge('portal/_navigation');
     $this->template->content = View_Twig::forge('manual/battle', $exports);
 }
Ejemplo n.º 3
0
 public function shutdown()
 {
     $queries = $this->logger->queries;
     $controller = $this->request->controller_instance;
     $tables = array();
     $subqueries = array();
     $queryTables = array();
     $sql = '';
     // Add the template files to be checked
     $template_loader = \View_Twig::loader();
     if (!is_null($template_loader) && method_exists($template_loader, 'getFiles')) {
         $templates = $template_loader->getFiles();
         $this->files = array_unique(array_merge($this->files, $templates));
     }
     // Add all loaded files within the app root, excluding cache and model proxies
     $this->files = array_merge($this->files, array_filter(get_included_files(), function ($path) {
         return strpos($path, APPPATH) === 0 && strpos($path, APPPATH . 'cache') !== 0 && strpos($path, APPPATH . 'classes/proxy') !== 0;
     }));
     $model_classes = array_filter(get_declared_classes(), function ($class) {
         return strpos($class, 'Model_') !== false;
     });
     // Construct an allowed list of tables for the cache queries
     $em = \D::manager();
     foreach ($model_classes as $model) {
         $meta = $em->getClassMetadata($model);
         if ($meta->isMappedSuperclass || !isset($meta->columnNames['updated_at']) || $meta->rootEntityName != $meta->name) {
             continue;
         }
         if (!in_array($meta->table['name'], $tables)) {
             $tables[] = $meta->table['name'];
         }
     }
     // Remove the app path from each file path, making them relative
     $this->files = array_map(function ($path) {
         return str_replace(PROJECTROOT, '', $path);
     }, $this->files);
     // Construct ourselves a number of sub queries to check all the relevant records in the database
     $num = 0;
     foreach ($queries as $query) {
         $parser = new \PHPSQL\Parser();
         $parsed = $parser->parse($query, true);
         if (!isset($parsed['FROM']) || count($parsed['FROM']) === 0) {
             continue;
         }
         $aliases = array();
         foreach ($parsed['FROM'] as $part) {
             if ($part['expr_type'] == 'table' && in_array($part['table'], $tables)) {
                 $aliases[] = isset($part['alias']['name']) ? $part['alias']['name'] : $part['table'];
             }
         }
         $from_pos = $parsed['FROM'][0]['position'];
         if (isset($parsed['ORDER']) && count($parsed['ORDER']) > 0) {
             $append = ' FROM ' . substr($query, $from_pos, $parsed['ORDER'][0]['position'] - $from_pos - 10);
         } else {
             $append = ' FROM ' . substr($query, $from_pos);
         }
         if (count($aliases) > 1) {
             $append = ', (COUNT(' . implode('.id)+COUNT(', $aliases) . '.id)) as count' . $append;
         } else {
             if (count($aliases) === 1) {
                 $append = ', COUNT(' . $aliases[0] . '.id) as count' . $append;
             } else {
                 continue;
             }
         }
         $subqueries[] = 'q' . $num;
         $queryTables[] = '(SELECT ' . (count($aliases) > 1 ? 'GREATEST(' : '') . 'COALESCE(MAX(' . implode('.updated_at),\'1000-01-01\'), COALESCE(MAX(', $aliases) . '.updated_at),\'1000-01-01\')' . (count($aliases) > 1 ? ')' : '') . ' AS updated_at' . $append . ') q' . $num;
         $num++;
     }
     if (count($this->queries) > 0) {
         foreach ($this->queries as $query) {
             $parser = new \PHPSQL\Parser();
             $parsed = $parser->parse($query, true);
             $field = null;
             if (!isset($parsed['FROM']) || count($parsed['FROM']) === 0) {
                 continue;
             }
             if (!isset($parsed['SELECT']) || count($parsed['SELECT']) === 0) {
                 continue;
             }
             foreach ($parsed['SELECT'] as $select) {
                 if ($select['expr_type'] == 'colref') {
                     $field = $select['base_expr'];
                     break;
                 }
             }
             if ($field === null) {
                 continue;
             }
             $aliases = array();
             foreach ($parsed['FROM'] as $part) {
                 if ($part['expr_type'] == 'table') {
                     $aliases[] = isset($part['alias']['name']) ? $part['alias']['name'] : $part['table'];
                 }
             }
             $from_pos = $parsed['FROM'][0]['position'];
             if (isset($parsed['ORDER']) && count($parsed['ORDER']) > 0) {
                 $append = ' FROM ' . substr($query, $from_pos, $parsed['ORDER'][0]['position'] - $from_pos - 10);
             } else {
                 $append = ' FROM ' . substr($query, $from_pos);
             }
             if (count($aliases) > 1) {
                 $append = ', (COUNT(' . implode('.' . $field . ')+COUNT(', $aliases) . '.' . $field . ')) as count' . $append;
             } else {
                 if (count($aliases) === 1) {
                     $append = ', COUNT(' . $aliases[0] . '.' . $field . ') as count' . $append;
                 } else {
                     continue;
                 }
             }
             $subqueries[] = 'q' . $num;
             $queryTables[] = '(SELECT ' . (count($aliases) > 1 ? 'GREATEST(' : '') . 'COALESCE(MAX(' . implode('.' . $field . '),\'1000-01-01\'), COALESCE(MAX(', $aliases) . '.' . $field . '),\'1000-01-01\')' . (count($aliases) > 1 ? ')' : '') . ' AS updated_at' . $append . ') q' . $num;
             $num++;
         }
     }
     if (count($queryTables)) {
         // TODO: proper chunking of subqueries! For now, we will truncate them if they exeed MySQL's limit of 61
         if (count($queryTables) > 60) {
             $queryTables = array_slice($queryTables, 0, 60);
             $subqueries = array_slice($subqueries, 0, 60);
         }
         // Complete the mega query that will check if items are updated or not...
         if (count($subqueries) > 1) {
             $sql = 'SELECT GREATEST(' . implode('.updated_at, ', $subqueries) . '.updated_at) AS updated_at, (' . implode('.count+', $subqueries) . '.count) AS count FROM ' . implode(', ', $queryTables);
         } else {
             $sql = 'SELECT q0.updated_at, q0.count FROM ' . implode(', ', $queryTables);
         }
         // Run the query - this must be done now because we can't reliably get the correct results from what we have
         try {
             $result = \DB::query($sql)->execute()->as_array();
             $result = $result[0];
             $result['updated_at'] = strtotime($result['updated_at']);
         } catch (\Exception $e) {
             // We can't continue if the query doesn't work
             return;
         }
     }
     // Add the rest of the stuff to the result
     $result['query_count'] = $num;
     $result['sql'] = $sql;
     $result['files'] = $this->files;
     $result['content'] = strval($this->request->response);
     $result['nocache'] = \CMF\Cache::getNoCacheAreas($result['content']);
     $result['logs_made'] = \CMF\Log::$logs_made;
     $result['content-type'] = 'text/html; charset=utf-8';
     $result['template'] = \CMF::$template;
     $result['module'] = \CMF::$module;
     // Store the content type header if it's set
     $headers = headers_list();
     foreach ($headers as $header) {
         if (stripos($header, 'content-type: ') === 0) {
             $result['content-type'] = substr($header, 14);
             break;
         }
     }
     // serialize and write it to disk
     \CMF\Cache::writeCacheFile($this->path, serialize($result));
 }
Ejemplo n.º 4
0
 public function action_entry()
 {
     // 登録時
     if (Input::method() == 'POST') {
         // バリデーション
         $val = Model_Twitteruser::validate('create');
         $input = array('uid' => Session::get('uid'), 'token' => Session::get('token'), 'secret' => Session::get('secret'));
         if ($val->run($input)) {
             // バリデーション成功時
             $user = Model_User::forge(array('name' => Session::get('nickname'), 'password' => substr(str_shuffle('1234567890abcdefghijklmnopqrstuvwxyz'), 0, 10), 'sex' => Input::post('sex'), 'birth_station_id' => Input::post('birth_station_id')));
             $twitter_user = Model_TwitterUser::forge($input);
             if ($user and $twitter_user) {
                 // ユーザー生成成功
                 try {
                     \DB::start_transaction();
                     if ($user->save() === false) {
                         // User保存失敗
                         throw new \Exception('user save failed.');
                     }
                     $twitter_user->user_id = $user->id;
                     if ($twitter_user->save() === false) {
                         // TwitterUser保存失敗
                         throw new \Exception('twitter_user save failed.');
                     }
                     //その他Userに付随する情報を作成
                     //user_state
                     $state = Model_Userstate::forge(array('id' => $user->id, 'ride_state' => '0', 'now_station_id' => $user->birth_station_id));
                     if ($state->save() === false) {
                         // user_state保存失敗
                         throw new \Exception('user_state save failed.');
                     }
                     //user_fortune
                     //ポイントは動的にできるように。。。
                     $fortune = Model_Userfortune::forge(array('id' => $user->id, 'points' => 10000, 'domination1' => ' ', 'domination2' => ' ', 'domination3' => ' '));
                     if ($fortune->save() == false) {
                         // user_state保存失敗
                         throw new \Exception('user_fortune save failed.');
                     }
                     // 保存成功
                     \DB::commit_transaction();
                     //サインアップ成功なのでいらないSessionは消す
                     Session::delete('uid');
                     Session::delete('token');
                     Session::delete('secret');
                     Session::delete('nickname');
                     Model_User::login_twitter($user->id);
                     Response::redirect('portal');
                 } catch (\Exception $e) {
                     \DB::rollback_transaction();
                     Response::redirect('portal');
                 }
             } else {
                 // ユーザー生成失敗
                 Response::redirect('portal');
             }
         } else {
             // バリデーション失敗時
             Response::redirect('portal');
         }
         // 登録できたら ポータルに戻る.
     }
     // Viewに受け渡す用
     $exp = ['name' => Session::get('nickname'), 'stations' => Model_Traininfo::get_birth_train_summary()];
     $this->template->title = '新規アカウント登録';
     $this->template->content = View_Twig::forge('portal/entry', $exp);
 }
Ejemplo n.º 5
0
 public function action_index()
 {
     $this->template->content = View_Twig::forge('game/on');
 }
Ejemplo n.º 6
0
 /**
  * Processes the value of this field before getting set to the model in the populate() method
  * @see \CMF\Model\Base::populate()
  * @param mixed $value The value about to be set
  * @param array $settings The settings for this field
  * @param object $model The model
  * @return mixed The value ready for setting on the model
  */
 public static function process($value, $settings, $model)
 {
     if (!(isset($settings['auto_update']) && !$settings['auto_update']) && (isset($settings['template']) && !empty($settings['template']))) {
         $post_data = $context = \Input::post();
         if (!is_array($context)) {
             $context = array();
         }
         $context['model'] = $model;
         if (class_exists('Model_Settings')) {
             $context['settings'] = \Model_Settings::instance();
         }
         $twig = \View_Twig::parser();
         $loader = \View_Twig::loader();
         if (!isset($loader) || is_null($loader)) {
             $loader = new Twig_Loader_String();
             $twig->setLoader($loader);
             \View_Twig::setLoader($loader);
         }
         return $twig->render($settings['template'], $context);
     }
     return $value;
 }
Ejemplo n.º 7
0
 public function action_index()
 {
     $exports = ['components' => [View_Twig::forge('game/component/status'), View_Twig::forge('game/component/train')], 'js' => [View_Twig::forge('game/js/map_position'), View_Twig::forge('game/js/train_position'), View_Twig::forge('game/js/my_position')]];
     $this->template->content = View_Twig::forge('game/game', $exports);
     //$this->template->content = View_Twig::forge('game/index', $exports);
 }
Ejemplo n.º 8
0
 /**
  * Loops through the provided nocache names and injects their executed values
  * into the content
  * 
  */
 public static function addNoCacheAreas($names, $content, $context)
 {
     if (count($names) === 0 || !$names) {
         return $content;
     }
     static::$modified = true;
     // Set up the twig environment for rendering the non-cached parts
     \View_Twig::initLoader();
     $env = \View_Twig::parser();
     $template = new \CMF\Twig\TemplateInclude($env);
     if (empty($context)) {
         $context = array();
     }
     if (!empty($context['template'])) {
         $module = @$context['module'];
         // Determine whether the ViewModel class exists...
         if ($viewClass = \CMF::hasViewModel($context['template'])) {
             $context['view'] = new $viewClass('view', false, $context['template']);
         } else {
             try {
                 $viewClass = ucfirst($module) . '\\View_Base';
                 if (!class_exists($viewClass)) {
                     $viewClass = '\\View_Base';
                 }
                 $context['view'] = new $viewClass('view', false, $context['template']);
             } catch (\Exception $e) {
             }
         }
     }
     foreach ($names as $name => $include) {
         $content = preg_replace('/<!-- nocache_' . $name . ' .*<!-- endnocache_' . $name . ' -->/sU', $template->renderInclude($include, $context), $content);
     }
     return $content;
 }