⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 prank.java

📁 一个类似炸弹人的游戏(city hunter)
💻 JAVA
字号:

package city_hunter;
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

//=======================//
//  Program entry point  //
//=======================//
public class pRank extends Form implements CommandListener  {
    private static final String IP = "mgpp.mymobilesoft.com";
    private static final String PATH = "/mgpp/RetrieveScore";
    private static final String PATH2 = "/mgpp/UploadScore";
    private static final String PORT = "80";
    private static final String GAMEID = "040715111029946";
    private static final String TOTAL_RECORD = "10";
    private static final String PREFIX = "http://"+IP+":"+PORT+PATH;
    private static final String PREFIX2 = "http://"+IP+":"+PORT+PATH2;

     private static final Command m_pUpload = new Command("上传", Command.OK, 1);
    private static final Command m_pBack = new Command("返回", Command.EXIT, 1);
    private static final Command m_pOK = new Command("确定", Command.OK, 1);
    private static final String TEXT_UI[] = {
      "排行榜",
      "你的名字",
      "电话号码",
      "你的分数是: ",
      "上传成功",
      "上传失败\n请在上传分数或取得排名前, 确定你的JAVA设定是正确的. "+
      "你可以浏览java.mymobilesoft.com或致电摩托罗拉的客户服务热线"+
      "25063888, 查询有关资料.",
      "你的排名是: "};
    private static final String TEXT_ERROR[] = {
      "排行榜没有资料",
      "无法连线",
      "设定失败",
      "输出错误",
      "HTTP 没有回应",
      "输入错误",
      "资料长度不正确",
      "无法读取资料"};

    private static final int STAGE_UPLOAD = 0;
    private static final int STAGE_RESULT = 10;
    private static final int STAGE_RANK = 20;

    private static int m_iErrorCode;
    private static TextField m_pName;
    private static TextField m_pPhoneNo;
    private static GameLogic m_pParent;
    private static int m_iNameID;
    private static int m_iScoreID;
    private static int m_iPhoneNoID;
    private static int m_iStage;

    private static int m_iRank;
    private static int m_iScore;
    private static byte m_iLevel;
    private static String m_sName;
    private static String m_sPhoneNo;

    //===============//
    //  Create form  //
    //===============//
    pRank(GameLogic pParent)  {
      super(TEXT_UI[0]);

      m_pName = new TextField(TEXT_UI[1], "Pacess", 20, TextField.ANY);
      m_pPhoneNo = new TextField(TEXT_UI[2], "85290009000", 20, TextField.NUMERIC);

      m_iScoreID = append(TEXT_UI[3]+"0");
      m_iNameID = append(m_pName);
      m_iPhoneNoID = append(m_pPhoneNo);
      addCommand(m_pUpload);
      addCommand(m_pBack);
      setCommandListener(this);

      m_pParent = pParent;
      m_iStage = STAGE_UPLOAD;
    }

    //==========================//
    //  Command button pressed  //
    //==========================//
    public void commandAction(Command pCommand, Displayable pDisplay)  {
      if (m_iStage == STAGE_UPLOAD)  {
        if (pCommand == m_pBack)  {m_pParent.BackFromUI(false);}  else
        if (pCommand == m_pUpload)  {
          //  Update user input  //
          m_sName = m_pName.getString();
          m_sPhoneNo = m_pPhoneNo.getString();

          //  Upload score  //
          delete(m_iPhoneNoID);
          try  {
            GetScore(PREFIX2, FormatSubmitBody());
            if (GetErrorCode() != 0)  {
              throw new IOException("Error code: "+GetErrorCode());
            }
            m_iPhoneNoID = append(TEXT_UI[4]);
          }  catch (Exception e)  {
            m_iPhoneNoID = append(TEXT_UI[5]);
          }

          //  Change to RESULT screen  //
          delete(m_iNameID);
          delete(m_iScoreID);
          removeCommand(m_pBack);
          removeCommand(m_pUpload);
          addCommand(m_pOK);
          m_iStage = STAGE_RESULT;
        }
      }  else

      if (m_iStage == STAGE_RESULT)  {
        if (pCommand == m_pOK)  {
          //  Add to local ranking too  //
          m_pParent.AddToRank(m_sName, m_iLevel, m_iScore);
          m_pParent.BackFromUI(true);
        }
      }  else

      if (m_iStage == STAGE_RANK)  {
        if (pCommand == m_pOK)  {
          //  Update user input  //
          m_sName = m_pName.getString();
          m_pParent.AddToRank(m_sName, m_iLevel, m_iScore);
          m_pParent.BackFromUI(true);
        }
      }
    }

