add native method
This commit is contained in:
@@ -405,7 +405,8 @@ export default function Header() {
|
|||||||
value={connectionType}
|
value={connectionType}
|
||||||
onChange={(e) => setConnectionType(e.target.value)}
|
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>
|
<option value="Other">{transI18n("other")}</option>
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
@@ -464,12 +465,14 @@ export default function Header() {
|
|||||||
>
|
>
|
||||||
{transI18n("connect")}
|
{transI18n("connect")}
|
||||||
</button>
|
</button>
|
||||||
|
{connectionType !== "Native" && (
|
||||||
<button
|
<button
|
||||||
className={`btn btn-secondary`}
|
className={`btn btn-secondary`}
|
||||||
onClick={checkConnection}
|
onClick={checkConnection}
|
||||||
>
|
>
|
||||||
{transI18n("checkGameConnect")}
|
{transI18n("checkGameConnect")}
|
||||||
</button>
|
</button>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -13,6 +13,16 @@ const notify = (msg: string, type: 'info' | 'success' | 'error' = 'info') => {
|
|||||||
else toast.info(msg);
|
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 => {
|
export const connectSocket = (): Socket => {
|
||||||
const { host, port, connectionType, setStatus } = useSocketStore.getState();
|
const { host, port, connectionType, setStatus } = useSocketStore.getState();
|
||||||
const {
|
const {
|
||||||
@@ -30,7 +40,10 @@ export const connectSocket = (): Socket => {
|
|||||||
} = useBattleDataStore.getState();
|
} = useBattleDataStore.getState();
|
||||||
|
|
||||||
let url = `${host}:${port}`;
|
let url = `${host}:${port}`;
|
||||||
if (connectionType === "FireflyPSLocal") {
|
if (connectionType === "Native") {
|
||||||
|
url = "http://localhost:1305"
|
||||||
|
}
|
||||||
|
else if (connectionType === "PS") {
|
||||||
url = "http://localhost:21000"
|
url = "http://localhost:21000"
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,17 +93,60 @@ export const connectSocket = (): Socket => {
|
|||||||
|
|
||||||
if (isSocketConnected()) onConnect();
|
if (isSocketConnected()) onConnect();
|
||||||
|
|
||||||
socket.on("OnSetBattleLineup", (json) => onSetBattleLineupService(JSON.parse(json)));
|
socket.on("OnSetBattleLineup", (json) => {
|
||||||
socket.on("OnTurnEnd", (json) => onTurnEndService(JSON.parse(json)));
|
const data = safeParse(json);
|
||||||
socket.on("OnUseSkill", (json) => onUseSkillService(JSON.parse(json)));
|
if (data) onSetBattleLineupService(data);
|
||||||
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("OnTurnEnd", (json) => {
|
||||||
socket.on('OnTurnBegin', (json) => onTurnBeginService(JSON.parse(json)));
|
const data = safeParse(json);
|
||||||
socket.on('OnBattleEnd', (json) => onBattleEndService(JSON.parse(json)));
|
if (data) onTurnEndService(data);
|
||||||
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("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) => {
|
socket.on("Error", (msg: string) => {
|
||||||
console.error("Server Error:", msg);
|
console.error("Server Error:", msg);
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ const useSocketStore = create<SocketState>()(
|
|||||||
host: "http://localhost",
|
host: "http://localhost",
|
||||||
port: 3443,
|
port: 3443,
|
||||||
status: false,
|
status: false,
|
||||||
connectionType: "FireflyPSLocal",
|
connectionType: "Native",
|
||||||
setHost: (host: string) => set({ host }),
|
setHost: (host: string) => set({ host }),
|
||||||
setConnectionType: (connectionType: string) => set({ connectionType }),
|
setConnectionType: (connectionType: string) => set({ connectionType }),
|
||||||
setPort: (port: number) => set({ port }),
|
setPort: (port: number) => set({ port }),
|
||||||
|
|||||||
Reference in New Issue
Block a user