Search in sources :

Example 1 with GameOfferPacket

use of battleship.packets.GameOfferPacket in project Spark by igniterealtime.

the class ChatRoomOpeningListener method chatRoomOpened.

@Override
public void chatRoomOpened(final ChatRoom room) {
    if (// Check for 1on1 Chat
    !(room instanceof ChatRoomImpl)) {
        return;
    }
    final ChatRoomButton sendGameButton = new ChatRoomButton("BS");
    room.getToolBar().addChatRoomButton(sendGameButton);
    final String opponentJID = ((ChatRoomImpl) room).getJID();
    sendGameButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            final GameOfferPacket offer = new GameOfferPacket();
            offer.setTo(opponentJID);
            offer.setType(IQ.Type.get);
            room.getTranscriptWindow().insertCustomText(BsRes.getString("request"), 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());
                    String name = XmppStringUtils.parseLocalpart(opponentJID);
                    if (answer.getType() == IQ.Type.result) {
                        // ACCEPT
                        room.getTranscriptWindow().insertCustomText(BsRes.getString("accepted", name), false, false, Color.BLUE);
                        createWindow(answer, opponentJID);
                    } else {
                        // DECLINE
                        room.getTranscriptWindow().insertCustomText(BsRes.getString("declined", name), false, false, Color.RED);
                    }
                }
            }, new PacketIDFilter(offer.getPacketID()));
        }
    });
}
Also used : ActionListener(java.awt.event.ActionListener) ActionEvent(java.awt.event.ActionEvent) GameOfferPacket(battleship.packets.GameOfferPacket) Stanza(org.jivesoftware.smack.packet.Stanza) StanzaListener(org.jivesoftware.smack.StanzaListener) ChatRoomImpl(org.jivesoftware.spark.ui.rooms.ChatRoomImpl) PacketIDFilter(org.jivesoftware.smack.filter.PacketIDFilter) ChatRoomButton(org.jivesoftware.spark.ui.ChatRoomButton)

Example 2 with GameOfferPacket

use of battleship.packets.GameOfferPacket in project Spark by igniterealtime.

the class BattleshipPlugin method initialize.

@Override
public void initialize() {
    ProviderManager.addIQProvider(GameOfferPacket.ELEMENT_NAME, GameOfferPacket.NAMESPACE, GameOfferPacket.class);
    ProviderManager.addExtensionProvider(MovePacket.ELEMENT_NAME, MovePacket.NAMESPACE, MovePacket.class);
    ProviderManager.addExtensionProvider(MoveAnswerPacket.ELEMENT_NAME, MoveAnswerPacket.NAMESPACE, MoveAnswerPacket.class);
    _gameofferListener = new StanzaListener() {

        @Override
        public void processPacket(Stanza stanza) {
            GameOfferPacket invitation = (GameOfferPacket) stanza;
            if (invitation.getType() == IQ.Type.get) {
                showInvitationInChat(invitation);
            }
        }
    };
    SparkManager.getConnection().addAsyncStanzaListener(_gameofferListener, new StanzaTypeFilter(GameOfferPacket.class));
    _chatRoomListener = new ChatRoomOpeningListener();
    SparkManager.getChatManager().addChatRoomListener(_chatRoomListener);
}
Also used : StanzaTypeFilter(org.jivesoftware.smack.filter.StanzaTypeFilter) GameOfferPacket(battleship.packets.GameOfferPacket) Stanza(org.jivesoftware.smack.packet.Stanza) StanzaListener(org.jivesoftware.smack.StanzaListener) ChatRoomOpeningListener(battleship.listener.ChatRoomOpeningListener)

Aggregations

GameOfferPacket (battleship.packets.GameOfferPacket)2 StanzaListener (org.jivesoftware.smack.StanzaListener)2 Stanza (org.jivesoftware.smack.packet.Stanza)2 ChatRoomOpeningListener (battleship.listener.ChatRoomOpeningListener)1 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 PacketIDFilter (org.jivesoftware.smack.filter.PacketIDFilter)1 StanzaTypeFilter (org.jivesoftware.smack.filter.StanzaTypeFilter)1 ChatRoomButton (org.jivesoftware.spark.ui.ChatRoomButton)1 ChatRoomImpl (org.jivesoftware.spark.ui.rooms.ChatRoomImpl)1