Skip to content

Commit

Permalink
Add FriendCacheTest.
Browse files Browse the repository at this point in the history
Add more friend helper methods.
Fix some method overloads.
  • Loading branch information
LossyDragon committed Aug 14, 2024
1 parent 5febcab commit 24711e2
Show file tree
Hide file tree
Showing 3 changed files with 201 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import `in`.dragonbra.javasteam.protobufs.steamclient.SteammessagesClientserverF
import `in`.dragonbra.javasteam.protobufs.steamclient.SteammessagesClientserverFriends.CMsgPersonaChangeResponse
import `in`.dragonbra.javasteam.protobufs.steamclient.SteammessagesClientserverLogin
import `in`.dragonbra.javasteam.steam.handlers.steamfriends.cache.AccountCache
import `in`.dragonbra.javasteam.steam.handlers.steamfriends.cache.Clan
import `in`.dragonbra.javasteam.steam.handlers.steamfriends.cache.User
import `in`.dragonbra.javasteam.steam.handlers.steamfriends.callback.AliasHistoryCallback
import `in`.dragonbra.javasteam.steam.handlers.steamfriends.callback.ChatActionResultCallback
import `in`.dragonbra.javasteam.steam.handlers.steamfriends.callback.ChatEnterCallback
Expand Down Expand Up @@ -67,18 +69,52 @@ import `in`.dragonbra.javasteam.util.log.LogManager
import `in`.dragonbra.javasteam.util.log.Logger
import java.io.IOException
import java.nio.charset.StandardCharsets
import java.util.*

