<commit_before>//
// $Id: ParlorDirector.java,v 1.8 2001/10/06 00:25:29 mdb Exp $

package com.threerings.parlor.client;

import java.util.ArrayList;
import java.util.Iterator;
import com.samskivert.util.HashIntMap;

import com.threerings.cocktail.cher.client.InvocationReceiver;

import com.threerings.parlor.Log;
import com.threerings.parlor.data.GameConfig;
import com.threerings.parlor.util.ParlorContext;

/**
 * The parlor director manages the client side of the game configuration
 * and matchmaking processes. It is also the entity that is listening for
 * game start notifications which it then dispatches the client entity
 * that will actually create and display the user interface for the game
 * that started.
 */
public class ParlorDirector
    implements ParlorCodes, InvocationReceiver
{
    /**
     * Constructs a parlor director and provides it with the parlor
     * context that it can use to access the client services that it needs
     * to provide its own services. Only one parlor director should be
     * active in the client at one time and it should be made available
     * via the parlor context.
     *
     * @param ctx the parlor context in use by the client.
     */
    public ParlorDirector (ParlorContext ctx)
    {
        _ctx = ctx;

        // register ourselves with the invocation director as handling
        // parlor notifications
        _ctx.getClient().getInvocationDirector().
            registerReceiver(MODULE_NAME, this);
    }

    /**
     * Sets the invitation handler, which is the entity that will be
     * notified when we receive incoming invitation notifications and when
     * invitations have been cancelled.
     *
     * @param handler our new invitation handler.
     */
    public void setInvitationHandler (InvitationHandler handler)
    {
        _handler = handler;
    }

    /**
     * Requests that the named user be invited to a game described by the
     * supplied game config.
     *
     * @param invitee the user to invite.
     * @param config the configuration of the game to which the user is
     * being invited.
     * @param observer the entity that will be notified if this invitation
     * is accepted, refused or countered.
     *
     * @return a unique id associated with this invitation that can be
     * used to discern between various outstanding invitations by the
     * invitation observer.
     */
    public int invite (String invitee, GameConfig config,
                       InvitationResponseObserver observer)
    {
        // generate the invocation service request
        int invid = ParlorService.invite(
            _ctx.getClient(), invitee, config, this);

        // create an invitation record and put it in the submitted table
        Invitation invite = new Invitation(invitee, config, observer);
        _submittedInvites.put(invid, invite);
        return invite.inviteId;
    }

    /**
     * Accept an invitation.
     *
     * @param inviteId the id of the invitation to accept.
     */
    public void accept (int inviteId)
    {
        Invitation invite = getInviteByLocalId(inviteId);
        if (invite == null) {
            // complain if we didn't find a matching invitation
            Log.warning("Received request to accept non-existent " +
                        "invitation [inviteId=" + inviteId + "].");
            return;
        }

        // generate the invocation service request
        ParlorService.respond(_ctx.getClient(), invite.remoteId,
                              INVITATION_ACCEPTED, null, this);
    }

    /**
     * Refuse an invitation.
     *
     * @param inviteId the id of the invitation to accept.
     * @param message the message to deliver to the inviting user
     * explaining the reason for the refusal or null if no message is to
     * be provided.
     */
    public void refuse (int inviteId, String message)
    {
        Invitation invite = getInviteByLocalId(inviteId);
        if (invite == null) {
            // complain if we didn't find a matching invitation
            Log.warning("Received request to accept non-existent " +
                        "invitation [inviteId=" + inviteId + "].");
            return;
        }

        // generate the invocation service request
        ParlorService.respond(_ctx.getClient(), invite.remoteId,
                              INVITATION_REFUSED, message, this);
    }

    /**
     * Counters a received invitation with an invitation with different
     * game configuration parameters.
     *
     * @param inviteId the id of the received invitation.
     * @param config the updated game configuration.
     * @param observer the entity that will be notified if this
     * counter-invitation is accepted, refused or countered.
     */
    public void counter (int inviteId, GameConfig config,
                         InvitationResponseObserver observer)
    {
        Invitation invite = getInviteByLocalId(inviteId);
        if (invite == null) {
            // complain if we didn't find a matching invitation
            Log.warning("Received request to counter non-existent " +
                        "invitation [inviteId=" + inviteId +
                        ", config=" + config + "].");
            return;
        }

        // update the invitation record with the observer (who will
        // eventually be hearing back from the other client about their
        // counter-invitation)
        invite.observer = observer;

        // generate the invocation service request
        ParlorService.respond(_ctx.getClient(), invite.remoteId,
                              INVITATION_COUNTERED, config, this);
    }

    /**
     * Issues a request to cancel an outstanding invitation.
     *
     * @param inviteId the id of the invitation to cancel.
     */
    public void cancel (int inviteId)
    {
        // look up the invitation record (oh the two separate key spaces
        // humanity)
        Invitation invite = getInviteByLocalId(inviteId);
        if (invite == null) {
            // complain if we didn't find a matching invitation
            Log.warning("Received request to cancel non-existent " +
                        "invitation [inviteId=" + inviteId + "].");
            return;
        }

        // if the invitation has not yet been acknowleged by the server,
        // we make a note that it should be cancelled when we do receive
        // the acknowlegement
        if (invite.remoteId == -1) {
            invite.cancelled = true;

        } else {
            // otherwise, generate the invocation service request
            ParlorService.cancel(_ctx.getClient(), invite.remoteId, this);
            // and remove it from the pending table
            _pendingInvites.remove(invite.remoteId);
        }
    }

    /**
     * Called by the invocation services when a game in which we are a
     * player is ready to begin.
     *
     * @param gameOid the object id of the game object.
     */
    public void handleGameReadyNotification (int gameOid)
    {
        Log.info("Handling game ready [goid=" + gameOid + "].");
    }

    /**
     * Called by the invocation services when another user has invited us
     * to play a game.
     *
     * @param remoteId the unique indentifier for this invitation (used
     * when countering or responding).
     * @param inviter the username of the inviting user.
     * @param config the configuration information for the game to which
     * we've been invited.
     */
    public void handleInviteNotification (
        int remoteId, String inviter, GameConfig config)
    {
        // create an invitation record for this invitation
        Invitation invite = new Invitation(inviter, config, null);
        invite.remoteId = remoteId;
<commit_msg>For now, just head directly to the game room when we are notified that our game is ready.<commit_after>//
// $Id: ParlorDirector.java,v 1.9 2001/10/09 20:20:52 mdb Exp $

package com.threerings.parlor.client;

import java.util.ArrayList;
import java.util.Iterator;
import com.samskivert.util.HashIntMap;

import com.threerings.cocktail.cher.client.InvocationReceiver;

import com.threerings.parlor.Log;
import com.threerings.parlor.data.GameConfig;
import com.threerings.parlor.util.ParlorContext;

/**
 * The parlor director manages the client side of the game configuration
 * and matchmaking processes. It is also the entity that is listening for
 * game start notifications which it then dispatches the client entity
 * that will actually create and display the user interface for the game
 * that started.
 */
public class ParlorDirector
    implements ParlorCodes, InvocationReceiver
{
    /**
     * Constructs a parlor director and provides it with the parlor
     * context that it can use to access the client services that it needs
     * to provide its own services. Only one parlor director should be
     * active in the client at one time and it should be made available
     * via the parlor context.
     *
     * @param ctx the parlor context in use by the client.
     */
    public ParlorDirector (ParlorContext ctx)
    {
        _ctx = ctx;

        // register ourselves with the invocation director as handling
        // parlor notifications
        _ctx.getClient().getInvocationDirector().
            registerReceiver(MODULE_NAME, this);
    }

    /**
     * Sets the invitation handler, which is the entity that will be
     * notified when we receive incoming invitation notifications and when
     * invitations have been cancelled.
     *
     * @param handler our new invitation handler.
     */
    public void setInvitationHandler (InvitationHandler handler)
    {
        _handler = handler;
    }

    /**
     * Requests that the named user be invited to a game described by the
     * supplied game config.
     *
     * @param invitee the user to invite.
     * @param config the configuration of the game to which the user is
     * being invited.
     * @param observer the entity that will be notified if this invitation
     * is accepted, refused or countered.
     *
     * @return a unique id associated with this invitation that can be
     * used to discern between various outstanding invitations by the
     * invitation observer.
     */
    public int invite (String invitee, GameConfig config,
                       InvitationResponseObserver observer)
    {
        // generate the invocation service request
        int invid = ParlorService.invite(
            _ctx.getClient(), invitee, config, this);

        // create an invitation record and put it in the submitted table
        Invitation invite = new Invitation(invitee, config, observer);
        _submittedInvites.put(invid, invite);
        return invite.inviteId;
    }

    /**
     * Accept an invitation.
     *
     * @param inviteId the id of the invitation to accept.
     */
    public void accept (int inviteId)
    {
        Invitation invite = getInviteByLocalId(inviteId);
        if (invite == null) {
            // complain if we didn't find a matching invitation
            Log.warning("Received request to accept non-existent " +
                        "invitation [inviteId=" + inviteId + "].");
            return;
        }

        // generate the invocation service request
        ParlorService.respond(_ctx.getClient(), invite.remoteId,
                              INVITATION_ACCEPTED, null, this);
    }

    /**
     * Refuse an invitation.
     *
     * @param inviteId the id of the invitation to accept.
     * @param message the message to deliver to the inviting user
     * explaining the reason for the refusal or null if no message is to
     * be provided.
     */
    public void refuse (int inviteId, String message)
    {
        Invitation invite = getInviteByLocalId(inviteId);
        if (invite == null) {
            // complain if we didn't find a matching invitation
            Log.warning("Received request to accept non-existent " +
                        "invitation [inviteId=" + inviteId + "].");
            return;
        }

        // generate the invocation service request
        ParlorService.respond(_ctx.getClient(), invite.remoteId,
                              INVITATION_REFUSED, message, this);
    }

    /**
     * Counters a received invitation with an invitation with different
     * game configuration parameters.
     *
     * @param inviteId the id of the received invitation.
     * @param config the updated game configuration.
     * @param observer the entity that will be notified if this
     * counter-invitation is accepted, refused or countered.
     */
    public void counter (int inviteId, GameConfig config,
                         InvitationResponseObserver observer)
    {
        Invitation invite = getInviteByLocalId(inviteId);
        if (invite == null) {
            // complain if we didn't find a matching invitation
            Log.warning("Received request to counter non-existent " +
                        "invitation [inviteId=" + inviteId +
                        ", config=" + config + "].");
            return;
        }

        // update the invitation record with the observer (who will
        // eventually be hearing back from the other client about their
        // counter-invitation)
        invite.observer = observer;

        // generate the invocation service request
        ParlorService.respond(_ctx.getClient(), invite.remoteId,
                              INVITATION_COUNTERED, config, this);
    }

    /**
     * Issues a request to cancel an outstanding invitation.
     *
     * @param inviteId the id of the invitation to cancel.
     */
    public void cancel (int inviteId)
    {
        // look up the invitation record (oh the two separate key spaces
        // humanity)
        Invitation invite = getInviteByLocalId(inviteId);
        if (invite == null) {
            // complain if we didn't find a matching invitation
            Log.warning("Received request to cancel non-existent " +
                        "invitation [inviteId=" + inviteId + "].");
            return;
        }

        // if the invitation has not yet been acknowleged by the server,
        // we make a note that it should be cancelled when we do receive
        // the acknowlegement
        if (invite.remoteId == -1) {
            invite.cancelled = true;

        } else {
            // otherwise, generate the invocation service request
            ParlorService.cancel(_ctx.getClient(), invite.remoteId, this);
            // and remove it from the pending table
            _pendingInvites.remove(invite.remoteId);
        }
    }

    /**
     * Called by the invocation services when a game in which we are a
     * player is ready to begin.
     *
     * @param gameOid the object id of the game object.
     */
    public void handleGameReadyNotification (int gameOid)
    {
        Log.info("Handling game ready [goid=" + gameOid + "].");

        // go there
        _ctx.getLocationDirector().moveTo(gameOid);
    }

    /**
     * Called by the invocation services when another user has invited us
     * to play a game.
     *
     * @param remoteId the unique indentifier for this invitation (used
     * when countering or responding).
     * @param inviter the username of the inviting user.
     * @param config the configuration information for the game to which
     * we've been invited.
     */
    public void handleInviteNotification (
        int remoteId, String inviter, GameConfig config)
    {
        // create an invitation record for this invitation
        Invitation invite = new Invitation(inviter, config, null);
        invite.remoteId = remoteId;

{'fraction_non_alphanumeric': 0.07246008015777085, 'fraction_numerical': 0.0021629874673961447, 'mean_word_length': 2.175757575757576, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4552422', 'n_tokens_mistral': 4025, 'n_tokens_neox': 3885, 'n_words': 1542}