Skip to content

Commit

Permalink
ho-dev#2063 download missing world details
Browse files Browse the repository at this point in the history
  • Loading branch information
wsbrenk committed May 20, 2024
1 parent a551ca4 commit c02ca30
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 11 deletions.
5 changes: 2 additions & 3 deletions src/main/java/core/db/DBManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -1560,7 +1560,6 @@ public void setTransferType(TransferType type) {
*
* @return the world detail league [ ]
*/
// WorldDetail
public List<WorldDetailLeague> getAllWorldDetailLeagues() {
return ((WorldDetailsTable) getTable(WorldDetailsTable.TABLENAME))
.getAllWorldDetailLeagues();
Expand All @@ -1571,11 +1570,11 @@ public List<WorldDetailLeague> getAllWorldDetailLeagues() {
*
* @param leagues the leagues
*/
public void saveWorldDetailLeagues(List<WorldDetailLeague> leagues) {
public void storeWorldDetailLeagues(List<WorldDetailLeague> leagues) {
WorldDetailsTable table = (WorldDetailsTable) getTable(WorldDetailsTable.TABLENAME);
table.truncateTable();
for (WorldDetailLeague league : leagues) {
table.insertWorldDetailsLeague(league);
table.storeWorldDetailsLeague(league);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/core/db/WorldDetailsTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ protected String createSelectStatement() {
return createSelectStatement("");
}

void insertWorldDetailsLeague(WorldDetailLeague league){
void storeWorldDetailsLeague(WorldDetailLeague league){
if(league == null)
return;
store(league);
Expand All @@ -44,7 +44,7 @@ List<WorldDetailLeague> getAllWorldDetailLeagues(){
@Override
protected void insertDefaultValues(){
for ( var league : WorldDetailLeague.allLeagues){
insertWorldDetailsLeague(league);
storeWorldDetailsLeague(league);
}
}
}
42 changes: 39 additions & 3 deletions src/main/java/core/model/WorldDetailsManager.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package core.model;

import core.db.DBManager;
import core.file.xml.XMLManager;
import core.file.xml.XMLWorldDetailsParser;
import core.net.MyConnector;
import core.util.HOLogger;

import java.util.HashMap;
import java.util.List;
import java.util.*;

public class WorldDetailsManager {

Expand Down Expand Up @@ -48,12 +51,45 @@ public WorldDetailLeague getWorldDetailLeagueByLeagueId(Integer leagueId) {
return leagueMap.get(leagueId);
}

/**
* Get world detail information of country id.
* If cache is empty, it will be initialized by the details stored in the database.
* If this information does not contain the requested country id, the world details will be downloaded
* from hattrick. The downloaded object will be added to the cache and the database.
* @param countryId Country Id
* @return WorldDetailLeague
*/
public WorldDetailLeague getWorldDetailLeagueByCountryId(Integer countryId) {
if (countryMap.isEmpty()) {
initialize();
}
var ret = countryMap.get(countryId);
if (ret == null) {
ret = downloadWorldDetailLeague(countryId);
countryMap.put(countryId, ret);
DBManager.instance().storeWorldDetailLeagues(Collections.singletonList(ret));
}
return ret;
}

return countryMap.get(countryId);
/**
* Download missing world detail information
* @param countryId Country Id
* @return WorldDetailLeague
*/
private WorldDetailLeague downloadWorldDetailLeague(Integer countryId) {
WorldDetailLeague ret = null;
try {
var worldDetails = MyConnector.instance().getWorldDetailsByCountryId(countryId);
var leagues = XMLWorldDetailsParser.parseDetails(XMLManager.parseString(worldDetails));
if (!leagues.isEmpty()) {
ret = leagues.get(0);
}
}
catch (Exception e) {
HOLogger.instance().warning(getClass(), "Error downloading world details from " + countryId);
}
return ret;
}

public final List<WorldDetailLeague> getLeagues() {
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/core/net/MyConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,11 @@ public String getWorldDetails(int leagueId) throws IOException {
return getCHPPWebFile(url);
}

public String getWorldDetailsByCountryId(int countryId) throws IOException {
String url = htUrl + "?file=worlddetails&version=1.9&countryID=" + countryId;
return getCHPPWebFile(url);
}

// ///////////////////////////////////////////////////////////////////////////////
// Update Checker
// //////////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/module/ifa/RightPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public void actionPerformed(ActionEvent arg0) {
worldDetails = MyConnector.instance().getWorldDetails(0);
List<WorldDetailLeague> leagues = XMLWorldDetailsParser.parseDetails(XMLManager
.parseString(worldDetails));
DBManager.instance().saveWorldDetailLeagues(leagues);
DBManager.instance().storeWorldDetailLeagues(leagues);
WorldDetailsManager.instance().refresh();
} catch (IOException e1) {
e1.printStackTrace();
Expand Down
5 changes: 3 additions & 2 deletions src/main/resources/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@

### Squad
* Fix length of owner notes in players' database table (#1816)
* player avatar image can be reloaded (#1815)
* Player avatar image can be reloaded (#1815)
* Fix error player download nickname null pointer exception (#1938)
* Fix initial sorting by player group (#1909)
* Show stamina sub skill (#383)
* fix error on player details display after initial download (#2044)
* Fix error on player details display after initial download (#2044)
* Update missing world details in database (#2063)

### Team Analyzer
* Restore size of match prediction dialog box (#1898)
Expand Down

0 comments on commit c02ca30

Please sign in to comment.