Started restructering the project.

This commit is contained in:
Jonathan Bernard 2010-07-04 08:01:51 -05:00
parent fc4084fb6e
commit 2ea56d05c5
8 changed files with 215 additions and 89 deletions

56
build.xml Normal file
View File

@ -0,0 +1,56 @@
<project name="JDB Team Maker" basedir="." default="release">
<property environment="env"/>
<property file="project.properties"/>
<path id="groovy.class.path">
<fileset dir="${env.GROOVY_HOME}/lib">
<include name="*.jar"/>
</fileset>
</path>
<taskdef name="groovyc"
classpathref="groovy.class.path"
classname="org.codehaus.groovy.ant.Groovyc"/>
<target name="init">
<mkdir dir="${build.dir}/classes"/>
</target>
<target name="clean">
<delete dir="${build.dir}"/>
</target>
<target name="compile" depends="init">
<groovyc
srcdir="${src.dir}"
destdir="${build.dir}/classes"
classpathref="groovy.class.path"/>
</target>
<target name="increment-build-number">
<propertyfile file="project.properties">
<entry key="build.number" operation="+" default="0"
value="1" type="int"/>
</propertyfile>
</target>
<target name="build" depends="compile,increment-build-number">
<unjar dest="${build.dir}/classes">
<fileset dir="${env.GROOVY_HOME}/embeddable">
<include name="*.jar"/>
</fileset>
</unjar>
<jar
destfile="${build.dir}/${app.name}-${app.version}.${build.number}.jar"
basedir="${build.dir}/classes">
<manifest>
<attribute name="Main-Class" value="com.jdbernard.teammaker.TeamMaker"/>
</manifest>
</jar>
</target>
<target name="release" depends="build">
</target>
</project>

6
project.properties Normal file
View File

@ -0,0 +1,6 @@
#Sun Jul 04 00:39:50 CDT 2010
app.version=0.3.0
src.dir=src
build.number=4
build.dir=build
app.name=team-maker

View File

@ -0,0 +1,14 @@
package com.jdbernard.teammaker
public class Player implements Comparable<Player> {
String name
int gamesSat
public int compareTo(Player that) {
int r = this.gamesSat - that.gamesSat
if (r == 0) r = this.name.compareTo(that.name)
return r
}
public String toString() { return name }
}

View File

@ -0,0 +1,14 @@
package com.jdbernard.teammaker
public abstract class PlayerChooser {
public abstract Player choose (def players)
public abstract float calculateOdds(def players, def player)
public final static Random = new Random(System.currentTimeMillis())
public static final Player chooseRandomly(def choices) {
choices.sort { rand.nextInt() }
return choices[0]
}
}

View File

@ -0,0 +1,68 @@
package com.jdbernard.teammaker
import java.awt.Color
import java.awt.Component
import javax.swing.EmptyBorder
import javax.swing.JLabel
import javax.swing.JList
import javax.swing.JPanel
import javax.swing.LineBorder
import javax.swing.ListCellRenderer
public class PlayerListCellRenderer extends JPanel implements ListCellRenderer {
boolean showStats
boolean colored
private PlayerChooser chooser
private JLabel nameLabel
private JLabel statsLabel
private static def linedBorder = new LineBorder(Color.BLACK)
private static def emptyBorder = new EmptyBorder(1)
private def colors = [Color.getHSBColor(0.32f, 1f, 1f),
Color.getHSBColor(0.24f,1f,1f), Color.getHSBColor(0.16f, 1f, 1f),
Color.getHSBColor(0.08f, 1f, 1f), Color.getHSBColor(0.0f, 1f, 1f)]
public PlayerListCellRenderer(Map params) {
showStats = params.showStats ?: false
colored = params.colored ?: false
chooser = params.chooser ?: WeightedChooser.getInstance()
setLayout(new BorderLayout())
setOpaque(colored)
nameLabel = new JLabel()
nameLabel.opaque = false
nameLabel.horizontalAlignment = JLabel.LEADING
add(nameLabel, BorderLayout.WEST)
if (showStats) {
statsLabel = new JLabel()
statsLabel.horizontalAlignment = JLabel.TRAILING,
statsLabel.opaque = false
add(statsLabel, BorderLayout.EAST)
}
}
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
assert value instanceof Player
nameLabel.setText(value.name)
if (isSelected) setBorder(linedBorder)
else setBorder(emptyBorder)
if (showStats)
statsLabel.text = value.gamesSat
if (colored) {
def c = colors[Math.min(4, value.gamesSat)]
setBackground(c)
}
return this
}
}

View File

@ -0,0 +1,14 @@
package com.jdbernard.teammaker
public class RandomChooser extends PlayerChooser {
public Player choose(def players) {
def choices = []
players.each { choices << it }
chooseRandomly(choices)
}
public float calculateOdds(def players, def player) {
return 1f / (float) players
}
}

View File

@ -0,0 +1,28 @@
package com.jdbernard.teammaker
import javax.swing.AbstractListModel
public class SortedPlayerModel extends AbstractListModel {
private TreeSet<Player> set = new TreeSet<Player>()
public int getSize() { return set.size() }
public Object getElementAt(int index) {
int i = 0
for (player in set) {
if (i == index) return player
i++
}
throw new ArrayIndexOutOfBoundsException()
}
public void addElement(Player p) {
set.add(p)
fireIntervalAdded(this, 0, set.size())
}
public void removeElement(Player p) {
set.remove(p)
fireIntervalRemoved(this, 0, set.size())
}
}

View File

