ServersList.java
上传用户:haojie1228
上传日期:2022-08-08
资源大小:347k
文件大小:7k
- package poker;
- import java.io.*;
- import javax.microedition.lcdui.*;
- import javax.microedition.rms.*;
- import javax.microedition.lcdui.Command;
- /**
- * <p>Title: </p>
- * <p>Description: </p>
- * <p>Copyright: Copyright (c) 2004</p>
- * <p>Company: </p>
- * @author not attributable
- * @version 1.0
- */
- public class ServersList implements CommandListener{
- List imList;
- String[] imValue=new String[20];
- String[] imID=new String[20];
- RecordStore rs=null;
- Image iamge;
- InputStream is=null;
- String url;
- Command exitCmd,okCmd,downCmd,cancelCmd,helpCmd;
- Alert MsgBox=new Alert("ERROR");
- Form waitForm=new Form(" downloading...");
- RoomList room;
- static ServersList _instance;
- static synchronized public ServersList getInstance() {
- if (_instance == null) {
- _instance = new ServersList();
- }
- return _instance;
- }
- public ServersList() {
- //super("ServerList");
- try {
- jbInit();
- }
- catch(Exception e) {
- e.printStackTrace();
- }
- }
- public void jbInit(){
- imList=new List("server list:",List.IMPLICIT);
- exitCmd=new Command("exit",Command.EXIT,1);
- okCmd=new Command("go on",Command.OK,1);
- cancelCmd=new Command("Cancel me",Command.CANCEL,1);
- downCmd=new Command("download new servers list",Command.SCREEN,2);
- helpCmd=new Command("Help me",Command.HELP,1);
- imList.addCommand(okCmd);
- imList.addCommand(downCmd);
- imList.addCommand(cancelCmd);
- imList.addCommand(exitCmd);
- imList.addCommand(helpCmd);
- imList.setCommandListener(this);
- //read from rms ,and put it into imList;
- try{
- rs.deleteRecordStore("db_serverList");
- rs=RecordStore.openRecordStore("db_serverList",true);
- int len=rs.getNumRecords();
- //System.out.println("record len "+len);
- if (len==0){
- //if rms has not data,then put default data;
- //create a new rms : db_serverList
- DbRecord.set("Liu Yan","192.168.1.19","1");
- rs.addRecord(DbRecord.recordByte,0,DbRecord.recordByte.length);
- DbRecord.set("ZhangYin","192.168.1.19","2");
- rs.addRecord(DbRecord.recordByte,0,DbRecord.recordByte.length);
- rs.closeRecordStore();
- }
- }catch(Exception e){
- Error.errCode=1;
- Error.errStr="open RMS error";
- return;
- }
- readFromRMS();
- }
- public void readFromRMS(){
- try{
- rs=RecordStore.openRecordStore("db_serverList",true);
- int len=rs.getNumRecords();
- for(int i=1;i<=len;i++){
- DbRecord.get(rs.getRecord(i));
- //after do this,the class DbRecord's servername and address can have a value;
- String s1=DbRecord.serverName.toString();
- String s2=DbRecord.address.toString();
- String s3=DbRecord.ServerID.toString();
- imList.append(s1,GetServicData.ServerImage);
- imValue[i]=s2;
- imID[i]=s3;
- //System.out.println(imList.getString(i-1)+imValue[i]+imID[i]);
- }
- rs.closeRecordStore();
- }catch(RecordStoreException e){
- Error.errCode=1;
- Error.errStr="Error when read RMS.";
- return;
- }
- }
- private void writeRMSfromList(){
- int len=imList.size();
- try{
- try{rs.closeRecordStore();}catch(Exception e1){};
- RecordStore.deleteRecordStore("db_serverList");
- }catch (Exception e){
- Error.errCode=1;
- Error.errStr="Error when delete RMS.";
- return;
- }
- try{
- rs = RecordStore.openRecordStore("db_serverList", true);
- for (int i = 0; i < len; i++) {
- //System.out.println(imList.getString(i)+imValue[i+1]+imID[i+1]);
- DbRecord.set(imList.getString(i), imValue[i+1],imID[i+1]);
- rs.addRecord(DbRecord.recordByte, 0, DbRecord.recordByte.length);
- }
- rs.closeRecordStore();
- }catch (Exception e){
- Error.errCode=1;
- Error.errStr="Error when read RMS after delete";
- return;
- }
- }
- private void connect() {
- GetServicData.kind=1;
- GetServicData.url=url;
- GetServicData.imList=imList;
- GetServicData.imID=imID;
- GetServicData.imValue=imValue;
- GetServicData.connect();
- imList=GetServicData.imList;
- imID=GetServicData.imID;
- imValue=GetServicData.imValue;
- writeRMSfromList();
- }
- public void downLoadServerList(){
- int ss=0;
- ss=imList.getSelectedIndex();
- //System.out.println("you select:"+ss);
- url="http://"+imValue[ss+1]+"/gameservice/infomationservice.asmx/GetServerList";
- //System.out.println(url);
- connect();
- return;
- }
- public String loginServer(){
- //0 : login ok
- //1 : password or accountName wrong
- //2 : other error
- try{
- rs=RecordStore.openRecordStore("db_AccountInfo",true);
- int len=rs.getNumRecords();
- DbAccount.get(rs.getRecord(1));
- String account=DbAccount.account;
- String password=DbAccount.password;
- System.out.println(account+password);
- rs.closeRecordStore();
- Login ll=Login.getInstance();
- String rr=ll.login(account,password).trim();
- System.out.println("login result:"+rr);
- if (rr=="0"){
- GameInfo.Account=account;
- GameInfo.password=password;
- }
- System.out.println("after checking login ,rr="+rr);
- return rr;
- }catch(RecordStoreException e){
- Error.errCode=3;
- Error.errStr="Login Error.";
- return "Wrong";
- }
- }
- public void commandAction(Command c, Displayable d) {
- if (c==okCmd){
- // go to room list
- int k = imList.getSelectedIndex();
- GameInfo.ServerAddress = imValue[k + 1].toString();
- GameInfo.ServerName = imList.getString(k);
- GameInfo.display.setCurrent(Func.waitForm);
- String ss=loginServer().trim();
- System.out.println("ss="+ss);
- if (ss.equals("0")){
- //login ok
- room=RoomList.getInstance();
- room.downLoad();
- _instance=null;
- }else{
- MsgBox.setType(AlertType.ERROR);
- MsgBox.setTimeout(5000);
- MsgBox.setString("login server failed.please check you account and password. In some case,please try to change the server to login again!");
- GameInfo.display.setCurrent(MsgBox,imList);
- Error.errCode=0;
- }
- }
- if (c==downCmd){
- Error.errCode=0;
- GameInfo.display.setCurrent(waitForm);
- downLoadServerList();
- if (Error.errCode>0)
- {
- MsgBox.setType(AlertType.ERROR);
- MsgBox.setTimeout(5000);
- MsgBox.setString(Error.errStr);
- GameInfo.display.setCurrent(MsgBox,imList);
- Error.errCode=0;
- }
- else
- {
- MsgBox.setTitle("DONE!");
- MsgBox.setString("ok");
- MsgBox.setTimeout(1000);
- MsgBox.setType(AlertType.INFO);
- GameInfo.display.setCurrent(MsgBox,imList);
- }
- return;
- }
- if (c==cancelCmd){
- Login login=Login.getInstance();
- GameInfo.display.setCurrent(login);
- login.drawFace();
- _instance=null;
- }
- }
- }