PageRenderTime 25ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/SteamMatchup/Scripts/matchup.js

https://github.com/djeebus/SteamMatchUp
JavaScript | 295 lines | 230 code | 63 blank | 2 comment | 28 complexity | 3a04ef691237752547495a080cd2bec9 MD5 | raw file
  1. (function ($) {
  2. $(document).ready(function () {
  3. $('form#add-user').submit(function () {
  4. if ($(this).valid()) {
  5. var username = $('#username').val();
  6. var exists = false;
  7. $.each(users, function () {
  8. if (this.username == username) {
  9. exists = true;
  10. return false;
  11. }
  12. });
  13. if (exists) {
  14. alert("This user is already being compared.");
  15. } else {
  16. addUser(username);
  17. }
  18. }
  19. return false;
  20. });
  21. $('form#add-user').validate();
  22. $('#show-missing').change(hideMissingPlayers);
  23. $('#friends .friend a.add').live('click', function () {
  24. var username = $(this).parents('.friend').attr('id');
  25. addUser(username);
  26. });
  27. $('.user a').live('click', function (e) {
  28. e.preventDefault();
  29. var index = $(this).data('user-index');
  30. removeUser(index);
  31. return false;
  32. });
  33. $('#options').accordion({
  34. collapsible: true,
  35. autoHeight: false,
  36. active: false
  37. });
  38. });
  39. function addUser(username) {
  40. addUserToComparison(username);
  41. addFriendsToContainer(username);
  42. }
  43. function removeUser(index) {
  44. users.splice(index, 1);
  45. friends.splice(index, 1);
  46. updatePlayers();
  47. updateFriends();
  48. }
  49. function hideMissingPlayers() {
  50. var minPlayers = $('#show-missing').val();
  51. for (var x = users.length; x > 0; x--) {
  52. var $games = $('.game.players-' + x);
  53. if (x >= minPlayers) {
  54. $games.show();
  55. } else {
  56. $games.hide();
  57. }
  58. }
  59. var total = $("<h3><span class='games-shown' /> games displayed</h3>");
  60. total.find('.games-shown').text($('.game:visible').length / users.length);
  61. $('#users').prepend(total);
  62. }
  63. var friends = [];
  64. function addFriendsToContainer(username) {
  65. $.ajax('/home/getfriends', {
  66. type: 'POST',
  67. data: {
  68. username: username
  69. },
  70. success: function (data) {
  71. data.username = username;
  72. friends[friends.length] = data;
  73. updateFriends();
  74. },
  75. error: function (req, status) {
  76. // error will probably be shown when trying to get their games
  77. //alert(status + ': ' + req.responseText);
  78. }
  79. });
  80. }
  81. var users = [];
  82. function addUserToComparison(username) {
  83. $('input[type=submit]').attr('disabled', 'disabled');
  84. $.ajax('/home/getgames', {
  85. type: 'POST',
  86. data: {
  87. username: username
  88. },
  89. success: function (data) {
  90. data.username = username;
  91. users[users.length] = data;
  92. updatePlayers();
  93. $.each(friends, function (x) {
  94. if (this.id == username) {
  95. friends.splice(x, 1);
  96. return false;
  97. }
  98. });
  99. },
  100. error: function (req, status) {
  101. alert('error: this user does not exist, or does not have their profile set up.');
  102. },
  103. complete: function () {
  104. $('input[type=submit]').removeAttr('disabled');
  105. }
  106. });
  107. }
  108. function updateFriends() {
  109. var uniqueFriends = getUniqueFriends();
  110. sortByName(uniqueFriends);
  111. $('#friends').empty();
  112. var tmpl = $('#friend').tmpl(uniqueFriends)
  113. tmpl.appendTo('#friends');
  114. }
  115. function getUniqueFriends() {
  116. var unique = [];
  117. $.each(friends, function () {
  118. var oneUsersFriends = this;
  119. $.each(oneUsersFriends, function () {
  120. var userFriend = this;
  121. var exists = false;
  122. $.each(unique, function () {
  123. if (this.id == userFriend.CommunityId) {
  124. exists = true;
  125. return false;
  126. }
  127. });
  128. $.each(users, function () {
  129. if (this.username == userFriend.CommunityId) {
  130. exists = true;
  131. return false;
  132. }
  133. });
  134. if (!exists) {
  135. unique[unique.length] = {
  136. steam: this.CommunityUrl,
  137. icon: this.IconUrl,
  138. name: this.Name,
  139. id: this.CommunityId
  140. };
  141. }
  142. });
  143. });
  144. return unique;
  145. }
  146. function updatePlayers() {
  147. var games = mergeGamesArrays();
  148. sortByName(games);
  149. generateLists(games);
  150. hideMissingPlayers();
  151. }
  152. function generateLists(games) {
  153. $('#users').empty();
  154. updateMissingOption();
  155. for (var u = 0; u < users.length; u++) {
  156. var user = users[u];
  157. var container = $('<div class="user"><h2><span class="username" /><a class="remove" href="javascript:void(0)">remove</a></h2><h3><span class="mine" /> games</div>');
  158. container.find('.username').text(user.username);
  159. container.find('a').data('user-index', u);
  160. var total = 0, mine = 0;
  161. $.each(games, function () {
  162. total++;
  163. var game = this;
  164. var exists = false;
  165. $.each(user, function () {
  166. var userGame = this;
  167. if (userGame.SteamUrl == game.steam) {
  168. exists = true;
  169. return false;
  170. }
  171. });
  172. if (exists) { mine++; }
  173. $('#game').tmpl(game, { 'css': exists ? 'exists' : 'missing' }).appendTo(container);
  174. });
  175. container.find('.mine').text(user.length);
  176. $('#users').append(container);
  177. }
  178. }
  179. function updateMissingOption() {
  180. var $select = $('#show-missing');
  181. $select.empty();
  182. for (var x = users.length; x > 0; x--) {
  183. var $option = $('<option />');
  184. $option.text(x);
  185. $select.append($option);
  186. }
  187. if (users.length < 2) {
  188. $('.show-missing').hide();
  189. } else {
  190. $('.show-missing').show();
  191. }
  192. }
  193. function mergeGamesArrays() {
  194. var games = [];
  195. $.each(users, function () {
  196. var u = this;
  197. $.each(this, function () {
  198. var userGame = this;
  199. var g = null;
  200. $.each(games, function () {
  201. if (this.steam == userGame.SteamUrl) {
  202. g = this;
  203. return false;
  204. }
  205. });
  206. if (g === null) {
  207. g = {
  208. name: userGame.Name,
  209. icon: userGame.IconUrl,
  210. steam: userGame.SteamUrl,
  211. players: 0
  212. };
  213. games[games.length] = g;
  214. }
  215. g.players++;
  216. });
  217. });
  218. return games;
  219. }
  220. function sortByName(array) {
  221. array.sort(function (a, b) {
  222. var x = a.name.toLowerCase();
  223. var y = b.name.toLowerCase();
  224. if (x < y)
  225. return -1;
  226. else if (x > y)
  227. return 1;
  228. else
  229. return 0;
  230. });
  231. }
  232. })(jQuery);