You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
421 lines
15 KiB
421 lines
15 KiB
1 month ago
|
using System;
|
||
|
using System.Linq;
|
||
|
using System.Collections.Generic;
|
||
|
using ProtoCSStruct;
|
||
|
using Sog;
|
||
|
using System.IO;
|
||
|
|
||
|
namespace Operation
|
||
|
{
|
||
|
public static class OperationServerUtils
|
||
|
{
|
||
|
private static long m_lastTickTime = 0;
|
||
|
public static OperationServerData GetOperationServerData()
|
||
|
{
|
||
|
return ServerDataObjMgr.GetDataObj<OperationServerData>(OperationDataObjType.OperationServerData);
|
||
|
}
|
||
|
|
||
|
public static ServerApp GetApp()
|
||
|
{
|
||
|
return GetOperationServerData().m_app;
|
||
|
}
|
||
|
|
||
|
public static ProtoCSStructPacker GetProtoPacker()
|
||
|
{
|
||
|
return ProtoPackerFactory.Instance.GetProtoCSStructPacker();
|
||
|
}
|
||
|
|
||
|
public static uint GetAppID()
|
||
|
{
|
||
|
return GetOperationServerData().m_app.ServerID;
|
||
|
}
|
||
|
|
||
|
public static StructPacketSender GetPacketSender()
|
||
|
{
|
||
|
return GetOperationServerData().m_packetSender;
|
||
|
}
|
||
|
|
||
|
public static OperationMsgHandler GetMsgHandler()
|
||
|
{
|
||
|
return ServiceMgr.GetService<OperationMsgHandler>(OperationServiceType.OperationMsgHandler);
|
||
|
}
|
||
|
|
||
|
//public static SendMailSvc GetSendMailSvc()
|
||
|
//{
|
||
|
// return ServiceMgr.GetService<SendMailSvc>(OperationServiceType.SendMailSvc);
|
||
|
//}
|
||
|
|
||
|
public static void SendToWorld<T>(int iMsgID, ref T structMessage, long iObjectID, uint headserverID, string Realmlist)
|
||
|
where T : struct, IStructMessage<T>
|
||
|
{
|
||
|
//var Realm = OperationServerUtils.GetOperationServerData().m_allRealm;
|
||
|
////不是给全部服务器发
|
||
|
//if (Realmlist != "0" && Realmlist != "")
|
||
|
//{
|
||
|
// var Realmlists = Realmlist.Split(',').Select(s => int.Parse(s)).ToList();
|
||
|
// Realm = Realm.FindAll(f => Realmlists.Exists(e => e == f.realmId));
|
||
|
//}
|
||
|
//var WorldIds = Realm.Select(s => s.WorldId).Distinct().ToArray();
|
||
|
|
||
|
var WorldIds = GetWords(Realmlist);
|
||
|
|
||
|
GetPacketSender().SendToWorldServerBymWorldIds(iMsgID, ref structMessage, iObjectID, WorldIds, headserverID);
|
||
|
}
|
||
|
|
||
|
public static void SendToMail<T>(int iMsgID, ref T structMessage, long iObjectID, uint headserverID, string Realmlist)
|
||
|
where T : struct, IStructMessage<T>
|
||
|
{
|
||
|
var Realm = OperationServerUtils.GetOperationServerData().m_allRealm.Values.ToList();
|
||
|
//不是给全部服务器发
|
||
|
if (Realmlist != "0" && Realmlist != "")
|
||
|
{
|
||
|
var Realmlists = Realmlist.Split(',').Select(s => int.Parse(s)).ToList();
|
||
|
Realm = Realm.FindAll(f => Realmlists.Exists(e => e == f.realmId));
|
||
|
}
|
||
|
var WorldIds = Realm.Select(s => s.WorldId).Distinct().ToArray();
|
||
|
|
||
|
GetPacketSender().SendToMailServerBymWorldIds(iMsgID, ref structMessage, iObjectID, WorldIds, headserverID);
|
||
|
}
|
||
|
|
||
|
//向 AccountDBServer发消息
|
||
|
public static void SendToDBServer<T>(int iMsgID, ref T structMessage, long iObjectID, uint headserverID)
|
||
|
where T : struct, IStructMessage<T>
|
||
|
{
|
||
|
GetPacketSender().SendToServerByID(DBServerSelect.GetDBServerID(),
|
||
|
iMsgID, ref structMessage, iObjectID, headserverID);
|
||
|
}
|
||
|
|
||
|
|
||
|
public static Realmlist GetRealmlistSvc()
|
||
|
{
|
||
|
return ServiceMgr.GetService<Realmlist>(OperationServiceType.RealmlistSvc);
|
||
|
}
|
||
|
|
||
|
public static SendNoticeSvc GetSendNoticeSvc()
|
||
|
{
|
||
|
return ServiceMgr.GetService<SendNoticeSvc>(OperationServiceType.SendNoticeSvc);
|
||
|
}
|
||
|
|
||
|
public static ExchangeCodeSvc GetExchangeSvc()
|
||
|
{
|
||
|
return ServiceMgr.GetService<ExchangeCodeSvc>(OperationServiceType.ExchangeCodeSvc);
|
||
|
}
|
||
|
|
||
|
public static SendMailSvc GetSendMailSvc()
|
||
|
{
|
||
|
return ServiceMgr.GetService<SendMailSvc>(OperationServiceType.SendMailSvc);
|
||
|
}
|
||
|
|
||
|
|
||
|
public static MailDBData GetMailDBData()
|
||
|
{
|
||
|
return ServerDataObjMgr.GetDataObj<MailDBData>(OperationDataObjType.MailDBDataType);
|
||
|
}
|
||
|
|
||
|
// 时间戳转为C#格式时间
|
||
|
public static DateTime StampToDateTime(long timeStamp)
|
||
|
{
|
||
|
DateTime dateTimeStart = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).ToLocalTime();
|
||
|
long lTime = long.Parse(timeStamp + "0000000");
|
||
|
TimeSpan toNow = new TimeSpan(lTime);
|
||
|
|
||
|
return dateTimeStart.Add(toNow);
|
||
|
}
|
||
|
|
||
|
//时长转秒数
|
||
|
public static void AddTimes(ref long date, int day, string time)
|
||
|
{
|
||
|
var dateobj = StampToDateTime(date);
|
||
|
AddTimes(ref dateobj, day, time);
|
||
|
date = GetTimeStamp(dateobj);
|
||
|
}
|
||
|
|
||
|
public static void AddTimes(ref DateTime date, int day, string time)
|
||
|
{
|
||
|
int hour; int minute; int second;
|
||
|
var times = time.Split(':');
|
||
|
|
||
|
if (day != 0)
|
||
|
{
|
||
|
date = date.AddDays(day);
|
||
|
}
|
||
|
if (times.Length == 3)
|
||
|
{
|
||
|
hour = int.Parse(times[0]);
|
||
|
if (hour != 0)
|
||
|
{
|
||
|
date = date.AddHours(hour);
|
||
|
}
|
||
|
minute = int.Parse(times[1]);
|
||
|
if (minute != 0)
|
||
|
{
|
||
|
date = date.AddMinutes(minute);
|
||
|
}
|
||
|
second = int.Parse(times[2]);
|
||
|
if (second != 0)
|
||
|
{
|
||
|
date = date.AddSeconds(second);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// DateTime时间格式转换为Unix时间戳格式
|
||
|
public static int GetTimeStamp(System.DateTime time)
|
||
|
{
|
||
|
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
|
||
|
return (int)(time - startTime).TotalSeconds;
|
||
|
}
|
||
|
|
||
|
public static long GetTimeSecond()
|
||
|
{
|
||
|
return GetOperationServerData().m_app.Time.GetTimeSecond();
|
||
|
}
|
||
|
|
||
|
public static HttpApiRootHandler GetHttpApiRootHandler()
|
||
|
{
|
||
|
return HttpApiRootHandler.Instance;
|
||
|
}
|
||
|
|
||
|
//获取服务器配置方法
|
||
|
public static OperationServerConfig GetServerConfig()
|
||
|
{
|
||
|
return (OperationServerConfig)ServerConfigMgr.Instance.m_serverConfig;
|
||
|
}
|
||
|
|
||
|
public static OperationServerConfig ParseDBConfig(ServerApp app, out int dbtype, out string dbname, out string dbip)
|
||
|
{
|
||
|
OperationServerConfig serverConfig = GetServerConfig();
|
||
|
if(serverConfig==null)
|
||
|
TraceLog.Error("OperationServerUtils.ParseDBConfig serverConfig null");
|
||
|
// 优先使用参数里传入的db配置, 方便每个人独立修改
|
||
|
string dbtypestr = app.GetCluster().GetAppParamByKey("opdbtype");
|
||
|
if (string.IsNullOrEmpty(dbtypestr) || !int.TryParse(dbtypestr, out dbtype))
|
||
|
{
|
||
|
dbtype = serverConfig.dbtype;
|
||
|
}
|
||
|
|
||
|
dbip = app.GetCluster().GetAppParamByKey("opdbip");
|
||
|
if (string.IsNullOrEmpty(dbip))
|
||
|
{
|
||
|
dbip = serverConfig.dbip;
|
||
|
}
|
||
|
|
||
|
if (dbtype == 1) // mongo db 不再使用
|
||
|
{
|
||
|
TraceLog.Error("OperationServerUtils.ParseDBConfig invalid dbtype 1");
|
||
|
}
|
||
|
|
||
|
// dbname不让单独修改, 大家统一简单些
|
||
|
dbname = serverConfig.dbname;
|
||
|
return serverConfig;
|
||
|
}
|
||
|
|
||
|
public static DBOperator GetDbOp()
|
||
|
{
|
||
|
var serverConfig = ParseDBConfig(GetApp(), out int dbtype, out string dbname, out string dbip);
|
||
|
|
||
|
if (dbtype == 0)
|
||
|
{
|
||
|
return new MySqlDBOperator(dbname, dbip
|
||
|
, serverConfig.dbuser, serverConfig.dbpassword);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
TraceLog.Error("OperationServerUtils.GetDbOp invalid dbtype {0}", dbtype);
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static int GetDbType()
|
||
|
{
|
||
|
var serverConfig = ParseDBConfig(GetApp(), out int dbtype, out string dbname, out string dbip);
|
||
|
|
||
|
return dbtype;
|
||
|
}
|
||
|
|
||
|
public static string GetNewPicUrl(string oldPic)
|
||
|
{
|
||
|
var newUrl = OperationServerUtils.GetServerConfig().imghttp;
|
||
|
|
||
|
if (!String.IsNullOrEmpty(oldPic))
|
||
|
{
|
||
|
if(oldPic.Contains(newUrl))
|
||
|
{
|
||
|
return oldPic;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
return newUrl + oldPic.Split("/", StringSplitOptions.RemoveEmptyEntries).Last();
|
||
|
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
return "";
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static int[] GetWords(string realmStr)
|
||
|
{
|
||
|
List<int> ret = new List<int>();
|
||
|
|
||
|
var Realm = OperationServerUtils.GetOperationServerData().m_allRealm.Values.ToList();
|
||
|
|
||
|
if(realmStr.Contains("#"))
|
||
|
{
|
||
|
List<int> WorldIds = new List<int>();
|
||
|
foreach (var item in realmStr.Split("#"))
|
||
|
{
|
||
|
if (string.IsNullOrWhiteSpace(item))
|
||
|
{
|
||
|
continue;
|
||
|
}
|
||
|
if (item.Contains("-"))
|
||
|
{
|
||
|
var itemList = item.Split("-",StringSplitOptions.RemoveEmptyEntries);
|
||
|
if(itemList.Count() >= 2)
|
||
|
{
|
||
|
var nowRealm = Realm.FindAll(f => RealmRule(f.realmId) > RealmRule(itemList[0].Toint32()) && RealmRule(f.realmId) < RealmRule(itemList[1].Toint32()));
|
||
|
WorldIds.AddRange(nowRealm.Select(s => s.WorldId).Distinct().ToArray());
|
||
|
}
|
||
|
}
|
||
|
if (item.Contains(","))
|
||
|
{
|
||
|
List<int> realmList = new List<int>();
|
||
|
foreach (var id in item.Split(","))
|
||
|
{
|
||
|
if (string.IsNullOrWhiteSpace(id))
|
||
|
{
|
||
|
continue;
|
||
|
}
|
||
|
int realmId = id.Toint32(0);
|
||
|
if (realmId > 0)
|
||
|
{
|
||
|
realmList.Add(realmId);
|
||
|
}
|
||
|
}
|
||
|
var nowRealm = Realm.FindAll(f => realmList.Exists(e => RealmRule(e) == RealmRule(f.realmId)));
|
||
|
WorldIds.AddRange(nowRealm.Select(s => s.WorldId).Distinct().ToList());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return WorldIds.ToArray();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
var nowRealm = new List<RealmBriefInfo>();
|
||
|
if (realmStr != "0" && realmStr != "")
|
||
|
{
|
||
|
var Realmlists = realmStr.Split(',').Select(s => int.Parse(s)).ToList();
|
||
|
nowRealm = Realm.FindAll(f => Realmlists.Exists(e => e == f.realmId));
|
||
|
}
|
||
|
|
||
|
var WorldIds = Realm.Select(s => s.WorldId).Distinct().ToArray();
|
||
|
return WorldIds;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private static int RealmRule(int realmId)
|
||
|
{
|
||
|
string realmStr = realmId.ToString();
|
||
|
if (realmStr.Length < 6)
|
||
|
return realmId;
|
||
|
string area = realmStr.Substring(0, 1);
|
||
|
string reaml = realmStr.Substring(3);
|
||
|
Int32.TryParse(area + reaml, out int result);
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
public static string GetAccount(string token)
|
||
|
{
|
||
|
if (token == null)
|
||
|
{
|
||
|
return null;
|
||
|
}
|
||
|
var accountList = OperationServerUtils.GetOperationServerData().m_accountList;//服务器保存了所有token列表
|
||
|
if (!accountList.ContainsKey(token))
|
||
|
{
|
||
|
return null;
|
||
|
}
|
||
|
return accountList[token];
|
||
|
}
|
||
|
|
||
|
public static void CleanTokenList(long nowMs)
|
||
|
{
|
||
|
//60秒一次
|
||
|
if (nowMs - m_lastTickTime < 60000)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
m_lastTickTime = nowMs;
|
||
|
|
||
|
var tokenList = OperationServerUtils.GetOperationServerData().m_tokenList;
|
||
|
var accountList = OperationServerUtils.GetOperationServerData().m_accountList;
|
||
|
|
||
|
if (tokenList != null && tokenList.Count > 0)
|
||
|
{
|
||
|
foreach (var item in tokenList)
|
||
|
{
|
||
|
if (item.Value + 60 * 60 < nowMs/1000)
|
||
|
{
|
||
|
tokenList.Remove(item.Key);
|
||
|
if (accountList.ContainsKey(item.Key))
|
||
|
{
|
||
|
accountList.Remove(item.Key);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Base64字符串转文件并保存
|
||
|
/// </summary>
|
||
|
/// <param name="base64String">base64字符串</param>
|
||
|
/// <param name="fileName">保存的文件名</param>
|
||
|
/// <returns>是否转换并保存成功</returns>
|
||
|
public static string Base64StringToFile(string base64String, string noticeid)
|
||
|
{
|
||
|
//服务器磁盘图片保存目录
|
||
|
string imgpath = OperationServerUtils.GetServerConfig().imgfliepath;
|
||
|
string strDate = DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + noticeid + ".png";
|
||
|
string fileFullPath = imgpath + strDate;//文件保存路径
|
||
|
|
||
|
if (!Directory.Exists(imgpath))
|
||
|
{
|
||
|
Directory.CreateDirectory(imgpath);
|
||
|
}
|
||
|
|
||
|
string strbase64 = base64String.Trim().Substring(base64String.IndexOf(",") + 1); //将‘,’以前的多余字符串删除
|
||
|
MemoryStream stream = new MemoryStream(Convert.FromBase64String(strbase64));
|
||
|
FileStream fs = new FileStream(fileFullPath, FileMode.OpenOrCreate, FileAccess.Write);
|
||
|
byte[] b = stream.ToArray();
|
||
|
fs.Write(b, 0, b.Length);
|
||
|
fs.Close();
|
||
|
|
||
|
return strDate;
|
||
|
}
|
||
|
|
||
|
// -1 时间未到 0 有效期内 1 已过期
|
||
|
public static int CheckTime(DateTime startTime, DateTime endTime)
|
||
|
{
|
||
|
var TimeSecond = OperationServerUtils.GetTimeSecond();
|
||
|
var startTimetamp = OperationServerUtils.GetTimeStamp(startTime);
|
||
|
var endTimetamp = OperationServerUtils.GetTimeStamp(endTime);
|
||
|
|
||
|
if (TimeSecond < startTimetamp)
|
||
|
{
|
||
|
return -1;
|
||
|
}
|
||
|
else if (TimeSecond > endTimetamp)
|
||
|
{
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|