Commit ebca1c4b authored by Tt's avatar Tt

1

parent e4707ca5
This diff is collapsed.
This diff is collapsed.
...@@ -135,35 +135,35 @@ export default class Game { ...@@ -135,35 +135,35 @@ export default class Game {
if (!Game.ins) Game.ins = new Game(); if (!Game.ins) Game.ins = new Game();
return Game.ins; return Game.ins;
} }
/** 游戏数据 */ /** 游戏数据源数组 */
private data: any; private data: Array<any>;
/** 选项列表 */
private lists: Array<Option>
/** 玩家实例 */ /** 玩家实例 */
public player: Player; public player: Player;
/** 游戏状态 */ /** 游戏状态 */
public state: GAME_STATE; public state: GAME_STATE;
/** 问题总数 */ /** 数据总数 */
public total: number; public total: number;
/** 每页数据数量 */
public pageSize: number;
/** /**
* 构造函数,初始化游戏基本属性 * 构造函数,初始化游戏基本属性
*/ */
constructor() { constructor() {
this.start = false; this.start = false;
this.lists = []; this.data = [];
this.page = 0;
this.pageSize = 6;
this.player = new Player(); this.player = new Player();
this.state = GAME_STATE.WAIT; this.state = GAME_STATE.WAIT;
} }
/** /**
* 获取当前选项列表长度 * 获取当前数据源长度
* @returns 选项列表长度 * @returns 数据源长度
*/ */
get len() { get len() {
return this.lists.length; return this.data ? this.data.length : 0;
} }
/** 是否为单人游戏模式 */
public singleGame: boolean;
/** 问题信息,包含文本和音频 */ /** 问题信息,包含文本和音频 */
public question: { text, audio }; public question: { text, audio };
/** 游戏标题 */ /** 游戏标题 */
...@@ -177,14 +177,15 @@ export default class Game { ...@@ -177,14 +177,15 @@ export default class Game {
* @param data 游戏配置数据 * @param data 游戏配置数据
*/ */
public init(data) { public init(data) {
this.singleGame = !data.onlineFlg; this.pageSize = 6; // 每页最多6个数据
this.question = { text: data.questionText, audio: data.questionTextAudio }; this.question = { text: data.questionText, audio: data.questionTextAudio };
this.title = data.title; this.title = data.title;
this.bgAudio = data.bgAudio || ""; this.bgAudio = data.bgAudio || "";
this.questionText = data.questionText; this.questionText = data.questionText;
this.start = false; this.start = false;
this.lists = []; this.page = 0; // 页码从0开始
this.data = data.questions; this.data = data.questions;
this.total = this.data.length;
} }
/** 游戏是否已开始 */ /** 游戏是否已开始 */
public start: boolean; public start: boolean;
...@@ -192,68 +193,91 @@ export default class Game { ...@@ -192,68 +193,91 @@ export default class Game {
public page: number; public page: number;
/** /**
* 重置游戏状态 * 重置游戏状态
* 重置玩家数据,初始化问题列表,准备开始新游戏 * 重置玩家数据,初始化游戏状态,准备开始新游戏
*/ */
reset() { reset() {
this.player.reset(); this.player.reset();
this.page = 1; this.page = 0; // 页码从0开始
this.start = true; this.start = true;
this.lists = [];
for (let i = 0; i < this.data.length; i++) {
let data = this.data[i];
this.lists.push(new Option(data, i));
}
this.state = GAME_STATE.WAIT; this.state = GAME_STATE.WAIT;
this.total = this.lists.length; this.total = this.data.length;
} }
/** /**
* 获取随机卡片信息 * 获取当前页的数据
* 从列表中随机抽取一个选项并从列表中移除 * @param {number} pageIndex 页码索引,默认为当前页码(页码从0开始)
* @returns 随机选项,如果列表为空则返回null * @returns {Array<Option>} 当前页的选项数据数组
*/ */
public getCardInfo() { getPageData(pageIndex?: number) {
let option: Option; // 如果没有指定页码索引,则使用当前页码(页码从0开始)
if (this.lists.length > 0) { const index = pageIndex !== undefined ? pageIndex : this.page;
let random = Math.floor(Math.random() * this.lists.length);
option = this.lists.splice(random, 1)[0]; // 计算当前页的起始索引和结束索引
} else { const startIndex = index * this.pageSize;
option = null; const endIndex = Math.min(startIndex + this.pageSize, this.data.length);
// 如果起始索引超出数据范围,返回空数组
if (startIndex >= this.data.length) {
return [];
} }
console.log(" this.cardInfo=======", option)
return option; // 提取当前页的数据并转换为Option对象
const pageData: Array<Option> = [];
for (let i = startIndex; i < endIndex; i++) {
pageData.push(new Option(this.data[i], i));
} }
return pageData;
}
/** /**
* 获取总页数/剩余问题数 * 获取总页数
* @returns 剩余问题数量 * @returns 总页数(向上取整)
*/ */
getTotalPageNum() { getTotalPageNum() {
return this.lists.length; return Math.ceil(this.data.length / this.pageSize);
} }
/** /**
* 获取当前页码 * 获取当前页码
* @returns 当前页码 * @returns 当前页码(从0开始)
*/ */
getCurrentPageNum() { getCurrentPageNum() {
return this.page; return this.page;
} }
/** /**
* 页码加1,用于游戏进度更新 * 页码加1,用于游戏进度更新
*/ */
addPage() { addPage() {
this.page += 1; this.page += 1;
} }
/**
* 获取数据总数
* @returns 数据总数
*/
get getTotal() {
return this.data.length;
}
/** /**
* 获取剩余总数 * 获取当前页的卡片信息
* @returns 剩余问题数量 * @returns 当前页的选项数据数组
*/ */
get getTotla() { getCardInfo() {
return this.lists.length; const currentPageData = this.getPageData();
if (currentPageData.length === 0) {
return null;
} }
return currentPageData[0]; // 返回当前页的第一个选项
}
/** /**
* 判断游戏是否结束 * 判断游戏是否结束
* @returns 当前页码是否超过问题总 * @returns 当前页码是否超过或等于总页
*/ */
get isOver() { get isOver() {
return this.page > this.lists.length; return this.page >= this.getTotalPageNum();
} }
} }
\ No newline at end of file
...@@ -295,7 +295,6 @@ ...@@ -295,7 +295,6 @@
| `player` | `Player` | 玩家实例 | | `player` | `Player` | 玩家实例 |
| `state` | `GAME_STATE` | 游戏状态 | | `state` | `GAME_STATE` | 游戏状态 |
| `total` | `number` | 问题总数 | | `total` | `number` | 问题总数 |
| `singleGame` | `boolean` | 是否为单人游戏模式 |
| `question` | `{text, audio}` | 问题信息 | | `question` | `{text, audio}` | 问题信息 |
| `title` | `string` | 游戏标题 | | `title` | `string` | 游戏标题 |
| `bgAudio` | `string` | 背景音乐URL | | `bgAudio` | `string` | 背景音乐URL |
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment