// This is the main loop. It samples all boards and tries at regular intervals
// to find new boards and to mark non-answering boards as, well, non-answering.
Этоголовная часть. Она опрашивает все доски с регулярным интервалом времени,
чтобы найти новые доски и отметить неотвечающие доски как неотвечающие
public void run() {
rdgtController c = new rdgtController();
if (c.connect(comport) == false) return;
System.out.println(rdgtMysql.connect());
int i = 0;
while (true) {
if ((i==0) || (c.db.count_alive()==0)) {
while (c.find_all_boards()==0) {} c.dump_all_active_boards();
i = 15;
} else {
try {Thread.sleep(1000);} catch(Throwable t) {}}
c.poll_all_active_boards();
i--;
}
}
}
rdgtChessboard.java
---
class rdgtChessboard {
Integer address;
rdgtHistory history = new rdgtHistory(this);
rdgtSnapshot snapshot = new rdgtSnapshot(this);
rdgtFenhax myFenhax = new rdgtFenhax();
boolean alive;
boolean dumped;
boolean updated;
rdgtDatabase db;
rdgtChessBoardForm board = null;
public rdgtChessboard(Integer _address, rdgtDatabase _db) {
address = _address;
db = _db;
alive = true;
dumped = false;
updated = false;
update_html();
board = new rdgtChessBoardForm();
new Thread(new Runnable() {
public void run() {
board.setVisible(true);
}
}).start();
}
rdgtSnapshot get_snapshot() {
return snapshot;
}
int get_address() {
return address.intValue();
}
public void print_position() {
board.printPosition(snapshot.debugprint());
board.printClock(snapshot.debugprint_clock());
}
String print_figure(int figure) {
if(figure == rdgtProtocol.EMPTY) {
return " ";
} else if (figure == rdgtProtocol.WPAWN) {
return "P";
} else if(figure == rdgtProtocol.WROOK) {
return "R";
} else if(figure == rdgtProtocol.WKNIGHT) {
return "N";
} else if(figure == rdgtProtocol.WBISHOP) {
return "B";
} else if(figure == rdgtProtocol.WQUEEN) {
return "Q";
} else if(figure == rdgtProtocol.WKING) {
return "K";
} else if (figure == rdgtProtocol.BPAWN) {
return "p";
} else if(figure == rdgtProtocol.BROOK) {
return "r";
} else if(figure == rdgtProtocol.BKNIGHT) {
return "n";
} else if(figure == rdgtProtocol.BBISHOP) {
return "b";
} else if(figure == rdgtProtocol.BQUEEN) {
return "q";
} else if(figure == rdgtProtocol.BKING) {
return "k";
} else {
return " ";
}
}
void set_alive(boolean x) {
alive = x;
update_html();
}
void set_dumped(boolean x) {
dumped = x;
}
void set_updated(boolean x) {
updated = x;
history.append(snapshot);
}
boolean get_alive() {
return alive;
}
boolean get_dumped() {
return dumped;
}
boolean get_updated() {
return updated;
}
void set_clockdata(boolean running, boolean batteryLow, boolean frontViewLeftSideHigh, boolean blacksTurn, boolean whitesTurn, int secW, int secB) {
snapshot.set_clockdata(running, batteryLow, frontViewLeftSideHigh, blacksTurn, whitesTurn, secW, secB);
snapshot.debugprint_clock();
}
void update_html() {
rdgtMysql.update_snapshot(snapshot);
String fen = rdgtMysql.snapshot2fen(snapshot);
myFenhax.add(fen);
rdgtMysql.update_moves(rdgtFenhax.getMoves(true, false), rdgtFenhax.getMoves(true, true), address.intValue());
}
void set_emptyboard() {
snapshot.set_emptyboard();
history.append(snapshot);
}
void set_boarddump(int[] all64, int startpos) {
snapshot.set_boarddump(all64, startpos);
dumped = true;
history.append(snapshot);
}
void set_fieldupdate(int piece, int pos) {
snapshot.set_fieldupdate(piece, pos);
updated = true;
history.append(snapshot);
}
}
rdgtConfig.java
---
class rdgtConfig {
String getDbServer() { return server; }
String getDbUser() { return user; }
String getDbPass() { return pass; }
String getDbBase() { return dbase; }
boolean useDbServer() { return mysql; }
boolean usePgnWriter() { return pgn; }
String getPgnFileName() { return pgnfile; }
String server= "localhost";
String user= "root";
String pass= "zar1562";
String dbase= "rdgt_development";
boolean mysql = true;
boolean pgn = false;
String pgnfile = "default.pgn";
}
rdgtController.java
---
class rdgtController extends rdgtProtocol {
rdgtDatabase db;
rdgtInterpreter ipr;
rdgtReceiver rec;
rdgtSerialport ser;
rdgtSender snd;
public rdgtController() {
db = new rdgtDatabase();
ipr = new rdgtInterpreter(db);
rec = new rdgtReceiver(ipr);
ser = new rdgtSerialport(rec);
snd = new rdgtSender(ser);
}
boolean connect(String comport) {
if (ser.open(comport)==false) {
System.out.println("Could not open "+comport);
return false;
}
System.out.println("COM port opened: "+comport);
return true;
}
int find_all_boards() {
System.out.println("Scanning for boards...");
db.unalive_all();
int alive = 0;
while (true) {
try {Thread.sleep(300);} catch(Throwable t) {}
snd.send(DGT_BUS_PING, 0);
try {Thread.sleep(1100);} catch(Throwable t) {}
int alivenow = db.count_alive();
if (alivenow == alive) break;
alive = alivenow;
for (Enumeration e = db.get_boards() ; e.hasMoreElements() ;) {
rdgtChessboard b = (rdgtChessboard)(e.nextElement());
if (b.get_alive()==true) {
snd.send(DGT_BUS_IGNORE_NEXT_BUS_PING, b.get_address());
}
}
}
System.out.println("Scanning completed, found "+Integer.toString(alive)+" boards.\n");
return alive;
}
void dump_all_active_boards() {
for (Enumeration e = db.get_boards() ; e.hasMoreElements() ;) {
rdgtChessboard b = (rdgtChessboard)(e.nextElement());
if (b.get_alive()==true) {
b.set_dumped(false);
for (int i=0; i<3; i++) { if (b.get_dumped()==true) break;
snd.send(DGT_BUS_SEND_BRD, b.get_address());
try {Thread.sleep(200);} catch(Throwable t) {}
}
if (b.get_dumped()==false) {
System.out.println("Board "+Integer.toString(b.get_address())+" does not reply to dump_board.");
b.set_alive(false);
}
}
}
}
void poll_all_active_boards() {
int TIMEOUT = 100;
for (Enumeration e = db.get_boards() ; e.hasMoreElements() ;) {
rdgtChessboard b = (rdgtChessboard)(e.nextElement());
if (b.get_alive()==true) {
snd.send(DGT_BUS_SEND_CLK, b.get_address());
try {Thread.sleep(TIMEOUT);} catch(Throwable t) {}
b.set_updated(false);
snd.send(DGT_BUS_SEND_CHANGES, b.get_address());
try {Thread.sleep(TIMEOUT);} catch(Throwable t) {}
for (int i=0; i<2; i=i+1) {
if (b.get_updated()==true) break;
snd.send(DGT_BUS_REPEAT_CHANGES, b.get_address());
try {Thread.sleep(TIMEOUT);} catch(Throwable t) {}}
if (b.get_updated()==false) {
System.out.println("Board "+Integer.toString(b.get_address())+" does not reply anymore.");
b.set_alive(false);
} else {
b.print_position();
}
}
}
}
}
rdgtDatabase.java
---
class rdgtDatabase {
Hashtable boards;
public rdgtDatabase() {
boards = new Hashtable();
}
rdgtChessboard get_board(int address) {
Integer _address = new Integer(address);
rdgtChessboard b = (rdgtChessboard)(boards.get(_address));
if (b==null) {
System.out.println("New board found: ID="+_address.toString());
b = new rdgtChessboard(_address, this);
boards.put(_address, b);
}
return b;
}
int get_boardcount() {
return boards.size();
}
Enumeration get_boards() {
return boards.elements();
}
void unalive_all() {
for (Enumeration e = boards.elements() ; e.hasMoreElements() ;) {
((rdgtChessboard)(e.nextElement())).set_alive(false);
}
}
int count_alive() {
int x = 0;
for (Enumeration e = boards.elements() ; e.hasMoreElements() ;) {
if (((rdgtChessboard)(e.nextElement())).get_alive()==true) x++;
}
return x;
}
}
rdgtFenboard.java
---
class rdgtFenboard {
int[] itsBoard;
String itsFen;
public rdgtFenboard(){
itsBoard = new int[64];
for(int i=0;i<64;i++)
itsBoard[i]='.';
itsFen="";
}
public rdgtFenboard(String fen){ setBoard(fen); }
public rdgtFenboard(int[] board) { setBoard(board); }
public boolean setBoard(String fen){
if(!validateFen(fen))
return false;
itsBoard=fen2board(fen);
itsFen=fen;
return true;
}
public int getPieceAt(int x, int y){
if(x<0 || x>7 || y<0 || y>7) return 0;
return itsBoard[y*8+x];
}
public boolean setBoard(int[] board){
if(board.length != 64)
return false;
itsBoard=board;
return true;
}
public void print(){
printBoard(itsBoard);
}
public int[] getBoard() { return itsBoard; }
public rdgtFenboard mask(rdgtFenboard b){
rdgtFenboard output;
output=new rdgtFenboard(mask(getBoard(),b.getBoard()));
return output;
}
public int diffCount(rdgtFenboard b){
rdgtFenboard m = mask(b);
int[] table=m.getBoard();
int output=0;
for(int i=0;i<64;i++){
if(table[i]!='.')
output++;
}
return output;
}
int[] fen2board(String aFen){
int[] board = new int[64];
int square=0;
int chr=0;
while(square<64){
char c = aFen.charAt(chr);
if(c=='r'||c=='R'||c=='n'||c=='N'||c=='b'||c=='B'||c=='q'||c=='Q'||c=='k'||c=='K'||c=='p'||c=='P'){
board[square]=c;
square++;
}
if(c>='1' && c<='8'){
for(int i=0;i<c-'1'+1;i++){
board[square]='.';
square++;
}
}
chr++;
}
return board;
}
String[] outputBoard(int[] board){
String s="";
String[] out = new String[8];
for(int chr=0;chr<64;chr++){
if(board[chr]<0)
s += "-" + (char)(Math.abs(board[chr]));
else if(board[chr]==0)
s += ", ";
else
s += (char)board[chr] + " ";
if((chr+1)%8==0){
out[chr/8]=s;
s="";
}
}
return out;
}
void printBoard(int[] board){
String[] out = outputBoard(board);
for(int i=0;i<8;i++){
System.out.println(out[i]);
}
System.out.println();
}
int[] mask(int[] b1, int[] b2){
int[] output=new int[64];
for(int i=0;i<64;i++){
if(b2[i]!=b1[i]){
if(b2[i]=='.')
output[i]=-b1[i];
else
output[i]=b2[i];
}
else
output[i] = '.';//b2[i]-b1[i];
}
return output;
}
boolean validateFen(String aFen){
/* Only check board part of FEN */
if(aFen.indexOf(' ')>0){
aFen = aFen.substring(0,aFen.indexOf(' '));
}
int s=0;
for(int c=0;c<aFen.length();c++){
if(aFen.charAt(c) == '/') s++;
}
if(s!=7) return false;
int x=0;
for(int c=0;c<aFen.length();c++){
if(aFen.charAt(c) == '/'){
if(x!=8) return false;
else x=0;
}
else
{
if(aFen.charAt(c) >= '1' && aFen.charAt(c)<= '8')
x+= aFen.charAt(c)-'1' ;
x++;
}
}
if(x!=8) return false;
return true;
}
}
rdgtFendiff.java
---
class rdgtFendiff {
char piece;
boolean added;
int square;
rdgtFenboard before;
rdgtFenboard after;
public rdgtFenboard getBefore() { return before; }
public rdgtFenboard getAfter() { return after; }
public char getPiece() { return piece; }
public boolean isAdded() { return added; }
public int getSquare() { return square; }
public boolean isWhite(){
if(piece>'A' && piece < 'Z')
return true;
else
return false;
}
public boolean equals(rdgtFendiff other){
if(getPiece() != other.getPiece()) return false;
if(getSquare() != other.getSquare()) return false;
if(isAdded() != other.isAdded()) return false;
return true;
}
public rdgtFendiff() {
secured=false;
}
public rdgtFendiff(char piece, boolean added, int square){
set(piece,added,square);
}
public rdgtFendiff(int piece, int square){
if(piece<0){
this.piece = (char)(-piece);
added=false;
}
else {
this.piece=(char)piece;
added=true;
}
this.square=square;
}
public rdgtFendiff(int piece,int square, rdgtFenboard before, rdgtFenboard after){
this.before=before;
this.after=after;
if(piece<0){
this.piece = (char)(-piece);
added=false;
}
else {
this.piece=(char)piece;
added=true;
}
this.square=square;
}
public void set(char piece, boolean added, int square){
this.piece=piece;
this.added=added;
this.square=square;
}
public String toString() {
String output = ""+piece;
if(added) output += "@";
else output += "&";
output += (char)((square)%8+'a') + "" + (8-square/8);
return output;
}
}
rdgtFenhax.java
---
public class rdgtFenhax {
static boolean debug=false;
static boolean rotated=false;
public rdgtFenhax(){
boards=new ArrayList();
moves=new movelist();
diffTable=new ArrayList();
result="?-?";
plyCount=0;
}
static String getMoves(boolean shortNotation, boolean nice) {
calcMoves();
return moves.getAll(shortNotation,nice);
}
static String getMoves(){
return getMoves(false,false);
}
static int getPlyCount(){
calcMoves();
return moves.size();
}
static void clear(){
boards.clear();
moves.clear();
diffTable.clear();
result="?-?";
}
static String getResult() { return result; }
static boolean add(String aFen){
rdgtFenboard f = new rdgtFenboard();
if(!f.setBoard(aFen))
return false;
boards.add(f);
if(aFen.indexOf("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR")>=0){
clear();
boards.add(f);
}
if((aFen.indexOf("rnbq1bnr/pppppppp") >=0 && aFen.indexOf("PPPPPPPP/RNBQ1BNR") >= 0)||
(aFen.indexOf("rnbqkbnr/pppppppp") >=0 && aFen.indexOf("PPPPPPPP/RNBQ1BNR") >= 0)||
(aFen.indexOf("rnbq1bnr/pppppppp") >=0 && aFen.indexOf("PPPPPPPP/RNBQKBNR") >= 0)){
int[] b=f.getBoard();
boolean black=false;
boolean white=false;
for(int i=16;i<=47;i++){
if(b[i] == 'k') black=true;
if(b[i] == 'K') white=true;
}
if(black && white) result="1/2-1/2";
else if(black) result="0-1";
else if(white) result="1-0";
}
if(boards.size()>=2){
rdgtFenboard last = (rdgtFenboard)boards.get(boards.size()-1);
rdgtFenboard nextlast = (rdgtFenboard)boards.get(boards.size()-2);
if(last.diffCount(nextlast) < 5){
rdgtFenboard m=nextlast.mask(last);
int[] mask = m.getBoard();
for(int i=0;i<64;i++){
if(mask[i] != '.' && mask[i]<0){
diffTable.add(new rdgtFendiff(mask[i],i,nextlast,last));
}
}
for(int i=0;i<64;i++){
if(mask[i] != '.' && mask[i]>0){
diffTable.add(new rdgtFendiff(mask[i],i,nextlast,last));