viewing paste Unknown #36407 | 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 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 260 261 262 263 264 265 266 267 268 269 270 271 272 273
// Copyright (c) Athena Dev Teams - Licensed under GNU GPL
// For more information, see LICENCE in the main folder
 
#define HERCULES_CORE
 
#include "config/core.h"
#include "int_clan.h"
 
#include "char/char.h"
#include "char/inter.h"
#include "char/mapif.h"
#include "common/cbasetypes.h"
#include "common/db.h"
#include "common/memmgr.h"
#include "common/mmo.h"
#include "common/nullpo.h"
#include "common/showmsg.h"
#include "common/socket.h"
#include "common/sql.h"
#include "common/strlib.h"
#include "common/timer.h"
 
#include <stdio.h>
#include <stdlib.h>
 
struct inter_clan_interface inter_clan_;
struct inter_clan_interface *inter_clan;
 
 
//clan cache
static struct DBMap* clan_db_; // int clan_id -> struct clan*
 
int inter_clan_removemember_tosql(uint32 account_id, uint32 char_id){
        if( SQL_ERROR == SQL->Query( inter->sql_handle, "UPDATE `%s` SET `clan_id` = '0' WHERE `char_id` = '%d'", char_db, char_id ) ){
                Sql_ShowDebug( inter->sql_handle );
                return 1;
        }else{
                return 0;
        }
}
struct clan* inter_clan_fromsql(int clan_id){
        struct clan* clan;
        struct SqlStmt *stmt = NULL;
        char* data;
        size_t len;
        int i;
 
    stmt = SQL->StmtMalloc(inter->sql_handle);
 
        if( clan_id <= 0 )
                return NULL;
 
        clan = (struct clan*)idb_get(clan_db_, clan_id);
 
        if( clan ){
                return clan;
        }
 
        if( SQL_ERROR == SQL->Query(inter->sql_handle, "SELECT `name`, `master`, `mapname`, `max_member` FROM `%s` WHERE `clan_id`='%d'", clan_db_, clan_id) ){
                Sql_ShowDebug(inter->sql_handle);
                return NULL;
        }
 
        if( SQL_SUCCESS != SQL->StmtNextRow(stmt) ){
                return NULL;// Clan does not exists.
        }
 
        CREATE(clan, struct clan, 1);
        memset(clan, 0, sizeof(struct clan));
 
        clan->id = clan_id;
        SQL->GetData(inter->sql_handle,  0, &data, &len); memcpy(clan->name, data, min(len, NAME_LENGTH));
        SQL->GetData(inter->sql_handle,  1, &data, &len); memcpy(clan->master, data, min(len, NAME_LENGTH));
        SQL->GetData(inter->sql_handle,  2, &data, &len); memcpy(clan->map, data, min(len, MAP_NAME_LENGTH_EXT));
        SQL->GetData(inter->sql_handle,  3, &data, NULL); clan->max_member = atoi(data);
 
        clan->connect_member = 0;
 
        SQL->FreeResult( inter->sql_handle );
 
        if( clan->max_member > MAX_CLAN ){
                ShowWarning("Clan %d:%s specifies higher capacity (%d) than MAX_CLAN (%d)\n", clan_id, clan->name, clan->max_member, MAX_CLAN);
                clan->max_member = MAX_CLAN;
        }
 
        if( SQL_ERROR == SQL->Query(inter->sql_handle, "SELECT `opposition`,`alliance_id`,`name` FROM `%s` WHERE `clan_id`='%d'", clan_alliance_db, clan_id) ){
                Sql_ShowDebug(inter->sql_handle);
                aFree(clan);
                return NULL;
        }
 
        for( i = 0; i < MAX_CLANALLIANCE && SQL_SUCCESS == SQL->StmtNextRow(stmt); i++ ){
                struct clan_alliance* a = &clan->alliance[i];
 
                SQL->GetData(inter->sql_handle, 0, &data, NULL); a->opposition = atoi(data);
                SQL->GetData(inter->sql_handle, 1, &data, NULL); a->clan_id = atoi(data);
                SQL->GetData(inter->sql_handle, 2, &data, &len); memcpy(a->name, data, min(len, NAME_LENGTH));
        }
 
        idb_put( clan_db_, clan_id, clan);
 
        if (chr->show_save_log)
                ShowInfo("Clan loaded (%d - %s)\n", clan_id, clan->name);
 
        return clan;
}
 
