viewing paste topic/11506- poll_0.3 | Athena

Posted on the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
/*
create table poll_title (
poll_id int(11) unsigned not null auto_increment primary key,
poll_title varchar(70),
char_name varchar(23),
poll_time datetime,
poll_level smallint(6),
poll_status enum('0','1') default '1'
) engine = innodb;
 
create table poll_options (
poll_id int(11) unsigned,
poll_option tinyint(4) unsigned,
poll_option_name varchar(70),
poll_votes int(11) unsigned not null,
key (poll_id)
) engine = innodb;
 
create table poll_votes (
poll_id int(11) unsigned,
poll_option tinyint(4) unsigned,
account_id int(11) unsigned,
name varchar(23),
key (poll_id),
key (poll_option),
key (account_id),
unique ( poll_id, poll_option, account_id )
) engine = innodb;
*/
 
prontera,155,183,5  script  Poll NPC    1_F_MARIA,{
    mes "["+ strnpcinfo(1) +"]";
    query_sql "select poll_id, poll_title, char_name, date_format( poll_time, '%W %e/%c/%y %r' ), poll_level from poll_title order by poll_id desc limit 1", .@id, .@title$, .@creator$, .@time$, .@level;
    if ( BaseLevel < .@level ) {
        mes "You must be level "+ .@level +" to view and vote in this Poll";
        close;
    }
    mes "By : ^0000FF"+ .@creator$ +"^000000";
    mes "Since : ^0000FF"+ .@time$ +"^000000";
    mes " ";
    mes "Question : ^0000FF"+ .@title$ +"^000000";
    if ( !query_sql( "select 1 from poll_votes where poll_id = "+ .@id +" and account_id = "+ getcharid(3), .@dummy ) ) {
        next;
        .@nb = query_sql( "select poll_option_name from poll_options where poll_id = "+ .@id, .@options$ );
        for ( .@i = 0; .@i < .@nb; ++.@i )
            .@menu$ = .@menu$ +"^0000FF"+ .@options$[.@i] +":";
        .@s = select( .@menu$ );
        query_sql "insert into poll_votes values ( "+ .@id +", "+ .@s +", "+ getcharid(3) +", '"+ escape_sql( strcharinfo(0) )+"' )";
        query_sql "update poll_options set poll_votes = poll_votes +1 where poll_id = "+ .@id +" and poll_option = "+ .@s;
        close;
    }
    query_sql "select sum(poll_votes) from poll_options where poll_id = "+ .@id, .@total_votes;
    .@nb = query_sql( "select poll_option_name, poll_votes, poll_votes / "+ .@total_votes +" * 1000, poll_votes / "+ .@total_votes +" * 35 from poll_options where poll_id = "+ .@id +" order by poll_votes desc", .@options$, .@votes, .@percent, .@chart );
    mes " ";
    .@j = 0;
    while ( .@j < .@nb ) {
        mes ( .@j +1 )+". ^0000FF"+ .@options$[.@j] +"^000000";
        .@i = 0;
        while ( .@i < .@chart[.@j] ) {
            .@str$[.@j] = .@str$[.@j] +"#";
            ++.@i;
        }
        if ( .@i < 35 ) {
            .@str$[.@j] = .@str$[.@j] +"^CCCCCC";
            while ( .@i < 35 ) {
                .@str$[.@j] = .@str$[.@j] +"#";
                ++.@i;
            }
        }
        mes .@str$[.@j];
        mes "^999999"+ .@votes[.@j] +"(votes) / "+ .@total_votes +"(total) = ^CC6600"+( .@percent[.@j] / 10 ) +"."+( .@percent[.@j] % 10 )+"%^000000";
        ++.@j;
        mes " ";
    }
    close;
OnInit:
    waitingroom "New Poll", 0;
    end;
}
 
