forked from roba269/wai
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscheduler.cpp
More file actions
123 lines (112 loc) · 3.9 KB
/
scheduler.cpp
File metadata and controls
123 lines (112 loc) · 3.9 KB
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
#include <cstdio>
#include <vector>
#include <set>
#include <cctype>
#include "mysql.h"
#include "db_wrapper.h"
#include "scheduler.h"
const int MAX_CMD_LEN = 512;
Scheduler* Scheduler::s_inst = NULL;
int Scheduler::_build_player_list(int game_type) {
MYSQL *handle = DBWrapper::GetHandle();
char cmd[MAX_CMD_LEN];
snprintf(cmd, MAX_CMD_LEN, "SELECT user_id,max(id) FROM main_app_submit WHERE game_type = %d AND status=1 GROUP BY user_id", game_type);
mysql_query(handle, cmd);
MYSQL_RES *mysql_res = mysql_store_result(handle);
MYSQL_ROW row;
int tmp_id = 0;
while (row = mysql_fetch_row(mysql_res)) {
int uid, sid;
sscanf(row[0], "%d", &uid);
sscanf(row[1], "%d", &sid);
m_uid2sid[game_type][uid] = sid;
fprintf(stderr, "uid:%d sid:%d\n", uid, sid);
}
mysql_free_result(mysql_res);
return 0;
}
static int GetUidFromSid(int sid) {
int uid = -1;
MYSQL *handle = DBWrapper::GetHandle();
char cmd[MAX_CMD_LEN];
snprintf(cmd, MAX_CMD_LEN, "SELECT user_id FROM main_app_submit WHERE id=%d", sid);
mysql_query(handle, cmd);
MYSQL_RES *res = mysql_store_result(handle);
MYSQL_ROW row;
if (row = mysql_fetch_row(res)) {
sscanf(row[0], "%d", &uid);
}
mysql_free_result(res);
return uid;
}
int Scheduler::_build_matrix(int game_type) {
MYSQL *handle = DBWrapper::GetHandle();
char cmd[MAX_CMD_LEN];
snprintf(cmd, MAX_CMD_LEN, "SELECT id FROM main_app_match WHERE game_type = %d AND result != -1", game_type);
mysql_query(handle, cmd);
MYSQL_RES *mysql_res = mysql_store_result(handle);
MYSQL_ROW row;
while (row = mysql_fetch_row(mysql_res)) {
int match_id;
sscanf(row[0], "%d", &match_id);
std::vector<int> sid_vec;
snprintf(cmd, MAX_CMD_LEN, "SELECT submit_id FROM main_app_match_players WHERE match_id = %d", match_id);
fprintf(stderr, "%s\n", cmd);
mysql_query(handle, cmd);
MYSQL_RES *mysql_res2 = mysql_store_result(handle);
MYSQL_ROW row2;
while (row2 = mysql_fetch_row(mysql_res2)) {
int sid;
sscanf(row2[0], "%d", &sid);
sid_vec.push_back(sid);
}
mysql_free_result(mysql_res2);
assert(sid_vec.size() == 2);
int u1 = GetUidFromSid(sid_vec[0]);
int u2 = GetUidFromSid(sid_vec[1]);
assert(u1 != -1 && u2 != -1);
if (u1 > u2) {
std::swap(u1,u2);
std::swap(sid_vec[0], sid_vec[1]);
}
std::pair<int,int> pr = std::make_pair(u1,u2);
m_mat[game_type][pr] = std::max(sid_vec[0], sid_vec[1]);
}
mysql_free_result(mysql_res);
}
void Scheduler::RegNewSub(int game_type, int uid, int sid) {
assert(m_uid2sid[game_type].count(uid) == 0 ||
sid > m_uid2sid[game_type][uid]);
m_uid2sid[game_type][uid] = sid;
}
void Scheduler::RegNewMatch(int game_type, int u1, int u2, int sub1, int sub2)
{
if (u1 > u2) {std::swap(u1,u2); std::swap(sub1,sub2);}
std::pair<int,int> pr = std::make_pair(u1, u2);
assert(m_mat[game_type].count(pr) == 0 ||
m_mat[game_type][pr] < std::max(sub1, sub2));
m_mat[game_type][pr] = std::max(sub1, sub2);
}
int Scheduler::InitScheduler(int game_type) {
_build_player_list(game_type);
_build_matrix(game_type);
return 0;
}
int Scheduler::ArrangeMatch(int game_type, int &u1, int &u2, int &sub1, int &sub2)
{
std::map<int,int>::iterator itr, itr2;
for (itr = m_uid2sid[game_type].begin() ; itr != m_uid2sid[game_type].end() ; ++itr) {
itr2 = itr;
for (++itr2 ; itr2 != m_uid2sid[game_type].end() ; ++itr2) {
u1 = itr->first;
u2 = itr2->first;
sub1 = itr->second;
sub2 = itr2->second;
if (m_mat[game_type][std::make_pair(u1, u2)] != std::max(sub1,sub2))
{
return 1;
}
}
}
return 0;
}