forked from jason53415/blockly-app
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.html
251 lines (250 loc) · 10.5 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>PAIA Desktop</title>
<link rel="stylesheet" href="node_modules/bootstrap-icons/font/bootstrap-icons.css">
<link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="css/style.css">
<script src="node_modules/jquery/dist/jquery.min.js"></script>
<script src="node_modules/bootstrap/dist/js/bootstrap.bundle.js"></script>
</head>
<body>
<!-- Login dialog -->
<div class="modal fade" id="login-dialog" tabindex="-1" data-backdrop="static" data-keyboard="false">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content p-3">
<h5 class="text-center mt-3">登入 PAIA</h5>
<form name="play" action="javascript:login()">
<div class="modal-body my-2">
<div class="container">
<div class="form-group">
<label for="email">Email</label>
<input type="email" class="form-control" id="email" aria-describedby="emailHelp" placeholder="account@gmail.com" required>
</div>
<div class="form-group mb-4">
<label for="password">密碼</label>
<input type="password" class="form-control" id="password" placeholder="Password" required>
</div>
<button type="submit" class="btn btn-primary">登入</button>
<span class="text-danger ml-3" id="state-content"></span>
</div>
</div>
</form>
<button type="button" onclick="location.href=window.paia.redirect();">
從 PAIA 網站登入
</button>
</div>
</div>
</div>
<div class="card m-3 mx-auto" id="user-info" style="display: none;">
<div class="card-body">
<h3 class="card-title float-left m-0"><img class="d-inline mr-2" id="user-avatar" style="max-width: 50px; max-width: 50px;"></img><span id="user-name"></span></h3>
<button class="btn btn-primary float-right mt-1" onclick="logout();">登出</button>
</div>
</div>
<!-- Game list -->
<div id="game_list"></div>
<div class="card m-3 mx-auto">
<div class="card-body">
<h3 class="card-title">加入新遊戲</h3>
<button class="btn btn-primary" onclick="window.path.open(window.path.join(window.path.dirname(), 'games').replace('app.asar', 'app.asar.unpacked'));">開啟遊戲位置</button>
<!-- <form name="add-game" action="javascript:download_game()">
<div class="form-group py-1">
<div class="input-group input-group-sm">
<div class="input-group-prepend">
<button type="submit" class="btn btn-primary">下載</button>
</div>
<input type="text" class="form-control" id="game_url" placeholder="遊戲 Github 網址" required>
</div>
</div>
</form> -->
</div>
</div>
</body>
</html>
<script>
async function token_login(token=null) {
const auth = await window.paia.auth(token);
if (!auth.ok) {
$(`#user-info`).hide();
$('#user-name').html('');
$('#state-content').html(auth.content);
$('#login-dialog').modal('show');
return;
} else {
$(`#user-info`).show();
window.paia.user().then((res) => {
if (res.ok) {
$('#user-name').html(res.content.username);
$('#user-avatar').attr("src", res.content.avatar);
}
})
$('#login-dialog').modal('hide');
}
};
async function login() {
const response = await window.paia.login($('#email').val(), $('#password').val());
if (response.ok) {
$('#state-content').html('登入成功');
$('#login-dialog').modal('hide');
$(`#user-info`).show();
window.paia.user().then((res) => {
if (res.ok) {
$('#user-name').html(res.content.username);
$('#user-avatar').attr("src", res.content.avatar);
}
});
window.paia.ga('login', {
event_category: 'general_desktop',
value: 'login'
});
} else {
$('#state-content').html(response.content);
}
};
function logout() {
window.paia.logout();
$(`#user-info`).hide();
$('#state-content').html('');
$('#login-dialog').modal('show');
}
// function google_login() {
// $('#state-content').html('請於瀏覽器登入,成功後會自動返回');
// try {
// myApiOauth.openAuthWindowAndGetTokens()
// .then(token => {
// window.setToken(token);
// var oauth2 = window.getOauth2();
// oauth2.userinfo.get(
// function(err, res) {
// if (err) {
// $('#state-content').html('登入失敗,請確認網路連線後重新登入');
// console.log(err);
// } else {
// $('#state-content').html('登入成功');
// $('#login-dialog').modal('hide');
// }
// });
// });
// } catch(e) {
// console.log(e);
// }
// };
async function update_game_list() {
$('#game_list').empty();
const gameListDir = window.path.join(window.path.dirname(), 'games').replace('app.asar', 'app.asar.unpacked');
window.fs.readdirSync(gameListDir).forEach(dirent => {
const gameDir = window.path.join(gameListDir, dirent);
const configPath = window.path.join(gameDir, 'game_config.json');
if (window.fs.existsSync(configPath)) {
const config = JSON.parse(window.file.read(configPath));
const logoPath = (config.logo !== undefined)? window.path.join(gameDir, config.logo[0]) : "media/paia-logo.png";
const gameName = (config.game_name !== undefined)? config.game_name : dirent;
const description = (config.description !== undefined)? config.description : "";
const url = (config.url !== undefined)? config.url : "None";
const version = (config.version !== undefined)? config.version : "unknown version";
$('#game_list').append(
`<div class="card m-3 mx-auto">
<div class="row g-0">
<div class="col-md-4">
<img src="${logoPath}" alt="...">
</div>
<div class="col-md-8">
<div class="card-body">
<h6 class="card-subtitle mb-2 text-muted float-right"><span class="verison" game="${dirent}">${config.version}</span></h6>
<h3 class="card-title">${gameName}</h3>
<p class="card-text" style="min-height: 100px">${description}</p>
<a href="code.html?game=${dirent}&ver=${config.version}" class="btn btn-primary">積木</a>
<a href="python.html?game=${dirent}&ver=${config.version}" class="btn btn-primary">Python</a>
<a onclick="git_download('${dirent}', this)" class="btn btn-primary float-right" id="${dirent}-download""><i class="bi bi-download"></i> 下載 / 更新</a>
<select class="float-right mr-2 mt-1" id="${dirent}-tags"></select>
</div>
</div>
</div>
</div>`);
if (url.indexOf("github.com") != -1) {
const data = url.substring(url.indexOf("github.com") + 11).split('/');
const owner = data[0];
const repo = data[1];
window.api.github("GET", `repos/${owner}/${repo}/tags`, null).then((res) => {
if (res.ok) {
res.content.forEach((tag) => {
$(`#${dirent}-tags`).append(`<option value="${tag.name}">${tag.name}</option>`);
});
}
});
} else {
$(`#${dirent}-tags`).css("display", "none");
$(`#${dirent}-download`).css("display", "none");
}
}
});
};
function update_version_name() {
Array.from(document.getElementsByClassName('verison')).forEach(e => {
const config_path = window.path.join(window.path.dirname(), 'games', e.getAttribute('game'), 'game_config.json');
if (window.fs.existsSync(config_path)) {
const config = JSON.parse(window.file.read(config_path));
e.innerHTML = config['game_name'] + '-' + config['version'];
if (e.classList.contains('text-danger')) {
e.classList.remove("text-danger");
}
e.classList.add("text-muted");
} else {
e.innerHTML = 'Not Found';
if (e.classList.contains('text-muted')) {
e.classList.remove("text-muted");
}
e.classList.add("text-danger");
document.querySelectorAll('a.'+ e.getAttribute('game')).forEach(btn => {
btn.classList.add("disabled");
});
}
});
};
function git_download(game, button) {
const gameDir = window.path.join(window.path.dirname(), 'games', game).replace('app.asar', 'app.asar.unpacked');;
const config = JSON.parse(window.file.read(window.path.join(gameDir, 'game_config.json')));
const url = (config.url !== undefined)? config.url : "None";
const version = $(`#${game}-tags`).val();
const data = url.substring(url.indexOf("github.com") + 11).split('/');
const owner = data[0];
const repo = data[1];
if (window.popup.confirm('下載後會覆蓋此遊戲資料夾下的所有檔案,確定執行?')) {
button.innerHTML = '<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> 下載 / 更新';
document.querySelectorAll('a.btn').forEach(e => {
e.classList.add("disabled");
});
window.repo.download(`direct:https://github.com/${owner}/${repo}/archive/refs/tags/${version}.zip`, gameDir, function (err) {
button.innerHTML = '<i class="bi bi-download"></i> 下載 / 更新';
document.querySelectorAll('a.btn').forEach(e => {
e.classList.remove("disabled");
});
if (err) {
window.popup.alert(err);
} else {
update_version_name();
const config = JSON.parse(window.file.read(path.join(gameDir, 'game_config.json')));
window.popup.alert(config['game_name'] + '-' + config['version'] + ' 下載成功');
}
});
};
};
document.title += ` ${window.app.getVersion()}`;
window.deeplink.onLogin((event, token) => {
token_login(token);
window.paia.ga('login', {
event_category: 'deeplink_desktop',
value: 'login'
});
});
token_login();
update_game_list();
update_version_name();
window.paia.ga('screen_view', {
app_name: "paia_desktop",
app_version: window.app.getVersion(),
screen_name: 'index'
});
</script>