Esempio n. 1
0
nh_bool
nhnet_set_option(const char *name, union nh_optvalue value, nh_bool isstr)
{
    int ret, i;
    json_t *jmsg, *joval, *jobj;
    struct nh_option_desc *gameopts, *birthopts, *opt;
    struct nh_autopickup_rule *r;

    ret = nh_set_option(name, value, isstr);
    if (!nhnet_active())
        return ret;

    if (!api_entry())
        return FALSE;

    gameopts = nhnet_get_options(GAME_OPTIONS);
    birthopts = nhnet_get_options(CURRENT_BIRTH_OPTIONS);
    opt = NULL;
    for (i = 0; gameopts[i].name && !opt; i++)
        if (!strcmp(name, gameopts[i].name))
            opt = &gameopts[i];
    for (i = 0; birthopts[i].name && !opt; i++)
        if (!strcmp(name, birthopts[i].name))
            opt = &birthopts[i];

    if (opt) {
        if (isstr || opt->type == OPTTYPE_STRING)
            joval = json_string(value.s);
        else if (opt->type == OPTTYPE_INT || opt->type == OPTTYPE_ENUM ||
                 opt->type == OPTTYPE_BOOL) {
            joval = json_integer(value.i);
        } else if (opt->type == OPTTYPE_AUTOPICKUP_RULES) {
            joval = json_array();
            for (i = 0; value.ar && i < value.ar->num_rules; i++) {
                r = &value.ar->rules[i];
                jobj =
                    json_pack("{ss,si,si,si}", "pattern", r->pattern, "oclass",
                              r->oclass, "buc", r->buc, "action", r->action);
                json_array_append_new(joval, jobj);
            }
        }

        jmsg =
            json_pack("{ss,so,si}", "name", name, "value", joval, "isstr",
                      isstr);
        jmsg = send_receive_msg("set_option", jmsg);
        if (json_unpack(jmsg, "{si,so!}", "return", &ret, "option", &jobj) ==
            -1) {
            print_error("Bad response in nhnet_set_option");
        } else {
            free_option_data(opt);
            read_json_option(jobj, opt);
        }
        json_decref(jmsg);
    }
    api_exit();
    return ret;
}
Esempio n. 2
0
void free_option_lists(void)
{
    int i, j;

    for (i = 0; i < OPTION_LIST_COUNT; i++)
        if (option_lists[i]) {
            for (j = 0; option_lists[i][j].name; j++)
                free_option_data(&option_lists[i][j]);
            free(option_lists[i]);
            option_lists[i] = NULL;
        }
}