-   script  poll    FAKE_NPC,{
OnWhisperGlobal:
    query_sql "select poll_id, poll_title, char_name, date_format( poll_time, '%W %e/%c/%y %r' ), poll_status from poll_title order by poll_id desc limit 1", .@id, .@title$, .@name$, .@time$, .@status;
    if ( .@status == 1 ) {
        mes "do you wish to close current poll ?";
        mes " ";
        mes "By : ^0000FF"+ .@name$ +"^000000";
        mes "Since : ^0000FF"+ .@time$ +"^000000";
        mes " ";
        mes "Question : ^0000FF"+ .@title$ +"^000000";
        next;
        if ( select ( "No:Yes" ) == 1 ) close;
        announce strcharinfo(0) +" has closed the poll ["+ .@title$ +"]", 0;
        query_sql "update poll_title set poll_status = '0' where poll_id = "+ .@id;
        delwaitingroom "Poll NPC";
        disablenpc "Poll NPC";
        close;
    }
    .@title$ = ""; // hmm .... -.-"
    while (1) {
        mes "Question : "+( ( getstrlen(.@title$) )? "^0000FF"+ .@title$ : "^FF0000Incomplete" )+"^000000";
        .@size = getarraysize(.@options$);
        if ( !.@size )
            mes "Options : ^FF0000Incomplete^000000";
        else
            for ( .@i = 0; .@i < .@size; ++.@i )
                mes "Option "+( .@i+1 )+" : ^0000FF"+ .@options$[.@i] +"^000000";
        if ( .@baselevel ) {
            mes " ";
            mes "Base Level Requirement : "+ .@baselevel;
        }
        next;
        switch ( select( "Question"+( ( getstrlen(.@title$) )? "" : " - ^FF0000Incomplete^000000" ),
            "Options "+( ( .@size >= 2 )? "^00CC00["+ .@size +"]" : "^FF0000["+ .@size +"] - Incomplete" )+"^000000",
            "Base Level Requirement",
            ( ( getstrlen(.@title$) && .@size >= 2 )? "^00CC00":"^FF0000" )+"Complete Setup" ) ) {
            case 1:
                mes "Question : "+( ( getstrlen(.@title$) )? "^0000FF"+ .@title$ : "^FF0000Incomplete" )+"^000000";
                mes " ";
                mes "Input the poll question";
                next;
                if ( input( .@tmp$, 2, 70 ) ) {
                    mes "Poll title must between 2~70 characters";
                    next;
                    break;
                }
                else if ( compare ( .@tmp$, ":" ) || compare ( .@tmp$, "^" ) ) {
                    mes "Poll Question must not allow to use these symbols";
                    mes "-> : <- colon";
                    mes "-> ^ <- caret";
                    next;
                    break;
                }
                mes "Question : "+( ( getstrlen(.@title$) )? "^0000FF"+ .@title$ : "^FF0000Incomplete" )+"^000000";
                mes " ";
                mes "Confirming this title ?";
                mes "^0000FF"+ .@tmp$ +"^000000";
                next;
                if ( select ( "Yes", "No" ) == 2 ) break;
                .@title$ = .@tmp$;
                break;
            case 2:
                while (1) {
                    mes "Question : "+( ( getstrlen(.@title$) )? "^0000FF"+ .@title$ : "^FF0000Incomplete" )+"^000000";
                    .@size = getarraysize(.@options$);
                    if ( !.@size )
                        mes "Options : ^FF0000Incomplete^000000";
                    else
                        for ( .@i = 0; .@i < .@size; ++.@i )
                            mes "Option "+( .@i+1 )+" : ^0000FF"+ .@options$[.@i] +"^000000";
                    next;
                    switch ( select( "Add new Option", "Delete an Option", "- back" ) ) {
                        case 1:
                            mes "Question : "+( ( getstrlen(.@title$) )? "^0000FF"+ .@title$ : "^FF0000Incomplete" )+"^000000";
                            .@size = getarraysize(.@options$);
                            if ( !.@size )
                                mes "Options : ^FF0000Incomplete^000000";
                            else
                                for ( .@i = 0; .@i < .@size; ++.@i )
                                    mes "Option "+( .@i+1 )+" : ^0000FF"+ .@options$[.@i] +"^000000";
                            mes " ";
                            if ( .@size == 30 ) {
                                mes "The Poll only allow up to 30 different options";
                                next;
                                break;
                            }
                            mes "Input Option No."+( .@size +1 );
                            next;
                            if ( input( .@tmp$, 2, 70 ) ) {
                                mes "Poll options must between 2~70 characters";
                                next;
                                break;
                            }
                            else if ( compare ( .@tmp$, ":" ) || compare ( .@tmp$, "^" ) ) {
                                mes "Poll title must not allow to use these symbols";
                                mes "-> : <- colon";
                                mes "-> ^ <- caret";
                                next;
                                break;
                            }
                            .@options$[ .@size ] = .@tmp$;
                            break;
                        case 2:
                            mes "Question : "+( ( getstrlen(.@title$) )? "^0000FF"+ .@title$ : "^FF0000Incomplete" )+"^000000";
                            .@size = getarraysize(.@options$);
                            if ( !.@size )
                                mes "Options : ^FF0000Incomplete^000000";
                            else
                                for ( .@i = 0; .@i < .@size; ++.@i )
                                    mes "Option "+( .@i+1 )+" : ^0000FF"+ .@options$[.@i] +"^000000";
                            mes " ";
                            if ( !.@size ) {
                                mes "There are no Options to remove";
                                next;
                                break;
                            }
                            .@menu$ = "";
                            for ( .@i = 0; .@i < .@size; ++.@i )
                                .@menu$ = .@menu$ +"^0000FF"+ .@options$[.@i] +":";
                            .@menu$ = .@menu$ +"^000000- back";
                            next;
                            .@s = select( .@menu$ ) -1;
                            if ( .@s == .@size ) break;
                            deletearray .@options$[ .@s ], 1;
                            break;
                        case 3:
                            break;
                    }
                    if ( @menu == 3 ) break;
                }
                break;
            case 3:
                mes "Question : "+( ( getstrlen(.@title$) )? "^0000FF"+ .@title$ : "^FF0000Incomplete" )+"^000000";
                mes " ";
                mes "Base Level Requirement : "+ .@baselevel;
                next;
                if ( input( .@tmp, 0, MAX_LEVEL ) ) {
                    mes "Question : "+( ( getstrlen(.@title$) )? "^0000FF"+ .@title$ : "^FF0000Incomplete" )+"^000000";
                    mes " ";
                    mes "Invalid range";
                    next;
                }
                else
                    .@baselevel = .@tmp;
                break;
            case 4:
                if ( !getstrlen(.@title$) ) {
                    mes "Question must not be leaving blank !";
                    next;
                    break;
                }
                else if ( .@size < 2 ) {
                    mes "Must setup at least 2 Options !";
                    next;
                    break;
                }
                query_sql "insert into poll_title values ( null, '"+ escape_sql(.@title$) +"', '"+ escape_sql( strcharinfo(0) ) +"', now(), "+ .@base_level +", '1' )";
                query_sql "select max(poll_id) from poll_title", .@poll_id;
                .@poll_options$ = "insert into poll_options values ";
                for ( .@i = 0; .@i < .@size; ++.@i )
                    .@poll_options$ = .@poll_options$ +"( "+ .@poll_id +", "+( .@i +1 )+", '"+ escape_sql(.@options$[.@i]) +"', 0 )"+( ( .@i < .@size -1 )? ",":";" );
                query_sql .@poll_options$;
                announce strcharinfo(0) +" has open a New Poll !! ["+ .@title$ +"]", 0;
                enablenpc "Poll NPC";
                donpcevent "Poll NPC::OnInit";
                close;
        }
    }
    close; // shouldn't reach
OnInit:
    query_sql "select poll_status from poll_title order by poll_id desc limit 1", .@status;
    if ( !.@status ) {
        sleep 1;
        delwaitingroom "Poll NPC";
        disablenpc "Poll NPC";
    }
    bindatcmd "poll", strnpcinfo(3)+"::OnWhisperGlobal";
    end;
}
Viewed 1148 times, submitted by AnnieRuru.