mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-25 18:50:56 +02:00
fix contract
This commit is contained in:
+21
-15
@@ -168,22 +168,28 @@ export class CodingContract {
|
||||
async prompt(): Promise<CodingContractResult> {
|
||||
const popupId = `coding-contract-prompt-popup-${this.fn}`;
|
||||
return new Promise<CodingContractResult>((resolve) => {
|
||||
createPopup(popupId, CodingContractPopup, {
|
||||
c: this,
|
||||
popupId: popupId,
|
||||
onClose: () => {
|
||||
resolve(CodingContractResult.Cancelled);
|
||||
removePopup(popupId);
|
||||
createPopup(
|
||||
popupId,
|
||||
CodingContractPopup,
|
||||
{
|
||||
c: this,
|
||||
popupId: popupId,
|
||||
onClose: () => {
|
||||
resolve(CodingContractResult.Cancelled);
|
||||
removePopup(popupId);
|
||||
},
|
||||
onAttempt: (val: string) => {
|
||||
console.error("attempting");
|
||||
if (this.isSolution(val)) {
|
||||
resolve(CodingContractResult.Success);
|
||||
} else {
|
||||
resolve(CodingContractResult.Failure);
|
||||
}
|
||||
removePopup(popupId);
|
||||
},
|
||||
},
|
||||
onAttempt: (val: string) => {
|
||||
if (this.isSolution(val)) {
|
||||
resolve(CodingContractResult.Success);
|
||||
} else {
|
||||
resolve(CodingContractResult.Failure);
|
||||
}
|
||||
removePopup(popupId);
|
||||
},
|
||||
});
|
||||
() => resolve(CodingContractResult.Cancelled),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
+12
-2
@@ -85,7 +85,7 @@ $(document).keydown(function (event) {
|
||||
// Terminal
|
||||
if (routing.isOn(Page.Terminal)) {
|
||||
var terminalInput = document.getElementById("terminal-input-text-box");
|
||||
if (terminalInput != null && !event.ctrlKey && !event.shiftKey) {
|
||||
if (terminalInput != null && !event.ctrlKey && !event.shiftKey && !Terminal.contractOpen) {
|
||||
terminalInput.focus();
|
||||
}
|
||||
|
||||
@@ -302,7 +302,7 @@ $(document).keydown(function (e) {
|
||||
terminalCtrlPressed = true;
|
||||
} else if (e.shiftKey) {
|
||||
shiftKeyPressed = true;
|
||||
} else if (terminalCtrlPressed || shiftKeyPressed) {
|
||||
} else if (terminalCtrlPressed || shiftKeyPressed || Terminal.contractOpen) {
|
||||
// Don't focus
|
||||
} else {
|
||||
var inputTextBox = document.getElementById("terminal-input-text-box");
|
||||
@@ -338,6 +338,9 @@ let Terminal = {
|
||||
commandHistory: [],
|
||||
commandHistoryIndex: 0,
|
||||
|
||||
// True if a Coding Contract prompt is opened
|
||||
contractOpen: false,
|
||||
|
||||
// Full Path of current directory
|
||||
// Excludes the trailing forward slash
|
||||
currDir: "/",
|
||||
@@ -2571,12 +2574,18 @@ let Terminal = {
|
||||
},
|
||||
|
||||
runContract: async function (contractName) {
|
||||
// There's already an opened contract
|
||||
if (Terminal.contractOpen) {
|
||||
return post("ERROR: There's already a Coding Contract in Progress");
|
||||
}
|
||||
|
||||
const serv = Player.getCurrentServer();
|
||||
const contract = serv.getContract(contractName);
|
||||
if (contract == null) {
|
||||
return post("ERROR: No such contract");
|
||||
}
|
||||
|
||||
Terminal.contractOpen = true;
|
||||
const res = await contract.prompt();
|
||||
|
||||
switch (res) {
|
||||
@@ -2603,6 +2612,7 @@ let Terminal = {
|
||||
post("Contract cancelled");
|
||||
break;
|
||||
}
|
||||
Terminal.contractOpen = false;
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@ import { KEY } from "../../../utils/helpers/keyCodes";
|
||||
|
||||
import { CodingContract, CodingContractType, CodingContractTypes } from "../../CodingContracts";
|
||||
import { ClickableTag, CopyableText } from "./CopyableText";
|
||||
import { PopupCloseButton } from "./PopupCloseButton";
|
||||
|
||||
type IProps = {
|
||||
c: CodingContract;
|
||||
@@ -28,9 +27,6 @@ export function CodingContractPopup(props: IProps): React.ReactElement {
|
||||
if (event.keyCode === KEY.ENTER && value !== "") {
|
||||
event.preventDefault();
|
||||
props.onAttempt(answer);
|
||||
} else if (event.keyCode === KEY.ESC) {
|
||||
event.preventDefault();
|
||||
props.onClose();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,8 +55,9 @@ export function CodingContractPopup(props: IProps): React.ReactElement {
|
||||
onChange={onChange}
|
||||
onKeyDown={onKeyDown}
|
||||
/>
|
||||
<PopupCloseButton popup={props.popupId} onClose={() => props.onAttempt(answer)} text={"Solve"} />
|
||||
<PopupCloseButton popup={props.popupId} onClose={props.onClose} text={"Close"} />
|
||||
<button className={"std-button"} onClick={() => props.onAttempt(answer)}>
|
||||
Solve
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -9,12 +9,12 @@ interface IProps<T> {
|
||||
content: (props: T) => React.ReactElement;
|
||||
id: string;
|
||||
props: T;
|
||||
removePopup: (id: string) => void;
|
||||
removePopup: () => void;
|
||||
}
|
||||
|
||||
export function Popup<T>(props: IProps<T>): React.ReactElement {
|
||||
function keyDown(event: KeyboardEvent): void {
|
||||
if (event.key === "Escape") props.removePopup(props.id);
|
||||
if (event.key === "Escape") props.removePopup();
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
|
||||
@@ -39,15 +39,17 @@ export function createPopup<T>(
|
||||
id: string,
|
||||
rootComponent: (props: T) => React.ReactElement,
|
||||
props: T,
|
||||
onClose?: () => void,
|
||||
): HTMLElement | null {
|
||||
let container = document.getElementById(id);
|
||||
if (container == null) {
|
||||
function onClick(this: HTMLElement, event: MouseEvent): any {
|
||||
function onClick(this: HTMLElement, event: MouseEvent): void {
|
||||
if (!event.srcElement) return;
|
||||
if (!(event.srcElement instanceof HTMLElement)) return;
|
||||
const clickedId = (event.srcElement as HTMLElement).id;
|
||||
if (clickedId !== id) return;
|
||||
removePopup(id);
|
||||
if (onClose) onClose();
|
||||
}
|
||||
const backgroundColor = deepestPopupId === "" ? "rgba(0,0,0,0.5)" : "rgba(0,0,0,0)";
|
||||
container = createElement("div", {
|
||||
@@ -62,7 +64,18 @@ export function createPopup<T>(
|
||||
}
|
||||
|
||||
if (deepestPopupId === "") deepestPopupId = id;
|
||||
ReactDOM.render(<Popup content={rootComponent} id={id} props={props} removePopup={removePopup} />, container);
|
||||
ReactDOM.render(
|
||||
<Popup
|
||||
content={rootComponent}
|
||||
id={id}
|
||||
props={props}
|
||||
removePopup={() => {
|
||||
removePopup(id);
|
||||
if (onClose) onClose();
|
||||
}}
|
||||
/>,
|
||||
container,
|
||||
);
|
||||
|
||||
return container;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user