omnipython-apps 0.1.4.dev8__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- apps/__init__.py +5 -0
- apps/about/__init__.py +257 -0
- apps/about/app.json +1 -0
- apps/about/credits.py +12 -0
- apps/about/facts.py +398 -0
- apps/about/fonts/OFL.txt +93 -0
- apps/about/fonts/latin1_14.bin +0 -0
- apps/about/fonts/latin1_16.bin +0 -0
- apps/about/fonts/latin1_24.bin +0 -0
- apps/about/fonts/latin1_32.bin +0 -0
- apps/about/fonts/latin1_40.bin +0 -0
- apps/backline/__init__.py +58 -0
- apps/backline/app.json +1 -0
- apps/backline/app.py +475 -0
- apps/backline/art.py +717 -0
- apps/backline/ask.py +781 -0
- apps/backline/box.py +502 -0
- apps/backline/cast.py +137 -0
- apps/backline/desk/__init__.py +329 -0
- apps/backline/desk/editor.py +331 -0
- apps/backline/desk/grid.py +154 -0
- apps/backline/desk/look.py +169 -0
- apps/backline/desk/pedals.py +102 -0
- apps/backline/desk/perform.py +157 -0
- apps/backline/desk/prompt.py +210 -0
- apps/backline/desk/strip.py +159 -0
- apps/backline/desk_main.py +226 -0
- apps/backline/engine.py +622 -0
- apps/backline/floor.py +509 -0
- apps/backline/footswitch.py +119 -0
- apps/backline/glyphs.py +82 -0
- apps/backline/kits.py +428 -0
- apps/backline/midipedal.py +362 -0
- apps/backline/page_data.py +413 -0
- apps/backline/parts.py +145 -0
- apps/backline/pedal.py +107 -0
- apps/backline/remote.py +134 -0
- apps/backline/songs/ballad_68.json +1 -0
- apps/backline/songs/bossa.json +1 -0
- apps/backline/songs/country_train.json +1 -0
- apps/backline/songs/funk.json +1 -0
- apps/backline/songs/metal.json +1 -0
- apps/backline/songs/pop.json +1 -0
- apps/backline/songs/rock.json +1 -0
- apps/backline/songs/shuffle.json +1 -0
- apps/battery/__init__.py +101 -0
- apps/battery/app.json +1 -0
- apps/bit_world/act_deck.py +683 -0
- apps/bit_world/act_game.py +762 -0
- apps/bit_world/act_pet.py +1032 -0
- apps/bit_world/app.json +1 -0
- apps/bit_world/bit_output.py +45 -0
- apps/bit_world/bit_palette.py +503 -0
- apps/bit_world/bit_sound.py +431 -0
- apps/bit_world/bit_sprites.py +632 -0
- apps/bit_world/bit_world.py +534 -0
- apps/btreceiver/__init__.py +690 -0
- apps/btreceiver/app.json +1 -0
- apps/btreceiver/chime.py +155 -0
- apps/btreceiver/demo.py +529 -0
- apps/btreceiver/screen.py +633 -0
- apps/camera/__init__.py +316 -0
- apps/camera/app.json +1 -0
- apps/card/__init__.py +113 -0
- apps/card/app.json +1 -0
- apps/cast/__init__.py +631 -0
- apps/cast/app.json +1 -0
- apps/cast/receivers.py +193 -0
- apps/chess/__init__.py +18 -0
- apps/chess/app.json +1 -0
- apps/chess/app.py +922 -0
- apps/chess/art.py +323 -0
- apps/chess/ble.py +411 -0
- apps/chess/engine.py +925 -0
- apps/chess/game.py +327 -0
- apps/chess/net.py +261 -0
- apps/clock/__init__.py +38 -0
- apps/clock/app.json +1 -0
- apps/detect/__init__.py +214 -0
- apps/detect/app.json +1 -0
- apps/fmsynth/__init__.py +41 -0
- apps/fmsynth/algorithms.py +61 -0
- apps/fmsynth/app.json +1 -0
- apps/fmsynth/app.py +355 -0
- apps/fmsynth/editor.py +206 -0
- apps/fmsynth/engine.py +257 -0
- apps/fmsynth/meters.py +262 -0
- apps/fmsynth/midi.py +375 -0
- apps/fmsynth/panel_view.py +1071 -0
- apps/fmsynth/patches/fmsynth-01.syx +0 -0
- apps/fmsynth/settings.py +138 -0
- apps/fmsynth/voice.py +398 -0
- apps/gamepad/__init__.py +685 -0
- apps/gamepad/app.json +1 -0
- apps/gamepad/pad.py +94 -0
- apps/gamepad/watch.py +352 -0
- apps/hidden/__init__.py +72 -0
- apps/hidden/app.json +1 -0
- apps/home/__init__.py +483 -0
- apps/home/app.json +1 -0
- apps/home/main.py +36 -0
- apps/homeassistant/__init__.py +213 -0
- apps/homeassistant/api.py +104 -0
- apps/homeassistant/app.json +1 -0
- apps/huey/__init__.py +287 -0
- apps/huey/acting.py +184 -0
- apps/huey/actor.py +194 -0
- apps/huey/app.json +1 -0
- apps/huey/client.py +125 -0
- apps/huey/servers.py +121 -0
- apps/huey/settings.example.json +8 -0
- apps/huey/settings.py +97 -0
- apps/huey/turn.py +504 -0
- apps/huey/voice.py +217 -0
- apps/info/__init__.py +810 -0
- apps/info/app.json +1 -0
- apps/lora/__init__.py +62 -0
- apps/lora/app.json +1 -0
- apps/manifest.py +71 -0
- apps/medialink_baby_monitor/__init__.py +222 -0
- apps/medialink_baby_monitor/app.json +1 -0
- apps/medialink_doorbell/__init__.py +251 -0
- apps/medialink_doorbell/app.json +1 -0
- apps/medialink_hands_free/__init__.py +217 -0
- apps/medialink_hands_free/app.json +1 -0
- apps/medialink_surveillance_camera/__init__.py +258 -0
- apps/medialink_surveillance_camera/app.json +1 -0
- apps/medialink_video_doorbell/__init__.py +289 -0
- apps/medialink_video_doorbell/app.json +1 -0
- apps/medialink_videophone/__init__.py +273 -0
- apps/medialink_videophone/app.json +1 -0
- apps/medialink_walkie_talkie/__init__.py +228 -0
- apps/medialink_walkie_talkie/app.json +1 -0
- apps/midicontroller/__init__.py +774 -0
- apps/midicontroller/app.json +1 -0
- apps/midicontroller/links.py +421 -0
- apps/midicontroller/surface.py +343 -0
- apps/midiengine/__init__.py +977 -0
- apps/midiengine/app.json +1 -0
- apps/midiengine/banks.py +85 -0
- apps/midiengine/midiin.py +165 -0
- apps/midiengine/player.py +392 -0
- apps/midiengine/umidiparser.py +1413 -0
- apps/mp3/__init__.py +240 -0
- apps/mp3/app.json +1 -0
- apps/perch/__init__.py +119 -0
- apps/perch/app.json +1 -0
- apps/perch/huey.py +481 -0
- apps/perch/huey_props.py +386 -0
- apps/perch/huey_shift.py +42 -0
- apps/perch/huey_views.py +296 -0
- apps/perch/scene_bench.py +680 -0
- apps/perch/scene_coast.py +479 -0
- apps/perch/scene_desk.py +492 -0
- apps/perch/stage.py +1274 -0
- apps/perch/vec.py +366 -0
- apps/repl/__init__.py +325 -0
- apps/repl/app.json +1 -0
- apps/roku/__init__.py +156 -0
- apps/roku/app.json +1 -0
- apps/rokucam/__init__.py +362 -0
- apps/rokucam/app.json +1 -0
- apps/security/__init__.py +261 -0
- apps/security/app.json +1 -0
- apps/settings/__init__.py +221 -0
- apps/settings/app.json +1 -0
- apps/shell/__init__.py +33 -0
- apps/shell/app.json +1 -0
- apps/spotify/__init__.py +669 -0
- apps/spotify/app.json +1 -0
- apps/spotify/screen.py +82 -0
- apps/steps/__init__.py +58 -0
- apps/steps/app.json +1 -0
- apps/stompbox/__init__.py +31 -0
- apps/stompbox/app.json +1 -0
- apps/stompbox/app.py +668 -0
- apps/stompbox/boards.json +20 -0
- apps/stompbox/effects.py +241 -0
- apps/stompbox/engine.py +340 -0
- apps/stompbox/footswitch.py +331 -0
- apps/stompbox/inputs.py +449 -0
- apps/stompbox/meter.py +72 -0
- apps/stompbox/outputs.py +135 -0
- apps/stompbox/pedals.txt +45 -0
- apps/stompbox/presets.py +106 -0
- apps/stompbox/styles.py +135 -0
- apps/stompbox/ui.py +817 -0
- apps/stompbox/wavfile.py +73 -0
- apps/synthstation/__init__.py +20 -0
- apps/synthstation/app.json +1 -0
- apps/synthstation/audio.py +397 -0
- apps/synthstation/catalog.py +50 -0
- apps/synthstation/meters.py +136 -0
- apps/synthstation/midi.py +380 -0
- apps/synthstation/pads.py +177 -0
- apps/synthstation/parts.py +128 -0
- apps/synthstation/station.py +1069 -0
- apps/system/__init__.py +11 -0
- apps/system/app.json +1 -0
- apps/touchcal/__init__.py +324 -0
- apps/touchcal/app.json +1 -0
- apps/touchcal/fit.py +262 -0
- apps/wake/__init__.py +268 -0
- apps/wake/app.json +1 -0
- apps/wake/feed.py +155 -0
- apps/watch/__init__.py +355 -0
- apps/watch/app.json +1 -0
- apps/watch/faces.py +35 -0
- apps/watch/wheel.py +157 -0
- apps/weather/__init__.py +278 -0
- apps/weather/app.json +1 -0
- apps/weather/icons.py +172 -0
- apps/weather/sizing.py +71 -0
- apps/weather/zippad.py +172 -0
- omnipython_apps-0.1.4.dev8.dist-info/METADATA +33 -0
- omnipython_apps-0.1.4.dev8.dist-info/RECORD +218 -0
- omnipython_apps-0.1.4.dev8.dist-info/WHEEL +5 -0
- omnipython_apps-0.1.4.dev8.dist-info/top_level.txt +1 -0
apps/chess/app.py
ADDED
|
@@ -0,0 +1,922 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 Brad Barnett
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: MIT
|
|
4
|
+
"""The app: the board on screen, the engine behind it, and a friend over the network.
|
|
5
|
+
|
|
6
|
+
Tap a piece, then a square it may go to; the squares it may reach show as
|
|
7
|
+
dots. Against the engine you play the side at the bottom of the board, and
|
|
8
|
+
Flip changes sides. A pawn reaching the last row asks what it becomes.
|
|
9
|
+
Moves slide, a taken piece fades and a king in check glows.
|
|
10
|
+
|
|
11
|
+
The panel beside the board (under it on a tall screen) says whose move it
|
|
12
|
+
is and how the game ended, and has the buttons: New, Undo (two moves back,
|
|
13
|
+
so it's your turn again), Flip, Theme, the engine's Level, and Friend, which
|
|
14
|
+
lists the other Chess apps on this network to play. The engine thinks on
|
|
15
|
+
appkit's worker, so the screen keeps moving while it does.
|
|
16
|
+
|
|
17
|
+
The theme, the level, your side and the game in progress are kept while the
|
|
18
|
+
program runs, so the game is still there when you come back to the app.
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
import math
|
|
22
|
+
|
|
23
|
+
import lvgl as lv
|
|
24
|
+
from touchkit import ui
|
|
25
|
+
|
|
26
|
+
from . import art, engine
|
|
27
|
+
from . import game as _game
|
|
28
|
+
from . import net as _net
|
|
29
|
+
from .engine import BLACK, QUEEN, WHITE, ticks_diff, ticks_ms
|
|
30
|
+
|
|
31
|
+
#: How often the animations and the network are looked after, in ms.
|
|
32
|
+
TICK_MS = 20
|
|
33
|
+
SLIDE_MS = 220
|
|
34
|
+
FADE_MS = 260
|
|
35
|
+
#: The friend list is refreshed this often while it shows.
|
|
36
|
+
SEARCH_MS = 4000
|
|
37
|
+
|
|
38
|
+
ENGINE = "engine"
|
|
39
|
+
FRIEND = "friend"
|
|
40
|
+
|
|
41
|
+
#: The running app, for a console or a test script. None while it isn't open.
|
|
42
|
+
current = None
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class _Tween:
|
|
46
|
+
"""One animation: a piece sliding, fading or a glow pulsing."""
|
|
47
|
+
|
|
48
|
+
def __init__(self, obj, kind, start, end, ms, done=None):
|
|
49
|
+
self.obj = obj
|
|
50
|
+
self.kind = kind
|
|
51
|
+
self.start = start
|
|
52
|
+
self.end = end
|
|
53
|
+
self.ms = ms
|
|
54
|
+
self.done = done
|
|
55
|
+
self.t0 = ticks_ms()
|
|
56
|
+
|
|
57
|
+
def step(self, now):
|
|
58
|
+
"""Move on; True when finished."""
|
|
59
|
+
t = ticks_diff(now, self.t0)
|
|
60
|
+
if self.kind == "pulse":
|
|
61
|
+
f = 0.5 - 0.5 * math.cos(2 * math.pi * (t % self.ms) / self.ms)
|
|
62
|
+
self.obj.set_style_image_opa(int(self.start + (self.end - self.start) * f), 0)
|
|
63
|
+
return False
|
|
64
|
+
f = 1.0 if t >= self.ms else t / self.ms
|
|
65
|
+
e = 1 - (1 - f) * (1 - f) # ease out
|
|
66
|
+
if self.kind == "slide":
|
|
67
|
+
(x0, y0), (x1, y1) = self.start, self.end
|
|
68
|
+
self.obj.set_pos(int(x0 + (x1 - x0) * e), int(y0 + (y1 - y0) * e))
|
|
69
|
+
else: # fade
|
|
70
|
+
self.obj.set_style_image_opa(int(self.start + (self.end - self.start) * f), 0)
|
|
71
|
+
if f >= 1.0:
|
|
72
|
+
if self.done is not None:
|
|
73
|
+
self.done()
|
|
74
|
+
return True
|
|
75
|
+
return False
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def _device_name():
|
|
79
|
+
try:
|
|
80
|
+
import boarddev
|
|
81
|
+
|
|
82
|
+
return boarddev.name()
|
|
83
|
+
except Exception:
|
|
84
|
+
return "chess"
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def _ways(name):
|
|
88
|
+
"""The ways this device reaches a friend, and why there are none when
|
|
89
|
+
there aren't: the network (not in a browser page) and Bluetooth."""
|
|
90
|
+
from . import ble
|
|
91
|
+
|
|
92
|
+
ways = []
|
|
93
|
+
why = "no network and no Bluetooth"
|
|
94
|
+
if not ble.BROWSER:
|
|
95
|
+
try:
|
|
96
|
+
ways.append(_net.Match(name))
|
|
97
|
+
except Exception as e:
|
|
98
|
+
why = e
|
|
99
|
+
print("chess: no network: %r" % (e,))
|
|
100
|
+
try:
|
|
101
|
+
import bledev.auto
|
|
102
|
+
|
|
103
|
+
if bledev.auto.backend_name() == "webble":
|
|
104
|
+
ways.append(ble.Guest(name))
|
|
105
|
+
elif bledev.auto.backend_name() is not None:
|
|
106
|
+
ways.append(ble.Host(name))
|
|
107
|
+
except Exception as e:
|
|
108
|
+
print("chess: no Bluetooth: %r" % (e,))
|
|
109
|
+
return ways, why
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
class App:
|
|
113
|
+
"""The whole app on *scope*. ``main(scope)`` makes one."""
|
|
114
|
+
|
|
115
|
+
def __init__(self, scope):
|
|
116
|
+
global current
|
|
117
|
+
self.scope = scope
|
|
118
|
+
store = getattr(scope, "store", None)
|
|
119
|
+
self.prefs = store() if store is not None else {}
|
|
120
|
+
self.theme_index = self.prefs.get("theme", 0) % len(art.THEMES)
|
|
121
|
+
self.level = self.prefs.get("level", 2)
|
|
122
|
+
if not 0 <= self.level < len(_game.LEVELS):
|
|
123
|
+
self.level = 2
|
|
124
|
+
#: Against the engine: the side you play, at the bottom of the board.
|
|
125
|
+
self.side = self.prefs.get("side", WHITE)
|
|
126
|
+
self.flipped = self.side == BLACK
|
|
127
|
+
self.mode = ENGINE
|
|
128
|
+
self.game = _game.Game()
|
|
129
|
+
for uci in self.prefs.get("moves", ()):
|
|
130
|
+
if self.game.play_uci(uci) is None:
|
|
131
|
+
break
|
|
132
|
+
self.selected = None
|
|
133
|
+
self.tweens = []
|
|
134
|
+
self.pieces = {} # square -> lv.image
|
|
135
|
+
self.dots = []
|
|
136
|
+
self.thinking = None # the position the engine is searching
|
|
137
|
+
self._think_serial = 0
|
|
138
|
+
self._think_t0 = 0
|
|
139
|
+
self.match = None
|
|
140
|
+
self.friend = None # the other player's name in a game over the network
|
|
141
|
+
self.my_colour = WHITE
|
|
142
|
+
self._invite = None
|
|
143
|
+
self._asking = None # who asked for the game on screen now
|
|
144
|
+
self._overlay = None
|
|
145
|
+
self._trash = []
|
|
146
|
+
self._searching = False
|
|
147
|
+
self._search_t = 0
|
|
148
|
+
self._found = []
|
|
149
|
+
self.status = ui.status(scope)
|
|
150
|
+
scope.root.remove_flag(lv.obj.FLAG.SCROLLABLE) # the glow may hang over the edge
|
|
151
|
+
scope.root.set_style_bg_color(ui.BG, 0)
|
|
152
|
+
scope.root.set_style_bg_opa(lv.OPA.COVER, 0)
|
|
153
|
+
|
|
154
|
+
self._layout()
|
|
155
|
+
self.pictures = art.Pictures(art.THEMES[self.theme_index], self.sq)
|
|
156
|
+
self._build()
|
|
157
|
+
self.sync()
|
|
158
|
+
self.timer = scope.every(TICK_MS, self._tick)
|
|
159
|
+
scope.on_close(self.close)
|
|
160
|
+
current = self
|
|
161
|
+
self._maybe_think()
|
|
162
|
+
# Advertise this game on the network soon after the board shows, so
|
|
163
|
+
# a friend's list finds it before anyone taps Friend here.
|
|
164
|
+
scope.after(800, lambda _t=None: self._open_match(quiet=True))
|
|
165
|
+
|
|
166
|
+
# --- layout ------------------------------------------------------------
|
|
167
|
+
|
|
168
|
+
def _layout(self):
|
|
169
|
+
scope = self.scope
|
|
170
|
+
# Places are in the app's root, which may start below a status bar;
|
|
171
|
+
# a touch comes in screen coordinates, so keep where the root is.
|
|
172
|
+
rect = scope.rect or (0, 0, scope.size[0], scope.size[1])
|
|
173
|
+
self.origin = (rect[0], rect[1])
|
|
174
|
+
rx, ry, rw, rh = 0, 0, rect[2], rect[3]
|
|
175
|
+
pad = 4 if min(rw, rh) < 300 else 10
|
|
176
|
+
if rw >= rh:
|
|
177
|
+
side_w = max(120, rw // 4)
|
|
178
|
+
board = min(rh - 2 * pad, rw - side_w - 3 * pad)
|
|
179
|
+
self.sq = max(16, board // 8)
|
|
180
|
+
b = self.sq * 8
|
|
181
|
+
self.bx = rx + pad
|
|
182
|
+
self.by = ry + (rh - b) // 2
|
|
183
|
+
# Without a status bar the status line sits at the top right: start below it.
|
|
184
|
+
top = pad if getattr(scope, "status", None) is not None else pad + ui.small.line_height + 4
|
|
185
|
+
self.panel = (self.bx + b + pad, ry + top, rx + rw - (self.bx + b + pad) - pad, rh - top - pad)
|
|
186
|
+
self.wide = True
|
|
187
|
+
self.info_lines = 2
|
|
188
|
+
else:
|
|
189
|
+
# Under the board: the turn, two lines of moves and two rows of buttons.
|
|
190
|
+
small = rw < 300 or rh < 480
|
|
191
|
+
self.button_h = (30 if rh < 300 else 34) if small else 44
|
|
192
|
+
# The smallest screens get one line of moves, to leave the board room.
|
|
193
|
+
self.info_lines = 1 if rh < 300 else 2
|
|
194
|
+
text = (ui.tiny if small else ui.big).line_height + self.info_lines * (ui.tiny if small else ui.small).line_height
|
|
195
|
+
board = min(rw - 2 * pad, rh - text - 2 * self.button_h - 4 * pad - 8)
|
|
196
|
+
self.sq = max(16, board // 8)
|
|
197
|
+
b = self.sq * 8
|
|
198
|
+
self.bx = rx + (rw - b) // 2
|
|
199
|
+
self.by = ry + pad
|
|
200
|
+
self.panel = (rx + pad, self.by + b + pad, rw - 2 * pad, ry + rh - (self.by + b + pad) - pad)
|
|
201
|
+
self.wide = False
|
|
202
|
+
|
|
203
|
+
def xy(self, square):
|
|
204
|
+
"""The top left of *square* on screen."""
|
|
205
|
+
col = square % 10 - 1
|
|
206
|
+
row = 9 - square // 10
|
|
207
|
+
if self.flipped:
|
|
208
|
+
col = 7 - col
|
|
209
|
+
row = 7 - row
|
|
210
|
+
return self.bx + col * self.sq, self.by + row * self.sq
|
|
211
|
+
|
|
212
|
+
def square_at(self, x, y):
|
|
213
|
+
"""The square under screen point (*x*, *y*), or None."""
|
|
214
|
+
col = (x - self.bx) // self.sq
|
|
215
|
+
row = (y - self.by) // self.sq
|
|
216
|
+
if not (0 <= col < 8 and 0 <= row < 8):
|
|
217
|
+
return None
|
|
218
|
+
if self.flipped:
|
|
219
|
+
col = 7 - col
|
|
220
|
+
row = 7 - row
|
|
221
|
+
return (9 - row) * 10 + col + 1
|
|
222
|
+
|
|
223
|
+
# --- building the screen -------------------------------------------------
|
|
224
|
+
|
|
225
|
+
def _build(self):
|
|
226
|
+
root = self.scope.root
|
|
227
|
+
theme = self.pictures.theme
|
|
228
|
+
sq = self.sq
|
|
229
|
+
self.board_img = lv.image(root)
|
|
230
|
+
self.board_img.set_src(self.pictures.board())
|
|
231
|
+
self.board_img.set_pos(self.bx, self.by)
|
|
232
|
+
self.board_img.add_flag(lv.obj.FLAG.CLICKABLE)
|
|
233
|
+
self.board_img.add_event_cb(self._on_board, lv.EVENT.PRESSED, None)
|
|
234
|
+
# Marks for the last move and the selected piece, under the pieces.
|
|
235
|
+
self.marks = []
|
|
236
|
+
for _ in range(3):
|
|
237
|
+
m = ui.plain(lv.obj(root))
|
|
238
|
+
m.set_size(sq, sq)
|
|
239
|
+
m.set_style_bg_opa(lv.OPA._40, 0)
|
|
240
|
+
m.remove_flag(lv.obj.FLAG.CLICKABLE)
|
|
241
|
+
m.add_flag(lv.obj.FLAG.HIDDEN)
|
|
242
|
+
self.marks.append(m)
|
|
243
|
+
self._mark_colour(theme)
|
|
244
|
+
self.glow = lv.image(root)
|
|
245
|
+
self.glow.set_src(self.pictures.glow())
|
|
246
|
+
self.glow.remove_flag(lv.obj.FLAG.CLICKABLE)
|
|
247
|
+
self.glow.add_flag(lv.obj.FLAG.HIDDEN)
|
|
248
|
+
self.glow_tween = None
|
|
249
|
+
self.layer = ui.plain(lv.obj(root)) # the pieces and dots go in here, over the marks
|
|
250
|
+
self.layer.set_pos(0, 0)
|
|
251
|
+
self.layer.set_size(self.scope.size[0], self.scope.size[1])
|
|
252
|
+
self.layer.remove_flag(lv.obj.FLAG.CLICKABLE)
|
|
253
|
+
self._build_panel()
|
|
254
|
+
|
|
255
|
+
def _mark_colour(self, theme):
|
|
256
|
+
c = lv.color_hex(theme.mark)
|
|
257
|
+
for m in self.marks[:2]:
|
|
258
|
+
m.set_style_bg_color(c, 0)
|
|
259
|
+
m.set_style_bg_opa(lv.OPA._30, 0)
|
|
260
|
+
self.marks[2].set_style_bg_color(c, 0)
|
|
261
|
+
self.marks[2].set_style_bg_opa(lv.OPA._60, 0)
|
|
262
|
+
|
|
263
|
+
def _build_panel(self):
|
|
264
|
+
px, py, pw, ph = self.panel
|
|
265
|
+
small = (pw < 160 or ph < 200) if self.wide else self.button_h < 40
|
|
266
|
+
self.small_text = small
|
|
267
|
+
font = ui.small if small else ui.font
|
|
268
|
+
self.turn_label = ui.label(self.scope.root, "", ui.tiny if small else ui.big, ui.WHITE)
|
|
269
|
+
self.turn_label.set_height((ui.tiny if small else ui.big).line_height * (2 if self.wide else 1))
|
|
270
|
+
self.turn_label.set_pos(px, py)
|
|
271
|
+
self.turn_label.set_width(pw)
|
|
272
|
+
self.turn_label.set_long_mode(lv.label.LONG_MODE.WRAP)
|
|
273
|
+
self.info_label = ui.label(self.scope.root, "", ui.tiny if small else ui.small, ui.MUTED)
|
|
274
|
+
self.info_label.set_width(pw)
|
|
275
|
+
self.info_label.set_long_mode(lv.label.LONG_MODE.WRAP if self.info_lines > 1 else lv.label.LONG_MODE.DOTS)
|
|
276
|
+
if self.info_lines == 1:
|
|
277
|
+
self.info_label.set_height((ui.tiny if small else ui.small).line_height)
|
|
278
|
+
names = (("New", self.new_game), ("Undo", self.undo), ("Flip", self.flip),
|
|
279
|
+
("Theme", self.next_theme), ("Level", self.next_level), ("Friend", self.friend_button))
|
|
280
|
+
self.buttons = {}
|
|
281
|
+
gap = 4
|
|
282
|
+
turn_font = ui.tiny if small else ui.big
|
|
283
|
+
info_font = ui.tiny if small else ui.small
|
|
284
|
+
if self.wide:
|
|
285
|
+
self.info_label.set_pos(px, py + 2 * turn_font.line_height + 2)
|
|
286
|
+
top = py + 2 * turn_font.line_height + 2 * info_font.line_height + 8
|
|
287
|
+
cols = 2 if pw >= 110 else 1
|
|
288
|
+
bw = (pw - gap * (cols - 1)) // cols
|
|
289
|
+
rows = (len(names) + cols - 1) // cols
|
|
290
|
+
bh = max(22, min(48, (py + ph - top - gap * (rows - 1)) // rows))
|
|
291
|
+
for i, (text, cb) in enumerate(names):
|
|
292
|
+
b, lbl = ui.button(self.scope.root, text, cb, bw, bh, font, ui.PANEL)
|
|
293
|
+
b.set_style_radius(8, 0)
|
|
294
|
+
b.set_style_pad_all(0, 0)
|
|
295
|
+
b.set_pos(px + (i % cols) * (bw + gap), top + (i // cols) * (bh + gap))
|
|
296
|
+
self.buttons[text] = (b, lbl)
|
|
297
|
+
else:
|
|
298
|
+
self.info_label.set_pos(px, py + turn_font.line_height + 4)
|
|
299
|
+
cols = 3
|
|
300
|
+
bw = (pw - gap * (cols - 1)) // cols
|
|
301
|
+
bh = self.button_h
|
|
302
|
+
top = py + ph - 2 * bh - gap
|
|
303
|
+
for i, (text, cb) in enumerate(names):
|
|
304
|
+
b, lbl = ui.button(self.scope.root, text, cb, bw, bh, font, ui.PANEL)
|
|
305
|
+
b.set_style_radius(8, 0)
|
|
306
|
+
b.set_pos(px + (i % cols) * (bw + gap), top + (i // cols) * (bh + gap))
|
|
307
|
+
self.buttons[text] = (b, lbl)
|
|
308
|
+
|
|
309
|
+
# --- drawing the game ----------------------------------------------------
|
|
310
|
+
|
|
311
|
+
def sync(self):
|
|
312
|
+
"""Put every piece where the game has it, with no animation."""
|
|
313
|
+
for img in self.pieces.values():
|
|
314
|
+
img.delete()
|
|
315
|
+
self.pieces = {}
|
|
316
|
+
for t in self.tweens:
|
|
317
|
+
if t.kind == "fade" and t.done is not None:
|
|
318
|
+
t.done()
|
|
319
|
+
self.tweens = [t for t in self.tweens if t.kind == "pulse"]
|
|
320
|
+
for square, code in self.game.squares():
|
|
321
|
+
self._place(square, code)
|
|
322
|
+
self._show_marks()
|
|
323
|
+
self._show_dots()
|
|
324
|
+
self._show_check()
|
|
325
|
+
self._show_text()
|
|
326
|
+
|
|
327
|
+
def _place(self, square, code):
|
|
328
|
+
img = lv.image(self.layer)
|
|
329
|
+
img.set_src(self.pictures.piece(code))
|
|
330
|
+
img.remove_flag(lv.obj.FLAG.CLICKABLE)
|
|
331
|
+
x, y = self.xy(square)
|
|
332
|
+
img.set_pos(x, y)
|
|
333
|
+
self.pieces[square] = img
|
|
334
|
+
return img
|
|
335
|
+
|
|
336
|
+
def _animate(self, move):
|
|
337
|
+
"""Slide what *move* moved and fade what it took."""
|
|
338
|
+
if move.captured and move.captured in self.pieces:
|
|
339
|
+
gone = self.pieces.pop(move.captured)
|
|
340
|
+
self.tweens.append(_Tween(gone, "fade", 255, 0, FADE_MS, gone.delete))
|
|
341
|
+
img = self.pieces.pop(move.frm, None)
|
|
342
|
+
if img is None:
|
|
343
|
+
self.sync()
|
|
344
|
+
return
|
|
345
|
+
self.pieces[move.to] = img
|
|
346
|
+
start = self.xy(move.frm)
|
|
347
|
+
img.move_foreground()
|
|
348
|
+
|
|
349
|
+
def landed():
|
|
350
|
+
if move.promotion and self.pieces.get(move.to) is img:
|
|
351
|
+
img.set_src(self.pictures.piece(move.promotion))
|
|
352
|
+
|
|
353
|
+
self.tweens.append(_Tween(img, "slide", start, self.xy(move.to), SLIDE_MS, landed))
|
|
354
|
+
if move.rook:
|
|
355
|
+
rs, re = move.rook
|
|
356
|
+
rook = self.pieces.pop(rs, None)
|
|
357
|
+
if rook is not None:
|
|
358
|
+
self.pieces[re] = rook
|
|
359
|
+
self.tweens.append(_Tween(rook, "slide", self.xy(rs), self.xy(re), SLIDE_MS))
|
|
360
|
+
|
|
361
|
+
def _show_marks(self):
|
|
362
|
+
last = self.game.last
|
|
363
|
+
for i, square in enumerate((last.frm, last.to) if last else (None, None)):
|
|
364
|
+
m = self.marks[i]
|
|
365
|
+
if square is None:
|
|
366
|
+
m.add_flag(lv.obj.FLAG.HIDDEN)
|
|
367
|
+
else:
|
|
368
|
+
m.set_pos(*self.xy(square))
|
|
369
|
+
m.remove_flag(lv.obj.FLAG.HIDDEN)
|
|
370
|
+
m = self.marks[2]
|
|
371
|
+
if self.selected is None:
|
|
372
|
+
m.add_flag(lv.obj.FLAG.HIDDEN)
|
|
373
|
+
else:
|
|
374
|
+
m.set_pos(*self.xy(self.selected))
|
|
375
|
+
m.remove_flag(lv.obj.FLAG.HIDDEN)
|
|
376
|
+
|
|
377
|
+
def _show_dots(self):
|
|
378
|
+
for d in self.dots:
|
|
379
|
+
d.delete()
|
|
380
|
+
self.dots = []
|
|
381
|
+
if self.selected is None:
|
|
382
|
+
return
|
|
383
|
+
sq = self.sq
|
|
384
|
+
for target in self.game.targets(self.selected):
|
|
385
|
+
d = ui.plain(lv.obj(self.layer))
|
|
386
|
+
d.remove_flag(lv.obj.FLAG.CLICKABLE)
|
|
387
|
+
d.set_style_radius(lv.RADIUS_CIRCLE, 0)
|
|
388
|
+
x, y = self.xy(target)
|
|
389
|
+
if self.game.piece(target):
|
|
390
|
+
# A ring round a piece that can be taken.
|
|
391
|
+
d.set_size(sq - 4, sq - 4)
|
|
392
|
+
d.set_pos(x + 2, y + 2)
|
|
393
|
+
d.set_style_border_width(max(2, sq // 14), 0)
|
|
394
|
+
d.set_style_border_color(lv.color_hex(0x000000), 0)
|
|
395
|
+
d.set_style_border_opa(lv.OPA._50, 0)
|
|
396
|
+
else:
|
|
397
|
+
r = max(6, sq // 3)
|
|
398
|
+
d.set_size(r, r)
|
|
399
|
+
d.set_pos(x + (sq - r) // 2, y + (sq - r) // 2)
|
|
400
|
+
d.set_style_bg_color(lv.color_hex(0x000000), 0)
|
|
401
|
+
d.set_style_bg_opa(lv.OPA._40, 0)
|
|
402
|
+
self.dots.append(d)
|
|
403
|
+
|
|
404
|
+
def _show_check(self):
|
|
405
|
+
if self.game.in_check() and self.game.result is None or (self.game.result and self.game.result[0] == "checkmate"):
|
|
406
|
+
k = self.game.king(self.game.turn)
|
|
407
|
+
x, y = self.xy(k)
|
|
408
|
+
off = (self.sq * 3 // 2 - self.sq) // 2
|
|
409
|
+
self.glow.set_pos(x - off, y - off)
|
|
410
|
+
self.glow.remove_flag(lv.obj.FLAG.HIDDEN)
|
|
411
|
+
if self.glow_tween is None:
|
|
412
|
+
self.glow_tween = _Tween(self.glow, "pulse", 110, 255, 1200)
|
|
413
|
+
self.tweens.append(self.glow_tween)
|
|
414
|
+
else:
|
|
415
|
+
self.glow.add_flag(lv.obj.FLAG.HIDDEN)
|
|
416
|
+
if self.glow_tween is not None:
|
|
417
|
+
self.tweens = [t for t in self.tweens if t is not self.glow_tween]
|
|
418
|
+
self.glow_tween = None
|
|
419
|
+
|
|
420
|
+
def _show_text(self):
|
|
421
|
+
g = self.game
|
|
422
|
+
colour = "White" if g.turn == WHITE else "Black"
|
|
423
|
+
if g.result is not None:
|
|
424
|
+
turn = g.outcome()
|
|
425
|
+
elif self.thinking is not None:
|
|
426
|
+
turn = "%s thinking..." % _game.LEVELS[self.level][0]
|
|
427
|
+
elif self.mode == FRIEND:
|
|
428
|
+
if g.turn == self.my_colour:
|
|
429
|
+
turn = "Your move" + (" (check)" if g.in_check() else "")
|
|
430
|
+
else:
|
|
431
|
+
turn = "Their move"
|
|
432
|
+
else:
|
|
433
|
+
turn = "%s to move" % colour + (": check" if g.in_check() else "")
|
|
434
|
+
self.turn_label.set_text(turn)
|
|
435
|
+
moves = g.moves[-2:]
|
|
436
|
+
line = ""
|
|
437
|
+
if moves:
|
|
438
|
+
n = len(g.moves) - len(moves)
|
|
439
|
+
parts = []
|
|
440
|
+
for i, m in enumerate(moves):
|
|
441
|
+
ply = n + i
|
|
442
|
+
if ply % 2 == 0:
|
|
443
|
+
parts.append("%d. %s" % (ply // 2 + 1, m.san))
|
|
444
|
+
elif not parts:
|
|
445
|
+
parts.append("%d... %s" % (ply // 2 + 1, m.san))
|
|
446
|
+
else:
|
|
447
|
+
parts.append(m.san)
|
|
448
|
+
line = " ".join(parts)
|
|
449
|
+
if self.mode == FRIEND:
|
|
450
|
+
who = "vs %s" % self.friend
|
|
451
|
+
if self.match is not None and not self.match.alive:
|
|
452
|
+
who += " (not heard from)"
|
|
453
|
+
else:
|
|
454
|
+
who = "vs %s" % _game.LEVELS[self.level][0]
|
|
455
|
+
if not self.small_text:
|
|
456
|
+
who += ", " + art.THEMES[self.theme_index].name
|
|
457
|
+
self.info_label.set_text(line + (("\n" if self.info_lines > 1 else " ") if line else "") + who)
|
|
458
|
+
self._label_buttons()
|
|
459
|
+
|
|
460
|
+
def _label_buttons(self):
|
|
461
|
+
friend = self.mode == FRIEND
|
|
462
|
+
self.buttons["Undo"][1].set_text("Resign" if friend else "Undo")
|
|
463
|
+
self.buttons["Level"][1].set_text(_game.LEVELS[self.level][0] if not friend else "Level")
|
|
464
|
+
self.buttons["Friend"][1].set_text("Leave" if friend else "Friend")
|
|
465
|
+
|
|
466
|
+
# --- input -------------------------------------------------------------
|
|
467
|
+
|
|
468
|
+
def _on_board(self, event):
|
|
469
|
+
indev = lv.indev_active()
|
|
470
|
+
if indev is None:
|
|
471
|
+
return
|
|
472
|
+
p = lv.point_t()
|
|
473
|
+
indev.get_point(p)
|
|
474
|
+
self.tap(self.square_at(p.x - self.origin[0], p.y - self.origin[1]))
|
|
475
|
+
|
|
476
|
+
def _my_turn(self):
|
|
477
|
+
g = self.game
|
|
478
|
+
if g.result is not None or self.thinking is not None or self._overlay is not None:
|
|
479
|
+
return False
|
|
480
|
+
if self.mode == FRIEND:
|
|
481
|
+
return g.turn == self.my_colour
|
|
482
|
+
return g.turn == self.side
|
|
483
|
+
|
|
484
|
+
def tap(self, square):
|
|
485
|
+
"""What a tap on *square* does: pick a piece, or move the one picked."""
|
|
486
|
+
if square is None or not self._my_turn():
|
|
487
|
+
return
|
|
488
|
+
g = self.game
|
|
489
|
+
code = g.piece(square)
|
|
490
|
+
if self.selected is not None and square in g.targets(self.selected):
|
|
491
|
+
frm = self.selected
|
|
492
|
+
self.selected = None
|
|
493
|
+
if g.is_promotion(frm, square):
|
|
494
|
+
self._ask_promotion(frm, square)
|
|
495
|
+
else:
|
|
496
|
+
self.move(frm, square)
|
|
497
|
+
return
|
|
498
|
+
if code and (code & 1) == g.turn and g.targets(square):
|
|
499
|
+
self.selected = None if square == self.selected else square
|
|
500
|
+
else:
|
|
501
|
+
self.selected = None
|
|
502
|
+
self._show_marks()
|
|
503
|
+
self._show_dots()
|
|
504
|
+
|
|
505
|
+
def move(self, frm, to, promotion=QUEEN, remote=False):
|
|
506
|
+
"""Play a move on the board; returns its `game.Move` or None."""
|
|
507
|
+
g = self.game
|
|
508
|
+
ply = len(g.moves)
|
|
509
|
+
m = g.play(frm, to, promotion)
|
|
510
|
+
if m is None:
|
|
511
|
+
return None
|
|
512
|
+
self.selected = None
|
|
513
|
+
self._animate(m)
|
|
514
|
+
self._show_marks()
|
|
515
|
+
self._show_dots()
|
|
516
|
+
self._show_check()
|
|
517
|
+
self._save()
|
|
518
|
+
if self.mode == FRIEND and not remote and self.match is not None:
|
|
519
|
+
self.match.send("move", ply, m.uci)
|
|
520
|
+
self._show_text()
|
|
521
|
+
self._maybe_think()
|
|
522
|
+
return m
|
|
523
|
+
|
|
524
|
+
def _ask_promotion(self, frm, to):
|
|
525
|
+
sq = self.sq
|
|
526
|
+
size = sq + 8
|
|
527
|
+
colour = self.game.turn
|
|
528
|
+
box = ui.box(self.scope.root, 0, 0, size * 4 + 16, size + 16)
|
|
529
|
+
box.set_style_pad_all(8, 0)
|
|
530
|
+
box.center()
|
|
531
|
+
box.align_to(self.board_img, lv.ALIGN.CENTER, 0, 0)
|
|
532
|
+
for i, kind in enumerate(_game.PROMOTIONS):
|
|
533
|
+
img = lv.image(box)
|
|
534
|
+
img.set_src(self.pictures.piece(kind | colour))
|
|
535
|
+
img.set_pos(i * size + 4, 4)
|
|
536
|
+
img.add_flag(lv.obj.FLAG.CLICKABLE)
|
|
537
|
+
|
|
538
|
+
def chose(e, kind=kind):
|
|
539
|
+
self._close_overlay()
|
|
540
|
+
self.move(frm, to, kind)
|
|
541
|
+
|
|
542
|
+
img.add_event_cb(chose, lv.EVENT.CLICKED, None)
|
|
543
|
+
self._overlay = box
|
|
544
|
+
|
|
545
|
+
def _close_overlay(self):
|
|
546
|
+
if self._overlay is not None:
|
|
547
|
+
# Deleted on the next tick: the tap that closes it may be one of
|
|
548
|
+
# its own buttons, still in LVGL's hands.
|
|
549
|
+
self._overlay.add_flag(lv.obj.FLAG.HIDDEN)
|
|
550
|
+
self._trash.append(self._overlay)
|
|
551
|
+
self._overlay = None
|
|
552
|
+
self._asking = None
|
|
553
|
+
|
|
554
|
+
# --- the buttons -----------------------------------------------------------
|
|
555
|
+
|
|
556
|
+
def new_game(self):
|
|
557
|
+
self._stop_thinking()
|
|
558
|
+
if self.mode == FRIEND and self.match is not None:
|
|
559
|
+
self.match.send("new")
|
|
560
|
+
self.game = _game.Game()
|
|
561
|
+
self.selected = None
|
|
562
|
+
self._save()
|
|
563
|
+
self.sync()
|
|
564
|
+
self._maybe_think()
|
|
565
|
+
|
|
566
|
+
def undo(self):
|
|
567
|
+
if self.mode == FRIEND:
|
|
568
|
+
if self.game.result is None and self.match is not None:
|
|
569
|
+
self.match.send("resign")
|
|
570
|
+
self.game.finish("resigned", 1 - self.my_colour)
|
|
571
|
+
self.sync()
|
|
572
|
+
return
|
|
573
|
+
self._stop_thinking()
|
|
574
|
+
g = self.game
|
|
575
|
+
# Back to your own move: one ply if the engine hasn't answered yet.
|
|
576
|
+
count = 1 if g.turn != self.side else 2
|
|
577
|
+
g.undo(min(count, len(g.moves)))
|
|
578
|
+
self.selected = None
|
|
579
|
+
self._save()
|
|
580
|
+
self.sync()
|
|
581
|
+
self._maybe_think()
|
|
582
|
+
|
|
583
|
+
def flip(self):
|
|
584
|
+
self.flipped = not self.flipped
|
|
585
|
+
if self.mode == ENGINE:
|
|
586
|
+
self._stop_thinking()
|
|
587
|
+
self.side = BLACK if self.flipped else WHITE
|
|
588
|
+
self.prefs["side"] = self.side
|
|
589
|
+
self.selected = None
|
|
590
|
+
self.sync()
|
|
591
|
+
self._maybe_think()
|
|
592
|
+
|
|
593
|
+
def next_theme(self):
|
|
594
|
+
self.theme_index = (self.theme_index + 1) % len(art.THEMES)
|
|
595
|
+
self.prefs["theme"] = self.theme_index
|
|
596
|
+
old = self.pictures
|
|
597
|
+
self.pictures = art.Pictures(art.THEMES[self.theme_index], self.sq)
|
|
598
|
+
self.board_img.set_src(self.pictures.board())
|
|
599
|
+
self.glow.set_src(self.pictures.glow())
|
|
600
|
+
self._mark_colour(self.pictures.theme)
|
|
601
|
+
self.sync()
|
|
602
|
+
# Only now is nothing on screen showing the old pictures.
|
|
603
|
+
old.forget()
|
|
604
|
+
|
|
605
|
+
def next_level(self):
|
|
606
|
+
if self.mode == FRIEND:
|
|
607
|
+
return
|
|
608
|
+
self.level = (self.level + 1) % len(_game.LEVELS)
|
|
609
|
+
self.prefs["level"] = self.level
|
|
610
|
+
self._show_text()
|
|
611
|
+
|
|
612
|
+
def _save(self):
|
|
613
|
+
self.prefs["moves"] = [m.uci for m in self.game.moves] if self.mode == ENGINE else []
|
|
614
|
+
|
|
615
|
+
# --- the engine ------------------------------------------------------------
|
|
616
|
+
|
|
617
|
+
def _maybe_think(self):
|
|
618
|
+
g = self.game
|
|
619
|
+
if self.mode != ENGINE or g.result is not None or g.turn == self.side or self.thinking is not None:
|
|
620
|
+
return
|
|
621
|
+
_name, depth, ms, noise = _game.LEVELS[self.level]
|
|
622
|
+
pos = g.pos.copy()
|
|
623
|
+
self.thinking = pos
|
|
624
|
+
self._think_serial += 1
|
|
625
|
+
serial = self._think_serial
|
|
626
|
+
self._think_t0 = ticks_ms()
|
|
627
|
+
level = self.level
|
|
628
|
+
|
|
629
|
+
def work():
|
|
630
|
+
return pos.think(depth, ms, noise)
|
|
631
|
+
|
|
632
|
+
def done(found, error):
|
|
633
|
+
if serial != self._think_serial:
|
|
634
|
+
return
|
|
635
|
+
self.thinking = None
|
|
636
|
+
took = ticks_diff(ticks_ms(), self._think_t0)
|
|
637
|
+
if error is not None:
|
|
638
|
+
self.status.set_text("Engine: %s" % (error,))
|
|
639
|
+
print("chess: engine failed: %r" % (error,))
|
|
640
|
+
self._show_text()
|
|
641
|
+
return
|
|
642
|
+
if found is None:
|
|
643
|
+
self._show_text()
|
|
644
|
+
return
|
|
645
|
+
frm, to, score, depth_done = found
|
|
646
|
+
print("chess: %s %s depth %d, %d nodes, %d ms" % (
|
|
647
|
+
_game.LEVELS[level][0], engine.square_name(frm) + engine.square_name(to), depth_done, pos.nodes, took))
|
|
648
|
+
self.status.set_text("depth %d, %.1f s" % (depth_done, took / 1000))
|
|
649
|
+
self.move(frm, to, QUEEN)
|
|
650
|
+
|
|
651
|
+
self._show_text()
|
|
652
|
+
self.scope.worker.submit(work, done, key="chess-think")
|
|
653
|
+
|
|
654
|
+
def _stop_thinking(self):
|
|
655
|
+
if self.thinking is not None:
|
|
656
|
+
self.thinking.halt()
|
|
657
|
+
self.thinking = None
|
|
658
|
+
self._think_serial += 1
|
|
659
|
+
|
|
660
|
+
# --- two players -----------------------------------------------------------
|
|
661
|
+
|
|
662
|
+
def _open_match(self, quiet=False):
|
|
663
|
+
if self.match is None:
|
|
664
|
+
if self.scope.closed:
|
|
665
|
+
return False
|
|
666
|
+
ways, why = _ways(_device_name())
|
|
667
|
+
if not ways:
|
|
668
|
+
if not quiet:
|
|
669
|
+
self.status.set_text("No network game here: %s" % (why,))
|
|
670
|
+
return False
|
|
671
|
+
self.match = _net.Matches(_device_name(), ways)
|
|
672
|
+
return True
|
|
673
|
+
|
|
674
|
+
def friend_button(self):
|
|
675
|
+
if self.mode == FRIEND:
|
|
676
|
+
self.leave()
|
|
677
|
+
return
|
|
678
|
+
if self._overlay is not None:
|
|
679
|
+
self._close_overlay()
|
|
680
|
+
return
|
|
681
|
+
if not self._open_match():
|
|
682
|
+
return
|
|
683
|
+
if not getattr(self.match, "can_find", True) and getattr(self.match, "can_pick", False):
|
|
684
|
+
self._pick()
|
|
685
|
+
return
|
|
686
|
+
self._show_friends()
|
|
687
|
+
|
|
688
|
+
def _pick(self):
|
|
689
|
+
# A browser page: its Bluetooth chooser lists the boards and
|
|
690
|
+
# computers playing Chess, and the one picked is asked for a game.
|
|
691
|
+
self._close_overlay()
|
|
692
|
+
bw = self.sq * 8 - 24
|
|
693
|
+
box = ui.box(self.scope.root, self.bx + 12, self.by + 12, bw, self.sq * 8 - 24)
|
|
694
|
+
box.set_style_bg_opa(lv.OPA._90, 0)
|
|
695
|
+
title = ui.label(box, "Play a friend over Bluetooth", ui.small, ui.WHITE)
|
|
696
|
+
title.set_width(bw - 24)
|
|
697
|
+
title.set_long_mode(lv.label.LONG_MODE.WRAP)
|
|
698
|
+
title.set_pos(0, 0)
|
|
699
|
+
self._list_label = ui.label(box, "Pick the board or computer running Chess in the browser's list. You play White.", ui.tiny, ui.MUTED)
|
|
700
|
+
self._list_label.set_width(bw - 24)
|
|
701
|
+
self._list_label.set_long_mode(lv.label.LONG_MODE.WRAP)
|
|
702
|
+
self._list_label.set_pos(0, ui.small.line_height * 2 + 2)
|
|
703
|
+
self._list_buttons = []
|
|
704
|
+
close, _ = ui.button(box, "Close", self._close_overlay, None, None, ui.small, ui.KEY)
|
|
705
|
+
close.align(lv.ALIGN.BOTTOM_RIGHT, 0, 0)
|
|
706
|
+
self._overlay = box
|
|
707
|
+
self._invite = "the other player"
|
|
708
|
+
self.match.pick()
|
|
709
|
+
|
|
710
|
+
def _show_friends(self):
|
|
711
|
+
self._close_overlay()
|
|
712
|
+
bw = self.sq * 8 - 24
|
|
713
|
+
box = ui.box(self.scope.root, self.bx + 12, self.by + 12, bw, self.sq * 8 - 24)
|
|
714
|
+
box.set_style_bg_opa(lv.OPA._90, 0)
|
|
715
|
+
title = ui.label(box, "Play a friend on this network", ui.small, ui.WHITE)
|
|
716
|
+
title.set_width(bw - 24)
|
|
717
|
+
title.set_long_mode(lv.label.LONG_MODE.WRAP)
|
|
718
|
+
title.set_pos(0, 0)
|
|
719
|
+
self._list_label = ui.label(box, "Looking...", ui.tiny, ui.MUTED)
|
|
720
|
+
self._list_label.set_pos(0, ui.small.line_height * 2 + 2)
|
|
721
|
+
self._list_buttons = []
|
|
722
|
+
close, _ = ui.button(box, "Close", self._close_overlay, None, None, ui.small, ui.KEY)
|
|
723
|
+
close.align(lv.ALIGN.BOTTOM_RIGHT, 0, 0)
|
|
724
|
+
self._overlay = box
|
|
725
|
+
self._search_t = ticks_ms() - SEARCH_MS
|
|
726
|
+
self._found = []
|
|
727
|
+
|
|
728
|
+
def _search(self):
|
|
729
|
+
if self._searching or self.match is None:
|
|
730
|
+
return
|
|
731
|
+
self._searching = True
|
|
732
|
+
match = self.match
|
|
733
|
+
|
|
734
|
+
def done(found, error):
|
|
735
|
+
self._searching = False
|
|
736
|
+
if error is not None:
|
|
737
|
+
print("chess: search: %r" % (error,))
|
|
738
|
+
return
|
|
739
|
+
self._found = found
|
|
740
|
+
self._fill_friends()
|
|
741
|
+
|
|
742
|
+
self.scope.worker.submit(lambda: match.find(timeout=2), done, key="chess-find")
|
|
743
|
+
|
|
744
|
+
def _fill_friends(self):
|
|
745
|
+
if self._overlay is None or self._invite is not None:
|
|
746
|
+
return
|
|
747
|
+
for b in self._list_buttons:
|
|
748
|
+
b.delete()
|
|
749
|
+
self._list_buttons = []
|
|
750
|
+
if not self._found:
|
|
751
|
+
self._list_label.set_text("Looking... open Chess on another device on this network.")
|
|
752
|
+
return
|
|
753
|
+
self._list_label.set_text("Tap one to ask for a game. You play White.")
|
|
754
|
+
y = ui.small.line_height * 2 + ui.tiny.line_height + 8
|
|
755
|
+
h = max(28, min(40, self.sq))
|
|
756
|
+
for name, address in self._found[:4]:
|
|
757
|
+
b, _ = ui.button(self._overlay, name, lambda a=address, n=name: self.invite(n, a), self.sq * 8 - 48, h, ui.small, ui.PANEL)
|
|
758
|
+
b.set_style_radius(8, 0)
|
|
759
|
+
b.set_pos(0, y)
|
|
760
|
+
y += h + 4
|
|
761
|
+
self._list_buttons.append(b)
|
|
762
|
+
|
|
763
|
+
def invite(self, name, address):
|
|
764
|
+
"""Ask the Chess app called *name* at *address* for a game."""
|
|
765
|
+
try:
|
|
766
|
+
self.match.invite(address)
|
|
767
|
+
except Exception as e:
|
|
768
|
+
self.status.set_text("%s didn't answer" % name)
|
|
769
|
+
print("chess: invite: %r" % (e,))
|
|
770
|
+
return
|
|
771
|
+
self._list_label.set_text("Asked %s. Waiting for an answer..." % name)
|
|
772
|
+
for b in self._list_buttons:
|
|
773
|
+
b.delete()
|
|
774
|
+
self._list_buttons = []
|
|
775
|
+
self._invite = name
|
|
776
|
+
|
|
777
|
+
def _start_friend_game(self, name, colour):
|
|
778
|
+
self._stop_thinking()
|
|
779
|
+
self._close_overlay()
|
|
780
|
+
self._invite = None
|
|
781
|
+
self.mode = FRIEND
|
|
782
|
+
self.friend = name
|
|
783
|
+
self.my_colour = colour
|
|
784
|
+
self.flipped = colour == BLACK
|
|
785
|
+
self.game = _game.Game()
|
|
786
|
+
self.selected = None
|
|
787
|
+
self.status.set_text("Playing %s" % name)
|
|
788
|
+
self.sync()
|
|
789
|
+
|
|
790
|
+
def leave(self):
|
|
791
|
+
"""End the game with the friend and go back to the engine."""
|
|
792
|
+
if self.match is not None:
|
|
793
|
+
self.match.send("bye")
|
|
794
|
+
self.match.forget_peer()
|
|
795
|
+
self.mode = ENGINE
|
|
796
|
+
self.friend = None
|
|
797
|
+
self.flipped = self.side == BLACK
|
|
798
|
+
self.game = _game.Game()
|
|
799
|
+
self._save()
|
|
800
|
+
self.sync()
|
|
801
|
+
self._maybe_think()
|
|
802
|
+
|
|
803
|
+
def _asked(self, name):
|
|
804
|
+
# Another player asks for a game: they play White.
|
|
805
|
+
self._close_overlay()
|
|
806
|
+
self._asking = name
|
|
807
|
+
box = ui.box(self.scope.root, self.bx + 12, self.by + self.sq * 2, self.sq * 8 - 24, self.sq * 4)
|
|
808
|
+
box.set_style_bg_opa(lv.OPA._90, 0)
|
|
809
|
+
msg = ui.label(box, "%s asks for a game. You'd play Black." % name, ui.small, ui.WHITE)
|
|
810
|
+
msg.set_width(self.sq * 8 - 48)
|
|
811
|
+
msg.set_long_mode(lv.label.LONG_MODE.WRAP)
|
|
812
|
+
h = max(28, min(40, self.sq))
|
|
813
|
+
|
|
814
|
+
def accept():
|
|
815
|
+
self.match.send("accept", self.match.name)
|
|
816
|
+
self._start_friend_game(name, BLACK)
|
|
817
|
+
|
|
818
|
+
def decline():
|
|
819
|
+
self.match.send("decline", self.match.name)
|
|
820
|
+
self.match.forget_peer()
|
|
821
|
+
self._close_overlay()
|
|
822
|
+
|
|
823
|
+
yes, _ = ui.button(box, "Play", accept, None, h, ui.small, ui.ACCENT)
|
|
824
|
+
yes.align(lv.ALIGN.BOTTOM_LEFT, 0, 0)
|
|
825
|
+
no, _ = ui.button(box, "No thanks", decline, None, h, ui.small, ui.KEY)
|
|
826
|
+
no.align(lv.ALIGN.BOTTOM_RIGHT, 0, 0)
|
|
827
|
+
self._overlay = box
|
|
828
|
+
|
|
829
|
+
def _network(self):
|
|
830
|
+
match = self.match
|
|
831
|
+
for kind, args in match.poll():
|
|
832
|
+
if kind == "invite" and args:
|
|
833
|
+
# An invite sent again (its answer was lost) must not
|
|
834
|
+
# interrupt the game it started, nor ask twice.
|
|
835
|
+
if (self.mode == FRIEND and match.alive) or self._asking == args[0]:
|
|
836
|
+
continue
|
|
837
|
+
self._asked(args[0])
|
|
838
|
+
elif kind == "accept" and self._invite is not None:
|
|
839
|
+
self._start_friend_game(args[0] if args else self._invite, WHITE)
|
|
840
|
+
elif kind == "decline" and self._invite is not None:
|
|
841
|
+
self._invite = None
|
|
842
|
+
match.forget_peer()
|
|
843
|
+
if self._overlay is not None:
|
|
844
|
+
self._list_label.set_text("They said no thanks.")
|
|
845
|
+
elif kind in ("unreachable", "bye") and self._invite is not None and self.mode != FRIEND:
|
|
846
|
+
# The chooser closed, or the link fell before an answer.
|
|
847
|
+
self._invite = None
|
|
848
|
+
match.forget_peer()
|
|
849
|
+
if self._overlay is not None:
|
|
850
|
+
self._list_label.set_text("No game: %s" % (args[0] if args else "they left"))
|
|
851
|
+
elif self.mode != FRIEND:
|
|
852
|
+
continue
|
|
853
|
+
elif kind == "move" and len(args) == 2:
|
|
854
|
+
ply = int(args[0])
|
|
855
|
+
if ply != len(self.game.moves):
|
|
856
|
+
continue # one we've played already
|
|
857
|
+
parsed = _game.parse_uci(args[1])
|
|
858
|
+
if parsed is None or self.game.turn == self.my_colour:
|
|
859
|
+
continue
|
|
860
|
+
frm, to, promo = parsed
|
|
861
|
+
self.move(frm, to, promo or QUEEN, remote=True)
|
|
862
|
+
elif kind == "resign":
|
|
863
|
+
self.game.finish("resigned", self.my_colour)
|
|
864
|
+
self.sync()
|
|
865
|
+
elif kind == "new":
|
|
866
|
+
self.game = _game.Game()
|
|
867
|
+
self.selected = None
|
|
868
|
+
self.sync()
|
|
869
|
+
elif kind == "bye":
|
|
870
|
+
self.game.finish("left", self.my_colour if self.game.result is None else None)
|
|
871
|
+
self.sync()
|
|
872
|
+
if self.mode == FRIEND:
|
|
873
|
+
alive = match.alive
|
|
874
|
+
if alive != getattr(self, "_was_alive", None):
|
|
875
|
+
self._was_alive = alive
|
|
876
|
+
self._show_text()
|
|
877
|
+
|
|
878
|
+
# --- the loop --------------------------------------------------------------
|
|
879
|
+
|
|
880
|
+
def _tick(self, _timer=None):
|
|
881
|
+
now = ticks_ms()
|
|
882
|
+
while self._trash:
|
|
883
|
+
self._trash.pop().delete()
|
|
884
|
+
if self.tweens:
|
|
885
|
+
self.tweens = [t for t in self.tweens if not t.step(now)]
|
|
886
|
+
if self.match is not None:
|
|
887
|
+
try:
|
|
888
|
+
self._network()
|
|
889
|
+
except Exception as e:
|
|
890
|
+
print("chess: network: %r" % (e,))
|
|
891
|
+
if (
|
|
892
|
+
self._overlay is not None
|
|
893
|
+
and self._invite is None
|
|
894
|
+
and self.mode != FRIEND
|
|
895
|
+
and getattr(self.match, "can_find", True)
|
|
896
|
+
and ticks_diff(now, self._search_t) >= SEARCH_MS
|
|
897
|
+
):
|
|
898
|
+
self._search_t = now
|
|
899
|
+
self._search()
|
|
900
|
+
if self.thinking is not None and self.status is not None:
|
|
901
|
+
took = ticks_diff(now, self._think_t0)
|
|
902
|
+
if took > 500 and (took // 100) % 5 == 0:
|
|
903
|
+
self.status.set_text("Thinking %.1f s" % (took / 1000))
|
|
904
|
+
|
|
905
|
+
def close(self):
|
|
906
|
+
global current
|
|
907
|
+
self._stop_thinking()
|
|
908
|
+
if self.match is not None:
|
|
909
|
+
try:
|
|
910
|
+
self.match.close()
|
|
911
|
+
except Exception as e:
|
|
912
|
+
print("chess: closing the network: %r" % (e,))
|
|
913
|
+
self.match = None
|
|
914
|
+
# Nothing may show a picture once its pixels are let go.
|
|
915
|
+
for obj in [self.board_img, self.glow, self.layer] + self.marks + self._trash:
|
|
916
|
+
obj.delete()
|
|
917
|
+
self._trash = []
|
|
918
|
+
if self._overlay is not None:
|
|
919
|
+
self._overlay.delete()
|
|
920
|
+
self._overlay = None
|
|
921
|
+
self.pictures.forget()
|
|
922
|
+
current = None
|