Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
E
exapunks-hackmatch-bot
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Taddeüs Kroes
exapunks-hackmatch-bot
Commits
d7d50530
Commit
d7d50530
authored
Apr 03, 2020
by
Taddeüs Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move x11 code to separate file, write keypress functions
parent
cfb67915
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
90 additions
and
47 deletions
+90
-47
interaction.py
interaction.py
+88
-0
parser.py
parser.py
+2
-47
No files found.
interaction.py
0 → 100644
View file @
d7d50530
import
time
from
random
import
randint
from
Xlib
import
display
,
X
,
XK
,
ext
from
PIL
import
Image
WINDOW_WIDTH
=
1600
WINDOW_HEIGHT
=
900
BOARD_X
=
367
BOARD_Y
=
129
BOARD_WIDTH
=
420
BOARD_HEIGHT
=
638
KEY_DELAY
=
0.015
disp
=
display
.
Display
()
def
find_window
(
name
):
def
traverse
(
window
):
if
window
.
get_wm_name
()
==
name
:
return
window
for
child
in
window
.
query_tree
().
children
:
win
=
traverse
(
child
)
if
win
:
return
win
return
traverse
(
display
.
Display
().
screen
().
root
)
def
get_exapunks_window
():
win
=
find_window
(
'EXAPUNKS'
)
assert
win
,
'EXAPUNKS window not found'
geo
=
win
.
get_geometry
()
assert
geo
.
width
==
WINDOW_WIDTH
assert
geo
.
height
==
WINDOW_HEIGHT
return
win
def
focus_window
(
window
):
window
.
set_input_focus
(
X
.
RevertToNone
,
X
.
CurrentTime
)
window
.
raise_window
()
disp
.
sync
()
def
screenshot_board
(
window
):
raw
=
window
.
get_image
(
BOARD_X
,
BOARD_Y
,
BOARD_WIDTH
,
BOARD_HEIGHT
,
X
.
ZPixmap
,
0xffffffff
)
dim
=
BOARD_WIDTH
,
BOARD_HEIGHT
im
=
Image
.
frombytes
(
'RGB'
,
dim
,
raw
.
data
,
'raw'
,
'BGRX'
)
return
im
.
convert
(
'HSV'
)
def
press_key
(
window
,
key
):
keysym
=
XK
.
string_to_keysym
(
key
)
keycode
=
disp
.
keysym_to_keycode
(
keysym
)
ext
.
xtest
.
fake_input
(
disp
,
X
.
KeyPress
,
keycode
)
disp
.
sync
()
time
.
sleep
(
KEY_DELAY
)
ext
.
xtest
.
fake_input
(
disp
,
X
.
KeyRelease
,
keycode
)
disp
.
sync
()
time
.
sleep
(
KEY_DELAY
)
if
__name__
==
'__main__'
:
win
=
get_exapunks_window
()
win
.
raise_window
()
old_focus
=
disp
.
get_input_focus
()
disp
.
set_input_focus
(
win
,
X
.
RevertToParent
,
X
.
CurrentTime
)
def
press_keys
(
keys
):
for
key
in
keys
:
press_key
(
win
,
key
)
#press_keys('aaaj')
#while True:
# press_keys('djkj' * 7)
# press_keys('ajkj' * 7)
while
True
:
press_key
(
win
,
'adjk'
[
randint
(
0
,
3
)])
disp
.
set_input_focus
(
old_focus
.
focus
,
X
.
RevertToParent
,
X
.
CurrentTime
)
disp
.
sync
()
parser.py
View file @
d7d50530
#!/usr/bin/env python3
import
os
import
time
import
numpy
as
np
from
Xlib
import
display
,
X
from
PIL
import
Image
COLUMNS
=
7
WINDOW_WIDTH
=
1600
WINDOW_HEIGHT
=
900
BLOCK_SIZE
=
60
BOARD_X
=
367
BOARD_Y
=
129
BOARD_WIDTH
=
COLUMNS
*
BLOCK_SIZE
BOARD_HEIGHT
=
638
MAX_COLUMN_HEIGHT
=
546
DETECT_COLUMN_OFFSET_X
=
8
,
50
...
...
@@ -49,43 +39,6 @@ def is_hue(h, hexpect):
return
abs
(
h
-
hexpect
)
<=
HUE_TOLERANCE
def
find_window
(
name
):
def
traverse
(
window
):
if
window
.
get_wm_name
()
==
name
:
return
window
for
child
in
window
.
query_tree
().
children
:
win
=
traverse
(
child
)
if
win
:
return
win
return
traverse
(
display
.
Display
().
screen
().
root
)
def
get_exapunks_window
():
win
=
find_window
(
'EXAPUNKS'
)
assert
win
,
'EXAPUNKS window not found'
geo
=
win
.
get_geometry
()
assert
geo
.
width
==
WINDOW_WIDTH
assert
geo
.
height
==
WINDOW_HEIGHT
return
win
def
focus_window
(
window
):
window
.
set_input_focus
(
X
.
RevertToNone
,
X
.
CurrentTime
)
window
.
raise_window
()
display
.
Display
().
sync
()
def
screenshot_board
(
window
):
start
=
time
.
time
()
raw
=
window
.
get_image
(
BOARD_X
,
BOARD_Y
,
BOARD_WIDTH
,
BOARD_HEIGHT
,
X
.
ZPixmap
,
0xffffffff
)
dim
=
BOARD_WIDTH
,
BOARD_HEIGHT
im
=
Image
.
frombytes
(
'RGB'
,
dim
,
raw
.
data
,
'raw'
,
'BGRX'
)
return
im
.
convert
(
'HSV'
)
def
detect_columns
(
board
):
def
saturated
(
x
,
y
):
h
,
s
,
v
=
board
.
getpixel
((
x
,
y
))
...
...
@@ -165,6 +118,8 @@ def print_board(blocks, exa, held):
if
__name__
==
'__main__'
:
from
iteraction
import
get_exapunks_window
,
screenshot_board
win
=
get_exapunks_window
()
win
.
raise_window
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment