time: 时间由精确到毫秒改为精确到秒

以去掉 timeb, 保证 Android 之 NDK 编译通过。
This commit is contained in:
Calcitem Team 2019-08-28 01:06:02 +08:00 committed by Calcitem
parent c8cf3c2609
commit 7aaa330f1f
4 changed files with 42 additions and 39 deletions

View File

@ -203,7 +203,7 @@ void GameController::gameReset()
remainingTime1 = remainingTime2 = 0; remainingTime1 = remainingTime2 = 0;
} else { } else {
// 将玩家的剩余时间置为限定时间 // 将玩家的剩余时间置为限定时间
remainingTime1 = remainingTime2 = timeLimit * 60000; remainingTime1 = remainingTime2 = timeLimit * 60;
} }
// 更新棋谱 // 更新棋谱
@ -213,7 +213,7 @@ void GameController::gameReset()
currentRow = 0; currentRow = 0;
// 发出信号通知主窗口更新LCD显示 // 发出信号通知主窗口更新LCD显示
QTime qtime = QTime(0, 0, 0, 0).addMSecs(remainingTime1); QTime qtime = QTime(0, 0, 0, 0).addSecs(remainingTime1);
emit time1Changed(qtime.toString("hh:mm:ss")); emit time1Changed(qtime.toString("hh:mm:ss"));
emit time2Changed(qtime.toString("hh:mm:ss")); emit time2Changed(qtime.toString("hh:mm:ss"));
@ -528,12 +528,12 @@ void GameController::timerEvent(QTimerEvent *event)
// 如果规则要求计时则time1和time2表示倒计时 // 如果规则要求计时则time1和time2表示倒计时
if (timeLimit > 0) { if (timeLimit > 0) {
// 玩家的剩余时间 // 玩家的剩余时间
remainingTime1 = timeLimit * 60000 - remainingTime1; remainingTime1 = timeLimit * 60 - remainingTime1;
remainingTime2 = timeLimit * 60000 - remainingTime2; remainingTime2 = timeLimit * 60 - remainingTime2;
} }
qt1 = QTime(0, 0, 0, 0).addMSecs(remainingTime1); qt1 = QTime(0, 0, 0, 0).addSecs(remainingTime1);
qt2 = QTime(0, 0, 0, 0).addMSecs(remainingTime2); qt2 = QTime(0, 0, 0, 0).addSecs(remainingTime2);
emit time1Changed(qt1.toString("hh:mm:ss")); emit time1Changed(qt1.toString("hh:mm:ss"));
emit time2Changed(qt2.toString("hh:mm:ss")); emit time2Changed(qt2.toString("hh:mm:ss"));

View File

@ -239,11 +239,11 @@ private:
// 规则限步数 // 规则限步数
NineChess::step_t stepsLimit; NineChess::step_t stepsLimit;
// 玩家1剩余时间秒) // 玩家1剩余时间秒)
int remainingTime1; time_t remainingTime1;
// 玩家2剩余时间秒) // 玩家2剩余时间秒)
int remainingTime2; time_t remainingTime2;
// 用于主窗口状态栏显示的字符串 // 用于主窗口状态栏显示的字符串
QString message; QString message;

View File

