Skip to content
This repository has been archived by the owner on Apr 2, 2022. It is now read-only.

Commit

Permalink
改善了限制玩家消息发送的机制
Browse files Browse the repository at this point in the history
  • Loading branch information
junbao-zhou committed Apr 29, 2020
1 parent 0b80d3c commit 1a4c455
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 155 deletions.
14 changes: 12 additions & 2 deletions CAPI/src/CAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,17 +157,27 @@ void CAPI::SendChatMessage(string message)
mes1->set_message(message);
Message* mes2 = new Message(PlayerId, mes1);
Message* mes3 = new Message(-1, mes2);
shared_ptr<Message> mes=make_shared<Message>(-1, mes3);
shared_ptr<Message> mes = make_shared<Message>(-1, mes3);
Send(mes);
}

void CAPI::SendCommandMessage(MessageToServer message)
{
static const int timelimit = 45;
static long long deltaSendTime[] = { timelimit + 5,timelimit + 5 };
static long long lastSendTime = 0;

long long now = getSystemTime();
if (now < lastSendTime + 40)
//std::cout << "now : " << now << " deltaSend : " << deltaSendTime[0] << " , " << deltaSendTime[1] << std::endl;
long long deltaTime = now - lastSendTime;
if (((double)deltaTime + (double)deltaSendTime[0] + (double)deltaSendTime[1]) / 3.0 < timelimit)
{
std::cout << "skip sending" << std::endl;
return;
}
lastSendTime = now;
deltaSendTime[0] = deltaSendTime[1];
deltaSendTime[1] = deltaTime;
MessageToServer* mes0 = new MessageToServer(message);
Message* mes2 = new Message(PlayerId, mes0);
Message* mes3 = new Message(-1, mes2);
Expand Down
296 changes: 149 additions & 147 deletions CAPI/src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,153 +7,155 @@ using namespace THUAI3;
Protobuf::Talent initTalent = Protobuf::Talent::None;//指定人物天赋。选手代码必须定义此变量,否则报错
void play()
{
char c;
cin.clear();
cin.ignore();
cin >> c;
switch (c)
{
case 'd':
{
int moveDistance = 0;
std::cout << endl << "Please Input your move distance" << endl;
cin >> moveDistance;
cout << moveDistance / PlayerInfo.moveSpeed * 1000 << endl;
move(Protobuf::Direction::Right, moveDistance / PlayerInfo.moveSpeed * 1000);
}break;
case 'e':
{
int moveDistance = 0;
std::cout << endl << "Please Input your move distance" << endl;
cin >> moveDistance;
move(Protobuf::Direction::RightUp, moveDistance / PlayerInfo.moveSpeed * 1000);
}
break;
case 'w':
{
int moveDistance = 0;
std::cout << endl << "Please Input your move distance" << endl;
cin >> moveDistance;
move(Protobuf::Direction::Up, moveDistance / PlayerInfo.moveSpeed * 1000);
}break;
case 'q':
{
int moveDistance = 0;
std::cout << endl << "Please Input your move distance" << endl;
cin >> moveDistance;
move(Protobuf::Direction::LeftUp, moveDistance / PlayerInfo.moveSpeed * 1000);
}break;
case 'a':
{
int moveDistance = 0;
std::cout << endl << "Please Input your move distance" << endl;
cin >> moveDistance;
move(Protobuf::Direction::Left, moveDistance / PlayerInfo.moveSpeed * 1000);
}break;
case 'z':
{
int moveDistance = 0;
std::cout << endl << "Please Input your move distance" << endl;
cin >> moveDistance;
move(Protobuf::Direction::LeftDown, moveDistance / PlayerInfo.moveSpeed * 1000);
}break;
case 'x':
{
int moveDistance = 0;
std::cout << endl << "Please Input your move distance" << endl;
cin >> moveDistance;
move(Protobuf::Direction::Down, moveDistance / PlayerInfo.moveSpeed * 1000);
}break;
case 'c':
{
int moveDistance = 0;
std::cout << endl << "Please Input your move distance" << endl;
cin >> moveDistance;
move(Protobuf::Direction::RightDown, moveDistance / PlayerInfo.moveSpeed * 1000);
}break;
case 'f':
{
std::cout << endl << "Please Input 3 parameters : isSelfPosition, pickType, dishOrToolType" << endl;
bool isSelfPosition = 0;
cin >> isSelfPosition;
int pickType = 0;
cin >> pickType;
int dishOrToolType = 0;
cin >> dishOrToolType;
pick(isSelfPosition, (ObjType)pickType, dishOrToolType);
}
break;
case 'u':
{
std::cout << endl << "Please Input 2 parameters : " << endl;
double param1 = 0;
cin >> param1;
double param2 = 0;
cin >> param2;
use(1, param1, param2);
}
break;
case 'i': use(0); break;
case 'r':
{
std::cout << endl << "Please Input 2 parameters : " << endl;
double distance = 0;
cin >> distance;
double angle = 0;
cin >> angle;
put(distance, angle, true);
_sleep(26);
move(Left, 1000);
}
break;
case 't':
{
std::cout << endl << "Please Input 2 parameters : " << endl;
double distance = 0;
cin >> distance;
double angle = 0;
cin >> angle;
put(distance, angle, false);
}
break;
case ':':
{
std::cout << endl << "Please Input your text to speak : " << endl;
string str;
cin >> str;
speakToFriend(str);
}
break;
case 'm':
{
std::cout << endl << "Input two interger to print a map cell :" << endl;
int x, y;
cin.clear();
cin.ignore();
cin >> x >> y;
list<Obj> l = MapInfo::get_mapcell(PlayerInfo.position.x, PlayerInfo.position.y);
if (l.empty()) cout << "empty" << endl;
std::cout << "objs in map[" << PlayerInfo.position.x << "][" << PlayerInfo.position.y << "] :" << endl;
for (list<Obj>::iterator i = l.begin(); i != l.end(); i++)
{
std::cout << "\tblocktype : " << i->objType << endl;
}
}
break;
case 's':
{
std::cout << endl << "Self info :" << endl;
std::cout << "\tposition : " << PlayerInfo.position.x << "," << PlayerInfo.position.y << endl;
std::cout << "\tdish : " << PlayerInfo.dish << endl;
std::cout << "\ttool : " << PlayerInfo.tool << endl;
std::cout << "\trecieveText : " << PlayerInfo.recieveText << endl;
}
//char c;
//cin.clear();
//cin.ignore();
//cin >> c;
//switch (c)
//{
//case 'd':
//{
// int moveDistance = 0;
// std::cout << endl << "Please Input your move distance" << endl;
// cin >> moveDistance;
// cout << moveDistance / PlayerInfo.moveSpeed * 1000 << endl;
// move(Protobuf::Direction::Right, moveDistance / PlayerInfo.moveSpeed * 1000);
//}break;
//case 'e':
//{
// int moveDistance = 0;
// std::cout << endl << "Please Input your move distance" << endl;
// cin >> moveDistance;
// move(Protobuf::Direction::RightUp, moveDistance / PlayerInfo.moveSpeed * 1000);
//}
//break;
//case 'w':
//{
// int moveDistance = 0;
// std::cout << endl << "Please Input your move distance" << endl;
// cin >> moveDistance;
// move(Protobuf::Direction::Up, moveDistance / PlayerInfo.moveSpeed * 1000);
//}break;
//case 'q':
//{
// int moveDistance = 0;
// std::cout << endl << "Please Input your move distance" << endl;
// cin >> moveDistance;
// move(Protobuf::Direction::LeftUp, moveDistance / PlayerInfo.moveSpeed * 1000);
//}break;
//case 'a':
//{
// int moveDistance = 0;
// std::cout << endl << "Please Input your move distance" << endl;
// cin >> moveDistance;
// move(Protobuf::Direction::Left, moveDistance / PlayerInfo.moveSpeed * 1000);
//}break;
//case 'z':
//{
// int moveDistance = 0;
// std::cout << endl << "Please Input your move distance" << endl;
// cin >> moveDistance;
// move(Protobuf::Direction::LeftDown, moveDistance / PlayerInfo.moveSpeed * 1000);
//}break;
//case 'x':
//{
// int moveDistance = 0;
// std::cout << endl << "Please Input your move distance" << endl;
// cin >> moveDistance;
// move(Protobuf::Direction::Down, moveDistance / PlayerInfo.moveSpeed * 1000);
//}break;
//case 'c':
//{
// int moveDistance = 0;
// std::cout << endl << "Please Input your move distance" << endl;
// cin >> moveDistance;
// move(Protobuf::Direction::RightDown, moveDistance / PlayerInfo.moveSpeed * 1000);
//}break;
//case 'f':
//{
// std::cout << endl << "Please Input 3 parameters : isSelfPosition, pickType, dishOrToolType" << endl;
// bool isSelfPosition = 0;
// cin >> isSelfPosition;
// int pickType = 0;
// cin >> pickType;
// int dishOrToolType = 0;
// cin >> dishOrToolType;
// pick(isSelfPosition, (ObjType)pickType, dishOrToolType);
//}
//break;
//case 'u':
//{
// std::cout << endl << "Please Input 2 parameters : " << endl;
// double param1 = 0;
// cin >> param1;
// double param2 = 0;
// cin >> param2;
// use(1, param1, param2);
//}
//break;
//case 'i': use(0); break;
//case 'r':
//{
// std::cout << endl << "Please Input 2 parameters : " << endl;
// double distance = 0;
// cin >> distance;
// double angle = 0;
// cin >> angle;
// put(distance, angle, true);
// _sleep(26);
// move(Left, 1000);
//}
//break;
//case 't':
//{
// std::cout << endl << "Please Input 2 parameters : " << endl;
// double distance = 0;
// cin >> distance;
// double angle = 0;
// cin >> angle;
// put(distance, angle, false);
//}
//break;
//case ':':
//{
// std::cout << endl << "Please Input your text to speak : " << endl;
// string str;
// cin >> str;
// speakToFriend(str);
//}
//break;
//case 'm':
//{
// std::cout << endl << "Input two interger to print a map cell :" << endl;
// int x, y;
// cin.clear();
// cin.ignore();
// cin >> x >> y;
// list<Obj> l = MapInfo::get_mapcell(PlayerInfo.position.x, PlayerInfo.position.y);
// if (l.empty()) cout << "empty" << endl;
// std::cout << "objs in map[" << PlayerInfo.position.x << "][" << PlayerInfo.position.y << "] :" << endl;
// for (list<Obj>::iterator i = l.begin(); i != l.end(); i++)
// {
// std::cout << "\tblocktype : " << i->objType << endl;
// }
//}
//break;
//case 's':
//{
// std::cout << endl << "Self info :" << endl;
// std::cout << "\tposition : " << PlayerInfo.position.x << "," << PlayerInfo.position.y << endl;
// std::cout << "\tdish : " << PlayerInfo.dish << endl;
// std::cout << "\ttool : " << PlayerInfo.tool << endl;
// std::cout << "\trecieveText : " << PlayerInfo.recieveText << endl;
//}

default:
break;
}
//default:
// break;
//}


std::cout << "Game Time : " << THUAI3::getGameTime() << endl;
/* 玩家在这里写代码 */
static Direction direc = (Direction)0;
//std::cout << "Game Time : " << THUAI3::getGameTime() << endl;
move(direc, 0);
direc = (Direction)((direc + 1) % 8);
/* 玩家在这里写代码 */
}
21 changes: 15 additions & 6 deletions communication/Agent/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class Program
private static System.Timers.Timer myTimer = new System.Timers.Timer();
private static IPEndPoint Server;
private static object[] LastSpam;// = Constants.MaxMessage;
private static int[][] deltaSendTime;
/*
private static void TimeCount(object source, System.Timers.ElapsedEventArgs e) //倒计时
{
Expand All @@ -37,8 +38,13 @@ public static void Main(string[] args)
if (Constants.PlayerCount < 1) Constants.PlayerCount = 1;
else if (Constants.PlayerCount > 2) Constants.PlayerCount = 2;
LastSpam = new object[Constants.PlayerCount];
deltaSendTime = new int[Constants.PlayerCount][];
//Constants.MaxMessage = int.Parse(messagelmt.Value());
Constants.TimeLimit = double.Parse(timelmt.Value());
Constants.TimeLimit = double.Parse(timelmt.Value());
for (int i = 0; i < Constants.PlayerCount; i++)
{
deltaSendTime[i] = new int[] { (int)Constants.TimeLimit + 5, (int)Constants.TimeLimit + 5 };
}
if (Constants.TimeLimit < 10) Constants.TimeLimit = 10;
return MainInternal(server.Value(), ushort.Parse(port.Value()), token.Value(), int.Parse(debugLevel.Value()));
});
Expand Down Expand Up @@ -76,13 +82,16 @@ private static int MainInternal(string ep, ushort port, string token, int debugL
var now = Environment.TickCount;
lock (LastSpam)
{
if (now <= (int)LastSpam[0] + Constants.TimeLimit) return;
for (int i = 1; i < Constants.PlayerCount; i++)
//Console.WriteLine(message.Address + " : " + now + " deltaSendTime : " + deltaSendTime[message.Address][0] + ", " + deltaSendTime[message.Address][1]);
int deltaTime = now - (int)LastSpam[message.Address];
if (((double)deltaTime + (double)deltaSendTime[message.Address][0] + (double)deltaSendTime[message.Address][1]) / 3.0 <= Constants.TimeLimit)
{
LastSpam[i - 1] = LastSpam[i];
Console.WriteLine("skip client's message");
return;
}
LastSpam[Constants.PlayerCount - 1] = now;
Console.WriteLine(now);
LastSpam[message.Address] = now;
deltaSendTime[message.Address][0] = deltaSendTime[message.Address][1];
deltaSendTime[message.Address][1] = deltaTime;
}
}
else
Expand Down

0 comments on commit 1a4c455

Please sign in to comment.