add native method

This commit is contained in:
2025-05-14 08:53:58 +07:00
parent 12a8c74c9b
commit 9dceba7fba
3 changed files with 81 additions and 22 deletions

View File

@@ -405,7 +405,8 @@ export default function Header() {
value={connectionType}
onChange={(e) => setConnectionType(e.target.value)}
>
<option value="FireflyPSLocal">Firefly PS Local</option>
<option value="Native">Native</option>
<option value="PS">PS</option>
<option value="Other">{transI18n("other")}</option>
</select>
</div>
@@ -464,12 +465,14 @@ export default function Header() {
>
{transI18n("connect")}
</button>
{connectionType !== "Native" && (
<button
className={`btn btn-secondary`}
onClick={checkConnection}
>
{transI18n("checkGameConnect")}
</button>
)}
</div>
</div>
</div>

View File

@@ -13,6 +13,16 @@ const notify = (msg: string, type: 'info' | 'success' | 'error' = 'info') => {
else toast.info(msg);
};
function safeParse(json: unknown | string) {
try {
return typeof json === "string" ? JSON.parse(json) : json;
} catch (e) {
console.error("JSON parse error:", e, json);
return null;
}
}
export const connectSocket = (): Socket => {
const { host, port, connectionType, setStatus } = useSocketStore.getState();
const {
@@ -30,7 +40,10 @@ export const connectSocket = (): Socket => {
} = useBattleDataStore.getState();
let url = `${host}:${port}`;
if (connectionType === "FireflyPSLocal") {
if (connectionType === "Native") {
url = "http://localhost:1305"
}
else if (connectionType === "PS") {
url = "http://localhost:21000"
}
@@ -80,17 +93,60 @@ export const connectSocket = (): Socket => {
if (isSocketConnected()) onConnect();
socket.on("OnSetBattleLineup", (json) => onSetBattleLineupService(JSON.parse(json)));
socket.on("OnTurnEnd", (json) => onTurnEndService(JSON.parse(json)));
socket.on("OnUseSkill", (json) => onUseSkillService(JSON.parse(json)));
socket.on("OnKill", (json) => onKillService(JSON.parse(json)));
socket.on("OnDamage", (json) => onDamageService(JSON.parse(json)));
socket.on('OnBattleBegin', (json) => onBattleBegin(JSON.parse(json)));
socket.on('OnTurnBegin', (json) => onTurnBeginService(JSON.parse(json)));
socket.on('OnBattleEnd', (json) => onBattleEndService(JSON.parse(json)));
socket.on('OnUpdateCycle', (json) => onUpdateCycleService(JSON.parse(json)));
socket.on('OnUpdateWave', (json) => OnUpdateWaveService(JSON.parse(json)));
socket.on('OnCreateBattle', (json) => onCreateBattleService(JSON.parse(json)));
socket.on("OnSetBattleLineup", (json) => {
const data = safeParse(json);
if (data) onSetBattleLineupService(data);
});
socket.on("OnTurnEnd", (json) => {
const data = safeParse(json);
if (data) onTurnEndService(data);
});
socket.on("OnUseSkill", (json) => {
const data = safeParse(json);
if (data) onUseSkillService(data);
});
socket.on("OnKill", (json) => {
const data = safeParse(json);
if (data) onKillService(data);
});
socket.on("OnDamage", (json) => {
const data = safeParse(json);
if (data) onDamageService(data);
});
socket.on("OnBattleBegin", (json) => {
const data = safeParse(json);
if (data) onBattleBegin(data);
});
socket.on("OnTurnBegin", (json) => {
const data = safeParse(json);
if (data) onTurnBeginService(data);
});
socket.on("OnBattleEnd", (json) => {
const data = safeParse(json);
if (data) onBattleEndService(data);
});
socket.on("OnUpdateCycle", (json) => {
const data = safeParse(json);
if (data) onUpdateCycleService(data);
});
socket.on("OnUpdateWave", (json) => {
const data = safeParse(json);
if (data) OnUpdateWaveService(data);
});
socket.on("OnCreateBattle", (json) => {
const data = safeParse(json);
if (data) onCreateBattleService(data);
});
socket.on("Error", (msg: string) => {
console.error("Server Error:", msg);

View File

@@ -18,7 +18,7 @@ const useSocketStore = create<SocketState>()(
host: "http://localhost",
port: 3443,
status: false,
connectionType: "FireflyPSLocal",
connectionType: "Native",
setHost: (host: string) => set({ host }),
setConnectionType: (connectionType: string) => set({ connectionType }),
setPort: (port: number) => set({ port }),