📄 gameservice.java
字号:
package control;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.SocketConnection;
import model.Commands;
import model.INFO;
public class GameService{
SocketConnection sc=null;
DataInputStream is=null;
DataOutputStream os=null;
String IP;
int port;
public GameService(){
this.IP=INFO.PlatformIP;
this.port=INFO.PlatformPort;
openStream();
}
public synchronized int[] enterPlatformAndGetGamesInfo(String userAccount)
throws IOException {
//openStream();
os.writeInt(Commands.EnterPlatformAndGetGamesInfo);
os.writeUTF(userAccount);
int len = is.readInt();
int[] result = new int[len];
for (int i = 0; i < result.length; i++)
result[i] = is.readInt();
//System.out.println("enterPlatformAndGetGamesInfo success");
//close();
return result;
}
public synchronized int[] enterGameAndGetRoomsInfo(String userAccount, int gameIndex)
throws IOException {
//openStream();
os.writeInt(Commands.EnterGameAndGetRoomsInfo);
os.writeUTF(userAccount);
os.writeInt(gameIndex);
int len = is.readInt();
int result[] = new int[len];
for (int i = 0; i < result.length; i++)
result[i] = is.readInt();
//close();
System.out.println("enterGameAndGetroomInfo success");
return result;
}
/**
* it means can't enterRoom if return null,because the room is full;
*
* @param gameIndex
* @param RoomIndex
* @return
* @throws IOException
*/
public synchronized String[] enterRoomAndGetOtheruser(String userAccount, int gameIndex,
int roomIndex) throws IOException {
//openStream();
os.writeInt(Commands.EnterRoomAndGetOtheruser);
os.writeUTF(userAccount);
os.writeInt(gameIndex);
os.writeInt(roomIndex);
int len = is.readInt();
String[] result = new String[len];
if(len==0)result=null;
for (int i = 0; i < result.length; i++) {
result[i] = is.readUTF();
}
System.out.println("enterRoomAndGetOtheruser success");
//close();
return result;
}
public synchronized void exitToRoomSelecting(String userAccount) throws IOException {
os.writeInt(Commands.ExitToRoomSelecting);
os.writeUTF(userAccount);
System.out.println("User "+userAccount+" exit to Room-Selecting Form.");
}
public synchronized void exitToGameSelecting(String userAccount) throws IOException{
os.writeInt(Commands.ExitToGameSelecting);
os.writeUTF(userAccount);
System.out.println("User "+userAccount+" exit to Game-Selecting Form.");
}
public synchronized void logout(String userAccount) throws IOException{
os.writeInt(Commands.Logout);
os.writeUTF(userAccount);
System.out.println("User "+userAccount+" logout.");
}
/**
* if SQL error,it will return {-1,-1,-1}
*
* @param userAccount
* @param gameIndex
* @return
* @throws IOException
*/
public synchronized int[] getRecord(String userAccount, int gameIndex)
throws IOException {
//openStream();
os.writeInt(Commands.GetRecord);
os.writeUTF(userAccount);
os.writeInt(gameIndex);
int result[] = new int[3];
result[0] = is.readInt();
result[1] = is.readInt();
result[2] = is.readInt();
//close();
return result;
}
public synchronized String[][] getRoomPlayersState(String userAccount, int gameIndex, int roomIndex) throws IOException {
// if(INFO.userAccount.equals("sheng")){
// try {
// Thread.sleep(3000);
// } catch (InterruptedException ex) {
// ex.printStackTrace();
// }
// }
//openStream();
os.writeInt(Commands.GetRoomPlayersState);
os.writeInt(gameIndex);
os.writeInt(roomIndex);
int len=is.readInt();
String [][]userState=new String[2][INFO.gameMaxPlayerNum[gameIndex]];
System.out.println("getRoomPlayersState");
for(int i=0;i<len/2;++i){
userState[0][i]=is.readUTF();
System.out.println(userState[0][i]);
}
for(int i=0;i<len/2;++i){
String state=is.readUTF();
System.out.println(""+state);
if(state.equals("-1")){
userState[1][i]="鑱婂ぉ";
}else{
userState[1][i]="娓告垙";
}
}
for (int i=len/2;i<INFO.gameMaxPlayerNum[gameIndex];i++){
userState[0][i]=" ";
userState[1][i]=" ";
}
//close();
return userState;
}
public synchronized String[] getUserInfo(String userAccount) throws IOException {
//openStream();
os.writeInt(Commands.GetUserInfo);
os.writeUTF(userAccount);
String passWord=is.readUTF();
String nickName = is.readUTF();
String eMail = is.readUTF();
String qq = is.readUTF();
String[] result = { passWord,nickName, eMail, qq };
//close();
return result;
}
public synchronized int[] getUserState(String userAccount) throws IOException {
//openStream();
os.writeInt(Commands.GetUserState);
os.writeUTF(userAccount);
int len = is.readInt();
int[] result = new int[len];
for (int i = 0; i < len; i++) {
result[i] = is.readInt();
}
//close();
return result;
}
public synchronized int[] leaveRoom(String userAccount, int gameIndex, int roomIndex) throws IOException {
return this.enterGameAndGetRoomsInfo(userAccount, gameIndex);
}
/**
* if return value is 1 It means success! else error!
*
* @param userAccount
* @param passWord
* @param nickName
* @param eMail
* @param qq
* @return
* @throws IOException
*/
public synchronized int register(final String userAccount, final String passWord, final String nickName,
final String eMail, final String qq) throws IOException {
//openStream();
os.writeInt(Commands.Register);
os.writeUTF(userAccount);
os.writeUTF(passWord);
os.writeUTF(nickName);
os.writeUTF(eMail);
os.writeUTF(qq);
int result = is.readInt();
//close();
return result;
}
/**
*
* @param userAccount
* @param isReady
* Ready is 1,Not ready is -1
* @return
* @throws IOException
*/
public synchronized int setReady(String userAccount, int isReady) throws IOException {
//openStream();
os.writeInt(Commands.SetReady);
os.writeUTF(userAccount);
os.writeInt(isReady);
int result = is.readInt();
//close();
System.out.println("setReady success");
return result;
}
public synchronized int setRecord(String userAccount, int gameIndex, int record,
boolean isWinner) throws IOException {
//openStream();
os.writeInt(Commands.SetRecord);
os.writeUTF(userAccount);
os.writeInt(gameIndex);
os.writeInt(record);
if (isWinner) {
os.writeInt(1);// 1 is winner,0 is loser
} else {
os.writeInt(0);
}
int result = is.readInt();
//close();
System.out.println("setRecord success");
return result;
}
public synchronized int getJarPort(int gameIndex,int roomIndex) throws IOException {
//openStream();
os.writeInt(Commands.GetJarPort);
os.writeInt(gameIndex);
os.writeInt(roomIndex);
int result = is.readInt();
//close();
System.out.println("getJarPort:"+result+" success");
return result;
}
public synchronized int removeJarPort(int gameIndex, int roomIndex) throws IOException {
//openStream();
os.writeInt(Commands.RemoveJarPort);
os.writeInt(gameIndex);
os.writeInt(roomIndex);
int result = is.readInt();
//close();
return result;
}
private synchronized void openStream() {
if (sc== null) {
try {
sc = (SocketConnection) Connector.open("socket://" + IP + ":"
+ port);
is = new DataInputStream(sc.openInputStream());
os = new DataOutputStream(sc.openOutputStream());
} catch (IOException e) {
System.out.println("error in GameService's openStream:"+e);
}
}
}
private void close() throws IOException {
is.close();
os.close();
sc.close();
is = null;
os = null;
sc=null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -