From e7fd1967d5206a7b07a9fa50dbcccc6edad20fce Mon Sep 17 00:00:00 2001 From: Michael McCarty Date: Fri, 5 Apr 2024 11:18:38 -0700 Subject: [PATCH] feat(track): Improve tracking of token sent and received - Assign a default duration of 12h if the duration string is invalid - Add UserID tracking for sent and received tokens - Include UserID in the display of sent and received tokens - Enable removal of tokens using reactions for received tokens if not final display - Track actorUserID in ContractTokenMessage for contract token actions. --- src/boost/boost_reactions.go | 4 ++-- src/track/track.go | 20 ++++++++++++++------ src/track/track_handlers.go | 7 ++++++- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/boost/boost_reactions.go b/src/boost/boost_reactions.go index ae7b9ebd..5c89e9b9 100644 --- a/src/boost/boost_reactions.go +++ b/src/boost/boost_reactions.go @@ -158,10 +158,10 @@ func ReactionAdd(s *discordgo.Session, r *discordgo.MessageReaction) string { if r.UserID != b.UserID { // Record the Tokens as received b.TokenReceivedTime = append(b.TokenReceivedTime, time.Now()) - track.ContractTokenMessage(s, r.ChannelID, b.UserID, track.TokenReceived) + track.ContractTokenMessage(s, r.ChannelID, b.UserID, track.TokenReceived, r.UserID) // Record who sent the token - track.ContractTokenMessage(s, r.ChannelID, r.UserID, track.TokenSent) + track.ContractTokenMessage(s, r.ChannelID, r.UserID, track.TokenSent, b.UserID) contract.Boosters[r.UserID].TokenSentTime = append(contract.Boosters[r.UserID].TokenSentTime, time.Now()) } else { track.FarmedToken(s, r.ChannelID, r.UserID) diff --git a/src/track/track.go b/src/track/track.go index e95e3b4c..73fc2fba 100644 --- a/src/track/track.go +++ b/src/track/track.go @@ -29,8 +29,10 @@ type tokenValue struct { DurationTime time.Duration // Duration of Token Value time TokenSentTime []time.Time // time of each token sent TokenSentValues []float64 // time of each token sent + TokenSentUserID []string // User ID of where each token was sent to TokenReceivedTime []time.Time // time of each received token TokenReceivedValues []float64 // time of each token sent + TokenReceivedUserID []string // User ID of where each token came from FarmedTokenTime []time.Time // time a self farmed token was received SumValueSent float64 // sum of all token values sent SumValueReceived float64 // sum of all token values received @@ -72,6 +74,8 @@ func resetTokenTracking(tv *tokenValue) { tv.LinkRecieved = true tv.TokenSentTime = nil tv.TokenReceivedTime = nil + tv.TokenReceivedUserID = nil + tv.TokenSentUserID = nil tv.TokenSentValues = nil tv.TokenReceivedValues = nil tv.SumValueSent = 0.0 @@ -183,9 +187,9 @@ func getTokenTrackingString(td *tokenValue, finalDisplay bool) string { if td.Details { for i, t := range td.TokenSentTime { if !finalDisplay { - fmt.Fprintf(&builder, "> %d: %6.3f\n", i+1, t.Unix(), td.TokenSentValues[i]) + fmt.Fprintf(&builder, "> %d: %6.3f <@%s>\n", i+1, t.Unix(), td.TokenSentValues[i], td.TokenSentUserID[i]) } else { - fmt.Fprintf(&builder, "> %d: %s %6.3f\n", i+1, t.Sub(td.StartTime).Round(time.Second), td.TokenSentValues[i]) + fmt.Fprintf(&builder, "> %d: %s %6.3f <@%s>\n", i+1, t.Sub(td.StartTime).Round(time.Second), td.TokenSentValues[i], td.TokenSentUserID[i]) } if builder.Len() > 1750 { fmt.Fprint(&builder, "> ...\n") @@ -197,16 +201,16 @@ func getTokenTrackingString(td *tokenValue, finalDisplay bool) string { if td.Details { for i, t := range td.TokenReceivedTime { if !finalDisplay { - fmt.Fprintf(&builder, "> %d: %6.3f\n", i+1, t.Unix(), td.TokenReceivedValues[i]) + fmt.Fprintf(&builder, "> %d: %6.3f <@%s>\n", i+1, t.Unix(), td.TokenReceivedValues[i], td.TokenReceivedUserID[i]) } else { - fmt.Fprintf(&builder, "> %d: %s %6.3f\n", i+1, t.Sub(td.StartTime).Round(time.Second), td.TokenReceivedValues[i]) + fmt.Fprintf(&builder, "> %d: %s %6.3f <@%s>\n", i+1, t.Sub(td.StartTime).Round(time.Second), td.TokenReceivedValues[i], td.TokenReceivedUserID[i]) } if builder.Len() > 1750 { fmt.Fprint(&builder, "> ...\n") break } } - if td.LinkRecieved { + if td.LinkRecieved && !finalDisplay { fmt.Fprint(&builder, "React with 1️⃣..🔟 to remove errant received tokens at that index.\n") } } @@ -290,11 +294,13 @@ func tokenTrackingTrack(userID string, name string, tokenSent int, tokenReceived if tokenSent > 0 { td.TokenSentTime = append(td.TokenSentTime, now) td.TokenSentValues = append(td.TokenSentValues, tokenValue) + td.TokenSentUserID = append(td.TokenSentUserID, userID) // Self reported td.SumValueSent += tokenValue } if tokenReceived > 0 { td.TokenReceivedTime = append(td.TokenReceivedTime, now) td.TokenReceivedValues = append(td.TokenReceivedValues, tokenValue) + td.TokenReceivedUserID = append(td.TokenReceivedUserID, userID) // Self reported td.SumValueReceived += tokenValue } td.TokenDelta = td.SumValueSent - td.SumValueReceived @@ -375,7 +381,7 @@ func FarmedToken(s *discordgo.Session, channelID string, userID string) { } // ContractTokenMessage will track the token sent from the contract Token reaction -func ContractTokenMessage(s *discordgo.Session, channelID string, userID string, kind int) { +func ContractTokenMessage(s *discordgo.Session, channelID string, userID string, kind int, actorUserID string) { if Tokens[userID] == nil { return } @@ -388,11 +394,13 @@ func ContractTokenMessage(s *discordgo.Session, channelID string, userID string, if kind == TokenSent { v.TokenSentTime = append(v.TokenSentTime, now) v.TokenSentValues = append(v.TokenSentValues, tokenValue) + v.TokenSentUserID = append(v.TokenSentUserID, actorUserID) // Token sent to... v.SumValueSent += tokenValue redraw = true } else if v.LinkRecieved && kind == TokenReceived { v.TokenReceivedTime = append(v.TokenReceivedTime, now) v.TokenReceivedValues = append(v.TokenReceivedValues, tokenValue) + v.TokenReceivedUserID = append(v.TokenReceivedUserID, actorUserID) // Token received from... v.SumValueReceived += tokenValue redraw = true } diff --git a/src/track/track_handlers.go b/src/track/track_handlers.go index 8c1dec3d..4b695e06 100644 --- a/src/track/track_handlers.go +++ b/src/track/track_handlers.go @@ -244,13 +244,18 @@ func HandleTokenCommand(s *discordgo.Session, i *discordgo.InteractionCreate) { linkReceived := false var duration time.Duration if opt, ok := optionMap["duration"]; ok { + var err error // Timespan of the contract duration contractTimespan := strings.TrimSpace(opt.StringValue()) contractTimespan = strings.Replace(contractTimespan, "day", "d", -1) contractTimespan = strings.Replace(contractTimespan, "hr", "h", -1) contractTimespan = strings.Replace(contractTimespan, "min", "m", -1) contractTimespan = strings.Replace(contractTimespan, "sec", "s", -1) - duration, _ = str2duration.ParseDuration(contractTimespan) + duration, err = str2duration.ParseDuration(contractTimespan) + if err != nil { + // Invalid duration, just assigning a 12h + duration = 12 * time.Hour + } } var trackingName = "" if opt, ok := optionMap["name"]; ok {