game_index.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /**
  2. * @author Suker
  3. */
  4. //重写requestAnimationFrame
  5. var $j=jQuery.noConflict();
  6. window.requestAnimationFrame = (function(){
  7. return window.requestAnimationFrame ||
  8. window.webkitRequestAnimationFrame ||
  9. window.mozRequestAnimationFrame ||
  10. window.oRequestAnimationFrame ||
  11. window.msRequestAnimationFrame ||
  12. window.setTimeout;
  13. })();
  14. //重写cancelAnimationFrame
  15. window.cancelAnimationFrame = (function() {
  16. return window.cancelAnimationFrame ||
  17. window.webkitCancelAnimationFrame ||
  18. window.mozCancelAnimationFrame ||
  19. window.oCancelAnimationFrame ||
  20. window.msCancelAnimationFrame ||
  21. window.clearTimeout;
  22. })();
  23. var _getvp = document.getElementById('viewport');
  24. if (_getvp && _getvp.content.indexOf('width=960') >= 0) {
  25. var _ua = navigator.userAgent.toLowerCase(), _setW = window.innerWidth > window.innerHeight ? window.innerWidth : window.innerHeight;
  26. window.glsysw = _setW > 1136 ? 1136 : (_setW == 961 ? 1136 : (_ua.indexOf('ipad') >= 0 ? 1024 : _setW));
  27. _getvp.content = 'width=' + window.glsysw + ',user-scalable=yes';
  28. if (_ua.indexOf('webkit') < 0) {
  29. _getvp.content += ',uc-user-scalable=yes,target-densitydpi=high-dpi';
  30. }
  31. _ua = _setW = null;
  32. }
  33. _getvp = null;
  34. //调出进度条
  35. function callLoading (method) {
  36. return;
  37. var _getLoadingDiv = document.getElementById('loadingDiv');
  38. if (_getLoadingDiv) {
  39. if (method == 'close') {
  40. document.body.removeChild(_getLoadingDiv);
  41. }
  42. else {
  43. _getLoadingDiv.style.height = window.innerHeight + 'px';
  44. }
  45. }
  46. else if (method == 'open') {
  47. _getLoadingDiv = document.createElement('div');
  48. _getLoadingDiv.id = 'loadingDiv';
  49. _getLoadingDiv.style.position = 'absolute';
  50. _getLoadingDiv.style.width = '100%';
  51. _getLoadingDiv.style.height = window.innerHeight + 'px';
  52. _getLoadingDiv.style.textAlign = 'center';
  53. _getLoadingDiv.style.background = '#FFF';
  54. _getLoadingDiv.style.zIndex = 1000000;
  55. _getLoadingDiv.style.left = '0px';
  56. _getLoadingDiv.style.top = '0px';
  57. _getLoadingDiv.innerHTML = [
  58. '<img src="../resource/img/logo.png" style="margin-top:' + ((window.innerHeight - 269) >> 1) + 'px;" />'
  59. ].join('');
  60. document.body.appendChild(_getLoadingDiv);
  61. }
  62. _getLoadingDiv = null;
  63. };
  64. var QueryString = {
  65. /**
  66. * 取得查询字符串参数
  67. * 例:假设查询字符串是?q=javascript&num=10
  68. * var args=getQueryStringArgs();
  69. * alert(args["q"]);
  70. * alert(args["num"]);
  71. */
  72. getQueryStringArgs: function () {
  73. //取得查询字符串并去掉开头的问号
  74. var qs = (location.search.length > 0 ? location.search.substring(1) : "");
  75. //保存数据的对象
  76. var args = {};
  77. //取得每一项
  78. var items = qs.split("&");
  79. var item = null,
  80. name = null,
  81. value = null;
  82. //逐个将每一项添加到args对象中
  83. for (var i = 0; i < items.length; i++) {
  84. item = items[i].split("=");
  85. name = decodeURIComponent(item[0]);
  86. value = decodeURIComponent(item[1]);
  87. args[name] = value;
  88. }
  89. return args;
  90. },
  91. /**
  92. * 对getQueryStringArgs()方法进行进一步封装,简化调用
  93. */
  94. getParameter: function (keyValue) {
  95. var args = this.getQueryStringArgs();
  96. if (args[keyValue] != undefined) {
  97. return args[keyValue];
  98. } else {
  99. return "";
  100. }
  101. }
  102. };
  103. var CookieUtil = {
  104. get: function (name){
  105. var cookieName = encodeURIComponent(name) + "=",
  106. cookieStart = document.cookie.indexOf(cookieName),
  107. cookieValue = null;
  108. if (cookieStart > -1){
  109. var cookieEnd = document.cookie.indexOf(";", cookieStart);
  110. if (cookieEnd == -1){
  111. cookieEnd = document.cookie.length;
  112. }
  113. cookieValue = decodeURIComponent(document.cookie.substring(cookieStart + cookieName.length, cookieEnd));
  114. }
  115. return cookieValue;
  116. },
  117. set: function (name, value, day, path, domain, secure) {
  118. var cookieText = encodeURIComponent(name) + "=" + encodeURIComponent(value);
  119. if(typeof day=="number"){
  120. var expires = new Date();
  121. expires.setTime(expires.getTime() + day*24*60*60*1000);
  122. cookieText += "; expires=" + expires.toGMTString();
  123. }
  124. if (path) {
  125. cookieText += "; path=" + path;
  126. }
  127. if (domain) {
  128. cookieText += "; domain=" + domain;
  129. }
  130. if (secure) {
  131. cookieText += "; secure";
  132. }
  133. document.cookie = cookieText;
  134. },
  135. unset: function (name, path, domain, secure){
  136. this.set(name, "", new Date(0), path, domain, secure);
  137. }
  138. };
  139. var $iframe=null;
  140. function crossAjax(config){
  141. document.domain='127.0.0.1'; //域名
  142. if(!$iframe){
  143. $iframe= $j('<iframe/>',{id:'crossDomain',style:'display:none;',src:server.DuopaoBlankURL}).appendTo('body').load(function(){
  144. $iframe.win=$j(this)[0].contentWindow;
  145. $iframe.loadReady=true;
  146. });
  147. }
  148. if($iframe.loadReady){
  149. $iframe.win.$.ajax(config);
  150. }else{
  151. $iframe.one('load',function(){
  152. $iframe.win.$.ajax(config);
  153. });
  154. }
  155. }
  156. //上传积分(每一关完成都会上传积分)
  157. var loginCounter=0;
  158. function dp_submitScore(vLevel,vScore){
  159. crossAjax({
  160. url:rootIndex+'uploadscore.php',
  161. data:{
  162. game_code:QueryString.getParameter('game_code'),
  163. game_level:vLevel,
  164. game_score:vScore,
  165. bs:QueryString.getParameter('bs')
  166. },
  167. type:'post',
  168. dataType:'json',
  169. success:function(res){
  170. //this added by JobsFan
  171. if (QueryString.getParameter('bs').indexOf('wx')!=-1){
  172. //dp_share(vScore);//不能写在这里,因为游戏分享的部分可能不一样
  173. $j(".nowrecord .num").html(vScore);
  174. $j(".bestrecord .num").html(res.bestscore);
  175. $j(".scoreinfo").show();
  176. }
  177. if(res.flag=='f0'){
  178. if(res.ulevel>0){
  179. // if(confirm('你的分数为'+res.uscore+',排名为第'+res.ulevel+'名,是否跳转到排行榜?')){
  180. // location.href=rootIndex+'games/toplist?game_code='+QueryString.getParameter('game_code');
  181. // }
  182. }
  183. }else if(res.flag=='f1'){
  184. if(loginCounter==1||confirm('您尚未登录,游戏积分无法上传到排行榜,是否登录?')){
  185. location.href=server.PageLoginURL+'?service='+encodeURIComponent(location.href);
  186. }else{
  187. loginCounter++;
  188. }
  189. }else{
  190. alert(res.msg);
  191. }
  192. }
  193. });
  194. }
  195. var rankLoading=false;
  196. function dp_Ranking(show_type){
  197. if(rankLoading){
  198. return;
  199. }
  200. var game_code=QueryString.getParameter('game_code');
  201. if(show_type==-1||true){
  202. location.href=rootIndex+'toplist.php?game_code='+game_code;
  203. return;
  204. }
  205. rankLoading=true;
  206. crossAjax({
  207. url:rootIndex+server.GameTop,
  208. data:{
  209. game_code:game_code,
  210. pflag:true,
  211. perPageSize:3,
  212. pageIndex:1,
  213. flag:true
  214. },
  215. success:function(result){
  216. rankLoading=false;
  217. var data=JSON.parse(result);
  218. if(data.flag=='f0'||data.flag=='f1'){
  219. var tem;
  220. if(data.flag!='f0'){
  221. tem='<div class="your_rank">您的成绩:<a id="login_check" href="javascript:" style="text-decoration: underline;color: inherit;">登录后才能查看呦</a></div>';
  222. }else{
  223. if(data.ulevel==-1){
  224. tem='<div class="your_rank">您的成绩:'+data.uscore+'分 排名:千里之外</div>';
  225. }else{
  226. tem='<div class="your_rank">您的成绩:'+data.uscore+'分 排名:第'+data.ulevel+'名</div>';
  227. }
  228. }
  229. tem+='<div class="rank_list">';
  230. if(data.count>0){
  231. for(var i=0;i<data.items.length;i++){
  232. var item=data.items[i];
  233. tem+='<div class="list_item">' +
  234. '<div class="icon"><img src="'+item.user.user_avatar+'" /></div>' +
  235. '<div class="info">' +
  236. '<div class="list_item_title">第'+item.ulevel+'名:<span><a class="'+item.user.user_sex+'_name" href="'+rootIndex+'ucinfo/'+item.user.user_id+'">'+item.user.user_nickname+'</a></span></div>' +
  237. '<div class="list_item_content">分数:'+item.game_score+'</div>' +
  238. '</div>' +
  239. '</div>';
  240. }
  241. }
  242. tem += '</div>';
  243. tem += '<a ontouchstart="" class="w_button w_button_small fl" id="gobackgame" href="u.ali213.net" style="width: 50px; margin-top: 10px;">继续游戏</a>';
  244. tem += '<a ontouchstart="" class="w_button w_button_small fr" href="'+rootIndex+'list.php" style="width: 50px; margin-top: 10px;">更多游戏</a>';
  245. tem += '<a ontouchstart="" class="w_button w_button_small fr" href="'+rootIndex+'toplist.php?game_code='+game_code+'" style="width: 50px; margin-top: 10px;margin-right:17px;">更多排名</a>';
  246. win=new DWindow({
  247. width:300,
  248. title:'排行榜',
  249. content:tem
  250. });
  251. win.$panel.find('#login_check').click(function(){
  252. location.href=server.PageLoginURL+'?service='+location.href;
  253. });
  254. win.$panel.find('#gobackgame').click(function(){
  255. win.close();
  256. });
  257. }else{
  258. alert(data.msg);
  259. }
  260. }
  261. })
  262. }
  263. //window.onerror = function(errorMsg, url, lineNumber) {
  264. // alert(errorMsg+'\n'+url+'\n'+lineNumber);
  265. //};
  266. window.addEventListener('load',function(e) {
  267. callAdSceneInGame(QueryString.getParameter('game_code'));
  268. return;
  269. //重置框架
  270. function _resetFrame () {
  271. var _getScreenFrame = document.getElementById('screenFrame');
  272. if (_getScreenFrame) {
  273. _getScreenFrame.style.display = 'block';
  274. _getScreenFrame.style.width = window.glsysw + 'px';
  275. if (!_getScreenFrame.style.height) {
  276. _getScreenFrame.style.height = window.innerHeight + 'px';
  277. }
  278. if (_getScreenFrame.children[0]) {
  279. _getScreenFrame.children[0].style.height = _getScreenFrame.style.height;
  280. }
  281. else {
  282. callLoading('open');
  283. _getScreenFrame.innerHTML = '<iframe src="' + _getScreenFrame.lang + '" scrolling="no"></iframe>';
  284. //监听框架事件
  285. _getScreenFrame.children[0].addEventListener('load', function(e) {
  286. e.target.style.height = e.target.parentNode.style.height;
  287. callLoading('close');
  288. }, true);
  289. }
  290. _getScreenFrame.style.marginTop = ((window.innerHeight - parseInt(_getScreenFrame.style.height)) >> 1) + 'px';
  291. }
  292. _getScreenFrame = null;
  293. }
  294. //调出翻转提示
  295. function _callLandscape (desc) {
  296. var _getLandscapeDiv = document.getElementById('landscapeDiv');
  297. if (_getLandscapeDiv) {
  298. if (desc == 0) {
  299. document.body.removeChild(_getLandscapeDiv);
  300. }
  301. else {
  302. _getLandscapeDiv.style.height = window.innerHeight + 'px';
  303. }
  304. }
  305. else if (desc) {
  306. _getLandscapeDiv = document.createElement('div');
  307. _getLandscapeDiv.id = 'landscapeDiv';
  308. _getLandscapeDiv.style.position = 'absolute';
  309. _getLandscapeDiv.style.width = '100%';
  310. _getLandscapeDiv.style.height = window.innerHeight + 'px';
  311. _getLandscapeDiv.style.textAlign = 'center';
  312. _getLandscapeDiv.style.background = '#FFF';
  313. _getLandscapeDiv.style.zIndex = 1000001;
  314. _getLandscapeDiv.style.left = '0px';
  315. _getLandscapeDiv.style.top = '0px';
  316. _getLandscapeDiv.innerHTML = [
  317. '<img src="../resource/img/orientation-landscape-' + desc + '.png" style="margin-top:' + ((window.innerHeight - 500) >> 1) + 'px;" />'
  318. ].join('');
  319. document.body.appendChild(_getLandscapeDiv);
  320. }
  321. _getLoadingDiv = null;
  322. }
  323. //创建顶部功能菜单
  324. function _createTopMenu () {
  325. var _getCallMenuBtn = document.getElementById('callMenuBtn'), _getTopMenu = document.getElementById('topMenu');
  326. if (!_getCallMenuBtn) {
  327. _getCallMenuBtn = document.createElement('a');
  328. _getCallMenuBtn.id = 'callMenuBtn';
  329. _getCallMenuBtn.style.position = 'absolute';
  330. _getCallMenuBtn.style.left = '45px';
  331. _getCallMenuBtn.style.top = '5px';
  332. _getCallMenuBtn.style.zIndex = 99;
  333. _getCallMenuBtn.className = 'menuBtn1 radius transition-ease';
  334. _getCallMenuBtn.innerHTML = '<span class="menuBtn1-icon"></span>';
  335. _getCallMenuBtn.onmousedown = function(e) {
  336. _getCallMenuBtn._canDo = true;
  337. };
  338. _getCallMenuBtn.onmouseup = function(e) {
  339. if (!_getCallMenuBtn._canDo) {
  340. return false;
  341. }
  342. _getCallMenuBtn._canDo = false;
  343. if (_getCallMenuBtn) {
  344. _getCallMenuBtn.style.top = '-40px';
  345. }
  346. if (_getTopMenu) {
  347. _getTopMenu.style.top = '0px';
  348. }
  349. if (e.preventDefault) {
  350. e.preventDefault();
  351. }
  352. };
  353. _getCallMenuBtn.ontouchstart = _getCallMenuBtn.onmousedown;
  354. _getCallMenuBtn.ontouchend = _getCallMenuBtn.onmouseup;
  355. document.body.appendChild(_getCallMenuBtn);
  356. }
  357. if (!_getTopMenu) {
  358. _getTopMenu = document.createElement('div');
  359. _getTopMenu.id = 'topMenu';
  360. _getTopMenu.style.zIndex = 99;
  361. _getTopMenu.className = 'transition-ease';
  362. _getTopMenu.innerHTML = [
  363. '<span style="position:relative;display:inline-block;width:20%;text-align:left;"><a id="directionMenuBtn_0" href="javascript:void(0);" class="menuBtn2 radius" style="top:-11px;margin-left:5px;"><span id="directionMenuBtn_1" class="menuBtn2-icon"></span><span id="directionMenuBtn_2" class="menuBtn2-desc">ali213.net</span></a></span>',
  364. '<span style="position:relative;display:inline-block;width:60%;text-align:center;"><a href="http://www.ali213.net"><img id="directionMenuBtnDuoPao" src="../resource/img/iconmap_topheader.png" /></a></span>',
  365. '<span style="position:relative;display:inline-block;width:20%;text-align:right;"><a id="closeMenuBtn_0" class="menuBtn3 radius" style="top:-11px;margin-right:5px;"><span id="closeMenuBtn_1" class="menuBtn3-icon"></span></a></span>'
  366. ].join('');
  367. _getTopMenu.onmousedown = function(e) {
  368. _getTopMenu._canDo = true;
  369. };
  370. _getTopMenu.onmouseup = function(e) {
  371. if (!_getTopMenu._canDo) {
  372. return false;
  373. }
  374. _getTopMenu._canDo = false;
  375. if (e.target.id.indexOf('directionMenuBtn_') >= 0) {
  376. location.href = document.body.lang;
  377. }
  378. else if (e.target.id == 'directionMenuBtnDuoPao') {
  379. location.href = e.target.parentNode.href;
  380. }
  381. else if (e.target.id.indexOf('closeMenuBtn_') >= 0) {
  382. if (_getCallMenuBtn) {
  383. _getCallMenuBtn.style.top = '5px';
  384. }
  385. if (_getTopMenu) {
  386. _getTopMenu.style.top = '-40px';
  387. }
  388. }
  389. if (e.preventDefault) {
  390. e.preventDefault();
  391. }
  392. };
  393. _getTopMenu.ontouchstart = _getTopMenu.onmousedown;
  394. _getTopMenu.ontouchend = _getTopMenu.onmouseup;
  395. document.body.appendChild(_getTopMenu);
  396. }
  397. }
  398. window.onresize = function(e) {
  399. _resetFrame();
  400. callLoading();
  401. _callLandscape();
  402. };
  403. //初始化函数
  404. function _init () {
  405. _resetFrame();
  406. _createTopMenu();
  407. }
  408. _init();
  409. var _lastDate = Date.now(), _isPause = false, _playTimer, _rafRun, _state = 0, _bodyDesc = document.body.id;
  410. if (!_playTimer) {
  411. (_rafRun = function() { //UI界面requestAnimationFrame主循环逻辑
  412. var _newDate = Date.now();
  413. if ((_newDate - _lastDate) >= 100) {
  414. _lastDate = _newDate;
  415. if (!_isPause) {
  416. switch (_state) {
  417. case 0: //正常
  418. if (_bodyDesc == 1) {
  419. if (window.innerWidth > window.innerHeight) {
  420. _callLandscape(_state = 1);
  421. }
  422. }
  423. else if (_bodyDesc == 2) {
  424. if (window.innerWidth < window.innerHeight) {
  425. _callLandscape(_state = 2);
  426. }
  427. }
  428. break;
  429. case 1: //处于横屏状态下
  430. if (window.innerWidth <= window.innerHeight) {
  431. _callLandscape(_state = 0);
  432. }
  433. break;
  434. case 2: //处于竖屏状态下
  435. if (window.innerWidth > window.innerHeight) {
  436. _callLandscape(_state = 0);
  437. }
  438. break;
  439. default:
  440. break;
  441. }
  442. }
  443. }
  444. _newDate = null;
  445. if (_rafRun)
  446. _playTimer = requestAnimationFrame(_rafRun);
  447. })();
  448. }
  449. });
  450. var DWindow=(function($){
  451. function DWindow(config){
  452. $.extend(this,{
  453. width:200,
  454. title:'',
  455. content:'',
  456. showClose:false
  457. },config);
  458. this.$mask=$('<div class="w_mask"></div>');
  459. this.$panel=$('<div class="w_panel"><div class="w_title">'+this.title+(this.showClose?'<a class="menuBtn3-icon"></a>':'')+'</div><div class="w_content">'+this.content+'</div></div>').css({
  460. width:this.width,
  461. visibility:'hidden'
  462. });
  463. this.$closer= this.$panel.find('.w_title .menuBtn3-icon');
  464. $(document.body).append(this.$mask,this.$panel);
  465. this.$panel.css({
  466. visibility:'visible'
  467. }).hide().fadeIn();
  468. this._init();
  469. }
  470. DWindow.prototype={
  471. constructor: DWindow,
  472. close:function(){
  473. this.$mask.fadeOut();
  474. this.$panel.fadeOut();
  475. },
  476. _init:function(){
  477. var that=this;
  478. this.$closer.on('click',function(){
  479. that.close();
  480. });
  481. },
  482. show:function(){
  483. this.$panel.show();
  484. this.$mask.show();
  485. },
  486. hide:function(){
  487. this.$panel.hide();
  488. this.$mask.hide();
  489. }
  490. };
  491. return DWindow;
  492. }($j));
  493. function onBridgeReady() {
  494. WeixinJSBridge.call('showOptionMenu');
  495. WeixinJSBridge.on('menu:share:appmessage', function(argv) {
  496. WeixinJSBridge.invoke('sendAppMessage', {
  497. "img_url": window.shareData.imgUrl,
  498. "link": window.shareData.timeLineLink,
  499. "desc": window.shareData.tContent,
  500. "title": window.shareData.tTitle
  501. }, function(res) {
  502. //document.location.href = mebtnopenurl;
  503. })
  504. });
  505. WeixinJSBridge.on('menu:share:timeline', function(argv) {
  506. WeixinJSBridge.invoke('shareTimeline', {
  507. "img_url": window.shareData.imgUrl,
  508. "img_width": "640",
  509. "img_height": "640",
  510. "link": window.shareData.timeLineLink,
  511. "desc": window.shareData.tContent,
  512. "title": window.shareData.tContent
  513. }, function(res) {
  514. //document.location.href = mebtnopenurl;
  515. });
  516. });
  517. }
  518. if (typeof WeixinJSBridge == "undefined") {
  519. if (document.addEventListener) {
  520. document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
  521. } else if (document.attachEvent) {
  522. document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
  523. document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
  524. }
  525. } else {
  526. onBridgeReady();
  527. }
  528. //jobs add
  529. $j(document).ready(function(){
  530. $j(".return").click(function(){
  531. $j(".scoreinfo").hide();
  532. });
  533. $j(".xuyao").click(function(){
  534. $j(".weixinshare").show();
  535. });
  536. $j(".weixinshare").click(function(){
  537. $j(this).hide();
  538. });
  539. });