Seperated model from GUI for player lists.

This commit is contained in:
Jonathan Bernard 2010-08-25 08:16:50 -05:00
parent 38c6cb2552
commit 06864ccb36
3 changed files with 93 additions and 59 deletions

View File

@ -1,7 +1,7 @@
#Mon, 09 Aug 2010 19:14:52 -0500 #Wed, 25 Aug 2010 08:14:14 -0500
#Mon Jul 05 23:10:39 CDT 2010 #Mon Jul 05 23:10:39 CDT 2010
app.version=0.3.0 app.version=0.4.0
build.number=85 build.number=0
src.dir=src src.dir=src
lib.dir=lib lib.dir=lib
build.dir=build build.dir=build

View File

@ -16,18 +16,15 @@ public class TeamMaker {
/* ======== MODEL ======== */ /* ======== MODEL ======== */
public static def swing = new SwingBuilder() public static def swing = new SwingBuilder()
public static final String version = "0.3" public static final String version = "0.4"
def frame def frame
def team1List def team1List
List team1Players
def team2List def team2List
List team2Players
def team1WinsButton def team1WinsButton
def team2WinsButton def team2WinsButton
def newGameButton def newGameButton
def sittingList def sittingList
List sittingPlayers
def inGamePlayerDropMenu def inGamePlayerDropMenu
def sittingPlayerDropMenu def sittingPlayerDropMenu
@ -38,12 +35,16 @@ public class TeamMaker {
OddsCalculator oddsCalculator = new OddsCalculator() OddsCalculator oddsCalculator = new OddsCalculator()
class Observables { class Observables {
@Bindable List team1Players = []
@Bindable List team2Players = []
@Bindable List sittingPlayers = []
@Bindable boolean inGame = false @Bindable boolean inGame = false
@Bindable int teamSize = 4 @Bindable int teamSize = 4
@Bindable Player popupPlayer = null @Bindable Player popupPlayer = null
@Bindable int popupTeam;
} }
def model = new Observables()
def model = new Observables()
/* ======== VIEW ======== */ /* ======== VIEW ======== */
@ -55,7 +56,7 @@ public class TeamMaker {
| | | |
| +-Current-Game--------------+ +-Sitting-Players+ | | +-Current-Game--------------+ +-Sitting-Players+ |
| | | | | | | | | | | |
| | Team 1 Team 2 | | Jeptha 3 | | | | Team A Team B | | Jeptha 3 | |
| | Derrick Eric | | Jack 2 | | | | Derrick Eric | | Jack 2 | |
| | Shane Brandon | | Sam 2 | | | | Shane Brandon | | Sam 2 | |
| | Shane Darren | | Greg 2 | | | | Shane Darren | | Greg 2 | |
@ -76,15 +77,16 @@ public class TeamMaker {
inGamePlayerDropMenu = swing.popupMenu(/*'Player Options'*/) { inGamePlayerDropMenu = swing.popupMenu(/*'Player Options'*/) {
menuItem('Bench Player',/* enabled: bind { !model.inGame },*/ menuItem('Bench Player',/* enabled: bind { !model.inGame },*/
actionPerformed: { benchPlayer(model.popupPlayer) }) actionPerformed: { benchPlayer(model.popupPlayer,
model.popupTeam) })
} }
sittingPlayerDropMenu = swing.popupMenu(/*'Player Options'*/) { sittingPlayerDropMenu = swing.popupMenu(/*'Player Options'*/) {
menuItem('Assign to Team 1', /*enabled: bind { !model.inGame },*/ menuItem('Assign to Team A', /*enabled: bind { !model.inGame },*/
actionPerformed: { assignPlayer(model.popupPlayer, team1List) }) actionPerformed: { assignPlayer(model.popupPlayer, 1) })
menuItem('Assign to Team 2', /*enabled: bind { !model.inGame },*/ menuItem('Assign to Team B', /*enabled: bind { !model.inGame },*/
actionPerformed: { assignPlayer(model.popupPlayer, team2List) }) actionPerformed: { assignPlayer(model.popupPlayer, 2) })
menuItem('Set Games Sat', menuItem('Set Games Sat',
actionPerformed: { setGamesSat(model.popupPlayer) }) actionPerformed: { setGamesSat(model.popupPlayer) })
@ -125,7 +127,7 @@ public class TeamMaker {
constraints: gbc(gridx: 0, gridy: 2, anchor: constraints: gbc(gridx: 0, gridy: 2, anchor:
GBC.CENTER, insets: [5, 5, 5, 0]), GBC.CENTER, insets: [5, 5, 5, 0]),
enabled: bind { model.inGame }, enabled: bind { model.inGame },
actionPerformed: { declareLoser(team2List)}) actionPerformed: { declareLoser(2)})
team2List = list(cellRenderer: teamListRenderer, team2List = list(cellRenderer: teamListRenderer,
constraints: gbc(gridx: 1, gridy: 1, fill: GBC.BOTH, constraints: gbc(gridx: 1, gridy: 1, fill: GBC.BOTH,
@ -140,7 +142,7 @@ public class TeamMaker {
constraints: gbc(gridx: 1, gridy: 2, constraints: gbc(gridx: 1, gridy: 2,
anchor: GBC.CENTER, insets: [5, 5, 5, 5]), anchor: GBC.CENTER, insets: [5, 5, 5, 5]),
enabled: bind { model.inGame }, enabled: bind { model.inGame },
actionPerformed: { declareLoser(team1List)} ) actionPerformed: { declareLoser(1)} )
} }
@ -170,8 +172,8 @@ public class TeamMaker {
constraints: gbc(gridx: 2, gridy: 1, anchor: GBC.CENTER, constraints: gbc(gridx: 2, gridy: 1, anchor: GBC.CENTER,
insets: [5, 5, 5, 5]), insets: [5, 5, 5, 5]),
actionPerformed: { actionPerformed: {
sittingList.model.removeElement( model.sittingPlayers.remove(sittingList.selectedValue)
sittingList.selectedValue) }) refreshGUI() })
} }
} }
@ -193,8 +195,8 @@ public class TeamMaker {
} }
public int getSpotsOpen() { public int getSpotsOpen() {
def spots = (model.teamSize - team1List.model.size()) + def spots = (model.teamSize - model.team1Players.size()) +
(model.teamSize - team2List.model.size()) (model.teamSize - model.team2Players.size())
return (spots == 0 ? model.teamSize : spots) return (spots == 0 ? model.teamSize : spots)
} }
@ -208,22 +210,17 @@ public class TeamMaker {
def player = new Player() def player = new Player()
player.name = name player.name = name
player.gamesSat = 0 player.gamesSat = 0
sittingList.model.addElement(player) model.sittingPlayers << player
// recalculate odds refreshGUI()
def players = sittingList.model.collect { it }
swing.doOutside {
oddsCalculator.recalculate(players, getSpotsOpen(), playerChooser)
swing.edt { sittingList.repaint() }
}
} }
private void newGame() { private void newGame() {
if (team1List.model.size() < model.teamSize) if (model.team1Players.size() < model.teamSize)
populate(team1List) populate(1)
if (team2List.model.size() < model.teamSize) if (model.team2Players.size() < model.teamSize)
populate(team2List) populate(2)
recalculateOdds() recalculateOdds()
@ -231,23 +228,45 @@ public class TeamMaker {
} }
private void declareLoser(def teamList) { private void declareLoser(int teamNum) {
sittingList.model.each { it.gamesSat++ } model.sittingPlayers.each { it.gamesSat++ }
teamList.model.each { player -> def teamPlayers = playersForTeam(teamNum)
teamPlayers.each { player ->
player.gamesSat = 0 player.gamesSat = 0
sittingList.model.addElement(player) model.sittingPlayers << player
} }
recalculateOdds() teamPlayers.clear()
refreshGUI()
teamList.model.clear()
teamList.repaint()
sittingList.repaint()
model.inGame = false model.inGame = false
} }
private void refreshGUI() {
if (log.isTraceEnabled()) log.trace("Refreshing GUI.")
if (log.isDebugEnabled()) {
log.debug("team1Players: ${model.team1Players}")
log.debug("team2Players: ${model.team2Players}")
}
team1List.model = new DefaultListModel()
team2List.model = new DefaultListModel()
sittingList.model = new SortedPlayerModel()
model.team1Players.each { team1List.model.addElement(it) }
model.team2Players.each { team2List.model.addElement(it) }
model.sittingPlayers.each { sittingList.model.addElement(it) }
frame.repaint()
recalculateOdds()
}
private void recalculateOdds() { private void recalculateOdds() {
def players = sittingList.model.collect { it } def players = model.sittingPlayers
swing.doOutside { swing.doOutside {
oddsCalculator.recalculate(players, getSpotsOpen(), playerChooser) oddsCalculator.recalculate(players, getSpotsOpen(), playerChooser)
swing.edt { sittingList.repaint() } swing.edt { sittingList.repaint() }
@ -255,24 +274,29 @@ public class TeamMaker {
} }
private void populate(def teamList) { private void populate(int teamNum) {
while (teamList.model.size() < model.teamSize) { def teamPlayers = playersForTeam(teamNum)
def player = playerChooser.choose(sittingList.model) while (teamPlayers.size() < model.teamSize) {
teamList.model.addElement(player) def player = playerChooser.choose(model.sittingPlayers)
sittingList.model.removeElement(player) teamPlayers << player
model.sittingPlayers.remove(player)
} }
refreshGUI();
} }
private benchPlayer(Player p) { private benchPlayer(Player p, int teamNum) {
team1List.remove(p) def teamPlayers = playersForTeam(teamNum)
sittingList.model.addElement(p) teamPlayers.remove(p)
recalculateOdds() model.sittingPlayers << p
refreshGUI()
} }
private assignPlayer(Player p, def teamList) { private assignPlayer(Player p, int teamNum) {
sittingList.model.removeElement(p) def teamPlayers = playersForTeam(teamNum)
teamList.model.addElement(p) model.sittingPlayers.remove(p)
recalculateOdds() teamPlayers << p
refreshGUI()
} }
private void showSittingPlayerDropMenu(def evt) { private void showSittingPlayerDropMenu(def evt) {
@ -288,6 +312,7 @@ public class TeamMaker {
int index = teamList.locationToIndex(evt.point) int index = teamList.locationToIndex(evt.point)
teamList.selectedIndex = index teamList.selectedIndex = index
this.model.popupPlayer = teamList.model.getElementAt(index) this.model.popupPlayer = teamList.model.getElementAt(index)
this.model.popupTeam = (teamList == team1List ? 1 : 2)
inGamePlayerDropMenu.show(teamList, evt.point.@x, evt.point.@y) inGamePlayerDropMenu.show(teamList, evt.point.@x, evt.point.@y)
} }
@ -298,8 +323,6 @@ public class TeamMaker {
JOptionPane.QUESTION_MESSAGE) JOptionPane.QUESTION_MESSAGE)
int newGames int newGames
sittingList.model.removeElement(p)
if (!value) return if (!value) return
try { newGames = value.toInteger() } try { newGames = value.toInteger() }
catch (Throwable t) { catch (Throwable t) {
@ -311,8 +334,17 @@ public class TeamMaker {
} }
p.gamesSat = newGames p.gamesSat = newGames
if (!sittingList.model.set.contains(p)) sittingList.model.addElement(p)
recalculateOdds() refreshGUI()
}
private List playersForTeam(int teamNum) {
if (teamNum == 1) return model.team1Players
else return model.team2Players
}
private List listForTeam(int teamNum) {
if (teamNum == 1) return team1List
else return team2List
} }
} }

View File

@ -8,14 +8,16 @@ public class WeightedChooser extends PlayerChooser {
// make a proper list // make a proper list
players = players.collect { it } players = players.collect { it }
def choices = [] def choices = []
def threshold = getThreshold(players) def threshold
if (log.isTraceEnabled()) { if (log.isTraceEnabled()) {
log.trace("Choosing a player weighted by games sat.") log.trace("Choosing a player weighted by games sat.")
log.trace("Players: $players") log.trace("Players: $players")
log.trace("Threshold: $threshold") log.trace("Threshold: ${getThreshold(players)}")
} }
threshold = getThreshold(players)
// add players, ignoring those past the hard limit // add players, ignoring those past the hard limit
players.each { player -> players.each { player ->
if (player.gamesSat >= threshold) { if (player.gamesSat >= threshold) {