-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ability to load card data from JSON files containing card data.
- Loading branch information
Showing
4 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
src/main/java/com/github/skiwi2/hearthmonitor/CardData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.github.skiwi2.hearthmonitor; | ||
|
||
import javax.script.Invocable; | ||
import javax.script.ScriptEngine; | ||
import javax.script.ScriptEngineManager; | ||
import javax.script.ScriptException; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.net.URISyntaxException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
/** | ||
* @author Frank van Heeswijk | ||
*/ | ||
public class CardData { | ||
private static final CardDataLoader CARD_DATA_LOADER = new CardDataLoader(); | ||
|
||
private final String id; | ||
private final String name; | ||
|
||
public CardData(final String id, final String name) { | ||
this.id = Objects.requireNonNull(id, "id"); | ||
this.name = Objects.requireNonNull(name, "name"); | ||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public static CardData getForCardId(final String cardId) { | ||
Objects.requireNonNull(cardId, "cardId"); | ||
return CARD_DATA_LOADER.load(cardId); | ||
} | ||
|
||
private static class CardDataLoader { | ||
private final Map<String, CardData> cardDataMap = new HashMap<>(); | ||
private final ScriptEngine scriptEngine; | ||
|
||
private CardDataLoader() { | ||
try { | ||
Path setListPath = Paths.get(CardData.class.getResource("SetList.json").toURI()); | ||
String setList = new String(Files.readAllBytes(setListPath), StandardCharsets.UTF_8); | ||
|
||
Path allSetsPath = Paths.get(CardData.class.getResource("AllSets.json").toURI()); | ||
String allSets = new String(Files.readAllBytes(allSetsPath), StandardCharsets.UTF_8); | ||
|
||
scriptEngine = new ScriptEngineManager().getEngineByName("nashorn"); | ||
scriptEngine.eval(new InputStreamReader(new FileInputStream(Paths.get(CardData.class.getResource("CardData.js").toURI()).toFile()), StandardCharsets.UTF_8)); | ||
|
||
Invocable invocable = (Invocable)scriptEngine; | ||
invocable.invokeFunction("setSetList", setList); | ||
invocable.invokeFunction("setAllSets", allSets); | ||
} catch (URISyntaxException | IOException | ScriptException | NoSuchMethodException ex) { | ||
throw new IllegalStateException("an error has occurred when initializing the card data loader", ex); | ||
} | ||
} | ||
|
||
private CardData load(final String cardId) { | ||
if (cardDataMap.containsKey(cardId)) { | ||
return cardDataMap.get(cardId); | ||
} | ||
try { | ||
CardData cardData = load0(cardId); | ||
cardDataMap.put(cardId, cardData); | ||
return cardData; | ||
} catch (ScriptException | NoSuchMethodException ex) { | ||
throw new IllegalStateException("an error occurred when loading cardId = " + cardId, ex); | ||
} | ||
} | ||
|
||
private CardData load0(final String cardId) throws ScriptException, NoSuchMethodException { | ||
Invocable invocable = (Invocable)scriptEngine; | ||
CardData cardData = (CardData)invocable.invokeFunction("getCardData", cardId); | ||
if (cardData == null) { | ||
throw new IllegalStateException("could not find card for cardId = " + cardId); | ||
} | ||
return cardData; | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
src/main/resources/com/github/skiwi2/hearthmonitor/AllSets.json
Large diffs are not rendered by default.
Oops, something went wrong.
28 changes: 28 additions & 0 deletions
28
src/main/resources/com/github/skiwi2/hearthmonitor/CardData.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* Created by Frank van Heeswijk on 23-2-2015. | ||
*/ | ||
|
||
var CardData = Java.type("com.github.skiwi2.hearthmonitor.CardData"); | ||
var setList; | ||
var allSets; | ||
|
||
function setSetList(setListRaw) { | ||
setList = JSON.parse(setListRaw); | ||
} | ||
|
||
function setAllSets(allSetsRaw) { | ||
allSets = JSON.parse(allSetsRaw); | ||
} | ||
|
||
function getCardData(cardId) { | ||
for (var i = 0; i < setList.length; i++) { | ||
var set = setList[i]; | ||
for (var j = 0; j < allSets[set].length; j++) { | ||
var card = allSets[set][j]; | ||
if (card.id == cardId) { | ||
return new CardData(card.id, card.name); | ||
} | ||
} | ||
} | ||
return null; | ||
} |
1 change: 1 addition & 0 deletions
1
src/main/resources/com/github/skiwi2/hearthmonitor/SetList.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
["Basic","Classic","Credits","Curse of Naxxramas","Debug","Goblins vs Gnomes","Missions","Promotion","Reward","System"] |