feat: 🎸 事件交互响应

dev
杨晓宇 3 years ago
parent bdf0fb463e
commit a81dfcb876

@ -5,17 +5,23 @@ export const PROMPT_DICT: {
[part: string]: PromptToken[]; [part: string]: PromptToken[];
} = { } = {
test: [ test: [
{ value: "黑长直" }, { value: "黑长直", class: "adj" },
{ value: "电波系" }, { value: "电波系", class: "adj" },
{ value: "伪娘" }, { value: "伪娘", class: "n" },
{ value: "兔女郎" }, { value: "兔女郎", class: "adj" },
{ value: "傲娇" }, { value: "傲娇", class: "adj" },
{ value: "病娇" }, { value: "病娇", class: "adj" },
{ value: "天然呆" }, { value: "天然呆", class: "adj" },
{ value: "活力" }, { value: "活力", class: "adj" },
{ value: "抖S" }, { value: "抖S", class: "adj" },
{ value: "抖M" }, { value: "抖M", class: "adj" },
{ value: "三无" }, { value: "三无", class: "adj" },
{ value: "泳装", class: "adj" },
{ value: "银发", class: "adj" },
{ value: "女仆", class: "n" },
{ value: "美少女", class: "n" },
{ value: "御姐", class: "n" },
{ value: "萝莉", class: "n" },
], ],
}; };

