Skip to content

Commit

Permalink
Update Unified Messages sample
Browse files Browse the repository at this point in the history
  • Loading branch information
affederaffe committed Sep 27, 2024
1 parent 1ebbf4b commit 0ede147
Showing 1 changed file with 14 additions and 37 deletions.
51 changes: 14 additions & 37 deletions Samples/013_UnifiedMessages/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
// through the connection to steam
//

var badgeRequest = JobID.Invalid;

if ( args.Length < 2 )
{
Console.WriteLine( "Sample8: No username and password specified!" );
Expand All @@ -39,7 +37,7 @@
var steamUnifiedMessages = steamClient.GetHandler<SteamUnifiedMessages>();

// we also want to create our local service interface, which will help us build requests to the unified api
var playerService = steamUnifiedMessages.CreateService<IPlayer>();
var playerService = steamUnifiedMessages.CreateService<Player>();


// register a few callbacks we're interested in
Expand All @@ -51,9 +49,6 @@
manager.Subscribe<SteamUser.LoggedOnCallback>( OnLoggedOn );
manager.Subscribe<SteamUser.LoggedOffCallback>( OnLoggedOff );

// we use the following callbacks for unified service responses
manager.Subscribe<SteamUnifiedMessages.ServiceMethodResponse>( OnMethodResponse );

var isRunning = true;

Console.WriteLine( "Connecting to Steam..." );
Expand Down Expand Up @@ -86,7 +81,7 @@ void OnDisconnected( SteamClient.DisconnectedCallback callback )
isRunning = false;
}

void OnLoggedOn( SteamUser.LoggedOnCallback callback )
async void OnLoggedOn( SteamUser.LoggedOnCallback callback )
{
if ( callback.Result != EResult.OK )
{
Expand Down Expand Up @@ -119,49 +114,31 @@ void OnLoggedOn( SteamUser.LoggedOnCallback callback )
appid = 440,
};

// now lets send the request, this is done by building an expression tree with the IPlayer interface
badgeRequest = playerService.SendMessage( x => x.GetGameBadgeLevels( req ) );
// now lets send the request and await for the response
var response = await playerService.GetGameBadgeLevels( req );

// alternatively, the request can be made using SteamUnifiedMessages directly, but then you must build the service request name manually
// the name format is in the form of <Service>.<Method>#<Version>
steamUnifiedMessages.SendMessage( "Player.GetGameBadgeLevels#1", req );
}
response = await steamUnifiedMessages.SendMessage<CPlayer_GetGameBadgeLevels_Request, CPlayer_GetGameBadgeLevels_Response>( "Player.GetGameBadgeLevels#1", req );

static void OnLoggedOff( SteamUser.LoggedOffCallback callback )
{
Console.WriteLine( "Logged off of Steam: {0}", callback.Result );
}

void OnMethodResponse( SteamUnifiedMessages.ServiceMethodResponse callback )
{
if ( callback.JobID != badgeRequest )
if ( response.Result != EResult.OK )
{
// always double check the jobid of the response to ensure you're matching to your original request
Console.WriteLine( $"Unified service request failed with {response.Result}" );
return;
}

// and check for success
if ( callback.Result != EResult.OK )
{
Console.WriteLine( $"Unified service request failed with {callback.Result}" );
return;
}

// retrieve the deserialized response for the request we made
// notice the naming pattern
// for requests: CMyService_Method_Request
// for responses: CMyService_Method_Response
CPlayer_GetGameBadgeLevels_Response resp = callback.GetDeserializedResponse<CPlayer_GetGameBadgeLevels_Response>();
Console.WriteLine( $"Our player level is {response.MessageBody.player_level}" );

Console.WriteLine( $"Our player level is {resp.player_level}" );

foreach ( var badge in resp.badges )
foreach ( var badge in response.MessageBody.badges )
{
Console.WriteLine( $"Badge series {badge.series} is level {badge.level}" );
}

badgeRequest = JobID.Invalid;

// now that we've completed our task, lets log off
steamUser.LogOff();
}

static void OnLoggedOff( SteamUser.LoggedOffCallback callback )
{
Console.WriteLine( "Logged off of Steam: {0}", callback.Result );
}

0 comments on commit 0ede147

Please sign in to comment.