import game_store from '../store/game'
import {gameAddLine, gameEnd} from "../requests/game";

// 往后端推送一次定时数据
export function post_data($big_data) {
  wx.showLoading({
    title: "正在结束上一个游戏",
    mask: true
  })
  var $this = this
  //判断数据长度大于理想状态 需要对数组进行切割 逐个提交数据
  let $len = 300
  console.log($big_data.attList.length, '结束游戏提交的数据量')
  if ($big_data.attList.length > $len) {
    let $count = Math.ceil($big_data.attList.length / $len)

    let index = 0
    let inter = setInterval(() => {
      if (index < $count) {
        let $params = {
          game_record_id: game_store.getters.getGameRecordId(),
          //数据段长度大于65 多个提交
          line: $big_data.attList.slice(index * $len, (index + 1) * $len),
          line_med: $big_data.medList.slice(index * $len, (index + 1) * $len)
        }
        gameAddLine($params).then((res) => {
          let $data = res.data
          if ($data.code == 0) {
            console.log('ok', $params['line'])
          }
        })
      }
      index++
      if (index == $count) {
        clearInterval(inter)
        game_over()
      }
    }, 200)
    return $count * 500
  } else {
    let $params = {
      game_record_id: game_store.getters.getGameRecordId(),
      //数据段长度小于65 单个提交
      line: $big_data.attList,
      line_med: $big_data.medList
    }

    if (!$params.line) {
      return false
    }
    gameAddLine($params).then((res) => {
      let $data = res.data
      if ($data.code == 0) {
      }
      setTimeout(() => {
        game_over()
      }, 800)
    })
    return 800
  }
}

//  结束游戏
function game_over() {
  let $params = {
    game_record_id: game_store.getters.getGameRecordId(),
    type: game_store.getters.getMode(),
    play_time: game_store.getters.getPlayTime() - Math.abs(game_store.getters.getPlayedTime())
  }
  //
  gameEnd($params).then((res) => {
    let $data = res.data
    to_report()
  })
}


function to_report() {
  mpvue.navigateTo({
    url: "/pages/report/main?id=" + game_store.getters.getGameRecordId(),
    success() {
      wx.hideLoading()
      //设置游戏状态为 停止游戏
      game_store.setters.setGameStatus(0)
      //游戏结束重置游戏时间
      wx.removeStorageSync('play_time')
      //删除游戏专注数据
      game_store.remover.rmGameAttMedDatas()
      //游戏结束重置游戏模式
      game_store.setters.setMode(0)
      //删除游戏得id
      game_store.setters.removeToyHex()
      //删除隐藏时间
      game_store.remover.rmHideTime()
    }
  })
}








