bibliofile / blockheads-api Goto Github PK
View Code? Open in Web Editor NEWAn API for interacting with Blockheads worlds - both on the cloud and mac servers!
License: GNU General Public License v3.0
An API for interacting with Blockheads worlds - both on the cloud and mac servers!
License: GNU General Public License v3.0
It would be nice if the mac API users didn't need to call unwatchChat
to make the API stop keeping the process alive.
https://nodejs.org/api/child_process.html#child_process_subprocess_unref
I am using the following script to get the LogParser:
/**
* This class should not be used by consumers of the library. It is used internally.
* @private
*/
class LogParser {
constructor(name) {
this.validLineStart = /^[A-Z][a-z]{2} ( |\d)\d \d\d:\d\d:\d\d ([\w\-]+) BlockheadsServer/
this.name = name
}
parse(log) {
const result = []
// Note: Yes, this is a big complicated, and could be more nicely expressed by splitting it up.
// however, since logs could potentially be very large, to be safe it's better to have
// an O(N) solution here than to have an easy to read solution.
const now = new Date()
const currentYear = new Date().getFullYear()
let temp
for (const line of log.split('\n')) {
if (temp) {
if (line.startsWith('\t')) {
//Remember to remove the leading tab
temp.raw += '\n' + line.substr(1)
temp.message += '\n' + line.substr(1)
// We know this won't pass the valid line test, so skip it
continue
}
// Not continued, add to the results and reset
// We might still have a valid new message though.
this.addIfValid(result, temp)
temp = undefined
}
if (this.validLineStart.test(line)) {
// When a new year occurs, some logs may be from last year.
// To handle this, assume all logs are from this year, and remove one year
// from the date if the log timestamp is in the future.
const time = new Date(line.substr(0, 15).replace(' ', ` ${currentYear}`))
if (now < time) time.setFullYear(currentYear - 1)
temp = {
raw: line,
timestamp: time,
message: line.substr(line.indexOf(']') + 3)
}
}
}
if (temp) this.addIfValid(result, temp)
return result
}
addIfValid(result, message) {
const keepName = [` - Player Connected`, ` - Player Disconnected`, ` - Client disconnected`]
const msg = message.message
if (msg.startsWith(this.name)) {
if (!keepName.some(s => msg.startsWith(`${this.name}${s}`))) {
message.message = msg.replace(this.name, '')
}
result.push(message)
}
}
}
exports.LogParser = LogParser;
This is the code I am using:
const { LogParser } = require('./bhlogparse');
const parser = new LogParser("DXL44'S FFA");
const logs = parser.parse("Apr 25 11:00:33 Wingys-MacPro BlockheadsServer[341]: DXL44'S FFA - WINGYSAM: /list-adminlist");
console.log(logs)
Expected result:
Something similar to:
[
{
raw: 'Apr 25 11:00:33 Wingys-MacPro BlockheadsServer[341]: DXL44\'S FFA - WINGYSAM: /list-adminlist',
timestamp: 2017-04-01T15:00:33.000Z,
author: 'WINGYSAM',
message: '/list-adminlist'
}
]
Actual result:
[ { raw: 'Apr 25 11:00:33 Wingys-MacPro BlockheadsServer[341]: DXL44\'S FFA - WINGYSAM: /list-adminlist',
timestamp: 2017-04-01T15:00:33.000Z,
message: ' - WINGYSAM: /list-adminlist' } ]
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.