From 311e6b89157b3b3d1cd285fd431f275d887754be Mon Sep 17 00:00:00 2001 From: Jonathan Bernard Date: Fri, 27 Aug 2010 04:59:54 -0500 Subject: [PATCH] Incremental commit. Starting on stat and game tracking. --- src/com/jdbernard/teammaker/Game.groovy | 36 ++++++++++++++ src/com/jdbernard/teammaker/Player.groovy | 40 +++++++++++++++ src/com/jdbernard/teammaker/TeamMaker.groovy | 52 +++++++++++++++++++- src/com/jdbernard/teammaker/TeamPanel.groovy | 6 +++ 4 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 src/com/jdbernard/teammaker/Game.groovy create mode 100644 src/com/jdbernard/teammaker/TeamPanel.groovy diff --git a/src/com/jdbernard/teammaker/Game.groovy b/src/com/jdbernard/teammaker/Game.groovy new file mode 100644 index 0000000..b161fda --- /dev/null +++ b/src/com/jdbernard/teammaker/Game.groovy @@ -0,0 +1,36 @@ +package com.jdbernard.teammaker + +public class Game { + static int BASE_POINT_VALUE = 1 + + // lists of players for each team + List team1 + List team2 + + // stats for each game + Map threePointAttempts = [:] + Map threePointers = [:] + Map twoPointAttempts = [:] + Map twoPointers = [:] + Map rebounds = [:] + Map assists = [:] + Map turnovers = [:] + + public int getTeam1Score() { return getTeamScore(team1) } + public int getTeam2Score() { return getTeamScore(team2) } + + public int getTeamScore(List team) { return team.sum { getPlayerScore(it) } } + + public int getPlayerScore(Player player) { + return (twoPointers[(player.name)] ?: 0) * BASE_POINT_VALUE + + (threePointers[(player.name)] ?: 0) * (BASE_POINT_VALUE + 1) + } + + public int getTeamRebounds(List team) { + return team.sum { rebounds[(it.name)] ?: 0 } + } + + public int getTeamTurnovers(List team) { + return team.sum { turnovers[(it.name)] ?: 0 } + } +} diff --git a/src/com/jdbernard/teammaker/Player.groovy b/src/com/jdbernard/teammaker/Player.groovy index cea5613..077e28b 100644 --- a/src/com/jdbernard/teammaker/Player.groovy +++ b/src/com/jdbernard/teammaker/Player.groovy @@ -1,8 +1,48 @@ package com.jdbernard.teammaker public class Player implements Comparable { + // persistant data String name + List games + + // transient data int gamesSat + boolean benched + + public int getPlusMinus() { + if (games.size() == 0) return 0 + int plusMinus games.sum { game -> + if (game.team1.contains(this)) { + return game.team1Score - game.team2Score + else return game.team2Score - game.team1Score + } + return plusMinus / games.size() + } + + public float getPointsPerGame() { + float points = 0 + games.each { game -> + points += (game.threePointers[(name)] ?: 0) * (Game.BASE_POINT_VALUE + 1) + points += (game.twoPointers[(name)] ?: 0) * (Game.BASE_POINT_VALUE) + } + + return (games.size() == 0 ? 0 : points /= games.size()) + } + + public float getReboundsPerGame() { + if (games.size() == 0) return 0 + return games.sum { it.rebounds[(name)] ?: 0 } / games.size() + } + + public float getAssistsPerGame() { + if (games.size() == 0) return 0 + return games.sum { it.assists[(name)] ?: 0 } / games.size() + } + + public float getTurnoversPerGame() { + if (games.size() == 0) return 0 + else return games.sum { it.turnovers[(name)] ?: 0 } / games.size() + } public int compareTo(Player that) { int r = that.gamesSat - this.gamesSat diff --git a/src/com/jdbernard/teammaker/TeamMaker.groovy b/src/com/jdbernard/teammaker/TeamMaker.groovy index 4c4c620..4317a4a 100644 --- a/src/com/jdbernard/teammaker/TeamMaker.groovy +++ b/src/com/jdbernard/teammaker/TeamMaker.groovy @@ -30,6 +30,8 @@ public class TeamMaker { def inGamePlayerDropMenu def sittingPlayerDropMenu + def config + protected Logger log = Logger.getLogger(getClass()) PlayerChooser playerChooser = new WeightedChooser(hardLimit: 2) @@ -75,7 +77,7 @@ public class TeamMaker { */ - private void init() { + private void initGUI() { inGamePlayerDropMenu = swing.popupMenu(/*'Player Options'*/) { menuItem('Bench Player',/* enabled: bind { !model.inGame },*/ @@ -97,10 +99,28 @@ public class TeamMaker { model.popupPlayers.each { setGamesSat(it) }}) } + // TODO + // settingsDialog = new SettingsDialog(main: this) + frame = swing.frame(title: "JDB TeamMaker v$version", size: [600, 400], locationRelativeTo: null, defaultCloseOperation: JFrame.EXIT_ON_CLOSE) { + menuBar() { + menu('Settings') { + pointsButtonGroup = buttonGroup() + pointsButtonGroup << radioButtonMenuItem("1's and 2's", + actionPerformed: { Game.BASE_POINT_VALUE = 1 }) + pointsButtonGroup << radioButtonMenuItem("2's and 3's", + actionPerformed: { Game.BASE_POINT_VALUE = 2 }) + + separator() + + menuItem('Team Size...', + actionPerformed: { setTeamSize() }) + } + } + panel() { gridBagLayout() @@ -196,7 +216,24 @@ public class TeamMaker { } TeamMaker() { - init() + config = new Properties() + + // look for team maker config file + File teammakerrc = new File(System.getProperty('user.home'), + '.teammakerrc') + if (!teammakerrc.exists()) + getClass().getResourceAsStream('/default-teammakerrc') + .withInputStream { is -> teammakerrc.text = is.text } + + // load config + teammakerrc.withInputStream { config.load(is) } + + // set configured properties + Game.BASE_POINT_VALUE = config.getProperty('base-point-value', + '1').toInteger() + model.teamSize = config.getProperty('team.size', '4').toInteger() + + initGUI() } /** @@ -208,6 +245,17 @@ public class TeamMaker { return (spots == 0 ? model.teamSize : spots) } + private void setTeamSize() { + def newSize = JOptionPane.showMessageDialog(frame, + "Enter the new team size: ", "Set Team Size...", + JOptionPane.QUESTION_MESSAGE) + + if (newSize.isInteger()) model.teamSize = newSize.toInteger() + else JOptionPane.showMessageDialog(frame, + "$newSize is not a valid team size (it is not a number...).", + "Set Team Size...", JOptionPane.ERROR_MESSAGE) + } + private void addPlayer() { def name = JOptionPane.showInputDialog(frame, "Enter the new player's name: ", "New Player...", diff --git a/src/com/jdbernard/teammaker/TeamPanel.groovy b/src/com/jdbernard/teammaker/TeamPanel.groovy new file mode 100644 index 0000000..267ca4c --- /dev/null +++ b/src/com/jdbernard/teammaker/TeamPanel.groovy @@ -0,0 +1,6 @@ +package com.jdbernard.teammaker + +public class TeamPanel { + + +}