nightmare4_fluffos_v1/
nightmare4_fluffos_v1/bin/
nightmare4_fluffos_v1/fluffos-2.9-ds2.14/
nightmare4_fluffos_v1/fluffos-2.9-ds2.14/ChangeLog.old/
nightmare4_fluffos_v1/fluffos-2.9-ds2.14/Win32/
nightmare4_fluffos_v1/fluffos-2.9-ds2.14/compat/
nightmare4_fluffos_v1/fluffos-2.9-ds2.14/compat/simuls/
nightmare4_fluffos_v1/fluffos-2.9-ds2.14/include/
nightmare4_fluffos_v1/fluffos-2.9-ds2.14/testsuite/
nightmare4_fluffos_v1/fluffos-2.9-ds2.14/testsuite/clone/
nightmare4_fluffos_v1/fluffos-2.9-ds2.14/testsuite/command/
nightmare4_fluffos_v1/fluffos-2.9-ds2.14/testsuite/data/
nightmare4_fluffos_v1/fluffos-2.9-ds2.14/testsuite/etc/
nightmare4_fluffos_v1/fluffos-2.9-ds2.14/testsuite/include/
nightmare4_fluffos_v1/fluffos-2.9-ds2.14/testsuite/inherit/
nightmare4_fluffos_v1/fluffos-2.9-ds2.14/testsuite/inherit/master/
nightmare4_fluffos_v1/fluffos-2.9-ds2.14/testsuite/log/
nightmare4_fluffos_v1/fluffos-2.9-ds2.14/testsuite/single/
nightmare4_fluffos_v1/fluffos-2.9-ds2.14/testsuite/single/tests/compiler/
nightmare4_fluffos_v1/fluffos-2.9-ds2.14/testsuite/single/tests/efuns/
nightmare4_fluffos_v1/fluffos-2.9-ds2.14/testsuite/single/tests/operators/
nightmare4_fluffos_v1/fluffos-2.9-ds2.14/testsuite/u/
nightmare4_fluffos_v1/fluffos-2.9-ds2.14/tmp/
nightmare4_fluffos_v1/fluffos-2.9-ds2.14/windows/
nightmare4_fluffos_v1/lib/cfg/
nightmare4_fluffos_v1/lib/cmds/admins/
nightmare4_fluffos_v1/lib/cmds/common/
nightmare4_fluffos_v1/lib/cmds/creators/include/
nightmare4_fluffos_v1/lib/cmds/creators/include/SCCS/
nightmare4_fluffos_v1/lib/cmds/hm/
nightmare4_fluffos_v1/lib/daemon/services/
nightmare4_fluffos_v1/lib/doc/
nightmare4_fluffos_v1/lib/doc/faq/
nightmare4_fluffos_v1/lib/doc/help/classes/
nightmare4_fluffos_v1/lib/doc/help/creators/
nightmare4_fluffos_v1/lib/doc/help/hm/
nightmare4_fluffos_v1/lib/doc/help/players/
nightmare4_fluffos_v1/lib/doc/help/races/
nightmare4_fluffos_v1/lib/doc/help/religion/
nightmare4_fluffos_v1/lib/doc/mudlib/
nightmare4_fluffos_v1/lib/doc/mudlib/cfg/
nightmare4_fluffos_v1/lib/domains/Ylsrim/
nightmare4_fluffos_v1/lib/domains/Ylsrim/adm/
nightmare4_fluffos_v1/lib/domains/Ylsrim/armour/
nightmare4_fluffos_v1/lib/domains/Ylsrim/fish/
nightmare4_fluffos_v1/lib/domains/Ylsrim/meal/
nightmare4_fluffos_v1/lib/domains/Ylsrim/npc/
nightmare4_fluffos_v1/lib/domains/Ylsrim/virtual/
nightmare4_fluffos_v1/lib/domains/Ylsrim/weapon/
nightmare4_fluffos_v1/lib/domains/Ylsrim/xtra/
nightmare4_fluffos_v1/lib/lib/comp/
nightmare4_fluffos_v1/lib/lib/lvs/
nightmare4_fluffos_v1/lib/lib/std/
nightmare4_fluffos_v1/lib/lib/user/
nightmare4_fluffos_v1/lib/news/
nightmare4_fluffos_v1/lib/obj/
nightmare4_fluffos_v1/lib/obj/include/
nightmare4_fluffos_v1/lib/save/
nightmare4_fluffos_v1/lib/save/kills/a/
nightmare4_fluffos_v1/lib/save/kills/b/
nightmare4_fluffos_v1/lib/save/kills/f/
nightmare4_fluffos_v1/lib/save/kills/m/
nightmare4_fluffos_v1/lib/save/kills/q/
nightmare4_fluffos_v1/lib/save/kills/r/
nightmare4_fluffos_v1/lib/secure/cfg/
nightmare4_fluffos_v1/lib/secure/cfg/classes/
nightmare4_fluffos_v1/lib/secure/cfg/races/SCCS/
nightmare4_fluffos_v1/lib/secure/cmds/creators/include/
nightmare4_fluffos_v1/lib/secure/cmds/players/
nightmare4_fluffos_v1/lib/secure/cmds/players/include/
nightmare4_fluffos_v1/lib/secure/daemon/include/
nightmare4_fluffos_v1/lib/secure/lib/
nightmare4_fluffos_v1/lib/secure/lib/include/
nightmare4_fluffos_v1/lib/secure/lib/net/
nightmare4_fluffos_v1/lib/secure/lib/std/
nightmare4_fluffos_v1/lib/secure/obj/
nightmare4_fluffos_v1/lib/secure/obj/include/
nightmare4_fluffos_v1/lib/secure/save/
nightmare4_fluffos_v1/lib/secure/save/boards/
nightmare4_fluffos_v1/lib/secure/save/votes/
nightmare4_fluffos_v1/lib/spells/
nightmare4_fluffos_v1/lib/verbs/admins/include/
nightmare4_fluffos_v1/lib/verbs/common/
nightmare4_fluffos_v1/lib/verbs/common/include/
nightmare4_fluffos_v1/lib/verbs/creators/
nightmare4_fluffos_v1/lib/verbs/creators/include/
nightmare4_fluffos_v1/lib/verbs/players/include/SCCS/
nightmare4_fluffos_v1/lib/verbs/rooms/
nightmare4_fluffos_v1/lib/verbs/rooms/include/
nightmare4_fluffos_v1/lib/www/
nightmare4_fluffos_v1/old/
nightmare4_fluffos_v1/old/tools/
nightmare4_fluffos_v1/win32/
/*
 *  /secure/daemon/voting.c
 *  from the Nightmare IV mudlib
 *  Handle player elections
 *  by Kalinash on 961026
 *  Version : @(#) voting.c 1.3@(#)
 *  Last Modified : 96/10/26
 */

