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

131 lines · 3544 bytes

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