Skip to content

Commit

Permalink
Add setting to offer closing web browser during breaks
Browse files Browse the repository at this point in the history
Implement the idea from Drklord to save CPU load during breaks discussed at https://forum.botlab.org/t/farm-manager-tribal-wars-2-farmbot/3038/303?u=viir

+ Expand the bot framework with a command to close the web browser.
+ Add a setting to the TW2 farmbot `bot-settings` to allow users to close the web browser during breaks.
  • Loading branch information
Viir committed Nov 1, 2021
1 parent 6c00145 commit 32d50e8
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 53 deletions.
3 changes: 2 additions & 1 deletion guide/tribal-wars-2/farm-manager-tribal-wars-2-farmbot.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,9 @@ Following is a list of available settings:
+ `farm-player`: Name of a player/character to farm. By default, the bot only farms barbarians, but this setting allows you to also farm players.
+ `farm-army-preset-pattern`: Text for filtering the army presets to use for farm attacks. Army presets only pass the filter when their name contains this text.
+ `limit-outgoing-commands-per-village`: The maximum number of outgoing commands per village before the bot considers the village completed. By default, the bot will use up all available 50 outgoing commands per village. You can also specify a range like `45-48`. The bot then picks a random value in this range.
+ `close-game-client-during-break`: Set this to 'yes' to make the bot close the game client/web browser during breaks.

To configure settings, open the configuration page at [https://catalog.botlab.org/ef8b6657b4d43689ea2ad236c293b455d034618e4374c9bff741db5a65e931c9](https://catalog.botlab.org/ef8b6657b4d43689ea2ad236c293b455d034618e4374c9bff741db5a65e931c9) in a web browser.
To configure settings, open the configuration page at [https://catalog.botlab.org/45f96646063640c064c96db0eb1f2baf2a5c46a05cede665e976e963ec8c73bc](https://catalog.botlab.org/45f96646063640c064c96db0eb1f2baf2a5c46a05cede665e976e963ec8c73bc) in a web browser.

On this page, scroll down to the button `Configure bot`.

Expand Down
109 changes: 66 additions & 43 deletions implement/applications/tribal-wars-2/tribal-wars-2-farmbot/Bot.elm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{- Tribal Wars 2 farmbot version 2021-09-23
{- Tribal Wars 2 farmbot version 2021-11-01
I search for barbarian villages around your villages and then attack them.
Expand Down Expand Up @@ -31,6 +31,7 @@
+ `farm-player`: Name of a player/character to farm. By default, the bot only farms barbarians, but this setting allows you to also farm players.
+ `farm-army-preset-pattern`: Text for filtering the army presets to use for farm attacks. Army presets only pass the filter when their name contains this text.
+ `limit-outgoing-commands-per-village`: The maximum number of outgoing commands per village before the bot considers the village completed. By default, the bot will use up all available 50 outgoing commands per village. You can also specify a range like `45 - 48`. The bot then picks a random value in this range for each village.
+ `close-game-client-during-break`: Set this to 'yes' to make the bot close the game client/web browser during breaks.
When using more than one setting, start a new line for each setting in the text input field.
Here is an example of `bot-settings` for three farm cycles with breaks of 20 to 40 minutes in between:
Expand Down Expand Up @@ -85,6 +86,7 @@ initBotSettings =
, farmArmyPresetPatterns = []
, limitOutgoingCommandsPerVillage = { minimum = 50, maximum = 50 }
, webBrowserUserProfileId = "default"
, closeGameClientDuringBreak = AppSettings.No
}


Expand Down Expand Up @@ -126,6 +128,9 @@ parseBotSettings =
, ( "web-browser-user-profile-id"
, AppSettings.valueTypeString (\webBrowserUserProfileId settings -> { settings | webBrowserUserProfileId = webBrowserUserProfileId })
)
, ( "close-game-client-during-break"
, AppSettings.valueTypeYesOrNo (\closeGameClientDuringBreak settings -> { settings | closeGameClientDuringBreak = closeGameClientDuringBreak })
)
]
|> Dict.fromList
)
Expand Down Expand Up @@ -229,6 +234,7 @@ type alias BotSettings =
, farmArmyPresetPatterns : List String
, limitOutgoingCommandsPerVillage : IntervalInt
, webBrowserUserProfileId : String
, closeGameClientDuringBreak : AppSettings.YesOrNo
}


Expand Down Expand Up @@ -499,8 +505,8 @@ botMain =
}


