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

📄 platformstate.java

📁 手机游戏对战平台。自己做的一个东东。 支持多个手机联网打游戏;采用socket链接通信。 平台:J2ME&JAVA
💻 JAVA
字号:
package model;

import control.CardServer;
import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import control.ChatServer;
import control.FIRServer;


/**
 * all platform user's states;
 * 
 * @author sheng
 * 
 */
public class PlatformState {
    // kinds of game
    final public static int GameCount = 4;
    // max room of each game
    final public static int MaxRom = 5;
    // max players of each room
    final public static int[] MaxPlayers = {2, 3, 2, 10};
    private static CardServer[] cardserver=new CardServer[PlatformState.MaxRom];
    private static ChatServer[] chathall= new ChatServer[30];
    private static FIRServer[] firroom = new FIRServer[PlatformState.MaxRom];
    // all user's state
    public static Map<String, UserState> userStateList = null;
    // every room's JarPort
    public static int[][] jarPort = null;

    public PlatformState() {
        if (userStateList == null) {
            userStateList = new TreeMap<String, UserState>();
        }
        if (jarPort == null) {
            jarPort = new int[GameCount][100];
            for (int i = 0; i < GameCount; i++) {
                for (int j = 0; j < MaxRom; j++) {
                    jarPort[i][j] = -1;
                }
            }
        }
    }

    /**
     * 
     * @param userAccount
     * @return
     */
    public int[] enterPlatformAndGetGamesInfo(String userAccount) {
        synchronized (PlatformState.userStateList) {
            int[] result = new int[GameCount];
            for (int i = 0; i < GameCount; i++) {
                result[i] = 0;
            }
            if (!userStateList.containsKey(userAccount)) {
                userStateList.put(userAccount, new UserState());
            } else {
                userStateList.get(userAccount).gamePo = -1;
                userStateList.get(userAccount).roomPo = -1;
                userStateList.get(userAccount).isReady = -1;
            }
            for (String user : userStateList.keySet()) {
                int gamepo = userStateList.get(user).gamePo;
                if (gamepo >= 0 && gamepo < GameCount) {
                    result[gamepo]++;
                }
            }
            //System.out.println("enterPlatformAndGetGamesInfo success");
            return result;
        }
    }

    public int[] enterGameAndGetRomInfo(String userAccount, int gameIndex) {
        synchronized (PlatformState.userStateList) {
            int[] result = new int[MaxRom];
            for (int i = 0; i < MaxRom; i++) {
                result[i] = 0;
            }
            if (userStateList.containsKey(userAccount)) {
                userStateList.get(userAccount).gamePo = gameIndex;
                userStateList.get(userAccount).roomPo = -1;
                userStateList.get(userAccount).isReady = -1;
            }
            for (String user : userStateList.keySet()) {
                int gamepo = userStateList.get(user).gamePo;
                int rompo = userStateList.get(user).roomPo;
                if (gamepo == gameIndex) {
                    if (gamepo == gameIndex && rompo >= 0) {
                        result[rompo]++;
                    }
                }
            }
            return result;
        }
    }
    
    public String[] enterRoomAndGetOtheruser(String userAccount, int gameIndex,
            int roomIndex) {
        synchronized (PlatformState.userStateList) {
            //System.out.println("enterRomAndGetOtheruser" + userAccount + " " + gameIndex + " " + roomIndex);
            if (userStateList.containsKey(userAccount)) {
                userStateList.get(userAccount).gamePo = gameIndex;
                userStateList.get(userAccount).roomPo = roomIndex;
                userStateList.get(userAccount).isReady = -1;

            } else {
                userStateList.put(userAccount, new UserState());
                userStateList.get(userAccount).gamePo = gameIndex;
                userStateList.get(userAccount).roomPo = roomIndex;
                userStateList.get(userAccount).isReady = -1;
            }
            List<String> result = new ArrayList<String>();
            for (String user : userStateList.keySet()) {
                System.out.println("user:" + user);
                if (userStateList.get(user).gamePo == gameIndex && userStateList.get(user).roomPo == roomIndex) {
                    result.add(user);
                }
            }
            if (result.size() > MaxPlayers[gameIndex]) {
                return null;
            }
            String[] re = new String[result.size()];
            for (int i = 0; i < re.length; i++) {
                re[i] = result.get(i);
            }
            System.out.println("enterRomAndGetOtheruser success");
            return re;
        }
    }

    public void exitToRoomSelecting(String userAccount) {
        synchronized (PlatformState.userStateList) {
            if (userStateList.containsKey(userAccount)){
                userStateList.get(userAccount).isReady=-1;
                userStateList.get(userAccount).roomPo=-1;
            }
        }
    }

    public void exitToGameSelecting(String userAccount) {
        synchronized (PlatformState.userStateList) {
            if (userStateList.containsKey(userAccount)){
                userStateList.get(userAccount).gamePo=-1;
                userStateList.get(userAccount).isReady=-1;
                userStateList.get(userAccount).roomPo=-1;
            }             
        }
    }
    
    public void logout(String userAccount) {
        if (userStateList.containsKey(userAccount)){
            userStateList.remove(userAccount);
        }
    }
    public String[] getRoomPlayersStates(int gameIndex, int roomIndex) {
        synchronized (PlatformState.userStateList) {
            List<String> userList = new ArrayList<String>();
            List<Integer> userState = new ArrayList<Integer>();
            for (String user : userStateList.keySet()) {
                int gamepo = userStateList.get(user).gamePo;
                int rompo = userStateList.get(user).roomPo;
                if (gamepo == gameIndex && rompo == roomIndex) {
                    userList.add(user);
                    userState.add(userStateList.get(user).isReady);
                }
            }
            if (userList.size() == 0) {
                return null;
            }
            String[] result = new String[userList.size() * 2];
            int len = userList.size();
            System.out.println("getRoomPlayersStates");
            for (int i = 0; i < len; i++) {
                result[i] = userList.get(i);
                result[i + len] = String.valueOf(userState.get(i));
                System.out.println(userList.get(i) + " " + userState.get(i) + " ");
            }

            return result;
        }
    }

    public int[] getUserState(String userAccount) {
        synchronized (PlatformState.userStateList) {
            int[] state = {-1, -1, -1};
            if (userStateList.containsKey(userAccount)) {
                state[0] = userStateList.get(userAccount).gamePo;
                state[1] = userStateList.get(userAccount).roomPo;
                state[2] = userStateList.get(userAccount).isReady;
            }
            return state;
        }
    }

   



    public int setReady(String userAccount, int isready) {
        synchronized (PlatformState.userStateList) {
            int gamepo, rompo;

            System.out.println(userAccount + " begin setReady:" + isready);
            if (userStateList.containsKey(userAccount)) {
                gamepo = userStateList.get(userAccount).gamePo;
                rompo = userStateList.get(userAccount).roomPo;
                System.out.println("gamepo:" + gamepo + " romPo:" + rompo);
                if (gamepo >= 0 && rompo >= 0) {
                    userStateList.get(userAccount).isReady = isready;
                    System.out.println("setReady " + userStateList.get(userAccount).isReady + " success");
                    return isready;
                } else {
                    return -1;
                }

            } else {
                return -1;
            }
        }

    }

    public int removeJarPort(int gameIndex, int roomIndex) {
        synchronized (PlatformState.jarPort) {
            jarPort[gameIndex][roomIndex] = -1;
            return jarPort[gameIndex][roomIndex];
        }
    }

    public int getJarPort(int gameIndex, int roomIndex) {
        
            if (gameIndex < 0 || roomIndex < 0) {
                return -1;
            }
           synchronized (PlatformState.jarPort) {
            int port = jarPort[gameIndex][roomIndex];

            if (port != 0 && port != -1) {
                return port;
            }
        }
            List<String> userList = new ArrayList<String>();
            synchronized (PlatformState.userStateList) {
            for (String user : userStateList.keySet()) {
                if (userStateList.get(user).gamePo == gameIndex && userStateList.get(user).roomPo == roomIndex) {
                    userList.add(user);
                }
            }
            System.out.println("openjarport");
            int result = openJar(userList, gameIndex, roomIndex);
            return result;
        }
    }

    /**
     * 
     * @param userList
     * @param gameIndex
     * @return
     */
    private int openJar(List<String> userList, int gameIndex, int roomIndex) {
        synchronized (PlatformState.jarPort) {
            int port = jarPort[gameIndex][roomIndex];
            if (port != 0 && port != -1) {
                return port;
            }
            switch (gameIndex) {
                /*
                 * FIR game Jar
                 */
                case 0: {
                    while (true) {
                        try {
                            port = (int) (Math.random() * 50000 + 1024);
                            //The purpose is to find a port which is not used, instead of creating a ServerSocket.So it close at once.
                            ServerSocket serversocket = new ServerSocket(port);
                            serversocket.close();
                            break;
                        } catch (Exception e) {
                            System.out.println(e + "serversocket");
                        }
                    }
                    String[] re = new String[userList.size()];
                    for (int i = 0; i < re.length; i++) {
                        re[i] = userList.get(i);
                    }
                    firroom[roomIndex] = new FIRServer(port, re);
                    firroom[roomIndex].start();
                    break;
                }
                /*
                 * card Jar
                 */
                case 1:
                {
                    while (true) {
                        try {
                            port = (int) (Math.random() * 50000 + 1024);
                            ServerSocket serversocket = new ServerSocket(port);
                            serversocket.close();
                            break;
                        } catch (Exception e) {
                            System.out.println(e + "serversocket");
                        }
                    }
                    String[] re = new String[userList.size()];
                    for (int i = 0; i < re.length; i++) {
                        re[i] = userList.get(i);
                    }
                    cardserver[roomIndex] = new CardServer(port, re);
                    cardserver[roomIndex].start();
                    break;
                }
                /*
                 * game 3 Jar
                 */
                case 2:
                    break;
                /*
                 * chat Jar
                 */
                case 3: {
                    while (true) {
                        try {
                            port = (int) (Math.random() * 50000 + 1024);
                            ServerSocket serversocket = new ServerSocket(port);
                            serversocket.close();
                            break;
                        } catch (Exception e) {
                            System.out.println(e + "serversocket");
                        }
                    }
                    String[] re = new String[userList.size()];
                    for (int i = 0; i < re.length; i++) {
                        re[i] = userList.get(i);
                    }
                    chathall[roomIndex] = new ChatServer(port, re);
                    chathall[roomIndex].start();
                    break;
                }
            }
            jarPort[gameIndex][roomIndex] = port;
            return port;
        }
    }
}
/**
 * user state in the platform
 * 
 * @author sheng
 * 
 */
class UserState {

    public int gamePo;// in which game
    public int roomPo;// in which room
    public int isReady;// if is ready when in the room

    public UserState() {
        gamePo = -1;
        roomPo = -1;
        isReady = -1;
    }
}

⌨️ 快捷键说明

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