Permalink
Browse files

BaldBot: catch win situation, print bot and winner usernames.

  • Loading branch information...
Totktonada committed Apr 19, 2012
1 parent 642577c commit c94100a30e4a05b026c72e641dfade046b7202d6
Showing with 109 additions and 8 deletions.
  1. +81 −5 BaldBot.cpp
  2. +26 −3 ServerMsgLexer.cpp
  3. +2 −0 ServerMsgLexer.hpp
View
@@ -1,12 +1,72 @@
#include <stdio.h>
#include "Socket.hpp"
#include "ServerMsgLexer.hpp"
+#include "Exception.hpp"
#define MSG(str) (str), (sizeof(str) - 1)
#define MIN(n1, n2) (((n1) <= (n2)) ? (n1) : (n2))
#define BUFFER_SIZE 4096
+class BaldBotException : public Exception {
+ const char* description;
+ const char* winners;
+
+public:
+ BaldBotException(const char* aDescription,
+ const char* aWinners,
+ const char* aFile, int aLine)
+ : Exception(aFile, aLine),
+ description(aDescription),
+ winners(aWinners)
+ {}
+
+ BaldBotException(const char* aDescription,
+ const char* aFile, int aLine)
+ : Exception(aFile, aLine),
+ description(aDescription),
+ winners(0)
+ {}
+
+ const char* getDescription() const
+ {
+ return description;
+ }
+
+ bool isWinnersException() const
+ {
+ return (winners != 0);
+ }
+
+ const char* getWinners() const
+ {
+ return winners;
+ }
+
+ const char* toString() const
+ {
+ String msg("BaldBotExpection(\"");
+
+ msg += description;
+ msg += "\"); ";
+ msg += getFile();
+
+ char* lineStr = 0;
+ asprintf(&lineStr, ":%d", getLine());
+
+ msg += lineStr;
+
+ if (winners != 0) {
+ msg += "; ";
+ msg += winners;
+ }
+
+ msg += "\n";
+
+ return msg.getCharPtr();
+ }
+};
+
struct BotState {
char buffer[BUFFER_SIZE];
ServerMsgLexer lexer;
@@ -31,20 +91,25 @@ ServerMsg* expectMsg(BotState& state, TypeOfServerMsg type, bool skipFailed)
state.lexer.putNewData(state.buffer, state.readed);
} else {
// state.lexer.putEOF();
- printf("Got EOF, exiting...");
- throw 2;
+ throw BaldBotException("Got EOF.",
+ __FILE__, __LINE__);
}
msg = state.lexer.getMsg();
}
// msg->print(stdout);
+ if (msg->type == MSG_WINNERS_ASYNC) {
+ throw BaldBotException("Winners", msg->str,
+ __FILE__, __LINE__);
+ }
} while ((skipFailed && !msg->ok) || (msg->type != type));
if (msg->ok)
return msg;
- throw 1; /* TODO */
+ throw BaldBotException("Fail message with expected type.",
+ __FILE__, __LINE__);
}
int main(int argc, char** argv)
@@ -75,6 +140,10 @@ int main(int argc, char** argv)
ServerMsg *msg;
state.socket.connect();
+ state.socket.write(MSG("nick\r\n"));
+ msg = expectMsg(state, MSG_NICK_RESPONCE, false);
+ printf("%s", msg->str);
+
state.socket.write(MSG("join\r\n"));
msg = expectMsg(state, MSG_JOIN_RESPONCE, false);
msg = expectMsg(state, MSG_ROUNDS_ASYNC, true);
@@ -135,10 +204,17 @@ int main(int argc, char** argv)
state.socket.disconnect();
// lexer.putEOF();
+ } catch(BaldBotException& ex) {
+ if (ex.isWinnersException()) {
+ printf("%s", ex.getWinners());
+ return 0;
+ } else {
+ printf("%s", ex.toString());
+ return 1;
+ }
} catch(Exception& ex) {
printf("%s", ex.toString());
- } catch(int ex) {
- printf("Exception: %d\n", ex);
+ return 1;
}
return 0;
View
@@ -11,8 +11,8 @@ void ServerMsg::print(FILE *stream)
status->printValues(stream);
break;
case MSG_NICK_RESPONCE:
- fprintf(stream, "MSG_NICK_RESPONCE: %s\n",
- ok ? "ok" : "fail");
+ fprintf(stream, "MSG_NICK_RESPONCE: %s; %s\n",
+ ok ? "ok" : "fail", str);
break;
case MSG_BUILD_RESPONCE:
fprintf(stream, "MSG_BUILD_RESPONCE: %s\n",
@@ -45,6 +45,9 @@ void ServerMsg::print(FILE *stream)
fprintf(stream, "MSG_ROUNDS_ASYNC: %s\n",
ok ? "round started" : "unknown");
break;
+ case MSG_WINNERS_ASYNC:
+ fprintf(stream, "MSG_WINNERS_ASYNC: %s\n", str);
+ break;
case MSG_UNKNOWN:
fprintf(stream, "MSG_UNKNOWN\n");
break;
@@ -65,6 +68,7 @@ const char* ServerMsgLexer::typeOfServerMsgStr[] = {
"[Join]",
"[Month completed]",
"[Rounds]",
+ "[Winner(s)]",
"",
""
};
@@ -131,6 +135,7 @@ ServerMsg* ServerMsgLexer::stHeadStart()
requestNextChar = 1;
break;
case MSG_ROUNDS_ASYNC:
+ case MSG_WINNERS_ASYNC:
timestampSkipped = 0;
state = ST_ASYNC_MSG;
requestNextChar = 1;
@@ -211,14 +216,20 @@ ServerMsg* ServerMsgLexer::stOkFailResponce()
ServerMsg *msg = new ServerMsg;
msg->type = msgType;
/* msg->ok is undefined. */
+ /* msg->str is undefined. */
msg->status = 0;
+ int size = tmpBuffer.getLength() + 1; /* with '\0' */
+ const char* str = tmpBuffer.getCharPtr();
+
switch (msgType) {
case MSG_STATUS_RESPONCE:
/* Not possible. */
die(__LINE__);
case MSG_NICK_RESPONCE:
msg->ok = tmpBuffer.startsWith("Your username: ");
+ msg->str = new char[size];
+ memcpy(msg->str, str, size);
break;
case MSG_BUILD_RESPONCE:
case MSG_MAKE_RESPONCE:
@@ -240,6 +251,7 @@ ServerMsg* ServerMsgLexer::stOkFailResponce()
msg->ok = 1;
break;
case MSG_ROUNDS_ASYNC:
+ case MSG_WINNERS_ASYNC:
case MSG_UNKNOWN:
case MSG_LEXER_ERROR:
/* Not possible. */
@@ -263,8 +275,12 @@ ServerMsg* ServerMsgLexer::stAsyncMsg()
ServerMsg *msg = new ServerMsg;
msg->type = msgType;
/* msg->ok is undefined. */
+ /* msg->str is undefined. */
msg->status = 0;
+ int size = tmpBuffer.getLength() + 1; /* with '\0' */
+ const char* str = tmpBuffer.getCharPtr();
+
switch (msgType) {
case MSG_STATUS_RESPONCE:
case MSG_NICK_RESPONCE:
@@ -281,6 +297,11 @@ ServerMsg* ServerMsgLexer::stAsyncMsg()
msg->ok = tmpBuffer.isEqual("Game ready! "
"You are player of this game round.\n");
break;
+ case MSG_WINNERS_ASYNC:
+ msg->ok = 1;
+ msg->str = new char[size];
+ memcpy(msg->str, str, size);
+ break;
case MSG_UNKNOWN:
case MSG_LEXER_ERROR:
/* Not possible. */
@@ -307,6 +328,7 @@ ServerMsg* ServerMsgLexer::stStatusResponce()
ServerMsg *msg = new ServerMsg;
msg->type = MSG_STATUS_RESPONCE;
msg->ok = 0;
+ /* msg->str is undefined */
/* msg->status is undefined */
tmpValue = -1;
tmpStatus = 0;
@@ -321,6 +343,7 @@ ServerMsg* ServerMsgLexer::stStatusResponce()
ServerMsg *msg = new ServerMsg;
msg->type = MSG_STATUS_RESPONCE;
msg->ok = 1;
+ /* msg->str is undefined */
msg->status = tmpStatus;
tmpValue = -1;
tmpStatus = 0;
@@ -374,7 +397,7 @@ ServerMsg* ServerMsgLexer::stError()
{
ServerMsg *msg = new ServerMsg;
msg->type = MSG_LEXER_ERROR;
- /* ok and status is undefined */
+ /* ok, str and status is undefined */
state = ST_START;
return msg;
}
View
@@ -18,13 +18,15 @@ enum TypeOfServerMsg {
MSG_JOIN_RESPONCE,
MSG_MONTH_COMPLETED,
MSG_ROUNDS_ASYNC,
+ MSG_WINNERS_ASYNC,
MSG_UNKNOWN,
MSG_LEXER_ERROR
};
struct ServerMsg {
TypeOfServerMsg type;
int ok;
+ char *str;
MsgStatus* status;
#ifndef DAEMON

0 comments on commit c94100a

Please sign in to comment.