Fixing target format shenanigans when the user has a translation plugin that changes the values of the dropdowns.

This commit is contained in:
Christian 'ketura' McCarty
2022-08-08 07:55:21 -05:00
parent 1c6f26ce6e
commit e66e478054
2 changed files with 29 additions and 10 deletions

View File

@@ -128,7 +128,7 @@ public class DeckRequestHandler extends LotroServerRequestHandler implements Uri
StringBuilder valid = new StringBuilder(); StringBuilder valid = new StringBuilder();
StringBuilder invalid = new StringBuilder(); StringBuilder invalid = new StringBuilder();
LotroFormat format = _formatLibrary.getHallFormats().get(targetFormat); LotroFormat format = validateFormat(targetFormat);
if(format == null || targetFormat == null) if(format == null || targetFormat == null)
{ {
responseWriter.writeHtmlResponse("Invalid format: " + targetFormat); responseWriter.writeHtmlResponse("Invalid format: " + targetFormat);
@@ -207,11 +207,13 @@ public class DeckRequestHandler extends LotroServerRequestHandler implements Uri
Player resourceOwner = getResourceOwnerSafely(request, participantId); Player resourceOwner = getResourceOwnerSafely(request, participantId);
LotroDeck lotroDeck = _lotroServer.createDeckWithValidate(deckName, contents, targetFormat, notes); LotroFormat validatedFormat = validateFormat(targetFormat);
LotroDeck lotroDeck = _lotroServer.createDeckWithValidate(deckName, contents, validatedFormat.getName(), notes);
if (lotroDeck == null) if (lotroDeck == null)
throw new HttpProcessingException(400); throw new HttpProcessingException(400);
_deckDao.saveDeckForPlayer(resourceOwner, deckName, targetFormat, notes, lotroDeck); _deckDao.saveDeckForPlayer(resourceOwner, deckName, validatedFormat.getName(), notes, lotroDeck);
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
@@ -476,6 +478,23 @@ public class DeckRequestHandler extends LotroServerRequestHandler implements Uri
return serializeDeck(deck); return serializeDeck(deck);
} }
private LotroFormat validateFormat(String name)
{
LotroFormat validatedFormat = _formatLibrary.getFormat(name);
if(validatedFormat == null)
{
try {
validatedFormat = _formatLibrary.getFormatByName(name);
}
catch(Exception ex)
{
validatedFormat = _formatLibrary.getFormatByName("Anything Goes");
}
}
return validatedFormat;
}
private Document serializeDeck(LotroDeck deck) throws ParserConfigurationException { private Document serializeDeck(LotroDeck deck) throws ParserConfigurationException {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
@@ -487,8 +506,11 @@ public class DeckRequestHandler extends LotroServerRequestHandler implements Uri
if (deck == null) if (deck == null)
return doc; return doc;
LotroFormat validatedFormat = validateFormat(deck.getTargetFormat());
Element targetFormat = doc.createElement("targetFormat"); Element targetFormat = doc.createElement("targetFormat");
targetFormat.setAttribute("formatName", deck.getTargetFormat()); targetFormat.setAttribute("formatName", validatedFormat.getName());
targetFormat.setAttribute("formatCode", validatedFormat.getCode());
deckElem.appendChild(targetFormat); deckElem.appendChild(targetFormat);
Element notes = doc.createElement("notes"); Element notes = doc.createElement("notes");

View File

@@ -817,7 +817,7 @@ var GempLotrDeckBuildingUI = Class.extend({
if (deckContents == null) if (deckContents == null)
alert("Deck must contain at least Ring-bearer, The One Ring and 9 sites"); alert("Deck must contain at least Ring-bearer, The One Ring and 9 sites");
else else
this.comm.saveDeck(this.deckName, $("#formatSelect :selected").text(), this.notes, deckContents, function (xml) { this.comm.saveDeck(this.deckName, that.formatSelect.val(), this.notes, deckContents, function (xml) {
that.deckModified(false); that.deckModified(false);
alert("Deck was saved"); alert("Deck was saved");
}, { }, {
@@ -1021,13 +1021,10 @@ var GempLotrDeckBuildingUI = Class.extend({
if (targetFormat.length > 0) if (targetFormat.length > 0)
{ {
var formatName = targetFormat[0].getAttribute("formatName"); var formatName = targetFormat[0].getAttribute("formatName");
var formatCode = targetFormat[0].getAttribute("formatCode");
//$('#formatSelect option[value="' + formatName + '"]').prop('selected', true); //$('#formatSelect option[value="' + formatName + '"]').prop('selected', true);
$('#formatSelect option').each(function(){ this.formatSelect.val(formatCode);
if($(this).text() == formatName){
$(this).prop('selected', true);
}
});
} }
var notes = root.getElementsByTagName("notes"); var notes = root.getElementsByTagName("notes");