From 06ed7e0103edf983b4eb74aef3ee1369f5ea0cc3 Mon Sep 17 00:00:00 2001 From: Bronson Graansma Date: Tue, 29 Jan 2019 10:02:30 -0500 Subject: [PATCH] addes picking --- src/pw/graansma/Main.java | 197 +++++++++++++++++++++++++++--------- src/pw/graansma/Player.java | 35 ++++++- 2 files changed, 178 insertions(+), 54 deletions(-) diff --git a/src/pw/graansma/Main.java b/src/pw/graansma/Main.java index 21a25da..2bd30ed 100644 --- a/src/pw/graansma/Main.java +++ b/src/pw/graansma/Main.java @@ -19,16 +19,20 @@ import java.util.ArrayList; import java.util.List; public class Main { - private static final String RANKINGS_URL = "https://fantasybaseballnerd.com/service/draft-rankings"; + private static final String RANKINGS_URL = "https://fantasybaseballnerd.com/service/league-rankings"; - private final List draft; + private final List league; private final List team; + private final List unavailable; private final File teamFile; + private final File unavailableFile; - private Main(File f) { - teamFile = f; - draft = new ArrayList<>(); - team = new ArrayList<>(); + private Main() { + teamFile = file("team.txt"); + unavailableFile = file("unavailable.txt"); + + league = new ArrayList<>(); + league = new ArrayList<>(); try { DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); @@ -37,27 +41,43 @@ public class Main { Document doc = dBuilder.parse(conn.getInputStream()); doc.getDocumentElement().normalize(); NodeList players = doc.getElementsByTagName("Player"); - for (int i = 0; i < players.getLength(); i++) { + for(int i = 0; i < players.getLength(); i++) { Node node = players.item(i); - if (node.getNodeType() == Node.ELEMENT_NODE) { - draft.add(new Player((Element) node)); - } - } - try(BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(f)))) { - String inputLine; - while((inputLine = br.readLine()) != null) { - team.add(getPlayer(inputLine.replace("\n", "").replace("\r", ""))); + if(node.getNodeType() == Node.ELEMENT_NODE) { + league.add(new Player((Element) node)); } } } catch(Exception e) { e.printStackTrace(); } + team = fromFile(teamFile); + unavailable = fromFile(unavailableFile); } - private void save() { + private List fromFile(File file) { + List players = new ArrayList<>(); + try(BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)))) { + String inputLine; + while((inputLine = br.readLine()) != null) { + players.add(getPlayer(inputLine.replace("\n", "").replace("\r", ""))); + } + } catch(Exception e) { + e.printStackTrace() + } + return players; + } - try(PrintWriter out = new PrintWriter(new FileOutputStream(teamFile, false))) { - for(Player p : team) { + private File file(String name) { + File file = new File(name); + if(!file.exists()) { + file.createNewFile() + } + return file; + } + + private void save(List players, File file) { + try(PrintWriter out = new PrintWriter(new FileOutputStream(file, false))) { + for(Player p : players) { out.println(p.name); } out.flush(); @@ -67,7 +87,7 @@ public class Main { } private Player getPlayer(String name) { - for(Player p : draft) { + for(Player p : league) { if(p.name.equalsIgnoreCase(name)) { return p; } @@ -75,41 +95,38 @@ public class Main { return null; } - private Player cut() { - int index = 0; - double benchMark = 0; - for(int i = 0; i < team.size(); i++) { - Player p = team.get(i); - double score = p.rank + p.draftPosition; - if(score > benchMark) { - benchMark = score; - index = i; - } - } - return team.remove(index); + private Map teamStructure() { + Map structure = new HashMap<>(); + structure.put(Player.STARTER, 2); + structure.put(Player.RELIEVER, 2); + structure.put(Player.PITCHER, 2); + structure.put(Player.INFIELDER, 4); + structure.put(Player.OUTFIELDER, 4); + structure.put(Player.CATCHER, 1); + structure.put(Player.HITTER, 1); + return structure; } - // cut 15 teamFile - // pick - public static void main(String... args) { - if(args.length < 2 || args.length > 3) { - System.err.println("Usage: java exe cut x teamFile" + - "\n or: java exe pick teamFile"); - } else { - Main m = new Main(new File(args[args.length -1])); + private Player pick() { + List available = new ArrayList<>(m.league); + available.removeAll(m.unavailable); + available.removeAll(m.team); + Map neededPositions = teamStructure(); - switch(args[0]) { - case "cut": - int count = Integer.valueOf(args[1]); - while(m.team.size() > count) { - System.out.println(m.cut().name); + for(Player p : team) { + for(String pos : neededPositions.getKeys()) { + if(p.is(pos)) { + neededPositions.put(pos, neededPositions.get(pos) - 1); + if(neededPositions == 0) { + neededPositions.remove(pos); } break; - case "pick": - //todo have a roster file I can't pick taken players - // only pick players in american league - // pick the best ranked player that I have an open position for - // positions: + } + } + } + //todo have a roster file I can't pick taken players + // pick the best ranked player that I have an open position for + // positions: /* SP x 2 RP x 2 @@ -119,9 +136,89 @@ public class Main { C x 1 DH x 1 (any) */ + + + int index = 0; + double benchMark = Double.MAX_VALUE; + for(int i = 0; i < available.size(); i++) { + Player p = available.get(i); + for(int j=0; j benchMark) { + benchMark = score; + index = i; + } + } + return team.remove(index); + } + + // cut 15 + // pick + // rule-out [-reset] "Player One" "Player Two" ... + // my-team "Player One" "Player Two" ... + public static void main(String... args) { + if(args.length < 2) { + System.err.println( + "Usage: java Main cut x" + + "\n or: java Main pick" + + "\n or: java Main rule-out ..." + + "\n or: java Main my-team ..." + ); + } else { + Main m = new Main(); + + switch(args[0]) { + case "cut": + int count = Integer.valueOf(args[1]); + while(m.team.size() > count) { + System.out.println(m.cut().name); + } + m.save(m.team, m.teamFile); + break; + case "pick": + Player picked = m.pick(); + System.out.println(picked.name + " " + picked.position); + m.save(m.team, m.teamFile); + break; + case "rule-out": + int index = 1; + if(args[1].equals("-reset")) { + m.unavailable.removeAll(); + index = 2; + } + for(int i = index; i < args.length; i++) { + m.unavailable.add(m.getPlayer(args[i])); + } + m.save(m.unavailable, m.unavailableFile); + break; + case "my-team": + m.team.removeAll(); + for(int i = 1; i < args.length; i++) { + m.team.add(getPlayer(args[i])); + } + m.save(m.team, m.teamFile); + break; + } - m.save(); } } } diff --git a/src/pw/graansma/Player.java b/src/pw/graansma/Player.java index 8695c83..e73e929 100644 --- a/src/pw/graansma/Player.java +++ b/src/pw/graansma/Player.java @@ -1,6 +1,9 @@ package pw.graansma; import org.w3c.dom.Element; +import java.util.Map; +import java.util.HashMap; +import java.util.List; public class Player { public final int id; @@ -8,10 +11,27 @@ public class Player { public final int rank; public final String position; public final double draftPosition; - public final String league; - public static final String AMERICAN_LEAGUE = "American"; - public static final String NATIONAL_LEAGUE = "National"; + public static final String STARTER = "SP"; + public static final String RELIEVER = "RP"; + public static final String INFIELDER = "IF"; + public static final String OUTFIELDER = "OF"; + public static final String PITCHER = "P"; + public static final String CATCHER = "C"; + public static final String HITTER = "DH"; + //todo sp and rp + + //todo figure out if api has actual dh in it + private static final Map> positions = new HashMap<>(); + static { + map.put(INFIELD, Arrays.asList("1B", "2B", "3B", "SS")); + map.put(OUTFIELD, Arrays.asList("LF", "CF", "RF")); + map.put(PITCHER, Arrays.asList("SP", "RP")); + map.put(STARTER, Collections.singletonList("SP")); + map.put(RELIEVER, Collections.singletonList("RP")); + map.put(CATCHER, Collections.singletonList("C")); + map.put(HITTER, Collections.singletonList("DH")); //todo can be all + } private String getString(Element element, String tag) { return element.getElementsByTagName(tag).item(0).getTextContent(); @@ -23,7 +43,14 @@ public class Player { rank = Integer.valueOf(getString(element, "rank")); position = getString(element, "position"); draftPosition = Double.valueOf(getString(element, "adp")); - league = getString(element, "league"); + } + + public double getRating() { + return rank + draftPosition; + } + + public boolean is(String pos) { + return positions.hasKey(pos) ? positions.get(pos).contains(position) : false; } @Override