More 2PLAYER stuff

This commit is contained in:
SilicaAndPina 2021-03-13 23:40:34 +13:00
parent 30e7250a48
commit 32a288c58a
6 changed files with 289 additions and 8 deletions

View file

@ -1774,6 +1774,9 @@ namespace HISP.Game
}
private static string buildAuction(User user, Auction auction)
{
Multiroom room = Multiroom.GetMultiroom(user.X, user.Y);
room.Join(user);
string message = "";
message += Messages.AuctionsRunning;
foreach(Auction.AuctionEntry entry in auction.AuctionEntries.ToArray())
@ -1789,7 +1792,7 @@ namespace HISP.Game
message += Messages.FormatAuctionSoldTo(Database.GetUsername(entry.HighestBidder), entry.HighestBid);
}
}
User[] users = GameServer.GetUsersAt(user.X, user.Y, true, true);
User[] users = room.JoinedUsers.ToArray();
List<string> usernames = new List<string>();
foreach(User userInst in users)
{
@ -2635,6 +2638,58 @@ namespace HISP.Game
message += Messages.MetaTerminator;
return message;
}
public static string build2PlayerGame(User user, string swf)
{
Multiroom room = Multiroom.GetMultiroom(user.X, user.Y);
room.Join(user);
if (TwoPlayer.IsPlayerInGame(user))
{
string username = "";
TwoPlayer twoPlayerGame = TwoPlayer.GetTwoPlayerGameInProgress(user);
if (twoPlayerGame.Invitee.Id == user.Id)
username = twoPlayerGame.Invitee.Username;
else
username = twoPlayerGame.Inviting.Username;
return Messages.Format2PlayerGameInProgress(username);
}
string message = Messages.TwoPlayerOtherPlayer;
foreach(User userAt in room.JoinedUsers.ToArray())
{
if (userAt.Id == user.Id)
continue;
message += Messages.Format2PlayerPlayerName(userAt.Username);
if(!TwoPlayer.IsPlayerInGame(userAt))
{
if (TwoPlayer.IsPlayerInvitingPlayer(user, userAt))
message += Messages.TwoPlayerSentInvite;
else
message += Messages.Format2PlayerInviteButton(userAt.Id);
if (TwoPlayer.IsPlayerInvitingPlayer(userAt, user))
message += Messages.Format2PlayerAcceptButton(userAt.Id);
}
else
{
string username = "";
TwoPlayer twoPlayerGame = TwoPlayer.GetTwoPlayerGameInProgress(userAt);
if (twoPlayerGame.Invitee.Id == userAt.Id)
username = twoPlayerGame.Invitee.Username;
else
username = twoPlayerGame.Inviting.Username;
message += Messages.Format2PlayerPlayingWith(username);
}
message += Messages.R1;
}
message += Messages.ExitThisPlace;
return message;
}
public static string BuildComposeMailMenu()
{
string message = Messages.CityHallMailSendMeta;
@ -2828,7 +2883,6 @@ namespace HISP.Game
}
if(TileCode == "AUCTION")
{
user.MetaPriority = false;
message += buildAuction(user, Auction.GetAuctionRoomById(int.Parse(TileArg)));
}
if(TileCode == "TRAINER")
@ -2847,6 +2901,10 @@ namespace HISP.Game
{
message += buildPond(user);
}
if(TileCode == "2PLAYER")
{
message += build2PlayerGame(user, TileArg);
}
if(TileCode == "HORSES")
{
message += buildHorseGame(user, TileArg);

View file

@ -38,7 +38,7 @@ namespace HISP.Game
{
if (tile.Code != null)
{
if (tile.Code.StartsWith("MULTIROOM"))
if (tile.Code.StartsWith("MULTIROOM") || tile.Code.StartsWith("2PLAYER") || tile.Code.StartsWith("AUCTION"))
{
Logger.DebugPrint("Created Multiroom @ " + tile.X.ToString() + "," + tile.Y.ToString());
new Multiroom(tile.X, tile.Y);
@ -69,7 +69,9 @@ namespace HISP.Game
foreach (User joinedUser in JoinedUsers)
if (joinedUser.Id != user.Id)
GameServer.UpdateArea(joinedUser.LoggedinClient);
if(!TwoPlayer.IsPlayerInGame(joinedUser))
if(!joinedUser.ListingAuction)
GameServer.UpdateArea(joinedUser.LoggedinClient);
}
}
@ -84,7 +86,9 @@ namespace HISP.Game
}
foreach (User joinedUser in JoinedUsers)
GameServer.UpdateArea(joinedUser.LoggedinClient);
if (!TwoPlayer.IsPlayerInGame(joinedUser))
if (!joinedUser.ListingAuction)
GameServer.UpdateArea(joinedUser.LoggedinClient);
}
}

View file

