This repository has been archived by the owner on Apr 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
console.js
111 lines (94 loc) · 2.48 KB
/
console.js
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
var express = require('express');
var app = express();
var nunjucks = require('nunjucks');
var bot = require('./bot')
var program = require('commander'); // Get Command-line arguments
// Results database
var db = require("./db");
var _ = require("underscore")
// Setup Logging
var log4js = require('log4js');
var logger = require('log4js').getLogger("webconsole");
var CHALLENGING = false;
if(program.startchallenging) CHALLENGING = true;
var minimaxbot = require("./bots/minimaxbot");
// Challenging logic
var MAX_ROOMS = 1;
setInterval(function() {
if(CHALLENGING && _.values(bot.ROOMS).length < MAX_ROOMS) {
logger.info("Challenging...");
bot.searchBattle();
}
}, 45000);
nunjucks.configure('templates', {
autoescape: true,
express: app,
watch: true
});
app.get('/', function(req, res){
db.find({}).sort({ date: -1}).exec(function(err, history) {
res.render('home.html', {
"games" : _.values(bot.ROOMS),
"domain" : bot.DOMAIN,
"history" : history,
"challenging" : CHALLENGING
});
});
});
// Challenge a specific user
app.get('/challenge', function(req, res){
bot.send("/challenge " + req.query.user + ", randombattle", null);
res.redirect("/");
});
app.get('/weights', function(req, res){
var text = "";
_.each(minimaxbot.BATTLE_FEATURES, function (feature, index) {
var value = minimaxbot.net.layers[1].filters[0].w.get(index);
text += feature + ": " + value + "<br>";
})
res.send(text);
});
// Challenging control
app.get('/startchallenging', function(req, res){
CHALLENGING = true;
res.redirect("/");
});
app.get('/endchallenging', function(req, res){
CHALLENGING = false;
res.redirect("/");
});
app.get('/room', function(req, res){
if(bot.ROOMS[req.query.id]) {
res.render("room.html", {
game: bot.ROOMS[req.query.id],
stringify : JSON.stringify,
format: function(str) {
return str.replace(/\n/g, "<br>").replace(/\t/g, " ");
}
});
} else {
res.redirect("/");
}
});
app.get('/replay', function(req, res){
db.findOne({ id: req.query.id }).exec(function(err, game) {
if(!game) {
res.redirect("/");
return;
}
game.decisions = JSON.parse(game.decisions);
res.render('replay.html', {
game : game,
stringify : JSON.stringify
});
});
});
app.get('/search', function(req, res){
logger.debug("Asked to query from web console.");
bot.searchBattle();
res.redirect("/");
});
var port = parseInt(program.port);
app.listen(port);
logger.info("Started web console on port " + port + "...");
module.exports = app;