<commit_before>///<reference path="./typings/index.d.ts"/>
import * as redis from "redis";
import RoutingServer from "./routingserver";
import ListenServer from "./listenserver";
import {ConfigSettings, Config, ConfigOptions} from "./config";
import Client from "./client";
import {requireNoCache} from "./utils";
import * as _ from "lodash";
import ClientCommandHandler from "./clientcommandhandler";
import ClientPacketHandler from "./clientpackethandler";
import TerrariaServerPacketHandler from "./terrariaserverpackethandler";
import ServerDetails from "./serverdetails";
import GlobalHandlers from "./globalhandlers";

class Dimensions {
  id: number;
  servers: { [id: string]: RoutingServer };
  options: ConfigOptions;
  listenServers: { [id: number]: ListenServer };
  handlers: GlobalHandlers;
  redisClient: redis.RedisClient;
  serversDetails: { [id: string]: ServerDetails };
  
  constructor() {
    this.id = 0;
    this.options = ConfigSettings.options;
    this.handlers = {
      command: new ClientCommandHandler(),
      clientPacketHandler: new ClientPacketHandler(),
      terrariaServerPacketHandler: new TerrariaServerPacketHandler()
    };

    this.redisClient = redis.createClient();
    this.redisClient.subscribe('dimensions_cli');
    this.redisClient
      .on('message', (channel: string, message: string) => {
        if (channel === "dimensions_cli") {
          this.handleCommand(message);
        }
      })
      .on('error', (err: Error) => {
        console.log("RedisError: " + err);
      });

    this.serversDetails = {};
    this.listenServers = {};
    this.servers = {};


    //self.interface = new Interface(self.handleCommand.bind(self));

    for (let i: number = 0; i < ConfigSettings.servers.length; i++) {
      let listenKey = ConfigSettings.servers[i].listenPort;
      this.listenServers[listenKey] = new ListenServer(ConfigSettings.servers[i], this.serversDetails, this.handlers, this.servers, this.options);

      for (let j: number = 0; j < ConfigSettings.servers[i].routingServers.length; j++) {
        this.servers[ConfigSettings.servers[i].routingServers[j].name] = ConfigSettings.servers[i].routingServers[j];
      }
    }

    /*setInterval(function() {
      self.printServerCounts();
    }, 5000);*/
  }

  printServerCounts(): void {
    let serverKeys: string[] = _.keys(this.servers);
    let info = "";
    for (let i: number = 0; i < serverKeys.length; i++) {
      info += "[" + serverKeys[i] + ": " + this.serversDetails[serverKeys[i]].clientCount + "] ";
    }
    console.log(info);
  }

  handleCommand(cmd: string): void {
    switch (cmd) {
      case "players":
        this.printServerCounts();
        break;
      case "reload":
        this.reloadServers();
        break;
      case "reloadhandlers":
        this.reloadClientHandlers();
        this.reloadTerrariaServerHandlers();
        console.log("\u001b[33mReloaded Packet Handlers.\u001b[37m");
        break;
      case "reloadcmds":
          try {
            let ClientCommandHandler = requireNoCache('./clientcommandhandler.js', require);
            this.handlers.command = new ClientCommandHandler();
          } catch (e) {
            console.log("Error loading Command Handler: " + e);
          }
        
        console.log("\u001b[33mReloaded Command Handler.\u001b[37m");
        break;
    }
  }

  reloadClientHandlers(): void {
      try {
        let ClientPacketHandler = requireNoCache('./clientpackethandler.js', require);
        this.handlers.clientPacketHandler = new ClientPacketHandler();
      } catch (e) {
        console.log("Error loading Client Packet Handler: " + e);
      }
  }

  reloadTerrariaServerHandlers(): void {
      try {
        let TerrariaServerPacketHandler = requireNoCache('./terrariaserverpackethandler.js', require);
        this.handlers.terrariaServerPacketHandler = new TerrariaServerPacketHandler();
      } catch (e) {
        console.log("Error loading TerrariaServer Packet Handler: " + e);
      }
  }

