<commit_before>const flatbuffers = require('./flatbuffers').flatbuffers;
const sisyfox = require('./sisycol_generated').sisyfox;

class Sisyfox {
    constructor(wsHost, wsPort=80) {
        this.callback = [];
        this.initCallbacks = [];
        this.defaultCallback = [];
        this.socketOpen = false;
        this.reconnections = 0;

        this.callback[sisyfox.sisycol.Payload.Error] = [function printError(response) {
            let error = response.payload(new sisyfox.sisycol.response.Error());
            console.log("SisyFox has trouble - Code #" + error.errorCode().toString());
        }];

        if (wsPort !== 80) {
            this.wsAddr = "ws://" + wsHost + ":" + wsPort.toString();
        } else {
            this.wsAddr = "ws://" + wsHost + "/wsocket";
        }

        this.createSocket();
        this.addDefaultCallbacks();
    }

    init() {
        const length = this.initCallbacks.length;
        if (this.initCallbacks.lengths === 0) {
            console.log("No init callback registered");
        } else {
            for (let index = 0; index < length; index++) {
                this.initCallbacks[index]();
            }
        }
    }

    createSocket() {
        console.log("Create Sisycol Socket...");
        const self = this;

        this.socket = new WebSocket(this.wsAddr);

        this.socket.binaryType = "arraybuffer";
        this.socket.onopen = function () {
            self.webSocketOpened();
        };
        this.socket.onclose = function () {
            self.webSocketClosed();
        };
        this.socket.onmessage = function (event) {
            self.webSocketReceived(event);
        };
    }

    reconnect() {
        const self = this;
        setTimeout(function () {self.reconnections--;}, 20000);
        this.reconnections++;
        this.createSocket();
    }

    webSocketClosed() {
        console.log("Connection to SisyFox closed.");
        if (this.socketOpen) {
            if (this.reconnections < 3) {
                this.reconnect();
            }
            this.socketOpen = false;
        }
    }

    webSocketOpened() {
        console.log("Connection to SisyFox established.");
        if (!this.reconnections) {
            this.init();
        }
        this.socketOpen = true;
    }

    webSocketReceived(event) {
        console.log("Received " + event.data.byteLength.toString() + " Bytes");
        const payload = new flatbuffers.ByteBuffer(new Uint8Array(event.data));
        this.handleResponse(sisyfox.sisycol.Root.getRootAsRoot(payload));
    }

    handleResponse(response) {
        if (this.callback[response.payloadType()] !== undefined) {
            for (let callback of this.callback[response.payloadType()]) {
                callback(response);
            }
        } else if (this.defaultCallback[response.payloadType()] === undefined) {
            console.log("No callback defined to handle this kind of response! (" + response.payloadType().toString() + ")");
        }
        if (this.defaultCallback[response.payloadType()] !== undefined) {
            this.defaultCallback[response.payloadType()](response);
        }
    }

    static getVersion(builder, high = 2, low = 0) {
        return sisyfox.sisycol.Version.createVersion(builder, high, low);
    }

    send(request) {
        const payload = new Uint8Array(request.length + 4);
        payload.set(request, 4);
        const dv = new DataView(payload.buffer, 0, 4);
        dv.setUint32(0, request.length, true);
        console.log("Transmit " + request.length.toString() + " Bytes");
        this.socket.send(payload);
    }

    prepareSend(payloadType, payload, builder) {
        sisyfox.sisycol.Root.startRoot(builder);
        sisyfox.sisycol.Root.addVersion(builder, Sisyfox.getVersion(builder));
        sisyfox.sisycol.Root.addMessageId(builder, 0);
        sisyfox.sisycol.Root.addPayloadType(builder, payloadType);
        sisyfox.sisycol.Root.addPayload(builder, payload);
        const request = sisyfox.sisycol.Root.endRoot(builder);
        builder.finish(request);
        console.log("Transmit " + builder.asUint8Array().length.toString() + " Bytes");
        this.socket.send(builder.asUint8Array());
    }

    setCallback(payloadType, func) {
        if (this.callback[payloadType] === undefined) {
            this.callback[payloadType] = [];
        }
        this.callback[payloadType].push(func);
    }

