hero-search.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. //为字符串类型添加去除左右空格的方法
  2. String.prototype.trim = function(){
  3. return this.replace(/(^\s*)|(\s*$)/g, "");
  4. };
  5. /**
  6. * 通过关键字搜索英雄 keyworld(String)可以针对英雄的中文名title,英文名name,称号displayName
  7. * champions(Json)全部英雄json格式的列表 返回匹配英雄的name(Array)
  8. */
  9. function searchChampionByKeyworld(keyworld, champions) {
  10. var key;
  11. var result = new Array();
  12. for (key in champions) {
  13. var name = champions[key]['name'];
  14. var nameLower = name.toLowerCase();
  15. var displayName = champions[key]['displayName'];
  16. var title = champions[key]['title'];
  17. // 按中文名,称号,英文名的优先级顺序查找
  18. if (title.indexOf(keyworld) != -1) {
  19. result.push(name);
  20. continue;
  21. }
  22. if (displayName.indexOf(keyworld) != -1) {
  23. result.push(name);
  24. continue;
  25. }
  26. // 不区分大小写,从开始位置进行匹配
  27. keyworld = keyworld.toLowerCase();
  28. if (nameLower.indexOf(keyworld) == 0) {
  29. result.push(name);
  30. continue;
  31. }
  32. }
  33. return result;
  34. }
  35. /*
  36. * var result = searchChampionByKeyworld('颂唱', champions);
  37. *
  38. * for(var key in result){ document.write(result[key]+'<br/>'); }
  39. */
  40. /**
  41. * 通过分类标签搜索英雄 tags(Array)分类标签 champions(Json)全部英雄json格式的列表 返回匹配英雄的name(Array)
  42. */
  43. function searchChampionByTags(tagsSearch, champions) {
  44. var key;
  45. var result = new Array();
  46. for (key in champions) {
  47. var tags = champions[key]['tags'];
  48. if (!tags) continue;
  49. var tagsArr = tags.split(',');
  50. var $result = isContain(tagsArr, tagsSearch);
  51. if ($result) {
  52. result.push(champions[key]['name']);
  53. }
  54. }
  55. return result;
  56. }
  57. /**
  58. * 通过分类标签搜索物品 tags(Array)物品分类标签 item(Json) 全部物品json格式的列表 返回匹配的物品的id(Array)
  59. */
  60. function searchItemByTags(tagsSearch, items) {
  61. var key;
  62. var result = new Array();
  63. for (key in items) {
  64. var tags = items[key]['tags'];
  65. if (tags == false)
  66. continue;
  67. var tagsArr = tags.split(',');
  68. var $result = isContain(tagsArr, tagsSearch);
  69. if ($result) {
  70. result.push(items[key]['id']); // 可能会修改
  71. }
  72. }
  73. return result;
  74. }
  75. /**
  76. * 判断数组arrOne是否包含数组arrTwo 如果是返回true,否则返回false 如果arrOne或arrTwo为空则返回false;
  77. */
  78. function isContain(arrOne, arrTwo) {
  79. if (arrOne.length == 0 || arrTwo.length == 0)
  80. return false;
  81. if (arrOne.length < arrTwo.length)
  82. return false;
  83. var keyOne, keyTwo;
  84. var result = true; //
  85. for (keyTwo in arrTwo) {
  86. var tag = arrTwo[keyTwo];// 要查找的目标标签
  87. var flag = false; // 默认找不到
  88. for (keyOne in arrOne) {
  89. if (tag == arrOne[keyOne]) {
  90. flag = true;
  91. break;
  92. }
  93. }
  94. if (!flag) {// 没有找到
  95. result = false;
  96. break;
  97. }
  98. }
  99. return result;
  100. }
  101. function getItemList(items) {
  102. // var category =
  103. // ['movement','mana_regen','health_regen','health','critical_strike','spell_damage','mana','armor','spell_block',
  104. // 'damage','attack_speed','life_steal','consumable','cooldown_reduction'];
  105. var disable_items = {'1080':1,'3206':1,'3207':1,'3208':1,'3209':1,'3204':1,'3205':1,'3154':1,'3160':1,'2037':1,'2039':1,'3131':1,'3123':1,'3005':1,'3122':1,'3104':1,'3159':1,'3090':1,'3188':1,'3084':1,'2040':1,'2048':1,'3107':1, '3007':1,'3073':1,'3029':1, '3008':1, '3350':1, '3351':1, '3352':1,'2010':1 ,'1075':1};
  106. var result = {};
  107. for ( var i in items) {
  108. if(typeof(disable_items[i])!=='undefined') continue;
  109. var tags = items[i]['tags'];
  110. if (tags == false)
  111. continue;
  112. var tagsArr = tags.split(',');
  113. for ( var j in tagsArr) {
  114. //alert(typeof(result[tagsArr[j]]));
  115. if (typeof(result[tagsArr[j]])==='undefined') {
  116. result[tagsArr[j]] = new Array();
  117. }
  118. result[tagsArr[j]].push(i);
  119. }
  120. }
  121. return result;
  122. }
  123. function showItems(item_list,items){
  124. var temp = "";
  125. var item_id;
  126. for(var key in item_list){
  127. item_id = item_list[key];
  128. temp += "<li item_id="+item_id+" id='item_"+item_id+
  129. "'><a href='#'><img alt='' title='"+items[item_id]['name']+" "+items[item_id]['description']+
  130. "' src='http://img.lolbox.duowan.com/zb/"+
  131. item_id+"_40x40.png' /></a></li>";
  132. }
  133. document.write(temp);
  134. }
  135. // 绑定搜索事件
  136. $(function() {
  137. // ----------------------------------------------------------
  138. // 分类搜索英雄
  139. var checkboxs = $('#champion_tags_search :checkbox');
  140. checkboxs.click(function() {
  141. var tags = new Array();
  142. checkboxs.each(function() {
  143. var tag = $(this);
  144. if (tag.attr("checked") == "checked") {
  145. tags.push(tag.attr('value'));
  146. }
  147. });
  148. if (tags.length == 0) { // 没有勾选,显示全部
  149. $("#champion_show li").css({
  150. display : 'block'
  151. });
  152. } else {// 有勾选,进行选择
  153. var res = searchChampionByTags(tags, heroesList);
  154. // 先对所有隐藏
  155. $("#champion_show li").css({
  156. display : 'none'
  157. });
  158. // 然后只显示匹配的
  159. for ( var key in res) {
  160. var target = "#cham_" + res[key];
  161. $(target).css({
  162. display : 'block'
  163. });
  164. }
  165. }
  166. });
  167. // ---------------------------------------------------------
  168. // 关键字搜索英雄
  169. var keyworldTxt = $("input#keyworld");
  170. keyworldTxt.keyup(function() {
  171. checkboxs.each(function() {
  172. $(this).removeAttr("checked");
  173. });
  174. var keyworld = keyworldTxt.attr('value').trim();
  175. if (keyworld.length == 0) { // 没有输入关键字
  176. $("#champion_show li").css({
  177. display : 'block'
  178. });
  179. } else {
  180. var res = searchChampionByKeyworld(keyworld, heroesList);
  181. $("#champion_show li").css({
  182. display : 'none'
  183. });
  184. for ( var key in res) {
  185. var target = "#cham_" + res[key];
  186. $(target).css({
  187. display : 'block'
  188. });
  189. }
  190. }
  191. });
  192. /*
  193. // ---------------------------------------------------------
  194. // 分类搜索物品
  195. var item_checkboxs = $('#item_tags_search :checkbox');
  196. item_checkboxs.click(function() {
  197. var tags = new Array();
  198. item_checkboxs.each(function() {
  199. var tag = $(this);
  200. if (tag.attr("checked") == "checked") {
  201. tags.push(tag.attr('value'));
  202. }
  203. });
  204. if (tags.length == 0) { // 没有勾选,显示全部
  205. $("#item_show li").css({
  206. display : 'block'
  207. });
  208. } else {// 有勾选,进行选择
  209. var res = searchItemByTags(tags, ZBItems);
  210. var temp = "";
  211. var key;
  212. for (key in res) {
  213. temp += res[key] + " ";
  214. }
  215. // alert(temp);
  216. // 先对所有隐藏
  217. $("#item_show li").css({
  218. display : 'none'
  219. });
  220. // 然后只显示匹配的
  221. for ( var key in res) {
  222. var target = "#item_" + res[key];
  223. $(target).css({
  224. display : 'block'
  225. });
  226. }
  227. }
  228. });
  229. */
  230. });