public void Disconnect() { SocketClient client; Program.OnlineAccounts.TryRemove(Account.Accountid, out client); Program.OnlineAccounts.TryAdd(client.Account.Accountid, client); var relations = PlayerRelationsRepository.GetAllRelations(client.Account.Accountid); foreach (var relation in relations) { AssociationType relationType; Enum.TryParse(relation.RelationType.ToString(), out relationType); switch (relationType) { case AssociationType.Friend: { var account = AccountRepository.GetAccount(relation.RelationId); if (account.Accountid == client.Account.Accountid) account = AccountRepository.GetAccount(relation.AccountId); if (Program.OnlineAccounts.ContainsKey(account.Accountid)) { var targetAssossiation = new PlayerAssociation(client.Account.Username.Length) { AccountId = client.Account.Accountid, AssociationExtra = 0, AssociationType = relation.RelationType, Username = client.Account.Username }; Program.OnlineAccounts[account.Accountid].Send(targetAssossiation); } break; } } } Handler.Shutdown(SocketShutdown.Both); Handler.BeginDisconnect(false, DisconnectCallback, this); }
private static void HandleSuccessfulLogin(SocketClient client) { Program.OnlineAccounts.TryAdd(client.Account.Accountid, client); var relations = PlayerRelationsRepository.GetAllRelations(client.Account.Accountid); foreach (var relation in relations) { AssociationType relationType; Enum.TryParse(relation.RelationType.ToString(), out relationType); switch (relationType) { case AssociationType.Friend: { var account = AccountRepository.GetAccount(relation.RelationId); if (account.Accountid == client.Account.Accountid) account = AccountRepository.GetAccount(relation.AccountId); if (Program.OnlineAccounts.ContainsKey(account.Accountid)) { var targetAssossiation = new PlayerAssociation(client.Account.Username.Length) { AccountId = client.Account.Accountid, AssociationExtra = 1, AssociationType = relation.RelationType, Username = client.Account.Username }; Program.OnlineAccounts[account.Accountid].Send(targetAssossiation); } var assossiation = new PlayerAssociation(account.Username.Length) { AccountId = account.Accountid, AssociationExtra = (short) (Program.OnlineAccounts.ContainsKey(account.Accountid) ? 1 : 0), AssociationType = relation.RelationType, Username = account.Username }; client.Send(assossiation); break; } case AssociationType.FriendRequest: { if (relation.AccountId != client.Account.Accountid) { var account = AccountRepository.GetAccount(relation.AccountId); //requesting account var assossiation = new PlayerAssociation(account.Username.Length) { AccountId = client.Account.Accountid,//receiving account AssociationType = (short) AssociationType.FriendRequest, Username = account.Username,//requesting account AssociationExtra = (short) account.Accountid//requesting account }; client.Send(assossiation); } else { var account = AccountRepository.GetAccount(relation.RelationId); var assossiation = new PlayerAssociation(account.Username.Length) { AccountId = client.Account.Accountid, AssociationType = (short) AssociationType.FriendRequest, Username = account.Username, AssociationExtra = (short) account.Accountid }; client.Send(assossiation); } break; } } } var data = new PlayerData(client.Account.Username.Length) { AccountId = client.Account.Accountid, Username = client.Account.Username }; client.Send(data); }
public static void Handle(SocketClient client, PlayerAssociation playerAssociation) { AssociationType aType; Enum.TryParse(playerAssociation.AssociationType.ToString(), out aType); switch (aType) { case Enums.AssociationType.FriendRequest: { var account = AccountRepository.GetAccount(playerAssociation.Username, string.Empty); //Person targeted for friend. if (account != null) { var relation = new PlayerAssociation(account.Username.Length) { AccountId = account.Accountid, AssociationType = (short) Enums.AssociationType.FriendRequest, Username = account.Username, AssociationExtra = (short) client.Account.Accountid }; client.Send(relation); if (Program.OnlineAccounts.ContainsKey(account.Accountid)) { var relation2 = new PlayerAssociation(client.Account.Username.Length) { AccountId = account.Accountid, Username = client.Account.Username, AssociationType = relation.AssociationType, AssociationExtra = (short) client.Account.Accountid }; var player = Program.OnlineAccounts[account.Accountid]; player.Send(relation2); } var relationDb = new PlayerRelations() { AccountId = relation.AccountId, RelationId = relation.AssociationExtra, RelationType = relation.AssociationType }; BaseRepository.Add(relationDb); } break; } case Enums.AssociationType.FriendAccept: { var relation = PlayerRelationsRepository.GetAllRelations(playerAssociation.AssociationExtra); foreach (var re in relation) { if (re.RelationType == (int) Enums.AssociationType.FriendRequest) { if (re.AccountId == playerAssociation.AssociationExtra && re.RelationId == playerAssociation.AccountId || re.AccountId == playerAssociation.AccountId && re.RelationId == playerAssociation.AssociationExtra) { re.RelationType = (short) Enums.AssociationType.Friend; } BaseRepository.Update(re); Accounts account;//Accepted Player account = AccountRepository.GetAccount(re.AccountId == client.Account.Accountid ? re.RelationId : re.AccountId); //Accepting player's Packet var relationPakcet = new PlayerAssociation(account.Username.Length) { AccountId = account.Accountid, AssociationType = (short)Enums.AssociationType.Friend, Username = account.Username, AssociationExtra = (short) (Program.OnlineAccounts.ContainsKey(account.Accountid) ? 1 : 0) }; client.Send(relationPakcet); if (Program.OnlineAccounts.ContainsKey(account.Accountid)) { var clientTarget = Program.OnlineAccounts[account.Accountid]; var relationPakcetTarget = new PlayerAssociation(client.Account.Username.Length) { AssociationExtra = 1, AccountId = client.Account.Accountid, Username = client.Account.Username, AssociationType = (short) Enums.AssociationType.Friend }; clientTarget.Send(relationPakcetTarget); } } } break; } case Enums.AssociationType.FriendDeny: { break; } } }