processWebBrowserBotEvent : BotEvent -> BotState -> { newState : BotState, response : BotResponse, statusMessage : String }
processWebBrowserBotEvent event stateBeforeIntegrateEvent =
processWebBrowserBotEvent : BotEvent -> BotFramework.GenericBotState -> BotState -> { newState : BotState, response : BotResponse, statusMessage : String }
processWebBrowserBotEvent event genericBotState stateBeforeIntegrateEvent =
case stateBeforeIntegrateEvent |> integrateWebBrowserBotEvent event of
Err integrateEventError ->
{ newState = stateBeforeIntegrateEvent
Expand Down Expand Up @@ -580,7 +586,9 @@ processWebBrowserBotEvent event stateBeforeIntegrateEvent =

Nothing ->
decideNextAction
{ lastPageLocation = stateBeforeIntegrateEvent.lastPageLocation }
{ lastPageLocation = stateBeforeIntegrateEvent.lastPageLocation
, webBrowserRunning = genericBotState.webBrowserRunning
}
{ stateBefore | currentActivity = Nothing }
|> Tuple.mapSecond Just

Expand All @@ -597,8 +605,8 @@ processWebBrowserBotEvent event stateBeforeIntegrateEvent =
}


decideNextAction : { lastPageLocation : Maybe String } -> BotState -> ( DecisionPathNode BotResponse, BotState )
decideNextAction { lastPageLocation } stateBefore =
decideNextAction : { lastPageLocation : Maybe String, webBrowserRunning : Bool } -> BotState -> ( DecisionPathNode BotResponse, BotState )
decideNextAction { lastPageLocation, webBrowserRunning } stateBefore =
case stateBefore.farmState of
InBreak farmBreak ->
let
Expand All @@ -615,14 +623,22 @@ decideNextAction { lastPageLocation } stateBefore =
)

else
let
botRequest =
if stateBefore.settings.closeGameClientDuringBreak == AppSettings.Yes then
Just (BotFramework.CloseWebBrowser { userProfileId = stateBefore.settings.webBrowserUserProfileId })

else
Nothing
in
( describeBranch
("Next farm cycle starts in "
++ (minutesToNextFarmCycleStart |> String.fromInt)
++ " minutes. Last cycle completed "
++ (minutesSinceLastFarmCycleCompletion |> String.fromInt)
++ " minutes ago."
)
(endDecisionPath (BotFramework.ContinueSession Nothing))
(endDecisionPath (BotFramework.ContinueSession botRequest))
, stateBefore
)

Expand All @@ -645,47 +661,54 @@ decideNextAction { lastPageLocation } stateBefore =

Just activity ->
let
continueWithStartWebBrowser =
( BotFramework.StartWebBrowser
{ pageGoToUrl = lastPageLocation
, userProfileId = stateBefore.settings.webBrowserUserProfileId
}
, { stateBefore
| lastStartWebBrowserTimeInSeconds = Just (stateBefore.timeInMilliseconds // 1000)
, startWebBrowserCount = stateBefore.startWebBrowserCount + 1
, readFromGameConsecutiveTimeoutsCount = 0
}
)

( requestToFramework, updatedStateForActivity ) =
case activity of
RequestToPage requestToPage ->
let
requestComponents =
componentsForRequestToPage requestToPage

requestToPageId =
stateBefore.lastRequestToPageId + 1

requestToPageIdString =
requestToPageId |> String.fromInt
in
( BotFramework.RunJavascriptInCurrentPageRequest
{ javascript = requestComponents.javascript
, requestId = requestToPageIdString
, timeToWaitForCallbackMilliseconds =
case requestComponents.waitForCallbackDuration of
Just waitForCallbackDuration ->
waitForCallbackDuration

Nothing ->
0
}
, { stateBefore
| lastRequestToPageId = requestToPageId
, pendingRequestToPageRequestId = Just requestToPageIdString
}
)
if not webBrowserRunning then
continueWithStartWebBrowser

else
let
requestComponents =
componentsForRequestToPage requestToPage

requestToPageId =
stateBefore.lastRequestToPageId + 1

requestToPageIdString =
requestToPageId |> String.fromInt
in
( BotFramework.RunJavascriptInCurrentPageRequest
{ javascript = requestComponents.javascript
, requestId = requestToPageIdString
, timeToWaitForCallbackMilliseconds =
case requestComponents.waitForCallbackDuration of
Just waitForCallbackDuration ->
waitForCallbackDuration

Nothing ->
0
}
, { stateBefore
| lastRequestToPageId = requestToPageId
, pendingRequestToPageRequestId = Just requestToPageIdString
}
)

RestartWebBrowser ->
( BotFramework.StartWebBrowser
{ pageGoToUrl = lastPageLocation
, userProfileId = stateBefore.settings.webBrowserUserProfileId
}
, { stateBefore
| lastStartWebBrowserTimeInSeconds = Just (stateBefore.timeInMilliseconds // 1000)
, startWebBrowserCount = stateBefore.startWebBrowserCount + 1
, readFromGameConsecutiveTimeoutsCount = 0
}
)
continueWithStartWebBrowser
in
( Just requestToFramework
, updatedStateForActivity
Expand Down

0 comments on commit 32d50e8

Please sign in to comment.