Getting rid of the MtG stuff in the project

This commit is contained in:
marcin.sciesinski
2021-02-18 21:32:15 -08:00
parent 81fb37cc67
commit bf129ce387
14 changed files with 0 additions and 544 deletions

View File

@@ -1,72 +0,0 @@
package com.gempukku.lotro.async.handler;
import com.gempukku.lotro.async.HttpProcessingException;
import com.gempukku.lotro.async.ResponseWriter;
import com.gempukku.mtg.MtgCardServer;
import com.gempukku.mtg.ProviderNotFoundException;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.QueryStringDecoder;
import io.netty.util.AsciiString;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MtgCardsRequestHandler implements UriRequestHandler {
private MtgCardServer _mtgCardServer;
public MtgCardsRequestHandler(Map<Type, Object> context) {
_mtgCardServer = (MtgCardServer) context.get(MtgCardServer.class);
}
@Override
public void handleRequest(String uri, HttpRequest request, Map<Type, Object> context, ResponseWriter responseWriter, String remoteIp) throws Exception {
QueryStringDecoder queryDecoder = new QueryStringDecoder(request.getUri());
String provider = getQueryParameterSafely(queryDecoder, "provider");
String updateMarker = getQueryParameterSafely(queryDecoder, "update");
if (provider != null) {
processCardListRequest(responseWriter, provider, updateMarker);
} else {
processProviderListRequest(responseWriter);
}
}
private void processProviderListRequest(ResponseWriter responseWriter) {
byte[] dataProvidersResponse = _mtgCardServer.getDataProvidersResponse();
Map<AsciiString, String> headers = new HashMap<AsciiString, String>();
headers.put(HttpHeaderNames.CONTENT_TYPE, "application/json; charset=UTF-8");
responseWriter.writeByteResponse(dataProvidersResponse, headers);
}
private void processCardListRequest(ResponseWriter responseWriter, String provider, String updateMarker) throws Exception {
try {
MtgCardServer.CardDatabaseHolder cardDatabaseHolder = _mtgCardServer.getCardDatabaseHolder(provider);
if (cardDatabaseHolder == null)
throw new HttpProcessingException(204);
else if (updateMarker != null && updateMarker.equals(String.valueOf(cardDatabaseHolder.getUpdateDate())))
throw new HttpProcessingException(304);
Map<AsciiString, String> headers = new HashMap<AsciiString, String>();
headers.put(HttpHeaderNames.CONTENT_TYPE, "application/json; charset=UTF-8");
headers.put(HttpHeaderNames.ETAG, String.valueOf(cardDatabaseHolder.getUpdateDate()));
responseWriter.writeByteResponse(cardDatabaseHolder.getBytes(), headers);
} catch (ProviderNotFoundException exp) {
throw new HttpProcessingException(404);
}
}
protected String getQueryParameterSafely(QueryStringDecoder queryStringDecoder, String parameterName) {
List<String> parameterValues = queryStringDecoder.parameters().get(parameterName);
if (parameterValues != null && parameterValues.size() > 0)
return parameterValues.get(0);
else
return null;
}
}

View File

