You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
2.4 KiB

#include "RankList.h"
#include <ctime>
#include <algorithm>
#include <sstream>
#include <fstream>
#include <string>
#include<time.h>
RankList::RankList()
{
if (!this->m_msg.empty())
{
this->m_msg.clear();
}
ReadFile();
}
void RankList::SaveMsg(PlayerMsg msg)
{
WriteTime(msg); //д<><D0B4>ʱ<EFBFBD><CAB1>
m_msg.push_back(msg);
std::sort(m_msg.begin(), m_msg.end(), SortPlayerMsg());
if (m_msg.size() > this->MAX_RANK)
{
m_msg.pop_back();
}
std::vector<PlayerMsg>::iterator it = m_msg.begin();
//<2F>޸<EFBFBD>id
for (int i = 0; i < m_msg.size(); i++, it++)
{
it->id = i + 1;
}
}
std::vector<PlayerMsg> RankList::getRankList()
{
return this->m_msg;
}
void RankList::SaveToRank()
{
WriteFile();
}
void RankList::WriteTime(PlayerMsg& msg)
{
std::time_t now;
std::tm ltm;
// <20><>ȡ<EFBFBD><C8A1>ǰʱ<C7B0><CAB1>
std::time(&now);
// ʹ<><CAB9>localtime_s<5F><73>ȫ<EFBFBD>ؽ<EFBFBD><D8BD><EFBFBD>ǰʱ<C7B0><CAB1>ת<EFBFBD><D7AA>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
localtime_s(&ltm, &now);
std::stringstream sstream;
// <20><><EFBFBD><EFBFBD> tm <20><EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɲ<EFBFBD><C9B2><EFBFBD>
sstream << ltm.tm_year + 1900 << "-"; // <20><>
sstream << ((ltm.tm_mon + 1 >= 10) ? "" : "0") << ltm.tm_mon + 1 << "-"; // <20><>
sstream << ((ltm.tm_mday >= 10) ? "" : "0") << ltm.tm_mday << "_"; // <20><>
sstream << ((ltm.tm_hour >= 10) ? "" : "0") << ltm.tm_hour << ":"; // ʱ
sstream << ((ltm.tm_min >= 10) ? "" : "0") << ltm.tm_min; // <20><>
msg.r_time = sstream.str();
}
void RankList::ReadFile()
{
std::ifstream infile;
infile.open(m_rankfile, std::ios::in | std::ios::binary);
if (!infile)
{
//<2F><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڣ<EFBFBD><DAA3>򴴽<EFBFBD>
std::ofstream os;
os.open(m_rankfile); //Ĭ<>ϻᴴ<CFBB><E1B4B4>
if (!os)
{
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD>ֻ<EFBFBD>ܽ<EFBFBD><DCBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
exit(0);
}
os.close();
}
else
{
std::string line;
std::stringstream stream;
int index = 0;
PlayerMsg msg;
while (std::getline(infile, line))
{
stream.clear();
stream.str(line);
stream >> msg.id >> msg.score >> msg.len >> msg.r_time;
m_msg.push_back(msg);
index++;
}
std::sort(m_msg.begin(), m_msg.end(), SortPlayerMsg());
}
infile.close();
}
void RankList::WriteFile()
{
std::ofstream outfile;
outfile.open(m_rankfile, std::ios::out | std::ios::binary); //ÿ<><C3BF>д<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>дһ<D0B4><D2BB>
if (!outfile)
{
return;
}
for (int i = 0; i < m_msg.size(); ++i)
{
outfile << m_msg[i].id << " " << m_msg[i].score << " " << m_msg[i].len
<< " " << m_msg[i].r_time << std::endl;
}
outfile.close();
}