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