forked from roba269/wai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_match.cpp
126 lines (120 loc) · 4.23 KB
/
simple_match.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <time.h>
#include <signal.h>
#include "common_define.h"
#include "simple_match.h"
#include "sandbox.h"
#include "db_wrapper.h"
const int BUF_LEN = 1024;
char trans[BUF_LEN*BUF_LEN];
static std::string exit_flag_2_str(ExitFlagType exit_type) {
switch (exit_type) {
case EXIT_NORMAL:
return "The_opponent_exited_normally.";
case EXIT_RF:
return "The_opponent_called_restricted_function.";
case EXIT_TLE:
return "The_opponent_exceeded_time_limit.";
case EXIT_MLE:
return "The_opponent_exceeded_memory_limit.";
case EXIT_RE:
return "The_opponent_got_Runtime_Error.";
default:
return "The_opponent_exited_by_unknown_reason.";
}
}
static std::string winner_to_str(int winner) {
if (winner == 0)
return "Draw.";
else if (winner == 1)
return "Player_1_win.";
else
return "Player_2_win.";
}
void SimpleMatch::Start() {
signal(SIGPIPE, SIG_IGN);
m_start_time = time(NULL);
m_judge->Run(false); /* No syscall restriction for judge */
for (unsigned int i = 0 ; i < m_player.size() ; ++i) {
m_player[i]->Run();
}
char buf[BUF_LEN], tmp_buf[BUF_LEN];
trans[0] = 0;
while (1) {
fprintf(stderr, "waiting for the judge speaking\n");
memset(buf, 0, sizeof(buf));
ExitFlagType tmp;
if (m_judge->Recv(buf, BUF_LEN-1, tmp) == 0) {
fprintf(stderr, "The judge crashed.");
break;
}
fprintf(stderr, "The judge said: {%s}\n", buf);
if (buf[0] == '>') {
int dst = buf[1] - '1';
m_player[dst]->Send(buf+4);
} else if (buf[0] == '<') {
int src = buf[1] - '1';
fprintf(stderr, "The judge try to read from player %d\n", src);
memset(buf, 0, sizeof(buf));
ExitFlagType exit_type;
if (m_player[src]->Recv(buf, BUF_LEN-1, exit_type) == 0) {
fprintf(stderr, "Player %d exited, type: %d\n", src, (int)exit_type);
m_winner = (1 - src) + 1;
fprintf(stderr, "the winner is %d\n", m_winner);
fprintf(stdout, "%d %s %s\n", m_winner,
winner_to_str(m_winner).c_str(),
exit_flag_2_str(exit_type).c_str());
fflush(stdout);
break;
}
fprintf(stderr, "The judge recv from %d: {%s}\n", src, buf);
if (m_record.length()) m_record += ",";
snprintf(tmp_buf, BUF_LEN, "%d:%s", src, buf);
m_record += (std::string)tmp_buf;
m_judge->Send(buf);
} else if (buf[0] == '+') {
fprintf(stdout, "%s\n", buf);
fflush(stdout);
// strcat(trans, buf+1);
} else if (isdigit(buf[0])) {
char res_str[BUF_LEN], reason[BUF_LEN];
sscanf(buf, "%d %s %s", &m_winner, res_str, reason);
fprintf(stderr, "the winner is %d\n", m_winner);
fprintf(stdout, "%d %s %s\n", m_winner, res_str, reason);
fflush(stdout);
break;
} else {
assert(false);
}
}
fprintf(stderr, "match main process exit.");
m_end_time = time(NULL);
if (_WriteToDatabase()) {
fprintf(stderr, "write to database error\n");
}
}
int SimpleMatch::_WriteToDatabase() {
/*
char cmd[BUF_LEN];
snprintf(cmd, BUF_LEN, "INSERT INTO main_app_match (game_type, result, start_time, end_time, player_cnt) VALUES (\"%s\", %d, FROM_UNIXTIME(%d), FROM_UNIXTIME(%d), %d)", "RENJU", m_winner, m_start_time, m_end_time, m_player.size());
MYSQL *handle = DBWrapper::GetHandle();
fprintf(stderr, "%s\n", cmd);
mysql_query(handle, cmd);
int match_id = mysql_insert_id(handle);
for (int i = 0 ; i < m_sid.size() ; ++i) {
snprintf(cmd, BUF_LEN, "INSERT INTO main_app_match_players "
"(submit_id, match_id) VALUES (%d,%d)", m_sid[i], match_id);
fprintf(stderr, "%s\n", cmd);
mysql_query(handle, cmd);
}
snprintf(cmd, BUF_LEN, "%s/%d.txt", RECORD_PREFIX, match_id);
FILE *fp = fopen(cmd, "w");
fprintf(fp, "%s\n", m_record.c_str());
fclose(fp);
*/
return 0;
}