Replay Rewrite - AI Testing On Replay

Offensive & Defensive AI Buttons On GLB Replay

  1. // ==UserScript==
  2. // @name Replay Rewrite - AI Testing On Replay
  3. // @namespace pbr/aitor
  4. // @description Offensive & Defensive AI Buttons On GLB Replay
  5. // @include http://*goallineblitz.com/game/home.pl
  6. // @include http://*goallineblitz.com/game/replay.pl?game_id=*&pbp_id=*
  7. // @include http://glb.warriorgeneral.com/game/home.pl
  8. // @include http://glb.warriorgeneral.com/game/replay.pl?game_id=*&pbp_id=*
  9. // @copyright 2009, pabst
  10. // @license (CC) Attribution Share Alike; http://creativecommons.org/licenses/by-sa/3.0/
  11. // @version 14.01.02
  12. // @require https://greasyfork.org/scripts/1371-libpbr2/code/libpbr2.js?version=3533
  13. // @require https://greasyfork.org/scripts/1348-pbr-game-scout/code/pbr%20Game%20Scout.js?version=3544
  14. // @require https://greasyfork.org/scripts/1373-pbr-game-scout-statslib/code/pbr%20Game%20Scout%20statslib.js?version=3538
  15. // ==/UserScript==
  16.  
  17. var scriptName = "AI Testing";
  18. var scriptVersion = "14.01.02";
  19. var scriptWebpage = "http://userscripts.org/scripts/show/54927";
  20. /*
  21. *
  22. * pabst did this 09/08/01+
  23. *
  24. *
  25. */
  26.  
  27. window.setTimeout( function() {
  28. console.log("AI start: "+GM_getValue("gmlist"));
  29. if ((window.location+"").indexOf("home.pl") != -1) {
  30. atHome();
  31. }
  32. else {
  33. pageMod();
  34.  
  35. init(true);
  36. }
  37. }, 100);
  38.  
  39. function atHome() {
  40. GM_setValue("gmlist","");
  41. var gmlist = "";
  42.  
  43. for (var i=0; i<document.links.length; i++) {
  44. if (document.links[i].href.match("team_tactics.pl") != null) {
  45. var id = document.links[i].href.toString();
  46. id = id.slice(id.indexOf("=")+1);
  47.  
  48. var str = document.body.innerHTML.split('game/team.pl?team_id='+id+'"')[1].split("</a>")[0];
  49. if (str.match('class="team_name"') != null) str = str.split(">")[1];
  50. else str = str.slice(1);
  51. gmlist += GM_getValue("gmlist")+id+":"+str+"\t";
  52. }
  53. }
  54.  
  55. if (gmlist.length > 0) gmlist = gmlist.slice(0,gmlist.length-1);
  56. GM_setValue("gmlist",gmlist);
  57. console.log("gmlist="+gmlist);
  58. }
  59.  
  60. function pageMod() {
  61. var div = document.createElement("div");
  62. div.setAttribute("id","dai_container");
  63. div.style.width = "480px";
  64. div.style.visiblity = "hidden";
  65. div.style.display = "none";
  66.  
  67. var el = document.getElementById("footer");
  68. el.parentNode.insertBefore(div, el);
  69.  
  70. var oaitest = document.createElement("input");
  71. oaitest.setAttribute("type","button");
  72. oaitest.setAttribute("id","oai_button");
  73. oaitest.setAttribute("value","Test OAI");
  74. oaitest.disabled = false;
  75. oaitest.addEventListener("click",testOAI,true);
  76. oaitest.disabled = true;
  77. div.appendChild(oaitest);
  78.  
  79. var select = document.createElement("select");
  80. select.setAttribute("id","oai_team_select");
  81. div.appendChild(select);
  82. select.addEventListener("change", clearOAI, false);
  83.  
  84. var tms = ["0:Mine"].concat(GM_getValue("gmlist","").split("\t"));
  85. if (tms[0].length > 0) {
  86. for (var i=0; i<tms.length; i++) {
  87. var opt = document.createElement('option');
  88. opt.text = tms[i].split(":")[1];
  89. opt.value = tms[i].split(":")[0];
  90. select.add(opt,null);
  91. }
  92. div.style.visibility = "visible";
  93. div.style.display = "block";
  94. }
  95.  
  96. var load = document.createElement("input");
  97. load.setAttribute("type","button");
  98. load.setAttribute("value","Load OAI");
  99. load.addEventListener("click",oaiPageLoad,true);
  100. div.appendChild(load);
  101.  
  102. var select2 = document.createElement("select");
  103. select2.setAttribute("id","oai_select");
  104. div.appendChild(select2);
  105.  
  106. var br = document.createElement("br");
  107. div.appendChild(br);
  108.  
  109. var daitest = document.createElement("input");
  110. daitest.setAttribute("type","button");
  111. daitest.setAttribute("id","dai_button");
  112. daitest.setAttribute("value","Test DAI");
  113. daitest.addEventListener("click",testDAI,true);
  114. daitest.disabled = true;
  115. div.appendChild(daitest);
  116.  
  117. var select = document.createElement("select");
  118. select.setAttribute("id","dai_team_select");
  119. div.appendChild(select);
  120. select.addEventListener("change", clearDAI, false);
  121.  
  122. var tms = ["0:Mine"].concat(GM_getValue("gmlist","").split("\t"));
  123. if (tms[0].length > 0) {
  124. for (var i=0; i<tms.length; i++) {
  125. var opt = document.createElement('option');
  126. opt.text = tms[i].split(":")[1];
  127. opt.value = tms[i].split(":")[0];
  128. select.add(opt,null);
  129. }
  130. div.style.visibility = "visible";
  131. div.style.display = "block";
  132. }
  133.  
  134. var load = document.createElement("input");
  135. load.setAttribute("type","button");
  136. load.setAttribute("value","Load DAI");
  137. load.addEventListener("click",daiPageLoad,true);
  138. div.appendChild(load);
  139.  
  140. var select2 = document.createElement("select");
  141. select2.setAttribute("id","dai_select");
  142. div.appendChild(select2);
  143.  
  144. var pos = ["HB","FB","QB","TE"];
  145. var tags = ["Untagged","Speedy","Combo","Power","Rusher","Passer","Receiver","Blocker",
  146. "Custom Tag 1","Custom Tag 2","Custom Tag 3"];
  147. var flags = ["none","speedy","combo","power","rusher","passer","receiver","blocker","custom1","custom2","custom3"];
  148.  
  149. for (var p=0; p<pos.length; p++) {
  150. if (p%2 == 0) {
  151. var br = document.createElement("br");
  152. div.appendChild(br);
  153. }
  154.  
  155. var select = document.createElement("select");
  156. select.setAttribute("id",pos[p]+"_tag_select");
  157. select.setAttribute("class","tag_select");
  158. select.disabled = true;
  159.  
  160. for (var t=0; t<tags.length; t++) {
  161. var opt = document.createElement('option');
  162. opt.text = pos[p]+" "+tags[t];
  163. opt.value = flags[t];
  164. select.add(opt,null);
  165. }
  166.  
  167. div.appendChild(select);
  168. }
  169. }
  170.  
  171. function clearOAI() {
  172. clearAI("oai");
  173. }
  174. function clearDAI() {
  175. clearAI("dai");
  176. for (var i=0; i<document.getElementsByClassName("tag_select").length; i++) {
  177. document.getElementsByClassName("tag_select")[i].disabled = true;
  178. }
  179. }
  180. function clearAI(str) {
  181. var select = document.getElementById(str+"_select");
  182. while (select.options.length > 0) {
  183. select.remove(0);
  184. }
  185.  
  186. document.getElementById(str+"_button").disabled = true;
  187. for (var i=0; i<document.getElementsByClassName("tab_select").length; i++) {
  188. document.getElementsByClassName("tab_select")[i].disabled = true;
  189. }
  190. }
  191.  
  192. function oaiPageLoad() {
  193. var teamId = document.getElementById("oai_team_select").value;
  194. if (teamId == null) return;
  195.  
  196. var address = "/game/team_ai.pl?team_id="+teamId;
  197. address += "&ai_type=team";
  198. getInetPage(address, loadOAI, null);
  199. }
  200.  
  201. function loadOAI(address, page) {
  202. clearOAI();
  203.  
  204. var select = document.getElementById("oai_select");
  205. var aiList = page.responseText.split("team_offense_ai.pl?team_id=");
  206. for (var i=1; i<aiList.length; i++) {
  207. var str = aiList[i].split("\">")[1].split("</a>")[0];
  208. var id = aiList[i].split("=")[1].split("\"")[0];
  209.  
  210. var opt = document.createElement("option");
  211. opt.text = str;
  212. opt.value = id;
  213. select.add(opt,null);
  214. }
  215.  
  216. if (select.options.length > 0) document.getElementById("oai_button").disabled = false;
  217. }
  218.  
  219. function daiPageLoad() {
  220. var teamId = document.getElementById("dai_team_select").value;
  221. if (teamId == null) return;
  222.  
  223. var address = "/game/team_ai.pl?team_id="+teamId;
  224. address += "&ai_type=team";
  225. getInetPage(address, loadDAI, null);
  226. }
  227.  
  228. function loadDAI(address, page) {
  229. clearDAI();
  230.  
  231. var select = document.getElementById("dai_select");
  232. var aiList = page.responseText.split("team_defense_ai.pl?team_id=");
  233. for (var i=1; i<aiList.length; i++) {
  234. var str = aiList[i].split("\">")[1].split("</a>")[0];
  235. var id = aiList[i].split("=")[1].split("\"")[0];
  236.  
  237. var opt = document.createElement("option");
  238. opt.text = str;
  239. opt.value = id;
  240. select.add(opt,null);
  241. }
  242.  
  243. if (select.options.length > 0) {
  244. document.getElementById("dai_button").disabled = false;
  245. for (var i=0; i<document.getElementsByClassName("tag_select").length; i++) {
  246. document.getElementsByClassName("tag_select")[i].disabled = false;
  247. }
  248. }
  249. }
  250.  
  251. function activate(e) {
  252. console.log("activate AI testing");
  253. lock();
  254.  
  255. run();
  256. }
  257.  
  258. function run() {
  259. var pbp = document.getElementById("pbp");
  260. if (pbp == null) {
  261. setTimeout(run, 300);
  262. }
  263. else if (pbp.childNodes.length == 0) {
  264. setTimeout(run, 300);
  265. }
  266. else {
  267. var page = new Object();
  268. page.responseText = pbp.innerHTML;
  269.  
  270. loadPBPSimple(page);
  271.  
  272. unlock();
  273. }
  274. }
  275.  
  276. function testDAI() {
  277. var oteam = document.getElementsByClassName("secondary_container")[0].childNodes[0].innerHTML.split("&nbsp;")[0];
  278. var dteam = document.getElementsByClassName("secondary_container")[1].childNodes[0].innerHTML.split("&nbsp;")[0];
  279.  
  280. var timeytg = document.getElementById("time_ytg");
  281. var t = timeytg.innerHTML.split(" ")[0];
  282. var dn = timeytg.innerHTML.split(" ")[1].slice(0,1);
  283. var ds = timeytg.innerHTML.split(" ")[3];
  284. var loc = timeytg.innerHTML.split(" on ")[1];
  285.  
  286. var score = new Array();
  287. score[oteam] = 0;
  288. score[dteam] = 0;
  289.  
  290. try {
  291. var play = plays[0];
  292. for (var i=0; i<plays.length; i++) {
  293. // console.log(t+" -- "+dn+" -- "+ds+" -- "+loc);
  294. // console.log(plays[i].timeRemaining+" | "+plays[i].down+" | "+plays[i].togo+" | "+plays[i].marker);
  295. if (plays[i].timeRemaining == t) {
  296. if (plays[i].down == dn) {
  297. if (plays[i].togo == ds) {
  298. if (plays[i].marker == loc) {
  299. play = plays[i];
  300. break;
  301. }
  302. }
  303. }
  304. }
  305. score[plays[i].team] += plays[i].score;
  306. }
  307. }
  308. catch (e) {
  309. var dai = document.getElementById("dai");
  310. if (dai == null) {
  311. var cont = document.getElementById("dai_container");
  312. dai = document.createElement("div");
  313. dai.setAttribute("id","dai");
  314. dai.style.textAlign = "left";
  315. dai.innerHTML = "Script not completely loaded yet, try again in a second.";
  316. cont.appendChild(dai);
  317. }
  318. return;
  319. }
  320.  
  321. var oscore = score[oteam];
  322. var dscore = score[dteam];
  323. if (oscore == "-") oscore = 0;
  324. if (dscore == "-") dscore = 0;
  325. var score = Math.abs(dscore - oscore);
  326. var score_neg_pos = (dscore - oscore) / (score || 1);
  327.  
  328. var qtr = play.quarter;
  329. var secs = play.timeRemaining.split(":");
  330. secs = parseInt(secs[0])*60+parseInt(secs[1]);
  331. var spotWho = play.marker.split(" ")[0];
  332. if (spotWho.toLowerCase() == "opp") {
  333. spotWho = "own";
  334. }
  335. else {
  336. spotWho = "their";
  337. }
  338. var spot = play.marker.split(" ")[1];
  339. var down = play.down;
  340. var togo = play.togo;
  341. play.formation = getFormation();
  342. formation = play.formation;
  343.  
  344. var receivers = 2;
  345. if (formation == "Shotgun+5WR") {
  346. formation = "Shotgun";
  347. receivers = 5;
  348. }
  349. else if (formation == "Shotgun") {
  350. receivers = 3;
  351. }
  352. else if (formation.indexOf("Singleback+Spread") != -1) {
  353. receivers = 4;
  354. }
  355. else if (formation.indexOf("Singleback+Big") != -1) {
  356. receivers = 2;
  357. }
  358. else if (formation.indexOf("Singleback") != -1) {
  359. receivers = 3;
  360. }
  361. else if (formation == "I+Big") {
  362. receivers = 1;
  363. }
  364. else if (formation == "I") {
  365. receivers = 2;
  366. }
  367. else if (formation == "Goal+Line") {
  368. receivers = 0;
  369. }
  370.  
  371. var mytime = play.timeoutsRemaining[1];
  372. var opptime = play.timeoutsRemaining[0];
  373. var teamid = document.getElementById("dai_team_select").value;
  374. var daiId = document.getElementById("dai_select").value;
  375. var action = "Test";
  376. var post = "quarter="+qtr;
  377. post += "&time_remaining="+secs;
  378. post += "&spot_who="+spotWho;
  379. post += "&spot="+spot;
  380. post += "&down="+down;
  381. post += "&to_go="+togo;
  382. post += "&score_neg_pos="+score_neg_pos;
  383. post += "&score="+score;
  384. post += "&formation="+formation;
  385. post += "&receivers="+receivers;
  386. post += "&my_timeouts="+mytime;
  387. post += "&opponent_timeouts="+opptime;
  388. post += "&team_id="+teamid;
  389. post += "&id="+daiId;
  390.  
  391. var pos = ["HB","FB","QB","TE"];
  392. for (var i=0; i<pos.length; i++) {
  393. var value = document.getElementById(pos[i]+"_tag_select").value;
  394. if (value != "none") {
  395. post += "&"+pos[i].toLowerCase()+"_flag="+value;
  396. }
  397. }
  398.  
  399. post += "&action="+action;
  400. console.log("("+post.length+") "+post);
  401.  
  402. var dai = document.getElementById("dai");
  403. if (dai == null) {
  404. var cont = document.getElementById("dai_container");
  405. dai = document.createElement("div");
  406. dai.setAttribute("id","dai");
  407. dai.style.textAlign = "left";
  408. cont.appendChild(dai);
  409. }
  410.  
  411. var address = "/game/team_defense_ai_test.pl";
  412. var http = new XMLHttpRequest();
  413. http.open("POST",address,true);
  414.  
  415. http.setRequestHeader('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9) Gecko/2008052912 Firefox/3.0 Greasemonkey');
  416. http.setRequestHeader('Accept', 'text/html');
  417. http.setRequestHeader('Host', 'goallineblitz.com');
  418. http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  419. http.setRequestHeader('Content-length', post.length);
  420. http.setRequestHeader('Connection', 'close');
  421.  
  422. http.onreadystatechange = function() {
  423. if (this.readyState == 4) {
  424. if (this.status != 200) {
  425. alert("pbr gm script: Error "+page.status+" loading "+address);
  426. }
  427. else {
  428. var text = this.responseText;
  429. var idx = text.indexOf('<div class="medium_head">Matching Input</div>');
  430. text = text.slice(idx);
  431. idx = text.indexOf('</div></div>');
  432. text = text.slice(0,idx+'</div></div>'.length);
  433. text = "<div>Warning: automatic tagging & running clocks not implemented.<br>"+
  434. "I have no idea why 'Singleback' is now called 'Singleback Left'.</div>"+text;
  435.  
  436. dai.innerHTML = "<br><b>Formation:</b> "+formation.replace("+"," ")+" ("+receivers+")<br>"+text;
  437. }
  438. }
  439. }
  440. http.send(post);
  441. }
  442.  
  443. function testOAI() {
  444. var oteam = document.getElementsByClassName("secondary_container")[0].childNodes[0].innerHTML.split("&nbsp;")[0];
  445. var dteam = document.getElementsByClassName("secondary_container")[1].childNodes[0].innerHTML.split("&nbsp;")[0];
  446.  
  447. var timeytg = document.getElementById("time_ytg");
  448. var t = timeytg.innerHTML.split(" ")[0];
  449. var dn = timeytg.innerHTML.split(" ")[1].slice(0,1);
  450. var ds = timeytg.innerHTML.split(" ")[3];
  451. var loc = timeytg.innerHTML.split(" on ")[1];
  452.  
  453. var score = new Array();
  454. score[oteam] = 0;
  455. score[dteam] = 0;
  456.  
  457. try {
  458. var play = plays[0];
  459. for (var i=0; i<plays.length; i++) {
  460. // console.log(t+" -- "+dn+" -- "+ds+" -- "+loc);
  461. // console.log(plays[i].timeRemaining+" | "+plays[i].down+" | "+plays[i].togo+" | "+plays[i].marker);
  462. if (plays[i].timeRemaining == t) {
  463. if (plays[i].down == dn) {
  464. if (plays[i].togo == ds) {
  465. if (plays[i].marker == loc) {
  466. play = plays[i];
  467. break;
  468. }
  469. }
  470. }
  471. }
  472. score[plays[i].team] += plays[i].score;
  473. }
  474. } catch (e) {
  475. var dai = document.getElementById("dai");
  476. if (dai == null) {
  477. var cont = document.getElementById("dai_container");
  478. dai = document.createElement("div");
  479. dai.setAttribute("id","dai");
  480. dai.style.textAlign = "left";
  481. dai.innerHTML = "Script not completely loaded yet, try again in a second.";
  482. cont.appendChild(dai);
  483. }
  484. return;
  485. }
  486.  
  487. var oscore = score[oteam];
  488. var dscore = score[dteam];
  489. if (oscore == "-") oscore = 0;
  490. if (dscore == "-") dscore = 0;
  491.  
  492. var score = Math.abs(oscore - dscore);
  493. var score_neg_pos = (oscore - dscore) / (score || 1);
  494.  
  495. var qtr = play.quarter;
  496. var secs = play.timeRemaining.split(":");
  497. secs = parseInt(secs[0])*60+parseInt(secs[1]);
  498.  
  499. var spotWho = play.marker.split(" ")[0];
  500. if (spotWho.toLowerCase() == "opp") {
  501. spotWho = "their";
  502. }
  503. else {
  504. spotWho = "own";
  505. }
  506. var spot = play.marker.split(" ")[1];
  507. var down = play.down;
  508. var togo = play.togo;
  509.  
  510. var mytime = play.timeoutsRemaining[0];
  511. var opptime = play.timeoutsRemaining[1];
  512. var teamid = document.getElementById("oai_team_select").value;
  513. var oaiId = document.getElementById("oai_select").value;
  514. var action = "Test";
  515. var post = "quarter="+qtr;
  516. post += "&time_remaining="+secs;
  517. post += "&spot_who="+spotWho;
  518. post += "&spot="+spot;
  519. post += "&down="+down;
  520. post += "&to_go="+togo;
  521. post += "&score_neg_pos="+score_neg_pos;
  522. post += "&score="+score;
  523. post += "&my_timeouts="+mytime;
  524. post += "&opponent_timeouts="+opptime;
  525. post += "&team_id="+teamid;
  526. post += "&id="+oaiId;
  527. post += "&action="+action;
  528. console.log(post);
  529.  
  530. var address = "/game/team_ai_test.pl";
  531. var http = new XMLHttpRequest();
  532. http.open("POST",address,true);
  533.  
  534. http.setRequestHeader('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9) Gecko/2008052912 Firefox/3.0 Greasemonkey');
  535. http.setRequestHeader('Accept', 'text/html');
  536. if (window.location.toString().indexOf("goallineblitz") != -1) {
  537. http.setRequestHeader('Host', 'goallineblitz.com');
  538. }
  539. else {
  540. http.setRequestHeader('Host', 'glb.warriorgeneral.com');
  541. }
  542. http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  543. http.setRequestHeader('Content-length', post.length);
  544. http.setRequestHeader('Connection', 'close');
  545.  
  546. http.onreadystatechange = function() {
  547. if (this.readyState == 4) {
  548. if (this.status != 200) {
  549. alert("pbr gm script: Error "+page.status+" loading "+address);
  550. }
  551. else {
  552. var text = this.responseText;
  553. var idx = text.indexOf('<div class="medium_head">Matching Input</div>');
  554. text = text.slice(idx);
  555. idx = text.indexOf('<div style="clear');
  556. text = text.slice(0,idx);
  557.  
  558. var dai = document.getElementById("dai");
  559. if (dai == null) {
  560. var cont = document.getElementById("dai_container");
  561. dai = document.createElement("div");
  562. dai.setAttribute("id","dai");
  563. dai.style.textAlign = "left";
  564. cont.appendChild(dai);
  565. }
  566. text = "<div>Warning: tagging & running clocks not implemented.<br>"+
  567. "I have no idea why 'Singleback' is now called 'Singleback Left'.</div>"+text;
  568. dai.innerHTML = text;
  569. }
  570. }
  571. }
  572. http.send(post);
  573. }
  574.  
  575. function getPlayer(pos) {
  576. var output = new Array();
  577. var images = document.getElementById("offense_container").getElementsByTagName("img");
  578. for (var i=0; i<images.length; i++) {
  579. var str = images[i].src;
  580. if (str.indexOf("/"+pos+".gif") != -1) {
  581. while(str.indexOf("/") != -1) {
  582. str = str.slice(str.indexOf("/")+1);
  583. }
  584. var id = images[i].parentNode.id.split("_")[2];
  585. for (var z=0; z<unsafeWindow.play_data[0].length; z++) {
  586. var p = unsafeWindow.play_data[0][z];
  587. if (p.id == id) {
  588. output.push(p);
  589. }
  590. }
  591. }
  592. }
  593. return output;
  594. }
  595.  
  596. function premodTest() {
  597. var xdiff = 115;
  598. var play_data = unsafeWindow.play_data;
  599. var minx = play_data[0][0].x;
  600. var maxx = play_data[0][0].x;
  601. for (var i = 1; i < unsafeWindow.play_data[0].length; i++) {
  602. minx = Math.min(minx, play_data[0][i].x);
  603. maxx = Math.max(maxx, play_data[0][i].x);
  604. }
  605. console.log("premodTest(): "+minx+" --> "+maxx+" : "+((maxx - minx)>xdiff));
  606. if ((maxx-minx) > xdiff) return true;
  607. return false;
  608. }
  609.  
  610. function getFormation() {
  611. var offense = ["QB","HB","FB","TE","LOT","ROT","LG","RG","C","WR1","WR2","WR3","WR4","WR5","K","P"];
  612. var players = new Array();
  613.  
  614. for (var i=0; i<offense.length; i++) {
  615. var p = getPlayer(offense[i]);
  616. if (p != null) {
  617. players[offense[i]] = p;
  618. }
  619. }
  620.  
  621. var shotgunDist = 8;
  622. if (premodTest() == true) shotgunDist = shotgunDist*3;
  623. console.log(premodTest()+": sgdist="+(diff(players["QB"][0].y, players["C"][0].y)));
  624.  
  625. var formation = "Unknown";
  626. try {
  627. if (players["K"].length != 0) {
  628. formation = "Kickoff";
  629. }
  630. else if (players["P"].length != 0) {
  631. formation = "Punt";
  632. }
  633. else if (players["FB"].length == 2) {
  634. formation = "Spike";
  635. }
  636. else if (players["WR5"].length != 0) {
  637. formation = "Shotgun+5WR";
  638. }
  639. else if (players["WR4"].length != 0) {
  640. formation = "Singleback+Spread";
  641. }
  642. else if (players["TE"].length == 3) {
  643. formation = "Goal+Line";
  644. }
  645. else if (diff(players["QB"][0].y, players["C"][0].y) > shotgunDist) {
  646. console.log("sgdist="+(diff(players["QB"][0].y, players["C"][0].y)));
  647. formation = "Shotgun";
  648. }
  649. else if ((diff(players["QB"][0].y, players["HB"][0].y) > shotgunDist) &&
  650. (players["FB"].length == 0)) {
  651. if (players["TE"].length == 1) {
  652. var cnt = 0;
  653. for (var r=0; r<3; r++) {
  654. if (players["WR"+(r+1)][0].x < players["QB"][0].x) cnt++;
  655. else cnt--;
  656. }
  657. if (Math.abs(cnt) == 3) formation = "Singleback+Trips";
  658. else formation = "Singleback+Left";
  659. }
  660. else {
  661. formation = "Singleback+Big";
  662. }
  663. }
  664. else if (diff(players["HB"][0].y, players["FB"][0].y) < 3) {
  665. formation = "Splitbacks+Pro";
  666. }
  667. else if (diff(players["HB"][0].x, players["FB"][0].x) < 3) {
  668. if (players["TE"].length == 2) {
  669. formation = "I+Big";
  670. }
  671. else {
  672. formation = "I";
  673. }
  674. }
  675. else if ((diff(players["QB"][0].y, players["HB"][0].y) > 8) &&
  676. (diff(players["WR1"][0].x, players["HB"][0].x) > diff(players["WR1"][0].x, players["FB"][0].x))) {
  677. formation = "I+Weak";
  678. }
  679. else if ((diff(players["QB"][0].y, players["HB"][0].y) > 8) &&
  680. (diff(players["WR1"][0].x, players["HB"][0].x) < diff(players["WR1"][0].x, players["FB"][0].x))) {
  681. formation = "I+Strong";
  682. }
  683. }
  684. catch (e) {
  685. console.log("AI Testing: "+e);
  686. }
  687. console.log("Formation = "+formation);
  688.  
  689. return formation;
  690. }
  691.  
  692.  
  693. function diff(x, y) {
  694. var a = parseFloat(x);
  695. var b = parseFloat(y);
  696. return Math.max(a,b) - Math.min(a,b);
  697. }