Ejemplo n.º 1
0
static void write_config_options(FILE *fp, struct nh_option_desc *options)
{
    int i;
    const char *optval;
    
    for (i = 0; options[i].name; i++) {
	optval = nh_get_option_string(&options[i]);
	if (options[i].type == OPTTYPE_STRING ||
	    options[i].type == OPTTYPE_ENUM)
	    fprintf(fp, "%s=\"%s\"\n", options[i].name, optval);
	else
	    fprintf(fp, "%s=%s\n", options[i].name, optval);
    }
}
Ejemplo n.º 2
0
static const char* get_display_string(struct nh_option_desc *option)
{
    switch ((int)option->type) {
	default:
	case OPTTYPE_BOOL:
	case OPTTYPE_ENUM:
	case OPTTYPE_INT:
	case OPTTYPE_STRING:
	    return nh_get_option_string(option);
	    
	case OPTTYPE_AUTOPICKUP_RULES:
	case OPTTYPE_MSGTYPE:
	case OPTTYPE_KEYMAP:
	    return "submenu";
    }
}
Ejemplo n.º 3
0
static void
ccmd_set_option(json_t * params)
{
    const char *optname, *optstr, *pattern;
    json_t *jmsg, *joval, *jopt;
    int isstr, i, ret;
    const struct nh_option_desc *gameopt, *birthopt, *option;
    union nh_optvalue value;
    struct nh_autopickup_rules ar = { NULL, 0 };
    struct nh_autopickup_rule *r;

    if (json_unpack
        (params, "{ss,so,si*}", "name", &optname, "value", &joval, "isstr",
         &isstr) == -1)
        exit_client("Bad parameters for set_option");

    /* find the option_desc for the options that should be set; the option type
       is required in order to decode the option value. */
    gameopt = nh_get_options(GAME_OPTIONS);
    birthopt =
        nh_get_options(gameid ? ACTIVE_BIRTH_OPTIONS : CURRENT_BIRTH_OPTIONS);
    option = find_option(optname, gameopt, birthopt);
    if (!option) {
        jmsg = json_pack("{si,so}", "return", FALSE, "option", json_object());
        client_msg("set_option", jmsg);
        return;
    }

    /* decode the option value depending on the option type */
    if (isstr || option->type == OPTTYPE_STRING) {
        if (!json_is_string(joval))
            exit_client("could not decode option string");
        value.s = (char *)json_string_value(joval);

    } else if (option->type == OPTTYPE_INT || option->type == OPTTYPE_ENUM ||
               option->type == OPTTYPE_BOOL) {
        if (!json_is_integer(joval))
            exit_client("could not decode option value");
        value.i = json_integer_value(joval);

    } else if (option->type == OPTTYPE_AUTOPICKUP_RULES) {
        if (!json_is_array(joval))
            exit_client("could not decode option");

        ar.num_rules = json_array_size(joval);
        ar.rules = malloc(sizeof (struct nh_autopickup_rule) * ar.num_rules);
        if (ar.num_rules) {
            value.ar = &ar;
            for (i = 0; i < ar.num_rules; i++) {
                r = &ar.rules[i];
                if (json_unpack
                    (json_array_get(joval, i), "{ss,si,si,si}", "pattern",
                     &pattern, "oclass", &r->oclass, "buc", &r->buc, "action",
                     &r->action) == -1)
                    exit_client("Error unpacking autopickup rule");
                strncpy(r->pattern, pattern, sizeof (r->pattern) - 1);
            }
        } else
            value.ar = NULL;
    }

    ret = nh_set_option(optname, value, isstr);

    if (option->type == OPTTYPE_AUTOPICKUP_RULES)
        free(ar.rules);

    gameopt = nh_get_options(GAME_OPTIONS);
    birthopt =
        nh_get_options(gameid ? ACTIVE_BIRTH_OPTIONS : CURRENT_BIRTH_OPTIONS);
    option = find_option(optname, gameopt, birthopt);

    jopt = json_option(option);
    optstr = nh_get_option_string(option);

    if (ret == TRUE)
        db_set_option(user_info.uid, optname, option->type, optstr);
    /* return the altered option struct and the string representation to the
       client. The intent is to save some network round trips and make a
       separate get_option_string message unneccessary */
    jmsg = json_pack("{si,so}", "return", ret, "option", jopt);
    client_msg("set_option", jmsg);
}