Randomized Bonus

Create randomized bonus games on Warlight.

  1. // ==UserScript==
  2. // @name Randomized Bonus
  3. // @namespace Deadman_RB
  4. // @version 1.02
  5. // @description Create randomized bonus games on Warlight.
  6. // @author Deadman
  7. // @match https://www.warlight.net/Profile?p=*
  8. // @grant GM_xmlhttpRequest
  9. // ==/UserScript==
  10.  
  11. // Compute Player Ids
  12. var idRegex = /p=(\d+)/;
  13. var yourProfileLink = document.evaluate('/html/body/div[1]/span/div/a[2]',
  14. document, null, XPathResult.ANY_TYPE, null).iterateNext();
  15. var yourId = yourProfileLink.href.match(idRegex)[1];
  16. var opponentId = document.URL.match(idRegex)[1];
  17. if (yourId == opponentId) {
  18. opponentId = "OpenSeat";
  19. }
  20.  
  21. // Add text box and button
  22. var levelElement = document.evaluate(
  23. '//*[@id="MainSiteContent"]/table/tbody/tr[2]/td[2]/table/tbody/tr/td/big',
  24. document, null, XPathResult.ANY_TYPE, null).iterateNext();
  25. addRandomizedButton(levelElement);
  26.  
  27. function getSampleGameId() {
  28. /// <summary>
  29. /// Gets the sample game Id and checks if it is a number.
  30. /// </summary>
  31. /// <returns type="number">Game Id.</returns>
  32. var gameIdElement = document.getElementById("gameId");
  33. if (gameIdElement !== undefined) {
  34. return parseInt(gameIdElement.value, 10);
  35. }
  36. }
  37.  
  38. function extractGameSettings() {
  39. /// <summary>
  40. /// Extract game settings from the sample game using GameFeed API.
  41. /// </summary>
  42.  
  43. var sampleGameId = getSampleGameId();
  44. if (isNaN(sampleGameId)) {
  45. alert("Invalid GameId");
  46. } else {
  47. doAsyncRequest("POST",
  48. 'https://www.warlight.net/API/GameFeed?GameID=' +
  49. sampleGameId.toString() + '&GetHistory=true', {},
  50. "GameFeed");
  51. }
  52. }
  53.  
  54. function setupRandomizedGame(response) {
  55. /// <summary>
  56. /// From the GameFeed API response, randomize bonuses and create a game
  57. /// using the template.
  58. /// </summary>
  59. /// <param name="response" type="string">
  60. /// The GameFeed API response for the provided sample game.
  61. /// </param>
  62.  
  63. var obj = JSON.parse(response);
  64. if (obj != undefined) {
  65. var templateId = obj.templateID;
  66. var bonuses = [];
  67. for (var i = 0; i < obj.map.bonuses.length; i++) {
  68. var bonusObj = obj.map.bonuses[i];
  69. if (bonusObj.value != 0) {
  70. var bonus = [];
  71. var originalBonusValue = parseInt(bonusObj.value, 10);
  72.  
  73. // set the bonus value to (original-1, original+1)
  74. bonus.push(bonusObj.name);
  75. bonus.push(originalBonusValue - 1);
  76. bonus.push(originalBonusValue + 1);
  77. bonuses.push(bonus);
  78. }
  79. }
  80. }
  81. createGame(templateId, bonuses);
  82. }
  83.  
  84. function addRandomizedButton(levelElement) {
  85. /// <summary>
  86. /// Add a text box(for sample game Id) and a button to create randomized
  87. /// game.
  88. /// </summary>
  89. /// <param name="levelElement" type="Element">
  90. /// The parent element if text box and button.
  91. /// </param>
  92.  
  93. var br = document.createElement('br');
  94. var gameId = document.createElement("input");
  95. gameId.setAttribute("id", "gameId");
  96. gameId.setAttribute("type", "text");
  97. var createButton = document.createElement("input");
  98. createButton.setAttribute("type", "button");
  99. createButton.setAttribute("value", "Create Randomized game");
  100. createButton.onclick = function () {
  101. var oldValue = createButton.value;
  102. createButton.setAttribute('disabled', true);
  103. createButton.value = '...processing...';
  104.  
  105. setTimeout(function(){
  106. createButton.value = oldValue;
  107. createButton.removeAttribute('disabled');
  108. }, 1000);
  109. extractGameSettings();
  110. };
  111.  
  112. // used to store response from GameFeed API
  113. var hiddenResponse = document.createElement("input");
  114. hiddenResponse.setAttribute("type", "hidden");
  115. hiddenResponse.setAttribute("id", "WLresponse");
  116. hiddenResponse.onchange = function (value) {
  117. setupRandomizedGame(hiddenResponse.value);
  118. };
  119. levelElement.appendChild(br);
  120. levelElement.appendChild(gameId);
  121. levelElement.appendChild(createButton);
  122. levelElement.appendChild(hiddenResponse);
  123. }
  124.  
  125. function createGame(templateId, bonuses) {
  126. /// <summary>
  127. /// Create a game on Warlight between the two players on given settings.
  128. /// </summary>
  129. /// <param name="templateId" type="number">
  130. /// The game template Id.
  131. /// </param>
  132. /// <param name="bonuses" type="array">
  133. /// All bonuses on the map and the range of values they can take.
  134. /// </param>
  135.  
  136. var template = templateId;
  137. var postDataObject = {
  138. "gameName": "Randomized bonuses game",
  139. "personalMessage": "Check bonuses carefully as they may have been altered",
  140. "templateID": template,
  141. "players": [{
  142. "token": yourId,
  143. "team": "None"
  144. }, {
  145. "token": opponentId,
  146. "team": "None"
  147. }],
  148. "overriddenBonuses": []
  149. };
  150. if (bonuses !== null) {
  151. for (var i = 0; i < bonuses.length; i++) {
  152. var bonusName = bonuses[i][0];
  153. var min = bonuses[i][1];
  154. var max = bonuses[i][2];
  155. postDataObject.overriddenBonuses.push({
  156. "bonusName": bonusName,
  157. value: getRandomInt(min, max) // Randomize the bonus
  158. });
  159. }
  160. }
  161. var response = doAsyncRequest("POST",
  162. 'https://www.warlight.net/API/CreateGame', JSON.stringify(
  163. postDataObject), "CreateGame");
  164. }
  165.  
  166. function getRandomInt(min, max) {
  167. /// <summary>
  168. /// Pick a random number in the interval (min, max)
  169. /// </summary>
  170. /// <param name="min" type="number">
  171. /// lower bound of number
  172. /// </param>
  173. /// <param name="max" type="number">
  174. /// upper bound of number
  175. /// </param>
  176. /// <returns type="number">
  177. /// Random number in the interval
  178. /// </returns>
  179.  
  180. return Math.floor(Math.random() * (max - min + 1)) + min;
  181. }
  182.  
  183. function doAsyncRequest(method, url, data, api) {
  184. /// <summary>
  185. /// Perform an asynchronous request to create a game on Warlight.
  186. /// </summary>
  187. /// <param name="method" type="string">
  188. /// GET/POST
  189. /// </param>
  190. /// <param name="url" type="string">
  191. /// The request url.
  192. /// </param>
  193. /// <param name="data" type="dictionary">
  194. /// Request parameters
  195. /// </param>
  196. /// <param name="api" type="string">
  197. /// Warlight api type
  198. /// </param>
  199. GM_xmlhttpRequest({
  200. method: method,
  201. url: url,
  202. data: data,
  203. onreadystatechange: function (response) {
  204. if (response.readyState != 4) return;
  205. if (api === "GameFeed") {
  206. var hiddenResponse = document.getElementById(
  207. "WLresponse");
  208. hiddenResponse.value = response.responseText;
  209. hiddenResponse.onchange();
  210. } else if (api === "CreateGame") {
  211. var obj = JSON.parse(response.responseText);
  212. if (obj.gameID !== undefined) {
  213. window.open(
  214. "https://www.warlight.net/MultiPlayer?GameID=" +
  215. obj.gameID, '_parent ');
  216. } else if (obj.error !== undefined) {f
  217. alert("Cannot create game. Warlight says: " +
  218. obj.error);
  219. }
  220. }
  221. }
  222. });
  223. }

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址