PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/share/spice/game_info/game_info.js

http://github.com/duckduckgo/zeroclickinfo-spice
JavaScript | 113 lines | 84 code | 6 blank | 23 comment | 19 complexity | 11ddd006ce1a025482d5855ce559c934 MD5 | raw file
Possible License(s): Apache-2.0
  1. function ddg_spice_game_info(api_result) {
  2. "use strict";
  3. if(!api_result || api_result.error !== "OK" || $.isArray(api_result.results) || api_result.results.length === 0 ) {
  4. return Spice.failed('game_info');
  5. }
  6. var ignore = ["video", "game", "games", "giantbomb"];
  7. var games = api_result.results;
  8. var query = DDG.get_query();
  9. $.each(ignore, function(ind, phrase) {
  10. query = query.replace(phrase, "");
  11. });
  12. query = $.trim(query);
  13. // filter irrelevant or incomplete games
  14. games = $.grep(games, function(data, ind) {
  15. return data.name != null && data.image !== null && data.image.thumb_url != null && (DDG.isRelevant(data.name, ignore) || (data.aliases != null && DDG.isRelevant(data.aliases, ignore)));
  16. });
  17. // sort them by the number of reviews, which is pretty much how 'controversial' they are
  18. games = games.sort(function(a, b) {
  19. return b.number_of_user_reviews - a.number_of_user_reviews;
  20. });
  21. // quit if there aren't any relevant games
  22. if(games.length == 0) {
  23. return;
  24. }
  25. Spice.add({
  26. data : api_result,
  27. sourceUrl : "http://www.giantbomb.com/search/?q="+encodeURI(query),
  28. id : "game_info",
  29. sourceName : "GiantBomb",
  30. view: "Tiles",
  31. templates : {
  32. items : games,
  33. item: Spice.game_info.game_info,
  34. detail: Spice.game_info.game_info_details,
  35. // gets called in the event of a single result
  36. single_item_handler : function(obj) {
  37. var data = obj.data.results[0];
  38. // set the header
  39. obj.header1 = data.name + " (Game Info)";
  40. // set the image
  41. obj.image_url = data.image.icon_url;
  42. // set the source
  43. obj.sourceUrl = data.site_detail_url;
  44. }
  45. }
  46. });
  47. }
  48. /**
  49. * release_date
  50. *
  51. * Find the release date for a game
  52. */
  53. Handlebars.registerHelper("GameInfo_release_date", function() {
  54. "use strict";
  55. var date_info = {
  56. month: [
  57. "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
  58. ],
  59. day: [
  60. "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
  61. ]
  62. };
  63. var release = this.original_release_date.split(" ")[0];
  64. var parts = release.split("-");
  65. var date = new Date(parts[0], parts[1] - 1, parts[2]);
  66. return date_info.day[date.getDay()] + " " + DDG.getOrdinal(date.getDate()) + " " + date_info.month[date.getMonth()] + " " + date.getFullYear();
  67. });
  68. /**
  69. * platform_summary
  70. *
  71. * Summarise the platforms a game is available on
  72. */
  73. Handlebars.registerHelper("GameInfo_platform_summary", function(platforms, options) {
  74. "use strict";
  75. options.hash.sep = ", ";
  76. options.hash.conj = " and ";
  77. if(platforms.length > 4) {
  78. platforms = [platforms[0], platforms[1], platforms[2], {name: (platforms.length - 3) + " more"}]
  79. }
  80. return Handlebars.helpers.concat(platforms, options);
  81. });
  82. /**
  83. * age_rating
  84. *
  85. * Summarise the game's age rating
  86. */
  87. Handlebars.registerHelper("GameInfo_age_rating", function() {
  88. "use strict";
  89. var rating = "";
  90. var ratings = this.original_game_rating;
  91. // they are in the form PEGI: 3,...
  92. for(var i = 0; i < ratings.length; i++) {
  93. var parts = ratings[i].name.split(": ");
  94. if(parts.length == 2) {
  95. switch(parts[0]) {
  96. case "PEGI":
  97. rating = parts[1] + (rating.length > 0 ? " / "+rating : "");
  98. break;
  99. case "ESRB":
  100. rating = (rating.length > 0 ? rating+" / " : "") + parts[1];
  101. break;
  102. default:
  103. }
  104. }
  105. }
  106. return (rating.length == 0) ? "N/A" : rating;
  107. });