2021-08-20 04:22:21 +02:00
|
|
|
import React, { useState, useEffect } from "react";
|
|
|
|
import ReactDOM from "react-dom";
|
|
|
|
import { killWorkerScript } from "../src/Netscript/killWorkerScript";
|
|
|
|
import { RunningScript } from "../src/Script/RunningScript";
|
|
|
|
|
|
|
|
import { createElement } from "./uiHelpers/createElement";
|
|
|
|
import { removeElementById } from "./uiHelpers/removeElementById";
|
|
|
|
|
|
|
|
let gameContainer: HTMLElement;
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
function getGameContainer(): void {
|
|
|
|
const container = document.getElementById("entire-game-container");
|
|
|
|
if (container == null) {
|
|
|
|
throw new Error(`Failed to find game container DOM element`)
|
|
|
|
}
|
|
|
|
|
|
|
|
gameContainer = container;
|
|
|
|
document.removeEventListener("DOMContentLoaded", getGameContainer);
|
|
|
|
}
|
|
|
|
|
|
|
|
document.addEventListener("DOMContentLoaded", getGameContainer);
|
|
|
|
})();
|
|
|
|
|
|
|
|
interface IProps {
|
|
|
|
script: RunningScript;
|
|
|
|
container: HTMLElement;
|
|
|
|
id: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
function ScriptLogPopup(props: IProps): React.ReactElement {
|
|
|
|
const setRerender = useState(false)[1];
|
|
|
|
|
2021-08-20 21:45:21 +02:00
|
|
|
function rerender(): void {
|
2021-08-20 04:22:21 +02:00
|
|
|
setRerender(old => !old);
|
|
|
|
}
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const id = setInterval(rerender, 1000);
|
|
|
|
return () => clearInterval(id);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
|
|
function close(): void {
|
|
|
|
const content = document.getElementById(props.id);
|
|
|
|
if (content == null) return;
|
|
|
|
ReactDOM.unmountComponentAtNode(content);
|
|
|
|
removeElementById(props.id);
|
|
|
|
}
|
|
|
|
|
2021-08-23 07:15:20 +02:00
|
|
|
useEffect(() => {
|
|
|
|
function closeHandler(event: KeyboardEvent) {
|
|
|
|
if(event.keyCode === 27) {
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
document.addEventListener('keydown', closeHandler);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
document.removeEventListener('keydown', closeHandler);
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
2021-08-20 04:22:21 +02:00
|
|
|
function kill(): void {
|
|
|
|
killWorkerScript(props.script, props.script.server, true);
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
|
|
|
function drag(event: React.MouseEvent<HTMLElement, MouseEvent>): void {
|
|
|
|
event.preventDefault();
|
|
|
|
let x = event.clientX;
|
|
|
|
let y = event.clientY;
|
|
|
|
let left = props.container.offsetLeft+props.container.clientWidth/2;
|
2021-08-21 08:31:37 +02:00
|
|
|
let top = props.container.offsetTop+props.container.clientWidth/5;
|
2021-08-20 04:22:21 +02:00
|
|
|
function mouseMove(event: MouseEvent): void {
|
|
|
|
left+=event.clientX-x;
|
|
|
|
top+=event.clientY-y;
|
|
|
|
props.container.style.left=left+'px';
|
|
|
|
props.container.style.top=top+'px';
|
|
|
|
// reset right and bottom to avoid the window stretching
|
|
|
|
props.container.style.right='';
|
|
|
|
props.container.style.bottom='';
|
|
|
|
x=event.clientX;
|
|
|
|
y=event.clientY;
|
2021-08-20 21:45:21 +02:00
|
|
|
}
|
|
|
|
function mouseUp(): void {
|
2021-08-20 04:22:21 +02:00
|
|
|
document.removeEventListener('mouseup', mouseUp)
|
|
|
|
document.removeEventListener('mousemove', mouseMove)
|
2021-08-20 21:45:21 +02:00
|
|
|
}
|
2021-08-20 04:22:21 +02:00
|
|
|
document.addEventListener('mouseup', mouseUp)
|
|
|
|
document.addEventListener('mousemove', mouseMove)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (<>
|
|
|
|
<div className="log-box-header" onMouseDown={drag}>
|
|
|
|
<p>{props.script.filename} {props.script.args.map((x: any): string => `${x}`).join(' ')}</p>
|
|
|
|
<div>
|
|
|
|
<button className="log-box-button" onClick={kill}>Kill Script</button>
|
|
|
|
<button className="log-box-button" onClick={close}>Close</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className="log-box-log-container">
|
2021-09-02 19:15:42 +02:00
|
|
|
<p>{props.script.logs.map((line: string, i: number): JSX.Element => <span key={i} style={{whiteSpace: "pre-line"}}>{line}<br /></span>)}</p>
|
2021-08-20 04:22:21 +02:00
|
|
|
</div>
|
|
|
|
</>);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function logBoxCreate(script: RunningScript): void {
|
2021-08-21 17:30:31 +02:00
|
|
|
const id = script.server+"-"+script.filename+script.args.map((x: any): string => `${x}`).join('-');
|
|
|
|
if(document.getElementById(id) !== null) return;
|
2021-08-20 04:22:21 +02:00
|
|
|
const container = createElement("div", {
|
|
|
|
class: "log-box-container",
|
|
|
|
id: id,
|
|
|
|
});
|
|
|
|
gameContainer.appendChild(container);
|
|
|
|
ReactDOM.render(<ScriptLogPopup script={script} id={id} container={container} />, container);
|
|
|
|
}
|
|
|
|
|