/**
* This handler handles all interaction with other users on the Steam3 network.
*/
@Suppress("unused", "MemberVisibilityCanBePrivate")
class SteamFriends : ClientMsgHandler() {

private var friendsList: MutableList<SteamID> = mutableListOf()
private var clanList: MutableList<SteamID> = mutableListOf()
var friendsList: MutableList<SteamID> = mutableListOf()
private set
var clanList: MutableList<SteamID> = mutableListOf()
private set

private var cache: AccountCache = AccountCache()

/**
* Gets a list of all caches users.
*
* @return a list of [User]
*/
fun getCachedUsers(): List<User> = cache.users.getList()

/**
* Gets a list of all cached clans.
*
* @return a list of [Clan]
*/
fun getCachedClans(): List<Clan> = cache.clans.getList()

/**
* Gets result if the given steam ID is the local user or not.
*
* @param steamID the steam ID of the local logged-in user
* @return true if the account is the local user, otherwise false.
*/
@JvmOverloads
fun isLocalUser(steamID: SteamID? = null): Boolean = cache.isLocalUser(steamID ?: client.steamID)

/**
* Gets the steam ID from the cached account.
*
* @param steamID the steam ID to check the cache
* @return The [SteamID] of the cached user.
*/
fun getFriendSteamID(steamID: SteamID): SteamID = cache.getUser(steamID).steamID // Why not...

/**
* Gets the local user's persona name. Will be null before user initialization.
* User initialization is performed prior to [AccountInfoCallback] callback.
Expand All @@ -87,6 +123,13 @@ class SteamFriends : ClientMsgHandler() {
*/
fun getPersonaName(): String? = cache.localUser.name

/**
* Gets the local user's persona avatar hash. Will be null before user initialization.
*
* @return The avatar hash.
*/
fun getPersonaAvatar(): ByteArray? = cache.localUser.avatarHash

/**
* Sets the local user's persona name and broadcasts it over the network.
* Results are returned in a [PersonaChangeCallback] callback.
Expand Down Expand Up @@ -230,6 +273,23 @@ class SteamFriends : ClientMsgHandler() {
*/
fun getFriendAvatar(steamID: SteamID): ByteArray? = cache.getUser(steamID).avatarHash

/**
* Gets the PersonaState Flags of a friend.
*
* @param steamID The steam id.
* @return and [EnumSet] of [EPersonaStateFlag]
*/
fun getFriendPersonaStateFlags(steamID: SteamID): EnumSet<EPersonaStateFlag>? =
cache.getUser(steamID).personaStateFlags

/**
* Gets the game app id of a friend.
*
* @param steamID The steam id.
* @return the game app id or 0 if not playing.
*/
fun getFriendGameAppId(steamID: SteamID): Int = cache.getUser(steamID).gameAppID

/**
* Gets the count of clans the local user is a member of.
* @return The number of clans this user is a member of.
Expand Down Expand Up @@ -467,7 +527,8 @@ class SteamFriends : ClientMsgHandler() {
* @param steamIdList A list of SteamIDs to request the info of.
* @param requestedInfo The requested info flags. If none specified, this uses [SteamConfiguration.getDefaultPersonaStateFlags].
*/
fun requestFriendInfo(steamIdList: List<SteamID>, requestedInfo: Int) {
@JvmOverloads
fun requestFriendInfo(steamIdList: List<SteamID>, requestedInfo: Int = 0) {
var info = requestedInfo

if (info == 0) {
Expand All @@ -490,7 +551,8 @@ class SteamFriends : ClientMsgHandler() {
* @param steamID A SteamID to request the info of.
* @param requestedInfo The requested info flags. If none specified, this uses [SteamConfiguration.getDefaultPersonaStateFlags].
*/
fun requestFriendInfo(steamID: SteamID, requestedInfo: Int) {
@JvmOverloads
fun requestFriendInfo(steamID: SteamID, requestedInfo: Int = 0) {
requestFriendInfo(listOf(steamID), requestedInfo)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ class AccountList<T : Account>(private val clazz: Class<T>) : ConcurrentHashMap<
steamID = id
}
}

/**
* Get all the values in the HashMap.
*
* @return a list of either [User] or [Clan].
*/
fun getList(): List<T> = values.toList()
}

@Suppress("MemberVisibilityCanBePrivate")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package in.dragonbra.javasteam.steam.handlers.steamfriends;

import com.google.protobuf.ByteString;
import in.dragonbra.javasteam.base.ClientMsgProtobuf;
import in.dragonbra.javasteam.base.PacketClientMsgProtobuf;
import in.dragonbra.javasteam.enums.*;
import in.dragonbra.javasteam.protobufs.steamclient.SteammessagesClientserverFriends;
import in.dragonbra.javasteam.steam.handlers.HandlerTestBase;
import in.dragonbra.javasteam.types.GameID;
import in.dragonbra.javasteam.types.SteamID;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;

import java.io.IOException;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;

// Maybe some cold snowy day this can be condensed down into some test packets.

/**
* @author Lossy
* @since 2024-08-14
*/
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
public class FriendCacheTest extends HandlerTestBase<SteamFriends> {

@Override
protected SteamFriends createHandler() {
return new SteamFriends();
}

@Test
public void verifyLocalUser() throws IOException {
var sid = steamClient.getSteamID();
var avatarHash = "fef49e7fa7e1997310d705b2a6158ff8dc1cdfeb";

var friendsListMsg = getPacket(EMsg.ClientFriendsList, true);
handler.handleMsg(friendsListMsg);

sid.setAccountType(EAccountType.Individual);

var localUser = SteammessagesClientserverFriends.CMsgClientPersonaState.Friend.newBuilder();
localUser.setAvatarHash(ByteString.copyFromUtf8(avatarHash));
localUser.setFriendid(sid.convertToUInt64());
localUser.setGameName("Team Fortress 2");
localUser.setGameid(440);
localUser.setGamePlayedAppId(440);
localUser.setPersonaState(EPersonaState.Online.code());
localUser.setPlayerName("testpersonaname");
localUser.setPersonaStateFlags(
EPersonaStateFlag.code(
EnumSet.of(EPersonaStateFlag.InJoinableGame, EPersonaStateFlag.ClientTypeMobile)
)
);

var personaState = new ClientMsgProtobuf<SteammessagesClientserverFriends.CMsgClientPersonaState.Builder>(
SteammessagesClientserverFriends.CMsgClientPersonaState.class,
EMsg.ClientPersonaState
);
personaState.getBody().setStatusFlags(
EClientPersonaStateFlag.code(
EnumSet.of(EClientPersonaStateFlag.PlayerName, EClientPersonaStateFlag.Presence, EClientPersonaStateFlag.GameDataBlob)
)
);
personaState.getBody().addFriends(localUser.build());

handler.handleMsg(new PacketClientMsgProtobuf(EMsg.ClientPersonaState, personaState.serialize()));

// AccountCache
Assertions.assertTrue(handler.isLocalUser());

// Account
Assertions.assertEquals(sid, handler.getFriendSteamID(sid));
Assertions.assertEquals("testpersonaname", handler.getPersonaName());
Assertions.assertEquals(avatarHash, new String((handler.getPersonaAvatar())));

// User
Assertions.assertNull(handler.getFriendRelationship(sid));
Assertions.assertEquals(EPersonaState.Online, handler.getFriendPersonaState(sid));
Assertions.assertEquals(EnumSet.of(EPersonaStateFlag.InJoinableGame, EPersonaStateFlag.ClientTypeMobile), handler.getFriendPersonaStateFlags(sid));
Assertions.assertEquals(440, handler.getFriendGameAppId(sid));
Assertions.assertEquals(new GameID(440), handler.getFriendGamePlayed(sid));
Assertions.assertEquals("Team Fortress 2", handler.getFriendGamePlayedName(sid));
}

@Test
public void verifyCachedFriends() throws IOException {
var msg = new ClientMsgProtobuf<SteammessagesClientserverFriends.CMsgClientFriendsList.Builder>(
SteammessagesClientserverFriends.CMsgClientFriendsList.class,
EMsg.ClientFriendsList
);
msg.getBody().setBincremental(false);

List<SteammessagesClientserverFriends.CMsgClientFriendsList.Friend> list = new ArrayList<>();

for (int idx = 0; idx < 10; idx++) {
var friendid = new SteamID(1234 + idx);
friendid.setAccountType(EAccountType.Individual);

var friend = SteammessagesClientserverFriends.CMsgClientFriendsList.Friend.newBuilder();
friend.setUlfriendid(friendid.convertToUInt64());
friend.setEfriendrelationship(EFriendRelationship.Friend.code());

list.add(friend.build());
}

msg.getBody().addAllFriends(list);

handler.handleMsg(new PacketClientMsgProtobuf(EMsg.ClientFriendsList, msg.serialize()));

Assertions.assertEquals(10, handler.getCachedUsers().size());
Assertions.assertEquals(10, handler.getFriendsList().size());

var sid2 = new SteamID(1236);
sid2.setAccountType(EAccountType.Individual);
Assertions.assertEquals(sid2, handler.getFriendSteamID(sid2));
Assertions.assertEquals(sid2, handler.getFriendByIndex(2));
Assertions.assertEquals(EFriendRelationship.Friend, handler.getFriendRelationship(sid2));
}

// TODO clan testing
}

0 comments on commit 24711e2

Please sign in to comment.