Пример #1
0
  private String evaluate(final String functionName, final String[] params, final Client client) {
    System.out.println("Params: " + params.length);

    if (params.length > 0) {
      if (params.length == 1) {
        System.out.println("Parameter (1): " + params[0]);

        if (functionName.equals("create_item")) {
          final Item item = parent.createItem(params[0]);

          if (item != null) {
            return "" + item.getDBRef();
          } else {
            return "-1";
          }
        } else if (functionName.equals("dbref")) {
          return "" + parent.getObject(params[0]).getDBRef();
        } else if (functionName.equals("rainbow")) {
          System.out.println(params[0]);
          return parent.rainbow(params[0]) + parent.colorCode("white");
        } else {
          return "PGM: No such function!";
        }
        // else { return "Incomplete function statement, no parameters!"; }
        // else { return "PGM: Error!"; }
      } else if (params.length == 2) {
        System.out.println("Parameter (1): " + params[0]);
        System.out.println("Parameter (2): " + params[1]);

        if (functionName.equals("colors")) {
          if (params.length >= 2) {
            parent.debug("Color: " + params[0]);
            parent.debug("Text: " + params[1]);

            return parent.colorCode(params[0]) + params[1] + parent.colorCode("white");
          } else {
            return "PGM: Error!";
          }
        } else if (functionName.equals("add")) { // {add:5,5} -> 10
          if (params[0].startsWith("{") && params[0].endsWith(")")) {
            params[0] = interpret(params[0], client);
          }

          if (params[1].startsWith("{") && params[1].endsWith(")")) {
            params[1] = interpret(params[1], client);
          }

          Integer first = null;
          Integer second = null;

          try {
            first = Integer.parseInt(params[0]);
            second = Integer.parseInt(params[1]);
          } catch (NumberFormatException nfe) {
            System.out.println("-- Stack Trace --");
            nfe.printStackTrace();

            return "";
          }

          return "" + (first + second);
        } else if (functionName.equals("sub")) { // {sub:5,3} -> 2
          if (params[0].startsWith("{") && params[0].endsWith(")")) {
            params[0] = interpret(params[0], client);
          }

          if (params[1].startsWith("{") && params[1].endsWith(")")) {
            params[1] = interpret(params[1], client);
          }

          Integer first = null;
          Integer second = null;

          try {
            first = Integer.parseInt(params[0]);
            second = Integer.parseInt(params[1]);
          } catch (NumberFormatException nfe) {
            System.out.println("-- Stack Trace --");
            nfe.printStackTrace();

            return "";
          }

          return "" + (first - second);
        } else if (functionName.equals(
            "eq")) { // {eq:<string 1>,<string 2>} -> {eq:test,test} -> :true
          if (params[0].startsWith("{") && params[0].endsWith(")")) {
            params[0] = interpret(params[0], client);
          }

          if (params[1].startsWith("{") && params[1].endsWith(")")) {
            params[1] = interpret(params[1], client);
          }

          if (params[0].equals(params[1])) {
            return ":true";
          } else {
            Integer first = null;
            Integer second = null;

            try {
              first = Integer.parseInt(params[0]);
              second = Integer.parseInt(params[1]);
            } catch (NumberFormatException nfe) {
              System.out.println("-- Stack Trace --");
              nfe.printStackTrace();

              return ":false";
            }

            // System.out.println("first: " + first);
            // System.out.println("Second: " + second);

            if (first == second) {
              return ":true";
            } else {
              return ":false";
            }
          }
        }
        // else { return "Incomplete function statement, no parameters!"; }
        else if (functionName.equals("give")) {
          final Player p = parent.getPlayer(Utils.toInt(params[0], -1));
          final Item i = parent.getItem(Utils.toInt(params[1], -1));

          if (p != null && i != null) {
            i.setLocation(p.getDBRef());
            p.getInventory().add(i);
          }

          return "";
        } else if (functionName.equals(
            "prop")) { // {prop:<property name>,<dbref of object to get property from>}
          final String property = params[0];
          final int dbref = Utils.toInt(params[1], -1);

          final MUDObject object = parent.getObject(dbref);

          if (object != null) {
            System.out.println("Object: " + object.getName());
            return "" + object.getProperty(property);
          } else return "";
          // else { return "Incomplete function statement, no parameters!"; }
        } else if (functionName.equals("tell")) {
          final String message = params[0];
          final Player p = parent.getPlayer(Utils.toInt(params[1], -1));

          if (message != null && p != null) {
            // parent.addMessage( new Message(message, p) );
            return message;
          }

          return "";
        } else {
          return "PGM: No such function!";
        }
      } else if (params.length == 3) {
        if (functionName.equals("if")) {
          final String result = interpret(params[0], client);

          System.out.println("result: " + result);

          if (result.equals(":true")) {
            return interpret(params[1], client);

          } else if (result.equals(":false")) {
            return interpret(params[2], client);
          }
        }

        return "";
      } else {
        if (functionName.equals("distance")) {

          /*
           * parameters:
           * 	2d/3d
           *  one or two points
           */

          System.out.println("PGM -distance-");

          List<Point> ptList = Utils.toPoints(Utils.join(params, ","));

          if (ptList != null) {
            return String.format("%.1f", Utils.distance(ptList.get(0), ptList.get(1)));
            // return ptList + "; distance is " + String.format("%.1f",
            // Utils.distance(ptList.get(0), ptList.get(1)));
          } else {
            return "PGM: Error!";
          }
          // else { return "Incomplete function statement, no parameters!"; }
        } else if (functionName.equals("exec")) {
          if (params != null) {
            if (params.length == 1) {

            } else if (params.length == 2) {

            } else {

            }
          } else {
          }
          ;

          return "";
        } else if (functionName.equals("with")) {
          List<String> params1 = new ArrayList<String>(params.length);
          boolean function_found = false;
          boolean var_defined = false;

          String last_var = "";

          int index = 0;

          // resolve all sub functions
          for (String param : params) {
            if (!isFunction(param) && !function_found) {
              function_found = true;

              if (!use_vars) {
                use_vars = true;
                vars = new Hashtable<String, String>();

                vars.put(param, "");

                last_var = param;
              }
            } else {
              if (!var_defined) vars.put(last_var, interpret(param, client));
              else params1.set(index, interpret(param, client));
            }

            index++;
          }

          // do things

          // vars to null
          vars = null;

          // use_vars to false
          use_vars = false;

          return Utils.join(params1, " ");
        } else if (functionName.equals("do")) {
          for (String param : params) {
            interpret(param, client);
          }

          return "";
        } else {
          return "PGM: No such function!";
        }
        // else { return "PGM: Error!"; }
      }
    } else {
      switch (functionName) {
        case "{&arg}":
          return "";
        case "{&cmd}":
          return "";
        case "{&how}":
          return "";
        case "{&player}":
          return "" + parent.getPlayer(client).getDBRef();
        case "{name}":
          return parent.getServerName();
        case "{version}":
          // return "-Result: " + MUDServer.getName() + " " + MUDServer.getVersion();
          return MUDServer.getVersion();
        case "{colors}":
          return "Incomplete function statement, no inputs!";
        default:
          if (use_vars) {
            String temp = functionName.replace("{", "").replace("}", "");
            String temp1 = vars.get(temp);

            if (temp1 != null) return temp1;
            else return "";
          } else return functionName;
      }

      /*if( !script.contains("{") && !script.contains("}") ) { // not a script...
      	return script;
      }
      else { // malformed script?
      	return "PGM: No such function! (1)";
      }*/
    }
  }