ai ratings revamp code review changes

* when resources are missing, fails gracefully
* uses highest found rating, rather than most recently found for the
case of sets containing duplicates
* moved ratings files to their own directory
* thread safe reading of all files
This commit is contained in:
brodee 2018-10-28 14:23:53 -07:00
parent 505a4b00a7
commit 6f93abfd5a
15 changed files with 41 additions and 1232 deletions

View file

@ -50,6 +50,9 @@ public final class RateCard {
private static final int DEFAULT_NOT_RATED_RARE_RATING = 75; private static final int DEFAULT_NOT_RATED_RARE_RATING = 75;
private static final int DEFAULT_NOT_RATED_MYTHIC_RATING = 90; private static final int DEFAULT_NOT_RATED_MYTHIC_RATING = 90;
private static String RATINGS_DIR = "/ratings/";
private static String RATINGS_SET_LIST = RATINGS_DIR + "setsWithRatings.csv";
private static final Logger log = Logger.getLogger(RateCard.class); private static final Logger log = Logger.getLogger(RateCard.class);
/** /**
@ -163,17 +166,7 @@ public final class RateCard {
* @return Rating number from [1:100]. * @return Rating number from [1:100].
*/ */
public static int getCardRating(Card card) { public static int getCardRating(Card card) {
if (setsWithRatingsToBeLoaded == null){ readRatingSetList();
setsWithRatingsToBeLoaded = new LinkedList<>();
InputStream is = RateCard.class.getResourceAsStream("/setsWithRatings.csv");
Scanner scanner = new Scanner(is);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.substring(0,1) != "#"){
setsWithRatingsToBeLoaded.add(line);
}
}
}
String exp = card.getExpansionSetCode().toLowerCase(); String exp = card.getExpansionSetCode().toLowerCase();
readRatings(exp); readRatings(exp);
@ -195,20 +188,45 @@ public final class RateCard {
} }
/** /**
* Reads ratings from resources. * reads the list of sets that have ratings csv files
* populates the setsWithRatingsToBeLoaded
*/
private synchronized static void readRatingSetList(){
try {
if (setsWithRatingsToBeLoaded == null){
setsWithRatingsToBeLoaded = new LinkedList<>();
InputStream is = RateCard.class.getResourceAsStream(RATINGS_SET_LIST);
Scanner scanner = new Scanner(is);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (!line.substring(0,1).equals("#")){
setsWithRatingsToBeLoaded.add(line);
}
}
}
}catch (Exception e) {
log.info("failed to read ratings set list file: " + RATINGS_SET_LIST );
e.printStackTrace();
}
}
/**
* Reads ratings from resources and loads them into ratings map
*/ */
private synchronized static void readRatings(String expCode) { private synchronized static void readRatings(String expCode) {
if (ratings == null) { if (ratings == null) {
ratings = new HashMap<>(); ratings = new HashMap<>();
} }
if (setsWithRatingsToBeLoaded.contains(expCode)){ if (setsWithRatingsToBeLoaded.contains(expCode)){
System.out.println("reading draftbot ratings for the set" + expCode); log.info("reading draftbot ratings for the set" + expCode);
readFromFile("/" + expCode + ".csv"); readFromFile(RATINGS_DIR + expCode + ".csv");
setsWithRatingsToBeLoaded.remove(expCode); setsWithRatingsToBeLoaded.remove(expCode);
} }
} }
/**
private static void readFromFile(String path) { * reads ratings from the file
*/
private synchronized static void readFromFile(String path) {
Integer min = Integer.MAX_VALUE, max = 0; Integer min = Integer.MAX_VALUE, max = 0;
Map<String, Integer> thisFileRatings = new HashMap<>(); Map<String, Integer> thisFileRatings = new HashMap<>();
try { try {
@ -233,9 +251,13 @@ public final class RateCard {
for (String name: thisFileRatings.keySet()){ for (String name: thisFileRatings.keySet()){
int r = thisFileRatings.get(name); int r = thisFileRatings.get(name);
int newrating = (int)(100.0f * (r - min) / (max - min)); int newrating = (int)(100.0f * (r - min) / (max - min));
if (!ratings.containsKey(name) ||
(ratings.containsKey(name) && newrating > ratings.get(name)) ){
ratings.put(name, newrating); ratings.put(name, newrating);
} }
}
} catch (Exception e) { } catch (Exception e) {
log.info("failed to read ratings file: " + path );
e.printStackTrace(); e.printStackTrace();
} }
} }

View file

@ -17,3 +17,4 @@ kld
mm3 mm3
ima ima
m13 m13
ktk
1 # this file specifies which sets have set files.
17 mm3
18 ima
19 m13
20 ktk