#include <lib.h>
#include <daemons.h>
#include <voting.h>
#include <clock.h>

inherit LIB_DAEMON;

private mapping mapVoting;
private mapping mapCouncil;

static void create() {
    daemon::create();
    SetNoClean(1);
    if( file_exists( SAVE_VOTES __SAVE_EXTENSION__ ) ) {
        unguarded( (: restore_object, SAVE_VOTES :) );
        if( mapVoting["status"] == VOTE_RUNNING )
            call_out( (: eventNextDay :), DAY );
    }

    if( ! mapVoting ) {
        mapVoting = ([]);
        mapVoting["candidates"] = ([]);
        mapVoting["votes"] = ([]);
        mapVoting["voted"] = ({});
        foreach( string sClass in CLASSES_D->GetClasses() ) {
            mapVoting["candidates"][sClass] = ({});
            mapVoting["votes"][sClass] = ([]);
        }
        mapVoting["status"] = VOTE_NOT_RUNNING;
    }
    if( ! mapCouncil ) 
        mapCouncil = ([]);

    eventSave();
}

mixed eventSave() {
    unguarded( (: save_object, SAVE_VOTES :) );
    return VOTE_SUCCESS;
}

mixed eventStartVoting() {
    if( GetStatus() == VOTE_RUNNING )
        return VOTE_ALREADY_RUNNING;

    mapVoting["candidates"] = ([]);
    mapVoting["votes"] = ([]);
    mapVoting["voted"] = ({});
    foreach( string sClass in CLASSES_D->GetClasses() ) {
        mapVoting["candidates"][sClass] = ({});
        mapVoting["votes"][sClass] = ([]);
    }
 
    mapVoting["status"] = VOTE_RUNNING;
    mapVoting["mode"] = VOTE_MODE_CANDIDATES;
    mapVoting["daycount"] = VOTE_DAY_COUNT;
    call_out( (: eventNextDay :), DAY );
    message("shout", "%^YELLOW%^Election announcement:%^RESET%^ " +
      "Class elections have now begun! Go to the voting booth " +
      "and nominate candidates.", users() );
    eventSave();
    return VOTE_SUCCESS;
}

