xiongzhu 2 년 전
부모
커밋
fbfd6aa5d0
1개의 변경된 파일72개의 추가작업 그리고 37개의 파일을 삭제
  1. 72 37
      src/views/PlayView.vue

+ 72 - 37
src/views/PlayView.vue

@@ -1,52 +1,64 @@
 <template>
 <template>
-    <div class="h-full overflow-auto p-4 rounded-lg bg-white dark:bg-neutral-800">
-        <el-timeline>
-            <el-timeline-item
-                type="primary"
-                placement="top"
-                v-for="(item, index) in history"
-                :key="index"
-                :timestamp="formatDatetime(item.date, item.time)"
-            >
-                <ElCard>
-                    <span class="whitespace-pre-wrap" v-html="item.plot"></span>
-                    <template v-if="index === history.length - 1">
-                        <br />
-                    </template>
-                </ElCard>
-            </el-timeline-item>
-        </el-timeline>
-        <div class="text-center">
-            <ElButtonGroup v-if="game.status === 'PLAYING'">
-                <ElButton :loading="loading" @click="continueGame">继续</ElButton>
-                <ElButton :loading="loading" @click="continueGame(true)">生成选择</ElButton>
-                <ElButton :loading="loading" @click="revert">回退</ElButton>
-            </ElButtonGroup>
-            <ElButton v-else @click="start" :loading="loading">开始</ElButton>
-        </div>
+    <!-- <div class="h-full overflow-auto p-4 rounded-lg bg-white dark:bg-neutral-800"> -->
+    <el-timeline>
+        <el-timeline-item
+            type="primary"
+            placement="top"
+            v-for="(item, index) in history"
+            :key="index"
+            :timestamp="formatDatetime(item.date, item.time)"
+        >
+            <ElCard>
+                <span class="whitespace-pre-wrap" v-html="item.plot"></span>
+                <template v-if="index === history.length - 1 && item.choices.length">
+                    <br /><br />
+                    <el-radio-group v-model="choice" class="ml-4">
+                        <el-radio v-for="c in item.choices" :key="c" :label="c" class="w-full"> </el-radio>
+                    </el-radio-group>
+                </template>
+                <template v-if="index === history.length - 1">
+                    <br />
+                </template>
+            </ElCard>
+        </el-timeline-item>
+    </el-timeline>
+    <div class="text-center">
+        <ElButtonGroup v-if="game.status === 'PLAYING'">
+            <ElButton :loading="loading" @click="continueGame(false)">继续</ElButton>
+            <ElButton :loading="loading" @click="continueGame(true)">生成选择</ElButton>
+            <ElButton :loading="loading" @click="onAddCharactor">加入角色</ElButton>
+            <ElButton :loading="loading" @click="revert">回退</ElButton>
+        </ElButtonGroup>
+        <ElButton v-else @click="start" :loading="loading">开始</ElButton>
     </div>
     </div>
+
+    <!-- </div> -->
 </template>
 </template>
 <script setup>
 <script setup>
 import { http } from '@/plugins/http'
 import { http } from '@/plugins/http'
-import { ref } from 'vue'
+import { ref, computed } from 'vue'
 import { useRoute } from 'vue-router'
 import { useRoute } from 'vue-router'
 import { format } from 'date-fns'
 import { format } from 'date-fns'
 import { zhCN } from 'date-fns/locale'
 import { zhCN } from 'date-fns/locale'
-
+import { ElMessage, ElMessageBox } from 'element-plus'
 const route = useRoute()
 const route = useRoute()
 const game = ref({})
 const game = ref({})
 const history = ref([])
 const history = ref([])
+const choice = ref('')
+const needChoice = computed(() => {
+    return (history.value[history.value.length - 1].choices || []).length > 0
+})
 async function getData(params) {
 async function getData(params) {
     http.get(`/game/${route.params.id}`, { params }).then((res) => {
     http.get(`/game/${route.params.id}`, { params }).then((res) => {
         game.value = res
         game.value = res
-    })
-    http.get(`/game/${route.params.id}/history`, { params }).then((res) => {
-        history.value = res
-    })
+    }),
+        http.get(`/game/${route.params.id}/history`, { params }).then((res) => {
+            history.value = res
+        })
+    choice.value = ''
 }
 }
 getData()
 getData()
 function formatDatetime(date, time) {
 function formatDatetime(date, time) {
-    console.log(date, time)
     return (
     return (
         format(new Date(date), 'MMMdo', { locale: zhCN }) +
         format(new Date(date), 'MMMdo', { locale: zhCN }) +
         {
         {
@@ -59,23 +71,46 @@ function formatDatetime(date, time) {
 const loading = ref(false)
 const loading = ref(false)
 async function start() {
 async function start() {
     loading.value = true
     loading.value = true
-    await http.post(`/game/${route.params.id}/start`)
+    await http.post(`/game/${route.params.id}/start`, { date: new Date() })
     loading.value = false
     loading.value = false
     getData()
     getData()
 }
 }
+
 async function continueGame(genChoice = false) {
 async function continueGame(genChoice = false) {
+    if (needChoice.value && !choice.value) {
+        ElMessage.info('请选择一个选项')
+        return
+    }
     loading.value = true
     loading.value = true
-    await http.post(`/game/${route.params.id}/continue`, { genChoice: genChoice })
-    loading.value = false
-    getData()
+    try {
+        await http.post(`/game/${route.params.id}/continue`, { genChoice: genChoice, choice: choice.value })
+        loading.value = false
+        getData()
+    } catch (error) {
+        ElMessage.error(error.message)
+        loading.value = false
+    }
 }
 }
 
 
-const reverting = ref(false)
 async function revert() {
 async function revert() {
     loading.value = true
     loading.value = true
     await http.post(`/game/${route.params.id}/revert`)
     await http.post(`/game/${route.params.id}/revert`)
     loading.value = false
     loading.value = false
     getData()
     getData()
 }
 }
+
+async function onAddCharactor() {
+    const { value } = await ElMessageBox.prompt('基本信息:', '添加角色', {
+        inputPattern: /^.+$/,
+        inputErrorMessage: '请输入基本信息'
+    })
+    try {
+        await http.post(`/game/${route.params.id}/addCharactor`, {
+            base: value
+        })
+    } catch (error) {
+        ElMessage.error(error.message)
+    }
+}
 </script>
 </script>
 <style lang="less" scoped></style>
 <style lang="less" scoped></style>