common.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. // 全局链接的配置
  2. var btGame;
  3. ;~function(bt){
  4. // 因考虑到,以后可能拿链接的时候,可能要区分游戏
  5. // 所以,统一通过函数返回链接吧~,能更加灵活的更改逻辑了~
  6. bt.URL = {
  7. root: "http://game.ikongzhong.cn/"
  8. ,getMoreGame: function(){
  9. // 点击更多
  10. bt.dc("more");
  11. return "http://game.ikongzhong.cn/";
  12. }
  13. ,getConcern: function(){
  14. return "http://game.ikongzhong.cn/";
  15. }
  16. ,appId: ""
  17. };
  18. // 先用着一个很搓,很有效的方式~
  19. bt.getGameId = function(){
  20. var href = location.href;
  21. href = href.slice(href.indexOf("://") + 3);
  22. var id = href.split("/")[2];
  23. return id;
  24. }
  25. // 先用着一个很搓,很有效的方式~
  26. bt.getGamePath = function(){
  27. var href = location.href;
  28. href = href.slice(0, href.lastIndexOf("/") + 1);
  29. return href;
  30. }
  31. }(btGame || (btGame = {}));
  32. // 基础弹出窗口的定义
  33. var btGame;
  34. ;~function(bt){
  35. function popupBox(id, hideClass){
  36. this.elemId = id;
  37. this.hideClass = hideClass || "bt-hide";
  38. };
  39. popupBox.prototype = {
  40. beforeShow: function(){
  41. // 给别人继承的,什么都不做
  42. }
  43. ,show: function(){
  44. this.beforeShow();
  45. // 为了让动画生效,加了一个timer
  46. var that = this;
  47. setTimeout(function(){
  48. $("#" + that.elemId).removeClass(that.hideClass);
  49. }, 1);
  50. }
  51. ,hide: function(){
  52. $("#" + this.elemId).addClass(this.hideClass);
  53. }
  54. };
  55. bt.popupBox = popupBox;
  56. }(btGame || (btGame = {}));
  57. // 代理函数
  58. var btGame;
  59. ;~function(bt){
  60. bt.proxy = function(func, define){
  61. return function(){
  62. func.apply(define, arguments);
  63. }
  64. }
  65. }(btGame || (btGame = {}));
  66. // 发布者
  67. var btGame;
  68. ;~function(bt){
  69. var publisher = function(obj){
  70. this.__publisher__ = obj;
  71. };
  72. publisher.prototype = {
  73. on: function(ev, func){
  74. this.__publisher__.on(ev, bt.proxy(func, this));
  75. },
  76. fire: function(ev){
  77. this.__publisher__.trigger(ev, [].slice.call(arguments, 1));
  78. },
  79. off: function(ev, func){
  80. if(func){
  81. this.__publisher__.off(ev, bt.proxy(func, this));
  82. }else{
  83. this.__publisher__.off(ev);
  84. }
  85. }
  86. };
  87. bt.makePublisher = function(obj){
  88. var type = typeof obj;
  89. var p = new publisher($("<div></div>"));
  90. if(type == "function"){
  91. obj.prototype.__publisher__ = p.__publisher__;
  92. $.extend(obj.prototype, publisher.prototype);
  93. }else if(type == "object"){
  94. obj.__publisher__ = p.__publisher__;
  95. $.extend(obj, publisher.prototype);
  96. }
  97. }
  98. }(btGame || (btGame = {}));
  99. // 获取 body 元素
  100. var btGame;
  101. ;~function(bt){
  102. // 因为插入,大部分都是基于 body 元素的
  103. // 提供一个获取 body 对象的方法
  104. var body;
  105. function getB(){
  106. if(!body){
  107. body = document.body || document.getElementsByTagName("body")[0];
  108. }
  109. return body;
  110. }
  111. bt.getDomBody = getB;
  112. function craeteDiv(){
  113. return document.createElement("div");
  114. }
  115. bt.getNewDiv = craeteDiv;
  116. }(btGame || (btGame = {}));
  117. // 锁屏
  118. var btGame;
  119. ;~function(bt){
  120. // 指定lock的id,如果没有,则调用默认的锁屏
  121. // 当调用show的时候,再检测锁屏是否存在,不存在锁屏,则创建
  122. // 通过添加、移除 class:bt-hide,进行显示和隐藏锁屏
  123. // 锁屏的动画,是通过 class:bt-animation 实现的
  124. var defaultLockId = "bt-lock-screen";
  125. var createLock = function(id){
  126. var div = bt.getNewDiv();
  127. div.id = id;
  128. var body = bt.getDomBody();
  129. body.appendChild(div);
  130. return $(div);
  131. };
  132. var lock = function(lockId){
  133. bt.popupBox.call(this, lockId || defaultLockId);
  134. };
  135. lock.__super__ = bt.popupBox;
  136. lock.prototype = $.extend({}, bt.popupBox.prototype, {
  137. beforeShow: function(){
  138. var elem = this.getElem();
  139. if(elem.size() <= 0){
  140. elem = createLock(this.elemId);
  141. elem.addClass("bt-lock-screen bt-animation bt-hide");
  142. }
  143. }
  144. ,remove: function(){
  145. var elem = this.getElem();
  146. if(elem.size() > 0){
  147. elem.addClass("bt-hide");
  148. // 为了看到动画,延迟一下吧
  149. setTimeout(function(){
  150. elem.remove();
  151. }, 200);
  152. }
  153. }
  154. ,getElem: function(){
  155. return $("#" + this.elemId);
  156. }
  157. });
  158. bt.lockScreen = function(id){
  159. return new lock(id);
  160. }
  161. }(btGame || (btGame = {}));
  162. // 闪屏
  163. var btGame;
  164. ;~function(bt){
  165. // 百田广告
  166. // 参数:
  167. // id: 广告div的id和className
  168. // html: 广告的内容
  169. // time: 广告多少秒后消失,小于0,则不会自动移除元素
  170. var defaultOptions = {id: "bt-advertisement", html: "广告", time: 1500};
  171. var flash = function(options){
  172. var newOptions = $.extend({}, defaultOptions, options || {});
  173. var $elem = $("#" + newOptions.id);
  174. var lock = new bt.lockScreen(newOptions.lockId);
  175. if($elem.size() <= 0){
  176. var $div = $(bt.getNewDiv()).attr({id: newOptions.id}).addClass(newOptions.id);
  177. var html = newOptions.html;
  178. $div.html(html);
  179. bt.getDomBody().appendChild($div[0]);
  180. $elem = $div;
  181. }
  182. this.event = newOptions.id + "_timeup";
  183. var that = this;
  184. if(newOptions.time > 0){
  185. // 时间到了之后,会发布事件哦~
  186. // 相同事件,不会重复发布
  187. var event = this.event;
  188. this.off(event);
  189. $elem.data("timer", setTimeout(
  190. function(){
  191. $elem.remove(), lock.hide();
  192. that.fire(event);
  193. newOptions = null;
  194. this.elem = this.lock = that.show = that.hide = null;
  195. }, newOptions.time <= 0 ? 1500 : newOptions.time)
  196. );
  197. }
  198. // 外部方法
  199. this.elem = $elem;
  200. this.lock = lock;
  201. this.show = function(html){
  202. html && this.elem.html(html);
  203. this.elem.removeClass("bt-hide");
  204. this.lock.show();
  205. }
  206. this.hide = function(){
  207. this.elem.addClass("bt-hide");
  208. this.lock.hide();
  209. }
  210. this.remove = function(){
  211. this.lock.remove();
  212. this.elem.remove();
  213. }
  214. }
  215. bt.makePublisher(flash);
  216. bt.advertisement = function(options){
  217. return new flash(options);
  218. };
  219. }(btGame || (btGame = {}));
  220. // 加载中的loading
  221. var btGame;
  222. ;~function(bt){
  223. var loadingDiv = null;
  224. var loadingText = null;
  225. var loading = function(rate, error){
  226. if(rate > 0 && !loadingDiv){
  227. loadingDiv = $(btGame.getNewDiv());
  228. loadingDiv.addClass("bt-game-loading");
  229. loadingDiv.html('<table><tr><td><img class="bt-img" src="vapp/preloadImage.png" /><div class="bt-text"></div></td></tr></table>');
  230. bt.getDomBody().appendChild(loadingDiv[0]);
  231. loadingText = loadingDiv.find(".bt-text");
  232. }
  233. if(loadingDiv){
  234. if(error){
  235. loadingText.html(error);
  236. }else{
  237. var r = Math.round(rate * 100);
  238. loadingText.html("加载进度:" + r + "%");
  239. }
  240. }
  241. if(rate >= 1){
  242. loadingDiv && loadingDiv.remove();
  243. loadingDiv = null;
  244. }
  245. }
  246. bt.gameLoading = loading;
  247. }(btGame || (btGame = {}));
  248. // 游戏区域大小设定
  249. // btGame.resizePlayArea($elem, width, height, top, left)
  250. // $elem: jQuery元素
  251. // width: 画布期望宽度
  252. // height: 画布期望高度
  253. // top: "top", "center", "bottom" 或 px
  254. // left: "left", "center", "right" 或 px
  255. var btGame;
  256. ;~function(bt){
  257. // @width: canvas期望的宽度
  258. // @height: canvas期望的高度
  259. function rate(width, height){
  260. var wWidth = window.innerWidth,
  261. wHeight = window.innerHeight;
  262. var mid;
  263. if(width <= wWidth && height <= wHeight){
  264. // 如果在屏幕内,就不用改了
  265. }else if(width > wWidth && height > wHeight){
  266. // 如果都大于屏幕
  267. var rateW = wWidth / width, rateH = wHeight / height;
  268. // 看谁更小,就以谁作为标准
  269. if(rateW <= rateH){
  270. mid = width;
  271. width = wWidth;
  272. height = height * width / mid;
  273. }else{
  274. mid = height;
  275. height = wHeight;
  276. width = width * height / mid;
  277. }
  278. }else if(width > wWidth){
  279. // 只有宽度大于屏幕
  280. mid = width;
  281. width = wWidth;
  282. height = height * wWidth / mid;
  283. }else if(height > wHeight){
  284. // 只有高度大于屏幕
  285. mid = height;
  286. height = wHeight;
  287. width = width * wHeight / mid;
  288. }else{
  289. // 没救了
  290. }
  291. var top = (wHeight - height) / 2, left = (wWidth - width) / 2;
  292. return {
  293. width: width
  294. ,height: height
  295. ,top: top
  296. ,left: left
  297. };
  298. }
  299. function resize($elem, width, height, top, left){
  300. var result = rate(width, height);
  301. $elem.css({
  302. width: result.width
  303. ,height: result.height
  304. ,top: top == "center" ? result.top : top == "left" ? 0 : top
  305. ,left: left == "center" ? result.left : left == "left" ? 0 : left
  306. });
  307. switch(top){
  308. case "top":
  309. $elem.css({top: 0});
  310. break;
  311. case "center":
  312. $elem.css({top: result.top});
  313. break;
  314. case "bottom":
  315. $elem.css({bottom: 0});
  316. break;
  317. default:
  318. $elem.css({top: top});
  319. }
  320. switch(left){
  321. case "left":
  322. $elem.css({left: 0});
  323. break;
  324. case "center":
  325. $elem.css({left: result.left});
  326. break;
  327. case "right":
  328. $elem.css({right: 0});
  329. break;
  330. default:
  331. $elem.css({left: left});
  332. }
  333. $elem.trigger("resizePlayArea", [result]);
  334. }
  335. function bindResize($elem, width, height, top, left){
  336. bt.checkHScreen(function(){
  337. setTimeout(function(){
  338. resize($elem, width, height, top, left);
  339. }, 500);
  340. });
  341. }
  342. bt.resizePlayArea = bindResize;
  343. }(btGame || (btGame = {}));
  344. // 告诉用户收藏
  345. var btGame;
  346. ;~function(bt){
  347. function ask(cb){
  348. if(confirm('关注"全球游戏排行榜"微信,就可以收藏这个游戏哦!')){
  349. cb ? cb() : top.location.href = bt.URL.getConcern();
  350. }
  351. }
  352. bt.attentOurGame = ask;
  353. }(btGame || (btGame = {}));
  354. // 横竖屏检测
  355. var btGame;
  356. ;~function(bt){
  357. // 检测,如果是横屏:true,如果是竖屏:false
  358. // 主要看window的宽高大小,如果width > height,就是横屏,反之~
  359. // btGame.checkHScreen(callback, once);
  360. // @param callback 回调函数 callback(true) -> 横屏
  361. // @param once 是否只检测一次,默认false,一直检测
  362. var screenResize = function(cb) {
  363. // 横屏true,竖屏false
  364. cb && cb(window.innerWidth > window.innerHeight);
  365. };
  366. function check(callback, once){
  367. if(!once){
  368. window.addEventListener("orientationchange", function() {
  369. screenResize(callback);
  370. });
  371. window.addEventListener("resize", function() {
  372. screenResize(callback);
  373. });
  374. }
  375. screenResize(callback);
  376. }
  377. bt.checkHScreen = check;
  378. }(btGame || (btGame = {}));
  379. // 横、竖屏的提醒
  380. var btGame;
  381. ;~function(bt){
  382. var onlyH = function(once, callback){
  383. this.myCallback = callback;
  384. this.tipsCount = 0;
  385. bt.checkHScreen(bt.proxy(this.callback, this), false);
  386. if(once){
  387. this.once = once;
  388. }
  389. };
  390. onlyH.prototype = {
  391. hscreen: function(){
  392. // 是横屏
  393. // 设置了提醒一次,就真的只提醒一次了
  394. this.buildScreen();
  395. if(this.once && this.tipsCount <= 0){
  396. this.screen && this.screen.show();
  397. }else if(!this.once){
  398. this.screen && this.screen.show();
  399. }
  400. this.tipsCount++;
  401. }
  402. ,vscreen: function(){
  403. // 不是横屏,把提醒去掉
  404. this.screen && this.screen.hide();
  405. this.myCallback && this.myCallback(this.tipsCount);
  406. }
  407. ,getScreenOption: function(){
  408. return {
  409. id: "bt-h-scrren"
  410. ,html: "<table><tr><td><img class='bt-h-screen-img' src='/vapp/52/bt-play-h-screen.png' /></td></tr></table>"
  411. ,time: 0
  412. ,lockId: 'bt-hide-lock'
  413. };
  414. }
  415. // 下面的两个,逻辑是不用重新更改的
  416. ,buildScreen: function(){
  417. !this.screen && (
  418. this.screen = btGame.advertisement(this.getScreenOption())
  419. );
  420. }
  421. ,callback: function(isHScreen){
  422. isHScreen ? this.vscreen() : this.hscreen();
  423. }
  424. };
  425. // 竖屏的检测,继承于横屏
  426. var onlyV = function(once, callback){
  427. onlyH.call(this, once, callback);
  428. }
  429. onlyV.__super__ = onlyH;
  430. onlyV.prototype = $.extend({}, onlyH.prototype, {
  431. hscreen: function(){
  432. onlyH.prototype.vscreen.call(this);
  433. }
  434. ,vscreen: function(){
  435. onlyH.prototype.hscreen.call(this);
  436. }
  437. ,getScreenOption: function(){
  438. return {
  439. id: "bt-v-scrren"
  440. ,html: "<table><tr><td><img class='bt-v-screen-img' src='/vapp/52/bt-play-v-screen.png' /></td></tr></table>"
  441. ,time: 0
  442. ,lockId: 'bt-hide-lock'
  443. };
  444. }
  445. });
  446. bt.onlyHScreen = function(once, callback){
  447. return new onlyH(once, callback);
  448. };
  449. bt.onlyVScreen = function(once, callback){
  450. return new onlyV(once, callback);
  451. }
  452. }(btGame || (btGame = {}));
  453. // 设置分享
  454. var btGame;
  455. ;~function(bt){
  456. // 暂时只有微信的分享
  457. bt.setShare = function(option){
  458. }
  459. }(btGame || (btGame = {}));