Advertisement
Guest User

Untitled

a guest
Jan 25th, 2015
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.72 KB | None | 0 0
  1. var start_x = 150;
  2. var x_incr, y_incr;
  3. var teamsData;
  4. var teamsDict = {};
  5. var standingsData = [];
  6. var fixturesData;
  7. var fixturesDict = {};
  8. var matchdayCompleted = 22;
  9. var colorsDict = {};
  10. var shortNamesDict = {};
  11. var circleRad = 15;
  12. var cont = 0;
  13. var board;
  14. var drawnMatchesArray = [];
  15. var bettingOddsTable;
  16. var bettingOddsDict = {};
  17. var shortNamesArray = [];
  18.  
  19. var selectMenu;
  20.  
  21.  
  22.  
  23. function preload() {
  24. var teamsURL = "data/teams.json";
  25. teamsData = loadJSON(teamsURL);
  26.  
  27. for (i = 1; i <= matchdayCompleted; i++)
  28. {
  29. var standingsURL = "data/standings/matchday" + i + ".json";
  30. standingsData.push(loadJSON(standingsURL));
  31. }
  32.  
  33. var fixturesURL = "data/fixtures.json";
  34. fixturesData = loadJSON(fixturesURL);
  35.  
  36. var bettingOddsURL = "data/odds.csv"
  37. bettingOddsTable = loadTable('data/match_betting_odds.csv','header');
  38.  
  39. var colorsURL = "data/team_colors.json";
  40. colorsDict = loadJSON(colorsURL);
  41. var shortNamesURL = "data/team_short_names.json";
  42. shortNamesDict = loadJSON(shortNamesURL);
  43.  
  44. }
  45.  
  46. function setup() {
  47. createCanvas(3000,1000);
  48. board = createGraphics(3000,1000);
  49. board.background(255,255,255);
  50. y_incr = floor(height/21);
  51. board.stroke(0,0,0,100);
  52. for (i = 0; i < height; i = i + y_incr) {
  53. board.line(0,i,width,i);
  54. }
  55. x_incr = (width - start_x)/40;
  56. for (i = start_x; i < width; i = i + x_incr) {
  57. board.line(i,0,i,height);
  58. }
  59.  
  60. for (i = 0; i < 39; i++) {
  61. var x_coor = start_x + x_incr/4 + i * x_incr;
  62. fill(0);
  63. textStyle(NORMAL);
  64. noStroke();
  65. textSize(20);
  66. text(""+i, x_coor, 2 * y_incr/3);
  67. }
  68.  
  69.  
  70. selectMenu = getElement('menu');
  71.  
  72. // load all teams
  73. for (i = 0; i<teamsData['count']; i++){
  74. var teamURL = teamsData['teams'][i]["_links"]["self"]["href"];
  75. var teamName = teamsData['teams'][i]["name"];
  76. var teamShortName = teamsData['teams'][i]["shortName"]
  77. teamsDict[teamName] = new Team(teamURL, teamName, teamShortName);
  78. shortNamesArray.push(shortNamesDict[teamName]);
  79. }
  80. shortNamesArray.sort();
  81. for (i in shortNamesDict){
  82. for (x in shortNamesArray){
  83. if (shortNamesArray[x] == shortNamesDict[i]) {
  84. var tempX = x;
  85. teamsDict[i].updateStanding(0,++tempX);
  86. var R = colorsDict[i][0];
  87. var G = colorsDict[i][1];
  88. var B = colorsDict[i][2];
  89. var y_coor = 1.7 * y_incr + x * y_incr;
  90. board.fill(R,G,B);
  91. board.textStyle(NORMAL);
  92. board.noStroke();
  93. board.textSize(22);
  94. var teamName = shortNamesArray[x];
  95. board.text(teamName, 4, y_coor);
  96. }
  97. }
  98. }
  99. for (i = 1; i<21; i++){
  100. board.fill(0);
  101. var y_coor = 1 * i * y_incr + 32;
  102. board.text(i + "", 170, y_coor);
  103. }
  104.  
  105.  
  106. // load the standings of all matchdays
  107. for (i = 0; i<standingsData.length; i++){
  108. var matchdayStandings = standingsData[i].standing;
  109. var matchday = standingsData[i].matchday;
  110. for (j = 0; j<matchdayStandings.length; j++){
  111. var tempTeamName = matchdayStandings[j]["teamName"];
  112. var tempTeam = teamsDict[tempTeamName];
  113. tempTeam.updateStanding(matchday,matchdayStandings[j]["position"])
  114. }
  115. }
  116.  
  117. // load bettings odds
  118. for (i = 1; i < bettingOddsTable.rows.length; i++){
  119. var home = bettingOddsTable.rows[i].get(0);
  120. var away = bettingOddsTable.rows[i].get(1);
  121. bettingOddsDict[[home,away]] = [bettingOddsTable.rows[i].get(3),
  122. bettingOddsTable.rows[i].get(4),bettingOddsTable.rows[i].get(5)]
  123. }
  124.  
  125. // load all fixtures
  126. for (i in fixturesData.fixtures){
  127. var x = fixturesData.fixtures[i];
  128. if (x.matchday <= matchdayCompleted){
  129. var tempMatchID = x._links.self.href.slice(-6);
  130. fixturesDict[tempMatchID] = new Fixture(
  131. tempMatchID, x._links.self.href, x.date, x.matchday,
  132. x.homeTeamName, x.awayTeamName,
  133. x.result.goalsHomeTeam, x.result.goalsAwayTeam);
  134. var tempHomeTeam = teamsDict[x.homeTeamName];
  135. tempHomeTeam.addMatch(x.matchday, tempMatchID);
  136. var tempAwayTeam = teamsDict[x.awayTeamName];
  137. tempAwayTeam.addMatch(x.matchday, tempMatchID);
  138. var homeTeam = shortNamesDict[x.homeTeamName];
  139. var awayTeam = shortNamesDict[x.awayTeamName];
  140. var tempOddsArray = bettingOddsDict[[homeTeam, awayTeam]];
  141. fixturesDict[tempMatchID].addOdds(tempOddsArray[0],tempOddsArray[1],tempOddsArray[2]);
  142. }
  143. }
  144.  
  145. var selected = selectMenu.elt.value;
  146. for (x in teamsDict){
  147. var tempTeam = teamsDict[x];
  148. var R = colorsDict[x][0];
  149. var G = colorsDict[x][1];
  150. var B = colorsDict[x][2];
  151. if (selected == "All") var A = 255;
  152. else A = 50;
  153. if (x == "Manchester United FC") A = 255;
  154. board.noFill();
  155. board.stroke(R,G,B,A);
  156. board.strokeWeight(5.0);
  157. board.strokeJoin(ROUND);
  158. board.beginShape();
  159. startingPosition = true;
  160. for (y in tempTeam.standings){
  161. if (startingPosition){
  162. startingPosition = false;
  163. continue;
  164. }
  165. var x_coor = start_x + x_incr/2 + y * x_incr;
  166. var y_coor = tempTeam.standings[y] * y_incr + y_incr/2;
  167. board.vertex(x_coor,y_coor) ;
  168.  
  169. }
  170. board.endShape();
  171. board.noStroke();
  172. startingPosition = true;
  173. for (y in tempTeam.standings){
  174. var x_coor = start_x + x_incr/2 + y * x_incr;
  175. var y_coor = tempTeam.standings[y] * y_incr + y_incr/2;
  176. var tempFixtureID = tempTeam.fixtures[y];
  177. if (startingPosition){
  178. startingPosition = false;
  179. continue;
  180. }
  181. board.fill(R,G,B,A );
  182. if (fixturesDict[tempFixtureID].imp) {
  183. board.rectMode(CENTER);
  184. board.rect(x_coor,y_coor,circleRad+10,circleRad+10);
  185. drawnMatchesArray.push([x_coor,y_coor,tempFixtureID]);
  186. } else {
  187. board.ellipse(x_coor,y_coor,circleRad,circleRad);
  188. drawnMatchesArray.push([x_coor,y_coor,tempFixtureID]);
  189. }
  190. }
  191. }
  192. }
  193.  
  194. function optionSelected(){
  195. var tempBoard;
  196. tempBoard = createGraphics(3000,1000);
  197. tempBoard.background(255,255,255);
  198. tempBoard.stroke(0,0,0,100);
  199. for (i = 0; i < height; i = i + y_incr) {
  200. tempBoard.line(0,i,width,i);
  201. }
  202. for (i = start_x; i < width; i = i + x_incr) {
  203. tempBoard.line(i,0,i,height);
  204. }
  205.  
  206. var selected = selectMenu.elt.value;
  207.  
  208. for (x in teamsDict){
  209. var tempTeam = teamsDict[x];
  210. var R = colorsDict[x][0];
  211. var G = colorsDict[x][1];
  212. var B = colorsDict[x][2];
  213. var A = 30;
  214. if (selected == "All") A = 255;
  215. if (x == selected) A = 255;
  216. tempBoard.noFill();
  217. tempBoard.stroke(R,G,B,A);
  218. tempBoard.strokeWeight(5.0);
  219. tempBoard.strokeJoin(ROUND);
  220. tempBoard.beginShape();
  221. startingPosition = true;
  222. for (y in tempTeam.standings){
  223. if (startingPosition){
  224. startingPosition = false;
  225. continue;
  226. }
  227. var x_coor = start_x + x_incr/2 + y * x_incr;
  228. var y_coor = tempTeam.standings[y] * y_incr + y_incr/2;
  229. tempBoard.vertex(x_coor,y_coor) ;
  230.  
  231. }
  232. tempBoard.endShape();
  233. tempBoard.noStroke();
  234. startingPosition = true;
  235. for (y in tempTeam.standings){
  236. var x_coor = start_x + x_incr/2 + y * x_incr;
  237. var y_coor = tempTeam.standings[y] * y_incr + y_incr/2;
  238. var tempFixtureID = tempTeam.fixtures[y];
  239. if (startingPosition){
  240. startingPosition = false;
  241. continue;
  242. }
  243. tempBoard.fill(R,G,B,A );
  244. if (fixturesDict[tempFixtureID].imp) {
  245. tempBoard.rectMode(CENTER);
  246. tempBoard.rect(x_coor,y_coor,circleRad+10,circleRad+10);
  247. } else {
  248. tempBoard.ellipse(x_coor,y_coor,circleRad,circleRad);
  249. }
  250. }
  251. }
  252. for (i in shortNamesDict){
  253. for (x in shortNamesArray){
  254. if (shortNamesArray[x] == shortNamesDict[i]) {
  255. var tempX = x;
  256. teamsDict[i].updateStanding(0,++tempX);
  257. var R = colorsDict[i][0];
  258. var G = colorsDict[i][1];
  259. var B = colorsDict[i][2];
  260. var A = 30;
  261. if (selected == "All") A = 255;
  262. if (i == selected) A = 255;
  263. var y_coor = 1.7 * y_incr + x * y_incr;
  264. tempBoard.fill(R,G,B,A);
  265. tempBoard.textStyle(NORMAL);
  266. tempBoard.noStroke();
  267. tempBoard.textSize(22);
  268. var teamName = shortNamesArray[x];
  269. tempBoard.text(teamName, 4, y_coor);
  270. }
  271. }
  272. }
  273. for (i = 1; i<21; i++){
  274. tempBoard.fill(0);
  275. var y_coor = 1 * i * y_incr + 32;
  276. tempBoard.text(i + "", 170, y_coor);
  277. }
  278.  
  279. board = tempBoard;
  280. }
  281.  
  282. function draw(){
  283. background(255,255,255);
  284. image(board,0,0);
  285. var expandedMatches = [];
  286.  
  287. textSize(22);
  288. text("Color Key", 25, 33);
  289.  
  290. textSize(15);
  291. text("Matchday", 153, 19);
  292. text(" /Position", 152, 39);
  293.  
  294. for (i = 1; i < 39; i++) {
  295. if (i<10) var x_coor = start_x + .42*x_incr + i * x_incr;
  296. else var x_coor = start_x + x_incr/3 + i * x_incr;
  297. fill(0);
  298. textStyle(NORMAL);
  299. noStroke();
  300. textSize(20);
  301. text(""+i, x_coor, 2 * y_incr/3);
  302. }
  303.  
  304. for (i = 0; i<drawnMatchesArray.length;i++) {
  305. var d = dist(mouseX, mouseY, drawnMatchesArray[i][0], drawnMatchesArray[i][1]);
  306. if (d < circleRad) {
  307. expandedMatches.push([drawnMatchesArray[i][0], drawnMatchesArray[i][1],drawnMatchesArray[i][2]]);
  308. }
  309. }
  310.  
  311. var rectDrawn = false;
  312. for (i = 0; i<expandedMatches.length;i++){
  313. var matchDetails = createGraphics(500,500);
  314. var tempFixture = fixturesDict[expandedMatches[i][2]];
  315. var homeTeam = shortNamesDict[tempFixture.homeTeamName];
  316. var awayTeam = shortNamesDict[tempFixture.awayTeamName];
  317. var tempText = homeTeam + " " + tempFixture.goalsHomeTeam + " - " + tempFixture.goalsAwayTeam + " " + awayTeam;
  318. rectMode(CORNER);
  319. fill(180,180,180,200);
  320. var x_coor = expandedMatches[i][0];
  321. var y_coor = expandedMatches[i][1];
  322. if (!rectDrawn){
  323. rectDrawn = true;
  324. rect(x_coor,y_coor,420,75*expandedMatches.length);
  325. }
  326. fill(0);
  327. textStyle(NORMAL);
  328. noStroke();
  329. textSize(30);
  330. text(tempText,x_coor + 15,expandedMatches[i][1] + 30 + i * 75)
  331. text("Date: " + tempFixture.date,x_coor + 15,expandedMatches[i][1]+60+ i * 75)
  332. }
  333.  
  334. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement