Skip to content

Commit

Permalink
current resttt status
Browse files Browse the repository at this point in the history
  • Loading branch information
vinhill committed Jul 31, 2021
1 parent 2e5e9ad commit 67307be
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 241 deletions.
15 changes: 0 additions & 15 deletions RESTTT/.env

This file was deleted.

6 changes: 0 additions & 6 deletions RESTTT/.glitch-assets

This file was deleted.

179 changes: 0 additions & 179 deletions RESTTT/LICENSE

This file was deleted.

3 changes: 2 additions & 1 deletion RESTTT/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"dependencies": {
"express": "^4.17.1",
"mysql": "^2.18.1",
"cors": "^2.8.5"
"cors": "^2.8.5",
"node-fetch": "^2.6.1"
},
"engines": {
"node": "12.x"
Expand Down
8 changes: 8 additions & 0 deletions RESTTT/shrinkwrap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ dependencies:
cors: 2.8.5
express: 4.17.1
mysql: 2.18.1
node-fetch: 2.6.1
packages:
/accepts/1.3.7:
dependencies:
Expand Down Expand Up @@ -291,6 +292,12 @@ packages:
node: '>= 0.6'
resolution:
integrity: sha512-hZXc7K2e+PgeI1eDBe/10Ard4ekbfrrqG8Ep+8Jmf4JID2bNg7NvCPOZN+kfF574pFQI7mum2AUqDidoKqcTOw==
/node-fetch/2.6.1:
dev: false
engines:
node: 4.x || >=6.0.0
resolution:
integrity: sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==
/object-assign/4.1.1:
dev: false
engines:
Expand Down Expand Up @@ -468,3 +475,4 @@ specifiers:
cors: ^2.8.5
express: ^4.17.1
mysql: ^2.18.1
node-fetch: ^2.6.1
64 changes: 42 additions & 22 deletions RESTTT/src/config_route.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const express = require("express");
const router = express.Router();
const db = require("./database.js");
const fetch = require("node-fetch");

const join_re = /Client "(?<steam_name>\w*)" spawned in server <(?<steam_id>STEAM_[0-9:]*)> .*/;
const leave_re = /Dropped "(?<steam_name>\w*)" from server<(?<steam_id>STEAM_[0-9:]*)>/;
Expand All @@ -10,22 +11,25 @@ const result_re = /ServerLog: Result: (?<group>Innocent|Traitors|Killer|Jester)
const map_re = /Map: (?<map>.*)/;
const start_re = /ServerLog: Round proper has begun.../;

function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}

async function update_db_through_log(log, date) {
let clients = new Set();
let roles = new Map();
let mid = null;
let map = null;


for (let line of log) {
// join
let match = join_re.exec(line);
if (match) {
clients.add(match.groups.steam_name);
// add player to player table, if it doesn't exist
await db.queryReader(
"INSERT OR IGNORE INTO player (name) VALUES (?)",
[match.groups.steam_name,]
await db.queryAdmin(
"INSERT IGNORE INTO player (name) VALUES (?)",
[match.groups.steam_name]
);
continue;
}
Expand Down Expand Up @@ -55,16 +59,15 @@ async function update_db_through_log(log, date) {
// round started
match = start_re.exec(line);
if (match) {
// TODO does this return the inserted row?
await db.queryReader(
await db.queryAdmin(
"INSERT INTO round (map, date) VALUES (?, ?)",
(map, date)
[map, date]
);
// TODO how to get the mid?
let [res, fields] = await db.queryReader("SELECT mid FROM round ORDER BY mid DESC LIMIT 1");
// get the mid of the just inserted round
let res = await db.queryReader("SELECT mid FROM round ORDER BY mid DESC LIMIT 1");
let mid = res[0].mid;
for (let [k, v] of roles.entries()) {
await db.queryReader(
await db.queryAdmin(
"INSERT INTO participates (mid, player, role) VALUES (?, ?, ?)",
[mid, k, v]
);
Expand All @@ -78,18 +81,18 @@ async function update_db_through_log(log, date) {
let attacker = match.groups.atk_name;
let victim = match.groups.vkt_name;
// for some reason, this log entry isn't capitalized
let atkrole = match.groups.atk_role.capitalize();
let vktrole = match.groups.vkt_role.capitalize();
let time = match.groupstime;
let atkrole = capitalizeFirstLetter(match.groups.atk_role);
let vktrole = capitalizeFirstLetter(match.groups.vkt_role);
let time = match.groups.time;
if (match.groups.type == "DMG") {
await db.queryReader(
await db.queryAdmin(
"INSERT INTO damages (mid, attacker, victim, atkrole, vktrole, time, damage) VALUES (?, ?, ?, ?, ?, ?, ?)",
(mid, attacker, victim, atkrole, vktrole, time, match.groups.dmg)
[mid, attacker, victim, atkrole, vktrole, time, match.groups.dmg]
);
} else {
await db.queryReader(
await db.queryAdmin(
"INSERT INTO kills (mid, attacker, victim, atkrole, vktrole, time) VALUES (?, ?, ?, ?, ?, ?)",
(mid, attacker, victim, atkrole, vktrole, time)
[mid, attacker, victim, atkrole, vktrole, time]
);
}
continue;
Expand All @@ -98,9 +101,9 @@ async function update_db_through_log(log, date) {
// round result
match = result_re.exec(line);
if (match) {
db.queryReader(
"UPDATE round SET winner_team = ? WHERE mid = ?",
(match.groups.group, mid)
db.queryAdmin(
"UPDATE round SET winner = ? WHERE mid = ?",
[match.groups.group, mid]
);
continue;
}
Expand All @@ -110,10 +113,19 @@ async function update_db_through_log(log, date) {
router.post("/parse", async function(req,res,next){
// check request parameters
let filename = req.body.name;
let date = req.body.date;
if (!filename) {
res.status(400).json("The config/parse route requires the 'name' field in the body. It will be resolved to the URL 'https://raw.githubusercontent.com/vinhill/TTTStats/master/logs/<name>'");
return;
}
if(!date) {
res.status(400).json("The config/parse route requires the 'date' field in the body.");
return;
}
let date_re = /^....-..-..$/;
if(!date_re.exec(date)) {
res.status(400).json("The config/parse route requires a date in the format YYYY-MM-DD");
}

// check if already present
let config = await db.queryReader("SELECT * FROM configs WHERE filename = ?", [filename]);
Expand All @@ -124,18 +136,26 @@ router.post("/parse", async function(req,res,next){

// retreive log
let data = null;
let status = 500;
try{
let fetched = await fetch(`https://raw.githubusercontent.com/vinhill/TTTStats/master/logs/${filename}`);
let data = await fetched.text();
data = await fetched.text();
status = fetched.status;
}catch(e){
res.status(400).json(`Could not get config file because of an error: ${e}`);
return;
}
if(status != 200) {
res.status(status).json(`Could not get config file: ${data}`);
}

// parse
update_db_through_log(data.split("\n"));
update_db_through_log(data.split("\n"), date);
db.clearCache();
res.status(200).end();

// if everything worked, add filename to configs table
await db.queryAdmin("INSERT INTO configs (filename) VALUES (?)", [filename]);
});

module.exports = router;
Loading

0 comments on commit 67307be

Please sign in to comment.