@ -0,0 +1,193 @@
using HISP.Player;
using HISP.Security;
using HISP.Server;
using System.Collections.Generic;
using System.Threading;
namespace HISP.Game
{
public class TwoPlayer
{
public static List<TwoPlayer> TwoPlayerGames = new List<TwoPlayer>();
public static bool IsPlayerInvitingPlayer(User sender, User checkInvites)
{
foreach (TwoPlayer twoPlayerGame in TwoPlayerGames.ToArray())
{
if ((twoPlayerGame.Invitee.Id == sender.Id && twoPlayerGame.Inviting.Id == checkInvites.Id) && !twoPlayerGame.Accepted)
{
return true;
}
}
return false;
}
public static bool IsPlayerInGame(User user)
{
foreach (TwoPlayer twoPlayerGame in TwoPlayerGames.ToArray())
{
if ((twoPlayerGame.Invitee.Id == user.Id || twoPlayerGame.Inviting.Id == user.Id) && twoPlayerGame.Accepted)
{
return true;
}
}
return false;
}
public static TwoPlayer GetTwoPlayerGameInProgress(User user)
{
foreach (TwoPlayer twoPlayerGame in TwoPlayerGames.ToArray())
{
if ((twoPlayerGame.Invitee.Id == user.Id || twoPlayerGame.Inviting.Id == user.Id) && twoPlayerGame.Accepted)
{
return twoPlayerGame;
}
}
throw new KeyNotFoundException("No game found");
}
public TwoPlayer(User inviting, User invitee, bool accepted, int randomId=-1)
{
RandomId = RandomID.NextRandomId(randomId);
Inviting = inviting;
Invitee = invitee;
Accepted = accepted;
PosX = Invitee.X;
PosY = Invitee.Y;
byte[] youHaveInvited = PacketBuilder.CreateChat(Messages.Format2PlayerYouInvited(inviting.Username), PacketBuilder.CHAT_BOTTOM_RIGHT);
byte[] yourInvited = PacketBuilder.CreateChat(Messages.Format2PlayerYourInvited(invitee.Username), PacketBuilder.CHAT_BOTTOM_RIGHT);
Invitee.LoggedinClient.SendPacket(youHaveInvited);
Inviting.LoggedinClient.SendPacket(yourInvited);
deleteTimer = new Timer(new TimerCallback(deleteTwoPlayer), null, 2 * 60 * 1000, 2 * 60 * 1000);
TwoPlayerGames.Add(this);
update();
if (Multiroom.IsMultiRoomAt(PosX, PosY))
{
this.Multiroom = Multiroom.GetMultiroom(PosX, PosY);
}
}
private void deleteTwoPlayer(object state)
{
if (!Accepted)
{
Accepted = false;
Invitee.MetaPriority = false;
Inviting.MetaPriority = false;
GameServer.UpdateAreaForAll(Invitee.X, Invitee.Y);
TwoPlayerGames.Remove(this);
}
deleteTimer.Dispose();
}
private void update()
{
GameServer.UpdateArea(Invitee.LoggedinClient);
GameServer.UpdateArea(Inviting.LoggedinClient);
}
private void updateOthers()
{
foreach(User user in this.Multiroom.JoinedUsers.ToArray())
if (IsPlayerInGame(user))
if(user.Id != Invitee.Id && user.Id != Inviting.Id)
GameServer.UpdateArea(user.LoggedinClient);
}
public void UpdateAll()
{
update();
updateOthers();
}
private string buildSwf(int playerNumb)
{
if(World.InSpecialTile(PosX, PosY))
{
World.SpecialTile tile = World.GetSpecialTile(PosX, PosY);
if(tile.Code != null)
{
if(tile.Code.StartsWith("2PLAYER-"))
{
string swf = tile.Code.Split('-')[1].ToLower();
if (!swf.Contains(".swf"))
swf += ".swf";
if (swf.Contains("?"))
swf += "&";
else
swf += "?";
swf += "playernum=" + playerNumb.ToString();
return swf;
}
}
}
return "test";
}
public int RandomId = 0;
public User Inviting = null;
public User Invitee = null;
public bool Accepted = false;
public Multiroom Multiroom;
public int PosX;
public int PosY;
private Timer deleteTimer;
public void Accept(User user)
{
if(user.Id == Inviting.Id)
{
Accepted = true;
updateOthers();
deleteTimer.Dispose();
byte[] loadSwfInvitee = PacketBuilder.CreateSwfModulePacket(buildSwf(2), PacketBuilder.PACKET_SWF_MODULE_FORCE);
byte[] loadSwfInvited = PacketBuilder.CreateSwfModulePacket(buildSwf(1), PacketBuilder.PACKET_SWF_MODULE_FORCE);
Invitee.LoggedinClient.SendPacket(loadSwfInvitee);
Inviting.LoggedinClient.SendPacket(loadSwfInvited);
}
}
public void CloseGame(User userWhoClosed)
{
if(userWhoClosed.Id == Inviting.Id || userWhoClosed.Id == Invitee.Id && Accepted)
{
byte[] gameClosedByOther = PacketBuilder.CreateChat(Messages.TwoPlayerGameClosedOther, PacketBuilder.CHAT_BOTTOM_RIGHT);
byte[] gameClosed = PacketBuilder.CreateChat(Messages.TwoPlayerGameClosed, PacketBuilder.CHAT_BOTTOM_RIGHT);
if (userWhoClosed.Id == Inviting.Id)
{
Invitee.LoggedinClient.SendPacket(gameClosedByOther);
Inviting.LoggedinClient.SendPacket(gameClosed);
}
else if (userWhoClosed.Id == Invitee.Id)
{
Invitee.LoggedinClient.SendPacket(gameClosed);
Inviting.LoggedinClient.SendPacket(gameClosedByOther);
}
Accepted = false;
Invitee.MetaPriority = false;
Inviting.MetaPriority = false;
UpdateAll();
TwoPlayerGames.Remove(this);
}
}
}
}

