viewing paste channel_fee plugin for Hercules | C

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
/**
 * This file is part of Hercules.
 * http://herc.ws - http://github.com/HerculesWS/Hercules
 *
 * Copyright (C) 2013-2015  Hercules Dev Team
 *
 * Hercules is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
/// Sample Hercules Plugin
 
#include "common/hercules.h" /* Should always be the first Hercules file included! (if you don't make it first, you won't be able to use interfaces) */
#include "common/mmo.h"
#include "common/db.h"
#include "common/utils.h"
#include "common/timer.h"
#include "common/nullpo.h"
#include "map/channel.h"
#include "map/irc-bot.h"
#include "map/pc.h"
#include "map/itemdb.h"
#include "map/clif.h"
#include "map/script.h"
#include "map/status.h"
 
#include "plugins/HPMHooking.h"
#include "common/HPMDataCheck.h" /* should always be the last Hercules file included! (if you don't make it last, it'll intentionally break compile time) */
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
HPExport struct hplugin_info pinfo = {
    "Channel Paid Messages",    // Plugin name
    SERVER_TYPE_MAP,// Which server types this plugin works with?
    "0.1",       // Plugin version
    HPM_VERSION, // HPM Version (don't change, macro is automatically updated)
};
 
 
enum {
    CF_NONE = 0,
    CF_ZENY = 1,
    CF_ITEM = 2
};
 
/**
 * Sends a message to a channel.
 *
 * @param chan The destination channel.
 * @param sd   The source character.
 * @param msg  The message to send.
 *
 * If no source character is specified, it'll send an anonymous message.
 */
void channel_send_fee (struct channel_data *chan, struct map_session_data *sd, const char *msg)
{
    char message[150];
    int i, zeny = 0;
    struct item it;
    int fee = CF_NONE;
    int idx;
    char alert[150];
 
 
    it.nameid = 0;
    it.amount = 0;
    nullpo_retv(chan);
    nullpo_retv(msg);
 
    // FIXME: make a better way to add fee on channels (directly on channels.conf)
    const struct {
        char name[HCS_NAME_LENGTH];
        int zeny;
        int nameid;
        int amount;
    } channels_fee[] = {
        // -- Add channels with fee like the example: {"name", zeny, {item id, item count}};
        {"main"    , 1000, 0  , 0}, // #main - 1000z and no item
    };
 
    if (sd && chan->msg_delay != 0
     && DIFF_TICK(sd->hchsysch_tick + chan->msg_delay*1000, timer->gettick()) > 0
     && !pc_has_permission(sd, PC_PERM_HCHSYS_ADMIN)) {
        clif->messagecolor_self(sd->fd, COLOR_RED, msg_sd(sd,1455));
        return;
    } else if (sd) {
 
        // -- Check for fees and pay them if exists
        for ( i = 0; i < ARRAYLENGTH(channels_fee); i++ ) {
            if ( strcmpi(channels_fee[i].name, chan->name) == 0 ) {
                if ( channels_fee[i].zeny > 0 ) {
                    fee |= CF_ZENY;
                    zeny = channels_fee[i].zeny;    
                }
                if ( itemdb->exists(channels_fee[i].nameid) ) {
                    fee |= CF_ITEM;
                    it.nameid = channels_fee[i].nameid;
                    it.amount = channels_fee[i].amount;
                }
                break;
            }
        }
 
        do {
            if ( fee ) {
                bool fee_ok = false;
                if ( fee&CF_ZENY ) {
                    // -- Zeny
                    if ( zeny > sd->status.zeny ) {
                        safesnprintf(alert, 150, "You need at least %d zeny to send messages on #%s channel.", zeny, chan->name);
                        clif->messagecolor_self(sd->fd, COLOR_RED, alert);
                        break;
                    } else {
                        fee_ok = true;
                    }
                }
                if ( fee&CF_ITEM ) {
                    // -- Item
                    idx = pc->search_inventory(sd, it.nameid);
                    if ( idx == INDEX_NOT_FOUND || sd->status.inventory[idx].amount < it.amount ) {
                        struct item_data *id = itemdb->search(it.nameid);
                        safesnprintf(alert, 150, "You need at least [%d x %s] to send messages on #%s channel.", it.amount, id->jname, chan->name);
                        clif->messagecolor_self(sd->fd, COLOR_RED, alert);
                        break;
                    } else {
                        fee_ok = true;
                    }
                }
 
                if ( fee_ok ) {
                    if ( fee&CF_ITEM )
                        if ( pc->delitem(sd, idx, it.amount, 0, DELITEM_NORMAL, LOG_TYPE_SCRIPT) ) // Returns 0 uppon success and 1 uppon failure (It's strange)
                            break;
                    if ( fee&CF_ZENY )
                        pc->payzeny(sd, zeny, LOG_TYPE_NPC, NULL);
                }
            }
            safesnprintf(message, 150, "[ #%s ] %s : %s", chan->name, sd->status.name, msg);
            clif->channel_msg(chan, sd, message);
            if (chan->type == HCS_TYPE_IRC)
                ircbot->relay(sd->status.name,msg);
            if (chan->msg_delay != 0)
                sd->hchsysch_tick = timer->gettick();
        } while(0);
 
    } else {
        safesnprintf(message, 150, "[ #%s ] %s", chan->name, msg);
        clif->channel_msg2(chan, message);
        if (chan->type == HCS_TYPE_IRC)
            ircbot->relay(NULL, msg);
    }
}
 
HPExport void server_online (void) {
    ShowInfo ("'%s' Plugin by Bio/Hercules. Version '%s'\n", pinfo.name, pinfo.version);
    channel->send = channel_send_fee;
}
Viewed 1070 times, submitted by Guest.