nightmare3_fluffos_v2/
nightmare3_fluffos_v2/bin/
nightmare3_fluffos_v2/fluffos-2.9-ds2.08/
nightmare3_fluffos_v2/fluffos-2.9-ds2.08/ChangeLog.old/
nightmare3_fluffos_v2/fluffos-2.9-ds2.08/Win32/
nightmare3_fluffos_v2/fluffos-2.9-ds2.08/compat/
nightmare3_fluffos_v2/fluffos-2.9-ds2.08/compat/simuls/
nightmare3_fluffos_v2/fluffos-2.9-ds2.08/include/
nightmare3_fluffos_v2/fluffos-2.9-ds2.08/testsuite/
nightmare3_fluffos_v2/fluffos-2.9-ds2.08/testsuite/clone/
nightmare3_fluffos_v2/fluffos-2.9-ds2.08/testsuite/command/
nightmare3_fluffos_v2/fluffos-2.9-ds2.08/testsuite/data/
nightmare3_fluffos_v2/fluffos-2.9-ds2.08/testsuite/etc/
nightmare3_fluffos_v2/fluffos-2.9-ds2.08/testsuite/include/
nightmare3_fluffos_v2/fluffos-2.9-ds2.08/testsuite/inherit/
nightmare3_fluffos_v2/fluffos-2.9-ds2.08/testsuite/inherit/master/
nightmare3_fluffos_v2/fluffos-2.9-ds2.08/testsuite/log/
nightmare3_fluffos_v2/fluffos-2.9-ds2.08/testsuite/single/
nightmare3_fluffos_v2/fluffos-2.9-ds2.08/testsuite/single/tests/compiler/
nightmare3_fluffos_v2/fluffos-2.9-ds2.08/testsuite/single/tests/efuns/
nightmare3_fluffos_v2/fluffos-2.9-ds2.08/testsuite/single/tests/operators/
nightmare3_fluffos_v2/fluffos-2.9-ds2.08/testsuite/u/
nightmare3_fluffos_v2/fluffos-2.9-ds2.08/tmp/
nightmare3_fluffos_v2/fluffos-2.9-ds2.08/windows/
nightmare3_fluffos_v2/lib/cmds/ambassador/
nightmare3_fluffos_v2/lib/cmds/database/
nightmare3_fluffos_v2/lib/cmds/hm/
nightmare3_fluffos_v2/lib/cmds/soul/
nightmare3_fluffos_v2/lib/daemon/cfg/
nightmare3_fluffos_v2/lib/daemon/cfg/mon_races/
nightmare3_fluffos_v2/lib/daemon/cfg/races/
nightmare3_fluffos_v2/lib/daemon/include/
nightmare3_fluffos_v2/lib/daemon/save/
nightmare3_fluffos_v2/lib/daemon/services/
nightmare3_fluffos_v2/lib/daemon/soul/
nightmare3_fluffos_v2/lib/doc/
nightmare3_fluffos_v2/lib/doc/TestPlans/
nightmare3_fluffos_v2/lib/doc/approval/
nightmare3_fluffos_v2/lib/doc/approval/QC/
nightmare3_fluffos_v2/lib/doc/approval/balance/
nightmare3_fluffos_v2/lib/doc/build/
nightmare3_fluffos_v2/lib/doc/build/armours/
nightmare3_fluffos_v2/lib/doc/build/economy/
nightmare3_fluffos_v2/lib/doc/build/etc/
nightmare3_fluffos_v2/lib/doc/build/monster/
nightmare3_fluffos_v2/lib/doc/build/room/
nightmare3_fluffos_v2/lib/doc/build/virtual/
nightmare3_fluffos_v2/lib/doc/build/weapon/
nightmare3_fluffos_v2/lib/doc/classes/
nightmare3_fluffos_v2/lib/doc/efun/
nightmare3_fluffos_v2/lib/doc/etc/
nightmare3_fluffos_v2/lib/doc/help/creator/
nightmare3_fluffos_v2/lib/doc/help/hm/
nightmare3_fluffos_v2/lib/doc/law/
nightmare3_fluffos_v2/lib/doc/lpc/basic/
nightmare3_fluffos_v2/lib/doc/lpc/data_types/
nightmare3_fluffos_v2/lib/doc/lpc/etc/
nightmare3_fluffos_v2/lib/doc/lpc/intermediate/
nightmare3_fluffos_v2/lib/doc/lpc/types/
nightmare3_fluffos_v2/lib/doc/mudlib/
nightmare3_fluffos_v2/lib/doc/mudlib/features/
nightmare3_fluffos_v2/lib/domains/Examples/etc/
nightmare3_fluffos_v2/lib/domains/Examples/room/
nightmare3_fluffos_v2/lib/domains/Examples/virtual/
nightmare3_fluffos_v2/lib/domains/Examples/virtual/exaA/
nightmare3_fluffos_v2/lib/domains/Examples/virtual/exaB/
nightmare3_fluffos_v2/lib/domains/Examples/weapon/
nightmare3_fluffos_v2/lib/domains/Praxis/
nightmare3_fluffos_v2/lib/domains/Praxis/adm/
nightmare3_fluffos_v2/lib/domains/Praxis/attic/
nightmare3_fluffos_v2/lib/domains/Praxis/cemetary/
nightmare3_fluffos_v2/lib/domains/Praxis/cemetary/mon/
nightmare3_fluffos_v2/lib/domains/Praxis/data/
nightmare3_fluffos_v2/lib/domains/Praxis/death/
nightmare3_fluffos_v2/lib/domains/Praxis/mountains/
nightmare3_fluffos_v2/lib/domains/Praxis/obj/armour/
nightmare3_fluffos_v2/lib/domains/Praxis/obj/magic/
nightmare3_fluffos_v2/lib/domains/Praxis/obj/weapon/
nightmare3_fluffos_v2/lib/domains/Praxis/orc_valley/
nightmare3_fluffos_v2/lib/domains/Praxis/quests/
nightmare3_fluffos_v2/lib/domains/Praxis/standardOld/
nightmare3_fluffos_v2/lib/include/
nightmare3_fluffos_v2/lib/log/
nightmare3_fluffos_v2/lib/log/errors/
nightmare3_fluffos_v2/lib/log/reports/
nightmare3_fluffos_v2/lib/log/watch/
nightmare3_fluffos_v2/lib/news/
nightmare3_fluffos_v2/lib/secure/cfg/
nightmare3_fluffos_v2/lib/secure/cmds/ambassador/
nightmare3_fluffos_v2/lib/secure/cmds/mortal/
nightmare3_fluffos_v2/lib/secure/save/users/d/
nightmare3_fluffos_v2/lib/secure/std/
nightmare3_fluffos_v2/lib/std/hm/
nightmare3_fluffos_v2/lib/std/living/
nightmare3_fluffos_v2/lib/std/room/
nightmare3_fluffos_v2/lib/std/user/
nightmare3_fluffos_v2/lib/std/virtual/
nightmare3_fluffos_v2/lib/www/
nightmare3_fluffos_v2/lib/www/errors/
nightmare3_fluffos_v2/lib/www/gateways/
nightmare3_fluffos_v2/win32/
//    /daemon/voting.c
//    from the Nightmare Mudlib
//    daemon to handle democracy on the mud
//    original by Descartes of Borg
//    re-written to use mappings by Kalinash 031394

