[LIB-9] Add chesshog-qrcode module
[chesshog.git] / chesshog-uci / src / main / java / org / hedgecode / chess / uci / AcceptorStub.java
1 /*
2  * Copyright (c) 2018. Developed by Hedgecode.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.hedgecode.chess.uci;
18
19 import java.util.List;
20
21 import org.hedgecode.chess.uci.annotation.CommandDirection;
22 import org.hedgecode.chess.uci.command.OptionParams;
23 import org.hedgecode.chess.uci.command.OptionType;
24
25 /**
26  *
27  *
28  * @author Dmitry Samoshin aka gotty
29  */
30 public class AcceptorStub implements Acceptor {
31
32     private static final String LOG_TO_FORMAT = ">> %s %s";
33     private static final String LOG_FROM_FORMAT = "<< %s %s";
34
35     private CommandExecutor commandExecutor;
36
37     AcceptorStub() {
38     }
39
40     public void init(CommandExecutor executor) {
41         commandExecutor = executor;
42     }
43
44     @Override
45     public void start() {
46         commandExecutor.exec(
47                 UCIConstants.UCI, CommandDirection.TO_ENGINE, null
48         );
49         log(CommandDirection.TO_ENGINE, UCIConstants.UCI, null);
50     }
51
52     @Override
53     public void id(String params) {
54         log(CommandDirection.FROM_ENGINE, UCIConstants.ID, params);
55     }
56
57     @Override
58     public void uciOK(String params) {
59         log(CommandDirection.FROM_ENGINE, UCIConstants.UCI_OK, params);
60         commandExecutor.exec(
61                 UCIConstants.IS_READY, CommandDirection.TO_ENGINE, null
62         );
63         log(CommandDirection.TO_ENGINE,  UCIConstants.IS_READY, null);
64     }
65
66     @Override
67     public void readyOK(String params) {
68         log(CommandDirection.FROM_ENGINE, UCIConstants.READY_OK, params);
69         commandExecutor.exec(
70                 UCIConstants.QUIT, CommandDirection.TO_ENGINE, null
71         );
72         log(CommandDirection.TO_ENGINE, UCIConstants.QUIT, null);
73     }
74
75     @Override
76     public void setOption(String name) {
77         log(
78                 CommandDirection.FROM_ENGINE,
79                 UCIConstants.OPTION,
80                 String.format(
81                         "%s %s %s %s",
82                         OptionParams.OPTION_NAME, name, OptionParams.OPTION_TYPE, OptionType.BUTTON.type()
83                 )
84         );
85     }
86
87     @Override
88     public void setOption(String name, boolean def) {
89         log(
90                 CommandDirection.FROM_ENGINE,
91                 UCIConstants.OPTION,
92                 String.format(
93                         "%s %s %s %s %s %b",
94                         OptionParams.OPTION_NAME, name, OptionParams.OPTION_TYPE, OptionType.CHECK.type(),
95                         OptionParams.OPTION_DEFAULT, def
96                 )
97         );
98     }
99
100     @Override
101     public void setOption(String name, int def, int min, int max) {
102         log(
103                 CommandDirection.FROM_ENGINE,
104                 UCIConstants.OPTION,
105                 String.format(
106                         "%s %s %s %s %s %d %s %d %s %d",
107                         OptionParams.OPTION_NAME, name, OptionParams.OPTION_TYPE, OptionType.SPIN.type(),
108                         OptionParams.OPTION_DEFAULT, def, OptionParams.OPTION_MIN, min, OptionParams.OPTION_MAX, max
109                 )
110         );
111     }
112
113     @Override
114     public void setOption(String name, String def) {
115         log(
116                 CommandDirection.FROM_ENGINE,
117                 UCIConstants.OPTION,
118                 String.format(
119                         "%s %s %s %s %s %s",
120                         OptionParams.OPTION_NAME, name, OptionParams.OPTION_TYPE, OptionType.STRING.type(),
121                         OptionParams.OPTION_DEFAULT, def == null ? "" : def
122                 )
123         );
124     }
125
126     @Override
127     public void setOption(String name, String def, List<String> vars) {
128         StringBuilder varOptions = new StringBuilder();
129         for (String var : vars)
130             varOptions.append(
131                     String.format("%s %s ", OptionParams.OPTION_VAR, var == null ? "" : var)
132             );
133         log(
134                 CommandDirection.FROM_ENGINE,
135                 UCIConstants.OPTION,
136                 String.format(
137                         "%s %s %s %s %s %s %s",
138                         OptionParams.OPTION_NAME, name, OptionParams.OPTION_TYPE, OptionType.COMBO.type(),
139                         OptionParams.OPTION_DEFAULT, def == null ? "" : def, varOptions.toString().trim()
140                 )
141         );
142     }
143
144     @Override
145     public void terminate(String params) {
146         log(CommandDirection.FROM_ENGINE, UCIConstants.TERMINATE, params);
147     }
148
149     private void log(CommandDirection direction, String name, String params) {
150         System.out.println(
151                 String.format(
152                         CommandDirection.TO_ENGINE.equals(direction)
153                                 ? LOG_TO_FORMAT
154                                 : LOG_FROM_FORMAT,
155                         name,
156                         params != null ? params : ""
157                 ).trim()
158         );
159     }
160
161 }