Skip to content

Commit

Permalink
v0.2.2:New Logging System #7 #31
Browse files Browse the repository at this point in the history
  • Loading branch information
dapucita committed Nov 30, 2020
1 parent 715f87a commit a77e72e
Showing 6 changed files with 97 additions and 26 deletions.
6 changes: 1 addition & 5 deletions bot.ts
Original file line number Diff line number Diff line change
@@ -20,11 +20,7 @@ window.logQueue = []; // init

const botRoomConfig: RoomConfig = JSON.parse(getCookieFromHeadless('botConfig'));

console.log("====");
console.log('\x1b[32m%s\x1b[0m', "H a x b o t r o n"); //green color
console.log("Haxbotron Debugging System on headless browser");
console.log(`The authentication token is conveyed via cookie(${botRoomConfig.token})`);
console.log("====");
console.log(`Haxbotron Bot Entry Point : The authentication token is conveyed via cookie(${botRoomConfig.token})`);

window.playerList = new Map(); // playerList:Player[] is an Map object. // playerList.get(player.id).name; : usage for playerList

41 changes: 39 additions & 2 deletions cli.ts
Original file line number Diff line number Diff line change
@@ -103,7 +103,7 @@ async function makeBot(hostConfig: any) {
isOpenHeadless = answerConfig.inputHeadlessModeSelect;
});

winstonLogger.info("The game host has started.");
winstonLogger.info("Haxbotron has started.");
//await nodeStorage.init();

/*
@@ -120,13 +120,47 @@ async function makeBot(hostConfig: any) {
clearInterval(storageLoop);
// browser.close();
isBotLaunched = false;
winstonLogger.info("The game host is closed.");
winstonLogger.info("Haxbotron is closed.");
return;
});

const loadedPages = await browser.pages(); // get all pages (acutally it will be only one page in the first loading of puppeteer)
const page = await loadedPages[0]; // target on empty, blank page

// logging system --
// https://stackoverflow.com/questions/47539043/how-to-get-all-console-messages-with-puppeteer-including-errors-csp-violations
await page.on('console', (msg: any) => {
switch(msg.type()) {
case "log": {
winstonLogger.info(msg.text());
break;
}
case "info": {
winstonLogger.info(msg.text());
break;
}
case "error": {
winstonLogger.error(msg.text());
break;
}
case "warning": {
winstonLogger.warn(msg.text());
break;
}
default: {
winstonLogger.info(msg.text());
break;
}
}
});
await page.on('pageerror', (msg: any) => {
winstonLogger.error(msg);
});
await page.on('requestfailed', (msg: any) => {
winstonLogger.error(`${msg.failure().errorText} ${msg.url()}`);
});
// -- logging system

await page.goto('https://www.haxball.com/headless', {
waitUntil: 'networkidle2'
});
@@ -151,6 +185,8 @@ async function makeBot(hostConfig: any) {

// get stored data from puppeteer html5 localstorage and copy them into node-persist storage
var storageLoop = setInterval(async function () {

/* ================= DEPRECATED (OLD LOGGING SYSTEM)
// log system with winston module. winstonLoggerSystem
var msgQueue: LogMessage[] = await page.evaluate(() => {
var msgQueueCopy = window.logQueue;
@@ -180,6 +216,7 @@ async function makeBot(hostConfig: any) {
}
}
}
================= */

// data from bot
var localStorageData: any[] = await page.evaluate(() => {
22 changes: 10 additions & 12 deletions controller/Logger.ts
Original file line number Diff line number Diff line change
@@ -14,31 +14,29 @@ export class Logger {
return this.instance;
}

//DEPRECATED
/*
private push(messageObject: LogMessage): void {
window.logQueue.push(messageObject)
}
private pop(): LogMessage | undefined {
return window.logQueue.pop();
}
*/

public i(msg: string): void { // for common info log
this.push({type: 2, context: msg});
//this.push({type: 2, context: msg});
console.info(msg);
}

public e(msg: string): void { // for error log
this.push({type: 0, context: msg});
//this.push({type: 0, context: msg});
console.error(msg);
}

public w(msg: string): void { // for warning log
this.push({type: 1, context: msg});
//this.push({type: 1, context: msg});
console.warn(msg);
}

}

/*
USAGE EXAMPLE
let something: Logger = new Logger(); // It makes an error: constructor of 'Singleton' is private.
let instance: Logger = Logger.getInstance(); instace.blahbalh(); // now do something with the instance.
*/
}
50 changes: 45 additions & 5 deletions index.ts
Original file line number Diff line number Diff line change
@@ -30,6 +30,8 @@ const MenuItemSwitch = {

var superKeyList: string;

var botConsoleLineCount: number = 0;

function createWindow() {
// create the Electron browser window
electronWindow = new BrowserWindow({
@@ -98,7 +100,7 @@ ipcMain.on('room-make-action', (event: any, arg: any) => { // webRender.js
isBotLaunched = true;
} else {
dialog.showErrorBox("You can launch only one bot.", "The bot was launched already. You can't launch other bot on this process.");
console.log("The bot was launched already");
winstonLogger.error("The bot was launched already");
}
});
// send chat message toward the game room
@@ -192,7 +194,7 @@ async function nodeStorageInit() {
}

async function bot(hostConfig: string) {
console.log('\x1b[32m%s\x1b[0m', "The game host has started.");
winstonLogger.info("Haxbotron has started.");

//await nodeStorage.init();

@@ -218,13 +220,49 @@ async function bot(hostConfig: string) {
document.getElementById('roomLinkIndicator').innerHTML = "link";
document.getElementById('botConsole').value = "";
`);
console.log('\x1b[31m%s\x1b[0m', "The game host is closed.");
winstonLogger.info("Haxbotron is closed.");
return;
});

const loadedPages = await browser.pages(); // get all pages (acutally it will be only one page in the first loading of puppeteer)
const page = await loadedPages[0]; // target on empty, blank page

// logging system --
// https://stackoverflow.com/questions/47539043/how-to-get-all-console-messages-with-puppeteer-including-errors-csp-violations
await page.on('console', (msg: any) => {
switch(msg.type()) {
case "log": {
winstonLogger.info(msg.text());
break;
}
case "info": {
winstonLogger.info(msg.text());
break;
}
case "error": {
winstonLogger.error(msg.text());
break;
}
case "warning": {
winstonLogger.warn(msg.text());
break;
}
default: {
winstonLogger.info(msg.text());
break;
}
}
botConsoleLineCount++;
electronWindow.webContents.executeJavaScript("document.getElementById('botConsole').value = '[" + botConsoleLineCount+ "] " + msg.text() + '\\r\\n' + "' + document.getElementById('botConsole').value;");
});
await page.on('pageerror', (msg: any) => {
winstonLogger.error(msg);
});
await page.on('requestfailed', (msg: any) => {
winstonLogger.error(`${msg.failure().errorText} ${msg.url()}`);
});
// -- logging system

await page.goto('https://www.haxball.com/headless', {
waitUntil: 'networkidle2'
});
@@ -262,6 +300,7 @@ async function bot(hostConfig: string) {

// get stored data from puppeteer html5 localstorage and copy them into node-persist storage
var storageLoop = setInterval(async function () {
/* DEPRECATED (OLD LOGGING SYSTEM)
// log system with winston module. winstonLoggerSystem
// log message queue copy
var msgQueue: LogMessage[] = await page.evaluate(() => {
@@ -291,13 +330,14 @@ async function bot(hostConfig: string) {
break;
}
}
}
}
// now print it on electron's textarea
if(await msgChunk != '') {
await electronWindow.webContents.executeJavaScript("document.getElementById('botConsole').value = '" + msgChunk + "' + document.getElementById('botConsole').value;");
}
// FIXME: this logging system has a problem. "Uncaught SyntaxError: Unexpected identifier at WebFrame". Maybe it was caused by quotation marks..
// bug: this logging system has a problem. "Uncaught SyntaxError: Unexpected identifier at WebFrame". Maybe it was caused by quotation marks..
*/

// data from bot
var localStorageData: any[] = await page.evaluate(() => {
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "haxbotron",
"version": "0.2.1",
"version": "0.2.2",
"description": "Haxbotron is a host bot application for Haxball game.",
"main": "index.js",
"scripts": {

0 comments on commit a77e72e

Please sign in to comment.