CG Multiplayer Helper

Adds a SWAP and PLAY_100 button to IDE

当前为 2016-08-10 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name CG Multiplayer Helper
  3. // @namespace http://hentschels.com/
  4. // @version 0.1
  5. // @description Adds a SWAP and PLAY_100 button to IDE
  6. // @author danBhentschel
  7. // @match https://www.codingame.com/ide/*
  8. // @grant none
  9. // @require http://code.jquery.com/jquery-latest.js
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14. var g_stopPlay100;
  15. $(document).on('DOMNodeInserted', checkForAgentPanel);
  16. function checkForAgentPanel(event) {
  17. if ($(event.target).is('.cg-ide-agents-management') &&
  18. $('.cg-ide-agents-management > .scroll-panel').length) {
  19. $(document).off('DOMNodeInserted', checkForAgentPanel);
  20. createSwapButton();
  21. createPlay100Button();
  22. createStopButton();
  23. }
  24. }
  25. function createSwapButton() {
  26. var panel = $('.cg-ide-agents-management > .scroll-panel');
  27. var swapButton = document.createElement('BUTTON');
  28. swapButton.innerHTML = 'SWAP';
  29. swapButton.setAttribute('id', 'pOneSwapButton');
  30. swapButton.style.padding = '5px 5px 5px 5px';
  31. panel.append(swapButton);
  32. console.log('P_ONE: Create SWAP button');
  33. $('#pOneSwapButton').click(swapClicked);
  34. }
  35. function swapClicked() {
  36. console.log('P_ONE: SWAP clicked');
  37.  
  38. var agentNames = getAgentNames();
  39. deleteAgents(agentNames);
  40. addAgents(agentNames);
  41. }
  42. function getAgentNames() {
  43. var agentNames = [];
  44. $('.agent').each(function(index) {
  45. agentNames[index] = $(this).find('.nickname').text();
  46. });
  47. return agentNames;
  48. }
  49. function deleteAgents(agentNames) {
  50. $('.delete-button').each(function(index) {
  51. console.log('P_ONE: Click DELETE for ' + agentNames[index]);
  52. $(this).click();
  53. });
  54. }
  55. function addAgents(agentNames) {
  56. addAgent($('.add-player-square'), agentNames, 0);
  57. }
  58. function addAgent(buttons, agentNames, position) {
  59. if (position >= agentNames.length) return;
  60. var nameIndex = (position + 1) % agentNames.length;
  61. var wantedAgentName = agentNames[nameIndex];
  62. console.log('P_ONE: Click ADD for ' + wantedAgentName + ' in position ' + position);
  63.  
  64. buttons.eq(position).click();
  65. setTimeout(() => { checkForPlayerSelector(buttons, agentNames, wantedAgentName, position); }, 500);
  66. }
  67. function checkForPlayerSelector(buttons, agentNames, wantedAgentName, position) {
  68. var searchBox = $('.field');
  69. if (searchBox.length && searchBox.closest('.searchfield').length) {
  70. selectAgentToAdd(buttons, agentNames, wantedAgentName, position);
  71. } else {
  72. setTimeout(() => { checkForPlayerSelector(buttons, agentNames, wantedAgentName, position); }, 500);
  73. }
  74. }
  75. function selectAgentToAdd(buttons, agentNames, wantedAgentName, position) {
  76. searchForAgent(wantedAgentName);
  77. setTimeout(() => { addSpecificAgent(buttons, agentNames, wantedAgentName, position); }, 200);
  78. }
  79. function addSpecificAgent(buttons, agentNames, wantedAgentName, position) {
  80. var found = false;
  81. $('.player-add-card').each(function() {
  82. var agentCardName = $(this).find('.agent-card-nickname').text();
  83. if (agentCardName == wantedAgentName) {
  84. console.log('P_ONE: Found ' + agentCardName);
  85. $(this).find('.add-agent-button').click();
  86. found = true;
  87. return false;
  88. }
  89. });
  90. if (found) {
  91. addAgent(buttons, agentNames, position + 1);
  92. } else {
  93. setTimeout(() => { addSpecificAgent(buttons, agentNames, wantedAgentName, position); }, 200);
  94. }
  95. }
  96. function searchForAgent(agentName) {
  97. var searchBox = $('.field');
  98. console.log('P_ONE: Search for ' + agentName);
  99. searchBox.val(agentName);
  100. searchBox[0].focus();
  101. pressEnter(searchBox[0]);
  102. }
  103. function pressEnter(element) {
  104. pressKey(13, element);
  105. }
  106. function pressKey(k, element) {
  107. var oEvent = document.createEvent('KeyboardEvent');
  108.  
  109. // Chromium Hack
  110. Object.defineProperty(oEvent, 'keyCode', {
  111. get : function() {
  112. return this.keyCodeVal;
  113. }
  114. });
  115. Object.defineProperty(oEvent, 'which', {
  116. get : function() {
  117. return this.keyCodeVal;
  118. }
  119. });
  120.  
  121. if (oEvent.initKeyboardEvent) {
  122. oEvent.initKeyboardEvent("keydown", true, true, document.defaultView, false, false, false, false, k, k);
  123. } else {
  124. oEvent.initKeyEvent("keydown", true, true, document.defaultView, false, false, false, false, k, 0);
  125. }
  126.  
  127. oEvent.keyCodeVal = k;
  128.  
  129. if (oEvent.keyCode !== k) {
  130. alert("keyCode mismatch " + oEvent.keyCode + "(" + oEvent.which + ")");
  131. }
  132.  
  133. element.dispatchEvent(oEvent);
  134. }
  135. function createPlay100Button() {
  136. var panel = $('.cg-ide-agents-management > .scroll-panel');
  137. var play100Button = document.createElement('BUTTON');
  138. play100Button.innerHTML = 'PLAY_100';
  139. play100Button.setAttribute('id', 'pOnePlay100Button');
  140. play100Button.style.padding = '5px 5px 5px 5px';
  141. panel.append(play100Button);
  142. console.log('P_ONE: Create PLAY_100 button');
  143. $('#pOnePlay100Button').click(play100Clicked);
  144. }
  145. function play100Clicked() {
  146. console.log('P_ONE: PLAY_100 clicked');
  147. g_stopPlay100 = false;
  148. $('#pOneStopButton').show();
  149. $('#pOnePlay100Button').hide();
  150. startPlay(1, { names: [], record: {} });
  151. }
  152. function startPlay(iteration, results) {
  153. if (iteration > 100 || g_stopPlay100) {
  154. $('#pOneStopButton').hide();
  155. $('#pOnePlay100Button').show();
  156. return;
  157. }
  158. console.log('Play ' + pad(iteration));
  159. $('.play').click();
  160. setTimeout(() => { checkForResult(iteration, results); }, 200);
  161. }
  162. function pad(num) {
  163. var s = "00" + num;
  164. return s.substr(s.length-3);
  165. }
  166. function checkForResult(iteration, results) {
  167. var rankedNames = $('.cg-ide-mini-leaderboard').find('.nickname');
  168. if ($('.play').is(':disabled') || rankedNames.length === 0) {
  169. setTimeout(() => { checkForResult(iteration, results); }, 200);
  170. } else {
  171. rankedNames.each(function(index) {
  172. var name = $(this).text();
  173. if (!results.record.hasOwnProperty(name)) {
  174. results.names.push(name);
  175. results.record[name] = 0;
  176. }
  177. if (index === 0) results.record[name]++;
  178. });
  179. logResults(results);
  180. startPlay(iteration + 1, results);
  181. }
  182. }
  183. function logResults(results) {
  184. var report = '';
  185. for (var i = 0; i < results.names.length; i++) {
  186. var name = results.names[i];
  187. report += name + ': ' + results.record[name] + ' ';
  188. }
  189. console.log(report);
  190. }
  191. function createStopButton() {
  192. var panel = $('.cg-ide-agents-management > .scroll-panel');
  193. var stopButton = document.createElement('BUTTON');
  194. stopButton.innerHTML = 'STOP';
  195. stopButton.setAttribute('id', 'pOneStopButton');
  196. stopButton.style.padding = '5px 5px 5px 5px';
  197. panel.append(stopButton);
  198. console.log('P_ONE: Create STOP button');
  199. $('#pOneStopButton').click(stopClicked);
  200. $('#pOneStopButton').hide();
  201. }
  202.  
  203. function stopClicked() {
  204. g_stopPlay100 = true;
  205. console.log('P_ONE: STOP requested. Stopping at next opportunity');
  206. }
  207. })();