#include <security.h>
#include <voting.h>
#include <daemons.h>
#define DAY 86400
#include <std.h>

inherit DAEMON;

void save_totals(string drow);

private mapping elections;
int running, start_date;
string next_vote_date;

void create() {
    elections = ([]);
    running = 0;
    restore_object(VOTE_SAVE);
}

int register_vote(string who, string whom, string Class) {
    if(!running) return NOT_ELECTION_DAY;
    if((time() - start_date) < (DAY * 4)) return TOO_EARLY;
    if(!who || !whom || !Class) return VOTE_ERROR;
    if(!elections["voted"]) elections["voted"] = ([]);
    if(!elections["voted"][Class]) elections["voted"][Class] = ({});
    if(!elections["candidates"]) elections["candidates"] = ([]);
    if(!elections["candidates"][Class]) elections["candidates"][Class] = ({});
    if(member_array(whom, elections["candidates"][Class]) == -1)
        return NOT_RUNNING;
    if(member_array(who, elections["voted"][Class]) != -1) 
        return ALREADY_VOTED;
    if(MULTI_D->non_voter(who)) return ALREADY_VOTED;
    if((member_array(who, elections["voted"][Class]) != -1) 
      && (member_array(whom, elections["voted"][Class]) != -1))
        return BAD_VOTE;
    elections["voted"][Class] += ({ who });
    if(!elections["votes"]) elections["votes"] = ([]);
    if(!elections["votes"][Class]) elections["votes"][Class] = ([]);
    if(!elections["votes"][Class][whom])
        elections["votes"][Class][whom] = 0;
    ++elections["votes"][Class][whom];
    save_object(VOTE_SAVE);
    save_totals("normal");
    return VOTE_OK;
}

int register_candidate(string who, string Class) {
    if(!running) return NOT_ELECTION_DAY;
    if(!who || !Class) return VOTE_ERROR;
    if(member_array(Class, CLASSES) == -1) return VOTE_ERROR;
    if(!elections["candidates"])
        elections["candidates"] = ([]);
    if(!elections["candidates"][Class])
        elections["candidates"][Class] = ({});
    if(member_array(who, elections["candidates"][Class]) != -1)
        return ALREADY_RUNNING;
    elections["candidates"][Class] += ({ who });
    save_object(VOTE_SAVE);
    return VOTE_OK;
}

string *query_candidates(string Class) {
    if(!running) return ({});
    return elections["candidates"][Class];
}

void set_vote_date(string date) {
    next_vote_date = date;
    save_object(VOTE_SAVE);
    return;
}

string query_vote_date() {
    return next_vote_date;
}

int query_voted(string who, string Class) {
    if(!elections["voted"]) return 0;
    if(!elections["voted"][Class]) elections["voted"][Class] = ({});
    if(member_array(who, elections["voted"][Class]) != -1 ||
      MULTI_D->non_voter(who))
        return 1;
    else
        return 0;
}

int election_day() {
    return running;
}

int is_time_to_vote() {
    if(!running) return 0;
    if((time() - start_date) < (DAY * 4))
        return 0;
    else
        return 1;
}

void start_elections() {
    restore_object(VOTE_SAVE);
    running = 1;
    start_date = time();
    save_object(VOTE_SAVE);
    return;
}

void end_elections() {
    save_totals("normal");
    save_totals("archive");
    running = 0;
    elections = ([]);
    save_object(VOTE_SAVE);
}

void save_totals(string drow) {
    string *magi, *who;
    int x, z, q;

    if(drow == "normal") {
        rm(DIR_VOTES+"/votes");
        magi = keys(elections["votes"]);
        x = sizeof(magi);
        if(magi)
            for(z=0;z<x;++z) {
                who = keys(elections["votes"][magi[z]]);
            for(q=0;q<sizeof(who);++q)
                write_file(DIR_VOTES+"/votes", sprintf("%s - %s : %d\n", magi[z], who[q], elections["votes"][magi[z]][who[q]]));
            }
    }
    if(drow == "archive") {
        rm("/log/archive/votes");
        magi = keys(elections["votes"]);
        x = sizeof(magi);
        if(magi)
            for(z=0;z<x;++z) {
                who = keys(elections["votes"][magi[z]]);
            for(q=0;q<sizeof(who);++q)
                write_file(DIR_VOTES+"/votes", sprintf("%s - %s : %d\n", magi[z], who[q], elections["votes"][magi[z]][who[q]]));
            }
    }
}