Search in sources :

Example 1 with SOCRobotParameters

use of soc.util.SOCRobotParameters in project JSettlers2 by jdmonin.

the class SOCUpdateRobotParams method parseDataStr.

/**
 * Parse the command String into a UpdateRobotParams message
 *
 * @param s   the String to parse
 * @return    a UpdateRobotParams message, or null if the data is garbled
 */
public static SOCUpdateRobotParams parseDataStr(String s) {
    // maxGameLength
    int mgl;
    // maxETA
    int me;
    // etaBonusFactor
    float ebf;
    // adversarialFactor
    float af;
    // leaderAdversarialFactor
    float laf;
    // devCardMultiplier
    float dcm;
    // threatMultiplier
    float tm;
    // strategyType
    int st;
    // trade flag
    int tf;
    StringTokenizer stok = new StringTokenizer(s, sep2);
    try {
        mgl = Integer.parseInt(stok.nextToken());
        me = Integer.parseInt(stok.nextToken());
        ebf = (Float.valueOf(stok.nextToken())).floatValue();
        af = (Float.valueOf(stok.nextToken())).floatValue();
        laf = (Float.valueOf(stok.nextToken())).floatValue();
        dcm = (Float.valueOf(stok.nextToken())).floatValue();
        tm = (Float.valueOf(stok.nextToken())).floatValue();
        st = Integer.parseInt(stok.nextToken());
        tf = Integer.parseInt(stok.nextToken());
    } catch (Exception e) {
        return null;
    }
    return new SOCUpdateRobotParams(new SOCRobotParameters(mgl, me, ebf, af, laf, dcm, tm, st, tf));
}
Also used : StringTokenizer(java.util.StringTokenizer) SOCRobotParameters(soc.util.SOCRobotParameters)

Example 2 with SOCRobotParameters

use of soc.util.SOCRobotParameters in project JSettlers2 by jdmonin.

the class SOCDBHelper method retrieveRobotParams.

/**
 * Get this robot's specialized parameters from the database, if it has an entry there.
 * Optionally, return defaults if not found or if no database: Default bot params are
 * {@link SOCServer#ROBOT_PARAMS_SMARTER} if the robot name starts with "robot "
 * or {@link SOCServer#ROBOT_PARAMS_DEFAULT} otherwise (starts with "droid ").
 * This matches the bot names generated in {@link SOCServer#setupLocalRobots(int, int)}.
 *
 * @param robotName Name of robot for db lookup
 * @param useDefaults  If true, return the server's default parameters if {@code robotName} not in the table
 *    or if a database isn't in use.
 * @return null if robotName not in database, or if db is empty and robotparams table doesn't exist;
 *    if {@code useDefaults}, will return the server default parameters instead of {@code null}.
 * @throws SQLException if unexpected problem retrieving the params
 */
public static final SOCRobotParameters retrieveRobotParams(final String robotName, final boolean useDefaults) throws SQLException {
    SOCRobotParameters params = retrieveRobotParams(robotName);
    if ((params == null) && useDefaults)
        if (robotName.startsWith("robot "))
            // uses SOCRobotDM.SMART_STRATEGY
            params = SOCServer.ROBOT_PARAMS_SMARTER;
        else
            // startsWith("droid ")
            // uses SOCRobotDM.FAST_STRATEGY
            params = SOCServer.ROBOT_PARAMS_DEFAULT;
    return params;
}
Also used : SOCRobotParameters(soc.util.SOCRobotParameters)

Example 3 with SOCRobotParameters

use of soc.util.SOCRobotParameters in project JSettlers2 by jdmonin.

the class SOCServerMessageHandler method handleIMAROBOT.

/**
 * Handle the "I'm a robot" message.
 * Robots send their {@link SOCVersion} before sending this message.
 * Their version is checked here, must equal server's version.
 * For stability and control, the cookie in this message must
 * match this server's {@link SOCServer#robotCookie}.
 * Otherwise the bot is rejected and they're disconnected by calling
 * {@link SOCServer#removeConnection(Connection, boolean)}.
 *<P>
 * Bot tuning parameters are sent here to the bot, from
 * {@link SOCDBHelper#retrieveRobotParams(String, boolean) SOCDBHelper.retrieveRobotParams(botName, true)}.
 * See that method for default bot params.
 * See {@link SOCServer#authOrRejectClientRobot(Connection, String, String, String)}
 * for {@link SOCClientData} flags and fields set for the bot's connection
 * and for other misc work done, such as {@link Server#cliConnDisconPrintsPending} updates.
 *<P>
 * The server's built-in bots are named and started in {@link SOCServer#setupLocalRobots(int, int)},
 * then must authenticate with this message just like external or third-party bots.
 *
 * @param c  the connection that sent the message
 * @param mes  the message
 * @since 1.0.0
 */
