webidor/src/main/java/cz/xelfi/quoridor/webidor/Game.java
changeset 48 69e897fe8140
parent 37 782d925cb5a1
child 52 45fb5f885591
     1.1 --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/Game.java	Wed Jul 29 08:19:35 2009 +0200
     1.2 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/Game.java	Sun Aug 30 14:37:47 2009 +0200
     1.3 @@ -32,12 +32,13 @@
     1.4  import cz.xelfi.quoridor.Player;
     1.5  import java.util.ArrayList;
     1.6  import java.util.List;
     1.7 -import java.util.UUID;
     1.8  import javax.xml.bind.annotation.XmlAccessType;
     1.9  import javax.xml.bind.annotation.XmlAccessorType;
    1.10  import javax.xml.bind.annotation.XmlAttribute;
    1.11 -import javax.xml.bind.annotation.XmlID;
    1.12 +import javax.xml.bind.annotation.XmlElement;
    1.13  import javax.xml.bind.annotation.XmlRootElement;
    1.14 +import javax.xml.bind.annotation.adapters.XmlAdapter;
    1.15 +import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
    1.16  
    1.17  /**
    1.18   *
    1.19 @@ -45,45 +46,31 @@
    1.20   */
    1.21  @XmlRootElement
    1.22  @XmlAccessorType(XmlAccessType.FIELD)
    1.23 -public final class Game {
    1.24 +public final class Game extends Object {
    1.25 +    @XmlElement
    1.26 +    private GameId id;
    1.27      @XmlAttribute
    1.28 -    private String white;
    1.29 -    @XmlAttribute
    1.30 -    private String black;
    1.31 -    @XmlID
    1.32 -    private String id;
    1.33 -
    1.34 -    private transient Board board;
    1.35 -    private transient List<Move> moves;
    1.36 +    @XmlJavaTypeAdapter(BoardAdapter.class)
    1.37 +    private Board board;
    1.38 +    @XmlElement
    1.39 +    @XmlJavaTypeAdapter(MoveAdapter.class)
    1.40 +    private List<Move> moves;
    1.41  
    1.42      Game() {
    1.43      }
    1.44  
    1.45      public Game(String first, String second) {
    1.46 -        this(
    1.47 -            UUID.randomUUID().toString(),
    1.48 -            first, second
    1.49 -        );
    1.50 +        this.id = new GameId(first, second);
    1.51      }
    1.52  
    1.53 -    public Game(String id, String first, String second) {
    1.54 -        this.white = first;
    1.55 -        this.black = second;
    1.56 +    public Game(GameId id) {
    1.57          this.id = id;
    1.58      }
    1.59  
    1.60 -    public String getId() {
    1.61 +    public GameId getId() {
    1.62          return id;
    1.63      }
    1.64 -
    1.65 -    public String getWhite() {
    1.66 -        return white;
    1.67 -    }
    1.68 -
    1.69 -    public String getBlack() {
    1.70 -        return black;
    1.71 -    }
    1.72 -
    1.73 +    
    1.74      public Board getBoard() {
    1.75          if (board == null) {
    1.76              board = Board.empty();
    1.77 @@ -93,10 +80,10 @@
    1.78  
    1.79      public void apply(String player, Move m) throws IllegalPositionException {
    1.80          Player p = null;
    1.81 -        if (getWhite().equals(player)) {
    1.82 +        if (id.getWhite().equals(player)) {
    1.83              p = getBoard().getPlayers().get(0);
    1.84          } else {
    1.85 -            if (getBlack().equals(player)) {
    1.86 +            if (id.getBlack().equals(player)) {
    1.87                  p = getBoard().getPlayers().get(1);
    1.88              }
    1.89          }
    1.90 @@ -114,4 +101,38 @@
    1.91          }
    1.92          return moves;
    1.93      }
    1.94 +
    1.95 +    private static final class MoveAdapter extends XmlAdapter<String[],List<Move>> {
    1.96 +        @Override
    1.97 +        public List<Move> unmarshal(String[] arr) throws Exception {
    1.98 +            List<Move> res = new ArrayList<Move>();
    1.99 +            for (String v : arr) {
   1.100 +                res.add(Move.valueOf(v));
   1.101 +            }
   1.102 +            return res;
   1.103 +        }
   1.104 +
   1.105 +        @Override
   1.106 +        public String[] marshal(List<Move> arr) throws Exception {
   1.107 +            List<String> res = new ArrayList<String>();
   1.108 +            for (Move m : arr) {
   1.109 +                res.add(m.toString()); 
   1.110 +            }
   1.111 +            return res.toArray(new String[0]);
   1.112 +        }
   1.113 +    } // end of MoveAdapter
   1.114 +
   1.115 +    private static final class BoardAdapter extends XmlAdapter<String,Board> {
   1.116 +
   1.117 +        @Override
   1.118 +        public Board unmarshal(String v) throws Exception {
   1.119 +            return Board.valueOf(v);
   1.120 +        }
   1.121 +
   1.122 +        @Override
   1.123 +        public String marshal(Board v) throws Exception {
   1.124 +            return v.toString();
   1.125 +        }
   1.126 +
   1.127 +    }
   1.128  }