  reloadServers(): void {
      try {
        let ConfigSettings = requireNoCache('./config.js', require).ConfigSettings;
        let currentRoster = {};
        let runAfterFinished = [];
        for (let i: number = 0; i < ConfigSettings.servers.length; i++) {
          let listenKey: number = ConfigSettings.servers[i].listenPort;
          if (this.listenServers[listenKey]) {
            this.listenServers[listenKey].updateInfo(ConfigSettings.servers[i]);
            for (var j = 0; j < ConfigSettings.servers[i].routingServers.length; j++) {
              this.servers[ConfigSettings.servers[i].routingServers[j].name] = ConfigSettings.servers[i].routingServers[j];
            }
          } else {
            runAfterFinished.push({<commit_msg>Fix reloading of handlers<commit_after>///<reference path="./typings/index.d.ts"/>
import * as redis from "redis";
import RoutingServer from "./routingserver";
import ListenServer from "./listenserver";
import {ConfigSettings, Config, ConfigOptions} from "./config";
import Client from "./client";
import {requireNoCache} from "./utils";
import * as _ from "lodash";
import ClientCommandHandler from "./clientcommandhandler";
import ClientPacketHandler from "./clientpackethandler";
import TerrariaServerPacketHandler from "./terrariaserverpackethandler";
import ServerDetails from "./serverdetails";
import GlobalHandlers from "./globalhandlers";

class Dimensions {
  id: number;
  servers: { [id: string]: RoutingServer };
  options: ConfigOptions;
  listenServers: { [id: number]: ListenServer };
  handlers: GlobalHandlers;
  redisClient: redis.RedisClient;
  serversDetails: { [id: string]: ServerDetails };
  
  constructor() {
    this.id = 0;
    this.options = ConfigSettings.options;
    this.handlers = {
      command: new ClientCommandHandler(),
      clientPacketHandler: new ClientPacketHandler(),
      terrariaServerPacketHandler: new TerrariaServerPacketHandler()
    };

    this.redisClient = redis.createClient();
    this.redisClient.subscribe('dimensions_cli');
    this.redisClient
      .on('message', (channel: string, message: string) => {
        if (channel === "dimensions_cli") {
          this.handleCommand(message);
        }
      })
      .on('error', (err: Error) => {
        console.log("RedisError: " + err);
      });

    this.serversDetails = {};
    this.listenServers = {};
    this.servers = {};


    //self.interface = new Interface(self.handleCommand.bind(self));

    for (let i: number = 0; i < ConfigSettings.servers.length; i++) {
      let listenKey = ConfigSettings.servers[i].listenPort;
      this.listenServers[listenKey] = new ListenServer(ConfigSettings.servers[i], this.serversDetails, this.handlers, this.servers, this.options);

      for (let j: number = 0; j < ConfigSettings.servers[i].routingServers.length; j++) {
        this.servers[ConfigSettings.servers[i].routingServers[j].name] = ConfigSettings.servers[i].routingServers[j];
      }
    }

    /*setInterval(function() {
      self.printServerCounts();
    }, 5000);*/
  }

  printServerCounts(): void {
    let serverKeys: string[] = _.keys(this.servers);
    let info = "";
    for (let i: number = 0; i < serverKeys.length; i++) {
      info += "[" + serverKeys[i] + ": " + this.serversDetails[serverKeys[i]].clientCount + "] ";
    }
    console.log(info);
  }

  handleCommand(cmd: string): void {
    switch (cmd) {
      case "players":
        this.printServerCounts();
        break;
      case "reload":
        this.reloadServers();
        break;
      case "reloadhandlers":
        this.reloadClientHandlers();
        this.reloadTerrariaServerHandlers();
        console.log("\u001b[33mReloaded Packet Handlers.\u001b[37m");
        break;
      case "reloadcmds":
          try {
            let ClientCommandHandler = requireNoCache('./clientcommandhandler.js', require).default;
            this.handlers.command = new ClientCommandHandler();
          } catch (e) {
            console.log("Error loading Command Handler: " + e);
          }
        
        console.log("\u001b[33mReloaded Command Handler.\u001b[37m");
        break;
    }
  }

  reloadClientHandlers(): void {
      try {
        let ClientPacketHandler = requireNoCache('./clientpackethandler.js', require).default;
        this.handlers.clientPacketHandler = new ClientPacketHandler();
      } catch (e) {
        console.log("Error loading Client Packet Handler: " + e);
      }
  }

  reloadTerrariaServerHandlers(): void {
      try {
        let TerrariaServerPacketHandler = requireNoCache('./terrariaserverpackethandler.js', require).default;
        this.handlers.terrariaServerPacketHandler = new TerrariaServerPacketHandler();
      } catch (e) {
        console.log("Error loading TerrariaServer Packet Handler: " + e);
      }
  }

  reloadServers(): void {
      try {
        let ConfigSettings = requireNoCache('./config.js', require).ConfigSettings;
        let currentRoster = {};
        let runAfterFinished = [];
        for (let i: number = 0; i < ConfigSettings.servers.length; i++) {
          let listenKey: number = ConfigSettings.servers[i].listenPort;
          if (this.listenServers[listenKey]) {
            this.listenServers[listenKey].updateInfo(ConfigSettings.servers[i]);
            for (var j = 0; j < ConfigSettings.servers[i].routingServers.length; j++) {
              this.servers[ConfigSettings.servers[i].routingServers[j].name] = ConfigSettings.servers[i].routingServers[j];
            }
          } else {
            runAfterFinished.push({
{'fraction_non_alphanumeric': 0.13364681295715777, 'fraction_numerical': 0.006269592476489028, 'mean_word_length': 3.036693378321383, 'pattern_counts': {'":': 10, '<': 15, '<?xml version=': 0, '>': 9, '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': '291805', 'n_tokens_mistral': 2802, 'n_tokens_neox': 2770, 'n_words': 562}