177 lines
5.0 KiB
C#
177 lines
5.0 KiB
C#
using System;
|
|
|
|
|
|
using StackExchange.Redis;
|
|
using Newtonsoft.Json;
|
|
using Amazon.DynamoDBv2.Model;
|
|
using Amazon.DynamoDBv2.Model.Internal.MarshallTransformations;
|
|
using Nettention.Proud;
|
|
|
|
|
|
using ServerCore;
|
|
using ServerBase;
|
|
|
|
|
|
namespace ServerCommon;
|
|
|
|
|
|
// 채팅서버에서 접속한 캐릭터 정보 얻을 떄 사용
|
|
// 온라인 캐릭터를 레디스에 저장해둠
|
|
// 현재는 채팅서버에서 사용할 정보인 캐릭터 이름만 저장
|
|
// 230718 친구신청관련 정보 추가(위치 변경 가능)
|
|
public class CharacterInfo
|
|
{
|
|
public string Name = string.Empty;
|
|
}
|
|
|
|
|
|
/*public struct FriendRequest
|
|
{
|
|
[JsonProperty("TargetNickName")]
|
|
public string TargetNickName { get; set; } = string.Empty;
|
|
[JsonProperty("TargetGuid")]
|
|
public string TargetGuid { get; set; } = string.Empty;
|
|
[JsonProperty("IsNew")]
|
|
public int IsNew { get; set; } = 1;
|
|
[JsonProperty("CreateTime")]
|
|
public long CreateTime { get; set; } = 0L;
|
|
|
|
public FriendRequest(string targetNickName, string targetGuid, int isNew, long createTime)
|
|
{
|
|
TargetNickName = targetNickName;
|
|
TargetGuid = targetGuid;
|
|
IsNew = isNew;
|
|
CreateTime = createTime;
|
|
}
|
|
}*/
|
|
|
|
public enum FriendRequetActionType
|
|
{
|
|
SendRequest,
|
|
DeleteSendedRequest,
|
|
CancelSendedRequest,
|
|
DeleteReceivedRequest,
|
|
AcceptRequest,
|
|
RefuseRequest,
|
|
ConfirmNewFriend,
|
|
ConfirmNewReceivedFriendRequest,
|
|
FolderNameChange,
|
|
FriendMoveFolder,
|
|
DeleteFriend,
|
|
|
|
|
|
}
|
|
|
|
public class CharacterStorage
|
|
{
|
|
//ConnectionMultiplexer _connection = default!;
|
|
IDatabase _database = default!;
|
|
|
|
string _keyPrefix = string.Empty;
|
|
string _friendReceivedInviteMyHomePrefix = string.Empty;
|
|
public CharacterStorage()
|
|
{
|
|
|
|
}
|
|
|
|
public void Init(IDatabase redisDB, string keyPrefix)
|
|
{
|
|
//_connection = await ConnectionMultiplexer.ConnectAsync(configuration);
|
|
//_database = _connection.GetDatabase();
|
|
_database = redisDB;
|
|
|
|
_keyPrefix = keyPrefix;
|
|
if (!_keyPrefix.EndsWith(":"))
|
|
_keyPrefix += ":";
|
|
|
|
_keyPrefix += "character:";
|
|
|
|
_friendReceivedInviteMyHomePrefix = "friendreceivedinvite:";
|
|
|
|
}
|
|
|
|
string KeyWithPrefix(string key) => _keyPrefix + key;
|
|
|
|
string FriendReceivedInviteMyHomeKeyWithPrefix(string suffix) => _friendReceivedInviteMyHomePrefix + suffix;
|
|
|
|
|
|
public async Task<bool> Enter(string accountGuid, string name)
|
|
{
|
|
string key = KeyWithPrefix(accountGuid);
|
|
|
|
var value = await _database.StringGetAsync(key, CommandFlags.PreferReplica);
|
|
if (value.HasValue)
|
|
return false;
|
|
|
|
CharacterInfo charInfo = new CharacterInfo();
|
|
charInfo.Name = name;
|
|
|
|
return await _database.StringSetAsync(key, JsonConvert.SerializeObject(charInfo));
|
|
}
|
|
|
|
public async Task Leave(string accountGuid)
|
|
{
|
|
string key = KeyWithPrefix(accountGuid);
|
|
await _database.KeyDeleteAsync(key);
|
|
}
|
|
|
|
public async Task<CharacterInfo?> GetCharacterInfo(string accountGuid)
|
|
{
|
|
string key = KeyWithPrefix(accountGuid);
|
|
var value = await _database.StringGetAsync(key, CommandFlags.PreferReplica);
|
|
if (value.HasValue == false)
|
|
return null;
|
|
|
|
return JsonConvert.DeserializeObject<CharacterInfo>(value.ToString());
|
|
}
|
|
|
|
|
|
public async Task<ServerErrorCode> AddReceivedInviteInfo(string suffix, string inviteInfo)
|
|
{
|
|
string key = FriendReceivedInviteMyHomeKeyWithPrefix(suffix);
|
|
|
|
bool ret = await _database.SortedSetAddAsync(key, inviteInfo, DateTimeOffset.Now.ToUnixTimeMilliseconds());
|
|
if (!ret)
|
|
{
|
|
Log.getLogger().warn($"AddReceivedInviteInfo can not insert suffix : {suffix}, key : {key}");
|
|
return (ServerErrorCode.DbUpdateFailed);
|
|
}
|
|
await _database.KeyExpireAsync(key, TimeSpan.FromMilliseconds(MetaHelper.GameConfigMeta.FriendIntiveCoolTime * 1000));
|
|
|
|
return ServerErrorCode.Success;
|
|
}
|
|
|
|
public async Task UpdateReceivedInviteInfo(string suffix, string originInviteInfo, string newInviteInfo)
|
|
{
|
|
string key = FriendReceivedInviteMyHomeKeyWithPrefix(suffix);
|
|
await _database.SortedSetRemoveAsync(key, originInviteInfo);
|
|
await _database.SortedSetAddAsync(key, newInviteInfo, DateTimeOffset.Now.ToUnixTimeMilliseconds());
|
|
}
|
|
|
|
public void RemoveReceivedInviteInfo(string suffix, string inviteInfo)
|
|
{
|
|
Task.Run(() =>
|
|
{
|
|
string key = FriendReceivedInviteMyHomeKeyWithPrefix(suffix);
|
|
_database.SortedSetRemoveAsync(key, inviteInfo);
|
|
});
|
|
}
|
|
|
|
|
|
public async Task<List<string>> GetReceivedInviteInfo(string suffix)
|
|
{
|
|
string key = FriendReceivedInviteMyHomeKeyWithPrefix(suffix);
|
|
var values = await _database.SortedSetRangeByRankAsync(key, 0, -1, Order.Ascending, CommandFlags.PreferReplica);
|
|
|
|
|
|
List<string> result = new List<string>();
|
|
foreach (var v in values)
|
|
{
|
|
result.Add(v.ToString());
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
}
|