cleanup code

This commit is contained in:
pypy
2020-11-07 22:48:50 +09:00
parent 06fee0eda0
commit dabd9995ee
3 changed files with 139 additions and 83 deletions

View File

@@ -14,7 +14,7 @@ import locale from 'element-ui/lib/locale/lang/en';
import sharedRepository from './repository/shared.js'; import sharedRepository from './repository/shared.js';
import configRepository from './repository/config.js'; import configRepository from './repository/config.js';
import webApiService from './service/webapi.js'; import webApiService from './service/webapi.js';
import logWatcherService from './service/logwatcher.js' import gameLogService from './service/gamelog.js'
(async function () { (async function () {
await CefSharp.BindObjectAsync( await CefSharp.BindObjectAsync(
@@ -3296,7 +3296,7 @@ import logWatcherService from './service/logwatcher.js'
el: '#x-app', el: '#x-app',
async mounted() { async mounted() {
this.checkAppVersion(); this.checkAppVersion();
await logWatcherService.reset(); await gameLogService.reset();
API.$on('SHOW_WORLD_DIALOG', (tag) => this.showWorldDialog(tag)); API.$on('SHOW_WORLD_DIALOG', (tag) => this.showWorldDialog(tag));
API.$on('SHOW_LAUNCH_DIALOG', (tag) => this.showLaunchDialog(tag)); API.$on('SHOW_LAUNCH_DIALOG', (tag) => this.showLaunchDialog(tag));
this.updateLoop(); this.updateLoop();
@@ -4435,8 +4435,6 @@ import logWatcherService from './service/logwatcher.js'
// App: gameLog // App: gameLog
var gameLogContextMap = new Map();
$app.data.lastLocation = ''; $app.data.lastLocation = '';
$app.data.lastLocation$ = {}; $app.data.lastLocation$ = {};
$app.data.discordActive = configRepository.getBool('discordActive'); $app.data.discordActive = configRepository.getBool('discordActive');
@@ -4483,7 +4481,7 @@ import logWatcherService from './service/logwatcher.js'
}; };
$app.methods.resetGameLog = async function () { $app.methods.resetGameLog = async function () {
await logWatcherService.reset(); await gameLogService.reset();
this.gameLogTable.data = []; this.gameLogTable.data = [];
}; };
@@ -4506,94 +4504,61 @@ import logWatcherService from './service/logwatcher.js'
}; };
$app.methods.updateGameLog = async function () { $app.methods.updateGameLog = async function () {
var { var currentUserDisplayName = API.currentUser.displayName;
displayName: currentUserDisplayName,
username: currentUserName
} = API.currentUser;
for (var [fileName, dt, type, ...args] of await logWatcherService.get()) { for (var gameLog of await gameLogService.poll(API.currentUser.username)) {
var gameLogContext = gameLogContextMap.get(fileName); var tableData = null;
if (gameLogContext === undefined) {
gameLogContext = {
// auth
loginProvider: null,
loginUser: null,
// hmd switch (gameLog.type) {
hmdModel: null, case 'location':
tableData = {
// location created_at: gameLog.dt,
location: null, type: 'Location',
}; data: gameLog.location
gameLogContextMap.set(fileName, gameLogContext); };
}
var gameLogTableData = null;
switch (type) {
case 'auth':
gameLogContext.loginProvider = args[0];
gameLogContext.loginUser = args[1];
break;
case 'hmd-model':
gameLogContext.hmdModel = args[0];
break; break;
case 'location': case 'location':
var location = args[0]; tableData = {
gameLogContext.location = location; created_at: gameLog.dt,
if (gameLogContext.loginUser === currentUserName) {
this.lastLocation = location;
}
break;
case 'world':
// var worldName = params[0];
gameLogTableData = {
created_at: dt,
type: 'Location', type: 'Location',
data: gameLogContext.location data: gameLog.location
}; };
break; break;
case 'player-joined': case 'player-joined':
var userDisplayName = args[0]; if (currentUserDisplayName === gameLog.userDisplayName) {
if (currentUserDisplayName === userDisplayName) {
continue; continue;
} }
gameLogTableData = { tableData = {
created_at: dt, created_at: gameLog.dt,
type: 'OnPlayerJoined', type: 'OnPlayerJoined',
data: userDisplayName data: gameLog.userDisplayName
}; };
break; break;
case 'player-left': case 'player-left':
var userDisplayName = args[0]; if (currentUserDisplayName === gameLog.userDisplayName) {
if (currentUserDisplayName === userDisplayName) {
continue; continue;
} }
gameLogTableData = { tableData = {
created_at: dt, created_at: gameLog.dt,
type: 'OnPlayerLeft', type: 'OnPlayerLeft',
data: userDisplayName data: gameLog.userDisplayName
}; };
break; break;
case 'notification': case 'notification':
var json = args[0]; tableData = {
gameLogTableData = { created_at: gameLog.dt,
created_at: dt,
type: 'Notification', type: 'Notification',
data: json data: gameLog.json
}; };
break; break;
} }
if (gameLogTableData !== null && if (tableData !== null) {
gameLogContext.loginUser === currentUserName) { this.gameLogTable.data.push(tableData);
this.gameLogTable.data.push(gameLogTableData);
} }
} }
} }

110
html/src/service/gamelog.js Normal file
View File

@@ -0,0 +1,110 @@
// requires binding of LogWatcher
var contextMap = new Map(); // <string, object>
class GameLogService {
async poll(loginUser) {
var rawGameLogs = await LogWatcher.Get();
var gameLogs = [];
var now = Date.now();
for (var [fileName, dt, type, ...args] of rawGameLogs) {
var context = contextMap.get(fileName);
if (context === undefined) {
context = {
updatedAt: null,
// auth
loginProvider: null,
loginUser: null,
// hmd
hmdModel: null,
// location
location: null,
};
contextMap.set(fileName, context);
}
var gameLog = parseRawGameLog(dt, type, args);
switch (gameLog.type) {
case 'auth':
context.loginProvider = gameLog.loginProvider;
context.loginUser = gameLog.loginUser;
break;
case 'hmd-model':
context.hmdModel = gameLog.hmdModel;
break;
case 'location':
context.location = gameLog.location;
break;
}
context.updatedAt = now;
if (loginUser !== null &&
loginUser === context.loginUser) {
gameLogs.push(gameLog);
}
}
return gameLogs;
}
async reset() {
await LogWatcher.Reset();
contextMap.clear();
}
}
function parseRawGameLog(dt, type, args) {
var gameLog = {
dt,
type
};
switch (type) {
case 'auth':
gameLog.loginProvider = args[0];
gameLog.loginUser = args[1];
break;
case 'hmd-model':
gameLog.hmdModel = args[0];
break;
case 'location':
gameLog.location = args[0];
break;
case 'world':
gameLog.worldName = args[0];
break;
case 'player-joined':
gameLog.userDisplayName = args[0];
break;
case 'player-left':
gameLog.userDisplayName = args[0];
break;
case 'notification':
gameLog.json = args[0];
break;
}
return gameLog;
}
var self = new GameLogService();
window.gameLogService = self;
export {
self as default,
GameLogService as LogWatcherService
};

View File

@@ -1,19 +0,0 @@
// requires binding of LogWatcher
class LogWatcherService {
get() {
return LogWatcher.Get();
}
reset() {
return LogWatcher.Reset();
}
}
var self = new LogWatcherService();
window.logWatcherService = self;
export {
self as default,
LogWatcherService
};