-
Notifications
You must be signed in to change notification settings - Fork 1
/
view.html
297 lines (223 loc) · 9.24 KB
/
view.html
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
<!--
// Javascript bughouse viewer v 1.01 (C) Sergiy Vasylkevych aka Fermy on FICS
// Feel free to use/copy/modify/distribute
// This update 02.21.2002
//-->
<!--
// I tried to modify it as less as possible
// I made the part that feeds it with chess.com games
// Pieces and things are from https://bughousedb.com/
// No idea about the rights -- bmacho
//
// last modification: 04.19.2021
//-->
<style> .debug {visibility: hidden} </style>
<script type="text/javascript" src="hy.js/hy.js"></script>
<script src="jquery-3.5.1.min.js" ></script>
<script src="chesscom_movelist_parse.js" ></script>
<script src="generate_bpgn.js" ></script>
<script>
var searchParams = new URLSearchParams(window.location.search)
var flip = ( searchParams.get('flip') == "true" || searchParams.get('flip') == "1" ) ? 'flip' : 'noflip'
var hide_elo = ( searchParams.get('anon') == "true" || searchParams.get('anon') == "1") ? true : false
</script>
<script type="text/javascript">
<!--
Init();
v1=new game("v1","", "","", "", "playb", "", "hy.js/themes/gif0/", 40, "silver",flip);//-->
</script>
<body>
<textarea id="pasteHere" style="
position: absolute;
border-radius: 25px;
width: 70%;
height: 30%;
top: 15%;
left: 15%;
text-align: center; vertical-align: middle;
font-size: 4em;
display: table-cell;
contenteditable='true';
visibility: hidden;
"> Paste the chess.com game link here! </textarea>
</body>
<script>
function handlePaste (e) {
document.body.removeEventListener('paste', handlePaste);
var clipboardData, pastedData;
// Stop data actually being pasted into div
e.stopPropagation();
e.preventDefault();
// Get pasted data via clipboard API
clipboardData = e.clipboardData || window.clipboardData;
pastedData = clipboardData.getData('Text');
// Remove the textarea
document.getElementById('pasteHere').remove()
// Do whatever with pasteddata
main( first_numbers_in_string(pastedData) )
}
function first_numbers_in_string (s) {
let nums = s.match(/\d+/g)
return nums ? nums[0] : null
}
document.getElementById('pasteHere').addEventListener('paste', handlePaste)
document.body.addEventListener('paste', handlePaste)
var gameA, gameB, movesA, movesB
function main (game_id) {
// change the url:
var queryParams = new URLSearchParams(window.location.search);
queryParams.set("game_id", game_id);
history.replaceState(null, null, "?"+queryParams.toString());
proxyurl = "https://chess-com-proxy.bmacho.workers.dev/game/"
// browser can't call the chess.com callback url directly bc of CORS restrictions
// so I've set up a cloudflare worker proxy
game_id = parseInt(game_id)
var game_id_diff = game_id > 7000000000 ? 2 : 1 // before id 7000000000 corresponing games typically were 1 id away, and after that id they are typically 2 id away (there are some exceptions tho)
var getgameA = $.getJSON( proxyurl + game_id )
var BLid = game_id - game_id_diff
var BUid = game_id + game_id_diff
var getgameBl = $.getJSON( proxyurl + BLid )
var getgameBu = $.getJSON( proxyurl + BUid )
$.when( getgameA, getgameBl, getgameBu ).done( function( json_replyA, json_replyBl, json_replyBu ) {
if (json_replyA[0] && json_replyA[0].game && json_replyA[0].game.partnerGameId) {
// there is partner id provided
console.log( "partner game id is provided")
partnerid = parseInt( json_replyA[0].game.partnerGameId )
if ( partnerid == BLid ) {
setUpGameFromJSONReplies( json_replyA, json_replyBl )
} else if ( partnerid == BUid ) {
setUpGameFromJSONReplies( json_replyA, json_replyBu )
} else {
console.log( "partner game is not at +- 1/2")
var getgameBB = $.getJSON( proxyurl + partnerid )
$.when( getgameA, getgameBB ).done( setUpGameFromJSONReplies )
}
} else {
console.log( "partner game id is not provided, try the nearby games if those are the partners")
var BU = json_replyBu[0]
var BL = json_replyBl[0]
var Bu_isbh = (BU.game == undefined) ? false : BU.game.type == "bughouse"
var Bl_isbh = (BL.game == undefined) ? false : BL.game.type == "bughouse"
if ( Bu_isbh && !Bl_isbh ) {
console.log( " lower game not bughouse, but upper game is bughouse, choose that " )
json_replyB = json_replyBu
} else if ( !Bu_isbh && Bl_isbh ) {
console.log( " upper game not bughouse, but lower game is bughouse, choose that " )
json_replyB = json_replyBl
} else if ( Bu_isbh && Bl_isbh ) {
// both of the nearby games are bughouse
// TODO: inform the user
// provide a sane default
// provide a way to choose the other game
// currently we choose the game with the lower id
// so if that's not the corresponding game, increment the id 2 or 1
json_replyB = json_replyBl
console.log( "both of the nearby games are bughouse, we chose the one with the lower id. If thats not the corresponding game, try to open ", BUid )
} else {
console.log( "couldn't find the game for the B board" )
}
setUpGameFromJSONReplies ( json_replyA, json_replyB )
}
})
}
function setUpGameFromJSONReplies ( json_replyA, json_replyB ) {
gameA = json_replyA[0]
gameB = json_replyB[0]
movesA = parseMoveList(gameA.game.moveList)
movesB = parseMoveList(gameB.game.moveList)
var bpgn1
bpgn1 = bpgn(gameA, gameB, movesA, movesB)
console.log(bpgn1)
v1.reloadgame(bpgn1, "", "")
assforward(999,'c','v1',0)
}
if ( searchParams.has('game_id') ) {
main( first_numbers_in_string( searchParams.get("game_id") ) )
} else {
document.getElementById("pasteHere").style.visibility = "visible"
document.getElementById('pasteHere').addEventListener('paste', handlePaste)
document.body.addEventListener('paste', handlePaste)
}
</script>
<script>
// up/down arrow to start/end
document.addEventListener('keydown', function( e ){
if (e.keyCode == "38") { //up
saveNote('v1')
assundomove(999,'c','v1')
}
if (e.keyCode == "40") { //down
saveNote('v1')
assforward(999,'c','v1',0)
}
});
// left/right arrows go back and forth
var inter = -1;
var HOP = 60; //miliseconds
window.onkeydown = function( e ){
if(inter == -1){
inter = setInterval(function(){
if (e.keyCode == "39") {
saveNote('v1')
assforward(1,'c','v1',0)
}
if (e.keyCode == "37") {
saveNote('v1')
assundomove(1,'c','v1')
}
}, HOP)
}
}
window.onkeyup = function(){
clearInterval(inter)
inter = -1
}
// disable right click
document.addEventListener('contextmenu', event => event.preventDefault());
</script>
<style>
#forkongithub a{background:#083;color:#fff;text-decoration:none;font-family:arial,sans-serif;text-align:center;font-weight:bold;padding:5px 40px;font-size:1rem;line-height:2rem;position:relative;transition:0.5s;}
#forkongithub a:hover{background:#c11;color:#fff;}
#forkongithub a::before,#forkongithub a::after{content:"";width:100%;display:block;position:absolute;top:1px;left:0;height:1px;background:#fff;}
#forkongithub a::after{bottom:1px;top:auto;}
#forkongithub {width:1010px; padding: 0; margin:0; border:none; text-align:center;}
</style>
<div id='forkongithub'>
<a href="https://github.com/bmacho/bughouse-viewer">Fork me on GitHub</a>
</div>
<div style="text-align: left;">
<div style="display: inline-block; text-align: left;">
<br>
Feel free to open an <a href="https://github.com/bmacho/bughouse-viewer/issues">[issue]</a> for any bug, annoyance, suggestion! <br>
Try out our bookmarklet: <a href='
javascript: void function() {
if (location.pathname.slice(0, 11) == "/game/live/") {
game_id = location.pathname.slice(11) ;
board_obj = document.getElementById("board-single") ;
flip = board_obj ? board_obj.classList.contains("flipped") ? "&flip=true" : "" : "" ;
window.open("https://bmacho.github.io/bughouse-viewer/view.html?game_id=" + game_id + flip, "_blank") ;
} ;
}()
'>[BHV]</a> and <a href="https://github.com/bmacho/bughouse-viewer/raw/main/BHV-links.user.js">[userscript]</a>!
<h2>What's new:</h2>
<ul>
<li>2023.11.08: new bug introduced by previous bugfix : unable to drop different pieces at the same square now fixed (thx SKA_cz for informing me)</li>
<li>2023.07.22: same variations should only appear once</li>
<li>2023.07.09: chess.com provides the id of partner games again, every game should work again. If something is not working please file an issue on github (on a side note, I've kept the code that downloads both of the nearby games, so it should be one roundtrip faster, on the cost of making 1.5x times the requests to chess.com)</li>
<li>2023.06.02: coordinates added (code from onlinea.eu, originally by Wir)</li>
<li>userscript fixed, improved, should redirect bughouse links from archive pages and member pages, also auto updates again ( code now updates and runs on other peoples computer, so much hassle to change it ) </li>
<li>bookmarklet fixed</li>
<li>the bookmarklet and the userscript now recognise flips</li>
<li><s>the userscript does NOT auto update from now, so please update it manually</s></li>
<li>code grabbed from onlinea.eu (SKA_cz and Wir) :
<ul>
<li>drag and drop moves</li>
<li>last moves are now highlighted</li>
<li>some random bugs are now eliminated</li>
</ul>
</li>
<li>you can edit and save notes</li>
<li>variations more intuitive</li>
</ul>
</div>
</div>