|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectsoc.client.SOCDisplaylessPlayerClient
soc.robot.SOCRobotClient
public class SOCRobotClient
This is a robot client that can play Settlers of Catan.
Once connected, messages from the server are processed in treat(SOCMessage)
.
For each game this robot client plays, there is a SOCRobotBrain
.
The built-in robots must be the same version as the server, to simplify things. Third-party bots might be based on this code and be other versions, to simplify their maintenance.
The IMAROBOT
connect message gives the bot's class name and
a required security cookie, which is passed into the robot client constructor and which must
match the server's generated cookie. You can set the server's cookie by setting the
server's jsettlers.bots.cookie
parameter, or view it by setting jsettlers.bots.showcookie
,
when starting the server.
Once a bot has connected to the server, it waits to be asked to join games via
ROBOTJOINREQUEST
messages. When it receives that
message type, the bot replies with JOINGAME
and the server
responds with JOINGAMEAUTH
. That message handler creates
a SOCRobotBrain
to play the game it is joining.
Field Summary | |
---|---|
private java.util.Hashtable<java.lang.String,CappedQueue<SOCMessage>> |
brainQs
the message queues for the different brains |
protected int |
cleanBrainKills
number of clean brain kills |
private java.lang.String |
cookie
The security cookie value; required by server v1.1.19 and higher. |
static java.lang.String |
CURRENT_PLANS
constants for debug recording |
static java.lang.String |
CURRENT_RESOURCES
|
private SOCRobotParameters |
currentRobotParameters
the current robot parameters for robot brains |
private static boolean |
debugRandomPause
For debugging/regression testing, randomly pause responding for several seconds, to simulate a "stuck" robot brain. |
private static double |
DEBUGRANDOMPAUSE_FREQ
When debugRandomPause is true but not debugRandomPauseActive ,
frequency of activating it; checked for each non-SOCGameTextMsg
and non-SOCGameServerText message received during our own turn. |
private static int |
DEBUGRANDOMPAUSE_SECONDS
When debugRandomPauseActive is activated, pause this many seconds
before continuing. |
private boolean |
debugRandomPauseActive
Is debugRandomPause currently in effect for this client?
If so, store messages into debugRandomPauseQueue instead of
sending them to robotBrains immediately. |
private java.util.Vector<SOCMessage> |
debugRandomPauseQueue
When debugRandomPauseActive is true, store incoming messages
from the server into this queue until debugRandomPauseUntil . |
private long |
debugRandomPauseUntil
When debugRandomPauseActive is true, resume at this time;
same format as System.currentTimeMillis() . |
private java.util.Hashtable<java.lang.String,java.util.Map<java.lang.String,SOCGameOption>> |
gameOptions
options for all games on the server we've been asked to join. |
protected int |
gamesFinished
number of games finished |
protected int |
gamesPlayed
number of games this bot has played |
protected int |
gamesWon
number of games this bot has won |
(package private) boolean |
printedInitialWelcome
Have we printed the initial welcome msg from server? Suppress further ones (disconnect-reconnect). |
private java.lang.Thread |
readerRobot
the thread that reads incoming messages |
(package private) SOCRobotResetThread |
resetThread
used to maintain connection |
private java.util.Hashtable<java.lang.String,SOCRobotBrain> |
robotBrains
the robot's "brains", 1 for each game this robot is currently playing. |
private java.util.Hashtable<java.lang.String,java.lang.Integer> |
seatRequests
a table of requests from the server to sit at games |
protected long |
startTime
start time |
Fields inherited from class soc.client.SOCDisplaylessPlayerClient |
---|
channels, connected, doc, ex, games, gotPassword, host, in, lastMessage, nickname, out, password, port, reader, rejected, s, sFeatures, sLocal, sLocalFeatures, sLocalVersion, STATSPREFEX, strSocketName, sVersion |
Constructor Summary | |
---|---|
SOCRobotClient(java.lang.String h,
int p,
java.lang.String nn,
java.lang.String pw,
java.lang.String co)
Constructor for connecting to the specified host, on the specified port |
|
SOCRobotClient(java.lang.String s,
java.lang.String nn,
java.lang.String pw,
java.lang.String co)
Constructor for connecting to a local game (practice) on a local stringport. |
Method Summary | |
---|---|
void |
addCleanKill()
add one the the number of clean brain kills |
void |
debugPrintBrainStatus(java.lang.String gameName,
boolean sendTextToGame)
Print brain variables and status for this game, to System.err
or as SOCGameTextMsg sent to the game's members,
by calling SOCRobotBrain.debugPrintBrainStatus() . |
void |
destroy()
losing connection to server; leave all games, then try to reconnect |
void |
disconnectReconnect()
disconnect and then try to reconnect. |
protected void |
handleADMINPING(SOCAdminPing mes)
handle the admin ping message |
protected void |
handleADMINRESET(SOCAdminReset mes)
handle the admin reset message |
protected void |
handleCHANGEFACE(SOCChangeFace mes)
handle the "change face" message |
protected void |
handleCLEARTRADEMSG(SOCClearTradeMsg mes)
handle the "clear trade" message |
protected void |
handleDELETEGAME(SOCDeleteGame mes)
handle the "delete game" message |
protected void |
handleGAMEMEMBERS(SOCGameMembers mes)
handle the "game members" message, which indicates the entire game state has now been sent. |
protected void |
handleGAMESTATE(SOCGameState mes)
handle the "game state" message |
private void |
handleGAMETEXTMSG_debug(SOCGameTextMsg mes)
Handle debug text messages from players to the robot, which start with the robot's nickname + ":". |
protected void |
handleGAMETEXTMSG(SOCGameTextMsg mes)
handle the "game text message" message |
protected void |
handleJOINGAME(SOCJoinGame mes)
handle the "join game" message |
protected void |
handleJOINGAMEAUTH(SOCJoinGameAuth mes,
boolean isPractice)
handle the "join game authorization" message |
protected void |
handleLARGESTARMY(SOCLargestArmy mes)
handle the "largest army" message |
protected void |
handleLONGESTROAD(SOCLongestRoad mes)
handle the "longest road" message |
protected void |
handlePutBrainQ(SOCMessageForGame mes)
handle any per-game message that just needs to go into its game's brainQs . |
protected void |
handlePUTPIECE(SOCPutPiece mes)
handle the "put piece" message |
protected void |
handleRESETBOARDAUTH(SOCResetBoardAuth mes)
handle board reset (new game with same players, same game name). |
protected void |
handleROBOTDISMISS(SOCRobotDismiss mes)
handle the "dismiss robot" message |
protected void |
handleROBOTJOINGAMEREQUEST(SOCRobotJoinGameRequest mes)
handle the "join game request" message. |
protected void |
handleSERVERPING(SOCServerPing mes)
handle the server ping message. |
protected void |
handleSITDOWN(SOCSitDown mes)
handle the "someone is sitting down" message |
protected void |
handleSTATUSMESSAGE(SOCStatusMessage mes)
handle the "status message" message by printing it to System.err; messages with status value 0 are ignored (no problem is being reported) once the initial welcome message has been printed. |
protected void |
handleUPDATEROBOTPARAMS(SOCUpdateRobotParams mes)
handle the update robot params message |
void |
init()
Initialize the robot player; connect to server, send first messages |
void |
leaveGame(SOCGame ga,
java.lang.String leaveReason,
boolean showDebugTrace)
the user leaves the given game |
static void |
main(java.lang.String[] args)
for stand-alones |
protected void |
sendRecordsText(SOCGame ga,
java.util.Vector<java.lang.String> record)
Call sendText on each string element of record. |
void |
treat(SOCMessage mes)
Treat the incoming messages. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final java.lang.String CURRENT_PLANS
public static final java.lang.String CURRENT_RESOURCES
private static boolean debugRandomPause
Note: This debugging tool is not scalable to many simultaneous games, because it delays all messages, not just ones for a specific game / brain, and it won't be our turn in each of those games.
DEBUGRANDOMPAUSE_FREQ
,
debugRandomPauseActive
private boolean debugRandomPauseActive
debugRandomPause
currently in effect for this client?
If so, store messages into debugRandomPauseQueue
instead of
sending them to robotBrains
immediately.
The pause goes on until debugRandomPauseUntil
arrives.
This is all handled within treat(SOCMessage)
.
private java.util.Vector<SOCMessage> debugRandomPauseQueue
debugRandomPauseActive
is true, store incoming messages
from the server into this queue until debugRandomPauseUntil
.
Initialized in treat(SOCMessage)
.
private long debugRandomPauseUntil
debugRandomPauseActive
is true, resume at this time;
same format as System.currentTimeMillis()
.
DEBUGRANDOMPAUSE_SECONDS
private static final double DEBUGRANDOMPAUSE_FREQ
debugRandomPause
is true but not debugRandomPauseActive
,
frequency of activating it; checked for each non-SOCGameTextMsg
and non-SOCGameServerText
message received during our own turn.
private static final int DEBUGRANDOMPAUSE_SECONDS
debugRandomPauseActive
is activated, pause this many seconds
before continuing.
debugRandomPauseUntil
,
Constant Field Valuesprivate java.lang.String cookie
private java.lang.Thread readerRobot
private SOCRobotParameters currentRobotParameters
private java.util.Hashtable<java.lang.String,SOCRobotBrain> robotBrains
SOCDisplaylessPlayerClient.games
private java.util.Hashtable<java.lang.String,CappedQueue<SOCMessage>> brainQs
private java.util.Hashtable<java.lang.String,java.lang.Integer> seatRequests
private java.util.Hashtable<java.lang.String,java.util.Map<java.lang.String,SOCGameOption>> gameOptions
SOCDisplaylessPlayerClient.games
once joined.
Key = game name, Value = map of game's SOCGameOption
s.
Entries are added in handleROBOTJOINGAMEREQUEST(SOCRobotJoinGameRequest)
.
Since the robot and server are the same version, the
set of "known options" will always be in sync.
protected int gamesPlayed
protected int gamesFinished
protected int gamesWon
protected int cleanBrainKills
protected long startTime
SOCRobotResetThread resetThread
boolean printedInitialWelcome
Constructor Detail |
---|
public SOCRobotClient(java.lang.String h, int p, java.lang.String nn, java.lang.String pw, java.lang.String co)
h
- hostp
- portnn
- nickname for robotpw
- password for robotco
- cookie for robot connections to serverpublic SOCRobotClient(java.lang.String s, java.lang.String nn, java.lang.String pw, java.lang.String co)
s
- the stringport that the server listens onnn
- nickname for robotpw
- password for robotco
- cookie for robot connections to serverMethod Detail |
---|
public void init()
public void disconnectReconnect()
SOCDisplaylessPlayerClient.ex
is set. Otherwise ex is null.
public void treat(SOCMessage mes)
SOCMessage.toMsg(String)
).
Note: Currently, does not call SOCDisplaylessPlayerClient.treat(SOCMessage)
.
New messages should be added in both places if both displayless and robot should handle them.
treat
in class SOCDisplaylessPlayerClient
mes
- the messageprotected void handleSERVERPING(SOCServerPing mes)
mes
- the messageprotected void handleADMINPING(SOCAdminPing mes)
mes
- the messageprotected void handleADMINRESET(SOCAdminReset mes)
mes
- the messageprotected void handleUPDATEROBOTPARAMS(SOCUpdateRobotParams mes)
mes
- the messageprotected void handleROBOTJOINGAMEREQUEST(SOCRobotJoinGameRequest mes)
seatRequests
.
Send a JOINGAME
to server in response.
Server will reply with JOINGAMEAUTH
.
Board resets are handled similarly.
mes
- the messagehandleRESETBOARDAUTH(SOCResetBoardAuth)
protected void handleSTATUSMESSAGE(SOCStatusMessage mes)
handleSTATUSMESSAGE
in class SOCDisplaylessPlayerClient
mes
- the messageprotected void handleJOINGAMEAUTH(SOCJoinGameAuth mes, boolean isPractice)
handleJOINGAMEAUTH
in class SOCDisplaylessPlayerClient
mes
- the messageisPractice
- Is the server local for practice, or remote?protected void handleJOINGAME(SOCJoinGame mes)
handleJOINGAME
in class SOCDisplaylessPlayerClient
mes
- the messageprotected void handleGAMEMEMBERS(SOCGameMembers mes)
seatRequests
for this game, sit down now.
handleGAMEMEMBERS
in class SOCDisplaylessPlayerClient
mes
- the messageprotected void handlePutBrainQ(SOCMessageForGame mes)
brainQs
.
This includes all messages that the SOCRobotBrain
needs to react to.
mes
- the messageprotected void handleGAMETEXTMSG(SOCGameTextMsg mes)
handleGAMETEXTMSG
in class SOCDisplaylessPlayerClient
mes
- the messageprivate final void handleGAMETEXTMSG_debug(SOCGameTextMsg mes)
protected void handleSITDOWN(SOCSitDown mes)
handleSITDOWN
in class SOCDisplaylessPlayerClient
mes
- the messageprotected void handleDELETEGAME(SOCDeleteGame mes)
handleDELETEGAME
in class SOCDisplaylessPlayerClient
mes
- the messageprotected void handleGAMESTATE(SOCGameState mes)
handleGAMESTATE
in class SOCDisplaylessPlayerClient
mes
- the messageprotected void handlePUTPIECE(SOCPutPiece mes)
mes
- the messageprotected void handleCLEARTRADEMSG(SOCClearTradeMsg mes)
handleCLEARTRADEMSG
in class SOCDisplaylessPlayerClient
mes
- the messageprotected void handleROBOTDISMISS(SOCRobotDismiss mes)
mes
- the messageprotected void handleCHANGEFACE(SOCChangeFace mes)
handleCHANGEFACE
in class SOCDisplaylessPlayerClient
mes
- the messageprotected void handleLONGESTROAD(SOCLongestRoad mes)
handleLONGESTROAD
in class SOCDisplaylessPlayerClient
mes
- the messageprotected void handleLARGESTARMY(SOCLargestArmy mes)
handleLARGESTARMY
in class SOCDisplaylessPlayerClient
mes
- the messageprotected void handleRESETBOARDAUTH(SOCResetBoardAuth mes)
SOCGame.resetAsCopy()
.
Take robotbrain out of old game, don't yet put it in new game.
Let server know we've done so, by sending LEAVEGAME via leaveGame(SOCGame, String, boolean)
.
Server will soon send a ROBOTJOINGAMEREQUEST if we should join the new game.
handleRESETBOARDAUTH
in class SOCDisplaylessPlayerClient
mes
- the messageSOCServer.resetBoardAndNotify(String, int)
,
handleROBOTJOINGAMEREQUEST(SOCRobotJoinGameRequest)
protected void sendRecordsText(SOCGame ga, java.util.Vector<java.lang.String> record)
ga
- Game to sendText torecord
- Strings to send, or nullpublic void debugPrintBrainStatus(java.lang.String gameName, boolean sendTextToGame)
System.err
or as SOCGameTextMsg
sent to the game's members,
by calling SOCRobotBrain.debugPrintBrainStatus()
.
gameName
- Game name; if no brain for that game, do nothing.sendTextToGame
- Send to game as SOCGameTextMsg
if true,
otherwise print to System.err
.public void leaveGame(SOCGame ga, java.lang.String leaveReason, boolean showDebugTrace)
ga
- the gameleaveReason
- reason for leavingpublic void addCleanKill()
public void destroy()
destroy
in class SOCDisplaylessPlayerClient
public static void main(java.lang.String[] args)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |