?? commandstransactor.java
字號:
package control;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import DBconnectionPool.SimpleConnection;
import model.Commands;
import model.PlatformState;
/**
* receive client's commands and send response
*
* @author sheng
*
*/
public class CommandsTransactor implements Runnable {
SimpleConnection con;
DataInputStream is;
DataOutputStream os;
Socket clientsocket;
private String userAccount;
public CommandsTransactor(Socket clientsocket) {
this.clientsocket = clientsocket;
}
public void openStream(){
try {
is = new DataInputStream(clientsocket.getInputStream());
os = new DataOutputStream(clientsocket.getOutputStream());
} catch (IOException e) {
System.out.println("error happen in init CommandsTransactor");
e.printStackTrace();
}
}
public void start() {
Thread t = new Thread(this);
t.start();
}
public void run() {
openStream();
con = new SimpleConnection();
while(true) {
try {
int command = is.readInt();
switch (command) {
case Commands.SetRecord: {
setRecord();
break;
}
case Commands.GetUserInfo:
getUserInfo();
break;
case Commands.EnterPlatformAndGetGamesInfo: {
enterPlatformAndGetGamesInfo();
break;
}
case Commands.EnterGameAndGetRoomsInfo:
enterGameAndGetRoomsInfo();
break;
case Commands.EnterRoomAndGetOtheruser:
enterRoomAndGetOtheruser();
break;
case Commands.Register:
register();
break;
case Commands.GetRecord:
getRecord();
break;
case Commands.GetUserState:
getUserState();
break;
case Commands.SetReady:
setReady();
break;
case Commands.GetJarPort:
getJarPort();
break;
case Commands.RemoveJarPort:
removeJarPort();
break;
case Commands.GetRoomPlayersState:
getRoomPlayersStates();
break;
case Commands.ExitToRoomSelecting:
exitToRoomSelecting();
break;
case Commands.ExitToGameSelecting:
exitToGameSelecting();
break;
case Commands.Logout:
logout();
break;
default:
System.out.println("error command");
break;
}
} catch (Exception e) {
System.out.println("error happen in CommandsTransactor's run(): "+e);
stop();
break;
}
}
}
private void getRoomPlayersStates() throws IOException {
int gameIndex=is.readInt();
int roomIndex=is.readInt();
PlatformState ps = new PlatformState();
String []result=ps.getRoomPlayersStates(gameIndex,roomIndex);
if(result==null||result.length==0){
os.writeInt(0);
return;
}
os.writeInt(result.length);
for(int i=0;i<result.length;i++)os.writeUTF(result[i]);
}
private void setReady() throws IOException {
String userAccount1 = is.readUTF();
int isready = is.readInt();
PlatformState ps = new PlatformState();
int result = ps.setReady(userAccount1, isready);
os.writeInt(result);
}
private void getUserState() throws IOException {
String userAccount = is.readUTF();
PlatformState ps = new PlatformState();
int[] result = ps.getUserState(userAccount);
os.writeInt(result.length);
for (int i = 0; i < result.length; i++) {
os.writeInt(result[i]);
}
}
private void enterRoomAndGetOtheruser() throws IOException {
String userAccount = is.readUTF();
int gameIndex = is.readInt();
int roomIndex = is.readInt();
PlatformState ps = new PlatformState();
String[] otherUser = ps.enterRoomAndGetOtheruser(userAccount, gameIndex,
roomIndex);
int len;
if(otherUser==null)len=0;else len=otherUser.length;
os.writeInt(len);
for (int i = 0; i < len; i++)
os.writeUTF(otherUser[i]);
}
private void exitToRoomSelecting() throws IOException {
String userAccount=is.readUTF();
PlatformState ps=new PlatformState();
ps.exitToRoomSelecting(userAccount);
}
private void exitToGameSelecting() throws IOException{
String userAccount=is.readUTF();
PlatformState ps=new PlatformState();
ps.exitToGameSelecting(userAccount);
}
private void logout() throws IOException {
String userAccount=is.readUTF();
PlatformState ps=new PlatformState();
ps.logout(userAccount);
}
private void enterGameAndGetRoomsInfo() throws IOException {
String userAccount = is.readUTF();
int gameIndex = is.readInt();
PlatformState ps = new PlatformState();
int[] result = ps.enterGameAndGetRomInfo(userAccount, gameIndex);
os.writeInt(result.length);
for (int i = 0; i < result.length; i++)
os.writeInt(result[i]);
}
private void enterPlatformAndGetGamesInfo() throws IOException {
String userAccount1 = is.readUTF();
this.userAccount=userAccount1;
PlatformState ps = new PlatformState();
int[] result = ps.enterPlatformAndGetGamesInfo(userAccount1);
os.writeInt(result.length);
for (int i = 0; i < result.length; i++)
os.writeInt(result[i]);
}
/**
* register
*
* @throws IOException
*/
private void register() throws IOException {
String userAccount = is.readUTF().trim();
String passWord = is.readUTF().trim();
String nickName = is.readUTF().trim();
String eMail = is.readUTF().trim();
String qq = is.readUTF().trim();
try {
Statement sm = con.getConnection().createStatement();
String sql = "insert into mb_UserBaseInfo(vc_UserAccount, vc_PassWord,vc_NickName,vc_EMail,vc_QQ)values('"
+ userAccount
+ "','"
+ passWord
+ "','"
+ nickName
+ "','"
+ eMail + "','" + qq + "');";
System.out.println(""+sql);
int result = sm.executeUpdate(sql);
for (int i = 0; i < PlatformState.GameCount; i++) {
String sql2 = "insert into mb_GameInfo(vc_UserAccount, int_GameType, int_Grade, int_Counts, int_WinCounts) values('"
+ userAccount + "'," + i + ",10,0,0)";
sm.executeUpdate(sql2);
}
os.writeInt(result);
} catch (SQLException e) {
e.printStackTrace();
os.writeInt(-1);
} finally {
//con.release();
}
}
/**
* get user base Info
*
* @throws IOException
*/
private void getUserInfo() throws IOException {
String userAccount = is.readUTF().trim();
try {
Statement sm = con.getConnection().createStatement();
String sql = "select * from mb_UserBaseInfo where vc_UserAccount = \""
+ userAccount + "\"";
ResultSet result = sm.executeQuery(sql);
result.next();
os.writeUTF(result.getString(2));//password
os.writeUTF(result.getString(3));// NickName
os.writeUTF(result.getString(4));// EMail
os.writeUTF(result.getString(5));// QQ
} catch (SQLException e) {
os.writeUTF("");
os.writeUTF("");
os.writeUTF("");
os.writeUTF("");
} finally {
//con.release();
}
return;
}
/**
* set the record
*
* @throws IOException
*/
private void setRecord() throws IOException {
String userAccount = is.readUTF().trim();
int gameType = is.readInt();
int record = is.readInt();
int isWinner = is.readInt();// 1 is winner,0 is loser
try {
Statement sm = con.getConnection().createStatement();
String sql = "Update mb_GameInfo set int_Counts=int_Counts+1,"
+ "int_Grade=int_Grade+" + record
+ " ,int_WinCounts=int_WinCounts+" + isWinner
+ " where vc_UserAccount='" + userAccount
+ "'and int_GameType=" + gameType;
int result = sm.executeUpdate(sql);
os.writeInt(result);
} catch (SQLException e) {
e.printStackTrace();
os.writeInt(-1);
} finally {
//con.release();
}
}
/**
* get user record
*
* @throws IOException
*/
private void getRecord() throws IOException {
String userAccount = is.readUTF().trim();
int gameType = is.readInt();
try {
Statement sm = con.getConnection().createStatement();
String sql = "select * from mb_GameInfo where vc_UserAccount='"
+ userAccount + "'and " + "int_gameType=" + gameType;
ResultSet result = sm.executeQuery(sql);
int grade=-1,counts=-1,wincounts=-1;
while(result.next()){
result.getString(1);
result.getString(2);
grade=result.getInt(3);// grade
counts=result.getInt(4);// counts
wincounts=result.getInt(5);// win counts
}
os.writeInt(grade);
os.writeInt(counts);
os.writeInt(wincounts);
} catch (SQLException e) {
e.printStackTrace();
os.writeInt(-1);
os.writeInt(-1);
os.writeInt(-1);
} finally {
//con.release();
}
}
private void getJarPort() throws IOException {
int gameIndex = is.readInt();
int roomIndex=is.readInt();
PlatformState ps = new PlatformState();
int result = ps.getJarPort(gameIndex,roomIndex);
os.writeInt(result);
System.out.println("getJarport:"+result+" success");
}
private void removeJarPort() throws IOException {
int gameIndex=is.readInt();
int roomIndex=is.readInt();
PlatformState ps = new PlatformState();
int result = ps.removeJarPort(gameIndex, roomIndex);
os.writeInt(result);
System.out.println("removeJarPort success");
}
/**
* Close all open streams
*/
public void stop() {
try {
if (is != null) {
is.close();
is=null;
}
if (os != null) {
os.close();
os=null;
}
if(clientsocket!=null){
clientsocket.close();
clientsocket=null;
}
PlatformState ps=new PlatformState();
ps.logout(userAccount);
} catch (IOException ioe) {
System.out.println("CommandsTransactor stop() error!");
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -