Skip to content

Commit

Permalink
Simplify the package readme
Browse files Browse the repository at this point in the history
  • Loading branch information
sliekens committed Oct 19, 2024
1 parent 07582e3 commit 208ccd5
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 146 deletions.
1 change: 1 addition & 0 deletions .markdownlint.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"first-line-h1": false,
"no-inline-html": {
"allowed_elements": [
"details",
Expand Down
83 changes: 43 additions & 40 deletions GW2SDK/PACKAGE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
## About

GW2SDK provides classes for interacting with the Guild Wars 2 API and game client. This package enables you to fetch information about the game, the player's account, PvP seasons, WvW matches and the in-game economy. It also provides realtime information from the game client such as the player's current character and the current map.
GW2SDK provides classes for interacting with the Guild Wars 2 API and game client.
This package enables you to fetch information about the game, the player's account,
PvP seasons, WvW matches and the in-game economy. It also provides realtime information
from the game client such as the player's current character and the current map.

## Key features

Expand All @@ -9,72 +12,63 @@ GW2SDK provides classes for interacting with the Guild Wars 2 API and game clien

## How to use

You can use the `Gw2Client` for many different purposes, such as fetching the current prices of all tradable items in the game, or the `GameLink` to receive realtime information from the game client. Below are some examples of how to use the `Gw2Client` and `GameLink` to fetch information about the game and the player's account.
API access using `Gw2Client`:

``` csharp
using System;
using System.Net.Http;
using System.Threading.Tasks;
using GuildWars2;
using GuildWars2.Commerce.Prices;
using GuildWars2.Items;

namespace PackageReadme;
namespace PackageReadme.Gw2ClientProgram;

internal class Gw2ClientProgram
internal class Program
{
public static async Task Main(string[] args)
{
// Create an instance of the Gw2Client, which depends on HttpClient
using var httpClient = new HttpClient();
var gw2 = new Gw2Client(httpClient);

// Print a table header
PrintTableHeader();

// Fetch the current prices of all items
await foreach (var itemPrice in gw2.Commerce.GetItemPricesBulk().ValueOnly())
{
// The item price contains the item's ID, which can be used to fetch the item's name
// The item price contains the item's ID, which can be used to fetch
// the item's name
var item = await gw2.Items.GetItemById(itemPrice.Id).ValueOnly();

// Print the item's name and its current highest buyer and lowest seller
PrintTableRow(item.Name, itemPrice.BestBid, itemPrice.BestAsk);
}

void PrintTableHeader()
{
/*
================================================================================================================================================================
| Item | Highest buyer | Lowest seller |
================================================================================================================================================================
*/
Console.WriteLine(new string('=', 160));
Console.WriteLine($"| {"Item",-50} | {"Highest buyer",-50} | {"Lowest seller",-50} |");
Console.WriteLine(new string('=', 160));
PrintItem(item, itemPrice);
}
}

void PrintTableRow(string item, Coin highestBuyer, Coin lowestSeller)
{
/*
| <item> | <highestBuyer> | <lowestSeller> |
*/
Console.WriteLine($"| {item,-50} | {highestBuyer,-50} | {lowestSeller,-50} |");
}
private static void PrintItem(Item item, ItemPrice price)
{
Console.WriteLine($"{"Item",-15}: {item.Name}");
Console.WriteLine($"{"Highest buyer",-15}: {price.BestBid}");
Console.WriteLine($"{"Lowest seller",-15}: {price.BestAsk}");
Console.WriteLine($"{"Bid-ask spread",-15}: {price.BidAskSpread}");
Console.WriteLine();
}
}

```

The `GameLink` can be used to receive realtime information from the game client. Below is an example of how to use the `GameLink` to receive information about the player's current character and the current map.
Game client access using `GameLink`:

(This example also requires the System.Reactive package to be installed.)

``` csharp
using System;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using GuildWars2;

namespace PackageReadme;
namespace PackageReadme.GameLinkProgram;

internal class GameLinkProgram
internal class Program
{
public static async Task Main(string[] args)
{
Expand All @@ -84,16 +78,19 @@ internal class GameLinkProgram
return;
}

Console.WriteLine("GameLink is starting! (Ensure the game is running and that you are loaded into a map.)");
Console.WriteLine("GameLink is starting! (Ensure the game is running"
+ " and that you are loaded into a map.)");

// Pre-fetch all maps from the API, they are used to display the player's current map
// Pre-fetch all maps from the API, they are used to display the player's
// current map
using var http = new HttpClient();
var gw2 = new Gw2Client(http);
var maps = await gw2.Exploration.GetMapSummaries()
.AsDictionary(map => map.Id)
.ValueOnly();

// Choose an interval to indicate how often you want to receive fresh data from the game
// Choose an interval to indicate how often you want to receive fresh data
// from the game
// For example, at most once every second
// Default: no limit, every change in the game state will be available immediately
var refreshInterval = TimeSpan.FromSeconds(1);
Expand All @@ -105,13 +102,16 @@ internal class GameLinkProgram
var subscription = gameLink.Subscribe(
tick =>
{
// Each 'tick' contains information about the player's character and actions, among other things
// Each 'tick' contains information about the player's character
// and actions, among other things
var player = tick.GetIdentity();

// The identity can be missing due to JSON errors, always check for null
// The identity can be missing due to JSON errors, always check
// for null
if (player != null)
{
// Use the player's map ID to find the map name in the pre-fetched list of maps
// Use the player's map ID to find the map name in the
// pre-fetched list of maps
var map = maps[player.MapId];

// Print the player's name and current map
Expand All @@ -132,6 +132,7 @@ internal class GameLinkProgram
gameLink.Dispose();
}
}

```

## Additional documentation
Expand All @@ -141,5 +142,7 @@ internal class GameLinkProgram

## Feedback & contributing

GW2SDK is released as open source under the MIT license. You are welcome to create an issue if you find something is missing or broken, or a discussion for other feedback, questions or ideas.
Check out the GitHub [page](https://github.com/sliekens/gw2sdk) to find more ways to contribute.
GW2SDK is released as open source under the MIT license. You are welcome to create
an issue if you find something is missing or broken, or a discussion for other
feedback, questions or ideas. Check out the GitHub [page](https://github.com/sliekens/gw2sdk)
to find more ways to contribute.
24 changes: 17 additions & 7 deletions gw2sdk.sln
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PollyUsage", "samples\Polly
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Translations", "samples\Translations\Translations.csproj", "{40937B58-1FFE-4B5B-A918-09F2DDEA2BF1}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PackageReadme", "samples\PackageReadme\PackageReadme.csproj", "{69FC5CC8-8365-4AB1-A379-5BC7E0796207}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GW2SDK.Profiling", "GW2SDK.Profiling\GW2SDK.Profiling.csproj", "{787D05A8-12E5-4124-90A2-FD30F6418900}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "PackageReadme", "PackageReadme", "{43B82868-E1D7-4B37-834B-0A7E2C64E8EC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GameLinkProgram", "samples\PackageReadme\GameLinkProgram\GameLinkProgram.csproj", "{32143300-94DA-4DBE-A5AC-FAAC81429608}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Gw2ClientProgram", "samples\PackageReadme\Gw2ClientProgram\Gw2ClientProgram.csproj", "{637E73C7-324B-4D60-89EB-CE36C1F56BC8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -81,14 +85,18 @@ Global
{40937B58-1FFE-4B5B-A918-09F2DDEA2BF1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{40937B58-1FFE-4B5B-A918-09F2DDEA2BF1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{40937B58-1FFE-4B5B-A918-09F2DDEA2BF1}.Release|Any CPU.Build.0 = Release|Any CPU
{69FC5CC8-8365-4AB1-A379-5BC7E0796207}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{69FC5CC8-8365-4AB1-A379-5BC7E0796207}.Debug|Any CPU.Build.0 = Debug|Any CPU
{69FC5CC8-8365-4AB1-A379-5BC7E0796207}.Release|Any CPU.ActiveCfg = Release|Any CPU
{69FC5CC8-8365-4AB1-A379-5BC7E0796207}.Release|Any CPU.Build.0 = Release|Any CPU
{787D05A8-12E5-4124-90A2-FD30F6418900}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{787D05A8-12E5-4124-90A2-FD30F6418900}.Debug|Any CPU.Build.0 = Debug|Any CPU
{787D05A8-12E5-4124-90A2-FD30F6418900}.Release|Any CPU.ActiveCfg = Release|Any CPU
{787D05A8-12E5-4124-90A2-FD30F6418900}.Release|Any CPU.Build.0 = Release|Any CPU
{32143300-94DA-4DBE-A5AC-FAAC81429608}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{32143300-94DA-4DBE-A5AC-FAAC81429608}.Debug|Any CPU.Build.0 = Debug|Any CPU
{32143300-94DA-4DBE-A5AC-FAAC81429608}.Release|Any CPU.ActiveCfg = Release|Any CPU
{32143300-94DA-4DBE-A5AC-FAAC81429608}.Release|Any CPU.Build.0 = Release|Any CPU
{637E73C7-324B-4D60-89EB-CE36C1F56BC8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{637E73C7-324B-4D60-89EB-CE36C1F56BC8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{637E73C7-324B-4D60-89EB-CE36C1F56BC8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{637E73C7-324B-4D60-89EB-CE36C1F56BC8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -102,7 +110,9 @@ Global
{F63A66C7-5520-4265-9F28-64DCCD1A6AA8} = {816817BB-0465-4D44-89A6-6CB64A1FF589}
{0BA80C1D-40A9-4B20-9558-82C0AA58BC1F} = {816817BB-0465-4D44-89A6-6CB64A1FF589}
{40937B58-1FFE-4B5B-A918-09F2DDEA2BF1} = {816817BB-0465-4D44-89A6-6CB64A1FF589}
{69FC5CC8-8365-4AB1-A379-5BC7E0796207} = {816817BB-0465-4D44-89A6-6CB64A1FF589}
{43B82868-E1D7-4B37-834B-0A7E2C64E8EC} = {816817BB-0465-4D44-89A6-6CB64A1FF589}
{32143300-94DA-4DBE-A5AC-FAAC81429608} = {43B82868-E1D7-4B37-834B-0A7E2C64E8EC}
{637E73C7-324B-4D60-89EB-CE36C1F56BC8} = {43B82868-E1D7-4B37-834B-0A7E2C64E8EC}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {30CA8636-682D-4E26-943C-45133727C406}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
<StartupObject>PackageReadme.GameLinkProgram</StartupObject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Reactive" />
<ProjectReference Include="..\..\..\GW2SDK\GW2SDK.csproj" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\GW2SDK\GW2SDK.csproj" />
<PackageReference Include="System.Reactive" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
using System.Threading.Tasks;
using GuildWars2;

namespace PackageReadme;
namespace PackageReadme.GameLinkProgram;

internal class GameLinkProgram
internal class Program
{
public static async Task Main(string[] args)
{
Expand All @@ -15,16 +15,19 @@ public static async Task Main(string[] args)
return;
}

Console.WriteLine("GameLink is starting! (Ensure the game is running and that you are loaded into a map.)");
Console.WriteLine("GameLink is starting! (Ensure the game is running"
+ " and that you are loaded into a map.)");

// Pre-fetch all maps from the API, they are used to display the player's current map
// Pre-fetch all maps from the API, they are used to display the player's
// current map
using var http = new HttpClient();
var gw2 = new Gw2Client(http);
var maps = await gw2.Exploration.GetMapSummaries()
.AsDictionary(static map => map.Id)
.AsDictionary(map => map.Id)
.ValueOnly();

// Choose an interval to indicate how often you want to receive fresh data from the game
// Choose an interval to indicate how often you want to receive fresh data
// from the game
// For example, at most once every second
// Default: no limit, every change in the game state will be available immediately
var refreshInterval = TimeSpan.FromSeconds(1);
Expand All @@ -36,13 +39,16 @@ public static async Task Main(string[] args)
var subscription = gameLink.Subscribe(
tick =>
{
// Each 'tick' contains information about the player's character and actions, among other things
// Each 'tick' contains information about the player's character
// and actions, among other things
var player = tick.GetIdentity();

// The identity can be missing due to JSON errors, always check for null
// The identity can be missing due to JSON errors, always check
// for null
if (player != null)
{
// Use the player's map ID to find the map name in the pre-fetched list of maps
// Use the player's map ID to find the map name in the
// pre-fetched list of maps
var map = maps[player.MapId];

// Print the player's name and current map
Expand Down
File renamed without changes.
49 changes: 0 additions & 49 deletions samples/PackageReadme/Gw2ClientProgram.cs

This file was deleted.

14 changes: 14 additions & 0 deletions samples/PackageReadme/Gw2ClientProgram/Gw2ClientProgram.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\GW2SDK\GW2SDK.csproj" />
</ItemGroup>

</Project>
Loading

0 comments on commit 208ccd5

Please sign in to comment.