Skip to content

Instantly share code, notes, and snippets.

@httpspace
Last active August 29, 2015 14:17
Show Gist options
  • Save httpspace/cc25edc08549dd722de8 to your computer and use it in GitHub Desktop.
Save httpspace/cc25edc08549dd722de8 to your computer and use it in GitHub Desktop.
<?php
use \GameController as GameCtrl,
\MainController as MainCtrl,
\Su\CooRoute as CooRoute,
\AppraisalController as Appraisal,
\JumpController as JumpCtrl;
class PlayController extends \BaseController
{
private static $js, $css, $tip='';
public static $jumpImages = array(
'/images/support_fans_1.gif',
'/images/support_fans_2.gif',
'/images/support_fans_3.gif',
);
public static $LayoutConf = array(
'js' => array(
'/Artists/js/index.js',
),
'css' => array(),
'aside' => true,
);
public static $gameConf = array(
'0' => array('call'=>'Poll' ,'start'=>'投票', 'verb'=>'投票'),
'1' => array('call'=>'Puzzle_Multiple' ,'start'=>'開始', 'verb'=>'作答'),
'2' => array('call'=>'Test_Multiple' ,'start'=>'開始', 'verb'=>'測驗'),
'3' => array('call'=>'Test_Single' ,'start'=>'開始', 'verb'=>'測驗'),
'4' => array('call'=>'Puzzle_Single' ,'start'=>'開始', 'verb'=>'作答'),
'5' => array('call'=>'test_Random' ,'start'=>'開始', 'verb'=>'測驗'),
);
public static $uid, $GameData, $relatedGame = [], $getTypeCode, $viewParam = [];
/**
* 資源檔設定
* @param int $game_type 遊戲類型
*/
public static function LoadResources ( $game_type, $page=false )
{
self::$js = &self::$LayoutConf['js'];
self::$css = &self::$LayoutConf['css'];
$js = &self::$js;
$css = &self::$css;
if( in_array( $game_type, [1,4] ) )
{
$css[] = 'jcrop/css/jquery.Jcrop.min.css';
$css[] = '/Artists/css/jquery/minicolors/jquery.minicolors.css';
$js[] = 'jcrop/js/jquery.Jcrop.min.js';
$js[] = '/js/Youtube/iframe_api.min.js';
$js[] = '/js/JsonString.js';
}
if( !in_array( $game_type, [5,0] ) )
{
$js[] = '/js/Game/play.js';
}
if( $game_type == 0 )
{
$css[] = '/css/Poll/Poll.css';
$js[] = '/js/class.js';
$js[] = '/js/Poll/Poll.js';
}
$js[] = '/js/User/load_collection.js';
}
public function __construct()
{
self::$getTypeCode = MainCtrl::getTypeCode();
error_reporting(E_ALL & ~E_NOTICE);
}
/* 所有遊戲的開端 */
public function getGameById ($gid)
{
// show(Input::all());
$this->init($gid);
$GameData = self::$GameData;
$game_id = $_COOKIE['game_id'];
$gid = $GameData['gid'];
$gameType = $GameData['game_type'];
$conf = self::$gameConf[$gameType];
$route = CooRoute::getRouter();
/* 如果 game_id (為空) 或 (不是當前做題) ->->-> 開始頁 */
if( empty($game_id) || $game_id != $gid )
{
CooRoute::setRouter('play');
}
/* (當前題目為與 cookie題目相同) */
// else if( $game_id == $gid )
// {
// /* (要求結果頁) 並且 (有作答) 或者 (隨機測驗) ->->-> 結果頁 */
// if( $route == 'result' && (
// isset( $_POST['option'] ) ||
// $gameType == 5
// )) CooRoute::setRouter('result');
// }
CooRoute::make('game_id', $gid);
$html = call_user_func(array($this, $conf['call']), $GameData );
return $this->Layout($html);
}
/* 初始化 */
public function init($gid)
{
/* 目前觀看的使用者id */
$uid = Account::get_login_user();
/* get game info */
$GameData = GameCtrl::getGamedata($gid, true);
$game_type = $GameData['game_type'];
$gid = $GameData['gid'];
$cid = $GameData['cid'];
/* 判斷是否上線中 */
if($GameData['state'] != 1)
{
if(!($GameData['state'] == 3 && ($uid == $GameData['uid'] or SentryMem::hasAccess("articlecategories.edit"))))
{
die(Layout::error(array('title' => '遊戲未上架'))->out());
}
}
/* 判斷此遊戲的公開狀態 */
if($GameData['permissions'] == 1 && ($uid == $GameData['uid'] or SentryMem::hasAccess("articlecategories.edit")))
{
die(Layout::error(array('title' => '遊戲不公開'))->out());
}
/* 取得遊戲創造者資訊 */
$GameData['game_user_info'] = Account::userInfo($GameData['user']['id']);
/* 設定遊戲基本參數 */
$GameData['url'] = self::getGameUrl( $gid );
$GameData['create_date'] = date('Y年m月d日',strtotime($GameData['created_at']));
$GameData['game_info'] = GameCtrl::$gameinfos[$game_type];
$GameData['game_typeCode'] = self::$getTypeCode[$GameData['game_info']['tid']];
$GameData['type_info'] = MainCtrl::getTypeInfos()[$GameData['game_typeCode']];
$GameData['game_conf'] = self::$gameConf[$game_type];
/* 把遊戲存回靜態 */
self::$GameData = $GameData;
/* 載入資原檔 (css, js) */
self::LoadResources( $game_type, '' );
/* 描述過長或空值處理 */
$desc = $GameData['game_description'];
if(empty($desc)) $desc = $GameData['game_name'];
if(mb_strlen($desc)>20) $desc = mb_substr($desc,1,20).'...';
/* 設定網頁title */
Layout::title($GameData['game_name']);
/* 取得FB用img */
if(in_array($GameData['MediaType'],[0,2]))
{
$filename = sha1($gid);
$FBimg = '/fb_og/'.substr($filename,0,3).'/'.substr($filename,3,strlen($filename)-3).'.jpg';
}
/* set meta */
self::$LayoutConf['meta'] = array(
array('property' => 'og:title', 'content' => $GameData['game_name'].'|QQbuzz'),
array('property' => 'og:description', 'content' => $desc),
array('property' => 'og:url', 'content' => self::getGameUrl( $gid )),
array('property' => 'og:image', 'content' => asset($FBimg)),
);
/* 設定 CooRoute 找不到的動作 */
CooRoute::noFound(function (){
return CooRoute::nextHome()->callHome();
});
/* jump */
CooRoute::set('jump', function () use ( $game_type, $cid ){
Layout::js('/js/FB/event.js');
/* 如果重整回到首頁 */
CooRoute::nextHome();
if(!in_array($game_type, array(0,5)))
{
$play_result = self::getPlayResult();
if( $play_result !== false ) self::rememberTemp($play_result);
}
return View::make('Main._jump.jump', array(
'next' => in_array($game_type, array(0,5)) ? 'play' : 'result',
'fbid' => JumpCtrl::get_fansid($game_type, $cid),
'img' => self::$jumpImages[rand(0,2)],
));
});
return $this;
}
/* 投票 */
public function Poll($GameData)
{
$gid = $GameData['gid'];
$uid = Account::get_login_user();
if($GameData['uid'] == $uid) self::$tip = '您是此投票作者,投票後的結果不會列入計算!';
else if(Record::is_played($uid, $gid)) self::$tip = '您已完成此投票,重新投票後的結果不會列入計算!';
return CooRoute::home('', function () use ( $GameData, $uid, $gid ) {
return self::public_StartPage( $GameData, JumpCtrl::hit() ?: 'play' );
})->set('play', function () use ( $GameData, $uid, $gid ) {
self::needLogin();
$html = self::poll_play_result( $GameData );
Visitor::inc( $gid );
return $html;
})->start();
}
/* 隨機心測 */
public static function test_Random ( $GameData )
{
$gid = $GameData['gid'];
return CooRoute::home('', function () use ( $GameData ) {
// echo 'home設定';
Layout::js('js/Game/randomTest.js');
return self::public_StartPage( $GameData, JumpCtrl::hit() ?: 'play' );
})->set('play', function () use ( $GameData, $gid ) {
// echo 'play設定';
/* 需要登入 */
self::needLogin();
/* 顯示結果 */
CooRoute::nextHome();
$html = self::result_testRandom( $GameData );
/* 紀錄 */
Visitor::inc( $gid );
return $html;
})->start();
}
/* 益智多題 */
public static function Puzzle_Multiple ( $GameData )
{
return CooRoute::home('', function () use ( $GameData ) {
self::forgetTemp();
return self::public_StartPage( $GameData );
})->set('play', function () use ( $GameData ) {
CooRoute::nextHome();
return self::public_PlayPage( $GameData, JumpCtrl::hit() ?: 'result' );
})->set('result', function () use ( $GameData ) {
$play_result = self::getPlayResult();
/* 需要登入 */
self::needLogin(function ($is_login)use( $play_result ){
if(!$is_login && $play_result) self::rememberTemp($play_result);
return $is_login;
});
CooRoute::nextHome();
/* 無做答,回首頁 */
if($play_result===false || empty($play_result))
{
return CooRoute::callHome();
}
JumpCtrl::unset_share_type();
/* 顯示結果 */
$html = self::result_puzzle( $GameData, $play_result);
/* 紀錄作答 */
Record::play($GameData['gid'], $play_result);
Visitor::inc($GameData['gid']);
return $html;
})->start();
}
/* 益智單題 */
public static function Puzzle_Single ( $GameData )
{
return CooRoute::home('play', function () use ( $GameData ) {
return self::public_PlayPage( $GameData, JumpCtrl::hit() ?: 'result' );
})->set('result', function () use ( $GameData ) {
$play_result = self::getPlayResult();
/* 需要登入 */
self::needLogin(function ($is_login)use(&$play_result){
if(!$is_login && $play_result) self::rememberTemp($play_result);
return $is_login;
});
CooRoute::nextHome();
/* 無做答,回首頁 */
if($play_result===false || empty($play_result))
{
return CooRoute::callHome();
}
JumpCtrl::unset_share_type();
/* 顯示結果 */
$html = self::result_puzzle( $GameData, $play_result);
/* 紀錄作答 */
Record::play($GameData['gid'], $play_result);
Visitor::inc($GameData['gid']);
return $html;
})->start();
}
/* 心測單選 */
public static function Test_Single ( $GameData )
{
return CooRoute::home('', function () use ( $GameData ) {
return self::public_StartPage( $GameData );
})->set('play', function () use ( $GameData ) {
CooRoute::nextHome();
return self::public_PlayPage( $GameData, JumpCtrl::hit() ?: 'result' );
})->set('result', function () use ( $GameData ) {
$play_result = self::getPlayResult();
/* 需要登入 */
self::needLogin(function ($is_login)use(&$play_result){
if(!$is_login && $play_result) self::rememberTemp($play_result);
return $is_login;
});
CooRoute::nextHome();
/* 無做答,回首頁 */
if($play_result===false || empty($play_result))
{
return CooRoute::callHome();
}
JumpCtrl::unset_share_type();
/* 顯示結果 */
$html = self::result_testSingle( $GameData, $play_result);
/* 紀錄作答 */
Record::play($GameData['gid'], $play_result);
Visitor::inc($GameData['gid']);
return $html;
})->start();
}
/* 心測多選 */
public static function Test_Multiple ( $GameData )
{
return CooRoute::home('', function () use ( $GameData ) {
return self::public_StartPage( $GameData );
})->set('play', function () use ( $GameData ) {
CooRoute::nextHome();
return self::public_PlayPage( $GameData, JumpCtrl::hit() ?: 'result' );
})->set('result', function () use ( $GameData ) {
$play_result = self::getPlayResult();
/* 需要登入 */
self::needLogin(function ($is_login)use(&$play_result){
if(!$is_login && $play_result) self::rememberTemp($play_result);
return $is_login;
});
/* 無做答,回首頁 */
if($play_result===false || empty($play_result))
{
return CooRoute::nextHome()->callHome();
}
/* 顯示結果 */
CooRoute::nextHome();
/* 清空分享模式2015/3/23 */
JumpCtrl::unset_share_type();
$html = self::result_testMultiple( $GameData, $play_result);
/* 紀錄 */
Record::play($GameData['gid'], $play_result);
Visitor::inc($GameData['gid']);
return $html;
})->start();
}
/**
私有方法
*/
/**
* 暫存方法
*/
private static function forgetTemp ()
{
$result_key = CooRoute::get('result_key');
Cache::forget($result_key);
CooRoute::forget($result_key);
}
private static function rememberTemp($play_result)
{
$result_key = substr(md5($_SERVER['REMOTE_ADDR']), 0, 10);
CooRoute::make('result_key', $result_key);
Cache::put($result_key, $play_result, 5);
}
private static function getTemp ()
{
$play_result = false;
if(CooRoute::has('result_key'))
{
if(Cache::has($result_key = CooRoute::get('result_key')))
{
$play_result = Cache::get( $result_key );
self::forgetTemp();
}
}
return $play_result;
}
/**
* 投票
* @return [type] [description]
*/
private static function poll_play_result ( $GameData )
{
$gid = $GameData['gid'];
$uid = Account::get_login_user();
return View::make('Main._game.Poll.play', array(
'gameId' => $gid,
'gameName' => $GameData['game_name'],
'gameDesc' => $GameData['game_description'],
'gameData' => $GameData['game_data'],
'voteCountData' => Poll::optionCount($GameData['gid']),
));
}
/**
* 隨機心理測驗
* @param array $GameData 遊戲資訊
* @return string html
*/
private static function result_testRandom ( $GameData )
{
$uid = Account::get_login_user();
$playerInfo = Account::userInfo($uid);
/* 依照uid & 項目數量 產生結果序號 */
$int = filter_var(md5($uid), FILTER_SANITIZE_NUMBER_INT);
$int = (int)substr($int, 0, 6);
$results = $GameData['game_result'];
$resultCount = count($results);
$resultOption = $int % $resultCount;
$result = $results[$resultOption];
$result['desc'] = $GameData['game_description'];
$result['result_val'] = strtr($result['result_val'], ['[!--@--]' => $playerInfo['username']]);
return View::make('Main._game.result_randomTest', ['result' => $result]);
}
/**
* 測驗結果頁 - 單題
* @param array $GameData 遊戲資訊
* @param array $play_result 遊戲者做的題目
* @return string 結果頁內容 (html)
*/
private static function result_testSingle ( $GameData, $play_result )
{
$uid = Account::get_login_user();
$playerInfo = Account::userInfo($uid);
$option = [];
foreach($GameData['game_data'] as $game_data_key => $game_data_val)
{
foreach($game_data_val['option'] as $key=>$val) $option[$game_data_key][$val['OID']] = $val;
}
foreach($play_result as $key=>$val)
{
$result = (isset($option[$key][$val])) ? $option[$key][$val] : die("<b>別亂試答案!</b>");
}
$result['option_detail']['result_detail']=strtr($result['option_detail']['result_detail'], ['{FB_USERNAME}' => $playerInfo['username']]);
return View::make('Main._game.result_test', ['result' => $result]);
}
/**
* 測試結果頁 - 多題
* @param array $GameData 遊戲資訊
* @param array $play_result 遊戲者作答
* @return string html
*/
private static function result_testMultiple( $GameData, $play_result )
{
$uid = Account::get_login_user();
$playerInfo = Account::userInfo($uid);
$option = [];
foreach($GameData['game_data'] as $game_data_key => $game_data_val)
{
foreach($game_data_val['option'] as $key => $val)
{
$option[$game_data_key][$val['OID']] = $val;
}
}
foreach($play_result as $key => $val)
{
if(isset($option[$key][$val])){
$result[]=$option[$key][$val];
}else{
// 紀錄try答案的使用者id或ip
die("<b>別亂試答案!</b>");
}
}
$Weights = array_column($result,'Weights');
foreach($Weights as $Weights_key => $Weights_val)
{
foreach($Weights_val as $key => $val)
{
if(empty($sum[$key]))$sum[$key]=0;
$sum[$key]+=$val;
}
}
$max_tmp = max($sum);
foreach($sum as $key => $val)
{
if($max_tmp == $val)
{
$max_array[] = $key;
}
}
$max = $max_array[array_rand($max_array)];
$result = $GameData['game_result'][$max];
$result['result_val']=strtr($result['result_val'], ['{FB_USERNAME}' => $playerInfo['username']]);
$result['result_detail']=strtr($result['result_detail'], ['{FB_USERNAME}' => $playerInfo['username']]);
return View::make('Main._game.result_test', ['result' => $result]);
}
/**
* 益智結果頁 - 單題 & 多題
* @param array $GameData 遊戲資訊
* @param array $play_result 遊戲者做的題目
* @return string 結果頁內容 (html)
*/
private static function result_puzzle ( $GameData, $play_result )
{
$resultNum = 0;
$Correct = array();
$inCorrect = array();
$Correct_array = array();
$game_data = $GameData['game_data'];
$game_user = $GameData['game_user_info'];
// 整理 ?
foreach($game_data as $key => &$val)
{
$tmp = array();
$ans = $val['option_result'];
foreach($val['option'] as $option_key => $option_val)
{
if($ans == $option_key)
{
$val['option_result'] = $option_val['OID'];
}
$tmp[$option_val['OID']] = $option_val;
}
$val['option'] = $tmp;
$val['option_detail']['userid'] = $game_user['id'];
$val['option_detail']['username'] = $game_user['username'];
$val['option_detail']['userurl'] = $game_user['page_personal'];
$val['option_detail']['created_at'] = strtotime($GameData['created_at']);
}
foreach($game_data as $key => &$val)
{
$Correct_array[$key]['haveDetail'] = isset($val['option_detail']);
$result = ( $val['option_result'] == $play_result[$key] );
if($result)
{
$temp = &$Correct;
$resultNum++;
}
else
{
$temp = &$inCorrect;
if(isset($val['option'][$play_result[$key]]))
{
$inCorrect[$key]['c'] = $val['option'][$play_result[$key]];
}
else
{
//當使用者選擇不是答案內的答案,紀錄try答案的使用者id或ip
die("別亂試答案");
}
}
$temp[$key]['q'] = $val['question'];
$temp[$key]['a'] = $val['option'][$val['option_result']];
$temp[$key]['d'] = $val['option_detail'];
$temp[$key]['result_header'] = 'heading'.($key+1);
$temp[$key]['result_wrong'] = 'wrong'.($key+1);
$Correct_array[$key]['result'] = $result;
}
$game_result = array();
if(!empty($GameData['game_result']))
{
$GameData['game_result'] = array_reverse($GameData['game_result']);
foreach($GameData['game_result'] as $val)
{
$inner = explode(",", $val['message_inner']);
$range = range($inner[0], $inner[1]);
if(in_range($resultNum, $inner[0]+1, $inner[1]))
{
$game_result = $val;
break;
}
}
}
$with = array(
'Correct_array' => $Correct_array,
'inCorrect' => $inCorrect,
'Correct' => $Correct,
'resultNum' => $resultNum,
'QuestionCount' => count($game_data),
'detail' => $detail,
'game_result' => $game_result,
);
return View::make('Main._game.result_puzzle', $with);
}
private static function getPlayResult ()
{
return Input::has('option') ? json_decode(Input::get('option'), true) : self::getTemp();
}
/**
開放工具
*/
/**
* 登記判斷登入後的動作
* @param boolean $callback 可以傳入function
* @return any false->顯示登入頁面, other->回傳
*/
public static function needLogin ($callback=false)
{
$is_login = Account::is_login();
if(is_callable($callback))
{
return call_user_func($callback, $is_login) ?: die(MainCtrl::pleaseLogin());
}
else
{
return $is_login ?: die(MainCtrl::pleaseLogin());
}
}
/**
* 回傳遊戲頁網址
* @param int,str $gid 遊戲序號
* @return str 遊戲網址
*/
public static function getGameUrl ($gid)
{
return is_local ? "http://".$_SERVER['HTTP_HOST']."/$gid" : "http://tw.qqbuzz.com/$gid";
}
/**
* 版面
* @param string $content 主要遊戲內容 html 碼
* @param array $conf 設定參數,同MainController::Layout的第二個參數
*/
public static function Layout ($content='', $conf=[])
{
$header = self::HeaderHtml();
$social = self::SocialHtml();
$detail = self::DetailHtml();
$tag = self::TagHtml();
$comments = self::Comments(['url' => self::$GameData['url']]);
$related = self::relatedHtml();
$sameCategory = is_mobile ? '' : self::sameCategoryHtml();
$tip = self::$tip;
$content = View::make('Main._game.base', array(
'tip' => $tip,
'typeClass' => MainCtrl::getTypeInfos()[self::$GameData['game_typeCode']]['class'],
'content' =>
$header.
$social.
$detail.
$content.
(CooRoute::getRouter() == 'result' ? self::lightBox() : '').
DM::_300('above_tags', '標籤上方').
( is_mobile ? ($social.$tag) : ($tag.$social) ).
self::doubleDM().
$comments.
DM::_300('follow_fb_comments', '留言板下方').
$related.
$sameCategory
));
$conf = array_merge($conf, self::$LayoutConf);
return Layout::content($content)->gameType(self::$GameData['game_type'])->config($conf)->out();
}
public static function lightBox ()
{
$dm = false;
$fans_url = false;
$GameData = self::$GameData;
switch ( JumpCtrl::boxContent() )
{
case 'fans':
$fans_url = 'http://facebook.com/'.JumpCtrl::get_fansid($GameData['game_type'], $GameData['cid']);
break;
case 'dm1':
$dm = DM::call('result_lightBox_dm1', '結果頁lightBox廣告 - 1');
break;
case 'dm2':
$dm = DM::call('result_lightBox_dm2', '結果頁lightBox廣告 - 2');
break;
default:
$dm = '';
break;
}
return !JumpCtrl::showBox() ? '' : View::make('Main._game.lightBox', array(
'dm' => $dm,
'fans_url' => $fans_url,
));
}
/**
共用版面
*/
/**
* 共用版 - 開始頁面
* @param array $GameData 遊戲資訊
* @return string html碼
*/
public static function public_StartPage ($GameData, $next='play')
{
self::$LayoutConf['js'][] = 'js/Game/start.js';
return View::make('Main._game.start', array(
'data' => $GameData,
'next' => $next,
));
}
/**
* 共用版 - 遊戲頁面
* @param array $GameData 遊戲資訊
* @return string html碼
*/
public static function public_PlayPage ($GameData, $next='result')
{
return View::make('Main._game.play', array(
'data' => $GameData,
'next' => $next,
));
}
/**
html版面物件
*/
public static function Comments ($param=[])
{
return @View::make('Main._fb.comments', array_merge($param, self::$viewParam['Comments']?:array(), array(
'width' => 730,
)));
}
public static function TagHtml ($param=[])
{
$GameData = self::$GameData;
return View::make('Main._game.tag', array_merge($param, self::$viewParam['tag']?:array(), array(
'key_word_array' => isset($GameData['key_word_array']) ? $GameData['key_word_array'] : array()
)));
}
public static function DetailHtml ($param=[])
{
$GameData = self::$GameData;
$GameCount = count($GameData['game_data']);
$note = '';
$gid = $GameData['gid'];
$game_type = $GameData['game_type'];
$note = in_array($game_type, array(1,4)) ? ("答對率:".Record::getAnswerRate($gid)."%") : null;
return View::make('Main._game.detail', array_merge($param, self::$viewParam['detail']?:array(), array(
'GameCount' => $GameCount,
'note' => $note,
'assess' => Assess::game($GameData['gid']),
'play_times'=> (isset($GameData['play_times']) ? $GameData['play_times'] : 0),
'play_times_txt' => self::$gameConf[$game_type]['verb'],
)));
}
public static function HeaderHtml ($param=[])
{
$GameData = self::$GameData;
$category = $GameData['category'];
$gameCode = self::$getTypeCode[$GameData['game_info']['tid']];
return View::make('Main._game.header', array_merge($param, self::$viewParam['header']?:array(), array(
'index_url' => '/',
'type_name' => $GameData['game_info']['name'],
'type_url' => $gameCode,
'category_name' => $category['c_name'],
'category_url' => $gameCode.'/'.$category['cid'],
'GameData' => $GameData,
'gameInfo' => $GameData['create_date'].' | '.$GameData['visitor'].' views',
)));
}
public static function SocialHtml ($param=[])
{
$GameData = self::$GameData;
return View::make('Main._game.social', array_merge($param, self::$viewParam['social']?:array(), array(
'gid' => $GameData['gid'],
'url' => $GameData['url'],
)));
}
public static function relatedHtml ()
{
$GameData = self::$GameData;
$gameInfo = $GameData['game_info'];
return View::make('Main._game.related', array(
'gameTypeName' => $gameInfo['name'],
'list' => self::relatedGame_list($gameInfo['tid'], 4, [$GameData['gid']])
));
}
public static function sameCategoryHtml ()
{
$GameData = self::$GameData;
$gameInfo = $GameData['game_info'];
return View::make('Main._game.sameCategory', array(
'gameTypeName' => $gameInfo['name'],
'list' => self::relatedGame_list($gameInfo['tid'], 6, [$GameData['gid']])
));
}
public static function doubleDM ()
{
return View::make('Main._dm.doubleDM');
}
public static function relatedGame_list ($typeid , $count=6, $escape=[])
{
$escape = array_merge(Layout::escape(), $escape);
if(empty(self::$relatedGame))
{
$memkey = "games#underPlay_".$typeid;
$remember = array();
self::$relatedGame = Cache::remember($memkey, 120, function()use($escape, $typeid){
$typeinfo = MainCtrl::getTypeInfos();
foreach($typeinfo as $info)
{
if($info['tid'] == $typeid ) {
$typeinfo = $info;
break;
}
}
$data = MainCtrl::getGames()
->where('game_type', 'REGEXP', $typeinfo['gtype'])
->whereNotIn('gid', $escape)
->orderByRaw("RAND()")
->limit(200)
->get()->toArray();
$data = MainCtrl::itemFormat($data);
foreach($data as $val)
{
$remember[$val['gid']] = $val;
}
return $remember;
});
}
$data = (self::$relatedGame !== null) ? self::$relatedGame : array();
$gids = array_column($data, 'gid');
$gids = array_diff($gids, $escape);
$gids = array_shuffle($gids, $count);
Layout::escape($gids);
return array_find($data, $gids);
}
/*儲存投票答案*/
public function poll_save_playerAnswer() {
$status = false;
$input = Input::all();
if (empty($input)) die();
if(!Account::is_login()) {
die(json_encode([
'status' => $status,
'msg' => '請先登入。',
]));
}
$msg = false;
$uid = Account::get_login_user();
$gid = $input['gid'];
$answer = $input['answer'];
$GameData = GameCtrl::getGamedata($gid, true);
/* 遊戲作者或已投票,不記錄 */
if(
$GameData['uid'] == $uid OR
Record::is_played($uid, $gid) OR
count($answer) != count(GameCtrl::getGamedata($gid)->game_data)
)
$msg = '投票資訊錯誤,請重新整理。';
if($msg === false)
{
Record::play($gid, $answer);
Visitor::inc($gid);
$msg = '投票成功';
}
echo json_encode([
'status' => $status,
'msg' => $msg,
], JSON_UNESCAPED_UNICODE);
}
public function write_ans_record(){
$play_result = self::getPlayResult();
if( $play_result !== false ) self::rememberTemp($play_result);
show(self::getPlayResult());
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment