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

📄 crystalcontroller.java

📁 一套MMORPG手机网络游戏的服务端
💻 JAVA
字号:
/*
 * Created on 2005-6-30
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.swing.game.crystal;

import java.io.File;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.swing.game.crystal.utils.Daemon;
import com.swing.game.crystal.utils.EquipUtils;
import com.swing.game.crystal.utils.ImageUtils;
import com.swing.game.crystal.utils.MapUtils;
import com.swing.game.crystal.utils.MazeUtils;
import com.swing.game.crystal.utils.PersonUtils;
import com.swing.game.crystal.utils.PlotUtils;
import com.swing.game.crystal.utils.T2OList;
import com.swing.game.crystal.utils.TradeCenter;
import com.swing.game.crystal.utils.TransportUtils;
import com.swing.server.common.Attachment;
import com.swing.server.common.GameEvent;
import com.swing.server.common.GameFunction;
import com.swing.server.common.Globals;
import com.swing.server.common.Player;
import com.swing.server.server.controller.GameController;

/**
 * @author vampire_a
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class CrystalController extends GameController {

    private Logger log = Logger.getLogger("CrystalController");

    public static int VERSION;

    public static String BASIC_URL;

    public static final List SPECIAL = new ArrayList();

    public static final Map MapDatas = MapUtils.initMap();

    public static final Map TransportMapping = TransportUtils
            .initTransportMap();

    public static final Map WeaponMapping = EquipUtils.initWeaponMap();

    public static final Map PlotMapping = PlotUtils.initPlotMap();

    public static final Map PersonMapping = PersonUtils.initPerson();
    
    public static final Map ImageMapping = ImageUtils.initImageMap();
    
    public static final MazeUtils Mazes = new MazeUtils();

    public static final T2OList PKList = new T2OList();

    public static final TradeCenter Trades = new TradeCenter();

    private final Daemon daemon = new Daemon();

    /*
     * (non-Javadoc)
     * 
     * @see com.swing.server.server.controller.GameController#initController()
     */
    protected void initController() {
        // TODO Auto-generated method stub
        PropertyConfigurator.configure(Globals.LOG4J_FILE);
        String id = null;
        String value = null;
        String name = this.getClass().getName();
        String shortName = name.substring(name.lastIndexOf(".") + 1);

        log.info("assembling module : " + shortName);

        File f = new File(Globals.CONFIG_PATH + shortName + ".xml");
        if (f == null)
            log.error("Can't find ChatController configure file!");
        else {
            try {
                SAXReader reader = new SAXReader();
                Document document = reader.read(f);
                Element root = document.getRootElement();
                List l = root.selectNodes("./Element");

                for (Iterator iter = l.iterator(); iter.hasNext();) {
                    Element e = (Element) iter.next();
                    id = e.attributeValue("id");
                    value = e.attributeValue("value");
                    log.debug(shortName + "attribute : id->" + id + " value->"
                            + value);
                    // 取得游戏代号
                    if (id.equals("GameCode"))
                        gameCode = value;
                    else if (id.equals("VERSION"))
                        VERSION = Integer.parseInt(value);
                    else if (id.equals("BASIC_URL"))
                        BASIC_URL = value;
                    else if (id.equals("SPLITTER_LEVEL_1"))
                        SPLITTER_1 = value;
                    else if (id.equals("SPLITTER_LEVEL_2"))
                        SPLITTER_2 = value;
                    // 取得游戏处理函数
                    else if (id.startsWith("FUNCTION")) {
                        Class cl = Class.forName(value);
                        if (!GameFunction.class.isAssignableFrom(cl)) {
                            log
                                    .warn("class file does not extend GameFunction: "
                                            + value.substring(value
                                                    .lastIndexOf(".")));
                            continue;
                        }
                        GameFunction gf = (GameFunction) cl.newInstance();
                        gf.setGameController(this);
                        this.function
                                .put(id.substring(id.indexOf("_") + 1), gf);
                    }
                }
            } catch (DocumentException de) {
                log.error("Error loading configure from file: " + f.getName(),
                        de);
            } catch (MalformedURLException mue) {
                log.error("Error loading configure from file: " + f.getName(),
                        mue);
            } catch (Exception e) {
                log.error("Error creating class from file: " + value, e);
            }
        }
        instance = this;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.swing.server.server.controller.GameController#createPlayer(int)
     */
    public Player createPlayer(int kind) {
        // TODO Auto-generated method stub
        return new CrystalPlayer(kind);
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.swing.server.server.controller.GameController#createGameEvent(java.lang.String)
     */
    public GameEvent createGameEvent(String gameCode) {
        CrystalEvent ce = new CrystalEvent();
        ce.setGameCode(gameCode);
        return ce;
    }

    /*
     * 在这里根据用户的上行进行处理数据,将结果串接在一起,发回给客户
     */
    protected void processEvent(GameEvent e) {

        StringBuffer sb = new StringBuffer();
        // 除SOCKET类型用户,都需要加上“HTTP 200 ”作为头
        Player p = (Player) this.playersByPlayerId.get(e.getPlayerId());
        if (p.getConnKind() > Attachment.SOCKET) {
            sb.append("HTTP 200 ");
        }
        if (p.getConnKind() > Attachment.POST)
            sb.append("The Website you visited is being built!");
        else {

            // 这里负责调用对应的处理函数处理用户的请求
            String command = null;
            String[] requests = e.getRequest().split(SPLITTER_1);
            GameFunction gf = null;
            for (int i = 1; i < requests.length; i++) {
                command = requests[i].substring(0, (requests[i]
                        .indexOf(SPLITTER_2) == -1 ? requests[i].length()
                        : requests[i].indexOf(SPLITTER_2)));
                gf = (GameFunction) this.function.get(command);
                log.debug("Handle Event -- " + p.getPlayerId() + " : "
                        + e.getRequest());

                List l = gf.handleRequest(p.getPlayerId(), requests[i]);
                if (l.size() != 0) {
                    log.debug("Result -- " + p.getPlayerId() + " : number = " + l.size() + " last = " + l.get(l.size() - 1));
                    for (Iterator iter = l.iterator(); iter.hasNext();) {
                        Object data = iter.next();
                        e.addResponse(data);
                    }
                }
            }
           
            sb.append(p.getAllData());
        }
        if (!sb.toString().equals("")) {
            e.addResponse(sb.toString());
        }
        if (e.getResponse().size() != 0)
            sendEvent(e, p);
    }

    public void stop() {
        daemon.stop();
        super.stop();
    }
}

⌨️ 快捷键说明

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