summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorschneefux <schneefux+commit@schneefux.xyz>2017-05-04 22:32:05 +0200
committerschneefux <schneefux+commit@schneefux.xyz>2017-05-04 22:32:05 +0200
commit79b3e8b0be084a22d4f30c5933935efe6ec1f743 (patch)
tree216d8244cf10052d8b62ece23528a04d545d03ed
parent78820b25d29d754ead3328d63119ccb2ec01db6b (diff)
downloadcruncher-release/2.2.0.tar.gz
cruncher-release/2.2.0.zip
fame crunchingrelease/2.2.0
-rw-r--r--crunch_team.sql34
-rw-r--r--worker.js14
2 files changed, 47 insertions, 1 deletions
diff --git a/crunch_team.sql b/crunch_team.sql
new file mode 100644
index 0000000..4133cf4
--- /dev/null
+++ b/crunch_team.sql
@@ -0,0 +1,34 @@
+UPDATE team_membership tm
+JOIN (
+ SELECT
+ tm.id AS tm_id,
+ SUM(
+ (1 + (tm_cnt-2) * 0.3) *
+ (p.winner * 0.3 + 0.7) *
+ (CASE
+ WHEN tm.status='initiate' THEN 10
+ WHEN tm.status='member' THEN 75
+ WHEN tm.status='veteran' THEN 100
+ WHEN tm.status='officer' THEN 125
+ WHEN tm.status='leader' THEN 125
+ END)
+ ) AS fame
+ FROM (
+ SELECT
+ t.id AS t_id,
+ m.api_id AS m_api_id,
+ COUNT(tm.id) AS tm_cnt
+ FROM participant p
+ JOIN player pl ON p.player_api_id = pl.api_id
+ JOIN team_membership tm ON pl.api_id = tm.player_api_id
+ JOIN team t ON tm.team_id = t.id
+ JOIN roster r ON p.roster_api_id = r.api_id
+ JOIN `match` m ON r.match_api_id = m.api_id
+ WHERE t.id IN (:team_ids)
+ GROUP BY t.id, m.api_id, r.id
+ ) AS cnt_by_m
+ JOIN participant p ON p.match_api_id = cnt_by_m.m_api_id
+ JOIN team_membership tm ON cnt_by_m.t_id = tm.team_id AND tm.player_api_id = p.player_api_id
+ GROUP BY tm.id
+) AS fame_diff ON tm.id = fame_diff.tm_id
+SET tm.fame = fame_diff.fame
diff --git a/worker.js b/worker.js
index 8c06d63..d7b7ffe 100644
--- a/worker.js
+++ b/worker.js
@@ -53,7 +53,7 @@ if (LOGGLY_TOKEN)
while (true) {
try {
seq = new Seq(DATABASE_URI, {
- logging: false,
+ //logging: false,
max: MAXCONNS
}),
rabbit = await amqp.connect(RABBITMQ_URI, { heartbeat: 320 }),
@@ -69,16 +69,19 @@ if (LOGGLY_TOKEN)
// load update SQL scripts; scripts use sequelize replacements
// for the `participant_api_id` array
const player_script = fs.readFileSync("crunch_player.sql", "utf8"),
+ team_script = fs.readFileSync("crunch_team.sql", "utf8"),
global_script = fs.readFileSync("crunch_global.sql", "utf8");
// fill a buffer and execute an SQL on a bigger (> 1o) batch
const participants_player = new Set(),
+ teams = new Set(),
participants_global = new Set(),
// store the msgs that should be ACKed
buffer = new Set();
let timeout = undefined;
// set maximum allowed number of unacked msgs
+ // TODO maybe split queues by type
await ch.prefetch(BATCHSIZE);
ch.consume("crunch", (msg) => {
const api_id = msg.content.toString();
@@ -86,6 +89,8 @@ if (LOGGLY_TOKEN)
participants_global.add(api_id);
if (msg.properties.type == "player")
participants_player.add(api_id);
+ if (msg.properties.type == "team")
+ teams.add(api_id);
buffer.add(msg);
if (timeout == undefined) timeout = setTimeout(crunch, LOAD_TIMEOUT*1000);
if (buffer.size >= BATCHSIZE) crunch();
@@ -98,9 +103,11 @@ if (LOGGLY_TOKEN)
// prevent async issues
const api_ids_player = [...participants_player],
+ team_ids = [...teams],
api_ids_global = [...participants_global],
msgs = new Set(buffer);
participants_global.clear();
+ teams.clear();
participants_player.clear();
buffer.clear();
clearTimeout(timeout);
@@ -111,6 +118,11 @@ if (LOGGLY_TOKEN)
replacements: { participant_api_ids: api_ids_global },
type: seq.QueryTypes.UPSERT
});
+ if (team_ids.length > 0)
+ await seq.query(team_script, {
+ replacements: { team_ids: team_ids },
+ type: seq.QueryTypes.UPDATE
+ });
if (api_ids_player.length > 0)
await seq.query(player_script, {
replacements: { participant_api_ids: api_ids_player },