/* * Copyright (c) 2018. Developed by Hedgecode. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.hedgecode.chess.uci; import java.util.List; import org.hedgecode.chess.uci.annotation.CommandDirection; import org.hedgecode.chess.uci.command.OptionParams; import org.hedgecode.chess.uci.command.OptionType; /** * * * @author Dmitry Samoshin aka gotty */ public class AcceptorStub implements Acceptor { private static final String LOG_TO_FORMAT = ">> %s %s"; private static final String LOG_FROM_FORMAT = "<< %s %s"; private CommandExecutor commandExecutor; AcceptorStub() { } public void init(CommandExecutor executor) { commandExecutor = executor; } @Override public void start() { commandExecutor.exec( UCIConstants.UCI, CommandDirection.TO_ENGINE, null ); log(CommandDirection.TO_ENGINE, UCIConstants.UCI, null); } @Override public void id(String params) { log(CommandDirection.FROM_ENGINE, UCIConstants.ID, params); } @Override public void uciOK(String params) { log(CommandDirection.FROM_ENGINE, UCIConstants.UCI_OK, params); commandExecutor.exec( UCIConstants.IS_READY, CommandDirection.TO_ENGINE, null ); log(CommandDirection.TO_ENGINE, UCIConstants.IS_READY, null); } @Override public void readyOK(String params) { log(CommandDirection.FROM_ENGINE, UCIConstants.READY_OK, params); commandExecutor.exec( UCIConstants.QUIT, CommandDirection.TO_ENGINE, null ); log(CommandDirection.TO_ENGINE, UCIConstants.QUIT, null); } @Override public void setOption(String name) { log( CommandDirection.FROM_ENGINE, UCIConstants.OPTION, String.format( "%s %s %s %s", OptionParams.OPTION_NAME, name, OptionParams.OPTION_TYPE, OptionType.BUTTON.type() ) ); } @Override public void setOption(String name, boolean def) { log( CommandDirection.FROM_ENGINE, UCIConstants.OPTION, String.format( "%s %s %s %s %s %b", OptionParams.OPTION_NAME, name, OptionParams.OPTION_TYPE, OptionType.CHECK.type(), OptionParams.OPTION_DEFAULT, def ) ); } @Override public void setOption(String name, int def, int min, int max) { log( CommandDirection.FROM_ENGINE, UCIConstants.OPTION, String.format( "%s %s %s %s %s %d %s %d %s %d", OptionParams.OPTION_NAME, name, OptionParams.OPTION_TYPE, OptionType.SPIN.type(), OptionParams.OPTION_DEFAULT, def, OptionParams.OPTION_MIN, min, OptionParams.OPTION_MAX, max ) ); } @Override public void setOption(String name, String def) { log( CommandDirection.FROM_ENGINE, UCIConstants.OPTION, String.format( "%s %s %s %s %s %s", OptionParams.OPTION_NAME, name, OptionParams.OPTION_TYPE, OptionType.STRING.type(), OptionParams.OPTION_DEFAULT, def == null ? "" : def ) ); } @Override public void setOption(String name, String def, List vars) { StringBuilder varOptions = new StringBuilder(); for (String var : vars) varOptions.append( String.format("%s %s ", OptionParams.OPTION_VAR, var == null ? "" : var) ); log( CommandDirection.FROM_ENGINE, UCIConstants.OPTION, String.format( "%s %s %s %s %s %s %s", OptionParams.OPTION_NAME, name, OptionParams.OPTION_TYPE, OptionType.COMBO.type(), OptionParams.OPTION_DEFAULT, def == null ? "" : def, varOptions.toString().trim() ) ); } @Override public void terminate(String params) { log(CommandDirection.FROM_ENGINE, UCIConstants.TERMINATE, params); } private void log(CommandDirection direction, String name, String params) { System.out.println( String.format( CommandDirection.TO_ENGINE.equals(direction) ? LOG_TO_FORMAT : LOG_FROM_FORMAT, name, params != null ? params : "" ).trim() ); } }