5 changed files with 191 additions and 14 deletions
@ -1,4 +1,24 @@ |
|||||
|
// 生成单一的动作构成数组
|
||||
|
const directions = (eventCode = 0) => { |
||||
|
const results = []; |
||||
|
for (let i = 0; i < 20; i++) { |
||||
|
results.push(eventCode) |
||||
|
} |
||||
|
return results; |
||||
|
} |
||||
|
|
||||
export const game = { |
export const game = { |
||||
|
frame: { |
||||
width: 1068, |
width: 1068, |
||||
height: 600 |
height: 600 |
||||
|
}, |
||||
|
count: 5, // 倒计时时长
|
||||
|
game: 60, // 游戏时长
|
||||
|
level: 0, |
||||
|
totalScore: 100, |
||||
|
totalTimes: 20, |
||||
|
scores: [ |
||||
|
{ direction: 0, score: 5 }, |
||||
|
], |
||||
|
directions: directions(), |
||||
} |
} |
||||
|
@ -0,0 +1,114 @@ |
|||||
|
import { game } from './config'; |
||||
|
const { count, game: gameDuration, level, scores, directions, totalScore, totalTimes } = game; |
||||
|
|
||||
|
let currentScore = 0; // 当前得分
|
||||
|
let currentTimes = 10; // 当前次数
|
||||
|
|
||||
|
// 生成开始游戏消息需要的data数据
|
||||
|
const createStartData = () => { |
||||
|
const now = Date.now(); |
||||
|
const countFinishTime = now + count * 1000; // 倒计时结束时间
|
||||
|
const gameFinishTime = countFinishTime + gameDuration * 1000; // 游戏结束时间
|
||||
|
const data = { |
||||
|
event: "start", |
||||
|
data: { |
||||
|
count: { |
||||
|
finishTime: countFinishTime, // 倒计时结束的ms数
|
||||
|
duration: count, // 倒计时时长
|
||||
|
}, |
||||
|
game: { |
||||
|
finishTime: gameFinishTime, // 游戏结束的ms数
|
||||
|
duration: gameDuration, // 游戏时长
|
||||
|
totalScore, // 总分
|
||||
|
totalTimes, // 总次数
|
||||
|
level, // 等级 0->入门 1->简单 2->普通 3->困难
|
||||
|
config: { |
||||
|
scores, |
||||
|
directions // 游戏的动作构成队列
|
||||
|
} |
||||
|
}, |
||||
|
status: 1, // 游戏状态 0 1 2
|
||||
|
param: {}, // 额外个性化参数
|
||||
|
} |
||||
|
} |
||||
|
return data; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 生成 暂停/继续 消息data数据 |
||||
|
* @param {number} code 0->暂停 1->继续 |
||||
|
* @returns {{}} |
||||
|
*/ |
||||
|
const createPauseData = (code) => { |
||||
|
return { |
||||
|
event: code === 0 ? 'pause' : 'continue', |
||||
|
data: { status: code ===0 ? 3 : 1 } |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 生成暂停/继续消息data数据 |
||||
|
* @param {number} score 总得分 客户端传来的 |
||||
|
* @param {number} times 总次数 客户端传来的 |
||||
|
* @returns {{data: {score, times, param: {}, status: number}, event: string}} |
||||
|
*/ |
||||
|
const createFinishData = (score=currentScore, times=currentTimes) => { |
||||
|
return { |
||||
|
event: 'finish', |
||||
|
data: { |
||||
|
score, // 得分
|
||||
|
times, // 次数
|
||||
|
status: 2, // 游戏状态 0 1 2
|
||||
|
param: {}, // 额外个性化参数
|
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 生成 play消息 发送的data数据 |
||||
|
* @param {number} score 当前得分 |
||||
|
* @param {number} times 当前次数 |
||||
|
* @param {number} direction 事件code 0默认 共6个事件参数 0 1 2 3 4 5 |
||||
|
* @returns {{data: {score: number, times: number, param: {direction: number}, status: number}, event: string}} |
||||
|
*/ |
||||
|
const createPlayData = ({ score=currentScore, times=currentTimes , direction=0 }) => { |
||||
|
return { |
||||
|
event: "play", |
||||
|
data: { |
||||
|
score, // 得分
|
||||
|
times, // 次数
|
||||
|
status: 1, // 游戏状态 0 1 2
|
||||
|
param: { |
||||
|
direction, // 0 1 2 3 4 5 共6中事件参数
|
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 生成提交的数据
|
||||
|
export const creatData = (eventType, ...args) => { |
||||
|
let data = null; |
||||
|
switch (eventType) { |
||||
|
case 'start': |
||||
|
data = createStartData() |
||||
|
break; |
||||
|
case 'pause': |
||||
|
data = createPauseData(0); |
||||
|
break; |
||||
|
case 'continue': |
||||
|
data = createPauseData(1); |
||||
|
break; |
||||
|
case 'finish': |
||||
|
data = createFinishData(); // TODO:
|
||||
|
break; |
||||
|
case 'play': |
||||
|
data = createPlayData({direction: +args[0]}); |
||||
|
break; |
||||
|
default: |
||||
|
break; |
||||
|
} |
||||
|
if (!data) { |
||||
|
alert('时间类型参数有误,消息数据为空'); |
||||
|
} |
||||
|
return data; |
||||
|
} |
Loading…
Reference in new issue