    //================================//
    //  Update UI content and button  //
    //================================//
    public void UpdateUI(String sName, String sPhoneNo, byte iLevel, int iScore, int iRank)  {
      deleteAll();
      if (m_iStage == STAGE_UPLOAD)  {
        removeCommand(m_pBack);
        removeCommand(m_pUpload);
      }  else

      if (m_iStage == STAGE_RESULT)  {
        removeCommand(m_pOK);
      }  else

      if (m_iStage == STAGE_RANK)  {
        removeCommand(m_pBack);
        removeCommand(m_pOK);
      }
      System.gc();

      //  Update latest data  //
      m_sName = sName;
      m_sPhoneNo = sPhoneNo;
      m_iLevel = iLevel;
      m_iScore = iScore;
      m_iRank = iRank;

      //  Create new screen  //
      m_pName = new TextField(TEXT_UI[1], sName, 20, TextField.ANY);
      if (m_iRank == 0)  {
        //  Create upload screen  //
        m_iStage = STAGE_UPLOAD;
        m_pPhoneNo = new TextField(TEXT_UI[2], sPhoneNo, 20, TextField.NUMERIC);
        m_iScoreID = append(TEXT_UI[3]+iScore);
        m_iNameID = append(m_pName);
        m_iPhoneNoID = append(m_pPhoneNo);
        addCommand(m_pUpload);
        addCommand(m_pBack);
      }  else  {
        //  Create rank screen  //
        m_iStage = STAGE_RANK;
        m_iScoreID = append(TEXT_UI[6]+m_iRank+"\n"+TEXT_UI[3]+m_iScore);
        m_iNameID = append(m_pName);
        addCommand(m_pOK);
      }
    }

    //========================//
    //  Generate request URL  //
    //========================//
    private static String FormatRequestBody() {
      StringBuffer pString;

      pString = new StringBuffer();
      pString.append("gameid=");
      pString.append(GAMEID);
      pString.append("&recordnum=");
      pString.append(TOTAL_RECORD);
      System.out.println(pString.toString());
      return pString.toString();
    }

    //=======================//
    //  Generate submit URL  //
    //=======================//
    private static String FormatSubmitBody() {
      StringBuffer pString;

      pString = new StringBuffer();
      pString.append("gameid=");
      pString.append(GAMEID);
      pString.append("&userid=");
      pString.append(m_sName);
      pString.append("&mobile=");
      pString.append(m_sPhoneNo);
      pString.append("&level=");
      pString.append(1);
      pString.append("&score=");
      pString.append(m_iScore);
      pString.append("&time=");
      pString.append(0);
      pString.append("&role=");
      pString.append(1);
      System.out.println(pString.toString());
      return pString.toString();
    }

    //=========================//
    //  Get score from server  //
    //=========================//
    public String GetScore(String urlString, String requestBodyString) throws IOException  {
      HttpConnection c = null;
      InputStream is = null;
      OutputStream os = null;
      String sResult;
      int rc;

      if (urlString == null)  {urlString = PREFIX;}
      if (requestBodyString == null)  {requestBodyString = FormatRequestBody();}

      sResult = null;
      try {
        // Establish HTTP connection to game portal
        m_iErrorCode = 1;
        c = (HttpConnection)Connector.open(urlString, Connector.READ_WRITE);

        // Set the request method to POST
        m_iErrorCode = 2;
        c.setRequestMethod(HttpConnection.POST);
        c.setRequestProperty("User-Agent","Profile/MIDP-1.0 Configuration/CLDC-1.0");
        c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

        // Obtain output stream for sending the request string
        m_iErrorCode = 3;
        os = c.openOutputStream();
        os.write(requestBodyString.getBytes());

        // Getting the response code will open the connection,
        // send the request, and read the HTTP response headers.
        // The headers are stored until requested.
        m_iErrorCode = 4;
        rc = c.getResponseCode();
        if (rc != HttpConnection.HTTP_OK) {
          throw new IOException("HTTP response code: " + rc);
        }

        //Obtain input stream for receiving server response
        m_iErrorCode = 5;
        is = c.openDataInputStream();

        //Retrieve the response from the server
        int ch;
        StringBuffer b = new StringBuffer();

        // Get the Content-length header
        m_iErrorCode = 6;
        String lengthStr = c.getHeaderField("Content-length");
        int lengthInt = Integer.parseInt(lengthStr);

        // Get response data
        m_iErrorCode = 7;
        for (int i=0; i<lengthInt; i++) {
          ch = is.read();
          b.append((char) ch);
        }

        sResult = b.toString();
        m_iErrorCode = 0;
        System.out.println("[GetScore] "+sResult);
      } catch (IOException e) {
        sResult = "IOException";
        System.out.println(e.toString());
      } finally {
        if (is != null)  {is.close();}
        if (os != null)  {os.close();}
        if (c != null)  {c.close();}
      }
      return sResult;
    }

    public static int GetErrorCode()  {return m_iErrorCode;}
    public static String GetError()  {return TEXT_ERROR[m_iErrorCode];}
    public static String GetUsername()  {return m_sName;}
    public static String GetPhoneNo()  {return m_sPhoneNo;}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -