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";
|
2021-10-07 22:04:04 +02:00
|
|
|
import { GetAllServers } from "../../Server/AllServers";
|
2021-09-09 09:17:01 +02:00
|
|
|
import { Server } from "../../Server/Server";
|
2021-10-07 22:04:04 +02:00
|
|
|
import { BaseServer } from "../../Server/BaseServer";
|
2019-03-25 04:03:24 +01:00
|
|
|
|
|
|
|
import { HacknetServer } from "../../Hacknet/HacknetServer";
|
2021-09-25 19:52:26 +02:00
|
|
|
import Select, { SelectChangeEvent } from "@mui/material/Select";
|
|
|
|
import MenuItem from "@mui/material/MenuItem";
|
2021-10-11 23:43:48 +02:00
|
|
|
import Button from "@mui/material/Button";
|
2019-03-25 04:03:24 +01:00
|
|
|
|
|
|
|
// 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
|
|
|
|
2021-09-09 09:17:01 +02:00
|
|
|
interface IProps {
|
2021-10-11 23:43:48 +02:00
|
|
|
purchase: () => void;
|
|
|
|
canPurchase: boolean;
|
2021-09-09 09:17:01 +02:00
|
|
|
serverType: number;
|
2021-09-25 19:52:26 +02:00
|
|
|
onChange: (event: SelectChangeEvent<string>) => void;
|
|
|
|
value: string;
|
2021-09-09 09:17:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function ServerDropdown(props: IProps): React.ReactElement {
|
2021-09-05 01:09:30 +02:00
|
|
|
/**
|
|
|
|
* Checks if the server should be shown in the dropdown menu, based on the
|
|
|
|
* 'serverType' property
|
|
|
|
*/
|
2021-10-07 22:04:04 +02:00
|
|
|
function isValidServer(s: BaseServer): boolean {
|
2021-09-09 09:17:01 +02:00
|
|
|
const purchased = s instanceof Server && s.purchasedByPlayer;
|
|
|
|
const type = props.serverType;
|
2021-09-05 01:09:30 +02:00
|
|
|
switch (type) {
|
|
|
|
case ServerType.All:
|
|
|
|
return true;
|
|
|
|
case ServerType.Foreign:
|
2021-09-09 09:17:01 +02:00
|
|
|
return s.hostname !== "home" && !purchased;
|
2021-09-05 01:09:30 +02:00
|
|
|
case ServerType.Owned:
|
2021-09-09 09:17:01 +02:00
|
|
|
return purchased || s instanceof HacknetServer || s.hostname === "home";
|
2021-09-05 01:09:30 +02:00
|
|
|
case ServerType.Purchased:
|
2021-09-09 09:17:01 +02:00
|
|
|
return purchased || s instanceof HacknetServer;
|
2021-09-05 01:09:30 +02:00
|
|
|
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-09 09:17:01 +02:00
|
|
|
const servers = [];
|
2021-10-07 22:04:04 +02:00
|
|
|
for (const server of GetAllServers()) {
|
2021-09-09 09:17:01 +02:00
|
|
|
if (isValidServer(server)) {
|
|
|
|
servers.push(
|
2021-09-25 19:52:26 +02:00
|
|
|
<MenuItem key={server.hostname} value={server.hostname}>
|
2021-09-09 09:17:01 +02:00
|
|
|
{server.hostname}
|
2021-09-25 19:52:26 +02:00
|
|
|
</MenuItem>,
|
2021-09-09 09:17:01 +02:00
|
|
|
);
|
2019-03-25 04:03:24 +01:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2021-09-09 09:17:01 +02:00
|
|
|
|
|
|
|
return (
|
2021-10-11 23:43:48 +02:00
|
|
|
<Select
|
|
|
|
startAdornment={
|
|
|
|
<Button onClick={props.purchase} disabled={!props.canPurchase}>
|
|
|
|
Buy
|
|
|
|
</Button>
|
|
|
|
}
|
|
|
|
sx={{ mx: 1 }}
|
|
|
|
value={props.value}
|
|
|
|
onChange={props.onChange}
|
|
|
|
>
|
2021-09-09 09:17:01 +02:00
|
|
|
{servers}
|
2021-09-25 19:52:26 +02:00
|
|
|
</Select>
|
2021-09-09 09:17:01 +02:00
|
|
|
);
|
2019-03-25 04:03:24 +01:00
|
|
|
}
|