Refactor and clean up code

This commit is contained in:
2020-09-27 23:26:41 +02:00
parent 283efc775b
commit a484010285
20 changed files with 349 additions and 373 deletions

View File

@@ -0,0 +1,101 @@
package de.jeyp91.tippliga;
import com.google.common.io.Resources;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
public class TLWMatchesCreatorTipperLeague extends TLWMatchesCreatorBase {
int season;
int league;
JSONArray matchPairingConfig;
JSONObject tipperList;
JSONArray tipperTeamConfig;
ArrayList<TLWMatchday> matchdays;
public TLWMatchesCreatorTipperLeague(int season, int league, String configFileName, ArrayList<TLWMatchday> matchdays) {
this.season = season;
this.league = league;
this.matchdays = matchdays;
URL matchPairConfigUrl = Resources.getResource("Tipper_Match_Pair_Config.json");
URL tipperListUrl = Resources.getResource(season + "\\" + configFileName);
URL tipperTeamConfigUrl = Resources.getResource("Tipper_Team_Config.json");
try {
JSONParser jsonParser = new JSONParser();
String matchPairingConfigString = Resources.toString(matchPairConfigUrl, StandardCharsets.UTF_8);
this.matchPairingConfig = (JSONArray) jsonParser.parse(matchPairingConfigString);
String tipperListString = Resources.toString(tipperListUrl, StandardCharsets.UTF_8);
this.tipperList = (JSONObject) jsonParser.parse(tipperListString);
String tipperTeamConfigString = Resources.toString(tipperTeamConfigUrl, StandardCharsets.UTF_8);
this.tipperTeamConfig = (JSONArray) jsonParser.parse(tipperTeamConfigString);
} catch (IOException | ParseException e) {
e.printStackTrace();
}
this.publicateMatchObjects();
}
private void publicateMatchObjects() {
for(Object matchdayConfig : this.matchPairingConfig) {
int matchday = ((Long) ((JSONObject) matchdayConfig).get("matchday")).intValue();
JSONArray matchesConfig = (JSONArray)((JSONObject) matchdayConfig).get("matches");
for(int i = 0; i < matchesConfig.size(); i++) {
int homeTipperNumber = ((Long) ((JSONObject) matchesConfig.get(i)).get("home")).intValue();
int guestTipperNumber = ((Long) ((JSONObject) matchesConfig.get(i)).get("guest")).intValue();
String homeName = this.tipperList.get(String.valueOf(homeTipperNumber)).toString();
String guestName = this.tipperList.get(String.valueOf(guestTipperNumber)).toString();
int teamIdHome = getTeamIdFromTipperName(homeName);
int teamIdGuest = getTeamIdFromTipperName(guestName);
int matchNo = (matchday - 1) * matchesConfig.size() + i + 1;
String matchDatetime = getDeliveryDateForMatchday(matchday);
TLWMatch tlwMatch = new TLWMatch(this.season, this.league, matchday, matchNo, teamIdHome, teamIdGuest, matchDatetime);
this.TLWMatches.add(tlwMatch);
}
}
}
private int getTeamIdFromTipperName(String name) {
int teamId = 0;
for(Object config : tipperTeamConfig) {
if (((JSONObject) config).get("team_name").toString().equals(name)) {
teamId = ((Long) (((JSONObject) config).get("team_id"))).intValue();
}
}
if(teamId == 0) {
System.out.println("ERROR! Did not find Tipper ID for " + name);
}
return teamId;
}
private String getDeliveryDateForMatchday(int matchday) {
String deliveryDate = "";
for (TLWMatchday matchdayObject : this.matchdays) {
if(matchdayObject.getMatchday() == matchday) {
deliveryDate = matchdayObject.getDeliveryDate();
}
}
return deliveryDate;
}
}