-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
186 lines (177 loc) · 5.36 KB
/
app.js
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
/* eslint-env browser */
/* global FCEUX */
void (function app() {
var url = new URL(location.href)
var token = url.searchParams.get('token')
var gist = url.searchParams.get('gist')
function cors(url) {
return 'https://cors.gerhut.workers.dev/?' + encodeURIComponent(url)
}
if (!token) {
token = prompt('GitHub Personal Access Token:')
if (token) {
url.searchParams.set('token', token)
history.replaceState(null, document.title, url.toString())
}
}
if (token) {
if (gist) {
fetch(cors('https://api.github.com/gists/' + encodeURIComponent(gist)), {
headers: {
Authorization: 'token ' + token
}
})
.then(function (response) {
if (!response.ok) {
throw Error('HTTP ' + response.status)
}
return response.json()
})
.then(function (data) {
var files = data.files
var saveFiles = {}
for (var filename in files) {
if ({}.hasOwnProperty.call(files, filename)) {
saveFiles[filename] = Uint8Array.from(JSON.parse(files[filename].content))
}
}
start(saveFiles)
})
.catch(function (error) {
console.error(error)
document.querySelector('[data-key="l"]').classList.add('is-error')
})
} else {
alert('No gist id provided.\nWill create a new gist.')
start()
}
} else {
alert('No token Provided, will not enable online save.')
start()
}
function save(saveFiles) {
if (!token) return
document.querySelector('[data-key="s"]').classList.remove('is-danger')
document.querySelector('[data-key="s"]').classList.add('is-loading')
var apiUrl = 'https://api.github.com/gists'
if (gist) {
apiUrl += '/' + encodeURIComponent(gist)
}
var files = {}
for (var filename in saveFiles) {
if ({}.hasOwnProperty.call(saveFiles, filename)) {
files[filename] = { content: JSON.stringify([].slice.call(saveFiles[filename])) }
}
}
fetch(cors(apiUrl), {
method: gist ? 'PATCH' : 'POST',
headers: {
Authorization: 'token ' + token
},
body: JSON.stringify({
description: 'Save files of 三国志II 覇王の大陸',
files: files
})
})
.then(function (response) {
if (!response.ok) {
throw Error('HTTP ' + response.status)
}
return response.json()
})
.then(function (data) {
console.log('Saved to GitHub Gist')
document.querySelector('[data-key="s"]').classList.remove('is-loading')
if (!gist) {
gist = data.id
url.searchParams.set('gist', gist)
history.replaceState(null, document.title, url.toString())
prompt('Your save state url is', url)
}
})
.catch(function (error) {
console.error(error)
document.querySelector('[data-key="s"]').classList.remove('is-loading')
document.querySelector('[data-key="s"]').classList.add('is-danger')
})
}
function start(saveFiles) {
var KEY_NAME_BIT = {
z: 1,
x: 2,
Space: 4,
Enter: 8,
ArrowUp: 16,
ArrowDown: 32,
ArrowLeft: 64,
ArrowRight: 128
}
FCEUX().then(function (fceux) {
if (fceux.init('canvas') === false) {
throw Error('Initialise FCEUX failed.')
}
fceux.addEventListener('game-loaded', function () {
if (saveFiles) {
console.log('Loaded', saveFiles)
fceux.importSaveFiles(saveFiles)
fceux.loadState()
}
requestAnimationFrame(function frame() {
fceux.update()
requestAnimationFrame(frame)
})
var bits = 0
function press(keyName, down) {
var bit = KEY_NAME_BIT[keyName]
if (bit !== undefined) {
console.log(bit, down)
if (down) {
bits |= bit
} else {
bits &= ~bit
}
fceux.setControllerBits(bits)
return true
}
if (keyName === 's' && down === false) {
fceux.saveState()
save(fceux.exportSaveFiles())
return true
}
if (keyName === 'l' && down === false) {
fceux.loadState()
return true
}
return false
}
document.addEventListener(
'keydown',
function (event) {
if (press(event.key, true)) event.preventDefault()
},
true
)
document.addEventListener(
'keyup',
function (event) {
if (press(event.key, false)) event.preventDefault()
},
true
)
;[].forEach.call(document.querySelectorAll('[data-key]'), function (button) {
function handleDown(event) {
if (press(event.currentTarget.dataset.key, true)) event.preventDefault()
}
function handleUp(event) {
if (press(event.currentTarget.dataset.key, false)) event.preventDefault()
}
button.addEventListener('mousedown', handleDown)
button.addEventListener('touchstart', handleDown)
button.addEventListener('mouseup', handleUp)
button.addEventListener('touchend', handleUp)
})
})
fceux.downloadGame('rom.nes')
})
}
})()