vendor/github.com/hugolgst/rich-go/client/inputMapper.go

115 lines · 2964 bytes

  1package client
  2
  3import (
  4	"time"
  5)
  6
  7// Activity holds the data for discord rich presence
  8type Activity struct {
  9	// What the player is currently doing
 10	Details string
 11	// The user's current party status
 12	State string
 13	// The id for a large asset of the activity, usually a snowflake
 14	LargeImage string
 15	// Text displayed when hovering over the large image of the activity
 16	LargeText string
 17	// The id for a small asset of the activity, usually a snowflake
 18	SmallImage string
 19	// Text displayed when hovering over the small image of the activity
 20	SmallText string
 21	// Information for the current party of the player
 22	Party *Party
 23	// Unix timestamps for start and/or end of the game
 24	Timestamps *Timestamps
 25	// Secrets for Rich Presence joining and spectating
 26	Secrets *Secrets
 27	// Clickable buttons that open a URL in the browser
 28	Buttons []*Button
 29}
 30
 31// Button holds a label and the corresponding URL that is opened on press
 32type Button struct {
 33	// The label of the button
 34	Label string
 35	// The URL of the button
 36	Url string
 37}
 38
 39// Party holds information for the current party of the player
 40type Party struct {
 41	// The ID of the party
 42	ID string
 43	// Used to show the party's current size
 44	Players int
 45	// Used to show the party's maximum size
 46	MaxPlayers int
 47}
 48
 49// Timestamps holds unix timestamps for start and/or end of the game
 50type Timestamps struct {
 51	// unix time (in milliseconds) of when the activity started
 52	Start *time.Time
 53	// unix time (in milliseconds) of when the activity ends
 54	End *time.Time
 55}
 56
 57// Secrets holds secrets for Rich Presence joining and spectating
 58type Secrets struct {
 59	// The secret for a specific instanced match
 60	Match string
 61	// The secret for joining a party
 62	Join string
 63	// The secret for spectating a game
 64	Spectate string
 65}
 66
 67func mapActivity(activity *Activity) *PayloadActivity {
 68	final := &PayloadActivity{
 69		Details: activity.Details,
 70		State:   activity.State,
 71		Assets: PayloadAssets{
 72			LargeImage: activity.LargeImage,
 73			LargeText:  activity.LargeText,
 74			SmallImage: activity.SmallImage,
 75			SmallText:  activity.SmallText,
 76		},
 77	}
 78
 79	if activity.Timestamps != nil && activity.Timestamps.Start != nil {
 80		start := uint64(activity.Timestamps.Start.UnixNano() / 1e6)
 81		final.Timestamps = &PayloadTimestamps{
 82			Start: &start,
 83		}
 84		if activity.Timestamps.End != nil {
 85			end := uint64(activity.Timestamps.End.UnixNano() / 1e6)
 86			final.Timestamps.End = &end
 87		}
 88	}
 89
 90	if activity.Party != nil {
 91		final.Party = &PayloadParty{
 92			ID:   activity.Party.ID,
 93			Size: [2]int{activity.Party.Players, activity.Party.MaxPlayers},
 94		}
 95	}
 96
 97	if activity.Secrets != nil {
 98		final.Secrets = &PayloadSecrets{
 99			Join:     activity.Secrets.Join,
100			Match:    activity.Secrets.Match,
101			Spectate: activity.Secrets.Spectate,
102		}
103	}
104
105	if len(activity.Buttons) > 0 {
106		for _, btn := range activity.Buttons {
107			final.Buttons = append(final.Buttons, &PayloadButton{
108				Label: btn.Label,
109				Url:   btn.Url,
110			})
111		}
112	}
113
114	return final
115}