Skip to content

Commit

Permalink
feat: fish improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
BridgeSenseDev committed Feb 4, 2025
1 parent bf5eb9c commit 74a576b
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 14 deletions.
4 changes: 3 additions & 1 deletion config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@
"fish": {
"state": false,
"delay": 30,
"sellCoinsValue": 2500000,
"fishOnly": false,
"autoCompleteEvents": true,
"sellCoinsValue": 100000,
"fishLocation": [
"Underwater Sanctuary",
"Wily River",
Expand Down
6 changes: 4 additions & 2 deletions config/commandsConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,10 @@ const (

type FishCommandConfig struct {
GeneralCommandConfig
SellCoinsValue int `json:"sellCoinsValue"`
FishLocation []FishLocation `json:"fishLocation"`
FishOnly bool `json:"fishOnly"`
AutoCompleteEvents bool `json:"autoCompleteEvents"`
SellCoinsValue int `json:"sellCoinsValue"`
FishLocation []FishLocation `json:"fishLocation"`
}

type StreamCommandConfig struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ export interface Delays {
export interface FishCommandConfig {
"state": boolean;
"delay": number;
"fishOnly": boolean;
"autoCompleteEvents": boolean;
"sellCoinsValue": number;
"fishLocation": FishLocation[] | null;
}
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/lib/state.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ class Cfg {
constructor() {
$effect.root(() => {
$effect(() => {
UpdateConfig(this.c);
if (this.c.discordStatus !== "") {
UpdateConfig(this.c);
}
});
});

Expand Down
66 changes: 63 additions & 3 deletions frontend/src/routes/commands/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,58 @@
(String(enumName).charAt(0).toUpperCase() +
String(enumName).slice(1)) as keyof typeof enumMap
];
console.log(Object.values(enumObject));
return Object.values(enumObject).filter((v) => typeof v === "string" && v !== "");
return Object.values(enumObject).filter((v) => v !== "");
}
let fishOnlyEnabled = $state(false);
let backupCommandsState: Record<string, boolean> = {};
$effect(() => {
if (commands.fish.state && commands.fish.fishOnly) {
fishOnlyEnabled = true;
}
});
function toggle(option: string, value: boolean | Event) {
if (value instanceof Event) {
value = (value.target as HTMLInputElement).checked;
}
if (option === "fishOnly") {
fishOnlyEnabled = value;
if (value) {
for (const command in commands) {
if (command !== "fish") {
backupCommandsState[command] = commands[command].state;
commands[command].state = false;
}
}
} else {
for (const command in backupCommandsState) {
if (command !== "fish") {
commands[command].state = backupCommandsState[command] ?? commands[command].state;
}
}
}
} else if (option === "fish") {
if (!value && fishOnlyEnabled) {
fishOnlyEnabled = false;
for (const command in backupCommandsState) {
if (command !== "fish") {
commands[command].state = backupCommandsState[command] ?? commands[command].state;
}
}
} else if (value && commands.fish.fishOnly) {
fishOnlyEnabled = true;
for (const command in commands) {
if (command !== "fish") {
backupCommandsState[command] = commands[command].state;
commands[command].state = false;
}
}
}
}
}
</script>

Expand All @@ -85,7 +135,16 @@
<Card.Header>
<div class="flex items-center space-x-2">
<Card.Title>{formatString(commandKey)}</Card.Title>
<Switch id={commandKey} bind:checked={commands[commandKey].state} />
<!--
Disable toggles (other than fish) when fishOnly is active.
Additionally, assume the "fishOnly" toggle exists as a command.
-->
<Switch
id={commandKey}
bind:checked={commands[commandKey].state}
disabled={fishOnlyEnabled && commandKey !== "fish"}
onCheckedChange={(e) => toggle(commandKey, e)}
/>
<Label for={commandKey}>Enabled</Label>
</div>
</Card.Header>
Expand Down Expand Up @@ -164,6 +223,7 @@
<Checkbox
id={`${commandKey}_${optionKey}`}
bind:checked={commands[commandKey][optionKey]}
onCheckedChange={(e) => toggle(optionKey, e)}
/>
<Label
class="cursor-pointer whitespace-nowrap"
Expand Down
39 changes: 39 additions & 0 deletions instance/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
_ "embed"
"encoding/json"
"fmt"
"github.com/BridgeSenseDev/Dank-Memer-Grinder/discord/types"
"github.com/BridgeSenseDev/Dank-Memer-Grinder/gateway"
"github.com/BridgeSenseDev/Dank-Memer-Grinder/utils"
"regexp"
Expand Down Expand Up @@ -162,5 +163,43 @@ func (in *Instance) EventsMessageCreate(message gateway.EventMessage) {
utils.Log(utils.Important, utils.Error, in.SafeGetUsername(),
fmt.Sprintf("Failed to send fish guesser chat message: %s", err.Error()))
}
} else if strings.Contains(embed.Title, "says...") {
if !in.Cfg.Commands.Fish.AutoCompleteEvents {
return
}

components := message.Components[0].(*types.ActionsRow).Components
if len(components) >= 2 {
if components[1].(*types.Button).Label == "Accept" {
err := in.ClickButton(message, 0, 0)
if err != nil {
utils.Log(utils.Discord, utils.Error, in.SafeGetUsername(), fmt.Sprintf("Failed to click accept task button: %s", err.Error()))
return
}
}
}

// Randomly click a button if button is not "Decline"
validIndices := make([]int, 0)
for i, cmp := range components {
btn, ok := cmp.(*types.Button)
if !ok {
continue
}

if btn.Label != "Decline" {
validIndices = append(validIndices, i)
}
}

if len(validIndices) > 0 {
utils.Rng.Seed(time.Now().UnixNano())
randomIndex := validIndices[utils.Rng.Intn(len(validIndices))]
err := in.ClickButton(message, 0, randomIndex)
if err != nil {
utils.Log(utils.Discord, utils.Error, in.SafeGetUsername(), fmt.Sprintf("Failed to click random task button: %s", err.Error()))
return
}
}
}
}
53 changes: 46 additions & 7 deletions instance/fish.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ func (in *Instance) FishMessageCreate(message gateway.EventMessage) {
}

if embed.Title == "Fishing" {
in.PauseCommands(false)
if !in.Cfg.Commands.Fish.FishOnly {
in.PauseCommands(false)
}

// Check location
currentLocation := strings.TrimSpace(strings.SplitN(embed.Fields[1].Value, ">", 2)[1])
Expand Down Expand Up @@ -117,16 +119,51 @@ func (in *Instance) FishMessageUpdate(message gateway.EventMessage) {
}
}
}
} else if strings.Contains(embed.Title, "caught a") {
} else if strings.Contains(embed.Title, "You caught") || embed.Title == "There was nothing to catch." {
if strings.Contains(embed.Description, "You have no more bucket space") {
in.LastRan["Fish"] = time.Now()

// Send fish buckets command
<-utils.Sleep(2 * time.Second)
err := in.SendSubCommand("fish", "buckets", nil, false)
if err != nil {
utils.Log(utils.Discord, utils.Error, in.SafeGetUsername(), fmt.Sprintf("Failed to send fish buckets command: %s", err.Error()))
}
} else {
in.UnpauseCommands()
if in.Cfg.Commands.Fish.FishOnly {
in.LastRan["Fish"] = time.Now().Add(time.Minute)

re := regexp.MustCompile(`<t:(\d+):R>`)
matches := re.FindStringSubmatch(embed.Description)
if len(matches) < 2 {
utils.Log(utils.Important, utils.Error, in.SafeGetUsername(), "Failed to find fish timestamp")
return
}

ts, err := strconv.ParseInt(matches[1], 10, 64)
if err != nil {
utils.Log(utils.Important, utils.Error, in.SafeGetUsername(), fmt.Sprintf("Failed to parse fish timestamp: %s", err.Error()))
return
}

cooldown := time.Duration(ts-time.Now().Unix()) * time.Second

randomDelay := time.Duration(utils.Rng.Intn(in.Cfg.Cooldowns.CommandInterval.MaxDelay-in.Cfg.Cooldowns.CommandInterval.MinDelay)+
in.Cfg.Cooldowns.CommandInterval.MinDelay) * time.Millisecond

<-utils.Sleep(cooldown + randomDelay)

if in.IsPaused() {
return
}

err = in.ClickButton(message, 0, 1)
if err != nil {
utils.Log(utils.Discord, utils.Error, in.SafeGetUsername(), fmt.Sprintf("Failed to click fish again button: %s", err.Error()))
}
} else {
in.UnpauseCommands()
}
}
} else if embed.Image != nil && strings.Contains(embed.Image.URL, "catch.webp") {
// Move next step in fish game
Expand All @@ -138,8 +175,6 @@ func (in *Instance) FishMessageUpdate(message gateway.EventMessage) {

cellWidth, cellHeight := img.Bounds().Dx()/gridSize, img.Bounds().Dy()/gridSize
in.handleCatchUpdate(img, cellWidth, cellHeight, message)
} else if embed.Title == "There was nothing to catch." {
in.UnpauseCommands()
} else if embed.Title == "Selling Creatures" {
// Choose between coins / tokens
buttonLabel := message.Components[0].(*types.ActionsRow).Components[1].(*types.Button).Label
Expand All @@ -155,7 +190,9 @@ func (in *Instance) FishMessageUpdate(message gateway.EventMessage) {
utils.Log(utils.Discord, utils.Error, in.SafeGetUsername(), fmt.Sprintf("Failed to click sell for tokens button: %s", err.Error()))
}
}
in.UnpauseCommands()
if !in.Cfg.Commands.Fish.FishOnly {
in.UnpauseCommands()
}
} else if embed.Title == "Picking Location" {
options := message.Components[0].(*types.ActionsRow).Components[0].(*types.SelectMenu).Options
chosenLocation := in.Cfg.Commands.Fish.FishLocation[utils.Rng.Intn(len(in.Cfg.Commands.Fish.FishLocation))]
Expand All @@ -178,7 +215,9 @@ func (in *Instance) FishMessageUpdate(message gateway.EventMessage) {
}
}
} else if embed.Title == "Traveling..." {
in.UnpauseCommands()
if !in.Cfg.Commands.Fish.FishOnly {
in.UnpauseCommands()
}

match := regexp.MustCompile(`<t:(\d+):[a-zA-Z]>`).FindStringSubmatch(embed.Fields[1].Value)
ts, _ := strconv.ParseInt(match[1], 10, 64)
Expand Down

0 comments on commit 74a576b

Please sign in to comment.