Search in sources :

Example 1 with GameOfferPacket

use of tic.tac.toe.packet.GameOfferPacket in project Spark by igniterealtime.

the class TicTacToePlugin method initialize.

@Override
public void initialize() {
    ClassLoader cl = getClass().getClassLoader();
    buttonimg = new ImageIcon(cl.getResource("ttt.button.png"));
    _currentInvitations = new HashSet<String>();
    ProviderManager.addIQProvider(GameOfferPacket.ELEMENT_NAME, GameOfferPacket.NAMESPACE, new GameOfferPacket.Provider());
    ProviderManager.addExtensionProvider(MovePacket.ELEMENT_NAME, MovePacket.NAMESPACE, new MovePacket.Provider());
    ProviderManager.addExtensionProvider(InvalidMove.ELEMENT_NAME, InvalidMove.NAMESPACE, new InvalidMove.Provider());
    // Add IQ listener to listen for incoming game invitations.
    _gameOfferListener = new StanzaListener() {

        public void processPacket(Stanza stanza) {
            GameOfferPacket invitation = (GameOfferPacket) stanza;
            if (invitation.getType() == IQ.Type.get) {
                showInvitationAlert(invitation);
            }
        }
    };
    SparkManager.getConnection().addAsyncStanzaListener(_gameOfferListener, new StanzaTypeFilter(GameOfferPacket.class));
    addButtonToToolBar();
}
Also used : ImageIcon(javax.swing.ImageIcon) Stanza(org.jivesoftware.smack.packet.Stanza) StanzaListener(org.jivesoftware.smack.StanzaListener) StanzaTypeFilter(org.jivesoftware.smack.filter.StanzaTypeFilter) MovePacket(tic.tac.toe.packet.MovePacket) GameOfferPacket(tic.tac.toe.packet.GameOfferPacket) InvalidMove(tic.tac.toe.packet.InvalidMove)

Example 2 with GameOfferPacket

use of tic.tac.toe.packet.GameOfferPacket in project Spark by igniterealtime.

the class TicTacToePlugin method addButtonToToolBar.

/**
 * Add the TTT-Button to every opening Chatroom
 * and create Listeners for it
 */
private void addButtonToToolBar() {
    _chatRoomListener = new ChatRoomListenerAdapter() {

        @Override
        public void chatRoomOpened(final ChatRoom room) {
            if (!(room instanceof ChatRoomImpl)) {
                // Don't do anything if this is not a 1on1-Chat
                return;
            }
            final ChatRoomButton sendGameButton = new ChatRoomButton(buttonimg);
            room.getToolBar().addChatRoomButton(sendGameButton);
            final String opponentJID = ((ChatRoomImpl) room).getJID();
            sendGameButton.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    if (_currentInvitations.contains(XmppStringUtils.parseBareJid(opponentJID))) {
                        return;
                    }
                    final GameOfferPacket offer = new GameOfferPacket();
                    offer.setTo(opponentJID);
                    offer.setType(IQ.Type.get);
                    _currentInvitations.add(XmppStringUtils.parseBareJid(opponentJID));
                    room.getTranscriptWindow().insertCustomText(TTTRes.getString("ttt.request.sent"), false, false, Color.BLUE);
                    try {
                        SparkManager.getConnection().sendStanza(offer);
                    } catch (SmackException.NotConnectedException e1) {
                        Log.warning("Unable to send offer to " + opponentJID, e1);
                    }
                    SparkManager.getConnection().addAsyncStanzaListener(new StanzaListener() {

                        @Override
                        public void processPacket(Stanza stanza) {
                            GameOfferPacket answer = (GameOfferPacket) stanza;
                            answer.setStartingPlayer(offer.isStartingPlayer());
                            answer.setGameID(offer.getGameID());
                            if (answer.getType() == IQ.Type.result) {
                                // ACCEPT
                                _currentInvitations.remove(XmppStringUtils.parseBareJid(opponentJID));
                                room.getTranscriptWindow().insertCustomText(TTTRes.getString("ttt.request.accept"), false, false, Color.BLUE);
                                createTTTWindow(answer, opponentJID);
                            } else {
                                // DECLINE
                                room.getTranscriptWindow().insertCustomText(TTTRes.getString("ttt.request.decline"), false, false, Color.RED);
                                _currentInvitations.remove(XmppStringUtils.parseBareJid(opponentJID));
                            }
                        }
                    }, new PacketIDFilter(offer.getPacketID()));
                }
            });
        }

        @Override
        public void chatRoomClosed(ChatRoom room) {
            if (room instanceof ChatRoomImpl) {
                ChatRoomImpl cri = (ChatRoomImpl) room;
                _currentInvitations.remove(cri.getParticipantJID());
            }
        }
    };
    SparkManager.getChatManager().addChatRoomListener(_chatRoomListener);
}
Also used : ActionEvent(java.awt.event.ActionEvent) SmackException(org.jivesoftware.smack.SmackException) Stanza(org.jivesoftware.smack.packet.Stanza) StanzaListener(org.jivesoftware.smack.StanzaListener) ChatRoomImpl(org.jivesoftware.spark.ui.rooms.ChatRoomImpl) ChatRoomListenerAdapter(org.jivesoftware.spark.ui.ChatRoomListenerAdapter) ActionListener(java.awt.event.ActionListener) ChatRoom(org.jivesoftware.spark.ui.ChatRoom) GameOfferPacket(tic.tac.toe.packet.GameOfferPacket) PacketIDFilter(org.jivesoftware.smack.filter.PacketIDFilter) ChatRoomButton(org.jivesoftware.spark.ui.ChatRoomButton)

Aggregations

StanzaListener (org.jivesoftware.smack.StanzaListener)2 Stanza (org.jivesoftware.smack.packet.Stanza)2 GameOfferPacket (tic.tac.toe.packet.GameOfferPacket)2 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 ImageIcon (javax.swing.ImageIcon)1 SmackException (org.jivesoftware.smack.SmackException)1 PacketIDFilter (org.jivesoftware.smack.filter.PacketIDFilter)1 StanzaTypeFilter (org.jivesoftware.smack.filter.StanzaTypeFilter)1 ChatRoom (org.jivesoftware.spark.ui.ChatRoom)1 ChatRoomButton (org.jivesoftware.spark.ui.ChatRoomButton)1 ChatRoomListenerAdapter (org.jivesoftware.spark.ui.ChatRoomListenerAdapter)1 ChatRoomImpl (org.jivesoftware.spark.ui.rooms.ChatRoomImpl)1 InvalidMove (tic.tac.toe.packet.InvalidMove)1 MovePacket (tic.tac.toe.packet.MovePacket)1