bitburner-src/src/ui/React/ServerDropdown.jsx

68 lines
1.9 KiB
React
Raw Normal View History

2019-03-25 04:03:24 +01:00
/**
* Creates a dropdown (select HTML element) with server hostnames as options
*
* Configurable to only contain certain types of servers
*/
import React from "react";
import { AllServers } from "../../Server/AllServers";
import { HacknetServer } from "../../Hacknet/HacknetServer";
// TODO make this an enum when this gets converted to TypeScript
export const ServerType = {
2021-09-05 01:09:30 +02:00
All: 0,
Foreign: 1, // Hackable, non-owned servers
Owned: 2, // Home Computer, Purchased Servers, and Hacknet Servers
Purchased: 3, // Everything from Owned except home computer
};
2019-03-25 04:03:24 +01:00
export class ServerDropdown extends React.Component {
2021-09-05 01:09:30 +02:00
/**
* Checks if the server should be shown in the dropdown menu, based on the
* 'serverType' property
*/
isValidServer(s) {
const type = this.props.serverType;
switch (type) {
case ServerType.All:
return true;
case ServerType.Foreign:
return s.hostname !== "home" && !s.purchasedByPlayer;
case ServerType.Owned:
2021-09-09 05:47:34 +02:00
return s.purchasedByPlayer || s instanceof HacknetServer || s.hostname === "home";
2021-09-05 01:09:30 +02:00
case ServerType.Purchased:
return s.purchasedByPlayer || s instanceof HacknetServer;
default:
2021-09-09 05:47:34 +02:00
console.warn(`Invalid ServerType specified for ServerDropdown component: ${type}`);
2021-09-05 01:09:30 +02:00
return false;
2019-03-25 04:03:24 +01:00
}
2021-09-05 01:09:30 +02:00
}
2019-03-25 04:03:24 +01:00
2021-09-05 01:09:30 +02:00
/**
* Given a Server object, creates a Option element
*/
renderOption(s) {
return (
<option key={s.hostname} value={s.hostname}>
{s.hostname}
</option>
);
}
2019-03-25 04:03:24 +01:00
2021-09-05 01:09:30 +02:00
render() {
const servers = [];
for (const serverName in AllServers) {
const server = AllServers[serverName];
if (this.isValidServer(server)) {
servers.push(this.renderOption(server));
}
2019-03-25 04:03:24 +01:00
}
2021-09-05 01:09:30 +02:00
return (
2021-09-09 05:47:34 +02:00
<select className={"dropdown"} onChange={this.props.onChange} style={this.props.style}>
2021-09-05 01:09:30 +02:00
{servers}
</select>
);
}
2019-03-25 04:03:24 +01:00
}