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

78 lines
2.0 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:
2019-03-25 04:03:24 +01:00
return (
2021-09-05 01:09:30 +02:00
s.purchasedByPlayer ||
s instanceof HacknetServer ||
s.hostname === "home"
);
case ServerType.Purchased:
return s.purchasedByPlayer || s instanceof HacknetServer;
default:
console.warn(
`Invalid ServerType specified for ServerDropdown component: ${type}`,
);
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 (
<select
className={"dropdown"}
onChange={this.props.onChange}
style={this.props.style}
>
{servers}
</select>
);
}
2019-03-25 04:03:24 +01:00
}