common.h 6.71 KB
#pragma once

#include <lib_include.h>
#include <lib_xmlparser.h>
#include <lib_util.h>
#include <lib_timer.h>
#include <lib_time.h>
#include <lib_random.h>
#include <lib_platform.h>
#include <lib_lock.h>
#include <lib_log.h>
#include <lib_file.h>
#include <lib_net/lib_tcp_client.h>
#include <lib_err_code.h>
#include <lib_proto/lib_proto.h>
#include <lib_proto/lib_msgbuf.h>

#include <bench_conf.h>
#include <service_if.h>
#include <service_mgr_if.h>

#include <google/protobuf/message.h>

#ifndef max
#define max(a,b)    (((a) > (b)) ? (a) : (b))
#endif
#ifndef min
#define min(a,b)    (((a) < (b)) ? (a) : (b))
#endif

// 检查配置文件 	
//#define __CHECK_CFG__

typedef uint64_t USER_ID;
typedef uint32_t USER_HEAD_ID;
typedef uint32_t PET_ID;
typedef uint32_t PET_ATTR;
typedef uint32_t PET_KEY;
typedef uint32_t EFF_ID;
typedef uint32_t SKILL_ID;
typedef uint32_t SKILL_TYPE;
typedef uint32_t BUF_ID;
typedef uint32_t PET_STAR;
typedef uint32_t PET_CNT;
typedef uint32_t PET_LV;
typedef uint32_t USER_HEAD_FRAME_ID;
typedef uint32_t CMD_ID;
typedef uint32_t PROTO_LEN;

typedef uint64_t MAIL_IDX;
typedef uint32_t MAIL_ID;
typedef uint32_t GAME_ID;
typedef uint32_t GAME_LEVEL;
typedef uint32_t GAME_SCORE;
typedef uint32_t TASK_ID;
typedef uint32_t STEP_ID;

typedef uint32_t EVENT_TYPE;
typedef uint64_t EVENT_ID;
typedef uint64_t EVENT_DATA;
typedef uint32_t EVENT_TIME;
typedef std::string EVENT_STR_DATA;
typedef std::string EVENT_BIN_DATA;

typedef uint32_t ITEM_ID;
typedef uint32_t ITEM_CNT;
typedef uint32_t ROOM_ID;

static const ITEM_CNT ITEM_CNT_MAX = UINT32_MAX;


//client
static const uint32_t MAXMESSAGE_LEN = 1048576;//1024*1024 = 1M	
#ifdef WIN32
	static const uint32_t SERVER_PACK_DEFAULT_SIZE = 8388608;//1024*1024*8 = 8Mdb	
#else
	static const uint32_t SERVER_PACK_DEFAULT_SIZE = 83886080;//1024*1024*80 = 80Mdb	
#endif

static const uint32_t USER_EVENT_STR_DATA_LEN_MAX = 20480;
static const uint32_t USER_EVENT_BIN_DATA_LEN_MAX = 65535;
static const uint32_t NICK_LEN = 32;

static const uint32_t ACCOUNT_MAX_LEN = 64;
static const uint32_t EXCHANGE_CODE_MAX_LEN = 32;

static const uint32_t ORDER_ID_MAX_LEN = 32;
static const uint32_t AMOUNT_MAX_LEN = 32;

//密码最大长度	
static const uint32_t PS_MAX_LEN = 64;

static const uint32_t CHAT_MSG_LEN_MAX = 256;

enum E_ITEM_ID
{
	E_ITEM_ID_SILVER		= 20002,//银币	
	E_ITEM_ID_GOLD			= 20018,//金币	
	E_ITEM_ID_USER_EXP			= 20116,//城主经验Id 20116	
};


//自然时间获得的粮草总数		
static const uint32_t FORAGE_TIME_MAX = 120;
//总的粮草总数		
static const uint32_t FORAGE_ALL_MAX = 600; 
//每多少秒增加一次体力		
static const uint32_t FORAGE_ADD_SEC = 360; 
	
static const uint32_t BTL_CMD_BEGIN = 0x00010801;
static const uint32_t BTL_CMD_END = 0x00010FFF;

enum E_USER_LOGIN_STATUS{
	E_USER_LOGIN_STATUS_CONNECTED = 1,//已连接成功	
	E_USER_LOGIN_STATUS_LOGINING = 2,//验证登录中	
//	E_USER_LOGIN_STATUS_APPROVED = 3,//通过身份校验	
	E_USER_LOGIN_STATUS_CREATE_ROLE = 4,//创建角色	
	E_USER_LOGIN_STATUS_COMPLETE = 5,//登录完成	
};

//玩家UID 最小起点,小于 MIN_UID 的都是AI	
static const USER_ID MIN_UID = 10142857;
static const USER_ID MAX_UID = 73709551615;

//OLD
//     1 0000 00000000000
//          1 00000000000
//1 8446 7440 73709551615
//static const USER_ID OLD_USER_ID_PLATFORM = 1000000000000000;
//NEW
//       1 00 00000000000
//          1 00000000000
//1 844674 40 73709551615
static const USER_ID NEW_USER_ID_PLATFORM = 10000000000000;

