Ejemplo n.º 1
0
static void
ccmd_get_topten(json_t * params)
{
    struct nh_topten_entry *scores;
    char buf[BUFSZ];
    const char *player;
    int listlen, top, around, own, i;
    json_t *jmsg, *jarr, *jobj;

    if (json_unpack
        (params, "{ss,si,si,si*}", "player", &player, "top", &top, "around",
         &around, "own", &own) == -1)
        exit_client("Bad parameters for get_topten");

    if (player && !player[0])
        player = NULL;

    scores = nh_get_topten(&listlen, buf, player, top, around, own);

    jarr = json_array();
    for (i = 0; i < listlen; i++) {
        jobj =
            json_pack
            ("{si,si,si,si,si,si,si,si,si,si,si,si,si,ss,ss,ss,ss,ss,ss,ss,si}",
             "rank", scores[i].rank, "points", scores[i].points, "maxlvl",
             scores[i].maxlvl, "hp", scores[i].hp, "maxhp", scores[i].maxhp,
             "deaths", scores[i].deaths, "ver_major", scores[i].ver_major,
             "ver_minor", scores[i].ver_minor, "patchlevel",
             scores[i].patchlevel, "deathdate", scores[i].deathdate,
             "birthdate", scores[i].birthdate, "moves", scores[i].moves,
             "end_how", scores[i].end_how, "plrole", scores[i].plrole, "plrace",
             scores[i].plrace, "plgend", scores[i].plgend, "plalign",
             scores[i].plalign, "name", scores[i].name, "death",
             scores[i].death, "entrytxt", scores[i].entrytxt, "highlight",
             scores[i].highlight);
        json_array_append_new(jarr, jobj);
    }
    jmsg = json_pack("{so,ss}", "toplist", jarr, "msg", buf);
    client_msg("get_topten", jmsg);
}
Ejemplo n.º 2
0
static void
ccmd_game_command(json_t * params)
{
    json_t *jarg;
    int count, result, gid;
    const char *cmd;
    struct nh_cmd_arg arg;

    if (json_unpack
        (params, "{ss,so,si*}", "command", &cmd, "arg", &jarg, "count",
         &count) == -1)
        exit_client("Bad set of parameters for game_command");

    if (json_unpack(jarg, "{si*}", "argtype", &arg.argtype) == -1)
        exit_client("Bad parameter arg in game_command");

    switch (arg.argtype) {
    case CMD_ARG_DIR:
        if (json_unpack(jarg, "{si*}", "d", &arg.d) == -1)
            exit_client("Bad direction arg in game_command");
        break;

    case CMD_ARG_POS:
        if (json_unpack(jarg, "{si,si*}", "x", &arg.pos.x, "y", &arg.pos.y) ==
            -1)
            exit_client("Bad position arg in game_command");
        break;

    case CMD_ARG_OBJ:
        if (json_unpack(jarg, "{si*}", "invlet", &arg.invlet) == -1)
            exit_client("Bad invlet arg in game_command");
        break;

    case CMD_ARG_NONE:
    default:
        break;
    }

    if (cmd[0] == '\0')
        cmd = NULL;

    result = nh_command(cmd, count, &arg);

    gid = gameid;
    if (result >= GAME_OVER) {
        close(gamefd);
        log_msg("Game %d (by %s) closed: game %s.", gameid, user_info.username,
                result == GAME_SAVED ? "saved" : "ended");
        gamefd = -1;
        gameid = 0;
    }

    client_msg("game_command", json_pack("{si}", "return", result));
    db_update_game(gameid, player_info.moves, player_info.z,
                   player_info.level_desc);

    /* move the finished game to its final resting place */
    if (result == GAME_OVER) {
        char basename[1024], filename[1024], final_name[1024];
        int len;
        char buf[BUFSZ];

        /* get the topten entry for the current game */
        struct nh_topten_entry *tte = nh_get_topten(&len, buf, NULL, 0, 0, 0);

        if (!db_get_game_filename(user_info.uid, gid, basename, 1024))
            return;

        snprintf(filename, 1024, "%s/save/%s/%s", settings.workdir,
                 user_info.username, basename);
        snprintf(final_name, 1024, "%s/completed/%s", settings.workdir,
                 basename);
        rename(filename, final_name);
        db_add_topten_entry(gid, tte->points, tte->hp, tte->maxhp, tte->deaths,
                            tte->end_how, tte->death, tte->entrytxt);
    }
}
Ejemplo n.º 3
0
struct nh_topten_entry *nhnet_get_topten(int *out_len, char *statusbuf,
        const char *player, int top, int around, nh_bool own)
{
    struct nh_topten_entry *ttlist;
    json_t *jmsg, *jarr, *jobj;
    const char *msg, *plrole, *plrace, *plgend, *plalign, *name, *death, *entrytxt;
    int len, i, highlight;

    if (!nhnet_active())
        return nh_get_topten(out_len, statusbuf, player, top, around, own);

    *out_len = 0;
    if (!api_entry())
        return NULL;

    jmsg = json_pack("{ss,si,si,si}", "player", player ? player : "", "top", top,
                     "around", around, "own", own);
    jmsg = send_receive_msg("get_topten", jmsg);
    if (json_unpack(jmsg, "{so,ss!}", "toplist", &jarr, "msg", &msg) == -1 ||
            !json_is_array(jarr)) {
        print_error("Incorrect return object in nhnet_get_topten");
        ttlist = NULL;
    } else {
        len = json_array_size(jarr);
        strncpy(statusbuf, msg, BUFSZ-1);
        *out_len = len;
        ttlist = xmalloc((len+1) * sizeof(struct nh_topten_entry));
        memset(ttlist, 0, (len+1) * sizeof(struct nh_topten_entry));
        for (i = 0; i < len; i++) {
            jobj = json_array_get(jarr, i);
            json_unpack(jobj, "{si,si,si,si,si,si,si,si,si,si,si,si,si,ss,ss,ss,ss,ss,ss,ss,si!}",
                        "rank", &ttlist[i].rank,
                        "points", &ttlist[i].points,
                        "maxlvl", &ttlist[i].maxlvl,
                        "hp", &ttlist[i].hp,
                        "maxhp", &ttlist[i].maxhp,
                        "deaths", &ttlist[i].deaths,
                        "ver_major", &ttlist[i].ver_major,
                        "ver_minor", &ttlist[i].ver_minor,
                        "patchlevel", &ttlist[i].patchlevel,
                        "deathdate", &ttlist[i].deathdate,
                        "birthdate", &ttlist[i].birthdate,
                        "moves", &ttlist[i].moves,
                        "end_how", &ttlist[i].end_how,
                        "plrole", &plrole,
                        "plrace", &plrace,
                        "plgend", &plgend,
                        "plalign", &plalign,
                        "name", &name,
                        "death", &death,
                        "entrytxt", &entrytxt,
                        "highlight", &highlight);
            strncpy(ttlist[i].plrole, plrole, PLRBUFSZ - 1);
            strncpy(ttlist[i].plrace, plrace, PLRBUFSZ - 1);
            strncpy(ttlist[i].plgend, plgend, PLRBUFSZ - 1);
            strncpy(ttlist[i].plalign, plalign, PLRBUFSZ - 1);
            strncpy(ttlist[i].name, name, PL_NSIZ - 1);
            strncpy(ttlist[i].death, death, BUFSZ - 1);
            strncpy(ttlist[i].entrytxt, entrytxt, BUFSZ - 1);
            ttlist[i].highlight = highlight;
        }
    }
    json_decref(jmsg);

    api_exit();
    return ttlist;
}