mixed eventEndVoting() {
    mapVoting["status"] = VOTE_NOT_RUNNING;
    eventTallyVotes();
    eventSave();
}

mixed eventAddCandidate( string sClass, string sWho ) {
    object ob;
    string sOldClass;
    
    sWho = convert_name( sWho );
    ob = find_player( sWho );

    if( GetStatus() == VOTE_NOT_RUNNING )
        return VOTE_NOT_RUNNING;
    if( mapVoting["mode"] != VOTE_MODE_CANDIDATES )
        return VOTE_MODE_VOTING;
    if( ! ob ) return VOTE_ERROR;
    if( ! ob->ClassMember(sClass) )
        return VOTE_NOT_CLASS_MEMBER;
    
    foreach( string cls in CLASSES_D->GetClasses() )            
        if( member_array( sWho, mapVoting["candidates"][cls] ) != -1 )
            return VOTE_ALREADY_RUNNING;
        
    mapVoting["candidates"][sClass] += ({ sWho });
    mapVoting["votes"][sClass][sWho] = 0;
    message("shout", "%^YELLOW%^Election announcement:%^RESET%^ " + 
      ob->GetName() + " has been nominated for the leader of the " +
      pluralize(capitalize(sClass)) + ".", users() );
    eventSave();
    return VOTE_SUCCESS;
}

mixed eventRemoveCandidate( string sClass, string sWho ) {
    sWho = convert_name( sWho );
    
    if( GetStatus() == VOTE_NOT_RUNNING )
        return VOTE_NOT_RUNNING;
    if( mapVoting["mode"] != VOTE_MODE_CANDIDATES )
        return VOTE_MODE_VOTING;
    if( member_array( sWho, mapVoting["candidates"][sClass] ) == -1 )
        return VOTE_NOT_CANDIDATE;
    
    mapVoting["candidates"][sClass] -= ({ sWho });
    map_delete( mapVoting["votes"][sClass], sWho );
    message("shout", "%^YELLOW%^Election announcement:%^RESET%^ " + 
      find_player(sWho)->GetName() + " has withdrawn from the " +
      "elections!", users() );
    eventSave();
    return VOTE_SUCCESS;
}

mixed eventCastVote( string sClass, string sVoter, string sVotee ) {
    mapping mapChar;
    
    sVoter = convert_name( sVoter );
    sVotee = convert_name( sVotee );
    
    if( GetStatus() == VOTE_NOT_RUNNING )
        return VOTE_NOT_RUNNING;
    
    if( mapVoting["mode"] != VOTE_MODE_VOTING )
        return VOTE_MODE_CANDIDATES;
        
    mapChar = CHARACTER_D->GetLink( sVoter );
    
    if( mapChar )
        if( mapChar["primary"] != sVoter )
            return VOTE_NOT_PRIMARY;
    
    if( member_array( sVotee, mapVoting["candidates"][sClass] ) == -1 )
        return VOTE_NOT_CLASS_MEMBER;

    if( member_array( sVoter, mapVoting["voted"] ) != -1 )
        return VOTE_ALREADY_VOTED;
        
    mapVoting["voted"] += ({ sVoter });
    mapVoting["votes"][sClass][sVotee]++;
    eventSave();
    return VOTE_SUCCESS;
}
        
