Guest User

Untitled

a guest
Jan 8th, 2016
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.45 KB | None | 0 0
  1. // ==UserScript==
  2. // @name TagPro Jerseys
  3. // @version 0.1
  4. // @description Set and change ball jerseys directly from the group page
  5. // @include http://tagpro-*.koalabeast.com:*
  6. // @include http://tagpro-*.koalabeast.com/groups/*
  7. // @include http://tangent.jukejuice.com:*
  8. // @include http://tangent.jukejuice.com/groups/*
  9. // @include http://*.newcompte.fr:*
  10. // @include http://*.newcompte.fr/groups/*
  11. // @grant GM_setValue
  12. // @grant GM_getValue
  13. // @author Some Ball -1
  14. // ==/UserScript==
  15.  
  16. // Spin the jerseys to show ball rotation
  17. var spinJerseys = true; //true or false
  18.  
  19. // Add your own imgur album links here inside quotes with commas between quoted album links
  20. // For example: var customAlbums = ["http://imgur.com/a/0zBNw", "http://imgur.com/a/1abcD"]; (do not include comma if only 1 album)
  21. // Images should have titles that match team names and a single digit numerical description that matches team color (0 for either/both, 1 for red, 2 for blue)
  22. var customAlbums = ["http://imgur.com/a/XOdZN"];
  23.  
  24. // Add your own imgur image links here inside quotes with commas between quoted image links, it must links to the png file
  25. // For example: var customImages = ["http://i.imgur.com/17aAwABc.png", "http://i.imgur.com/abc123DEF.png"]; (do not include comma if only 1 image)
  26. // Images should have titles that match team names and a single digit numerical description that matches team color (0 for either/both, 1 for red, 2 for blue)
  27. var customImages = [];
  28.  
  29.  
  30. if(location.pathname.match(/\/groups\/.+/)) //group page
  31. {
  32. var init = false;
  33. tagpro.group.socket.on('private',function(priv) {
  34. if(GM_getValue('groupId')!==location.pathname) //new group
  35. {
  36. GM_setValue('groupId',location.pathname);
  37. GM_setValue('fromGroup',false);
  38. GM_setValue('redJersey',false);
  39. GM_setValue('blueJersey',false);
  40. }
  41. if(priv.isPrivate && !init)
  42. {
  43. setup();
  44. init = true;
  45. }
  46. });
  47. function setup()
  48. {
  49. $('#redTeam div:first').after($('<table class="score" style="width: 94%; margin: 10px">').append($('<td style="font-weight: bold; padding: 0">').text('Jersey')).append($('<td style="width: 100%; padding: 0"><select id="redTeamJerseys" style="width: 100%"><option value="none">None</option>'))); //add jersey select
  50. $('#redTeam').prepend($('<table id="reds" style="width: 94%; margin: 10px 10px 0px 10px;">').append($('<td style="padding:0px 5px 0px 0px;">').append($('#redTeam :first').css({'width': '100%', 'margin': '0'}))).append($('<td style="padding:0px 0px 0px 5px;">').append($('#redTeam :first').css({'float': 'none', 'margin': '0'})))); //fix the Score not fitting
  51. $('#blueTeam div:first').after($('<table class="score" style="width: 94%; margin: 10px">').append($('<td style="font-weight: bold; padding: 0">').text('Jersey')).append($('<td style="width: 100%; padding: 0"><select id="blueTeamJerseys" style="width: 100%"><option value="none">None</option>')));
  52. $('#blueTeam').prepend($('<table id="blues" style="width: 94%; margin: 10px 10px 0px 10px;">').append($('<td style="padding:0px 5px 0px 0px;">').append($('#blueTeam :first').css({'width': '100%', 'margin': '0'}))).append($('<td style="padding:0px 0px 0px 5px;">').append($('#blueTeam :first').css({'float': 'none', 'margin': '0'}))));
  53. $('#redTeamJerseys').change(function() {GM_setValue('redJersey',$('#redTeamJerseys').val()==='none'?false:$('#redTeamJerseys').val())});
  54. $('#blueTeamJerseys').change(function() {GM_setValue('blueJersey',$('#blueTeamJerseys').val()==='none'?false:$('#blueTeamJerseys').val())});
  55. tagpro.group.socket.on('play',function() {
  56. GM_setValue('fromGroup',true);
  57. });
  58. tagpro.group.socket.on('private',function() {
  59. GM_setValue('fromGroup',false);
  60. });
  61. $('#joinButton').click(function() {
  62. GM_setValue('fromGroup',true);
  63. });
  64. $('#leaveButton').click(function() {
  65. GM_setValue('fromGroup',false);
  66. GM_setValue('redJersey',false);
  67. GM_setValue('blueJersey',false);
  68. });
  69. var jerseys = [[[]]], //league > team > jersey colors
  70. teams = [[]], //league > team
  71. leagues = []; //league
  72. $.ajax({
  73. url: 'https://api.imgur.com/3/album/0zBNw/images',
  74. headers: {
  75. 'Authorization': 'Client-ID d9ac388143def6c' //don't steal my client-id. get your own very quickly from here: https://api.imgur.com/oauth2/addclient
  76. },
  77. type: 'GET',
  78. success: function(data) {
  79. data.data.forEach(function(curr) {
  80. if(curr.description && curr.title)
  81. {
  82. var descriptor = curr.description.match(/(.+)\|(.+)/),
  83. league,
  84. team;
  85. if(leagues.indexOf(descriptor[1])===-1) //new league
  86. {
  87. leagues.push(descriptor[1]);
  88. league = leagues.length-1;
  89. teams[league] = [];
  90. jerseys[league] = [[]];
  91. }
  92. league = league || leagues.indexOf(descriptor[1]);
  93. if(teams[league].indexOf(curr.title)===-1) //new team
  94. {
  95. teams[league].push(curr.title);
  96. team = teams[league].length-1;
  97. jerseys[league][team] = [];
  98. }
  99. jerseys[league][team || teams[league].indexOf(curr.title)][parseInt(descriptor[2])] = curr.id;
  100. }
  101. });
  102. function nextAlbum(albumIndex)
  103. {
  104. if(albumIndex<customAlbums.length)
  105. {
  106. var id = customAlbums[albumIndex].match(/http:\/\/imgur\.com\/a\/(.+)/);
  107. ajaxAlbum(id[1],albumIndex+1);
  108. }
  109. else nextImage(0); //move on to custom images
  110. }
  111. function ajaxAlbum(id,nextIndex)
  112. {
  113. $.ajax({
  114. url: 'https://api.imgur.com/3/album/'+id+'/images',
  115. headers: {
  116. 'Authorization': 'Client-ID d9ac388143def6c' //don't steal my client-id. get your own very quickly from here: https://api.imgur.com/oauth2/addclient
  117. },
  118. type: 'GET',
  119. success: function(data) {
  120. data.data.forEach(function(curr) {
  121. if(curr.title)
  122. {
  123. var color = curr.description?curr.description.match(/\d/):0,
  124. league,
  125. team;
  126. if(leagues.indexOf('Custom')===-1)
  127. {
  128. leagues.push('Custom');
  129. league = leagues.length-1;
  130. teams[league] = [];
  131. jerseys[league] = [[]];
  132. }
  133. league = league || leagues.indexOf('Custom');
  134. if(teams[league].indexOf(curr.title)===-1)
  135. {
  136. teams[league].push(curr.title);
  137. team = teams[league].length-1;
  138. jerseys[league][team] = [];
  139. }
  140. jerseys[league][team || teams[league].indexOf(curr.title)][color?color:0] = curr.id;
  141. }
  142. });
  143. nextAlbum(nextIndex);
  144. }
  145. });
  146. }
  147. function nextImage(imageIndex)
  148. {
  149. if(imageIndex<customImages.length)
  150. {
  151. var id = customImages[imageIndex].match(/http:\/\/i\.imgur\.com\/(.+)\.png/);
  152. ajaxImage(id[1],imageIndex+1);
  153. }
  154. else sortAndAdd(); //move on to sorting
  155. }
  156. function ajaxImage(id,nextIndex)
  157. {
  158. $.ajax({
  159. url: 'https://api.imgur.com/3/image/'+id,
  160. headers: {
  161. 'Authorization': 'Client-ID d9ac388143def6c' //don't steal my client-id. get your own very quickly from here: https://api.imgur.com/oauth2/addclient
  162. },
  163. type: 'GET',
  164. success: function(data) {
  165. var curr = data.data;
  166. if(curr.title)
  167. {
  168. var color = curr.description?curr.description.match(/\d/):0,
  169. league,
  170. team;
  171. if(leagues.indexOf('Custom')===-1)
  172. {
  173. leagues.push('Custom');
  174. league = leagues.length-1;
  175. teams[league] = [];
  176. jerseys[league] = [[]];
  177. }
  178. league = league || leagues.indexOf('Custom');
  179. if(teams[league].indexOf(curr.title)===-1)
  180. {
  181. teams[league].push(curr.title);
  182. team = teams[league].length-1;
  183. jerseys[league][team] = [];
  184. }
  185. jerseys[league][team || teams[league].indexOf(curr.title)][color?color:0] = curr.id;
  186. }
  187. nextImage(nextIndex);
  188. }
  189. });
  190. }
  191. nextAlbum(0); //begin with custom albums
  192. }
  193. });
  194. function sortAndAdd()
  195. {
  196. var toSortLeagues = [],
  197. toSortTeams = [],
  198. haveCustom = leagues.indexOf('Custom')>-1?1:0;
  199. for(var i = 0;i < leagues.length;i++)
  200. {
  201. toSortTeams = [];
  202. for(var j = 0;j < teams[i].length;j++)
  203. {
  204. toSortTeams.push([teams[i][j],jerseys[i][j]]);
  205. }
  206. toSortTeams.sort(function(a,b) {
  207. if(a[0].toUpperCase()>b[0].toUpperCase()) return 1; //regular alphabetical sort, ignore case
  208. if(a[0].toUpperCase()<b[0].toUpperCase()) return -1;
  209. if(a[0]>b[0]) return 1; //sort with case if otherwise identical
  210. if(a[0]<b[0]) return -1;
  211. return 0;
  212. });
  213. for(var j = 0;j < toSortTeams.length;j++)
  214. {
  215. teams[i][j] = toSortTeams[j][0];
  216. jerseys[i][j] = toSortTeams[j][1];
  217. }
  218. if(!haveCustom || (haveCustom && i<leagues.length-1)) //ignore 'Custom' league for sorting
  219. {
  220. toSortLeagues.push([leagues[i],teams[i],jerseys[i]]);
  221. }
  222. }
  223. toSortLeagues.sort(function(a,b) {
  224. if(a[0].toUpperCase()>b[0].toUpperCase()) return 1;
  225. if(a[0].toUpperCase()<b[0].toUpperCase()) return -1;
  226. if(a[0]>b[0]) return 1;
  227. if(a[0]<b[0]) return -1;
  228. return 0;
  229. });
  230. if(haveCustom)
  231. {
  232. toSortLeagues.push([leagues[leagues.length-1],teams[teams.length-1],jerseys[jerseys.length-1]]); //add 'Custom' back at end of list
  233. }
  234. for(var i = 0;i < toSortLeagues.length;i++)
  235. {
  236. leagues[i] = toSortLeagues[i][0];
  237. teams[i] = toSortLeagues[i][1];
  238. jerseys[i] = toSortLeagues[i][2];
  239. var groupRed = $('<optgroup label="'+leagues[i]+'">');
  240. var groupBlue = $('<optgroup label="'+leagues[i]+'">');
  241. for(var j = 0;j < teams[i].length;j++)
  242. {
  243. var team = jerseys[i][j],
  244. toAppend;
  245. if(team[1]) toAppend = team[1];
  246. else if(team[0]) toAppend = team[0]
  247. toAppend = $('<option value="'+toAppend+'">'+teams[i][j]+'</option>');
  248. groupRed.append(toAppend);
  249. if(team[2]) toAppend = team[2];
  250. else if(team[0]) toAppend = team[0];
  251. toAppend = $('<option value="'+toAppend+'">'+teams[i][j]+'</option>');
  252. groupBlue.append(toAppend);
  253. }
  254. $('#redTeamJerseys')[0].add(groupRed[0]);
  255. $('#blueTeamJerseys')[0].add(groupBlue[0]);
  256. }
  257. if(GM_getValue('redJersey')) $('#redTeamJerseys').val(GM_getValue('redJersey'));
  258. if(GM_getValue('blueJersey')) $('#blueTeamJerseys').val(GM_getValue('blueJersey'));
  259. }
  260. }
  261. }
  262. else //in-game, draw jersey if there is one
  263. {
  264. tagpro.ready(function() {
  265. if(!tagpro.group.socket || !GM_getValue('fromGroup'))
  266. {
  267. GM_setValue('fromGroup',false);
  268. GM_setValue('redJersey',false);
  269. GM_setValue('blueJersey',false);
  270. }
  271. else if(GM_getValue('redJersey') || GM_getValue('blueJersey'))
  272. {
  273. var red = GM_getValue('redJersey');
  274. var blue = GM_getValue('blueJersey');
  275. var jersey = [red==='none'?false:red,blue==='none'?false:blue]; //incase 'none' somehow makes it through
  276. if(jersey[0] || jersey[1])
  277. {
  278. var tr = tagpro.renderer,
  279. oldUPSP = tr.updatePlayerSpritePosition;
  280.  
  281. tr.createJersey = function(player) {
  282. if(!jersey[player.team-1]) //make empty container if one team doesn't have a jersey
  283. {
  284. if(player.sprites.jersey) player.sprites.ball.removeChild(player.sprites.jersey);
  285. player.sprites.jersey = new PIXI.DisplayObjectContainer();
  286. player.sprites.jersey.team = player.team;
  287. player.sprites.ball.addChildAt(player.sprites.jersey,1);
  288. }
  289. else
  290. {
  291. if(player.sprites.jersey) player.sprites.ball.removeChild(player.sprites.jersey);
  292. player.sprites.jersey = new PIXI.Sprite(PIXI.Texture.fromImage('http://i.imgur.com/'+jersey[player.team-1]+'.png'));
  293. player.sprites.jersey.team = player.team;
  294. player.sprites.ball.addChildAt(player.sprites.jersey,1); //add on top of ball, below other stuff
  295. player.sprites.jersey.anchor.x = 0.5;
  296. player.sprites.jersey.anchor.y = 0.5;
  297. player.sprites.jersey.x = 20;
  298. player.sprites.jersey.y = 20;
  299. }
  300. };
  301. tr.updatePlayerSpritePosition = function(player) {
  302. if(!player.sprites.jersey) tr.createJersey(player);
  303. if(player.sprites.jersey.team!==player.team) tr.createJersey(player);
  304. var index = player.sprites.ball.getChildIndex(player.sprites.actualBall)+1;
  305. if(index!==player.sprites.ball.getChildIndex(player.sprites.jersey)) player.sprites.ball.setChildIndex(player.sprites.jersey,index);
  306. if(spinJerseys) player.sprites.jersey.rotation = player.angle;
  307. oldUPSP(player);
  308. };
  309. }
  310. tagpro.socket.on('end',function() {
  311. GM_setValue('fromGroup',false);
  312. });
  313. }
  314. });
  315. }
Advertisement
Add Comment
Please, Sign In to add comment