@ -587,7 +587,7 @@ bool NineChess::start()
// 如果游戏处于未开始状态 // 如果游戏处于未开始状态
case GAME_NOTSTARTED: case GAME_NOTSTARTED:
// 启动计时器 // 启动计时器
ftime(&startTimeb); startTimeb = time(NULL);
// 进入开局状态 // 进入开局状态
context.stage = GAME_PLACING; context.stage = GAME_PLACING;
return true; return true;
@ -710,7 +710,7 @@ bool NineChess::place(int pos, int time_p, int8_t cp)
if (cp) { if (cp) {
player_ms = update(time_p); player_ms = update(time_p);
sprintf(cmdline, "(%1u,%1u) %02u:%02u.%03u", sprintf(cmdline, "(%1u,%1u) %02u:%02u.%03u",
c, p, player_ms / 60000, (player_ms % 60000) / 1000, player_ms % 1000); c, p, player_ms / 60, (player_ms % 60), 0);
cmdlist.emplace_back(string(cmdline)); cmdlist.emplace_back(string(cmdline));
currentStep++; currentStep++;
} }
@ -792,7 +792,7 @@ bool NineChess::place(int pos, int time_p, int8_t cp)
if (cp) { if (cp) {
player_ms = update(time_p); player_ms = update(time_p);
sprintf(cmdline, "(%1u,%1u)->(%1u,%1u) %02u:%02u.%03u", currentPos / N_SEATS, currentPos % N_SEATS + 1, sprintf(cmdline, "(%1u,%1u)->(%1u,%1u) %02u:%02u.%03u", currentPos / N_SEATS, currentPos % N_SEATS + 1,
c, p, player_ms / 60000, (player_ms % 60000) / 1000, player_ms % 1000); c, p, player_ms / 60, (player_ms % 60), 0);
cmdlist.emplace_back(string(cmdline)); cmdlist.emplace_back(string(cmdline));
currentStep++; currentStep++;
moveStep++; moveStep++;
@ -919,7 +919,7 @@ bool NineChess::capture(int pos, int time_p, int8_t cp)
if (cp) { if (cp) {
player_ms = update(time_p); player_ms = update(time_p);
sprintf(cmdline, "-(%1u,%1u) %02u:%02u.%03u", c, p, player_ms / 60000, (player_ms % 60000) / 1000, player_ms % 1000); sprintf(cmdline, "-(%1u,%1u) %02u:%02u.%03u", c, p, player_ms / 60, (player_ms % 60), 0);
cmdlist.emplace_back(string(cmdline)); cmdlist.emplace_back(string(cmdline));
currentStep++; currentStep++;
moveStep = 0; moveStep = 0;
@ -1094,7 +1094,7 @@ bool NineChess::command(const char *cmd)
if (args >= 4) { if (args >= 4) {
if (args == 7) { if (args == 7) {
if (mm >= 0 && ss >= 0 && mss >= 0) if (mm >= 0 && ss >= 0 && mss >= 0)
tm = mm * 60000 + ss * 1000 + mss; tm = mm * 60 + ss * mss;
} }
if (choose(c1, p1)) { if (choose(c1, p1)) {
@ -1109,7 +1109,7 @@ bool NineChess::command(const char *cmd)
if (args >= 2) { if (args >= 2) {
if (args == 5) { if (args == 5) {
if (mm >= 0 && ss >= 0 && mss >= 0) if (mm >= 0 && ss >= 0 && mss >= 0)
tm = mm * 60000 + ss * 1000 + mss; tm = mm * 60 + ss + mss;
} }
return _capture(c1, p1, tm); return _capture(c1, p1, tm);
} }
@ -1119,7 +1119,7 @@ bool NineChess::command(const char *cmd)
if (args >= 2) { if (args >= 2) {
if (args == 5) { if (args == 5) {
if (mm >= 0 && ss >= 0 && mss >= 0) if (mm >= 0 && ss >= 0 && mss >= 0)
tm = mm * 60000 + ss * 1000 + mss; tm = mm * 60 + ss + mss;
} }
return _place(c1, p1, tm); return _place(c1, p1, tm);
} }
@ -1178,8 +1178,8 @@ bool NineChess::command(int move)
inline int NineChess::update(int time_p /*= -1*/) inline int NineChess::update(int time_p /*= -1*/)
{ {
int ret = -1; int ret = -1;
int *player_ms = (context.turn == PLAYER1 ? &elapsedMS_1 : &elapsedMS_2); time_t *player_ms = (context.turn == PLAYER1 ? &elapsedMS_1 : &elapsedMS_2);
int playerNext_ms = (context.turn == PLAYER1 ? elapsedMS_2 : elapsedMS_1); time_t playerNext_ms = (context.turn == PLAYER1 ? elapsedMS_2 : elapsedMS_1);
// 根据局面调整计时器 // 根据局面调整计时器
@ -1187,24 +1187,27 @@ inline int NineChess::update(int time_p /*= -1*/)
return -1; return -1;
} }
ftime(&currentTimeb); #if 1
currentTimeb = time(NULL);
// 更新时间 // 更新时间
if (time_p >= *player_ms) { if (time_p >= *player_ms) {
*player_ms = ret = time_p; *player_ms = ret = time_p;
int t = elapsedMS_1 + elapsedMS_2; time_t t = elapsedMS_1 + elapsedMS_2;
startTimeb.time = currentTimeb.time - (t / 1000); startTimeb = currentTimeb - t;
#if 0
startTimeb.millitm = currentTimeb.millitm - (t % 1000); startTimeb.millitm = currentTimeb.millitm - (t % 1000);
if (t % 1000 > currentTimeb.millitm) { if (t % 1000 > currentTimeb.millitm) {
startTimeb.time--; startTimeb.time--;
startTimeb.millitm += 1000; startTimeb.millitm += 1000;
} }
#endif
} else { } else {
*player_ms = ret = static_cast<int>(currentTimeb.time - startTimeb.time) * 1000 *player_ms = ret = currentTimeb - startTimeb - playerNext_ms;
+ (currentTimeb.millitm - startTimeb.millitm) - playerNext_ms;
} }
#endif
// 有限时要求则判断胜负 // 有限时要求则判断胜负
if (currentRule.maxTimeLedToLose > 0) { if (currentRule.maxTimeLedToLose > 0) {
@ -1237,15 +1240,15 @@ bool NineChess::win(bool forceDraw)
// 这里不能update更新时间否则会形成循环嵌套 // 这里不能update更新时间否则会形成循环嵌套
// 如果玩家1超时 // 如果玩家1超时
if (elapsedMS_1 > currentRule.maxTimeLedToLose * 60000) { if (elapsedMS_1 > currentRule.maxTimeLedToLose * 60) {
elapsedMS_1 = currentRule.maxTimeLedToLose * 60000; elapsedMS_1 = currentRule.maxTimeLedToLose * 60;
winner = PLAYER2; winner = PLAYER2;
tips = "玩家1超时判负。"; tips = "玩家1超时判负。";
sprintf(cmdline, "Time over. Player2 win!"); sprintf(cmdline, "Time over. Player2 win!");
} }
// 如果玩家2超时 // 如果玩家2超时
else if (elapsedMS_2 > currentRule.maxTimeLedToLose * 60000) { else if (elapsedMS_2 > currentRule.maxTimeLedToLose * 60) {
elapsedMS_2 = currentRule.maxTimeLedToLose * 60000; elapsedMS_2 = currentRule.maxTimeLedToLose * 60;
winner = PLAYER1; winner = PLAYER1;
tips = "玩家2超时判负。"; tips = "玩家2超时判负。";
sprintf(cmdline, "Time over. Player1 win!"); sprintf(cmdline, "Time over. Player1 win!");
@ -1640,7 +1643,7 @@ enum NineChess::Player NineChess::getWhosPiece(int c, int p)
return NOBODY; return NOBODY;
} }
void NineChess::getElapsedTimeMS(int &p1_ms, int &p2_ms) void NineChess::getElapsedTimeMS(time_t &p1_ms, time_t &p2_ms)
{ {
update(); update();

View File

@ -22,7 +22,7 @@
#ifndef NINECHESS #ifndef NINECHESS
#define NINECHESS #define NINECHESS
#include <sys/timeb.h> //#include <sys/timeb.h>
#include <string> #include <string>
#include <cstring> #include <cstring>
#include <list> #include <list>
@ -385,7 +385,7 @@ public:
} }
// 玩家1和玩家2的用时 // 玩家1和玩家2的用时
void getElapsedTimeMS(int &p1_ms, int &p2_ms); void getElapsedTimeMS(time_t &p1_ms, time_t &p2_ms);
// 获取棋局的字符提示 // 获取棋局的字符提示
const string getTips() const const string getTips() const
@ -409,13 +409,13 @@ public:
} }
// 获取开局时间 // 获取开局时间
timeb getStartTimeb() const time_t getStartTimeb() const
{ {
return startTimeb; return startTimeb;
} }
// 重新设置开局时间 // 重新设置开局时间
void setStartTimeb(timeb stimeb) void setStartTimeb(int stimeb)
{ {
startTimeb = stimeb; startTimeb = stimeb;
} }
@ -561,16 +561,16 @@ private:
int moveStep {}; int moveStep {};
// 游戏起始时间 // 游戏起始时间
timeb startTimeb {}; time_t startTimeb {};
// 当前游戏时间 // 当前游戏时间
timeb currentTimeb {}; time_t currentTimeb {};
// 玩家1用时秒) // 玩家1用时秒)
int elapsedMS_1 {}; time_t elapsedMS_1 {};
// 玩家2用时秒) // 玩家2用时秒)
int elapsedMS_2 {}; time_t elapsedMS_2 {};
/* 当前着法AI会用到如下表示 /* 当前着法AI会用到如下表示
0x 00 00 0x 00 00