mixed eventNextDay() {
    mapVoting["daycount"]--;
    
    if( ! mapVoting["daycount"] ) {
        if( mapVoting["mode"] == VOTE_MODE_CANDIDATES ) {
            mapVoting["mode"] = VOTE_MODE_VOTING;
            mapVoting["daycount"] = VOTE_DAY_COUNT;
            call_out( (: eventNextDay :), DAY );
            message("shout", "%^YELLOW%^Election announcement:%^RESET%^ " +
              "Nominations are closed, go vote for the candidates!!", users() );
            eventSave();
            return VOTE_SUCCESS;
        }
        
        eventEndVoting();
        return VOTE_SUCCESS;
    }

    message("shout", "%^YELLOW%^Election announcement:%^RESET%^ " +
      "Only " + mapVoting["daycount"] + " " + 
      ( (int) mapVoting["daycount"] > 1 ) ? "days" : "day" + " left to " + 
      ( (int) mapVoting["mode"] == VOTE_MODE_VOTING )
      ? "vote for the candidates." : "nominate candidates.", users());
    call_out( (: eventNextDay :), DAY );
    eventSave();
    return VOTE_SUCCESS;
}

mixed eventTallyVotes() {
    mapping mapWho;
    string  *asWho;

    mapCouncil = ([]);
    
    foreach( string sClass in CLASSES_D->GetClasses() ) {
        mapWho = mapVoting["votes"][lower_case(sClass)];
        if( ! ( sizeof( asWho = keys( mapWho ) ) ) )
            return VOTE_SUCCESS;

        while( sizeof( asWho ) >= 2 ) {
            string player1, player2;

            player1 = asWho[0];
            player2 = asWho[1];

            if( mapWho[player1] > mapWho[player2] )
                asWho -= ({ player2 });
            else
                asWho -= ({ player1 });
        }
                
        mapCouncil[lower_case(sClass)] = asWho[0];
    }

    eventSave();
    return VOTE_SUCCESS;
}

int GetStatus() { return mapVoting["status"]; }
int GetMode() { return mapVoting["mode"]; }

string *GetCandidates( string sClass ) {
    return mapVoting["candidates"][lower_case(sClass)];
}

string *GetCouncil() {
    string *asWho;
    
    asWho = ({});

    foreach( string sClass in CLASSES_D->GetClasses() )
        asWho += ({ mapCouncil[sClass] });
        
    return asWho;
}

string GetCouncilMember( string sClass ) { return mapCouncil[sClass]; }

int GetVoteStatus( object ob ) {
    string sVoter;
    mapping mapChar;

    sVoter = convert_name( ob->GetName() );

    if( mapVoting["status"] == VOTE_NOT_RUNNING )
        return VOTE_ALREADY_VOTED;
    if( mapVoting["mode"] == VOTE_MODE_CANDIDATES )
        return VOTE_ALREADY_VOTED;
    if( member_array( sVoter, mapVoting["voted"] ) != -1 )
        return VOTE_ALREADY_VOTED;
    if( ! ob->GetClass() )
        return VOTE_ALREADY_VOTED;
    mapChar = CHARACTER_D->GetLink( sVoter );

    if( mapChar )
        if( mapChar["primary"] != sVoter )
        return VOTE_ALREADY_VOTED;

    return VOTE_SUCCESS;
}

string GetVoteRoom() { return VOTE_ROOM; }

int IsCouncilMember( object ob ) {
    foreach( string sClass in CLASSES_D->GetClasses() )
        if( mapCouncil[sClass] == convert_name(ob->GetName()) )
            return 1;
    return 0;
}

mixed GetTest() {
    foreach( string sClass in CLASSES_D->GetClasses() )
        this_player()->eventPrint( sClass + " : " + mapCouncil[sClass] );
    return 1;
}