Created
December 17, 2021 20:52
-
-
Save bryanhitc/31ab55cb808a8b7378c6bfda169e7fec to your computer and use it in GitHub Desktop.
lcu-sharp-event-example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using LCUSharp; | |
using LCUSharp.Websocket; | |
public class Program | |
{ | |
public event EventHandler<LeagueEvent> GameFlowChanged; | |
private readonly TaskCompletionSource<bool> _work = new TaskCompletionSource<bool>(false); | |
public static async Task Main(string[] args) | |
{ | |
Console.WriteLine("Running..."); | |
await new Program().EventExampleAsync(); | |
} | |
public async Task EventExampleAsync() | |
{ | |
// Initialize a connection to the league client. | |
var api = await LeagueClientApi.ConnectAsync(); | |
Console.WriteLine("Connected!"); | |
// Register game flow event. | |
GameFlowChanged += OnGameFlowChanged; | |
api.EventHandler.Subscribe("/lol-gameflow/v1/gameflow-phase", GameFlowChanged); | |
// Wait until work is complete. | |
await _work.Task; | |
Console.WriteLine("Done."); | |
} | |
private void OnGameFlowChanged(object sender, LeagueEvent e) | |
{ | |
var result = e.Data.ToString(); | |
var state = string.Empty; | |
switch (result) | |
{ | |
case "None": | |
state = "main menu"; | |
break; | |
case "Lobby": | |
state = "lobby"; | |
break; | |
case "ChampSelect": | |
state = "champ select"; | |
break; | |
case "GameStart": | |
state = "game started"; | |
break; | |
case "InProgress": | |
state = "game"; | |
break; | |
case "WaitingForStats": | |
state = "waiting for stats"; | |
break; | |
default: | |
state = $"unknown state: {result}"; | |
break; | |
} | |
// Print new state and set work to complete. | |
Console.WriteLine($"Status update: Entered {state}."); | |
_work.SetResult(true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment