diff --git a/src/java/org/jivesoftware/spark/ui/ShakeWindow.java b/src/java/org/jivesoftware/spark/ui/ShakeWindow.java
index 6ae72442..2bfeeb86 100644
--- a/src/java/org/jivesoftware/spark/ui/ShakeWindow.java
+++ b/src/java/org/jivesoftware/spark/ui/ShakeWindow.java
@@ -24,7 +24,9 @@ import org.jivesoftware.spark.SparkManager;
import javax.swing.Timer;
import javax.swing.JFrame;
+import java.awt.Dimension;
import java.awt.Point;
+import java.awt.Toolkit;
import java.awt.Window;
import java.awt.Frame;
import java.awt.event.ActionEvent;
@@ -41,6 +43,7 @@ public class ShakeWindow {
private Point naturalLocation;
private long startTime;
private Timer shakeTimer;
+ private Timer moveTimer;
private final double TWO_PI = Math.PI * 2.0;
private boolean added = false;
@@ -105,6 +108,47 @@ public class ShakeWindow {
SparkManager.getNativeManager().stopFlashing(window);
}
+
+ /**
+ * punishes the User by moving the Chatwindow around for 10 seconds
+ */
+ public void startRandomMovement(final int seconds)
+ {
+ if(window instanceof JFrame){
+ JFrame f = (JFrame)window;
+ f.setState(Frame.NORMAL);
+ f.setVisible(true);
+ }
+ SparkManager.getNativeManager().flashWindow(window);
+
+ final long startTime = System.currentTimeMillis()/1000L;
+
+ moveTimer = new Timer(5, new ActionListener() {
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
+
+ double x = Math.random()*10000 % d.getWidth();
+ double y = Math.random()*10000 % d.getHeight();
+ int xx = Math.round(Math.round(x));
+ int yy = Math.round(Math.round(y));
+ window.setLocation(xx,yy);
+ window.repaint();
+
+ long now = System.currentTimeMillis()/1000L;
+ long diff = now-startTime;
+ System.out.println(diff);
+ if(diff > seconds)
+ {
+ moveTimer.stop();
+ }
+
+ }
+ });
+
+ moveTimer.start();
+
+ }
}
diff --git a/src/java/org/jivesoftware/spark/ui/rooms/ChatRoomImpl.java b/src/java/org/jivesoftware/spark/ui/rooms/ChatRoomImpl.java
index 7d01ab52..c6963290 100644
--- a/src/java/org/jivesoftware/spark/ui/rooms/ChatRoomImpl.java
+++ b/src/java/org/jivesoftware/spark/ui/rooms/ChatRoomImpl.java
@@ -357,7 +357,14 @@ public class ChatRoomImpl extends ChatRoom {
return true;
}
-
+ /**
+ * Returns the Bare-Participant JID
+ *
+ * user@server.com
+ * for retrieving the full Jid use ChatRoomImpl.getJID()
+ *
+ * @return
+ */
public String getParticipantJID() {
return participantJID;
}
diff --git a/src/plugins/tictactoe/src/java/tic/tac/toe/GameBoard.java b/src/plugins/tictactoe/src/java/tic/tac/toe/GameBoard.java
index c840fa96..42c11890 100644
--- a/src/plugins/tictactoe/src/java/tic/tac/toe/GameBoard.java
+++ b/src/plugins/tictactoe/src/java/tic/tac/toe/GameBoard.java
@@ -204,4 +204,15 @@ public class GameBoard {
return null;
}
+
+ /**
+ * Checks if the palced move is Valid
+ * @param x
+ * @param y
+ * @return
+ */
+ public boolean isValidMove(Mark markplaced, int x , int y)
+ {
+ return _board[x][y] == 0 && _currentPlayer == markplaced.getValue();
+ }
}
diff --git a/src/plugins/tictactoe/src/java/tic/tac/toe/TicTacToePlugin.java b/src/plugins/tictactoe/src/java/tic/tac/toe/TicTacToePlugin.java
index 0909a0eb..0e70b68c 100644
--- a/src/plugins/tictactoe/src/java/tic/tac/toe/TicTacToePlugin.java
+++ b/src/plugins/tictactoe/src/java/tic/tac/toe/TicTacToePlugin.java
@@ -47,6 +47,7 @@ import org.jivesoftware.spark.ui.ChatRoomListener;
import org.jivesoftware.spark.ui.ChatRoomListenerAdapter;
import org.jivesoftware.spark.ui.rooms.ChatRoomImpl;
import tic.tac.toe.packet.GameOfferPacket;
+import tic.tac.toe.packet.InvalidMove;
import tic.tac.toe.packet.MovePacket;
import tic.tac.toe.ui.GamePanel;
@@ -77,6 +78,7 @@ public class TicTacToePlugin implements Plugin {
ProviderManager.getInstance().addIQProvider(GameOfferPacket.ELEMENT_NAME, GameOfferPacket.NAMESPACE,GameOfferPacket.class);
ProviderManager.getInstance().addExtensionProvider(MovePacket.ELEMENT_NAME, MovePacket.NAMESPACE, MovePacket.class);
+ ProviderManager.getInstance().addExtensionProvider(InvalidMove.ELEMENT_NAME, InvalidMove.NAMESPACE, InvalidMove.class);
// Add IQ listener to listen for incoming game invitations.
_gameOfferListener = new PacketListener() {
@@ -166,6 +168,14 @@ public class TicTacToePlugin implements Plugin {
});
}
+
+ @Override
+ public void chatRoomClosed(ChatRoom room) {
+ if (room instanceof ChatRoomImpl) {
+ ChatRoomImpl cri = (ChatRoomImpl) room;
+ _currentInvitations.remove(cri.getParticipantJID());
+ }
+ }
};
diff --git a/src/plugins/tictactoe/src/java/tic/tac/toe/packet/InvalidMove.java b/src/plugins/tictactoe/src/java/tic/tac/toe/packet/InvalidMove.java
new file mode 100644
index 00000000..347d4ed0
--- /dev/null
+++ b/src/plugins/tictactoe/src/java/tic/tac/toe/packet/InvalidMove.java
@@ -0,0 +1,63 @@
+package tic.tac.toe.packet;
+
+import org.jivesoftware.smack.packet.PacketExtension;
+
+public class InvalidMove implements PacketExtension {
+
+ public static final String ELEMENT_NAME = "ttt-invalid";
+ public static final String NAMESPACE = "tictactoe";
+
+ private int _gameID;
+ private int _posx;
+ private int _posy;
+
+ @Override
+ public String getElementName() {
+ return ELEMENT_NAME;
+ }
+
+ @Override
+ public String getNamespace() {
+ return NAMESPACE;
+ }
+
+ public int getGameID() {
+ return _gameID;
+ }
+
+ public void setGameID(int gameID) {
+ _gameID = gameID;
+ }
+
+ public int getPositionX() {
+ return _posx;
+ }
+
+ public int getPositionY() {
+ return _posy;
+ }
+
+ public void setPositionX(int x) {
+ _posx = x;
+ }
+
+ public void setPositionY(int y) {
+ _posy = y;
+ }
+
+ @Override
+ public String toXML() {
+ StringBuffer buf = new StringBuffer();
+ buf.append("<" + ELEMENT_NAME + " xmlns=\"" + NAMESPACE + "\">");
+
+ buf.append("").append(_gameID).append("");
+
+ buf.append("").append(_posx).append("");
+
+ buf.append("").append(_posy).append("");
+
+ buf.append("" + ELEMENT_NAME + ">");
+ return buf.toString();
+ }
+
+}
diff --git a/src/plugins/tictactoe/src/java/tic/tac/toe/ui/GamePanel.java b/src/plugins/tictactoe/src/java/tic/tac/toe/ui/GamePanel.java
index 93f70273..c2a8d0a7 100644
--- a/src/plugins/tictactoe/src/java/tic/tac/toe/ui/GamePanel.java
+++ b/src/plugins/tictactoe/src/java/tic/tac/toe/ui/GamePanel.java
@@ -21,20 +21,21 @@ package tic.tac.toe.ui;
import java.awt.BorderLayout;
import java.awt.Color;
-
import javax.swing.JFrame;
import javax.swing.JPanel;
-
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.filter.PacketExtensionFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
+import org.jivesoftware.smack.util.StringUtils;
+import org.jivesoftware.spark.SparkManager;
+import org.jivesoftware.spark.ui.ChatRoom;
import org.jivesoftware.spark.ui.ShakeWindow;
-
import tic.tac.toe.GameBoard;
import tic.tac.toe.Mark;
import tic.tac.toe.TTTRes;
+import tic.tac.toe.packet.InvalidMove;
import tic.tac.toe.packet.MovePacket;
/**
@@ -95,13 +96,48 @@ public class GamePanel extends JPanel {
MovePacket.ELEMENT_NAME, MovePacket.NAMESPACE);
if (move.getGameID() == _gameID) {
+
+ if(_gameboard.isValidMove(getYourMark() ,move.getPositionX(), move.getPositionY()))
+ {
_gameboardpanel.placeMark(getYourMark(),
move.getPositionX(), move.getPositionY());
+ }
+ else
+ {
+ InvalidMove inval = new InvalidMove();
+ inval.setGameID(move.getGameID());
+ inval.setPositionX(move.getPositionX());
+ inval.setPositionY(move.getPositionY());
+ Message message =new Message(_opponent);
+ message.addExtension(inval);
+ _connection.sendPacket(message);
+
+ ChatRoom cr = SparkManager.getChatManager().getChatRoom(StringUtils.parseBareAddress(_opponent));
+ cr.getTranscriptWindow().insertCustomText(_opponent+"seems to be cheating\n"+
+ "He tried placing a wrong Move", true, false, Color.red);
+
+ }
}
}
}, new PacketExtensionFilter(MovePacket.ELEMENT_NAME,
MovePacket.NAMESPACE));
+
+ _connection.addPacketListener(new PacketListener() {
+
+ @Override
+ public void processPacket(Packet packet) {
+
+ //InvalidMove im = (InvalidMove)packet.getExtension(InvalidMove.ELEMENT_NAME, InvalidMove.NAMESPACE);
+ ChatRoom cr = SparkManager.getChatManager().getChatRoom(StringUtils.parseBareAddress(_opponent));
+ cr.getTranscriptWindow().insertCustomText("You seem to be Cheating\n"+
+ "You placed a wrong Move", true, false, Color.red);
+ ShakeWindow sw = new ShakeWindow(_frame);
+ sw.startRandomMovement(10);
+
+ }
+ }, new PacketExtensionFilter(InvalidMove.ELEMENT_NAME,
+ InvalidMove.NAMESPACE));
}
@@ -145,8 +181,6 @@ public class GamePanel extends JPanel {
if (_gameboard.getWinner() == getMyMark().getValue()) {
-
-
remove(_playerdisplay);
add(new GameEndsUI(TTTRes.getString("ttt.win"), Color.GREEN), BorderLayout.SOUTH);