//================================================================
//  Vote Point Trader NPC
//  Created by Pajonox
//  Works on Hercules and rAthena (SQL mode)
//  Players can exchange votepoints (stored in `login`.`votepoints`) 
//  for items you configure below.
//  Voting Guide: https://rankedprivateservers.com/vote_return_guide.php
//================================================================
//  CONFIGURE ITEMS & COSTS BELOW
//  Each item in .ItemID must have a matching cost in .ItemCost.
//================================================================

prontera,146,180,6	script	Vote Point Trader	123,{
    mes "[Vote Point Trader]";
    
    // Get player's vote points
    .@aid = getcharid(3);
    .@sql_result = query_sql("SELECT `votepoints` FROM `login` WHERE `account_id` = '" + .@aid + "'", @points);
    if (.@sql_result == 0) {
        mes "^FF0000Database error! Unable to retrieve vote points.^000000";
        close;
    }
    if (@points < 0) set @points, 0;
    
    mes "You currently have ^00ff00" + @points + " vote points^000000.";
    mes "If you are unsure how to vote, go to the website";
    mes "and check the character in the right down corner of your screen.";
    next;
    
    // Build menu
    .@menu$ = "";
    for (.@i = 0; .@i < getarraysize(.ItemID); .@i++) {
        .@itemname$ = getitemname(.ItemID[.@i]);
        if (.@itemname$ == "") .@itemname$ = "Item #" + .ItemID[.@i];
        .@menu$ += .@itemname$ + " - " + .ItemCost[.@i] + " pts:";
    }
    .@menu$ += "Cancel";
    
    .@sel = select(.@menu$) - 1;
    
    if (.@sel < 0 || .@sel >= getarraysize(.ItemID)) {
        mes "Come back anytime.";
        close;
    }
    
    .@item = .ItemID[.@sel];
    .@cost = .ItemCost[.@sel];
    
    // Check if player has enough points
    .@aid = getcharid(3);
    .@sql_result = query_sql("SELECT `votepoints` FROM `login` WHERE `account_id` = '" + .@aid + "'", @points);
    if (.@sql_result == 0) {
        mes "^FF0000Database error! Cannot verify vote points.^000000";
        close;
    }
    if (@points < .@cost) {
        mes "Sorry, you need " + .@cost + " vote points for that item.";
        close;
    }
    
    // Check weight
    if (checkweight(.@item, 1) == 0) {
        mes "You are overweight or out of inventory slots.";
        close;
    }
    
    // Give item and deduct points
    getitem .@item, 1;
    .@aid = getcharid(3);
    .@sql_result = query_sql("UPDATE `login` SET `votepoints` = `votepoints` - " + .@cost + " WHERE `account_id` = '" + .@aid + "' LIMIT 1");
    if (.@sql_result == 0) {
        mes "^FF0000Warning: Points may not have been deducted properly!^000000";
    }
    
    mes "Transaction successful! You received ^008800" + getitemname(.@item) + "^000000.";
    close;

OnInit:
    setarray .ItemID[0], 35017, 35018, 35019, 35021, 35022, 35020;
    setarray .ItemCost[0], 2, 2, 2, 1, 1, 5;
    end;
}
