Remove Gist
This commit is contained in:
74
src/main/java/de/jeyp91/teamidmatcher/TeamIDMatcher.java
Normal file
74
src/main/java/de/jeyp91/teamidmatcher/TeamIDMatcher.java
Normal file
@@ -0,0 +1,74 @@
|
||||
package de.jeyp91.teamidmatcher;
|
||||
|
||||
import com.google.common.collect.HashBiMap;
|
||||
import de.jeyp91.ResourceProvider;
|
||||
import de.jeyp91.StatusHolder;
|
||||
import de.jeyp91.apifootball.APIFootballMatch;
|
||||
import org.apache.logging.log4j.*;
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
public class TeamIDMatcher {
|
||||
|
||||
private static HashBiMap<Integer, Integer> ids = null;
|
||||
private static final Logger logger = LogManager.getLogger(TeamIDMatcher.class);
|
||||
private static JSONArray teams;
|
||||
private static boolean init = false;
|
||||
public static final int HOME = 0;
|
||||
public static final int GUEST = 1;
|
||||
|
||||
private static void initBiMap() {
|
||||
if(!init) {
|
||||
ids = HashBiMap.create();
|
||||
|
||||
teams = ResourceProvider.getTeamIDMatcherConfig();
|
||||
|
||||
for (Object team : teams) {
|
||||
int tippligaID = ((Long) ((JSONObject) team).get("tippligaID")).intValue();
|
||||
int apiFootballID = ((Long) ((JSONObject) team).get("apiFootballID")).intValue();
|
||||
ids.put(tippligaID, apiFootballID);
|
||||
}
|
||||
|
||||
init = true;
|
||||
}
|
||||
}
|
||||
|
||||
public static Integer getApiFootballIdFromTippligaId(int id) {
|
||||
initBiMap();
|
||||
if(ids.containsKey(id)) {
|
||||
return ids.get(id);
|
||||
} else {
|
||||
logger.error("Tippliga ID: " + id + " not in ID Matcher.");
|
||||
StatusHolder.setError();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Integer getTippligaIdFromApiFootballId(APIFootballMatch match, int homeguest) {
|
||||
Integer apiFootballId = homeguest == HOME ? match.getTeamIdHome() : match.getTeamIdGuest();
|
||||
Integer tlwId = null;
|
||||
if(apiFootballId == null) return null;
|
||||
initBiMap();
|
||||
if(ids.inverse().containsKey(apiFootballId)) {
|
||||
tlwId = ids.inverse().get(apiFootballId);
|
||||
} else {
|
||||
String teamname = homeguest == HOME ? match.getTeamNameHome() : match.getTeamNameGuest();
|
||||
logger.error("Could not find id " + apiFootballId + " for " + teamname);
|
||||
StatusHolder.setError();
|
||||
}
|
||||
return tlwId;
|
||||
}
|
||||
|
||||
public static String getTeamNameFromTippligaId(Integer id) {
|
||||
initBiMap();
|
||||
String name = "";
|
||||
for(Object team : teams) {
|
||||
int tippligaID = ((Long)((JSONObject) team).get("tippligaID")).intValue();
|
||||
if(id == tippligaID) {
|
||||
name = (((JSONObject) team).get("teamname")).toString();
|
||||
break;
|
||||
}
|
||||
}
|
||||
return name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package de.jeyp91.teamidmatcher;
|
||||
|
||||
import de.jeyp91.apifootball.APIFootballConnector;
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
public class TeamIDMatcherTemplateCreator {
|
||||
APIFootballConnector con;
|
||||
JSONArray teams;
|
||||
|
||||
public TeamIDMatcherTemplateCreator(int season, int league) {
|
||||
this.con = APIFootballConnector.getAPIFootballConnectorInstance(season);
|
||||
JSONObject apiObject = con.getTeamsForLeague(league);
|
||||
JSONObject teamsObject = (JSONObject) apiObject.get("api");
|
||||
this.teams = (JSONArray) teamsObject.get("teams");
|
||||
}
|
||||
|
||||
public String getTeamsTemplate() {
|
||||
String teamsTemplate = "";
|
||||
for (Object team : this.teams) {
|
||||
JSONObject teamObject = (JSONObject) team;
|
||||
teamsTemplate += ",\n";
|
||||
teamsTemplate += " {\n";
|
||||
teamsTemplate += " \"teamname\": \"" + teamObject.get("name") + "\",\n";
|
||||
teamsTemplate += " \"tippligaID\": " + ",\n";
|
||||
teamsTemplate += " \"apiFootballID\": " + teamObject.get("team_id") + "\n";
|
||||
teamsTemplate += " }";
|
||||
}
|
||||
return teamsTemplate;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user