int mapif_clan_info( int fd ){
        struct DBIterator *iter;
        int offset;
        struct clan* clan;
        int length;
 
        length = 4 + db_size(clan_db_) * sizeof( struct clan );
 
        WFIFOHEAD( fd, length );
        WFIFOW( fd, 0 ) = 0x38A0;
        WFIFOW( fd, 2 ) = length;
 
        offset = 4;
        iter = db_iterator(clan_db_);
        for( clan = (struct clan*)dbi_first(iter); dbi_exists(iter); clan = (struct clan*)dbi_next(iter) ){
                memcpy( WFIFOP( fd, offset ), clan, sizeof( struct clan ) );
                offset += sizeof( struct clan );
        }
        dbi_destroy(iter);
 
        WFIFOSET( fd, length );
 
        return 0;
}
 
static int mapif_parse_clan_request( int fd ){
        mapif_clan_info( fd );
 
        return 0;
}
 
static int mapif_parse_clan_message( int fd ){
        unsigned char buf[500];
        uint16 len;
 
        len = RFIFOW(fd,2);
 
        WBUFW(buf,0) = 0x38A1;
        memcpy( WBUFP(buf,2), RFIFOP(fd,2), len-2 );
 
        mapif->sendallwos( fd, buf, len );
 
        return 0;
}
 
static void mapif_clan_refresh_onlinecount( int fd, struct clan* clan ){
        unsigned char buf[8];
 
        WBUFW(buf,0) = 0x38A2;
        WBUFL(buf,2) = clan->id;
        WBUFW(buf,6) = clan->connect_member;
 
        mapif->sendallwos( fd, buf, 8 );
}
 
static void mapif_parse_clan_member_left( int fd ){
        struct clan* clan = (struct clan*)idb_get(clan_db_, RFIFOL(fd,2) );
 
        if( clan == NULL ){ // Unknown clan
                return;
        }
 
        if( clan->connect_member > 0 ){
                clan->connect_member--;
 
                mapif_clan_refresh_onlinecount( fd, clan );
        }
}
 
static void mapif_parse_clan_member_joined( int fd ){
        struct clan* clan = (struct clan*)idb_get(clan_db_, RFIFOL(fd,2) );
 
        if( clan == NULL ){ // Unknown clan
                return;
        }
 
        clan->connect_member++;
 
        mapif_clan_refresh_onlinecount( fd, clan );
}
 
// Communication from the map server
// - Can analyzed only one by one packet
// Data packet length that you set to inter.c
//- Shouldn't do checking and packet length, RFIFOSKIP is done by the caller
// Must Return
//      1 : ok
//  0 : error
int inter_clan_parse_frommap( int fd ){
        RFIFOHEAD(fd);
        switch(RFIFOW(fd,0)) {
                case 0x30A0:
                        mapif_parse_clan_request( fd );
                        return 1;
                case 0x30A1:
                        mapif_parse_clan_message( fd );
                        return 1;
                case 0x30A2:
                        mapif_parse_clan_member_left( fd );
                        return 1;
                case 0x30A3:
                        mapif_parse_clan_member_joined( fd );
                        return 1;
                default:
                        return 0;
        }
}
 
// Initialize clan sql
int inter_clan_init(void){
        char* data;
    struct SqlStmt *stmt = NULL;
        int* clan_ids;
        int amount;
 
        clan_db_ = idb_alloc(DB_OPT_RELEASE_DATA);
 
 
        if( SQL_ERROR == SQL->Query( inter->sql_handle, "SELECT `clan_id` FROM `%s`", clan_db_ ) ){
                Sql_ShowDebug(inter->sql_handle);
                return 1;
        }
 
        amount = (int)SQL->NumRows( inter->sql_handle );
    stmt = SQL->StmtMalloc(inter->sql_handle);
 
        if( amount > 0 ){
                int i;
 
                CREATE( clan_ids, int, amount );
                i = 0;
 
                while( SQL_SUCCESS == SQL->StmtNextRow(stmt) ){
                        SQL->GetData(inter->sql_handle,  0, &data, NULL);
                        clan_ids[i++] = atoi(data);
                }
 
                SQL->FreeResult( inter->sql_handle );
 
                // If we didnt load a row as expected
                amount = i;
 
                for( i = 0; i < amount; i++ ){
                        inter_clan->fromsql( clan_ids[i] );
                }
 
                aFree(clan_ids);
        }
 
        return 0;
}
 
void inter_clan_sql_final(){
        db_destroy(clan_db_);
}
 
void inter_clan_defaults(void)
{
    inter_clan = &inter_clan_;
 
        inter_clan->sql_init = inter_clan_init;
        inter_clan->fromsql = inter_clan_fromsql;
        inter_clan->sql_final = inter_clan_sql_final;
        inter_clan->parse_frommap = inter_clan_parse_frommap;
}
 
Viewed 649 times, submitted by unknown.