Guest User

Untitled

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