import CSMessage from "@/Common/Message/CSMessage";
import { INetResponse } from "@/Engine/CatanEngine/NetManagerV2/Core/INetResponse";
import { gameObj } from "@/context/GameItemsContext";
import { GameInfoRequest } from "@/define/Request/GameRequest";
import GameManager from "@/modules/GameManager";

export function* gameSync(): IterableIterator<any> {
	const { gameData, setGameData } = gameObj;
	if (GameManager.IsInGame) {
		// 不用同步資料，因為已經在遊戲中，所以不用同步資料。
		return;
	}
	let req: GameInfoRequest = new GameInfoRequest();
	yield req.SendAsync();
	let resp: INetResponse<JSON> = req.Result;
	/** 0是UUID 1[0]是機台相關資訊 1[1]排序 */
	if (resp.IsValid) {
		const respD: any = resp.Data;
	} else {
		CSMessage.NetError(resp.Method, resp.Status, "Get GameInfoRequest Error");
		return;
	}
	const data: any = JSON.parse(resp.Data[1]);
	const slotData = data[0];
	const slotList: number[] = data[1][0];
	setGameData({
		...gameData,
		slotData,
		slotList
	});
}