# HG changeset patch # User Jaroslav Tulach # Date 1284217491 -7200 # Node ID 1758a7727278dedde1f92b07a87d81447abce020 # Parent 9ecd02d694cd2967206b690b31a8b6f48e0642d4 Splitting classes representing data types on the server into own module diff -r 9ecd02d694cd -r 1758a7727278 pom.xml --- a/pom.xml Sun Sep 05 22:34:43 2010 +0200 +++ b/pom.xml Sat Sep 11 17:04:51 2010 +0200 @@ -35,6 +35,7 @@ 1.60 1.1 1.8 + 1.0 quoridor @@ -43,8 +44,9 @@ statistics freemarkerdor emailer + wsdor Quoridor related projects Master project that agregates all quoridor related functionality. - + \ No newline at end of file diff -r 9ecd02d694cd -r 1758a7727278 webidor/pom.xml --- a/webidor/pom.xml Sun Sep 05 22:34:43 2010 +0200 +++ b/webidor/pom.xml Sat Sep 11 17:04:51 2010 +0200 @@ -73,6 +73,11 @@ ${quoridorVersion} jar + + ${project.groupId} + wsdor + 1.0 + diff -r 9ecd02d694cd -r 1758a7727278 webidor/src/main/java/cz/xelfi/quoridor/webidor/CommentedMove.java --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/CommentedMove.java Sun Sep 05 22:34:43 2010 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,104 +0,0 @@ -package cz.xelfi.quoridor.webidor; - -import cz.xelfi.quoridor.IllegalPositionException; -import cz.xelfi.quoridor.Move; -import java.util.ArrayList; -import java.util.List; -import javax.xml.bind.annotation.XmlAttribute; -import javax.xml.bind.annotation.XmlElement; - -public final class CommentedMove { - Move move; - int index; - List comments; - - private CommentedMove() { - super(); - } - - CommentedMove(Move move, int index) { - super(); - this.move = move; - this.index = index; - } - - public Move getMove() { - return move; - } - - @XmlAttribute(name="move") - String getMoveText() { - return move.toString(); - } - - void setMoveText(String m) throws IllegalPositionException { - move = Move.valueOf(m); - } - - @XmlAttribute - int getIndex() { - return index; - } - - void setIndex(int i) { - index = i; - } - - void addNote(Note n) { - if (comments == null) { - comments = new ArrayList(); - } - comments.add(n); - } - - @XmlElement(name="comment", required=false) - public final List getComments() { - return comments; - } - - final void setComments(List l) { - this.comments = l; - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append("Move[").append(move).append("@").append(index); - if (comments != null) { - sb.append(", "); - sb.append(comments); - } - sb.append("]"); - return sb.toString(); - } - - @Override - public boolean equals(Object obj) { - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - final CommentedMove other = (CommentedMove) obj; - if (this.move != other.move && (this.move == null || !this.move.equals(other.move))) { - return false; - } - if (this.index != other.index) { - return false; - } - if (this.comments != other.comments && (this.comments == null || !this.comments.equals(other.comments))) { - return false; - } - return true; - } - - @Override - public int hashCode() { - int hash = 5; - hash = 43 * hash + (this.move != null ? this.move.hashCode() : 0); - hash = 43 * hash + this.index; - hash = 43 * hash + (this.comments != null ? this.comments.hashCode() : 0); - return hash; - } -} diff -r 9ecd02d694cd -r 1758a7727278 webidor/src/main/java/cz/xelfi/quoridor/webidor/Game.java --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/Game.java Sun Sep 05 22:34:43 2010 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,213 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * The contents of this file are subject to the terms of either the GNU - * General Public License Version 2 only ("GPL") or the Common - * Development and Distribution License("CDDL") (collectively, the - * "License"). You may not use this file except in compliance with the - * License. You can obtain a copy of the License at - * http://www.netbeans.org/cddl-gplv2.html - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the - * specific language governing permissions and limitations under the - * License. When distributing the software, include this License Header - * Notice in each file and include the License file at - * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this - * particular file as subject to the "Classpath" exception as provided - * by Sun in the GPL Version 2 section of the License file that - * accompanied this code. If applicable, add the following below the - * License Header, with the fields enclosed by brackets [] replaced by - * your own identifying information: - * "Portions Copyrighted [year] [name of copyright owner]" - * - * Contributor(s): - * - * Portions Copyrighted 2009 Jaroslav Tulach - */ - -package cz.xelfi.quoridor.webidor; - -import cz.xelfi.quoridor.Board; -import cz.xelfi.quoridor.IllegalPositionException; -import cz.xelfi.quoridor.Move; -import cz.xelfi.quoridor.Player; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; -import javax.xml.bind.annotation.XmlAccessType; -import javax.xml.bind.annotation.XmlAccessorType; -import javax.xml.bind.annotation.XmlAttribute; -import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlElementWrapper; -import javax.xml.bind.annotation.XmlRootElement; -import javax.xml.bind.annotation.adapters.XmlAdapter; -import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; - -/** - * - * @author Jaroslav Tulach - */ -@XmlRootElement -@XmlAccessorType(XmlAccessType.FIELD) -public final class Game extends Object { - @XmlElement - private GameId id; - @XmlElement - @XmlJavaTypeAdapter(BoardAdapter.class) - private Board board; - @XmlElementWrapper(name="moves") - @XmlElement(name="item") - private List moves; - private Integer move; - - Game() { - } - - public Game(String first, String second) { - this.id = new GameId(first, second); - this.board = Board.empty(); - } - - public Game(GameId id) { - this.id = id; - this.board = Board.empty(); - } - - public GameId getId() { - return id; - } - - public Board getBoard() { - return board; - } - - @XmlAttribute - final String getCurrentPlayer() { - Player w = board.getWinner(); - if(w==null){ - if (board.getCurrentPlayer() == board.getPlayers().get(0)) { - return id.getWhite(); - } else { - return id.getBlack(); - } - }else{ - if (w == board.getPlayers().get(0)) { - return id.getWhite(); - } else { - return id.getBlack(); - } - } - } - - public void apply(String player, Move m, Date when) throws IllegalPositionException { - Player p = null; - if (id.getWhite().equals(player)) { - p = getBoard().getPlayers().get(0); - } else { - if (id.getBlack().equals(player)) { - p = getBoard().getPlayers().get(1); - } - } - if (p != getBoard().getCurrentPlayer()) { - throw new IllegalArgumentException("Wrong player: " + player); - } - - board = getBoard().apply(m); - if (when == null) { - when = new Date(id.getModified()); - } - final GameStatus status = GameStatus.valueOf(board); - id = new GameId( - id.getId(), id.getWhite(), id.getBlack(), - new Date(id.getStarted()), when, status, - id.getComments(), !status.isInProgress()); - getMoves().add(new CommentedMove(m, getMoves().size() + 1)); - } - - public void comment(String player, String comment, Date date) { - Note n = new Note(comment, date, player); - id = new GameId( - id.getId(), id.getWhite(), id.getBlack(), - new Date(id.getStarted()), new Date(id.getModified()), - GameStatus.valueOf(board), id.getComments() + 1, id.isFinished() - ); - getMoves().get(getMoves().size() - 1).addNote(n); - } - - - public List getMoves() { - if (moves == null) { - moves = new ArrayList(); - } - return moves; - } - - @XmlAttribute - public int getCurrentMove() { - return move == null ? getMoves().size() : move; - } - - public Game snapshot(int move) throws IllegalPositionException { - Board b = Board.empty(); - int cnt = 0; - for (CommentedMove m : getMoves()) { - if (move-- <= 0) { - break; - } - b = b.apply(m.getMove()); - cnt++; - } - - Game g = new Game( - new GameId( - id.getId(), id.getWhite(), id.getBlack(), - new Date(id.getStarted()), new Date(id.getModified()), - GameStatus.history, id.getComments(), id.isFinished() - ) - ); - g.board = b; - g.move = cnt; - g.moves = new ArrayList(getMoves()); - return g; - } -/* - private static final class MoveAdapter extends XmlAdapter> { - @Override - public List unmarshal(CommentedMove[] arr) throws Exception { - List res = new ArrayList(); - if (arr != null) { - for (CommentedMove v : arr) { - res.add(Move.valueOf(v.move)); - } - } - return res; - } - - @Override - public CommentedMove[] marshal(List arr) throws Exception { - List res = new ArrayList(); - if (arr != null) { - int index = 0; - for (Move m : arr) { - res.add(new CommentedMove(m.toString(), ++index)); - } - } - return res.toArray(new CommentedMove[0]); - } - } // end of MoveAdapter -*/ - private static final class BoardAdapter extends XmlAdapter { - - @Override - public Board unmarshal(String v) throws Exception { - return v == null ? null : Board.valueOf(v); -// return v == null ? null : new Board(v); - } - - @Override - public String marshal(Board v) throws Exception { - return v == null ? null : v.toString(); -// return v == null ? null : Board.board2HashCode(v); - } - - } -} diff -r 9ecd02d694cd -r 1758a7727278 webidor/src/main/java/cz/xelfi/quoridor/webidor/GameId.java --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/GameId.java Sun Sep 05 22:34:43 2010 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,136 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * The contents of this file are subject to the terms of either the GNU - * General Public License Version 2 only ("GPL") or the Common - * Development and Distribution License("CDDL") (collectively, the - * "License"). You may not use this file except in compliance with the - * License. You can obtain a copy of the License at - * http://www.netbeans.org/cddl-gplv2.html - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the - * specific language governing permissions and limitations under the - * License. When distributing the software, include this License Header - * Notice in each file and include the License file at - * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this - * particular file as subject to the "Classpath" exception as provided - * by Sun in the GPL Version 2 section of the License file that - * accompanied this code. If applicable, add the following below the - * License Header, with the fields enclosed by brackets [] replaced by - * your own identifying information: - * "Portions Copyrighted [year] [name of copyright owner]" - * - * Contributor(s): - * - * Portions Copyrighted 2009 Jaroslav Tulach - */ - -package cz.xelfi.quoridor.webidor; - -import java.util.Comparator; -import java.util.Date; -import java.util.UUID; -import javax.xml.bind.annotation.XmlAccessType; -import javax.xml.bind.annotation.XmlAccessorType; -import javax.xml.bind.annotation.XmlAttribute; -import javax.xml.bind.annotation.XmlID; -import javax.xml.bind.annotation.XmlRootElement; - -/** Basic identification of a game. - * - * @author Jaroslav Tulach - */ -@XmlRootElement -@XmlAccessorType(XmlAccessType.FIELD) -public class GameId { - public static final Comparator NEWEST_FIRST = new NewestFirst(); - - @XmlAttribute - private String white; - @XmlAttribute - private String black; - @XmlAttribute - private long started; - @XmlAttribute - private long modified; - @XmlAttribute - private GameStatus status; - @XmlID @XmlAttribute - private String id; - @XmlAttribute - private int comments; - @XmlAttribute - private boolean finished; - - GameId() { - } - - public GameId(String first, String second) { - this(first, second, new Date()); - } - private GameId(String first, String second, Date d) { - this( - UUID.randomUUID().toString(), - first, second, d, d, GameStatus.whiteMove, 0, false - ); - } - - public GameId( - String id, String first, String second, - Date started, Date last, GameStatus result, - int comments, boolean finished - ) { - this.white = first; - this.black = second; - this.id = id; - this.started = started.getTime(); - this.modified = last.getTime(); - this.status = result; - this.comments = comments; - this.finished = finished; - } - - public String getId() { - return id; - } - - public String getWhite() { - return white; - } - - public String getBlack() { - return black; - } - - public long getStarted() { - return started; - } - - public long getModified() { - return modified; - } - - public GameStatus getStatus() { - return status; - } - - public int getComments() { - return comments; - } - - public boolean isFinished() { - return finished; - } - - private static final class NewestFirst implements Comparator { - public int compare(GameId o1, GameId o2) { - if (o1 == o2) { - return 0; - } - long diff = o2.getModified() - o1.getModified(); - if (diff != 0) { - return diff < 0 ? -1 : 1; - } - return o1.getId().compareTo(o2.getId()); - } - } -} diff -r 9ecd02d694cd -r 1758a7727278 webidor/src/main/java/cz/xelfi/quoridor/webidor/GameStatus.java --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/GameStatus.java Sun Sep 05 22:34:43 2010 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,61 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * The contents of this file are subject to the terms of either the GNU - * General Public License Version 2 only ("GPL") or the Common - * Development and Distribution License("CDDL") (collectively, the - * "License"). You may not use this file except in compliance with the - * License. You can obtain a copy of the License at - * http://www.netbeans.org/cddl-gplv2.html - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the - * specific language governing permissions and limitations under the - * License. When distributing the software, include this License Header - * Notice in each file and include the License file at - * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this - * particular file as subject to the "Classpath" exception as provided - * by Sun in the GPL Version 2 section of the License file that - * accompanied this code. If applicable, add the following below the - * License Header, with the fields enclosed by brackets [] replaced by - * your own identifying information: - * "Portions Copyrighted [year] [name of copyright owner]" - * - * Contributor(s): - * - * Portions Copyrighted 2009 Jaroslav Tulach - */ - -package cz.xelfi.quoridor.webidor; - -import cz.xelfi.quoridor.Board; - -/** Possible states of the game. - * - * @author Jaroslav Tulach - */ -public enum GameStatus { - whiteMove, - blackMove, - whiteWon, - blackWon, - history; - - /** Creates appropriate status of the game based on the state - * on the board. - * - * @param board the board - * @return status describing the situation on the board - */ - public static GameStatus valueOf(Board board) { - if (board.getWinner() != null) { - return board.getWinner() == board.getPlayers().get(0) ? GameStatus.whiteWon : GameStatus.blackWon; - } else { - return board.getCurrentPlayer() == board.getPlayers().get(0) ? GameStatus.whiteMove : GameStatus.blackMove; - } - } - - /** @return true if the game is in progress - */ - public boolean isInProgress() { - return this == whiteMove || this == blackMove; - } -} diff -r 9ecd02d694cd -r 1758a7727278 webidor/src/main/java/cz/xelfi/quoridor/webidor/JAXBContextResolver.java --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/JAXBContextResolver.java Sun Sep 05 22:34:43 2010 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,70 +0,0 @@ -/* - * - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. - * - * The contents of this file are subject to the terms of either the GNU - * General Public License Version 2 only ("GPL") or the Common Development - * and Distribution License("CDDL") (collectively, the "License"). You - * may not use this file except in compliance with the License. You can obtain - * a copy of the License at https://jersey.dev.java.net/CDDL+GPL.html - * or jersey/legal/LICENSE.txt. See the License for the specific - * language governing permissions and limitations under the License. - * - * When distributing the software, include this License Header Notice in each - * file and include the License file at jersey/legal/LICENSE.txt. - * Sun designates this particular file as subject to the "Classpath" exception - * as provided by Sun in the GPL Version 2 section of the License file that - * accompanied this code. If applicable, add the following below the License - * Header, with the fields enclosed by brackets [] replaced by your own - * identifying information: "Portions Copyrighted [year] - * [name of copyright owner]" - * - * Contributor(s): - * - * If you wish your version of this file to be governed by only the CDDL or - * only the GPL Version 2, indicate your decision by adding "[Contributor] - * elects to include this software in this distribution under the [CDDL or GPL - * Version 2] license." If you don't indicate a single choice of license, a - * recipient has the option to distribute your version of this file under - * either the CDDL, the GPL Version 2 or to extend the choice of license to - * its licensees as provided above. However, if you add GPL Version 2 code - * and therefore, elected the GPL Version 2 license, then the option applies - * only if the new code is made subject to such option by the copyright - * holder. - */ - -package cz.xelfi.quoridor.webidor; - -import com.sun.jersey.api.json.JSONConfiguration; -import com.sun.jersey.api.json.JSONJAXBContext; -import java.util.Arrays; -import java.util.HashSet; -import java.util.Set; -import javax.ws.rs.ext.ContextResolver; -import javax.ws.rs.ext.Provider; -import javax.xml.bind.JAXBContext; - -/** - * - * @author japod - */ -@Provider -public final class JAXBContextResolver implements ContextResolver { - - private final JAXBContext context; - - private final Set types; - - private final Class[] cTypes = {Game.class, GameId.class, GameStatus.class }; - - public JAXBContextResolver() throws Exception { - this.types = new HashSet(Arrays.asList(cTypes)); - this.context = new JSONJAXBContext(JSONConfiguration.natural().build(), cTypes); - } - - public JAXBContext getContext(Class objectType) { - return (types.contains(objectType)) ? context : null; - } -} \ No newline at end of file diff -r 9ecd02d694cd -r 1758a7727278 webidor/src/main/java/cz/xelfi/quoridor/webidor/Note.java --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/Note.java Sun Sep 05 22:34:43 2010 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,79 +0,0 @@ -package cz.xelfi.quoridor.webidor; - -import java.util.Date; -import javax.xml.bind.annotation.XmlAccessType; -import javax.xml.bind.annotation.XmlAccessorType; -import javax.xml.bind.annotation.XmlAttribute; -import javax.xml.bind.annotation.XmlValue; - -/** - * - * @author Jaroslav Tulach - */ -@XmlAccessorType(XmlAccessType.FIELD) -public class Note { - @XmlValue - private String comment; - @XmlAttribute - private Date when; - @XmlAttribute - private String who; - - private Note() { - } - - public Note(String comment, Date when, String who) { - this.comment = comment; - this.when = new Date(when.getTime()); - this.who = who; - } - - public String getComment() { - return comment; - } - - public Date getWhen() { - return (Date) when.clone(); - } - - public String getWho() { - return who; - } - - @Override - public String toString() { - return "Note[" + who + "@" + when + ": " + comment + "]"; - } - - @Override - public boolean equals(Object obj) { - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - final Note other = (Note) obj; - if ((this.comment == null) ? (other.comment != null) : !this.comment.equals(other.comment)) { - return false; - } - if (this.when != other.when && (this.when == null || !this.when.equals(other.when))) { - return false; - } - if ((this.who == null) ? (other.who != null) : !this.who.equals(other.who)) { - return false; - } - return true; - } - - @Override - public int hashCode() { - int hash = 7; - hash = 59 * hash + (this.comment != null ? this.comment.hashCode() : 0); - hash = 59 * hash + (this.when != null ? this.when.hashCode() : 0); - hash = 59 * hash + (this.who != null ? this.who.hashCode() : 0); - return hash; - } - - -} diff -r 9ecd02d694cd -r 1758a7727278 webidor/src/main/java/cz/xelfi/quoridor/webidor/User.java --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/User.java Sun Sep 05 22:34:43 2010 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,128 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * The contents of this file are subject to the terms of either the GNU - * General Public License Version 2 only ("GPL") or the Common - * Development and Distribution License("CDDL") (collectively, the - * "License"). You may not use this file except in compliance with the - * License. You can obtain a copy of the License at - * http://www.netbeans.org/cddl-gplv2.html - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the - * specific language governing permissions and limitations under the - * License. When distributing the software, include this License Header - * Notice in each file and include the License file at - * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this - * particular file as subject to the "Classpath" exception as provided - * by Sun in the GPL Version 2 section of the License file that - * accompanied this code. If applicable, add the following below the - * License Header, with the fields enclosed by brackets [] replaced by - * your own identifying information: - * "Portions Copyrighted [year] [name of copyright owner]" - * - * Contributor(s): - * - * Portions Copyrighted 2009 Jaroslav Tulach - */ - -package cz.xelfi.quoridor.webidor; - -import java.util.ArrayList; -import java.util.List; -import java.util.Set; -import java.util.TreeSet; -import javax.xml.bind.annotation.XmlAccessType; -import javax.xml.bind.annotation.XmlAccessorType; -import javax.xml.bind.annotation.XmlAttribute; -import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlRootElement; -import javax.xml.bind.annotation.XmlValue; - -/** - * - * @author Jaroslav Tulach - */ -@XmlRootElement -@XmlAccessorType(XmlAccessType.NONE) -public final class User extends Object { - @XmlAttribute - private String id; - @XmlElement(name="property") - private List properties; - private transient Set permissions; - - User() { - } - - public User(String id) { - this.id = id; - } - - public void addProperty(String name, String value) { - if (properties == null) { - properties = new ArrayList(); - } - properties.add(new Property(name, value)); - } - - public void addPermission(String permission) { - if (permissions == null) { - permissions = new TreeSet(); - } - permissions.add(permission); - } - - public String getProperty(String name) { - if (properties == null) { - return null; - } - for (Property p : properties) { - if (p.name.equals(name)) { - return p.value; - } - } - return null; - } - - public boolean hasPermission(String permission) { - if (permissions == null) { - return false; - } - return permissions.contains(permission); - } - - public static boolean canSee(GameId gId, String userId) { - if (!gId.isFinished()) { - return true; - } - if (userId.equals(gId.getWhite())) { - return true; - } - if (userId.equals(gId.getBlack())) { - return true; - } - return false; - } - - public boolean canSee(GameId gId){ - return canSee(gId, id); - } - - public String getId() { - return id; - } - - public static final class Property { - @XmlAttribute - private String name; - @XmlValue - private String value; - - private Property() { - } - - Property(String name, String value) { - this.name = name; - this.value = value; - } - } -} diff -r 9ecd02d694cd -r 1758a7727278 wsdor/pom.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/wsdor/pom.xml Sat Sep 11 17:04:51 2010 +0200 @@ -0,0 +1,91 @@ + + + 4.0.0 + + all-quoridor + cz.xelfi.quoridor + 1.0 + + cz.xelfi.quoridor + wsdor + bundle + ${wsdorVersion} + wsdor + http://maven.apache.org + + + + org.apache.felix + maven-bundle-plugin + 2.0.1 + true + + + cz.xelfi.quoridor.webidor + cz.xelfi.quoridor.webidor.* + + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.0.2 + + 1.5 + 1.5 + + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.5 + + + + + + + junit + junit + 3.8.1 + test + + + ${project.groupId} + quoridor + ${quoridorVersion} + + + com.sun.jersey + jersey-core + 1.1.0-ea + + + com.sun.jersey + jersey-server + 1.1.0-ea + + + com.sun.jersey + jersey-json + 1.1.0-ea + jar + + + jaxb-api + javax.xml.bind + + + stax-api + stax + + + + + diff -r 9ecd02d694cd -r 1758a7727278 wsdor/src/main/java/cz/xelfi/quoridor/webidor/CommentedMove.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/wsdor/src/main/java/cz/xelfi/quoridor/webidor/CommentedMove.java Sat Sep 11 17:04:51 2010 +0200 @@ -0,0 +1,104 @@ +package cz.xelfi.quoridor.webidor; + +import cz.xelfi.quoridor.IllegalPositionException; +import cz.xelfi.quoridor.Move; +import java.util.ArrayList; +import java.util.List; +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlElement; + +public final class CommentedMove { + Move move; + int index; + List comments; + + private CommentedMove() { + super(); + } + + CommentedMove(Move move, int index) { + super(); + this.move = move; + this.index = index; + } + + public Move getMove() { + return move; + } + + @XmlAttribute(name="move") + String getMoveText() { + return move.toString(); + } + + void setMoveText(String m) throws IllegalPositionException { + move = Move.valueOf(m); + } + + @XmlAttribute + int getIndex() { + return index; + } + + void setIndex(int i) { + index = i; + } + + void addNote(Note n) { + if (comments == null) { + comments = new ArrayList(); + } + comments.add(n); + } + + @XmlElement(name="comment", required=false) + public final List getComments() { + return comments; + } + + final void setComments(List l) { + this.comments = l; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("Move[").append(move).append("@").append(index); + if (comments != null) { + sb.append(", "); + sb.append(comments); + } + sb.append("]"); + return sb.toString(); + } + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final CommentedMove other = (CommentedMove) obj; + if (this.move != other.move && (this.move == null || !this.move.equals(other.move))) { + return false; + } + if (this.index != other.index) { + return false; + } + if (this.comments != other.comments && (this.comments == null || !this.comments.equals(other.comments))) { + return false; + } + return true; + } + + @Override + public int hashCode() { + int hash = 5; + hash = 43 * hash + (this.move != null ? this.move.hashCode() : 0); + hash = 43 * hash + this.index; + hash = 43 * hash + (this.comments != null ? this.comments.hashCode() : 0); + return hash; + } +} diff -r 9ecd02d694cd -r 1758a7727278 wsdor/src/main/java/cz/xelfi/quoridor/webidor/Game.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/wsdor/src/main/java/cz/xelfi/quoridor/webidor/Game.java Sat Sep 11 17:04:51 2010 +0200 @@ -0,0 +1,213 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * Portions Copyrighted 2009 Jaroslav Tulach + */ + +package cz.xelfi.quoridor.webidor; + +import cz.xelfi.quoridor.Board; +import cz.xelfi.quoridor.IllegalPositionException; +import cz.xelfi.quoridor.Move; +import cz.xelfi.quoridor.Player; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlElementWrapper; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.adapters.XmlAdapter; +import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; + +/** + * + * @author Jaroslav Tulach + */ +@XmlRootElement +@XmlAccessorType(XmlAccessType.FIELD) +public final class Game extends Object { + @XmlElement + private GameId id; + @XmlElement + @XmlJavaTypeAdapter(BoardAdapter.class) + private Board board; + @XmlElementWrapper(name="moves") + @XmlElement(name="item") + private List moves; + private Integer move; + + Game() { + } + + public Game(String first, String second) { + this.id = new GameId(first, second); + this.board = Board.empty(); + } + + public Game(GameId id) { + this.id = id; + this.board = Board.empty(); + } + + public GameId getId() { + return id; + } + + public Board getBoard() { + return board; + } + + @XmlAttribute + final String getCurrentPlayer() { + Player w = board.getWinner(); + if(w==null){ + if (board.getCurrentPlayer() == board.getPlayers().get(0)) { + return id.getWhite(); + } else { + return id.getBlack(); + } + }else{ + if (w == board.getPlayers().get(0)) { + return id.getWhite(); + } else { + return id.getBlack(); + } + } + } + + public void apply(String player, Move m, Date when) throws IllegalPositionException { + Player p = null; + if (id.getWhite().equals(player)) { + p = getBoard().getPlayers().get(0); + } else { + if (id.getBlack().equals(player)) { + p = getBoard().getPlayers().get(1); + } + } + if (p != getBoard().getCurrentPlayer()) { + throw new IllegalArgumentException("Wrong player: " + player); + } + + board = getBoard().apply(m); + if (when == null) { + when = new Date(id.getModified()); + } + final GameStatus status = GameStatus.valueOf(board); + id = new GameId( + id.getId(), id.getWhite(), id.getBlack(), + new Date(id.getStarted()), when, status, + id.getComments(), !status.isInProgress()); + getMoves().add(new CommentedMove(m, getMoves().size() + 1)); + } + + public void comment(String player, String comment, Date date) { + Note n = new Note(comment, date, player); + id = new GameId( + id.getId(), id.getWhite(), id.getBlack(), + new Date(id.getStarted()), new Date(id.getModified()), + GameStatus.valueOf(board), id.getComments() + 1, id.isFinished() + ); + getMoves().get(getMoves().size() - 1).addNote(n); + } + + + public List getMoves() { + if (moves == null) { + moves = new ArrayList(); + } + return moves; + } + + @XmlAttribute + public int getCurrentMove() { + return move == null ? getMoves().size() : move; + } + + public Game snapshot(int move) throws IllegalPositionException { + Board b = Board.empty(); + int cnt = 0; + for (CommentedMove m : getMoves()) { + if (move-- <= 0) { + break; + } + b = b.apply(m.getMove()); + cnt++; + } + + Game g = new Game( + new GameId( + id.getId(), id.getWhite(), id.getBlack(), + new Date(id.getStarted()), new Date(id.getModified()), + GameStatus.history, id.getComments(), id.isFinished() + ) + ); + g.board = b; + g.move = cnt; + g.moves = new ArrayList(getMoves()); + return g; + } +/* + private static final class MoveAdapter extends XmlAdapter> { + @Override + public List unmarshal(CommentedMove[] arr) throws Exception { + List res = new ArrayList(); + if (arr != null) { + for (CommentedMove v : arr) { + res.add(Move.valueOf(v.move)); + } + } + return res; + } + + @Override + public CommentedMove[] marshal(List arr) throws Exception { + List res = new ArrayList(); + if (arr != null) { + int index = 0; + for (Move m : arr) { + res.add(new CommentedMove(m.toString(), ++index)); + } + } + return res.toArray(new CommentedMove[0]); + } + } // end of MoveAdapter +*/ + private static final class BoardAdapter extends XmlAdapter { + + @Override + public Board unmarshal(String v) throws Exception { + return v == null ? null : Board.valueOf(v); +// return v == null ? null : new Board(v); + } + + @Override + public String marshal(Board v) throws Exception { + return v == null ? null : v.toString(); +// return v == null ? null : Board.board2HashCode(v); + } + + } +} diff -r 9ecd02d694cd -r 1758a7727278 wsdor/src/main/java/cz/xelfi/quoridor/webidor/GameId.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/wsdor/src/main/java/cz/xelfi/quoridor/webidor/GameId.java Sat Sep 11 17:04:51 2010 +0200 @@ -0,0 +1,136 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * Portions Copyrighted 2009 Jaroslav Tulach + */ + +package cz.xelfi.quoridor.webidor; + +import java.util.Comparator; +import java.util.Date; +import java.util.UUID; +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlID; +import javax.xml.bind.annotation.XmlRootElement; + +/** Basic identification of a game. + * + * @author Jaroslav Tulach + */ +@XmlRootElement +@XmlAccessorType(XmlAccessType.FIELD) +public class GameId { + public static final Comparator NEWEST_FIRST = new NewestFirst(); + + @XmlAttribute + private String white; + @XmlAttribute + private String black; + @XmlAttribute + private long started; + @XmlAttribute + private long modified; + @XmlAttribute + private GameStatus status; + @XmlID @XmlAttribute + private String id; + @XmlAttribute + private int comments; + @XmlAttribute + private boolean finished; + + GameId() { + } + + public GameId(String first, String second) { + this(first, second, new Date()); + } + private GameId(String first, String second, Date d) { + this( + UUID.randomUUID().toString(), + first, second, d, d, GameStatus.whiteMove, 0, false + ); + } + + public GameId( + String id, String first, String second, + Date started, Date last, GameStatus result, + int comments, boolean finished + ) { + this.white = first; + this.black = second; + this.id = id; + this.started = started.getTime(); + this.modified = last.getTime(); + this.status = result; + this.comments = comments; + this.finished = finished; + } + + public String getId() { + return id; + } + + public String getWhite() { + return white; + } + + public String getBlack() { + return black; + } + + public long getStarted() { + return started; + } + + public long getModified() { + return modified; + } + + public GameStatus getStatus() { + return status; + } + + public int getComments() { + return comments; + } + + public boolean isFinished() { + return finished; + } + + private static final class NewestFirst implements Comparator { + public int compare(GameId o1, GameId o2) { + if (o1 == o2) { + return 0; + } + long diff = o2.getModified() - o1.getModified(); + if (diff != 0) { + return diff < 0 ? -1 : 1; + } + return o1.getId().compareTo(o2.getId()); + } + } +} diff -r 9ecd02d694cd -r 1758a7727278 wsdor/src/main/java/cz/xelfi/quoridor/webidor/GameStatus.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/wsdor/src/main/java/cz/xelfi/quoridor/webidor/GameStatus.java Sat Sep 11 17:04:51 2010 +0200 @@ -0,0 +1,61 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * Portions Copyrighted 2009 Jaroslav Tulach + */ + +package cz.xelfi.quoridor.webidor; + +import cz.xelfi.quoridor.Board; + +/** Possible states of the game. + * + * @author Jaroslav Tulach + */ +public enum GameStatus { + whiteMove, + blackMove, + whiteWon, + blackWon, + history; + + /** Creates appropriate status of the game based on the state + * on the board. + * + * @param board the board + * @return status describing the situation on the board + */ + public static GameStatus valueOf(Board board) { + if (board.getWinner() != null) { + return board.getWinner() == board.getPlayers().get(0) ? GameStatus.whiteWon : GameStatus.blackWon; + } else { + return board.getCurrentPlayer() == board.getPlayers().get(0) ? GameStatus.whiteMove : GameStatus.blackMove; + } + } + + /** @return true if the game is in progress + */ + public boolean isInProgress() { + return this == whiteMove || this == blackMove; + } +} diff -r 9ecd02d694cd -r 1758a7727278 wsdor/src/main/java/cz/xelfi/quoridor/webidor/JAXBContextResolver.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/wsdor/src/main/java/cz/xelfi/quoridor/webidor/JAXBContextResolver.java Sat Sep 11 17:04:51 2010 +0200 @@ -0,0 +1,70 @@ +/* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common Development + * and Distribution License("CDDL") (collectively, the "License"). You + * may not use this file except in compliance with the License. You can obtain + * a copy of the License at https://jersey.dev.java.net/CDDL+GPL.html + * or jersey/legal/LICENSE.txt. See the License for the specific + * language governing permissions and limitations under the License. + * + * When distributing the software, include this License Header Notice in each + * file and include the License file at jersey/legal/LICENSE.txt. + * Sun designates this particular file as subject to the "Classpath" exception + * as provided by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the License + * Header, with the fields enclosed by brackets [] replaced by your own + * identifying information: "Portions Copyrighted [year] + * [name of copyright owner]" + * + * Contributor(s): + * + * If you wish your version of this file to be governed by only the CDDL or + * only the GPL Version 2, indicate your decision by adding "[Contributor] + * elects to include this software in this distribution under the [CDDL or GPL + * Version 2] license." If you don't indicate a single choice of license, a + * recipient has the option to distribute your version of this file under + * either the CDDL, the GPL Version 2 or to extend the choice of license to + * its licensees as provided above. However, if you add GPL Version 2 code + * and therefore, elected the GPL Version 2 license, then the option applies + * only if the new code is made subject to such option by the copyright + * holder. + */ + +package cz.xelfi.quoridor.webidor; + +import com.sun.jersey.api.json.JSONConfiguration; +import com.sun.jersey.api.json.JSONJAXBContext; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; +import javax.ws.rs.ext.ContextResolver; +import javax.ws.rs.ext.Provider; +import javax.xml.bind.JAXBContext; + +/** + * + * @author japod + */ +@Provider +public final class JAXBContextResolver implements ContextResolver { + + private final JAXBContext context; + + private final Set types; + + private final Class[] cTypes = {Game.class, GameId.class, GameStatus.class }; + + public JAXBContextResolver() throws Exception { + this.types = new HashSet(Arrays.asList(cTypes)); + this.context = new JSONJAXBContext(JSONConfiguration.natural().build(), cTypes); + } + + public JAXBContext getContext(Class objectType) { + return (types.contains(objectType)) ? context : null; + } +} \ No newline at end of file diff -r 9ecd02d694cd -r 1758a7727278 wsdor/src/main/java/cz/xelfi/quoridor/webidor/Note.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/wsdor/src/main/java/cz/xelfi/quoridor/webidor/Note.java Sat Sep 11 17:04:51 2010 +0200 @@ -0,0 +1,79 @@ +package cz.xelfi.quoridor.webidor; + +import java.util.Date; +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlValue; + +/** + * + * @author Jaroslav Tulach + */ +@XmlAccessorType(XmlAccessType.FIELD) +public class Note { + @XmlValue + private String comment; + @XmlAttribute + private Date when; + @XmlAttribute + private String who; + + private Note() { + } + + public Note(String comment, Date when, String who) { + this.comment = comment; + this.when = new Date(when.getTime()); + this.who = who; + } + + public String getComment() { + return comment; + } + + public Date getWhen() { + return (Date) when.clone(); + } + + public String getWho() { + return who; + } + + @Override + public String toString() { + return "Note[" + who + "@" + when + ": " + comment + "]"; + } + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final Note other = (Note) obj; + if ((this.comment == null) ? (other.comment != null) : !this.comment.equals(other.comment)) { + return false; + } + if (this.when != other.when && (this.when == null || !this.when.equals(other.when))) { + return false; + } + if ((this.who == null) ? (other.who != null) : !this.who.equals(other.who)) { + return false; + } + return true; + } + + @Override + public int hashCode() { + int hash = 7; + hash = 59 * hash + (this.comment != null ? this.comment.hashCode() : 0); + hash = 59 * hash + (this.when != null ? this.when.hashCode() : 0); + hash = 59 * hash + (this.who != null ? this.who.hashCode() : 0); + return hash; + } + + +} diff -r 9ecd02d694cd -r 1758a7727278 wsdor/src/main/java/cz/xelfi/quoridor/webidor/User.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/wsdor/src/main/java/cz/xelfi/quoridor/webidor/User.java Sat Sep 11 17:04:51 2010 +0200 @@ -0,0 +1,128 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * Portions Copyrighted 2009 Jaroslav Tulach + */ + +package cz.xelfi.quoridor.webidor; + +import java.util.ArrayList; +import java.util.List; +import java.util.Set; +import java.util.TreeSet; +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.XmlValue; + +/** + * + * @author Jaroslav Tulach + */ +@XmlRootElement +@XmlAccessorType(XmlAccessType.NONE) +public final class User extends Object { + @XmlAttribute + private String id; + @XmlElement(name="property") + private List properties; + private transient Set permissions; + + User() { + } + + public User(String id) { + this.id = id; + } + + public void addProperty(String name, String value) { + if (properties == null) { + properties = new ArrayList(); + } + properties.add(new Property(name, value)); + } + + public void addPermission(String permission) { + if (permissions == null) { + permissions = new TreeSet(); + } + permissions.add(permission); + } + + public String getProperty(String name) { + if (properties == null) { + return null; + } + for (Property p : properties) { + if (p.name.equals(name)) { + return p.value; + } + } + return null; + } + + public boolean hasPermission(String permission) { + if (permissions == null) { + return false; + } + return permissions.contains(permission); + } + + public static boolean canSee(GameId gId, String userId) { + if (!gId.isFinished()) { + return true; + } + if (userId.equals(gId.getWhite())) { + return true; + } + if (userId.equals(gId.getBlack())) { + return true; + } + return false; + } + + public boolean canSee(GameId gId){ + return canSee(gId, id); + } + + public String getId() { + return id; + } + + public static final class Property { + @XmlAttribute + private String name; + @XmlValue + private String value; + + private Property() { + } + + Property(String name, String value) { + this.name = name; + this.value = value; + } + } +}