Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
217 additions
and
31 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package eventview | ||
|
||
import ( | ||
"darkbot/app/settings/logus" | ||
"darkbot/app/settings/types" | ||
"darkbot/app/viewer/apis" | ||
"darkbot/app/viewer/views" | ||
_ "embed" | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
"time" | ||
) | ||
|
||
type EventRenderer struct { | ||
main views.TemplateShared | ||
api *apis.API | ||
} | ||
|
||
func NewEventRenderer(api *apis.API) *EventRenderer { | ||
base := EventRenderer{} | ||
base.api = api | ||
base.main.Header = "#darkbot-event-view" | ||
return &base | ||
} | ||
|
||
func (t *EventRenderer) DiscoverMessageID(content string, msgID types.DiscordMessageID) { | ||
if strings.Contains(content, t.main.Header) { | ||
t.main.MessageID = msgID | ||
} | ||
} | ||
|
||
func (t *EventRenderer) MatchMessageID(messageID types.DiscordMessageID) bool { | ||
return messageID == t.main.MessageID | ||
} | ||
|
||
func (t *EventRenderer) Send() { | ||
t.main.Send(t.api) | ||
} | ||
|
||
type PlayerTemplate struct { | ||
Time string | ||
Name string | ||
System string | ||
} | ||
|
||
func (t *EventRenderer) Render() error { | ||
record, err := t.api.Scrappy.GetPlayerStorage().GetLatestRecord() | ||
if logus.CheckWarn(err, "unable to get players") { | ||
return err | ||
} | ||
|
||
logus.Debug("rendered events", logus.DiscordMessageID(t.main.MessageID)) | ||
|
||
eventTags, err := t.api.Players.Events.TagsList(t.api.ChannelID) | ||
logus.CheckWarn(err, "failed to acquire player event list", logus.ChannelID(t.api.ChannelID)) | ||
|
||
if len(eventTags) > 0 { | ||
var sb strings.Builder | ||
|
||
sb.WriteString(fmt.Sprintf("**%s** %s\n", t.main.Header, time.Now().String())) | ||
sb.WriteString("**Event table of players**\n") | ||
sb.WriteString("```json\n") | ||
|
||
for _, eventTag := range eventTags { | ||
sb.WriteString(fmt.Sprintf(`"%s": `, eventTag)) | ||
|
||
matchedPlayers := []PlayerTemplate{} | ||
for _, player := range record.List { | ||
if views.TagContains(player.Name, []types.Tag{eventTag}) { | ||
matchedPlayers = append(matchedPlayers, PlayerTemplate{Name: player.Name, Time: player.Time, System: player.System}) | ||
continue | ||
} | ||
} | ||
result, err := json.Marshal(matchedPlayers) | ||
logus.CheckError(err, "failed to marshal event matched players") | ||
sb.WriteString(fmt.Sprintf("%s", string(result))) | ||
|
||
sb.WriteString("\n") | ||
} | ||
|
||
sb.WriteString("```\n") | ||
t.main.Content = sb.String() | ||
|
||
} | ||
|
||
return nil | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package eventview | ||
|
||
import ( | ||
"darkbot/app/configurator" | ||
"darkbot/app/scrappy" | ||
"darkbot/app/scrappy/player" | ||
"darkbot/app/scrappy/shared/records" | ||
"darkbot/app/settings/logus" | ||
"darkbot/app/settings/types" | ||
"darkbot/app/viewer/apis" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestPlayerEvent(t *testing.T) { | ||
configurator.FixtureMigrator(func(dbpath types.Dbpath) { | ||
channelID, _ := configurator.FixtureChannel(dbpath) | ||
|
||
configurator.NewConfiguratorPlayerEvent(configurator.NewConfigurator(dbpath)).TagsAdd(channelID, []types.Tag{"player1", "player2"}...) | ||
|
||
players := player.NewPlayerStorage(player.FixturePlayerAPIMock()) | ||
storage := scrappy.FixtureMockedStorage(scrappy.WithPlayerStorage(players)) | ||
api := apis.NewAPI(channelID, dbpath, apis.WithStorage(storage)) | ||
record := records.NewStampedObjects[player.Player]() | ||
record.Add(player.Player{Name: "player1", System: "system1", Region: "region1"}) | ||
record.Add(player.Player{Name: "player2", System: "system2", Region: "region2"}) | ||
record.Add(player.Player{Name: "player3", System: "system3", Region: "region3"}) | ||
record.Add(player.Player{Name: "player4", System: "system4", Region: "region4"}) | ||
players.Add(record) | ||
|
||
playerView := NewEventRenderer(api) | ||
playerView.Render() | ||
logus.Debug(playerView.main.Content) | ||
logus.Debug("test TestPlayerEvent is finished") | ||
|
||
assert.NotEmpty(t, playerView.main.Content) | ||
}) | ||
} |
Oops, something went wrong.