Skip to content

Commit

Permalink
Merge pull request #215 from ethanmoffat/trade
Browse files Browse the repository at this point in the history
Implement trading between players. Includes context menu, dialog, and packets.
  • Loading branch information
ethanmoffat authored Sep 16, 2022
2 parents 7ec1d32 + dc5e49f commit d5edeb6
Show file tree
Hide file tree
Showing 51 changed files with 1,351 additions and 1,373 deletions.
2 changes: 1 addition & 1 deletion EOLib.IO/Map/Matrix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public Matrix(int rows, int cols, T defaultValue)
Fill(defaultValue);
}

public Matrix(Matrix<T> other)
public Matrix(IReadOnlyMatrix<T> other)
: this(new T[other.Rows, other.Cols])
{
for (int row = 0; row < other.Rows; ++row)
Expand Down
2 changes: 1 addition & 1 deletion EOLib/Domain/Character/InventoryItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace EOLib.Domain.Character
{
[Record]
[Record(Features.Default | Features.EquatableEquals | Features.ObjectEquals)]
public sealed partial class InventoryItem
{
public short ItemID { get; }
Expand Down
23 changes: 23 additions & 0 deletions EOLib/Domain/Notifiers/ITradeEventNotifier.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using AutomaticTypeMapper;

namespace EOLib.Domain.Notifiers
{
public interface ITradeEventNotifier
{
void NotifyTradeRequest(short playerId, string name);

void NotifyTradeAccepted();

void NotifyTradeClose(bool cancel);
}

[AutoMappedType]
public class NoopTradeEventNotifier : ITradeEventNotifier
{
public void NotifyTradeRequest(short playerId, string name) { }

public void NotifyTradeAccepted() { }

public void NotifyTradeClose(bool cancel) { }
}
}
84 changes: 84 additions & 0 deletions EOLib/Domain/Trade/TradeActions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using AutomaticTypeMapper;
using EOLib.Net;
using EOLib.Net.Communication;
using System;

namespace EOLib.Domain.Trade
{
[AutoMappedType]
public class TradeActions : ITradeActions
{
private readonly IPacketSendService _packetSendService;

public TradeActions(IPacketSendService packetSendService)
{
_packetSendService = packetSendService;
}

public void RequestTrade(short characterID)
{
var packet = new PacketBuilder(PacketFamily.Trade, PacketAction.Request)
.AddChar(6)
.AddShort(characterID)
.Build();
_packetSendService.SendPacket(packet);
}

public void AcceptTradeRequest(short characterID)
{
var packet = new PacketBuilder(PacketFamily.Trade, PacketAction.Accept)
.AddChar(6)
.AddShort(characterID)
.Build();
_packetSendService.SendPacket(packet);
}

public void RemoveItemFromOffer(short itemID)
{
var packet = new PacketBuilder(PacketFamily.Trade, PacketAction.Remove)
.AddShort(itemID)
.Build();
_packetSendService.SendPacket(packet);
}

public void AddItemToOffer(short itemID, int amount)
{
var packet = new PacketBuilder(PacketFamily.Trade, PacketAction.Add)
.AddShort(itemID)
.AddInt(amount)
.Build();
_packetSendService.SendPacket(packet);
}

public void AgreeToTrade(bool agree)
{
var packet = new PacketBuilder(PacketFamily.Trade, PacketAction.Agree)
.AddChar((byte)(agree ? 1 : 0))
.Build();
_packetSendService.SendPacket(packet);
}

public void CancelTrade()
{
var packet = new PacketBuilder(PacketFamily.Trade, PacketAction.Close)
.AddChar(6)
.Build();
_packetSendService.SendPacket(packet);
}
}

public interface ITradeActions
{
void RequestTrade(short characterID);

void AcceptTradeRequest(short characterID);

void RemoveItemFromOffer(short itemID);

void AddItemToOffer(short itemID, int amount);

void AgreeToTrade(bool agree);

void CancelTrade();
}
}
18 changes: 18 additions & 0 deletions EOLib/Domain/Trade/TradeOffer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Amadevus.RecordGenerator;
using EOLib.Domain.Character;
using System.Collections.Generic;

namespace EOLib.Domain.Trade
{
[Record(Features.Default | Features.EquatableEquals | Features.ObjectEquals)]
public sealed partial class TradeOffer
{
public bool Agrees { get; }

public short PlayerID { get; }

public string PlayerName { get; }

public IReadOnlyList<InventoryItem> Items { get; }
}
}
39 changes: 39 additions & 0 deletions EOLib/Domain/Trade/TradeRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using AutomaticTypeMapper;
using EOLib.Domain.Character;
using System.Collections.Generic;

namespace EOLib.Domain.Trade
{
public interface ITradeRepository : IResettable
{
TradeOffer PlayerOneOffer { get; set; }

TradeOffer PlayerTwoOffer { get; set; }
}

public interface ITradeProvider
{
TradeOffer PlayerOneOffer { get; }

TradeOffer PlayerTwoOffer { get; }
}

[AutoMappedType(IsSingleton = true)]
public class TradeRepository : ITradeRepository, ITradeProvider
{
public TradeOffer PlayerOneOffer { get; set; }

public TradeOffer PlayerTwoOffer { get; set; }

public TradeRepository()
{
ResetState();
}

public void ResetState()
{
PlayerOneOffer = new TradeOffer(false, 0, string.Empty, new List<InventoryItem>());
PlayerTwoOffer = new TradeOffer(false, 0, string.Empty, new List<InventoryItem>());
}
}
}
30 changes: 0 additions & 30 deletions EOLib/Net/API/PacketAPI.cs

This file was deleted.

Loading

0 comments on commit d5edeb6

Please sign in to comment.