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
app.version=0.3.0
build.number=85
app.version=0.4.0
build.number=0
src.dir=src
lib.dir=lib
build.dir=build

View File

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