From 1a4c455f3400f9ae6bb6916af2aaf8f78a92424b Mon Sep 17 00:00:00 2001 From: BowmanChow Date: Thu, 30 Apr 2020 02:28:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=96=84=E4=BA=86=E9=99=90=E5=88=B6?= =?UTF-8?q?=E7=8E=A9=E5=AE=B6=E6=B6=88=E6=81=AF=E5=8F=91=E9=80=81=E7=9A=84?= =?UTF-8?q?=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CAPI/src/CAPI.cpp | 14 +- CAPI/src/player.cpp | 296 +++++++++++++++++---------------- communication/Agent/Program.cs | 21 ++- 3 files changed, 176 insertions(+), 155 deletions(-) diff --git a/CAPI/src/CAPI.cpp b/CAPI/src/CAPI.cpp index f9ac29b..63f70ca 100644 --- a/CAPI/src/CAPI.cpp +++ b/CAPI/src/CAPI.cpp @@ -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 mes=make_shared(-1, mes3); + shared_ptr mes = make_shared(-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); diff --git a/CAPI/src/player.cpp b/CAPI/src/player.cpp index 8bff409..1ea1bff 100644 --- a/CAPI/src/player.cpp +++ b/CAPI/src/player.cpp @@ -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 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::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 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::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); + /* 玩家在这里写代码 */ } \ No newline at end of file diff --git a/communication/Agent/Program.cs b/communication/Agent/Program.cs index f1f782e..0e69173 100644 --- a/communication/Agent/Program.cs +++ b/communication/Agent/Program.cs @@ -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) //倒计时 { @@ -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())); }); @@ -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