From b686afed619a67506823ce66503758c96fa8b479 Mon Sep 17 00:00:00 2001 From: SilicaAndPina Date: Fri, 5 Mar 2021 15:29:31 +1300 Subject: [PATCH] Add 'add buddy'? --- DataCollection/gamedata.json | 5 +++ .../HorseIsleServer/Game/Messages.cs | 14 +++++- .../HorseIsleServer/Game/Meta.cs | 12 ++++- .../HorseIsleServer/Player/Friends.cs | 26 ++++++++--- .../HorseIsleServer/Player/User.cs | 1 + .../HorseIsleServer/Server/Converters.cs | 2 +- .../HorseIsleServer/Server/Database.cs | 44 +++---------------- .../HorseIsleServer/Server/GameDataJson.cs | 10 ++++- .../HorseIsleServer/Server/GameServer.cs | 21 +++++++++ .../HorseIsleServer/Server/PacketBuilder.cs | 1 + 10 files changed, 88 insertions(+), 48 deletions(-) diff --git a/DataCollection/gamedata.json b/DataCollection/gamedata.json index 7b0512f..bd97d92 100755 --- a/DataCollection/gamedata.json +++ b/DataCollection/gamedata.json @@ -225,6 +225,11 @@ "buddy_button":"^B1B%PLAYERID%", "tag_button":"^B1X%PLAYERID%", "pm_button":"^B1P%PLAYERNAME%", + "add_buddy":{ + "add_pending":"Attempting to Add Buddy. The other player must click ADD BUDDY as well. (Many players reserve this for just a couple players so don't feel insulted if they do not).", + "other_pending":"%PLAYERNAME% is trying to add you as a buddy, click ADD BUDDY to make them a buddy.", + "add_confirmed":"You and %PLAYERNAME% are now buddies." + }, "socials":{ "socials_button":"^B5%ID%%SOCIALNAME%", "socials_message":"* %SOCIALMSG% *", diff --git a/Horse Isle Server/HorseIsleServer/Game/Messages.cs b/Horse Isle Server/HorseIsleServer/Game/Messages.cs index 2b2f580..322304f 100755 --- a/Horse Isle Server/HorseIsleServer/Game/Messages.cs +++ b/Horse Isle Server/HorseIsleServer/Game/Messages.cs @@ -13,6 +13,11 @@ namespace HISP.Game // Mod isle public static string ModIsleMessage; + // Add Buddy + public static string AddBuddyPending; + public static string AddBuddyOtherPendingFormat; + public static string AddBuddyYourNowBuddiesFormat; + // Socials public static string SocialButton; public static string SocialMessageFormat; @@ -1020,7 +1025,14 @@ namespace HISP.Game // Click public static string NothingInterestingHere; - + public static string FormatAddBuddyConfirmed(string playername) + { + return AddBuddyYourNowBuddiesFormat.Replace("%PLAYERNAME%", playername); + } + public static string FormatAddBuddyPendingOther(string playername) + { + return AddBuddyOtherPendingFormat.Replace("%PLAYERNAME%", playername); + } public static string FormatOtherNoCompetitionGear(string pronoun) { return NoCompetitionGearOther.Replace("%PRONOUN%", pronoun); diff --git a/Horse Isle Server/HorseIsleServer/Game/Meta.cs b/Horse Isle Server/HorseIsleServer/Game/Meta.cs index cf1e76a..32b6ba2 100755 --- a/Horse Isle Server/HorseIsleServer/Game/Meta.cs +++ b/Horse Isle Server/HorseIsleServer/Game/Meta.cs @@ -1217,7 +1217,7 @@ namespace HISP.Game message += Messages.BuddyListOfflineBuddys; string username = Database.GetUsername(id); - int minutes = (DateTime.UtcNow - Converters.UnixTimeStampToDateTime(Database.GetPlayerLastLogin(id))).Minutes; + int minutes = Convert.ToInt32(Math.Round((Converters.UnixTimeStampToDateTime(Database.GetPlayerLastLogin(id)) - DateTime.UtcNow).TotalMinutes)); message += Messages.FormatOfflineBuddyEntry(username, id, minutes); } @@ -1994,9 +1994,17 @@ namespace HISP.Game public static string BuildPlayerList(User user) { string message = ""; + + int friendsOnline = 0; + foreach(int friendId in user.Friends.List) + { + if (GameServer.IsUserOnline(friendId)) + friendsOnline++; + } + message += Messages.PlayerListHeader; message += Messages.PlayerListSelectFromFollowing; - message += Messages.FormatPlayerBuddyList(user.Friends.Count); + message += Messages.FormatPlayerBuddyList(friendsOnline); message += Messages.PlayerListOfNearby; message += Messages.FormatPlayerList(GameServer.GetNumberOfPlayers() - 1); message += Messages.PlayerListOfPlayersAlphabetically; diff --git a/Horse Isle Server/HorseIsleServer/Player/Friends.cs b/Horse Isle Server/HorseIsleServer/Player/Friends.cs index 4120c21..8a96a07 100755 --- a/Horse Isle Server/HorseIsleServer/Player/Friends.cs +++ b/Horse Isle Server/HorseIsleServer/Player/Friends.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using HISP.Game; using HISP.Server; namespace HISP.Player @@ -49,24 +50,37 @@ namespace HISP.Player removeFrom.Friends.List.Remove(baseUser.Id); } - catch (KeyNotFoundException) { /* User is ofline, remove from database is sufficent */ }; + catch (KeyNotFoundException) { /* User is offline, remove from database is sufficent */ }; baseUser.Friends.List.Remove(userid); } public void AddFriend(User userToFriend) { - bool pendingRequest = Database.IsPendingBuddyRequestExist(baseUser.Id, userToFriend.Id); - if (pendingRequest) + if (userToFriend.PendingBuddyRequestTo == baseUser) { - Database.AcceptBuddyRequest(baseUser.Id, userToFriend.Id); - + Database.AddBuddy(baseUser.Id, userToFriend.Id); List.Add(userToFriend.Id); userToFriend.Friends.List.Add(baseUser.Id); + + byte[] nowFriendsMsg = PacketBuilder.CreateChat(Messages.FormatAddBuddyConfirmed(userToFriend.Username), PacketBuilder.CHAT_BOTTOM_RIGHT); + byte[] nowFriendsOther = PacketBuilder.CreateChat(Messages.FormatAddBuddyConfirmed(baseUser.Username), PacketBuilder.CHAT_BOTTOM_RIGHT); + + userToFriend.LoggedinClient.SendPacket(nowFriendsOther); + baseUser.LoggedinClient.SendPacket(nowFriendsMsg); + + GameServer.UpdateArea(baseUser.LoggedinClient); + GameServer.UpdateArea(userToFriend.LoggedinClient); } else { - Database.AddPendingBuddyRequest(baseUser.Id, userToFriend.Id); + baseUser.PendingBuddyRequestTo = userToFriend; + byte[] pendingMsg = PacketBuilder.CreateChat(Messages.AddBuddyPending, PacketBuilder.CHAT_BOTTOM_RIGHT); + byte[] pendingMsgOther = PacketBuilder.CreateChat(Messages.FormatAddBuddyPendingOther(baseUser.Username), PacketBuilder.CHAT_BOTTOM_RIGHT); + baseUser.LoggedinClient.SendPacket(pendingMsg); + if(!userToFriend.MuteBuddyRequests && !userToFriend.MuteAll) + userToFriend.LoggedinClient.SendPacket(pendingMsgOther); + } } diff --git a/Horse Isle Server/HorseIsleServer/Player/User.cs b/Horse Isle Server/HorseIsleServer/Player/User.cs index 8cde317..3762148 100755 --- a/Horse Isle Server/HorseIsleServer/Player/User.cs +++ b/Horse Isle Server/HorseIsleServer/Player/User.cs @@ -97,6 +97,7 @@ namespace HISP.Player public Riddler LastRiddle; public Award Awards; public User SocializingWith; + public User PendingBuddyRequestTo; public int CapturingHorseId; public DateTime LoginTime; public string LastSeenWeather; diff --git a/Horse Isle Server/HorseIsleServer/Server/Converters.cs b/Horse Isle Server/HorseIsleServer/Server/Converters.cs index b7b2a9a..5ce9044 100755 --- a/Horse Isle Server/HorseIsleServer/Server/Converters.cs +++ b/Horse Isle Server/HorseIsleServer/Server/Converters.cs @@ -40,7 +40,7 @@ namespace HISP.Server public static DateTime UnixTimeStampToDateTime(double unixTimeStamp) { // Unix timestamp is seconds past epoch - System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc); + System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); dtDateTime = dtDateTime.AddSeconds(unixTimeStamp).ToLocalTime(); return dtDateTime; } diff --git a/Horse Isle Server/HorseIsleServer/Server/Database.cs b/Horse Isle Server/HorseIsleServer/Server/Database.cs index 2cad5e8..9cd5815 100755 --- a/Horse Isle Server/HorseIsleServer/Server/Database.cs +++ b/Horse Isle Server/HorseIsleServer/Server/Database.cs @@ -24,7 +24,7 @@ namespace HISP.Server string UserTable = "CREATE TABLE IF NOT EXISTS Users(Id INT, Username TEXT(16),Email TEXT(128),Country TEXT(128),SecurityQuestion Text(128),SecurityAnswerHash TEXT(128),Age INT,PassHash TEXT(128), Salt TEXT(128),Gender TEXT(16), Admin TEXT(3), Moderator TEXT(3))"; string ExtTable = "CREATE TABLE IF NOT EXISTS UserExt(Id INT, X INT, Y INT, LastLogin INT, Money INT, QuestPoints INT, BankBalance DOUBLE, BankInterest DOUBLE, ProfilePage Text(1028),IpAddress TEXT(1028),PrivateNotes Text(1028), CharId INT, ChatViolations INT,Subscriber TEXT(3), SubscribedUntil INT, Experience INT, Tiredness INT, Hunger INT, Thirst INT, FreeMinutes INT)"; string MailTable = "CREATE TABLE IF NOT EXISTS Mailbox(RandomId INT, IdTo INT, IdFrom INT, Subject TEXT(128), Message Text(1028), TimeSent INT, BeenRead TEXT(3))"; - string BuddyTable = "CREATE TABLE IF NOT EXISTS BuddyList(Id INT, IdFriend INT, Pending TEXT(3))"; + string BuddyTable = "CREATE TABLE IF NOT EXISTS BuddyList(Id INT, IdFriend INT)"; string WorldTable = "CREATE TABLE World(Time INT, Day INT, Year INT, StartTime INT)"; string WeatherTable = "CREATE TABLE IF NOT EXISTS Weather(Area TEXT(1028), Weather TEXT(64))"; string InventoryTable = "CREATE TABLE IF NOT EXISTS Inventory(PlayerID INT, RandomID INT, ItemID INT, Data INT)"; @@ -4292,7 +4292,7 @@ namespace HISP.Server { db.Open(); MySqlCommand sqlCommand = db.CreateCommand(); - sqlCommand.CommandText = "SELECT COUNT(1) FROM BuddyList WHERE Id=@id OR IdFriend=@id AND Pending='NO'"; + sqlCommand.CommandText = "SELECT COUNT(1) FROM BuddyList WHERE Id=@id OR IdFriend=@id"; sqlCommand.Parameters.AddWithValue("@id", id); sqlCommand.Prepare(); @@ -4314,7 +4314,7 @@ namespace HISP.Server List BuddyList = new List(); MySqlCommand sqlCommand = db.CreateCommand(); - sqlCommand.CommandText = "SELECT Id,IdFriend FROM BuddyList WHERE Id=@id OR IdFriend=@id AND Pending='NO'"; + sqlCommand.CommandText = "SELECT Id,IdFriend FROM BuddyList WHERE Id=@id OR IdFriend=@id"; sqlCommand.Parameters.AddWithValue("@id", id); sqlCommand.Prepare(); MySqlDataReader dataReader = sqlCommand.ExecuteReader(); @@ -4326,7 +4326,7 @@ namespace HISP.Server if (adder != id) BuddyList.Add(adder); else if (friend != id) - BuddyList.Add(adder); + BuddyList.Add(friend); } sqlCommand.Dispose(); @@ -4334,23 +4334,6 @@ namespace HISP.Server } } - public static bool IsPendingBuddyRequestExist(int id, int friendId) - { - using (MySqlConnection db = new MySqlConnection(ConnectionString)) - { - db.Open(); - MySqlCommand sqlCommand = db.CreateCommand(); - sqlCommand.CommandText = "SELECT COUNT(1) FROM BuddyList WHERE (Id=@id AND IdFriend=@friendId) OR (Id=@friendid AND IdFriend=@Id) AND Pending='YES'"; - sqlCommand.Parameters.AddWithValue("@id", id); - sqlCommand.Parameters.AddWithValue("@friendId", friendId); - sqlCommand.Prepare(); - - Int32 count = Convert.ToInt32(sqlCommand.ExecuteScalar()); - sqlCommand.Dispose(); - return count >= 1; - } - } - public static void RemoveBuddy(int id, int friendId) { using (MySqlConnection db = new MySqlConnection(ConnectionString)) @@ -4365,27 +4348,14 @@ namespace HISP.Server sqlCommand.Dispose(); } } - public static void AcceptBuddyRequest(int id, int friendId) + + public static void AddBuddy(int id, int friendId) { using (MySqlConnection db = new MySqlConnection(ConnectionString)) { db.Open(); MySqlCommand sqlCommand = db.CreateCommand(); - sqlCommand.CommandText = "UPDATE BuddyList SET Pending=false WHERE (Id=@id AND IdFriend=@friendId) OR (Id=@friendid AND IdFriend=@Id)"; - sqlCommand.Parameters.AddWithValue("@id", id); - sqlCommand.Parameters.AddWithValue("@friendId", friendId); - sqlCommand.Prepare(); - sqlCommand.ExecuteNonQuery(); - sqlCommand.Dispose(); - } - } - public static void AddPendingBuddyRequest(int id, int friendId) - { - using (MySqlConnection db = new MySqlConnection(ConnectionString)) - { - db.Open(); - MySqlCommand sqlCommand = db.CreateCommand(); - sqlCommand.CommandText = "INSERT INTO BuddyList VALUES(@id,@friendId,'YES')"; + sqlCommand.CommandText = "INSERT INTO BuddyList VALUES(@id,@friendId)"; sqlCommand.Parameters.AddWithValue("@id", id); sqlCommand.Parameters.AddWithValue("@friendId", friendId); sqlCommand.Prepare(); diff --git a/Horse Isle Server/HorseIsleServer/Server/GameDataJson.cs b/Horse Isle Server/HorseIsleServer/Server/GameDataJson.cs index 3f752cc..075f236 100755 --- a/Horse Isle Server/HorseIsleServer/Server/GameDataJson.cs +++ b/Horse Isle Server/HorseIsleServer/Server/GameDataJson.cs @@ -835,7 +835,7 @@ namespace HISP.Server Map.NewUserStartY = gameData.messages.new_user.starting_y; // Warp Command - + Messages.SuccessfullyWarpedToPlayer = gameData.messages.commands.warp.player; Messages.SuccessfullyWarpedToLocation = gameData.messages.commands.warp.location; Messages.OnlyUnicornCanWarp = gameData.messages.commands.warp.only_unicorn; @@ -847,12 +847,20 @@ namespace HISP.Server Map.ModIsleX = gameData.messages.commands.mod_isle.x; Map.ModIsleY = gameData.messages.commands.mod_isle.y; + + // Add Buddy + Messages.AddBuddyPending = gameData.messages.meta.player_interaction.add_buddy.add_pending; + Messages.AddBuddyOtherPendingFormat = gameData.messages.meta.player_interaction.add_buddy.other_pending; + Messages.AddBuddyYourNowBuddiesFormat = gameData.messages.meta.player_interaction.add_buddy.add_confirmed; + + // Socials Messages.SocialButton = gameData.messages.meta.player_interaction.socials.socials_button; Messages.SocialMessageFormat = gameData.messages.meta.player_interaction.socials.socials_message; Messages.SocialTypeFormat = gameData.messages.meta.player_interaction.socials.socials_menu_type; // Trade + Messages.TradeWithPlayerFormat = gameData.messages.meta.player_interaction.trade.trading_with; Messages.TradeWaitingForOtherDone = gameData.messages.meta.player_interaction.trade.trade_wait_for_done; diff --git a/Horse Isle Server/HorseIsleServer/Server/GameServer.cs b/Horse Isle Server/HorseIsleServer/Server/GameServer.cs index f7ce06d..52ee320 100755 --- a/Horse Isle Server/HorseIsleServer/Server/GameServer.cs +++ b/Horse Isle Server/HorseIsleServer/Server/GameServer.cs @@ -263,6 +263,25 @@ namespace HISP.Server sender.SendPacket(metaTag); } break; + case PacketBuilder.PLAYER_INTERACTION_ADD_BUDDY: + packetStr = Encoding.UTF8.GetString(packet); + playerIdStr = packetStr.Substring(2, packetStr.Length - 4); + playerId = -1; + try + { + playerId = int.Parse(playerIdStr); + } + catch (FormatException) + { + Logger.ErrorPrint(sender.LoggedinUser.Username + " tried to trade with User ID NaN."); + break; + } + if (IsUserOnline(playerId)) + { + User userToAdd = GetUserById(playerId); + sender.LoggedinUser.Friends.AddFriend(userToAdd); + } + break; case PacketBuilder.PLAYER_INTERACTION_ADD_ITEM: if (sender.LoggedinUser.TradingWith == null) break; @@ -4288,6 +4307,7 @@ namespace HISP.Server if (loggedInUser.TradingWith != null) loggedInUser.TradingWith.CancelTradeMoved(); + loggedInUser.PendingBuddyRequestTo = null; byte[] moveResponse = PacketBuilder.CreateMovementPacket(loggedInUser.X, loggedInUser.Y, loggedInUser.CharacterId, loggedInUser.Facing, direction, true); sender.SendPacket(moveResponse); @@ -6242,6 +6262,7 @@ namespace HISP.Server if (!client.LoggedinUser.MuteLogins && !client.LoggedinUser.MuteAll) if (client.LoggedinUser.Id != sender.LoggedinUser.Id) client.SendPacket(logoutMessageBytes); + // Tell clients of diconnect (remove from chat) byte[] playerRemovePacket = PacketBuilder.CreatePlayerLeavePacket(sender.LoggedinUser.Username); foreach (GameClient client in ConnectedClients) diff --git a/Horse Isle Server/HorseIsleServer/Server/PacketBuilder.cs b/Horse Isle Server/HorseIsleServer/Server/PacketBuilder.cs index 9caa2fb..0fa636a 100755 --- a/Horse Isle Server/HorseIsleServer/Server/PacketBuilder.cs +++ b/Horse Isle Server/HorseIsleServer/Server/PacketBuilder.cs @@ -61,6 +61,7 @@ namespace HISP.Server public const byte PLAYER_INTERACTION_ADD_ITEM = 0x29; public const byte PLAYER_INTERACTION_ACCEPT = 0x2A; public const byte PLAYER_INTERACTION_TRADE_REJECT = 0x2B; + public const byte PLAYER_INTERACTION_ADD_BUDDY = 0x1E; public const byte AUCTION_BID_100 = 0x29; public const byte AUCTION_BID_1K = 0x2A;