    callDefaultCallback(payloadType, response) {
        if (this.defaultCallback[payloadType] !== undefined) {
            this.defaultCallback[payloadType](response);
        }
    }

    addInitCallback(func) {
        this.initCallbacks.push(func);
    }

    addDefaultCallbacks() {
        this.defaultCallback[sisyfox.sisycol.Payload.AddUser] = function () {
            console.log("new user was created.");
        };
        this.defaultCallback[sisyfox.sisycol.Payload.AddScore] = function () {
            console.log("new score was added");
        };
        this.defaultCallback[sisyfox.sisycol.Payload.SetUser] = function () {
            console.log("changed user");
        };
        this.defaultCallback[sisyfox.sisycol.Payload.UnsetUser] = function () {
            console.log("user logged out");
        };
        this.defaultCallback[sisyfox.sisycol.Payload.SetSetting] = function (response) {
            const payload = response.payload(new sisyfox.sisycol.response.SetSetting());
            switch (payload.type()) {
                case sisyfox.sisycol.SettingType.SPAWN_POINT:
                    console.log("spawn point changed");
                    break;
                case sisyfox.sisycol.SettingType.PITCH_SENSITIVITY:
                    console.log("Info", "Steigungs-Empfindlichkeit wurde auf " +
                        Math.round(payload.value() / 2.55).toString() + "% geändert");
                    break;
                case sisyfox.sisycol.SettingType.DIRECTION_HELPER:
                    if (payload.value()) {
                        console.log("Info", "Richtungshilfe wurde aktiviert");
                    } else {

                        console.log("Info", "Richtungshilfe wurde deaktiviert");
                    }
                    break;
                case sisyfox.sisycol.SettingType.EVENT_PROBABILITY:
                    console.log("Info", "Event-Wahrscheinlichkeit wurde auf " +
                        Math.round(payload.value() / 2.55).toString() + "% geändert");
                    break;
                case sisyfox.sisycol.SettingType.MOUSE_SENSITIVITY:
                    console.log("Info", "Empfindlichkeit des Balls wurde auf " +
                        Math.round(payload.value() / 2.55).toString() + "% geändert");
                    break;
                case sisyfox.sisycol.SettingType.COMPETITION_MODE:
                    if (payload.value()) {
                        console.log("Info", "Highscore Modus wurde aktiviert");
                    } else {
                        console.log("Info", "Highscore Modus wurde deaktiviert");
                    }
                    break;
                case sisyfox.sisycol.SettingType.TREE_COLLIDER:
                    if (payload.value()) {
                        console.log("Info", "Baum-Collider wurde aktiviert");
                    } else {
                        console.log("Info", "Baum-Collider wurde deaktiviert");
                    }
                    break;
                case sisyfox.sisycol.SettingType.GAME_MODE:
                    console.log("Info", "Spielmodus wurde geändert");
                    break;
            }<commit_msg>fix bug in defaultCallback in sisyfox.js<commit_after>const flatbuffers = require('./flatbuffers').flatbuffers;
const sisyfox = require('./sisycol_generated').sisyfox;

class Sisyfox {
    constructor(wsHost, wsPort=80) {
        this.callback = [];
        this.initCallbacks = [];
        this.defaultCallback = [];
        this.socketOpen = false;
        this.reconnections = 0;

        this.callback[sisyfox.sisycol.Payload.Error] = [function printError(response) {
            let error = response.payload(new sisyfox.sisycol.response.Error());
            console.log("SisyFox has trouble - Code #" + error.errorCode().toString());
        }];

        if (wsPort !== 80) {
            this.wsAddr = "ws://" + wsHost + ":" + wsPort.toString();
        } else {
            this.wsAddr = "ws://" + wsHost + "/wsocket";
        }

        this.createSocket();
        this.addDefaultCallbacks();
    }

    init() {
        const length = this.initCallbacks.length;
        if (this.initCallbacks.lengths === 0) {
            console.log("No init callback registered");
        } else {
            for (let index = 0; index < length; index++) {
                this.initCallbacks[index]();
            }
        }
    }

    createSocket() {
        console.log("Create Sisycol Socket...");
        const self = this;

        this.socket = new WebSocket(this.wsAddr);

        this.socket.binaryType = "arraybuffer";
        this.socket.onopen = function () {
            self.webSocketOpened();
        };
        this.socket.onclose = function () {
            self.webSocketClosed();
        };
        this.socket.onmessage = function (event) {
            self.webSocketReceived(event);
        };
    }

    reconnect() {
        const self = this;
        setTimeout(function () {self.reconnections--;}, 20000);
        this.reconnections++;
        this.createSocket();
    }

    webSocketClosed() {
        console.log("Connection to SisyFox closed.");
        if (this.socketOpen) {
            if (this.reconnections < 3) {
                this.reconnect();
            }
            this.socketOpen = false;
        }
    }

    webSocketOpened() {
        console.log("Connection to SisyFox established.");
        if (!this.reconnections) {
            this.init();
        }
        this.socketOpen = true;
    }

    webSocketReceived(event) {
        console.log("Received " + event.data.byteLength.toString() + " Bytes");
        const payload = new flatbuffers.ByteBuffer(new Uint8Array(event.data));
        this.handleResponse(sisyfox.sisycol.Root.getRootAsRoot(payload));
    }

    handleResponse(response) {
        if (this.callback[response.payloadType()] !== undefined) {
            for (let callback of this.callback[response.payloadType()]) {
                callback(response);
            }
        } else if (this.defaultCallback[response.payloadType()] === undefined) {
            console.log("No callback defined to handle this kind of response! (" + response.payloadType().toString() + ")");
        }
        if (this.defaultCallback[response.payloadType()] !== undefined) {
            this.defaultCallback[response.payloadType()](response);
        }
    }

    static getVersion(builder, high = 2, low = 0) {
        return sisyfox.sisycol.Version.createVersion(builder, high, low);
    }

    send(request) {
        const payload = new Uint8Array(request.length + 4);
        payload.set(request, 4);
        const dv = new DataView(payload.buffer, 0, 4);
        dv.setUint32(0, request.length, true);
        console.log("Transmit " + request.length.toString() + " Bytes");
        this.socket.send(payload);
    }

    prepareSend(payloadType, payload, builder) {
        sisyfox.sisycol.Root.startRoot(builder);
        sisyfox.sisycol.Root.addVersion(builder, Sisyfox.getVersion(builder));
        sisyfox.sisycol.Root.addMessageId(builder, 0);
        sisyfox.sisycol.Root.addPayloadType(builder, payloadType);
        sisyfox.sisycol.Root.addPayload(builder, payload);
        const request = sisyfox.sisycol.Root.endRoot(builder);
        builder.finish(request);
        console.log("Transmit " + builder.asUint8Array().length.toString() + " Bytes");
        this.socket.send(builder.asUint8Array());
    }

    setCallback(payloadType, func) {
        if (this.callback[payloadType] === undefined) {
            this.callback[payloadType] = [];
        }
        this.callback[payloadType].push(func);
    }

    callDefaultCallback(payloadType, response) {
        if (this.defaultCallback[payloadType] !== undefined) {
            this.defaultCallback[payloadType](response);
        }
    }

    addInitCallback(func) {
        this.initCallbacks.push(func);
    }

    addDefaultCallbacks() {
        this.defaultCallback[sisyfox.sisycol.Payload.AddUser] = function () {
            console.log("new user was created.");
        };
        this.defaultCallback[sisyfox.sisycol.Payload.AddScore] = function () {
            console.log("new score was added");
        };
        this.defaultCallback[sisyfox.sisycol.Payload.SetUser] = function () {
            console.log("changed user");
        };
        this.defaultCallback[sisyfox.sisycol.Payload.UnsetUser] = function () {
            console.log("user logged out");
        };
        this.defaultCallback[sisyfox.sisycol.Payload.SetSetting] = function (response) {
            console.log("changed setting")
{'fraction_non_alphanumeric': 0.12933343537154665, 'fraction_numerical': 0.004821305578939313, 'mean_word_length': 2.0208044382801664, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3553493', 'n_tokens_mistral': 3756, 'n_tokens_neox': 3600, 'n_words': 594}