Skip to content

Commit 9787b80

Browse files
committed
refactor: capital constants and lots of debug logging
1 parent 59fa9c8 commit 9787b80

File tree

14 files changed

+185
-112
lines changed

14 files changed

+185
-112
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Features:
3434
- Clone the repo, cd to src
3535
- Run `nim c -r webfisher`
3636
- Edit the generated config file in your `~/.config/webfisher/config.json` (Linux), or `~\Appdata\Roaming\webfisher\config.json` (Windows).
37-
- If you run Webfisher from the CLI, you can also supply a config file with `-f <path to config.json>`.
37+
- If you run Webfisher from the CLI, you can also supply a config file with `-f="<path to config.json>"`.
3838
- Arguments can be found by tacking `-h` or `--help`
3939

4040
Upon start, Webfisher will wait 5 seconds before attempting to do anything, so use this time to get Webfishing and your cursor situated.

default.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
}:
66
buildNimPackage (finalAttrs: {
77
pname = "webfisher";
8-
version = "1.3.1";
8+
version = "1.3.2";
99

1010
src = ./.;
1111

src/webfisher.nim

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import
99
webfisher / [
1010
config,
1111
input,
12+
logging,
1213
screen,
1314
task
1415
]
@@ -34,15 +35,16 @@ var globalState: GlobalState = GlobalState(
3435

3536

3637
block webfisher:
38+
debug("Starting Webfisher...")
3739
initConfig()
3840
initDisplay()
3941
initDevice()
4042

41-
echo fmt"Started in {globalConfig.gameMode} mode."
43+
notice(fmt"Started in {globalConfig.gameMode} mode.")
4244
if globalConfig.autoSoda:
43-
echo "AutoSoda enabled."
45+
info("AutoSoda enabled.")
4446
if globalConfig.autoShop:
45-
echo fmt"AutoShop enabled, using bait {globalConfig.bait}."
47+
info(fmt"AutoShop enabled, using bait {globalConfig.bait}.")
4648

4749
# Add some wait time so the user can get situated
4850
sleep(5000)

src/webfisher/cli.nim

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import
66
]
77

88
import
9-
constants
9+
constants,
10+
logging
1011

1112

1213
type
@@ -34,7 +35,7 @@ Options:
3435
quit(0)
3536

3637
proc cliVersion(): void =
37-
echo fmt"Webfisher {releaseVersion}"
38+
notice(fmt"Webfisher {RELEASEVERSION}")
3839
quit(0)
3940

4041
proc processCliArgs*(): CliArgs =
@@ -43,6 +44,7 @@ proc processCliArgs*(): CliArgs =
4344
cliArgs.timestamps = false
4445

4546
for kind, key, val in getopt():
47+
debug(fmt"Arg key {key} value {val}")
4648
case kind
4749
of cmdArgument:
4850
cliArgs.mode = key

src/webfisher/config.nim

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,10 @@ proc initConfig*(): void =
219219

220220
initLogger(cliArgs.loglevel, cliArgs.timestamps)
221221

222+
debug("Initializing configuration...")
222223
if cliArgs.file != "":
223224
configDir = cliArgs.file
224225

225226
createConfig(configDir)
226227
updateConfig(configDir)
227228
globalConfig = parseConfig(configDir, cliArgs)
228-
229-
proc getConfig*(): Config =
230-
return globalConfig
231-

src/webfisher/constants.nim

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,52 +4,52 @@ type
44

55

66
const
7-
releaseVersion* = "1.3.1"
7+
RELEASEVERSION* = "1.3.2"
88

9-
fishingReelPixels*: PixelList = @[
9+
FISHINGREELPIXELS*: PixelList = @[
1010
(428, 785, 156, 145, 74),
1111
(388, 745, 156, 145, 74),
1212
(428, 705, 156, 145, 74),
1313
(468, 745, 156, 145, 74)
1414
]
15-
catchMenuPixels*: PixelList = @[
15+
CATCHMENUPIXELS*: PixelList = @[
1616
(320, 800, 90, 117, 90),
1717
(1500, 850, 255, 238, 213),
1818
(1500, 950, 255, 238, 213),
1919
(400, 950, 255, 238, 213)
2020
]
21-
emptyBaitPixels*: PixelList = @[
21+
EMPTYBAITPIXELS*: PixelList = @[
2222
(1853, 1033, 16, 28, 49),
2323
(1845, 1025, 16, 28, 49),
2424
(1838, 1017, 16, 28, 49)
2525
]
26-
baitShopPixels*: PixelList = @[
26+
BAITSHOPPIXELS*: PixelList = @[
2727
(192, 157, 90, 117, 90),
2828
(1161, 157, 90, 117, 90),
2929
(1135, 900, 255, 238, 213),
3030
(210, 900, 255, 238, 213)
3131
]
32-
baitSelectPixels*: PixelList = @[
32+
BAITSELECTPIXELS*: PixelList = @[
3333
(400, 350, 255, 238, 213),
3434
(1515, 350, 90, 117, 90),
3535
(1521, 741, 255, 238, 213),
3636
(397, 741, 255, 238, 213)
3737
]
3838

3939
# We only use the worms as the start since we then just move the cursor by known a known pixel distance to hit the other baits
40-
baitShopWorm*: CursorPos = (280, 370)
41-
baitShopPixelDistance*: int = 107
42-
baitSelectWorm*: CursorPos = (566, 495)
43-
baitSelectPixelDistance*: int = 38
40+
BAITSHOPWORM*: CursorPos = (280, 370)
41+
BAITSHOPPIXELSHOPDISTANCE*: int = 107
42+
BAITSELECTWORM*: CursorPos = (566, 495)
43+
BAITSELECTPIXELDISTANCE*: int = 38
4444

45-
animationCatchFish*: int = 2000
46-
animationCatchMenu*: int = 1250
47-
animationEquipItem*: int = 1250
48-
animationDrinkSoda*: int = 1750
45+
ANIMATIONCATCHFISH*: int = 2000
46+
ANIMATIONCATCHMENU*: int = 1250
47+
ANIMATIONEQUIPITEM*: int = 1250
48+
ANIMATIONDRINKSODA*: int = 1750
4949

50-
animationMenuDelay*: int = 250
51-
animationMenuClose*: int = 500
50+
ANIMATIONMENUDELAY*: int = 250
51+
ANIMATIONMENUCLOSE*: int = 500
5252

53-
uinputTimeout*: int = 10
54-
uinputTime*: int = 20
53+
UINPUTTIMEOUT*: int = 10
54+
UINPUTTIME*: int = 20
5555

src/webfisher/linux/input.nim

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,23 @@ proc createDevice(): ptr libevdev_uinput =
4949
return uinput
5050

5151
proc initDevice*(): void =
52+
debug("Creating Webfisher input device...")
5253
webfisherDevice = createDevice()
54+
debug("Attaching to display...")
5355
webfisherDisplay = XOpenDisplay(nil)
5456

5557
proc cleanupDevice*(): void =
58+
debug("Removing Webfisher input device...")
5659
libevdev_uinput_destroy(webfisherDevice)
60+
debug("Detaching from display...")
5761
discard XCloseDisplay(webfisherDisplay)
5862

5963
proc manageKey(key: int, state: int): void =
64+
debug(fmt"Setting key {key} to state {state}...")
6065
libevdev_uinput_write_event(webfisherDevice, EV_KEY, key, state)
61-
sleep(uinputTimeout) # Buffer time so listeners can see events more consistently
66+
sleep(UINPUTTIMEOUT) # Buffer time so listeners can see events more consistently
6267
libevdev_uinput_write_event(webfisherDevice, EV_SYN, SYN_REPORT, 0)
63-
sleep(uinputTimeout)
68+
sleep(UINPUTTIMEOUT)
6469

6570
proc pressKey(key: int): void =
6671
manageKey(key, 1)
@@ -86,10 +91,11 @@ proc pressNum*(num: int, time: int): void =
8691
releaseKey(num + 1)
8792

8893
proc manageMouse(state: int): void =
94+
debug(fmt"Setting mouse to state {state}...")
8995
libevdev_uinput_write_event(webfisherDevice, EV_KEY, BTN_LEFT, state)
90-
sleep(uinputTimeout)
96+
sleep(UINPUTTIMEOUT)
9197
libevdev_uinput_write_event(webfisherDevice, EV_SYN, SYN_REPORT, 0)
92-
sleep(uinputTimeout)
98+
sleep(UINPUTTIMEOUT)
9399

94100
proc pressMouse*(): void =
95101
manageMouse(1)
@@ -103,6 +109,7 @@ proc pressMouse*(time: int): void =
103109
releaseMouse()
104110

105111
proc moveMouseAbs*(x, y: int): void =
112+
debug(fmt"Moving mouse (abs) to x {x} y {y}...")
106113
var
107114
# Most of this is unused since we use the root display so source doesn't apply
108115
# wSrc, wDest: Window # Unused
@@ -118,9 +125,10 @@ proc moveMouseAbs*(x, y: int): void =
118125
destX, destY)
119126

120127
discard XFlush(webfisherDisplay)
121-
sleep(uinputTimeout)
128+
sleep(UINPUTTIMEOUT)
122129

123130
proc moveMouseRel*(x, y: int): void =
131+
debug(fmt"Moving mouse (rel) to x {x} y {y}...")
124132
var
125133
# Most of this is unused since we use the root display so source doesn't apply
126134
# wSrc, wDest: Window # Unused
@@ -136,5 +144,5 @@ proc moveMouseRel*(x, y: int): void =
136144
destX, destY)
137145

138146
discard XFlush(webfisherDisplay)
139-
sleep(uinputTimeout)
147+
sleep(UINPUTTIMEOUT)
140148

src/webfisher/linux/screen.nim

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import
2+
std/strformat,
23
x11 / [
34
x,
45
xlib,
56
xutil
67
]
78

8-
import ../constants
9+
import
10+
../constants,
11+
../logging
912

1013
from ../config import globalConfig
1114

@@ -31,12 +34,15 @@ var webfisherDisplay: PDisplay
3134
# f.write(image.data[i * 4 + 0])
3235

3336
proc initDisplay*(): void =
37+
debug("Attaching to display...")
3438
webfisherDisplay = XOpenDisplay(nil)
3539

3640
proc cleanupDisplay*(): void =
41+
debug("Detaching from display...")
3742
discard XCloseDisplay(webfisherDisplay)
3843

3944
proc getScreenshot(): PXImage =
45+
debug("Getting screenshot...")
4046
var screenshot = XGetImage(webfisherDisplay,
4147
RootWindow(webfisherDisplay, DefaultScreen(webfisherDisplay)),
4248
globalConfig.screenConfig[0].cint,
@@ -53,40 +59,48 @@ proc getPixelColor(screenshot: PXImage; x: int; y: int): Pixel =
5359
pixel.r = ((color shr 16) and 0xFF).int
5460
pixel.g = ((color shr 8) and 0xFF).int
5561
pixel.b = (color and 0xFF).int
62+
debug(fmt"Pixel r {pixel.r} g {pixel.g} b {pixel.b}")
5663
return pixel
5764

5865
proc checkPixels(screenshot: PXImage, pixelList: PixelList, count: int): bool =
5966
var valid = 0
6067
for checkPixel in pixelList:
6168
let pixel = getPixelColor(screenshot, checkPixel.x, checkPixel.y)
69+
debug(fmt"checkPixel r {checkPixel.r} g {checkPixel.g} b {checkPixel.b}")
6270
if pixel.r == checkPixel.r and
6371
pixel.g == checkPixel.g and
6472
pixel.b == checkPixel.b:
6573
# We test if most of the pixels match since the reel could block the pixel
6674
valid += 1
75+
debug(fmt"Pixel match, valid +1 {valid}")
6776
discard XDestroyImage(screenshot)
6877
if valid >= count:
6978
return true
7079
else:
7180
return false
7281

7382
proc getFishingGame*(): bool =
83+
debug("Checking for fishing game...")
7484
let screenshot = getScreenshot()
75-
return checkPixels(screenshot, fishingReelPixels, 3)
85+
return checkPixels(screenshot, FISHINGREELPIXELS, 3)
7686

7787
proc getCatchMenu*(): bool =
88+
debug("Checking for catch menu...")
7889
let screenshot = getScreenshot()
79-
return checkPixels(screenshot, catchMenuPixels, 3)
90+
return checkPixels(screenshot, CATCHMENUPIXELS, 3)
8091

8192
proc getEmptyBait*(): bool =
93+
debug("Checking for empty bait...")
8294
let screenshot = getScreenshot()
83-
return checkPixels(screenshot, emptyBaitPixels, 2)
95+
return checkPixels(screenshot, EMPTYBAITPIXELS, 2)
8496

8597
proc getBaitShop*(): bool =
98+
debug("Checking for bait shop...")
8699
let screenshot = getScreenshot()
87-
return checkPixels(screenshot, baitShopPixels, 3)
100+
return checkPixels(screenshot, BAITSHOPPIXELS, 3)
88101

89102
proc getBaitSelect*(): bool =
103+
debug("Checking for bait select...")
90104
let screenshot = getScreenshot()
91-
return checkPixels(screenshot, baitSelectPixels, 3)
105+
return checkPixels(screenshot, BAITSELECTPIXELS, 3)
92106

src/webfisher/logging.nim

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,28 @@ import
88
var consoleLog = newConsoleLogger(fmtStr="[$levelname]: ")
99

1010

11+
proc debug*(str: string): void =
12+
consoleLog.log(lvlDebug, str)
13+
14+
proc info*(str: string): void =
15+
consoleLog.log(lvlInfo, str)
16+
17+
proc notice*(str: string): void =
18+
consoleLog.log(lvlNotice, str)
19+
20+
proc warn*(str: string): void =
21+
consoleLog.log(lvlWarn, str)
22+
23+
proc error*(str: string): void =
24+
consoleLog.log(lvlError, str)
25+
26+
proc fatal*(str: string): void =
27+
consoleLog.log(lvlFatal, str)
28+
1129
proc initLogger*(lvl: string, ts: bool): void =
1230
if ts:
1331
consoleLog = newConsoleLogger(fmtStr="[$datetime][$levelname]: ")
32+
debug("Logger timestamps enabled.")
1433

1534
case lvl:
1635
of "d", "debug", "6":
@@ -29,24 +48,7 @@ proc initLogger*(lvl: string, ts: bool): void =
2948
of "none", "0":
3049
setLogFilter(lvlNone)
3150
else:
32-
consoleLog.log(lvlFatal, fmt"{lvl} is not a valid value for log-level.")
51+
fatal(fmt"{lvl} is not a valid value for log-level.")
3352
quit(1)
3453

35-
proc debug*(str: string): void =
36-
consoleLog.log(lvlDebug, str)
37-
38-
proc info*(str: string): void =
39-
consoleLog.log(lvlInfo, str)
40-
41-
proc notice*(str: string): void =
42-
consoleLog.log(lvlNotice, str)
43-
44-
proc warn*(str: string): void =
45-
consoleLog.log(lvlWarn, str)
46-
47-
proc error*(str: string): void =
48-
consoleLog.log(lvlError, str)
49-
50-
proc fatal*(str: string): void =
51-
consoleLog.log(lvlFatal, str)
52-
54+
debug(fmt"Logger initialized with {lvl}.")

src/webfisher/newconfig.nim

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
type
2+
# config type cli description
3+
# ("autoShop", "type", "desc")
4+
ConfigOption* = seq[tuple[c, t, d: string]]
5+
6+
7+
const
8+
configFile*: ConfigOption = @[
9+
("autoShop", "bool", "Whether to automatically purchase bait")
10+
]
11+
# configCli should inherit configFile options so Webfisher can be configured from Cli
12+
configCli*: ConfigOption = @[
13+
("version", "bool", "Show version and exit")
14+
]
15+

0 commit comments

Comments
 (0)