@ -1,8 +1,9 @@
import { Button, Skeleton } from "@mui/material"; import { Button, CircularProgress, Divider, Skeleton } from "@mui/material";
import { PRESET_TOKENS } from "prompts/presets"; import { PRESET_TOKENS } from "prompts/presets";
import React, { useEffect, useState } from "react"; import React, { useEffect, useState } from "react";
import { generatePicture } from "service/service"; import { generatePicture } from "service/service";
import { GAME_SCENES, SceneProps } from "types"; import { GAME_SCENES, SceneProps } from "types";
import { Message } from "./ui";
type GenerateSceneProps = { type GenerateSceneProps = {
prompts: string[]; prompts: string[];
@ -27,7 +28,8 @@ export const GenerateScene = ({ setScene, prompts }: GenerateSceneProps) => {
uc: "lowres, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, username, blurry", uc: "lowres, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, username, blurry",
ucPreset: 0, ucPreset: 0,
width: 256, width: 256,
}).then((result) => { })
.then((result) => {
setLoading(false); setLoading(false);
console.log(result); console.log(result);
const source = result.data.split("\n"); const source = result.data.split("\n");
@ -44,6 +46,9 @@ export const GenerateScene = ({ setScene, prompts }: GenerateSceneProps) => {
// const canvas = document.getElementById("picture"); // const canvas = document.getElementById("picture");
// const context = canvas.getContext("2d"); // const context = canvas.getContext("2d");
// context.putImageData(imageData); // context.putImageData(imageData);
})
.catch((e) => {
Message.error(e.message);
}); });
}; };
useEffect(() => { useEffect(() => {
@ -52,17 +57,11 @@ export const GenerateScene = ({ setScene, prompts }: GenerateSceneProps) => {
return ( return (
<div> <div>
{loading && ( {loading && <CircularProgress />}
<Skeleton
sx={{ bgcolor: "grey.900" }}
variant="rectangular"
width={256}
height={384}
/>
)}
{!loading && !!code && ( {!loading && !!code && (
<div> <div>
<img src={`data:image/jpeg;base64,${code}`}></img> <img src={`data:image/jpeg;base64,${code}`}></img>
<Divider />
<Button <Button
variant={"outlined"} variant={"outlined"}
onClick={() => { onClick={() => {

@ -1,8 +1,9 @@
import { Button } from "@mui/material"; import { Button, CircularProgress, Divider } from "@mui/material";
import { getLotteryPool } from "../keywords/dict"; import { getLotteryPool } from "../keywords/dict";
import React, { useCallback, useEffect, useState } from "react"; import React, { useCallback, useEffect, useState } from "react";
import { GAME_SCENES, PromptToken, SceneProps } from "types"; import { GAME_SCENES, PromptToken, SceneProps } from "types";
import { OpenAIService } from "service/gpt"; import { OpenAIService } from "service/gpt";
import { Message } from "./ui";
type LotterySceneProps = { type LotterySceneProps = {
setPrompts: React.Dispatch<React.SetStateAction<string[]>>; setPrompts: React.Dispatch<React.SetStateAction<string[]>>;
@ -15,6 +16,7 @@ type ActivePromptToken = {
/** 抽奖界面 */ /** 抽奖界面 */
export const LotteryScene = ({ setScene, setPrompts }: LotterySceneProps) => { export const LotteryScene = ({ setScene, setPrompts }: LotterySceneProps) => {
const [activeTokens, setActiveTokens] = useState<ActivePromptToken[]>([]); const [activeTokens, setActiveTokens] = useState<ActivePromptToken[]>([]);
const [loading, setLoading] = useState(false);
const LOTTERY_POOL = getLotteryPool(); const LOTTERY_POOL = getLotteryPool();
const getPromptsByTokens = useCallback(async () => { const getPromptsByTokens = useCallback(async () => {
const { getPrompts } = await OpenAIService(); const { getPrompts } = await OpenAIService();
@ -48,25 +50,36 @@ export const LotteryScene = ({ setScene, setPrompts }: LotterySceneProps) => {
> >
</Button> </Button>
<Divider />
{activeTokens.map((token, index) => ( {activeTokens.map((token, index) => (
<Button <Button
variant={token.active ? "contained" : "outlined"} variant={token.active ? "contained" : "outlined"}
key={token.value} key={token.value}
onClick={() => { onClick={() => {
activeTokens[index].active = !activeTokens[index].active; const tokens = [...activeTokens];
setActiveTokens(activeTokens); tokens[index].active = !activeTokens[index].active;
setActiveTokens(tokens);
}} }}
> >
{token.value} {token.value}
</Button> </Button>
))} ))}
<Divider />
<Button <Button
variant={"outlined"}
onClick={async () => { onClick={async () => {
try {
setLoading(true);
await getPromptsByTokens(); await getPromptsByTokens();
setLoading(false);
setScene(GAME_SCENES.GENERATE); setScene(GAME_SCENES.GENERATE);
} catch (e: any) {
console.log(e);
Message.error(e.message);
}
}} }}
> >
{loading ? <CircularProgress /> : "开始召唤!"}
</Button> </Button>
</div> </div>
); );

@ -1,12 +1,66 @@
import React, { useState } from "react"; import React, { useEffect, useState } from "react";
import { GAME_SCENES, PromptToken } from "types"; import { GAME_SCENES, PromptToken } from "types";
import { MenuScene } from "./menu"; import { MenuScene } from "./menu";
import { LotteryScene } from "./lottery"; import { LotteryScene } from "./lottery";
import { GenerateScene } from "./generate"; import { GenerateScene } from "./generate";
import { Alert, Snackbar } from "@mui/material";
type SnackSetterFunction = (snack: {
open: boolean;
message: string;
type: "error" | "success" | "warning" | "info";
}) => void;
const Snacker = {} as any;
export const Message = {
error: (message: string) => {
Snacker.setSnack({
open: true,
message,
type: "error",
});
},
warning: (message: string) => {
Snacker.setSnack({
open: true,
message,
type: "warning",
});
},
success: (message: string) => {
Snacker.setSnack({
open: true,
message,
type: "success",
});
},
show: (message: string) => {
Snacker.setSnack({
open: true,
message,
type: "info",
});
},
};
export const GameUI = () => { export const GameUI = () => {
const [scene, setScene] = useState<GAME_SCENES>(GAME_SCENES.MENU); const [scene, setScene] = useState<GAME_SCENES>(GAME_SCENES.MENU);
const [prompts, setPrompts] = useState<string[]>([]); const [prompts, setPrompts] = useState<string[]>([]);
const [snack, setSnack] = useState({
open: false,
message: "",
type: "info" as "error" | "success" | "warning" | "info",
});
const handleClose = () => {
setSnack({
...snack,
open: false,
});
};
useEffect(() => {
Snacker.setSnack = setSnack;
}, []);
return ( return (
<div> <div>
{scene === GAME_SCENES.MENU && <MenuScene setScene={setScene} />} {scene === GAME_SCENES.MENU && <MenuScene setScene={setScene} />}
@ -16,6 +70,19 @@ export const GameUI = () => {
{scene === GAME_SCENES.GENERATE && ( {scene === GAME_SCENES.GENERATE && (
<GenerateScene setScene={setScene} prompts={prompts} /> <GenerateScene setScene={setScene} prompts={prompts} />
)} )}
<Snackbar
anchorOrigin={{ vertical: "top", horizontal: "center" }}
open={snack.open}
onClose={handleClose}
>
<Alert
onClose={handleClose}
severity={snack.type}
sx={{ width: "100%" }}
>
{snack.message}
</Alert>
</Snackbar>
</div> </div>
); );
}; };

@ -16,7 +16,7 @@ export const OpenAIService = async () => {
role: "user", role: "user",
content: `我想用NovelAI生成一张${keywords.join( content: `我想用NovelAI生成一张${keywords.join(
"," ","
)}20使`, )}20使`,
}, },
], ],
}); });

@ -25,4 +25,5 @@ export type SceneProps = {
export type PromptToken = { export type PromptToken = {
value: string; value: string;
class: "n" | "v" | "adj" | "adv";
}; };

Loading…
Cancel
Save