@ -1,18 +1,13 @@
package com.jdbernard.teammaker
import groovy.beans.Bindable import groovy.beans.Bindable
import groovy.swing.SwingBuilder import groovy.swing.SwingBuilder
import java.awt.Color
import java.awt.Component
import java.awt.GridBagConstraints as GBC import java.awt.GridBagConstraints as GBC
import java.awt.GridBagLayout import java.awt.GridBagLayout
import java.awt.BorderLayout import java.awt.BorderLayout
import javax.swing.AbstractListModel
import javax.swing.DefaultListModel import javax.swing.DefaultListModel
import javax.swing.JFrame import javax.swing.JFrame
import javax.swing.JLabel
import javax.swing.JList
import javax.swing.JOptionPane import javax.swing.JOptionPane
import javax.swing.JPanel
import javax.swing.ListCellRenderer
public class TeamMaker { public class TeamMaker {
@ -20,7 +15,6 @@ public class TeamMaker {
public static def swing = new SwingBuilder() public static def swing = new SwingBuilder()
public static final String version = "0.2" public static final String version = "0.2"
public static final Random rand = new Random(System.currentTimeMillis())
def frame def frame
def team1List def team1List
@ -30,23 +24,11 @@ public class TeamMaker {
def newGameButton def newGameButton
def sittingList def sittingList
PlayerChooser chooser = new RandomChooser()
@Bindable boolean inGame = false @Bindable boolean inGame = false
@Bindable int teamSize = 4 @Bindable int teamSize = 4
class Player implements Comparable<Player> {
String name
int gamesSat
public int compareTo(Player that) {
int r = this.gamesSat - that.gamesSat
if (r == 0) r = this.name.compareTo(that.name)
return r
}
}
class SortedPlayerModel extends AbstractListModel {
}
/* ======== VIEW ======== */ /* ======== VIEW ======== */
@ -95,7 +77,7 @@ public class TeamMaker {
label('Team B', constraints: gbc(gridx: 1, gridy: 0, label('Team B', constraints: gbc(gridx: 1, gridy: 0,
fill: GBC.BOTH, insets: [5, 5, 0, 5])) fill: GBC.BOTH, insets: [5, 5, 0, 5]))
def teamListRenderer = new PlayerRenderer(showStats: false, def teamListRenderer = new PlayerListCellRenderer(showStats: false,
colored: false) colored: false)
team1List = list(cellRenderer: teamListRenderer, team1List = list(cellRenderer: teamListRenderer,
@ -150,9 +132,9 @@ public class TeamMaker {
fill: GBC.BOTH, insets: [5, 5, 0, 5], weighty: 1), fill: GBC.BOTH, insets: [5, 5, 0, 5], weighty: 1),
border: titledBorder(title: 'Sitting Players')) { border: titledBorder(title: 'Sitting Players')) {
sittingList = list(cellRenderer: new PlayerRenderer( sittingList = list(cellRenderer: new PlayerListCellRenderer(
showStats: true, colored: true), showStats: true, colored: true),
model: new DefaultListModel()) model: new SortedPlayerModel())
} }
button('Next Game', constraints: gbc(gridx: 0, gridy: 1, button('Next Game', constraints: gbc(gridx: 0, gridy: 1,
@ -180,72 +162,9 @@ public class TeamMaker {
} }
} }
swing.bind(source: this, sourceProperty: 'inGame',
target: team1WinsButton, targetProperty: 'enabled')
swing.bind(source: this, sourceProperty: 'inGame',
target: team2WinsButton, targetProperty: 'enabled')
} }
class PlayerRenderer extends JPanel implements ListCellRenderer {
boolean showStats
boolean colored
private JLabel nameLabel
private JLabel statsLabel
private static def linedBorder
private static def emptyBorder
private def colors = [Color.getHSBColor(0.32f, 1f, 1f),
Color.getHSBColor(0.24f,1f,1f), Color.getHSBColor(0.16f, 1f, 1f),
Color.getHSBColor(0.08f, 1f, 1f), Color.getHSBColor(0.0f, 1f, 1f)]
public PlayerRenderer(Map params) {
showStats = params.showStats ?: false
colored = params.colored ?: false
setLayout(new BorderLayout())
setOpaque(colored)
linedBorder = TeamMaker.swing.lineBorder(color: Color.BLACK)
emptyBorder = TeamMaker.swing.emptyBorder(1)
nameLabel = TeamMaker.swing.label(opaque: false,
horizontalAlignment: JLabel.LEADING)
add(nameLabel, BorderLayout.WEST)
if (showStats) {
statsLabel = TeamMaker.swing.label(
horizontalAlignment: JLabel.TRAILING,
opaque: false)
add(statsLabel, BorderLayout.EAST)
}
}
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
assert value instanceof Player
nameLabel.setText(value.name)
if (isSelected) setBorder(linedBorder)
else setBorder(emptyBorder)
if (showStats)
statsLabel.text = value.gamesSat
if (colored) {
def c = colors[Math.min(4, value.gamesSat)]
setBackground(c)
}
return this
}
}
/* ======== CONTROLLER ======== */ /* ======== CONTROLLER ======== */
@ -281,8 +200,15 @@ public class TeamMaker {
private static Player choosePlayer(def list) { private static Player choosePlayer(def list) {
def choices = [] def choices = []
list.model.each { choices << it } list.model.each { player ->
player.gamesSat.times { choices << player }
}
// if no players have sat at least one game, add all once
if (choices.size() == 0) list.model.each { player -> choices << player }
choices.sort { rand.nextInt() } choices.sort { rand.nextInt() }
println choices
return choices[0] return choices[0]
} }