1 changed files with 89 additions and 0 deletions
@ -0,0 +1,89 @@ |
|||
package app.updates; |
|||
|
|||
import app.entities.Stats; |
|||
import app.entities.server.Server; |
|||
import jakarta.annotation.PostConstruct; |
|||
import org.slf4j.Logger; |
|||
import org.slf4j.LoggerFactory; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.beans.factory.annotation.Value; |
|||
import org.springframework.stereotype.Component; |
|||
import org.springframework.web.client.RestClientException; |
|||
import org.springframework.web.client.RestTemplate; |
|||
|
|||
import java.util.HashMap; |
|||
|
|||
@Component |
|||
public class PreviewUpdater extends BaseUpdater{ |
|||
Stats stats; |
|||
RestTemplate restTemplate; |
|||
private final Logger logger = LoggerFactory.getLogger(PreviewUpdater.class); |
|||
//ТУТ НАДО ЧТОБ ЗНАЧЕНИЕ ИЗ ЕНВ БРАЛСЬ
|
|||
private String domain = "tf2.pblr-nyk.pro"; |
|||
private String preview = "https://%s/site_content/images/preview/%s.%s"; |
|||
|
|||
@Value("${backend.updates.a2s}") |
|||
private boolean update = false; |
|||
private HashMap<String, String> preview_cache; |
|||
|
|||
@Autowired |
|||
public PreviewUpdater(Stats stats) { |
|||
this.stats = stats; |
|||
restTemplate = new RestTemplate(); |
|||
preview_cache = new HashMap(); |
|||
} |
|||
|
|||
@PostConstruct |
|||
public void updateValues() { |
|||
if (update) { |
|||
logger.warn("Preview loader enabled"); |
|||
stats.getServers().forEach((server_name, server) -> { |
|||
CreateTaskUpdater(() -> { |
|||
loadPreview(stats.getServers().get(server_name)); |
|||
return null; |
|||
}, 30000); |
|||
}); |
|||
} |
|||
} |
|||
|
|||
public void loadPreview(Server server) { |
|||
if (server.getMap() == null || server.getMap().isEmpty()) { |
|||
logger.warn("{} not have map, set default", server.getName()); |
|||
server.setPreview(not_found()); |
|||
return; |
|||
} |
|||
|
|||
if (preview_cache.containsKey(server.getMap())) { |
|||
if (preview_cache.get(server.getMap()).equals(server.getPreview())) return; |
|||
} |
|||
|
|||
String url = getPreview(server.getMap()); |
|||
if (url != null) { |
|||
server.setPreview(url); |
|||
preview_cache.put(server.getMap(), server.getPreview()); |
|||
} else { |
|||
logger.warn("Not found preview for {}", server.getMap()); |
|||
server.setPreview(not_found()); |
|||
} |
|||
} |
|||
|
|||
public String getPreview(String map) { |
|||
String url; |
|||
//Check jpg
|
|||
try { |
|||
url = preview.formatted(domain, map, "jpg"); |
|||
restTemplate.headForHeaders(url); |
|||
return url; |
|||
} catch (RestClientException err) {} |
|||
//Check txt contained url
|
|||
try { |
|||
url = preview.formatted(domain, map, "url"); |
|||
return restTemplate.getForEntity(url, String.class).getBody().replace("\n", ""); |
|||
} catch (RestClientException err) {} |
|||
return null; |
|||
} |
|||
|
|||
public String not_found() { |
|||
return preview.formatted(domain, "_default", "jpg"); |
|||
} |
|||
} |
Loading…
Reference in new issue