-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.yue
executable file
·306 lines (274 loc) · 7.61 KB
/
client.yue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
require'prerequisites'
import 'BearLibTerminal' as terminal
import ADDRESS, PORT from config
import abs, clamp, floor, max, random from math
import insert from table
import format from string
global TTY
global BOARD
Screen, Board = require'class.screen', require'class.board'
FLOOR, WALL = 0, 1
Floor, Wall = require'class.turf.floor', require'class.turf.wall'
import 'rlsh'
import time from rlsh
import 'colors'
import 'class.vector2' as Vec2
class CLIENT using Accessors
set: (...) => terminal.set ...
delay: (...) => terminal.delay ...
refresh: => terminal.refresh!
read: => terminal.read!
read_str: (x=0,
y=0,
s=" ",
maxW=@width - #s) => terminal.read_str x, y, s, maxW
color_from_name: (name) => terminal.color_from_name name
close: =>
@disconnect!
terminal.close!
clear: => terminal.clear!
clear_area: (x=0,
y=0,
w=@width,
h=@height) => terminal.clear_area x, y, w, h
peek: => terminal.peek!
state: (...) => terminal.state ...
check: (...) => terminal.check ...
put: (x, y, ...) => terminal.put x, y, ...
put_ext: (x, y, x2, y2, ...) => terminal.put_ext x, y, x2, y2, ...
layer: (...) => terminal.layer ...
print: (x, y, ...) => terminal.print x, y, ...
printy: (x, y, x2, y2, a, ...) => @print x, y, x2, y2, a, ...
printf: (x, y, ...) => @print x, y, ...
printyf: (x, y, x2, y2, a, ...) => @print x, y, x2, y2, a, ...
measure: (s) => terminal.measure s --vararg: num width, num height
@accessor 'title',
get: => @_title
set: (@_title) => @set"window.title = '#{@title}'"
@accessor 'typeface',
get: => @_typeface
set: (@_typeface) => @set"font: #{@typeface}, size=#{@font_size};"
@accessor 'font_size',
get: => @_font_size
set: (@_font_size) => @set"font: #{@typeface}, size=#{@font_size};"
@accessor 'cursor',
get: => @_cursor
set: (@_cursor) => @inbuf, @enbuf = @textbuf\sub(1, @cursor), @textbuf\sub(@cursor + 1)
@accessor 'width',
get: => @state TK_WIDTH
@accessor 'height',
get: => @state TK_HEIGHT
@accessor 'W',
get: => @width
@accessor 'H',
get: => @height
@accessor 'cell_width',
get: => terminal.state TK_CELL_WIDTH
@accessor 'cell_height',
get: => terminal.state TK_CELL_HEIGHT
@accessor 'cellW',
get: => @cell_width
@accessor 'cellH',
get: => @cell_height
@accessor 'color',
get: => terminal.state TK_COLOR
set: (c) =>
if istable c
c = terminal.color_from_argb c.a, c.r, c.g, c.b
elseif isstring c
c = terminal.color_from_name c
terminal.color c
@accessor 'bkcolor',
get: => terminal.state TK_BKCOLOR
set: (c) =>
if istable c
c = terminal.color_from_argb c.a, c.r, c.g, c.b
elseif isstring c
c = terminal.color_from_name c
terminal.bkcolor c
@accessor 'mouse_x',
get: => @state TK_MOUSE_X
@accessor 'mouse_y',
get: => @state TK_MOUSE_Y
@accessor 'mouse_v2',
get: => Vec2 @state(TK_MOUSE_X), @state(TK_MOUSE_Y)
@accessor 'has_input',
get: => terminal.has_input!
frametime: 1/60
frametimer: 0
running: true
new: (w = 80,
h = 30,
scale = 1,
@typeface = "resource/font/FSEX302.ttf",
@font_size = 24) =>
@ticks = 0
@steps = 0
@textbuf = ''
@inbuf = ''
@enbuf = ''
@caret = '_'
@cursor = 0
@vursor = 1
@size = 0
@history = {}
@addr = ADDRESS
@port = PORT
@updr = 0.23
terminal.open!
terminal.set"window: size=#{floor w}x#{floor h};"
terminal.set"font: #{@typeface}, size=#{@font_size};"
terminal.set"input.filter = [keyboard+, mouse+]"
terminal.set"input.mouse-cursor = false;"
@title = "rlsh@#{@addr}:#{@port}"
TTY = @
BOARD = Board!
@refresh!
@connect!
@step! while @running
connect: =>
@socket = socket.udp!
@socket\setpeername @addr, @port
@socket\settimeout 0
@send 'login'
disconnect: =>
@send 'logout'
@socket\close!
print '* disconnected'
send: (cmd, data={}) =>
data._cmd = cmd
@socket\send bitser.dumps data
update: (dt) =>
--ease.update dt
@ticks += dt
@caret = (time! % .6 < .3) and 0x2581 or 0x20
if @ticks > @updr
@ticks -= @updr
@update_countdown ??= 10
@update_countdown -= 1
if @update_countdown < 0
@send 'update'
@update_countdown = 10
if @line
@send @line
@line = nil
data, _error = @socket\receive!
while data
data = bitser.loads data
local _cmd
if type(data) == 'string'
_cmd = data
else
import _cmd from data
switch _cmd
when 'batch'
@timeouts = 0
import cells from data
for turf in *cells
import x, y, glyph, fg, bg from turf
BOARD\set_cell x, y,
:glyph
:fg
:bg
when 'update'
import x, y, glyph, fg, bg from data
BOARD\set_cell x, y,
:glyph
:fg
:bg
when 'handshake'
print '* connected to', @addr, @port
@send 'handshake'
when 'pong'
@ping = false
else
@history[] = data
print data
data, _error = @socket\receive!
if _error
@timeouts ??= 0
if _error != 'timeout'
error 'unknown network error: ' .. tostring _error
input: (key, char) =>
switch key
when TK_CLOSE
@close!
when TK_RETURN
if #@textbuf > 0
switch @textbuf
when '@exit', '@quit'
@close!
else
@send 'post',
text: @textbuf
@history[] = @textbuf
@textbuf = ''
@inbuf, @enbuf = '', ''
@size, @cursor = 0, 0
when TK_BACKSPACE
@size = math.max 0, #@textbuf - 1
@textbuf = @inbuf\sub(1, @cursor - 1) .. @enbuf
@cursor = max 0, @cursor - 1
when TK_RIGHT then @cursor += 1
when TK_LEFT then @cursor = math.max 0, @cursor - 1
when TK_HOME then @cursor = 0
when TK_END then @cursor = @size
when TK_UP
@vursor = math.min #@history, @vursor + 1
@textbuf = @history[@vursor] or ''
@size = #@textbuf
@cursor = @size
when TK_DOWN
@vursor = math.max 1, @vursor - 1
@textbuf = @history[@vursor]
@size = #@textbuf
@cursor = @size
else
if @check TK_CHAR
@textbuf = @inbuf .. string.char char .. @enbuf
@cursor += 1
@size += #@textbuf
render: (...) =>
@layer 0
@clear_area!
for _, x, y, cell in BOARD\each!
@color = cell.fg or 'white'
@bkcolor = cell.bg or 'black'
@put x-1, y-1, cell.glyph
text = @inbuf .. @enbuf
@print 0, @height - 1, text
for i=1, 4
break if #@history < i
mi = #@history - i + 1
@print 0, @height - 1 - i, @history[mi]
@layer 1
@clear_area!
@color = colors.Red
@put @cursor, @height - 1, @caret
@put @mouse_x, @mouse_y, 0x25CB
step: =>
if @has_input
@input @read!, @state TK_CHAR
dt = if @time
offset = @time
@time = time!
@time - offset
else
@time = time!
0
for v in *{1/2, 1, 2}
v *= @frametime
dt = v if abs(dt - v) < .002
dt = clamp dt, 0, @frametime*2
@frametimer += dt
@frametimer = clamp @frametimer, 0, @frametime*8
ticked = false
while @frametimer > @frametime
@frametimer -= @frametime
@update @frametime
ticked = true
if ticked
@render @frametimer / @frametime
@refresh!
@delay 1
CLIENT!