Смекни!
smekni.com

Обеспечение всемирной трансляции спортивных шахматных соревнований с применением разработанного в ходе проекта законченного программного продукта (стр. 23 из 27)

s.append("&bsol;n<td><a href=&bsol;"");

s.append(snapshotprefix);

s.append(Integer.toString(b.get_address()));

s.append(extension);

s.append("&bsol;">Link</a><td><a href=&bsol;"");

s.append(historyprefix);

s.append(Integer.toString(b.get_address()));

s.append(extension);

s.append("&bsol;">Link</a>&bsol;n");

}

s.append("</table>&bsol;n</body>&bsol;n</html>&bsol;n");

if (writefile(indexfilename+extension, s.toString()) == false) {

System.err.println("Error: Failed to write indexfile.");

return;

}

}

static void print_history(rdgtHistory h) {

StringBuffer s = new StringBuffer(2000);

s.append("<html>&bsol;n<head></head>&bsol;n");

s.append("<body><h1>History for Chessboard: ");

s.append(Integer.toString(h.board.get_address()));

s.append("</h1>&bsol;n");

int i = 0;

for (Enumeration e = h.history.elements() ; e.hasMoreElements() ;) {

rdgtSnapshot ss = (rdgtSnapshot)(e.nextElement());

i=i+1;

s.append("<p><hr size=1><p><h3>Move: ");

s.append(Integer.toString(i));

s.append(" &nbsp; &nbsp;");

s.append(ss.get_time().toString());

s.append(boardview(ss));

s.append("&bsol;n");

}

s.append("</body>&bsol;n</html>&bsol;n");

if (writefile(historyprefix + Integer.toString(h.board.get_address()) + extension, s.toString()) == false) {

System.err.println("Error: Failed to write historyfile.");

return;

}

}

static String boardview(rdgtSnapshot b) {

StringBuffer s = new StringBuffer(1000);

s.append("<table border=0 cellpadding=0 cellspacing=0>&bsol;n");

boolean filled = true;

for (int i=0; i<8; i=i+1) {

s.append("<tr>");

for (int j=0; j<8; j=j+1) {

s.append("<td><img src=&bsol;"images/");

int p = b.pieces[(i*8)+j];

if (p==EMPTY ) s.append("empty");

else if (p==WPAWN ) s.append("w_pawn");

else if (p==WROOK ) s.append("w_rook");

else if (p==WKNIGHT) s.append("w_knight");

else if (p==WBISHOP) s.append("w_bishop");

else if (p==WKING ) s.append("w_king");

else if (p==WQUEEN ) s.append("w_queen");

else if (p==BPAWN ) s.append("b_pawn");

else if (p==BROOK ) s.append("b_rook");

else if (p==BKNIGHT) s.append("b_knight");

else if (p==BBISHOP) s.append("b_bishop");

else if (p==BKING ) s.append("b_king");

else if (p==BQUEEN ) s.append("b_queen");

if (filled) s.append("1"); else s.append("2");

filled = !filled;

s.append(".gif&bsol;">");

}

filled = !filled;

s.append("</tr>&bsol;n");

}

s.append("&bsol;n</table>");

return s.toString();

}

static void print_snapshot(rdgtSnapshot ss) {

StringBuffer s = new StringBuffer(2000);

s.append("<html>&bsol;n<head>&bsol;n <meta http-equiv=refresh content=4>&bsol;n</head>&bsol;n");

s.append("<body><h1>Snapshot of Chessboard: ");

s.append(Integer.toString(ss.board.get_address()));

s.append("</h1>&bsol;n<h3>");

s.append(ss.get_time().toString());

s.append("</h3>&bsol;n");

s.append(boardview(ss));

s.append("&bsol;n</body>&bsol;n</html>&bsol;n");

if (writefile(snapshotprefix + Integer.toString(ss.board.get_address()) + extension, s.toString()) == false) {

System.err.println("Error: Failed to write snapshotfile.");

return;

}

}

static boolean writefile(String filename, String content) {

FileWriter f = null;

for (int i=0; i<10; i=i+1) {

try {

f = new FileWriter(filename);

break;

}

catch (Throwable t) {

System.err.println("Error opening "+filename+" for writing. Retrying...");

}

}

if (f==null) {

System.out.println("Failed to open file "+filename);

return false;

}

try {

f.write(content);

f.close();

}

catch (Exception e) {

System.out.println("Error writing file "+filename+". ("+e.toString()+")");

return false;

}

return true;

}

}

rdgtInterpreter.java

---

class rdgtInterpreter extends rdgtProtocol {

rdgtDatabase db;

public rdgtInterpreter(rdgtDatabase _db) {

db = _db;

}

public boolean interpret(int[] data) {

int len = ((int) data[1] * 0x80) + (int) data[2];

int address = ((int) data[3] * 0x80) + (int) data[4];

String tmp = "Len:" + Integer.toString(len) + " Address:" + Integer.toString(address);

if (data[0] == DGT_BUS_PING) {

System.out.println("Received message: PING " + tmp);

} else if (data[0] == DGT_BUS_UPDATE) {

System.out.println("Received message: UPDATE " + tmp);

} else if (data[0] == DGT_BUS_BWTIME) {

System.out.println("Received message: BWTIME " + tmp);

} else {

System.out.println("Received message: (some other message) " + tmp);

}

if (check_checksum(data) == false) {

System.out.println("Message failed checksum test");

return false;

}

if (data[0] == DGT_BUS_PING) {

if (data.length != 6) { // address+data

System.out.println("Illegal: Got a BUS_PING with wrong size");

return false;

}

db.get_board(address).set_alive(true);

return true;

} else if (data[0] == DGT_BUS_BRD_DUMP) {

if (data.length != (6 + 64)) {

System.out.println("Illegal: Got a BUS_BRD_DUMP with wrong size");

return false;

}

rdgtChessboard c = db.get_board(address);

c.set_boarddump(data, 5);

return true;

} else if (data[0] == DGT_BUS_UPDATE) {

byte pr[] = new byte[data.length - 5];

for (int i = 5; i < data.length; i++) {

pr[i - 5] = (byte) data[i];

}

System.out.println(new String(pr));

rdgtChessboard c = db.get_board(address);

for (int i = 5; i < data.length - 1; i++) {

if (data[i] == EE_POWERUP) {

}

else if (data[i] == EE_EOF) {

}

else if (data[i] == EE_FOURROWS) {

}

else if (data[i] == EE_EMPTYBOARD) {

c.set_emptyboard();

} else if (data[i] == EE_DOWNLOADED) {

}

else if (data[i] == EE_BEGINPOS) {

}

else if (data[i] == EE_BEGINPOS_ROT) {

}

else if (data[i] == EE_START_TAG) {

}

else if (data[i] == EE_WATCHDOG_ACTION) {

}

else if (data[i] == EE_NOP) {

}

else if (data[i] >= 0x40 && data[i] <= 0x5F) { // 2-byte piece update

c.set_fieldupdate(data[i] & 0x0F, data[i + 1]);

i++;

} else if ((data[i] >= 0x60 && data[i] <= 0x69) || (data[i] >= 0x70 && data[i] <= 0x79)) {

i += 2;

} else {

System.out.println("Unknown data in DGT_BUS_UPDATE command.");

return false;

}

}

c.set_updated(true);

return true;

}

else if (data[0] == DGT_BUS_BWTIME) {

if (data.length != 13) {

System.out.println("Illegal: Got a DGT_BUS_BWTIME with wrong size (!=10)");

return false;

}

if ((data[11] & 0x20) != 0) {

System.out.println("(No clock connected)");

return true;

}

boolean running = (data[11] & 0x01) != 0;

boolean batteryLow = (data[11] & 0x04) != 0;

boolean frontViewLeftSideHigh = (data[11] & 0x02) != 0;

boolean blackPlayersTurn = (data[11] & 0x08) != 0;

boolean whitePlayersTurn = (data[11] & 0x10) != 0;

int hoursW = data[5] & 0x0F;

int minutesW = (data[6] & 0x0F) + ((data[6] >> 4) * 10);

int secondsW = (data[7] & 0x0F) + ((data[7] >> 4) * 10);

int hoursB = data[8] & 0x0F;

int minutesB = (data[9] & 0x0F) + ((data[9] >> 4) * 10);

int secondsB = (data[10] & 0x0F) + ((data[10] >> 4) * 10);

secondsW = (((hoursW * 60) + minutesW) * 60) + secondsW;

secondsB = (((hoursB * 60) + minutesB) * 60) + secondsB;

db.get_board(address).set_clockdata(running, batteryLow, frontViewLeftSideHigh,

blackPlayersTurn, whitePlayersTurn, secondsW, secondsB);

return true;

} else if (data[0] == DGT_BUS_FROM_START) {

} else if (data[0] == DGT_BUS_START_GAME_WRITTEN) {

} else if (data[0] == DGT_BUS_VERSION) {

}

System.err.println("Unknown message (checksum was ok).");

return true;

}

boolean check_checksum(int[] data) {

int sum = 0;

for (int i = 0; i < (data.length - 1); i = i + 1) {

sum = (sum + data[i]) & 0x7F;

}

if (sum == data[data.length - 1]) {

return true;

}

return false;

}

}

rdgtMysql.java

---

class rdgtMysql extends rdgtProtocol {

static String server= "localhost";

static String user= "root";

static String pass= "zar1562";

static String dbase= "rdgt_development";

static String update_table= "games";

static Connection conn = null;

static Statement stmt = null;

static boolean rotated=false;

static String connect()

{

try

{

Class.forName("com.mysql.jdbc.Driver").newInstance();

conn = DriverManager.getConnection("jdbc:mysql://"+server+"/"+dbase+"?user="+user+"&password="+pass);

return "";

}

catch (SQLException e)

{

return e.getSQLState();

}

catch (Exception e)

{

return "0";

}

}

static boolean disconnect()

{

if (conn != null) {

try

{

conn.close();

return true;

}

catch (SQLException SQLE)

{

return false;

}

}

else

return false;

}

static String execute(String expr)

{

try

{

stmt = null;

stmt = conn.createStatement();

stmt.executeUpdate(expr);

return "";

}

catch (SQLException e)

{

return e.getSQLState();

}

catch (Exception e)

{

return "0";

}

finally

{

if (stmt != null) {

try

{

stmt.close();

}

catch (SQLException SQLE)

{

;

}

}

}

}

static String snapshot2fen (rdgtSnapshot b)

{

StringBuffer s = new StringBuffer(100);

int counter = 0;

for (int i=0; i<8; i++)

{

for (int j=0; j<8; j++)

{

if ((counter != 0) && (b.pieces[(i*8)+j] != EMPTY))

{

s.append(counter);

counter = 0;

}

switch(b.pieces[(i*8)+j])

{

case WPAWN:s.append("P"); break;

case WROOK: s.append("R"); break;

case WKNIGHT: s.append("N"); break;

case WBISHOP: s.append("B"); break;

case WKING: s.append("K"); break;

case WQUEEN: s.append("Q"); break;

case BPAWN: s.append("p"); break;

case BROOK: s.append("r"); break;

case BKNIGHT: s.append("n"); break;

case BBISHOP: s.append("b"); break;

case BKING: s.append("k"); break;

case BQUEEN: s.append("q"); break;

default: counter++; break;

}

}

if (counter != 0)

s.append(counter);

s.append("/");

counter = 0;

}

String outfen= s.toString().substring(0,s.toString().length()-1);

switch(b.board.address.intValue()){

case 3737:

case 3811:

case 2896:

case 3740:

case 3588:

case 2897:

default:

rotated=false; break;

}

if(rotated){

String newFen="";

for(int i=outfen.length()-1;i>=0;i--){

newFen += outfen.charAt(i);

}

outfen=newFen;

}

return outfen;

}

static void update_snapshot(rdgtSnapshot b)

{

if (conn != null)

{

String fen = snapshot2fen(b);

String turn;

if(rotated){

if(b.isWhiteTurn()) turn="Black";

else turn="White";

}

else{

if(b.isWhiteTurn()) turn="White";

else turn="Black";

}

String sql_command = "UPDATE "+update_table+" SET fen = '"+fen+"',wtime='"+b.getTimeWhite()+"',btime='"+b.getTimeBlack()+"',turn='"+turn+"' WHERE dgt_active like '%Fen%' AND dgt_board = "+b.board.address;

execute(sql_command);

System.out.println(b.board.address+" "+fen+" "+b.getTimeWhite()+" "+b.getTimeBlack()+" "+turn.substring(0,1));

}

}

static void update_moves(String moves, String movestable, int board_id)

{

if (conn != null)

{

int t=0;

if(moves.length()>20) t=moves.length()-20;

System.out.println(board_id + " Moves: " + moves.substring(t));

String sql_command = "UPDATE games SET moves = '"+moves+"' WHERE dgt_active like '%Moves%' AND dgt_board = "+board_id;

execute(sql_command);

}

}

}

rdgtProtocol.java

---

class rdgtProtocol {

static int DGT_TO_BUSMODE = 0x4a;

static int DGT_BUS_SEND_CLK = 0x01 | 0x80;

static int DGT_BUS_SEND_BRD = 0x02 | 0x80;

static int DGT_BUS_SEND_CHANGES = 0x03 | 0x80;

static int DGT_BUS_REPEAT_CHANGES = 0x04 | 0x80;

static int DGT_BUS_SET_START_GAME = 0x05 | 0x80;

static int DGT_BUS_SEND_FROM_START = 0x06 | 0x80;

static int DGT_BUS_PING = 0x07 | 0x80;

static int DGT_BUS_END_BUSMODE = 0x08 | 0x80;

static int DGT_BUS_RESET = 0x09 | 0x80;

static int DGT_BUS_IGNORE_NEXT_BUS_PING = 0x0a | 0x80;

static int DGT_BUS_SEND_VERSION = 0x0b | 0x80;

static int DGT_BUS_BRD_DUMP = 0x03 | 0x80;

static int DGT_BUS_BWTIME = 0x04 | 0x80;

static int DGT_BUS_UPDATE = 0x05 | 0x80;

static int DGT_BUS_FROM_START = 0x06 | 0x80;

static int DGT_BUS_START_GAME_WRITTEN = 0x08 | 0x80;

static int DGT_BUS_VERSION = 0x09 | 0x80;

static boolean is_busmessage(int m) {

return (m== DGT_BUS_BRD_DUMP || m == DGT_BUS_BWTIME ||

m == DGT_BUS_UPDATE || m == DGT_BUS_FROM_START || m == DGT_BUS_PING ||

m == DGT_BUS_START_GAME_WRITTEN || m == DGT_BUS_VERSION);

}

static final int EMPTY = 0x00;

static final int WPAWN = 0x01;

static final int WROOK = 0x02;

static final int WKNIGHT = 0x03;

static final int WBISHOP = 0x04;

static final int WKING = 0x05;

static final int WQUEEN = 0x06;

static final int BPAWN = 0x07;

static final int BROOK = 0x08;

static final int BKNIGHT = 0x09;

static final int BBISHOP = 0x0a;

static final int BKING = 0x0b;

static final int BQUEEN = 0x0c;

boolean is_piece(int p) {

return (p==EMPTY || p==WPAWN || p==WROOK ||

p==WKNIGHT || p==WBISHOP || p==WKING ||

p==WQUEEN || p==BPAWN || p==BROOK ||

p==BKNIGHT || p==BBISHOP || p==BKING ||

p==BQUEEN);

}

static int EE_POWERUP = 0x6a;

static int EE_EOF = 0x6b;

static int EE_FOURROWS = 0x6c;

static int EE_EMPTYBOARD = 0x6d;

static int EE_DOWNLOADED = 0x6e;

static int EE_BEGINPOS = 0x6f;

static int EE_BEGINPOS_ROT = 0x7a;

static int EE_START_TAG = 0x7b;

static int EE_WATCHDOG_ACTION = 0x7c;

static int EE_NOP = 0x7f;

}

rdgtReceiver.java

---

class rdgtReceiver extends rdgtProtocol {

int messagetype;

int messagelen_msb;

int messagelen_lsb;

int[] data;

int useddata;

int state;

rdgtInterpreter interpreter;

public rdgtReceiver(rdgtInterpreter _interpreter) {

state = 0;

interpreter = _interpreter;

}

void receive(int[] d) {

char c[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};

StringBuffer x = new StringBuffer("Received: ");

for (int i=0; i<d.length; i=i+1) {

x.append(c[d[i]>>4]).append(c[d[i] & 0x0F]).append(" ");

}

if (rdgtChess.debug) System.out.println(x.toString());

_receive (d, 0);

}

void _receive(int[] d, int start) {