Пример #1
0
faction *addfaction(const char *email, const char *password,
    const struct race * frace, const struct locale * loc)
{
    faction *f = calloc(1, sizeof(faction));
    char buf[128];

    if (!f) abort();
    if (check_email(email) == 0) {
        faction_setemail(f, email);
    } else {
        log_info("Invalid email address for faction %s: %s\n", itoa36(f->no), email?email:"");
        faction_setemail(f, NULL);
    }

    f->alliance_joindate = turn;
    f->lastorders = 0;
    f->_alive = true;
    f->password_id = 0;
    f->age = 0;
    f->race = frace;
    f->magiegebiet = 0;
    f->locale = loc;
    f->uid = 0;
    f->flags = FFL_ISNEW|FFL_PWMSG;

    if (password) {
        faction_setpassword(f, password_hash(password, PASSWORD_DEFAULT));
        ADDMSG(&f->msgs, msg_message("changepasswd", "value", password));
    }

    f->options =
        WANT_OPTION(O_REPORT) | WANT_OPTION(O_ZUGVORLAGE) |
        WANT_OPTION(O_COMPUTER) | WANT_OPTION(O_COMPRESS) |
        WANT_OPTION(O_ADRESSEN) | WANT_OPTION(O_STATISTICS);

    f->no = unused_faction_id();
    if (rule_region_owners()) {
        alliance *al = makealliance(f->no, NULL);
        setalliance(f, al);
    }
    addlist(&factions, f);
    fhash(f);

    slprintf(buf, sizeof(buf), "%s %s", LOC(loc, "factiondefault"), itoa36(f->no));
    f->name = str_strdup(buf);

    if (!f->race) {
        log_warning("creating a faction that has no race", itoa36(f->no));
    }

    return f;
}
Пример #2
0
static void test_remove_empty_factions_alliance(CuTest *tc) {
    faction *f;
    struct alliance *al;

    test_cleanup();
    f = test_create_faction(0);
    al = makealliance(0, "Hodor");
    setalliance(f, al);
    CuAssertPtrEquals(tc, f, alliance_get_leader(al));
    CuAssertIntEquals(tc, 1, ql_length(al->members));
    remove_empty_factions();
    CuAssertPtrEquals(tc, 0, al->_leader);
    CuAssertIntEquals(tc, 0, ql_length(al->members));
    test_cleanup();
}
Пример #3
0
newfaction *read_newfactions(const char *filename)
{
    newfaction *newfactions = NULL;
    FILE *F = fopen(filename, "r");
    char buf[1024];

    if (F == NULL)
        return NULL;
    for (;;) {
        faction *f;
        char race[20], email[64], lang[8], password[16];
        newfaction *nf, **nfi;
        int bonus = 0, subscription = 0;
        int alliance = 0;

        if (fgets(buf, sizeof(buf), F) == NULL)
            break;

        email[0] = '\0';
        password[0] = '\0';

        if (sscanf(buf, "%54s %20s %8s %d %d %16s %d", email, race, lang, &bonus,
            &subscription, password, &alliance) < 3)
            break;
        if (email[0] == '\0')
            break;
        if (password[0] == '\0') {
            size_t sz;
            sz = strlcpy(password, itoa36(rng_int()), sizeof(password));
            sz += strlcat(password, itoa36(rng_int()), sizeof(password));
        }
        for (f = factions; f; f = f->next) {
            if (strcmp(f->email, email) == 0 && f->subscription
                && f->age < MINAGE_MULTI)
                break;
        }
        if (f && f->units)
            continue;                 /* skip the ones we've already got */
        for (nf = newfactions; nf; nf = nf->next) {
            if (strcmp(nf->email, email) == 0)
                break;
        }
        if (nf)
            continue;
        nf = calloc(sizeof(newfaction), 1);
        if (set_email(&nf->email, email) != 0) {
            log_error("Invalid email address for subscription %s: %s\n", itoa36(subscription), email);
            continue;
        }
        nf->password = _strdup(password);
        nf->race = rc_find(race);
        nf->subscription = subscription;
        if (alliances != NULL) {
            struct alliance *al = findalliance(alliance);
            if (al == NULL) {
                char zText[64];
                sprintf(zText, "Allianz %d", alliance);
                al = makealliance(alliance, zText);
            }
            nf->allies = al;
        }
        else {
            nf->allies = NULL;
        }
        if (nf->race == NULL) {
            /* if the script didn't supply the race as a token, then it gives us a
             * race in the default locale (which means that itis a UTF8 string) */
            nf->race = findrace(race, default_locale);
            if (nf->race == NULL) {
                char buffer[32];
                size_t outbytes = sizeof(buffer) - 1;
                size_t inbytes = strlen(race);
                unicode_latin1_to_utf8(buffer, &outbytes, race, &inbytes);
                buffer[outbytes] = 0;
                nf->race = findrace(buffer, default_locale);
                if (nf->race == NULL) {
                    log_error("new faction has unknown race '%s'.\n", race);
                    free(nf);
                    continue;
                }
            }
        }
        nf->lang = get_locale(lang);
        nf->bonus = bonus;
        assert(nf->race && nf->email && nf->lang);
        nfi = &newfactions;
        while (*nfi) {
            if ((*nfi)->race == nf->race)
                break;
            nfi = &(*nfi)->next;
        }
        nf->next = *nfi;
        *nfi = nf;
    }
    fclose(F);
    return newfactions;
}