feat: Rentable Spaces

by @oobjectt
https://github.com/billsonnn/nitro-react/pull/147
This commit is contained in:
Niklas 2023-10-23 16:34:22 +02:00
parent e160833554
commit c6cf366aa4
4 changed files with 170 additions and 1 deletions

View File

@ -0,0 +1,46 @@
import {FriendlyTime} from "@nitro/renderer";
import {FC} from "react";
import {LocalizeText} from "../../../../api";
import {Button, Column, Flex, LayoutCurrencyIcon, NitroCardContentView, NitroCardHeaderView, NitroCardView, Text} from "../../../../common";
import {useFurnitureRentableSpaceWidget} from "../../../../hooks";
export const FurnitureRentableSpaceView: FC<{}> = props => {
const {renter, isRoomOwner, onRent, onCancelRent, onClose} = useFurnitureRentableSpaceWidget();
if (!renter) return null;
return (
<NitroCardView className="nitro-guide-tool no-resize" theme="primary-slim">
<NitroCardHeaderView headerText={LocalizeText("rentablespace.widget.title")} onCloseClick={onClose} />
<NitroCardContentView className="text-black">
<Column>
{!renter.rented && (
<>
<Text>{LocalizeText("rentablespace.widget.instructions")}</Text>
<Flex pointer center className="p-2 bg-primary border border-dark rounded text-light h3" onClick={onRent}>
{renter.price + " x"}&nbsp;
<LayoutCurrencyIcon type={-1} className="mt-1" />
&nbsp;
{LocalizeText("catalog.purchase_confirmation.rent")}
</Flex>
</>
)}
{renter.rented && (
<>
<Text bold>{LocalizeText("rentablespace.widget.rented_to_label")}</Text>
<Text italics>{renter.renterName}</Text>
<Text bold>{LocalizeText("rentablespace.widget.expires_label")}</Text>
<Text italics>{FriendlyTime.shortFormat(renter.timeRemaining)}</Text>
{isRoomOwner && (
<Button variant="danger" className="mt-2" onClick={onCancelRent}>
{LocalizeText("rentablespace.widget.cancel_rent")}
</Button>
)}
</>
)}
</Column>
</NitroCardContentView>
</NitroCardView>
);
};

View File

@ -13,6 +13,7 @@ import {FurnitureGiftOpeningView} from "./FurnitureGiftOpeningView";
import {FurnitureHighScoreView} from "./FurnitureHighScoreView";
import {FurnitureInternalLinkView} from "./FurnitureInternalLinkView";
import {FurnitureMannequinView} from "./FurnitureMannequinView";
import { FurnitureRentableSpaceView } from './FurnitureRentableSpaceView';
import {FurnitureRoomLinkView} from "./FurnitureRoomLinkView";
import {FurnitureSpamWallPostItView} from "./FurnitureSpamWallPostItView";
import {FurnitureStackHeightView} from "./FurnitureStackHeightView";
@ -44,6 +45,7 @@ export const FurnitureWidgetsView: FC<{}> = props => {
<FurnitureTrophyView />
<FurnitureContextMenuView />
<FurnitureYoutubeDisplayView />
<FurnitureRentableSpaceView />
<FurnitureFootballGateView />
</Base>
);

View File

@ -5,16 +5,18 @@ export * from "./useFurnitureCraftingWidget";
export * from "./useFurnitureDimmerWidget";
export * from "./useFurnitureExchangeWidget";
export * from "./useFurnitureExternalImageWidget";
export * from './useFurnitureFootballGateWidget';
export * from "./useFurnitureFootballGateWidget";
export * from "./useFurnitureFriendFurniWidget";
export * from "./useFurnitureHighScoreWidget";
export * from "./useFurnitureInternalLinkWidget";
export * from "./useFurnitureMannequinWidget";
export * from "./useFurniturePlaylistEditorWidget";
export * from "./useFurniturePresentWidget";
export * from "./useFurnitureRentableSpaceWidget";
export * from "./useFurnitureRoomLinkWidget";
export * from "./useFurnitureSpamWallPostItWidget";
export * from "./useFurnitureStackHeightWidget";
export * from "./useFurnitureStickieWidget";
export * from "./useFurnitureTrophyWidget";
export * from "./useFurnitureYoutubeWidget";

View File

@ -0,0 +1,119 @@
import {
RentableSpaceCancelRentMessageComposer,
RentableSpaceRentMessageComposer,
RentableSpaceStatusMessageEvent,
RentableSpaceStatusMessageParser,
RoomEngineTriggerWidgetEvent,
RoomWidgetEnum,
} from "@nitro/renderer";
import {useState} from "react";
import {GetRoomEngine, GetSessionDataManager, LocalizeText, SendMessageComposer} from "../../../../api";
import {useMessageEvent, useRoomEngineEvent} from "../../../events";
import {useNavigator} from "../../../navigator";
import {useNotification} from "../../../notification";
import {useFurniRemovedEvent} from "../../engine";
const useFurnitureRentableSpaceWidgetState = () => {
const [renter, setRenter] = useState<RentableSpaceStatusMessageParser>(null);
const [itemId, setItemId] = useState<number>(-1);
const [category, setCategory] = useState<number>(-1);
const {navigatorData = null} = useNavigator();
const {simpleAlert} = useNotification();
const isRoomOwner = GetSessionDataManager().userName === navigatorData.enteredGuestRoom?.ownerName;
const onClose = () => {
setItemId(-1);
setCategory(-1);
setRenter(null);
};
const onRent = () => {
if (!itemId) return;
SendMessageComposer(new RentableSpaceRentMessageComposer(itemId));
};
const onCancelRent = () => {
if (!itemId) return;
SendMessageComposer(new RentableSpaceCancelRentMessageComposer(itemId));
onClose();
};
const getRentErrorCode = (code: number) => {
let errorAlert = "";
switch (code) {
case RentableSpaceStatusMessageParser.SPACE_ALREADY_RENTED:
errorAlert = LocalizeText("rentablespace.widget.error_reason_already_rented");
break;
case RentableSpaceStatusMessageParser.SPACE_EXTEND_NOT_RENTED:
errorAlert = LocalizeText("rentablespace.widget.error_reason_not_rented");
break;
case RentableSpaceStatusMessageParser.SPACE_EXTEND_NOT_RENTED_BY_YOU:
errorAlert = LocalizeText("rentablespace.widget.error_reason_not_rented_by_you");
break;
case RentableSpaceStatusMessageParser.CAN_RENT_ONLY_ONE_SPACE:
errorAlert = LocalizeText("rentablespace.widget.error_reason_can_rent_only_one_space");
break;
case RentableSpaceStatusMessageParser.NOT_ENOUGH_CREDITS:
errorAlert = LocalizeText("rentablespace.widget.error_reason_not_enough_credits");
break;
case RentableSpaceStatusMessageParser.NOT_ENOUGH_PIXELS:
errorAlert = LocalizeText("rentablespace.widget.error_reason_not_enough_duckets");
break;
case RentableSpaceStatusMessageParser.CANT_RENT_NO_PERMISSION:
errorAlert = LocalizeText("rentablespace.widget.error_reason_no_permission");
break;
case RentableSpaceStatusMessageParser.CANT_RENT_NO_HABBO_CLUB:
errorAlert = LocalizeText("rentablespace.widget.error_reason_no_habboclub");
break;
case RentableSpaceStatusMessageParser.CANT_RENT:
errorAlert = LocalizeText("rentablespace.widget.error_reason_disabled");
break;
case RentableSpaceStatusMessageParser.CANT_RENT_GENERIC:
errorAlert = LocalizeText("rentablespace.widget.error_reason_generic");
break;
}
onClose();
return simpleAlert(errorAlert);
};
useRoomEngineEvent<RoomEngineTriggerWidgetEvent>(RoomEngineTriggerWidgetEvent.OPEN_WIDGET, event => {
if (event.widget !== RoomWidgetEnum.RENTABLESPACE) return;
const roomObject = GetRoomEngine().getRoomObject(event.roomId, event.objectId, event.category);
if (!roomObject) return;
setItemId(roomObject.id);
setCategory(event.category);
});
useFurniRemovedEvent(itemId !== -1 && category !== -1, event => {
if (event.id !== itemId || event.category !== category) return;
onCancelRent();
});
useMessageEvent<RentableSpaceStatusMessageEvent>(RentableSpaceStatusMessageEvent, event => {
const parser = event.getParser();
if (!parser) return;
if (
(parser.canRentErrorCode !== 0 && (!isRoomOwner || !GetSessionDataManager().isModerator)) ||
(parser.renterName === "" && parser.canRentErrorCode !== 0)
)
return getRentErrorCode(parser.canRentErrorCode);
setRenter(parser);
});
return {renter, isRoomOwner, onRent, onCancelRent, onClose};
};
export const useFurnitureRentableSpaceWidget = useFurnitureRentableSpaceWidgetState;