-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.coffee
302 lines (263 loc) · 8.5 KB
/
client.coffee
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
Comments = require 'comments'
Db = require 'db'
Dom = require 'dom'
Modal = require 'modal'
Obs = require 'obs'
Plugin = require 'plugin'
Page = require 'page'
Server = require 'server'
Ui = require 'ui'
Form = require 'form'
Time = require 'time'
Social = require 'social'
Draughts = require 'draughts'
{tr} = require 'i18n'
modes = ['International Draughts','Siamese Draughts']
exports.renderSettings = !->
if Db.shared
Ui.emptyText tr("Game has started - %1", modes[Db.shared.get('mode')||0])
else
userCnt = Plugin.users.count().get()
selUserId = null
if userCnt is 2
for userId, v of Plugin.users.get()
if +userId isnt Plugin.userId()
selUserId = userId
break
selectMember
name: 'opponent'
value: selUserId
title: tr("Opponent")
Form.condition (val) ->
tr("Please select an opponent") if !val.opponent
selectMode
name: 'mode'
title: tr('Mode')
exports.render = !->
whiteId = Db.shared.get('white')
blackId = Db.shared.get('black')
color = if Plugin.userId() is whiteId
'white'
else if Plugin.userId() is blackId
'black'
Comments.enable
messages:
move: (c) -> tr("%1 moved %2", c.user, c.v)
resign: (c) -> tr("%1 resigned", c.user)
proposeDraw: (c) -> tr("%1 proposed a draw", c.user)
acceptDraw: (c) -> tr("%1 accepted the draw", c.user)
declineDraw: (c) -> tr("%1 declined the draw", c.user)
if challenge = Db.shared.get('challenge')
Dom.div !->
Dom.style
textAlign: 'center'
fontSize: '120%'
Dom.text tr("%1 (white) vs %2 (black)", Plugin.userName(whiteId), Plugin.userName(blackId))
if challenge[Plugin.userId()]
Dom.div tr("%1 challenged you for a game of %2.", Plugin.userName(Plugin.ownerId()), modes[Db.shared.get 'mode'])
Ui.bigButton tr("Accept"), !->
Server.call 'accept'
else
break for id of challenge
Dom.div tr("Waiting for %1 to accept...", Plugin.userName(id))
else
isBlack = Db.shared.get('black') is Plugin.userId() and Db.shared.get('white') isnt Plugin.userId()
renderSide = (side) !->
Dom.div !->
Dom.style
textAlign: 'center'
fontSize: '130%'
padding: '8px 0'
color: 'inherit'
fontWeight: 'normal'
id = Db.shared.get(side)
if id is Plugin.userId()
Dom.text tr("You")
else
Dom.text Plugin.userName(id)
if result = Db.shared.get('result')
Dom.style fontWeight: 'bold'
if result is side
if id is Plugin.userId()
Dom.text " - win!"
else
Dom.text " - wins!"
else if result is 'draw'
Dom.text " - draw"
else if result
Dom.text " - lost"
else if Db.shared.get('turn') is side
if id is Plugin.userId()
Dom.style color: Plugin.colors().highlight, fontWeight: 'bold'
Dom.text " - to move"
renderSide if isBlack then 'white' else 'black'
Dom.div !->
Dom.style
Box: 'center'
margin: '4px 0'
selected = Obs.create false
markers = Obs.create {}
# Draughts field index indicating last-moved-piece, selected, possible-move
Obs.observe !->
if last = Db.shared.get('last')
markers.set last[0], 'last'
markers.set last[1], 'last'
if s = selected.get()
s-=1
markers.set s, 'selected'
for pos in Draughts.find(s)
log 'pos: '+pos
markers.set pos, 'move'
# require('toast').show tr(""+s)
Obs.onClean !->
markers.set {}
Dom.div !->
size = 0 | Math.max(200, Math.min(Page.width() - 16, 480)) / 10
Dom.cls 'board'
Dom.style width: "#{size * 10}px"
(if isBlack then [0..9] else [9..0]).forEach (board_y) !->
Dom.div !->
(if isBlack then [9..0] else [0..9]).forEach (board_x) !->
Dom.div !->
cb = (board_x % 2) is (board_y % 2)
Dom.cls 'square'
if cb
Dom.cls 'black'
index = Draughts.index(board_x, board_y)
piece = Db.shared.get('board',index)
# Dom.text index
if marker = markers.get(index)
Dom.div !->
Dom.style
position: 'absolute'
width: if piece then '90%' else '50%'
height: if piece then '90%' else '50%'
left: if piece then '5%' else '25%'
top: if piece then '5%' else '25%'
background: if marker is 'last' then Plugin.colors().bar else Plugin.colors().highlight
opacity: if marker is 'last' then .6 else 0.9
borderRadius: '999px'
if piece
Dom.div !->
Dom.style
position: 'absolute'
left: 0
top: 0
width: '100%'
height: '100%'
background: "url(#{Plugin.resourceUri piece + '.png'}) no-repeat 50% 50%"
backgroundSize: "#{0 | size * .75}px"
if not Db.shared.get('result')
Dom.onTap !->
turn = Db.shared.get('turn')
if turn is color
s = selected.get()
if !s and piece and piece[0] is turn[0]
selected.set index+1
return
if s and s-1 isnt index and Db.shared.peek('board',index)?[0] isnt turn[0]
s-=1
log 'move', s, '>', index
if markers.get(index) is 'move' and Draughts.canMove(s, index)
# log 'server call'
Server.call 'move', s, index
# log 'done server call'
else
require('toast').show tr("Invalid move!")
selected.set false
renderSide if isBlack then 'black' else 'white'
if (Plugin.userId() is blackId or Plugin.userId() is whiteId) and not Db.shared.get('result')
Dom.div !->
Dom.style textAlign: 'center'
if not (draw = Db.shared.get 'draw')
Ui.button tr("Propose Draw"), !->
Modal.show tr("Are you sure you want to propose a draw?"), null, (choice) !->
if choice is 'yes'
log "draw"
Server.call 'draw'
, ['no',tr('No'),'yes',tr('Yes')]
else if Db.shared.get(draw) is Plugin.userId()
Ui.button tr("Decline Draw"), !->
Modal.show tr("Are you sure you want to decline the draw?"), null, (choice) !->
if choice is 'yes'
log "draw decline"
Server.call 'draw_decline'
, ['no',tr('No'),'yes',tr('Yes')]
Ui.button tr("Accept Draw"), !->
Modal.show tr("Are you sure you want to accept the draw?"), null, (choice) !->
if choice is 'yes'
log "draw accept"
Server.call 'draw_accept'
, ['no',tr('No'),'yes',tr('Yes')]
else
Dom.div !->
Dom.text('Waiting for a response to your draw request.')
Ui.button tr("Resign"), !->
Modal.show tr("Are you sure you want to resign?"), null, (choice) !->
if choice is 'yes'
log "resign"
Server.call 'resign'
, ['no',tr('No'),'yes',tr('Yes')]
# input that handles selection of a member
selectMember = (opts) !->
opts ||= {}
[handleChange, initValue] = Form.makeInput opts, (v) ->
0 | v
value = Obs.create(initValue)
Form.box !->
Dom.style fontSize: '125%', marginBottom: '12px'
Dom.text opts.title || tr("Selected member")
v = value.get()
Dom.div !->
Dom.style color: (if v then 'inherit' else '#aaa')
Dom.text (if v then Plugin.userName(v) else tr("Nobody"))
if v
Ui.avatar Plugin.userAvatar(v), style: position: 'absolute', right: '-28px', top: '50%', marginTop: '-20px'
Dom.onTap !->
Modal.show opts.selectTitle || tr("Select member"), !->
Plugin.users.iterate (user) !->
if Plugin.userId() isnt user
Ui.item !->
Ui.avatar key: user.get('avatar'), style: marginRight: '12px'
Dom.text user.get('name')
if +user.key() is +value.get()
Dom.style fontWeight: 'bold'
Dom.div !->
Dom.style
Flex: 1
padding: '0 10px'
textAlign: 'right'
fontSize: '150%'
color: Plugin.colors().highlight
Dom.text "✓"
Dom.onTap !->
handleChange user.key()
value.set user.key()
Modal.remove()
, (choice) !->
log 'choice', choice
if choice is 'clear'
handleChange ''
value.set ''
, ['cancel', tr("Cancel"), 'clear', tr("Clear")]
############### MODE
selectMode = (opts) !->
opts ||= {}
Form.segmented
name: 'mode'
value: 0
segments: [0, modes[0], 1, modes[1]]
onChange: (v) !->
log "on change", v
Dom.css
'.board':
boxShadow: '0 0 8px #000'
'.square':
display: 'inline-block'
width: '10%'
padding: '10% 0 0' # use padding-top trick to maintain aspect ratio
position: 'relative'
'.square.white':
backgroundColor: 'rgb(244,234,193)'
'.square.black':
backgroundColor: 'rgb(223,180,135)'