private void handleIMAROBOT(final Connection c, final SOCImARobot mes) {
    if (c == null)
        return;
    final String botName = mes.getNickname();
    final String rejectReason = srv.authOrRejectClientRobot(c, botName, mes.getCookie(), mes.getRBClass());
    if (rejectReason != null) {
        if (rejectReason.equals(SOCServer.MSG_NICKNAME_ALREADY_IN_USE))
            c.put(SOCStatusMessage.toCmd(SOCStatusMessage.SV_NAME_IN_USE, c.getVersion(), rejectReason));
        c.put(new SOCRejectConnection(rejectReason).toCmd());
        c.disconnectSoft();
        // make an effort to send reject message before closing socket
        final Connection rc = c;
        srv.miscTaskTimer.schedule(new TimerTask() {

            public void run() {
                srv.removeConnection(rc, true);
            }
        }, 300);
        // <--- Early return: rejected ---
        return;
    }
    // 
    // send the current robot parameters
    // 
    SOCRobotParameters params = null;
    try {
        params = SOCDBHelper.retrieveRobotParams(botName, true);
        // or srv.ROBOT_PARAMS_DEFAULT (SOCRobotDM.FAST_STRATEGY).
        if ((params != null) && (params != SOCServer.ROBOT_PARAMS_SMARTER) && (params != SOCServer.ROBOT_PARAMS_DEFAULT) && D.ebugIsEnabled())
            D.ebugPrintln("*** Robot Parameters for " + botName + " = " + params);
    } catch (SQLException sqle) {
        System.err.println("Error retrieving robot parameters from db: Using defaults.");
    }
    if (params == null)
        // fallback in case of SQLException
        params = SOCServer.ROBOT_PARAMS_DEFAULT;
    c.put(SOCUpdateRobotParams.toCmd(params));
}
Also used : TimerTask(java.util.TimerTask) SQLException(java.sql.SQLException) Connection(soc.server.genericServer.Connection) StringConnection(soc.server.genericServer.StringConnection) SOCRobotParameters(soc.util.SOCRobotParameters)

Example 4 with SOCRobotParameters

use of soc.util.SOCRobotParameters in project JSettlers2 by jdmonin.

the class SOCDBHelper method retrieveRobotParams.

/**
 * DB query portion (no defaults) of {@link #retrieveRobotParams(String, boolean)}.
 */
private static SOCRobotParameters retrieveRobotParams(final String robotName) throws SQLException {
    SOCRobotParameters robotParams = null;
    if (checkConnection()) {
        if (robotParamsQuery == null)
            // <--- Early return: Table not found in db, is probably empty ---
            return null;
        try {
            robotParamsQuery.setString(1, robotName);
            ResultSet resultSet = robotParamsQuery.executeQuery();
            if (resultSet.next()) {
                int mgl = resultSet.getInt(2);
                int me = resultSet.getInt(3);
                float ebf = resultSet.getFloat(4);
                float af = resultSet.getFloat(5);
                float laf = resultSet.getFloat(6);
                float dcm = resultSet.getFloat(7);
                float tm = resultSet.getFloat(8);
                int st = resultSet.getInt(9);
                int tf = resultSet.getInt(14);
                robotParams = new SOCRobotParameters(mgl, me, ebf, af, laf, dcm, tm, st, tf);
            }
            resultSet.close();
        } catch (SQLException sqlE) {
            errorCondition = true;
            sqlE.printStackTrace();
            throw sqlE;
        }
    }
    return robotParams;
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) SOCRobotParameters(soc.util.SOCRobotParameters)

Aggregations

SOCRobotParameters (soc.util.SOCRobotParameters)4 SQLException (java.sql.SQLException)2 ResultSet (java.sql.ResultSet)1 StringTokenizer (java.util.StringTokenizer)1 TimerTask (java.util.TimerTask)1 Connection (soc.server.genericServer.Connection)1 StringConnection (soc.server.genericServer.StringConnection)1