已关闭。此问题需要details or clarity。当前不接受答案。
**想要改进此问题?**添加详细信息并通过editing this post阐明问题。
8天前关闭。
Improve this question
我现在有一个脚本,它使用LocalStorage
作为我的Haxball房间:https://www.haxball.com/play
class HaxStatistics {
constructor(playerName = '') {
this.playerName = playerName;
this.games = 0;
this.wins = 0;
this.winrate = '0.00%';
this.playtime = 0;
this.goals = 0;
this.assists = 0;
this.CS = 0;
this.ownGoals = 0;
}
}
function globalStatsCommand(player, message) {
var stats = new HaxStatistics(player.name);
if (localStorage.getItem(authArray[player.id][0])) {
stats = JSON.parse(localStorage.getItem(authArray[player.id][0]));
}
var statsString = printPlayerStats(stats);
room.sendAnnouncement(
statsString,
player.id,
infoColor,
'bold',
HaxNotification.CHAT
);
}
function statsLeaderboardCommand(player, message) {
var key = message.split(/ +/)[0].substring(1).toLowerCase();
printRankings(key, player.id);
}
function updatePlayerStats(player, teamStats) {
var stats = new HaxStatistics(player.name);
var pComp = getPlayerComp(player);
if (localStorage.getItem(authArray[player.id][0])) {
stats = JSON.parse(localStorage.getItem(authArray[player.id][0]));
}
stats.games++;
if (lastWinner == teamStats) stats.wins++;
stats.winrate = ((100 * stats.wins) / (stats.games || 1)).toFixed(1) + `%`;
stats.goals += getGoalsPlayer(pComp);
stats.assists += getAssistsPlayer(pComp);
stats.ownGoals += getOwnGoalsPlayer(pComp);
stats.CS += getCSPlayer(pComp);
stats.playtime += getGametimePlayer(pComp);
localStorage.setItem(authArray[player.id][0], JSON.stringify(stats));
}
function updateStats() {
if (
players.length >= 2 * teamSize &&
(
game.scores.time >= (5 / 6) * game.scores.timeLimit ||
game.scores.red == game.scores.scoreLimit ||
game.scores.blue == game.scores.scoreLimit
) &&
teamRedStats.length >= teamSize && teamBlueStats.length >= teamSize
) {
for (let player of teamRedStats) {
updatePlayerStats(player, Team.RED);
}
for (let player of teamBlueStats) {
updatePlayerStats(player, Team.BLUE);
}
}
}
function printRankings(statKey, id = 0) {
var leaderboard = [];
statKey = statKey == "cs" ? "CS" : statKey;
for (var i = 0; i < localStorage.length; i++) {
var key = localStorage.key(i);
if (key.length == 43)
leaderboard.push([
JSON.parse(localStorage.getItem(key)).playerName,
JSON.parse(localStorage.getItem(key))[statKey],
]);
}
if (leaderboard.length < 5) {
if (id != 0) {
room.sendAnnouncement(
'Non ci sono ancora abbastanza partite giocate!',
id,
errorColor,
'bold',
HaxNotification.CHAT
);
}
return;
}
leaderboard.sort(function (a, b) { return b[1] - a[1]; });
var rankingString = `${statKey.charAt(0).toUpperCase() + statKey.slice(1)}> `;
for (let i = 0; i < 5; i++) {
let playerName = leaderboard[i][0];
let playerStat = leaderboard[i][1];
if (statKey == 'playtime') playerStat = getTimeStats(playerStat);
rankingString += `#${i + 1} ${playerName} : ${playerStat}, `;
}
rankingString = rankingString.substring(0, rankingString.length - 2);
room.sendAnnouncement(
rankingString,
id,
infoColor,
'bold',
HaxNotification.CHAT
);
}
由于LDB files
不可读,它的最大空间为5mb,由于经济原因,我目前无法使用数据库,我想读取位于我的AWS VPS中的本地JSON file
。我通过找到这个documentation找到了FileReader
的可能解决方案,并尝试了这个解决方案:
let reader = new FileReader();
reader.readAsText("/home/haxroomie/stats.json");
console.log(reader.result)
不幸的是,它给了我一个错误,我不知道如何移动了...建议?
小澄清:我要打开的文件在我的AWS VPS
上
1条答案
按热度按时间jaql4c8m1#
您正在尝试从路径读取文件,
FileReader
应在readAsText
函数中接收Blob
。你应该在你的HTML文件中有一个
File input
元素,你应该用这个输入选择想要的文件,在文件选择之后,Input的事件应该保存所选文件的blob,你可以把它传递给上面提到的函数。来自documentaions:
FileReader.readAsText()readAsText()方法用于读取指定的Blob或File的内容。当读取操作完成时,readyState更改为DONE,并触发loadend事件,result属性包含文件的内容作为文本字符串。
来源:https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsText