Skip to content

Commit

Permalink
Sort room tickers by recently added
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiascode committed Dec 17, 2024
1 parent 55fc958 commit 104f94a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
16 changes: 12 additions & 4 deletions src/server/messages.d
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ module soulfind.server.messages;
@safe:

import soulfind.defines;
import soulfind.server.room;
import std.algorithm : sort;
import std.bitmanip : Endian, nativeToLittleEndian, read;
import std.stdio : writefln;
import std.string : lastIndexOf;
Expand Down Expand Up @@ -1106,18 +1108,24 @@ class SItemSimilarUsers : SMessage

class SRoomTicker : SMessage
{
this(string room, string[string] tickers) scope
this(string room, Ticker[] tickers) scope
{
super(RoomTicker);

writes(room);
writei(cast(uint) tickers.length);
foreach (string user, string ticker ; tickers)
foreach (ticker ; sort_timestamp(tickers))
{
writes(user);
writes(ticker);
writes(ticker.username);
writes(ticker.content);
}
}

@trusted
private auto sort_timestamp(Ticker[] tickers) scope
{
return tickers.sort!((ref a, ref b) => a.timestamp < b.timestamp);
}
}

class SRoomTickerAdd : SMessage
Expand Down
20 changes: 16 additions & 4 deletions src/server/room.d
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,21 @@ module soulfind.server.room;
import soulfind.defines;
import soulfind.server.messages;
import soulfind.server.user;
import std.datetime : Clock;

struct Ticker
{
string username;
ulong timestamp;
string content;
}

class Room
{
string name;

private User[string] user_list;
private string[string] tickers;
private Ticker[string] tickers;


this(string name)
Expand All @@ -41,7 +49,7 @@ class Room
name, user_names, statuses, speeds, upload_numbers,
shared_files, shared_folders, country_codes
);
scope tickers_msg = new SRoomTicker(name, tickers);
scope tickers_msg = new SRoomTicker(name, tickers.values);

send_to_all(joined_room_msg);
user.send_message(join_room_msg);
Expand Down Expand Up @@ -154,15 +162,19 @@ class Room
if (username !in user_list)
return;

if (username in tickers && tickers[username] == content)
if (username in tickers && tickers[username].content == content)
return;

del_ticker(username);

if (!content)
return;

tickers[username] = content;
tickers[username] = Ticker(
username,
Clock.currTime.toUnixTime,
content
);

scope msg = new SRoomTickerAdd(name, username, content);
send_to_all(msg);
Expand Down

0 comments on commit 104f94a

Please sign in to comment.