@@ -32,7 +32,6 @@ public class RootUriRequestHandler implements UriRequestHandler {
private ServerStatsRequestHandler _serverStatsRequestHandler;
private PlayerStatsRequestHandler _playerStatsRequestHandler;
private TournamentRequestHandler _tournamentRequestHandler;
private MtgCardsRequestHandler _mtgCardsRequestHandler;
private SoloDraftRequestHandler _soloDraftRequestHandler;
private Pattern originPattern;
@@ -58,7 +57,6 @@ public class RootUriRequestHandler implements UriRequestHandler {
_serverStatsRequestHandler = new ServerStatsRequestHandler(context);
_playerStatsRequestHandler = new PlayerStatsRequestHandler(context);
_tournamentRequestHandler = new TournamentRequestHandler(context);
_mtgCardsRequestHandler = new MtgCardsRequestHandler(context);
_soloDraftRequestHandler = new SoloDraftRequestHandler(context);
}
@@ -66,8 +64,6 @@ public class RootUriRequestHandler implements UriRequestHandler {
public void handleRequest(String uri, HttpRequest request, Map<Type, Object> context, ResponseWriter responseWriter, String remoteIp) throws Exception {
if (uri.startsWith(_webContextPath)) {
_webRequestHandler.handleRequest(uri.substring(_webContextPath.length()), request, context, responseWriter, remoteIp);
} else if (uri.equals("/mtg-cards/database.json")) {
_mtgCardsRequestHandler.handleRequest(uri, request, context, responseWriter, remoteIp);
} else if (uri.equals("/gemp-lotr")) {
responseWriter.writeError(301, Collections.singletonMap("Location", "/gemp-lotr/"));
} else if (uri.equals(_serverContextPath)) {

View File

@@ -16,7 +16,6 @@ import com.gempukku.lotro.packs.PacksStorage;
import com.gempukku.lotro.service.AdminService;
import com.gempukku.lotro.service.LoggedUserHolder;
import com.gempukku.lotro.tournament.*;
import com.gempukku.mtg.MtgCardServer;
import java.lang.reflect.Type;
import java.util.Map;
@@ -114,8 +113,6 @@ public class ServerBuilder {
pairingMechanismRegistry,
extract(objectMap, CardSets.class)
));
objectMap.put(MtgCardServer.class, new MtgCardServer());
}
private static <T> T extract(Map<Type, Object> objectMap, Class<T> clazz) {
@@ -126,21 +123,12 @@ public class ServerBuilder {
}
public static void constructObjects(Map<Type, Object> objectMap) {
if (isMtgEnabled())
extract(objectMap, MtgCardServer.class).startServer();
extract(objectMap, HallServer.class).startServer();
extract(objectMap, LotroServer.class).startServer();
extract(objectMap, ChatServer.class).startServer();
}
private static boolean isMtgEnabled() {
String mtgEnabled = ApplicationConfiguration.getProperty("mtg.enabled");
return mtgEnabled != null && mtgEnabled.equals("true");
}
public static void destroyObjects(Map<Type, Object> objectMap) {
if (isMtgEnabled())
extract(objectMap, MtgCardServer.class).stopServer();
extract(objectMap, HallServer.class).stopServer();
extract(objectMap, LotroServer.class).stopServer();
extract(objectMap, ChatServer.class).stopServer();

View File

@@ -1,31 +0,0 @@
package com.gempukku.mtg;
public class CardData {
private final String _id;
private final String _name;
private final int _price;
private final String _link;
public CardData(String id, String name, int price, String link) {
_name = name;
_id = id;
_link = link;
_price = price;
}
public String getId() {
return _id;
}
public String getName() {
return _name;
}
public int getPrice() {
return _price;
}
public String getLink() {
return _link;
}
}

View File

@@ -1,105 +0,0 @@
package com.gempukku.mtg;
import com.gempukku.lotro.AbstractServer;
import com.gempukku.mtg.provider.mtggoldfish.MtgGoldfishProvider;
import org.json.simple.JSONArray;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class MtgCardServer extends AbstractServer {
private Map<String, MtgDataProvider> _dataProviders = new HashMap<String, MtgDataProvider>();
private Map<String, CardDatabaseHolder> _providersData = Collections.synchronizedMap(new HashMap<String, CardDatabaseHolder>());
public MtgCardServer() {
_dataProviders.put("mtgGoldFish", new MtgGoldfishProvider());
}
public CardDatabaseHolder getCardDatabaseHolder(String providerName) throws ProviderNotFoundException {
if (!_dataProviders.containsKey(providerName))
throw new ProviderNotFoundException();
return _providersData.get(providerName);
}
public byte[] getDataProvidersResponse() {
final JSONArray providers = new JSONArray();
for (Map.Entry<String, MtgDataProvider> stringMtgDataProviderEntry : _dataProviders.entrySet()) {
Map<String, Object> provider = new LinkedHashMap<String, Object>();
provider.put("id", stringMtgDataProviderEntry.getKey());
provider.put("name", stringMtgDataProviderEntry.getValue().getDisplayName());
providers.add(provider);
}
return providers.toJSONString().getBytes(Charset.forName("UTF-8"));
}
@Override
protected void cleanup() {
for (Map.Entry<String, MtgDataProvider> dataProviderEntry : _dataProviders.entrySet()) {
final String key = dataProviderEntry.getKey();
MtgDataProvider dataProvider = dataProviderEntry.getValue();
if (dataProvider.shouldBeUpdated()) {
dataProvider.update(
new MtgDataProvider.UpdateCallback() {
@Override
public void callback(TimestampedCardCollection result) {
_providersData.put(key, new CardDatabaseHolder(marshallData(result.getSetCardDataList()), result.getUpdateDate()));
}
});
}
}
}
public static byte[] marshallData(List<SetCardData> setCardDataList) {
JSONArray allSets = new JSONArray();
for (SetCardData setCardData : setCardDataList) {
List<Object> setArray = new LinkedList<Object>();
for (CardData cardData : setCardData.getAllCards()) {
Map<String, Object> cardObject = new LinkedHashMap<String, Object>();
cardObject.put("id", cardData.getId());
cardObject.put("name", cardData.getName());
cardObject.put("price", cardData.getPrice());
String link = cardData.getLink();
if (link != null)
cardObject.put("link", link);
setArray.add(cardObject);
}
Map<String, Object> setObject = new LinkedHashMap<String, Object>();
setObject.put("name", setCardData.getSetName());
String setLink = setCardData.getSetLink();
if (setLink != null)
setObject.put("link", setLink);
setObject.put("cards", setArray);
allSets.add(setObject);
}
return allSets.toJSONString().getBytes(Charset.forName("UTF-8"));
}
public class CardDatabaseHolder {
private long _updateDate;
private byte[] _bytes;
public CardDatabaseHolder(byte[] bytes, long updateDate) {
_bytes = bytes;
_updateDate = updateDate;
}
public long getUpdateDate() {
return _updateDate;
}
public byte[] getBytes() {
return _bytes;
}
}
}

View File

@@ -1,13 +0,0 @@
package com.gempukku.mtg;
public interface MtgDataProvider {
boolean shouldBeUpdated();
void update(UpdateCallback updateCallback);
String getDisplayName();
interface UpdateCallback {
void callback(TimestampedCardCollection result);
}
}

View File

@@ -1,4 +0,0 @@
package com.gempukku.mtg;
public class ProviderNotFoundException extends Exception {
}

View File

@@ -1,27 +0,0 @@
package com.gempukku.mtg;
import java.util.List;
public class SetCardData {
private String _setName;
private String _setLink;
private List<CardData> _allCards;
public SetCardData(String setName, String setLink, List<CardData> allCards) {
_setName = setName;
_setLink = setLink;
_allCards = allCards;
}
public List<CardData> getAllCards() {
return _allCards;
}
public String getSetName() {
return _setName;
}
public String getSetLink() {
return _setLink;
}
}

View File

@@ -1,21 +0,0 @@
package com.gempukku.mtg;
import java.util.List;
public class TimestampedCardCollection {
private final long _updateDate;
private final List<SetCardData> _setCardDataList;
public TimestampedCardCollection(long updateDate, List<SetCardData> setCardDataList) {
_updateDate = updateDate;
_setCardDataList = setCardDataList;
}
public List<SetCardData> getSetCardDataList() {
return _setCardDataList;
}
public long getUpdateDate() {
return _updateDate;
}
}

View File

@@ -1,22 +0,0 @@
package com.gempukku.mtg.provider;
public class RetryUtil {
public static <T, U extends Exception> T executeInRetry(Retryable<T, U> task, int retries) throws U {
int retryCount = 0;
while (true) {
try {
return task.execute();
} catch (Exception exp) {
if (task.isRetryable(exp)) {
retryCount++;
if (retryCount == retries)
throw (U) exp;
} else if (exp instanceof RuntimeException) {
throw (RuntimeException) exp;
} else {
throw new RuntimeException(exp);
}
}
}
}
}

View File

@@ -1,7 +0,0 @@
package com.gempukku.mtg.provider;
public interface Retryable<T, U extends Exception> {
T execute() throws U;
boolean isRetryable(Exception exception);
}

View File

@@ -1,19 +0,0 @@
package com.gempukku.mtg.provider.mtggoldfish;
public class MtgGoldfishCardSet {
private String _urlPostfix;
private String _infoLine;
public MtgGoldfishCardSet(String urlPostfix, String infoLine) {
_urlPostfix = urlPostfix;
_infoLine = infoLine;
}
public String getInfoLine() {
return _infoLine;
}
public String getUrlPostfix() {
return _urlPostfix;
}
}

View File

@@ -1,185 +0,0 @@
package com.gempukku.mtg.provider.mtggoldfish;
import com.gempukku.mtg.*;
import com.gempukku.mtg.provider.RetryUtil;
import com.gempukku.mtg.provider.Retryable;
import org.apache.log4j.Logger;
import org.jsoup.HttpStatusException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.Charset;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.LinkedList;
import java.util.List;
import java.util.TimeZone;
import java.util.concurrent.ThreadLocalRandom;
public class MtgGoldfishProvider implements MtgDataProvider {
private static final Logger LOG = Logger.getLogger(MtgGoldfishProvider.class);
private static final long DOWNLOAD_EVERY = 24L * 60 * 60 * 1000;
private static final int SLEEP_MINIMUM = 8 * 1000;
private static final int SLEEP_MAXIMUM = 12 * 1000;
private static final int TIMEOUT = 5 * 60 * 1000;
private static final int RETRY_MAX = 5;
private long _nextStart;
public MtgGoldfishProvider() {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("EST"));
long someDownloadTime = sdf.parse("2015-01-01 04:25:00").getTime();
long number = (System.currentTimeMillis() - someDownloadTime) / DOWNLOAD_EVERY;
_nextStart = someDownloadTime + number * DOWNLOAD_EVERY;
} catch (ParseException exp) {
// ignore
}
}
@Override
public String getDisplayName() {
return "MtgGoldFish.com";
}
@Override
public boolean shouldBeUpdated() {
return _nextStart < System.currentTimeMillis();
}
@Override
public void update(final UpdateCallback updateCallback) {
_nextStart += DOWNLOAD_EVERY;
Thread thr = new Thread(
new Runnable() {
@Override
public void run() {
updateCallback.callback(updateDatabase());
}
});
thr.start();
}
private TimestampedCardCollection updateDatabase() {
long updateDate = System.currentTimeMillis();
final List<SetCardData> resultArray = new LinkedList<SetCardData>();
try {
List<MtgGoldfishCardSet> mtgCardSets = downloadSetListWithRetry();
for (final MtgGoldfishCardSet mtgCardSet : mtgCardSets) {
downloadSetWithRetry(resultArray, mtgCardSet);
}
} catch (IOException exp) {
LOG.error("Unable to download card list", exp);
}
return new TimestampedCardCollection(updateDate, resultArray);
}
private List<MtgGoldfishCardSet> downloadSetListWithRetry() throws IOException {
return RetryUtil.executeInRetry(
new Retryable<List<MtgGoldfishCardSet>, IOException>() {
@Override
public List<MtgGoldfishCardSet> execute() throws IOException {
return downloadSetList();
}
@Override
public boolean isRetryable(Exception exception) {
return exception instanceof IOException;
}
}, RETRY_MAX);
}
private List<MtgGoldfishCardSet> downloadSetList() throws IOException {
List<MtgGoldfishCardSet> results = new LinkedList<MtgGoldfishCardSet>();
Document doc = Jsoup.connect("http://www.mtggoldfish.com/prices/paper/standard").get();
String[] priceListTypes = new String[]{"Standard", "Modern", "Legacy", "Special"};
for (String priceListType : priceListTypes) {
Elements cardElements = doc.select(".priceList-setMenu-" + priceListType);
Elements setImages = cardElements.select("li img");
for (Element setImage : setImages) {
String uriPostfix = setImage.attr("alt");
String setName = setImage.parent().text();
results.add(new MtgGoldfishCardSet(uriPostfix, setName));
}
}
return results;
}
private void downloadSetWithRetry(final List<SetCardData> resultArray, final MtgGoldfishCardSet mtgCardSet) throws IOException {
try {
RetryUtil.executeInRetry(
new Retryable<Void, IOException>() {
@Override
public Void execute() throws IOException {
downloadSet(mtgCardSet, resultArray);
return null;
}
@Override
public boolean isRetryable(Exception exception) {
return exception instanceof IOException;
}
}, RETRY_MAX);
} catch (HttpStatusException exp) {
// Unable to download the set - skip it
}
}
private void downloadSet(MtgGoldfishCardSet mtgCardSet, List<SetCardData> result) throws IOException {
try {
Thread.sleep(getSleepTime());
} catch (InterruptedException e) {
// Ignore
}
String link = "http://www.mtggoldfish.com/index/" + mtgCardSet.getUrlPostfix();
Document doc = Jsoup.parse(new URL(link), TIMEOUT);
boolean isInPaper = (doc.select("#priceHistoryTabs [href=#tab-paper]").size() > 0);
if (isInPaper && !containsLinkSet(result, link)) {
List<CardData> allCards = new LinkedList<CardData>();
Elements cardElements = doc.select(".index-price-table-paper tbody tr");
for (Element cardElement : cardElements) {
String cardName = cardElement.select("td:nth-of-type(1) a").text();
String cardId = mtgCardSet.getUrlPostfix() + "-" + cardName;
float price = Float.parseFloat(cardElement.select("td:nth-of-type(4)").text().replace(",", ""));
allCards.add(new CardData(cardId, cardName, Math.round(price * 100), null));
}
boolean hasFoils = doc.select(".index-price-header-foil-switcher").size() > 0;
if (hasFoils) {
downloadSet(new MtgGoldfishCardSet(mtgCardSet.getUrlPostfix() + "_F", mtgCardSet.getInfoLine() + " · Foil"), result);
}
result.add(new SetCardData(mtgCardSet.getInfoLine(), link, allCards));
}
}
private boolean containsLinkSet(List<SetCardData> result, String link) {
for (SetCardData setCardData : result) {
if (setCardData.getSetLink().equals(link))
return true;
}
return false;
}
private int getSleepTime() {
return SLEEP_MINIMUM + ThreadLocalRandom.current().nextInt(SLEEP_MAXIMUM - SLEEP_MINIMUM);
}
public static void main(String[] args) {
MtgGoldfishProvider mtgCardServer = new MtgGoldfishProvider();
TimestampedCardCollection timestampedCardCollection = mtgCardServer.updateDatabase();
System.out.println(new String(MtgCardServer.marshallData(timestampedCardCollection.getSetCardDataList()), Charset.forName("UTF-8")));
}
}

View File

@@ -1,22 +0,0 @@
package com.gempukku.mtg.provider.tcgplayer;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
public class TCGPlayerProvider {
private List<String> downloadSetList() throws IOException {
List<String> results = new LinkedList<String>();
Document doc = Jsoup.connect("http://shop.tcgplayer.com/magic").get();
for (Element element : doc.select("#SetName option")) {
String setName = element.attr("value");
if (!setName.equals("All"))
results.add(setName);
}
return results;
}
}