inline uint32_t g_gen_platform(USER_ID uid){
// 	if (0 != (uint32_t)(uid/OLD_USER_ID_PLATFORM)){
// 		return (uint32_t)(uid/OLD_USER_ID_PLATFORM);
// 	}

	return (uint32_t)(uid/NEW_USER_ID_PLATFORM);
}

#define GEN_UID(__platform__, __server_id__, __uid__)  \
	((USER_ID)(__platform__)*NEW_USER_ID_PLATFORM \
	+ (USER_ID)(__server_id__) * 100000000000 \
	+ (USER_ID)(__uid__))


//2000100010142859,10142862 =>2000100010142862
#define GEN_MAIL_RECV_UID(__send_uid__, __recv_uid__)  \
	((USER_ID)(__send_uid__)/100000000*100000000 \
	+ (USER_ID)(__recv_uid__) % 100000000)

inline bool g_is_robot(USER_ID uid){
	return uid < MIN_UID;
}

enum E_MAIL
{
	MAIL_BODY_MAX_LEN       = 1024,
	MAIL_ATTACHMENT_MAX_LEN = 20480,
	MAIL_ARG_MAX_LEN		= 1024,
	MAIL_BIN_DATA_MAX_LEN = 65535,
};

inline uint32_t get_valid_len(const char * str, uint32_t max_len){
    if( strlen(str) > max_len ){
        return max_len;
    } else {
        return (uint32_t)strlen(str);
    }
}

struct uint_pair {
	uint_pair() : key(0), val(0) {}
	uint_pair(uint32_t p_key, uint32_t p_val) : key(p_key), val(p_val) {}
	uint32_t key;
	uint32_t val;
	void init(){
		this->key = 0;
		this->val = 0;
	}
};
//字符串分割成uint32,只能是uint类型字符串 格式必须是 1,2;3,4;5,6 可以接收空字符串	
inline void str_split_uint(const std::string& source, std::vector<uint_pair>& vec_dest)
{
	uint_pair u_pa;
	char ctr_key[11] = {0};

	int i = 0;
	for(std::string::const_iterator it = source.begin(); it != source.end(); ++it) {
		if (*it != ';') {
			if (*it != ',') {
				ctr_key[i++] = *it;
			} else {
				u_pa.key = atoi(ctr_key);
				::memset(ctr_key, 0, 11);
				i = 0;
			}
		} else {
			u_pa.val = atoi(ctr_key);
			vec_dest.push_back(u_pa);
			::memset(ctr_key, 0, 11);
			i = 0;
		}
	}

	if (ctr_key[0] != 0) {
		u_pa.val = atoi(ctr_key);
		vec_dest.push_back(u_pa);
	}
}
//每个vector中间用';'隔开,结尾不含; 如: 1,2;3,4;5,6	
inline void uint_concat_str(const std::vector<uint_pair>& vec_source, std::string& str_dest)
{
	if (vec_source.empty()) {
		return;
	}
	for (auto it = vec_source.begin(); it != vec_source.end(); ++it) {
		char ctr_dest[24] = {0};
		::sprintf(ctr_dest, "%u,%u;", it->key, it->val);
		str_dest += ctr_dest;
	}
	str_dest.pop_back();
}

// @brief:	计算今天是周几(周一:1 ~ 周日:7)	
inline uint32_t time_week(time_t now_time)
{
#ifdef WIN32
	struct tm* ptm = ::localtime(&now_time);
	return 0 == ptm->tm_wday ? 7 : ptm->tm_wday;
#else
	struct tm t_m;
	::localtime_r(&now_time, &t_m);
	return 0 == t_m.tm_wday ? 7 : t_m.tm_wday;
#endif
}

#define DEF_MSG_HANDLE_FUN(__HANDLE_CLASS__, __NAME__) \
	class __HANDLE_CLASS__;\
	typedef int (__HANDLE_CLASS__::* __NAME__##_MSG_HANDLE_FUN)( __NAME__##_MSG_HANDLE_FUN_PAR);\
	struct  MSG_HANDLE_##__NAME__##_T{\
		google::protobuf::Message* prototype;\
		__NAME__##_MSG_HANDLE_FUN func;\
		bool is_callback;\
		MSG_HANDLE_##__NAME__##_T(){\
			this->prototype = NULL;\
			this->func = NULL;\
			this->is_callback = true;\
		}\
		MSG_HANDLE_##__NAME__##_T( google::protobuf::Message* proto, __NAME__##_MSG_HANDLE_FUN fun, bool is_callback = true ){\
			this->prototype = proto;\
			this->func = fun;\
			this->is_callback = is_callback;\
		}\
	};\
	typedef std::map<CMD_ID, MSG_HANDLE_##__NAME__##_T> __NAME__##_MSG_MAP_T;

//#define NEW_RECONNECT

//#define TEST_FIRST_BLOOD //todo