View file

@ -103,6 +103,7 @@ namespace HISP.Player
public DateTime LoginTime;
public string LastSeenWeather;
public int LastClickedRanchBuilding = 0;
public bool ListingAuction = false;
public string GetWeatherSeen()
{

View file

@ -2754,6 +2754,7 @@ namespace HISP.Server
break;
case "41": // Put horse into auction
sender.LoggedinUser.MetaPriority = true;
sender.LoggedinUser.ListingAuction = true;
metaPacket = PacketBuilder.CreateMetaPacket(Meta.BuildAuctionHorseList(sender.LoggedinUser));
sender.SendPacket(metaPacket);
break;
@ -3304,6 +3305,27 @@ namespace HISP.Server
byte module = packet[1];
switch(module)
{
case PacketBuilder.SWFMODULE_INVITE:
if(packet.Length < 4)
{
Logger.ErrorPrint(sender.LoggedinUser.Username + " Sent an invalid 2PLAYER INVITE Packet (WRONG SIZE)");
break;
}
string packetStr = Encoding.UTF8.GetString(packet);
string playerIdStr = packetStr.Substring(2, packetStr.Length - 4);
int playerId = -1;
try
{
playerId = int.Parse(playerIdStr);
}
catch (Exception) { };
if(IsUserOnline(playerId))
{
User toInvite = GetUserById(playerId);
TwoPlayer twoPlayerGame = new TwoPlayer(toInvite, sender.LoggedinUser, false);
}
break;
case PacketBuilder.SWFMODULE_DRAWINGROOM:
if(packet.Length < 3)
{
@ -3489,7 +3511,7 @@ namespace HISP.Server
break;
}
string packetStr = Encoding.UTF8.GetString(packet);
packetStr = Encoding.UTF8.GetString(packet);
string drawing = packetStr.Substring(3, packetStr.Length - 5);
if (drawing.Contains("X")) // Clear byte
@ -3551,7 +3573,7 @@ namespace HISP.Server
Logger.ErrorPrint(sender.LoggedinUser.Username + " Sent invalid BRICKPOET MOVE packet (swf communication, WRONG SIZE)");
break;
}
string packetStr = Encoding.UTF8.GetString(packet);
packetStr = Encoding.UTF8.GetString(packet);
if(!packetStr.Contains('|'))
{
Logger.ErrorPrint(sender.LoggedinUser.Username + " Sent invalid BRICKPOET MOVE packet (swf communication, NO | SEPERATOR)");
@ -3650,7 +3672,7 @@ namespace HISP.Server
}
Dressup.DressupRoom room = Dressup.GetDressupRoom(roomId);
string packetStr = Encoding.UTF8.GetString(packet);
packetStr = Encoding.UTF8.GetString(packet);
string moveStr = packetStr.Substring(3, packetStr.Length - 5);
string[] moves = moveStr.Split('|');
@ -6864,6 +6886,7 @@ namespace HISP.Server
}
forClient.LoggedinUser.MetaPriority = false;
forClient.LoggedinUser.ListingAuction = false;
string LocationStr = "";
if (!World.InSpecialTile(forClient.LoggedinUser.X, forClient.LoggedinUser.Y))

View file

@ -112,6 +112,8 @@ namespace HISP.Server
public const byte HORSE_ESCAPE = 0x1E;
public const byte HORSE_CAUGHT = 0x1D;
public const byte SWFMODULE_INVITE = 0x14;
public const byte SWFMODULE_ARENA = 0x52;
public const byte SWFMODULE_BRICKPOET = 0x5A;
public const byte SWFMODULE_DRAWINGROOM = 0x5B;