mirror of
https://github.com/igniterealtime/Spark.git
synced 2025-12-01 12:27:58 +00:00
TicTacToe:
If a user tries to Cheat, let him know that we know^^ git-svn-id: http://svn.igniterealtime.org/svn/repos/spark/trunk@12499 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
committed by
wolf.posdorfer
parent
7875fb8071
commit
8beb84f0c6
@ -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;
|
||||
|
||||
@ -106,5 +109,46 @@ 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();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -357,7 +357,14 @@ public class ChatRoomImpl extends ChatRoom {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the Bare-Participant JID
|
||||
*
|
||||
* <b> user@server.com </b> <br>
|
||||
* for retrieving the full Jid use ChatRoomImpl.getJID()
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getParticipantJID() {
|
||||
return participantJID;
|
||||
}
|
||||
|
||||
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@ -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());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
@ -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("<gameID>").append(_gameID).append("</gameID>");
|
||||
|
||||
buf.append("<positionX>").append(_posx).append("</positionX>");
|
||||
|
||||
buf.append("<positionY>").append(_posy).append("</positionY>");
|
||||
|
||||
buf.append("</" + ELEMENT_NAME + ">");
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@ -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,14 +96,49 @@ 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));
|
||||
|
||||
}
|
||||
|
||||
public PlayerDisplay getPlayerDisplay() {
|
||||
@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user