go-dota2

repository·master·Indexed 41 days ago

https://github.com/paralin/go-dota2

A Go client plugin for go-steam that provides programmatic access to Dota 2, Deadlock, and Underlords services. It uses an automated API generation process to expose Valve's underlying API to Go developers, featuring an SOCache mechanism for monitoring common objects like lobbies and parties.

Tokens
18K
Snippets
112
Records
136
Agent score
52%

What's inside go-dota2

  1. Overview of go-dota2

    master
    go-dota2 is a DOTA 2 client plugin for go-steam. It provides a Go implementation for interacting with Valve's Dota 2 services. While primarily focused on Dota 2, the project architecture also supports Deadlock and Underlords. The library uses a dynamic API generator (apigen) to convert Valve's naming conventions into a Go API, ensuring that the full Dota API is available through the codebase.
  2. How the SOCache mechanism works

    master

    The SOCache mechanism is a caching system used to watch for changes to common objects such as Lobby, LobbyInvite, Party, and PartyInvite. Because these objects are not exposed via their own dedicated events, you must use the cache to subscribe to updates.

    To use the cache:

    1. Call dota.GetCache().SubscribeType(type) to get an event channel and a cancellation function.
    2. Listen to the event channel for updates.
    3. Type-assert the Object field in the received event to the appropriate protocol type (e.g., *gcmm.CSODOTALobby).
    4. Important: Always call the returned eventCancel function when finished to prevent resource leaks.
    import (
    	gcmm "github.com/paralin/go-dota2/protocol"
    	"github.com/paralin/go-dota2/cso"
    )
    
    eventCh, eventCancel, err := dota.GetCache().SubscribeType(cso.Lobby)
    if err != nil {
        return err
    }
    
    defer eventCancel()
    
    lobbyEvent := <-eventCh
    lobby := lobbyEvent.Object.(*gcmm.CSODOTALobby)
  3. Initialize a new Dota2 client

    master

    To use the go-dota2 SDK, you must first have an existing *steam.Client instance. Use the New function to wrap this client with a Dota2 handler. This process automatically registers the Dota2 packet handlers with the Steam client's Game Coordinator (GC).

    New requires:

    • client: A pointer to an initialized *steam.Client.
    • le: A logrus.FieldLogger for logging internal events and errors.

    Note that New initializes the internal state with gcsdkm.GCConnectionStatus_GCConnectionStatus_NO_SESSION.

    import (
    	"github.com/paralin/go-steam"
    	"github.com/paralin/go-dota2"
    	"github.com/sirupsen/logrus"
    )
    
    // ... setup steam client ...
    
    logger := logrus.New()
    client := steam.NewClient() // Assuming steam client is initialized
    
    dota := dota2.New(client, logger)
  4. Subscribe to SOCache object updates

    master

    Use SubscribeType on the cache object to monitor changes to specific object types. This returns a channel of events and a cancellation function.

    eventCh, eventCancel, err := dota.GetCache().SubscribeType(cso.Lobby)
  5. Request player rank and quick stats

    master

    Retrieve competitive rankings and specific item/hero statistics.

    • RequestRank(ctx, rankType): Requests a specific rank type using protocol.ERankType.
    • RequestQuickStats(ctx, playerAccountID, heroID, itemID, leagueID): Requests quick statistics for a player, a specific hero, and a specific item within a league.
  6. Claim event actions

    master

    Use ClaimEventAction or ClaimEventActionUsingItem to claim specific event actions. Both methods await responses (*protocol.CMsgDOTAClaimEventActionResponse and *protocol.CMsgClientToGCClaimEventActionUsingItemResponse respectively).

    func (d *Dota2) ClaimEventAction(
    	ctx context.Context,
    	eventID uint32,
    	actionID uint32,
    	quantity uint32,
    	data protocol.CMsgDOTAClaimEventActionData,
    	scoreMode protocol.EEventActionScoreMode,
    	suppressRewards bool,
    ) (*protocol.CMsgDOTAClaimEventActionResponse, error)
    
    func (d *Dota2) ClaimEventActionUsingItem(
    	ctx context.Context,
    	eventID uint32,
    	actionID uint32,
    	itemID uint64,
    	quantity uint32,
    	suppressRewards bool,
    ) (*protocol.CMsgClientToGCClaimEventActionUsingItemResponse, error)
  7. Claim gated events

    master

    Use ClaimGatedEvent(ctx, eventID) to claim a gated event. This method awaits a response of type *protocol.CMsgDOTAClaimGatedEventResponse.

    func (d *Dota2) ClaimGatedEvent(
    	ctx context.Context,
    	eventID protocol.EEvent,
    ) (*protocol.CMsgDOTAClaimGatedEventResponse, error)
  8. Apply moderation to a showcase

    master

    Use ApplyModerationShowcaseModeration to apply moderation to a showcase. This method awaits a response of type *protocol.CMsgClientToGCShowcaseModerationApplyModerationResponse.

    func (d *Dota2) ApplyModerationShowcaseModeration(
    	ctx context.Context,
    	accountID uint32,
    	showcaseType protocol.EShowcaseType,
    	showcaseTimestamp uint32,
    	approve bool,
    ) (*protocol.CMsgClientToGCShowcaseModerationApplyModerationResponse, error)
  9. Interact with Bingo Dev features

    master

    The Dota2 client provides several methods for interacting with the Bingo development features. Most of these methods await a response and return a *protocol.CMsg...Response and an error.

    • SendBingoDevAddTokens: Adds tokens to a specific league and phase.
    • SendBingoDevClearInventory: Clears the inventory for a specific league.
    • SendBingoModifySquare: Modifies a specific square in the bingo grid using an action (type protocol.CMsgClientToGCBingoModifySquare_EModifyAction).
    • SendBingoShuffleCard: Shuffles the cards in a specific league and phase.
    • SendBingoShuffleCard: Shuffles the cards in a specific league and phase.
  10. Send Profile and Account Updates

    master

    Methods to update the user's visible profile information or trigger account recalibration.

    // Update profile background and featured heroes
    resp, err := dota2.SendProfileUpdate(ctx, backgroundItemID, featuredHeroIDs)
    
    // Recalibrate MMR
    resp, err := dota2.SendRecalibrateMMR(ctx)