
import { NetworkHelper } from "./NetworkHelper_PK_Game";
import { asyncDelay } from "./util_PK_Game";

const ansPro = [0.1, 0.2, 0.3, 0.4, 0.7, 0.8, 0.9]
export class AI {
  networkHelper: NetworkHelper;
  playerData: any;


  anspronum: number;

  constructor(networkHelper: NetworkHelper, playerData: any) {
    this.networkHelper = networkHelper;
    this.playerData = playerData;
    this.anspronum = ansPro[this.playerData.lv]
  }

  async onFrameEvent(data) {
    console.log("AI get event: " + JSON.stringify(data));

    if (data.type == "SERVER_allPlayerInfo") {
      this.networkHelper.sendFrame({
        type: "gamePrepare",
        uuid: this.playerData.uuid,
      });

    }
    if (data.type == "SERVER_gameEv") {

      cc.log("ai on msg SERVER_gameEv")

      if (!data.question) {

        return;
        cc.log(data);
      }
      let rigthIndex = data.question.rightIndex;

      let time = 2 + Math.floor(Math.random() * 4)

      await asyncDelay(time);

      let index = 0;
      if (Math.random() <= this.anspronum) {
        index = rigthIndex;
      } else {
        index = Math.floor(Math.random() * 3.99);
        while (index == rigthIndex) {
          index = Math.floor(Math.random() * 3.99);
        }

      }

      this.networkHelper.sendFrame({
        type: "anserQues",
        uuid: this.playerData.uuid,
        index: index
      });




    }
  }

}
