Skip to content

Commit 1e923c0

Browse files
committed
Optimize SendItems serialization; small bugfix
Closes #27
1 parent 3e20cba commit 1e923c0

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

server/GameInv/InventoryNS/Inventory.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,11 @@ public bool RemoveItem(Item item, bool noLog = false) {
147147
}
148148

149149
public bool RemoveItem(string id, bool noLog = false) {
150-
return RemoveItem(_items[GetItemIndex(id)], noLog);
150+
var index = GetItemIndex(id);
151+
// ReSharper disable once ConvertIfStatementToReturnStatement
152+
if (index == -1) return false;
153+
154+
return RemoveItem(_items[index], noLog);
151155
}
152156

153157
public bool ModifyItem(Item item) {

server/GameInv/Ws/WebSocketConnectionInterfaceWrapper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace GameInv.Ws {
44
public class WebSocketConnectionInterfaceWrapper {
5-
private const string AuthenticatedString = "authenticated";
5+
private const string AuthenticatedString = "Authenticated";
66
private readonly IWebSocketConnection _socketConnection;
77

88
public WebSocketConnectionInterfaceWrapper(IWebSocketConnection socketConnection) {

server/GameInv/Ws/WsConnectionHandler.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,24 @@ private static void FailAuth(IWebSocketConnection socket) {
9797
Log.Info($"Socket {socket.ConnectionInfo.Id} failed auth");
9898
}
9999

100-
private void SendItems(WebSocketConnectionInterfaceWrapper socket) {
100+
private void SendItems(WebSocketConnectionInterfaceWrapper? socket) {
101101
var items = _gameInv.Inventory.ToList();
102102
var itemsData = items.Select(i => (ItemData)i);
103103
var serializedItems = JsonConvert.SerializeObject(itemsData);
104104
var message = EncodeMessage("items", null, serializedItems);
105-
socket.Send(message);
105+
106+
if (socket is not null) {
107+
socket.Send(message);
108+
return;
109+
}
110+
111+
foreach (var sock in _allSockets.Values) {
112+
sock.Send(message);
113+
}
106114
}
107115

108116
private void SendItems() {
109-
foreach (var socket in _allSockets.Values) {
110-
SendItems(socket);
111-
}
117+
SendItems(null);
112118
}
113119
}
114120
}

0 commit comments

Comments
 (0)