|
借用hnroc和结合nwb13的优化代码,
我也作了自己的想法
因为网吧的服务器多数有60%是内部玩家,既然是内网就不必要再去查询Mysql数据库以免浪费资源
毕竟内网玩家老喜欢reconnect
代码如下:
- // last edit by YanOnline at 2005.1.24
- #include <amxmod>
- #include <amxmisc>
- #include <mysql>
- #define DEBUG
- new g_country[32][32],g_city[32][32]
- new g_buffer[512]
- new sBuffer[2048]
- new bool:g_sql_connected
- new g_dbc
- public plugin_init() {
- register_plugin("Look Ip and City","1.0","hnroc")
- register_clcmd("say /city", "cmdcity", 0, "- display city (MOTD)")
- register_clcmd("say city", "cmdcity", 0, "- display city (MOTD)")
- register_cvar("amx_mysql_host","127.0.0.1")
- register_cvar("amx_mysql_user","root")
- register_cvar("amx_mysql_pass","yan")
- register_cvar("amx_mysql_db","amx")
- register_cvar("amx_hideop","1")
- register_srvcmd("amx_load_lic","sql_init")
- new filename[32]
- get_basedir( filename , 31 )
- server_cmd("exec %s/config/mysql.cfg" , filename)
- server_cmd("amx_load_lic")
- }
- public sql_init(){
- new host[64],user[32],pass[32],dbname[32],error[32]
- get_cvar_string("amx_mysql_host",host,63)
- get_cvar_string("amx_mysql_user",user,31)
- get_cvar_string("amx_mysql_pass",pass,31)
- get_cvar_string("amx_mysql_db",dbname,31)
- #if defined DEBUG
- server_print("[AMX]Sql setting:host[%s] user[%s] password[%s] database[%s]",host,user,pass,dbname)
- #endif
- new dbc = mysql_connect(host,user,pass,dbname,error,32)
- if (dbc < 1 ){
- server_print("[AMX] SQL Connection Failed! Try once")
- dbc = mysql_connect(host,user,pass,dbname,error,32)
- if (dbc < 1){
- server_print("[AMX] SQL Connection Failed! Check your sql server and its settings")
- g_sql_connected = false
- return PLUGIN_HANDLED
- }else{
- g_sql_connected = true
- g_dbc = dbc
- }
- }else{
- g_sql_connected = true
- g_dbc = dbc
- }
- #if defined DEBUG
- server_print("[AMX] SQL Connect OK!")
- #endif
- return PLUGIN_HANDLED
- }
- public client_connect(id){
- if (!g_sql_connected)
- return PLUGIN_CONTINUE
- new query[512],error[128]
- new ipb1[12],ipb2[12],ipb3[12],ipb4[12]
- new userip[16],uip[16]
- new ipp[32],pos
- get_user_ip(id,userip,15,1)
- new ips[7]
- copy(ips,7,userip)
- if (equali(ips,"192.168")){
- set_user_flags(id,read_flags("b"))
- client_print(0,print_chat,"*内网优先玩家: %s",userip)
- copy(g_country[id],31,"广州新力量网吧")
- copy(g_city[id],31," 内部玩家")
- }
- else
- {
- copy(uip,16,userip)
- while(replace(uip,16,"."," ")){}
- parse(uip,ipb1,12,ipb2,12,ipb3,12,ipb4,12)
- pos += format(ipp[pos],31-pos,"%s",ipb1)
- if(str_to_num(ipb2)>=100)
- pos += format(ipp[pos],31-pos,"%s",ipb2)
- else if(str_to_num(ipb2)>=10)
- pos += format(ipp[pos],31-pos,"0%s",ipb2)
- else if(str_to_num(ipb2)>=1)
- pos += format(ipp[pos],31-pos,"00%s",ipb2)
- if(str_to_num(ipb3)>=100)
- pos += format(ipp[pos],31-pos,"%s",ipb3)
- else if(str_to_num(ipb3)>=10)
- pos += format(ipp[pos],31-pos,"0%s",ipb3)
- else if(str_to_num(ipb3)>=1)
- pos += format(ipp[pos],31-pos,"00%s",ipb3)
- if(str_to_num(ipb4)>=100)
- pos += format(ipp[pos],31-pos,"%s",ipb4)
- else if(str_to_num(ipb4)>=10)
- pos += format(ipp[pos],31-pos,"0%s",ipb4)
- else if(str_to_num(ipb4)>=1)
- pos += format(ipp[pos],31-pos,"00%s",ipb4)
- client_print(0,print_chat,"*IP引索: %s ",ipp)
- format(query,511,"SELECT * FROM mytable WHERE startip<=%s AND endip>=%s",ipp,ipp)
- if (mysql_query(g_dbc,query) < 1){
- mysql_error(g_dbc,error,127)
- server_print("[AMX] MySQL error: can't load address: '%s'",error)
- return PLUGIN_HANDLED
- }
- while(mysql_nextrow(g_dbc) > 0 ){
- mysql_getfield(g_dbc, 3, g_country[id], 31)
- mysql_getfield(g_dbc, 4, g_city[id], 31)
- }
- if (strlen(g_country[id]) == 0)
- copy(g_country[id],31,"未知地址")
- if (strlen(g_city[id]) == 0)
- copy(g_city[id],31,"未知地址")
- }
- return PLUGIN_CONTINUE
- }
- public client_putinserver(id){
- if (!g_sql_connected)
- return PLUGIN_CONTINUE
- new ids[1]
- ids[0]=id
- set_task(8.0,"show_msg",0,ids,1)
- return PLUGIN_CONTINUE
- }
- public show_msg(ids[]){
- new name[32],ip[15],id,pos
- id=ids[0]
- if (get_cvar_num("amx_hideop")){
- if (get_user_flags(id)&ADMIN_IMMUNITY) {
- get_user_name(id,name,31)
- pos += format(g_buffer[pos],511-pos," %s ",name)
- pos += format(g_buffer[pos],511-pos,"进入服务器,他来自:^n")
- pos += format(g_buffer[pos],511-pos," 身份OP,地址隐藏")
- // pos += format(g_buffer[pos],511-pos," 祝您玩的愉快!")
- }
- else{
- get_user_name(id,name,31)
- get_user_ip(id,ip,15,1)
- pos += format(g_buffer[pos],511-pos,"%s 终于来啦! ^n",name)
- pos += format(g_buffer[pos],511-pos,"他的IP:%s ^n",ip)
- pos += format(g_buffer[pos],511-pos,"来自:%s / %s",g_country[id],g_city[id])
- // pos += format(g_buffer[pos],511-pos," 祝您玩的愉快,请不要作弊!")
- }
- }
- else{
- get_user_name(id,name,31)
- get_user_ip(id,ip,15,1)
- pos += format(g_buffer[pos],511-pos,"%s 终于来啦! ^n",name)
- pos += format(g_buffer[pos],511-pos,"他的IP:%s ^n",ip)
- pos += format(g_buffer[pos],511-pos,"来自:%s / %s",g_country[id],g_city[id])
- //pos += format(g_buffer[pos],511-pos," 祝您玩的愉快,请不要作弊!")
- }
- set_hudmessage(0, 255, 0, 0.05, 0.50, 2, 0.01, 8.0, 0.01, 3.0, 4)
- show_hudmessage(0, g_buffer)
- return PLUGIN_CONTINUE
- }
- public cmdcity(id){
- new players[32],inum,state[4]
- new iLen = 0
- get_players(players,inum)
- iLen = format( sBuffer,2047, "<meta http-equiv=Content-Type content=charset=utf-8><style>body{color:#FFCC99;background-color:black;margin-top:0}.header{background-color:#9C0000;}.one{background-color:#310000;}.two{background-color:#630000;}td{font-size:13px}</style><table><tr class=header>")
- if (!g_sql_connected)
- iLen += format(g_buffer[iLen],511-iLen," 连接sql服务器失败, 请检查设置或者sql服务器")
- else{
- iLen += format( sBuffer[iLen],2047-iLen,"%2s %15s %22s^n","<td>玩家ID","<td> 玩家IP","<td>所在城市</tr>")
- for( new i = 0; i < inum; i++ ) {
- if (equal(state,"one")) copy(state,3,"two")
- else copy(state,3,"one")
- new ip[32],name[32]
- if(get_cvar_num("amx_hideop")){
- if (get_user_flags(players[i])&ADMIN_IMMUNITY) {
- get_user_name(players[i],name,31)
- iLen += format(sBuffer[iLen],2047-iLen,"<tr class=%s><td>%s<td>%13s<td>%21s%s^n</tr>",state,name,"*****","*****","*****")
- }
- else{
- get_user_name(players[i],name,31)
- get_user_ip(players[i],ip,31,1)
- iLen += format(sBuffer[iLen],2047-iLen,"<tr class=%s><td>%1s<td>%s<td>%s%s</tr>",state,name,ip,g_country[players[i]],g_city[players[i]])
- }
- }
- else{
- get_user_name(players[i],name,31)
- get_user_ip(players[i],ip,31,1)
- iLen += format(sBuffer[iLen],2047-iLen,"<tr class=%s><td>%1s<td>%s<td>%s%s</tr>",state,name,ip,g_country[players[i]],g_city[players[i]])
- }
- }
- }
- show_motd(id, sBuffer, "查看玩家来源:" )
- return PLUGIN_HANDLED
- }
- public plugin_end(){
- if (!g_sql_connected)
- return PLUGIN_CONTINUE
- mysql_close(g_dbc)
- server_print("[AMX] SQL Connection closed!")
- return PLUGIN_CONTINUE
- }
复制代码
你们认为怎样呢?
改源就是进步!! |
|