GithubHelp home page GithubHelp logo

bibliofile / blockheads-api Goto Github PK

View Code? Open in Web Editor NEW
7.0 7.0 2.0 718 KB

An API for interacting with Blockheads worlds - both on the cloud and mac servers!

License: GNU General Public License v3.0

TypeScript 57.01% AppleScript 7.76% HTML 33.09% JavaScript 1.46% Shell 0.68%

blockheads-api's People

Contributors

bibliofile avatar dependabot[bot] avatar wingysam avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

blockheads-api's Issues

LogParser getting messages incorrectly

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' } ]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.