{"size": 2216, "ext": "js", "max_stars_repo_path": "class/server.js", "max_stars_repo_name": "dogerish/st", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "class/server.js", "max_issues_repo_name": "dogerish/st", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "class/server.js", "max_forks_repo_name": "dogerish/st", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 28.7792207792, "max_line_length": 85, "alphanum_fraction": 0.630866426}
const { request, requestv2  } = require("../utils/requests.js");
const { ServerNotFoundError } = require("../utils/errors.js");
const STAsync          = require("./async.js");
const STLiveGame       = require("../struct/livegame.js");
const STServerInfo     = require("../struct/serverinfo.js");
const STCountry        = require("../struct/country.js");
const STServerActivity = require("../struct/serveractivity.js");

class STServer extends STAsync
{
	// list all servers online
	static async /*Array<STServer>*/ list()
	{ return (await requestv2`servers`).map(s => new STServer(s.host, s.port).set(s)); }

	/*
	String       host;
	Number       port;
	String       description;
	String       descriptionStyled;

	Number       clients;
	Number       maxClients;
	Boolean      isFull;
	Boolean      zombie;
	STLiveGame   game;
	String       masterMode;

	STServerInfo info;
	STCountry    country;
	Number       version;
	?Number      totalGames;
	?Number      rank;
	*/
	constructor(/*String*/ host, /*Number*/ port)
	{
		super(
			[
				"host",    "port",       "description", "descriptionStyled",
				"clients", "maxClients", "isFull",      "zombie",            "game",
				"info",    "country",    "version",     "?totalGames",       "?rank"
			],
			{ host, port }
		);
	}

	// fetch and return this
	async /*STServer*/ fetch()
	{
		if ((await requestv2`servers`).some(
			({ host, port }) => host == this.host && port == this.port
		))
			return this.set(await requestv2`server/${this.host}/${this.port}`);
		throw new ServerNotFoundError(this.host, this.port);
	}

	// set props based on reutrn from API call, returns this
	/*STServer*/ set(/*Object*/ from)
	{
		this.game = new STLiveGame(from);
		if (from.info) from.info = new STServerInfo(from.info);
		if (from.country !== undefined)
			from.country = new STCountry(from.country, from.countryName);
		for (let key of ["totalGames", "rank"])
			if (from[key]) from[key] = Number(from[key]);
		return this.copyProps(from);
	}

	// fetch activity for this server
	async /*STServerActivity*/ fetchActivity()
	{
		let a = await request`server/activity/${this.host}/${this.port}`;
		return new STServerActivity(a.day, a.month);
	}
}

module.exports = STServer;
