-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
136 lines (119 loc) · 4.61 KB
/
app.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const express = require('express');
const serveIndex = require('serve-index');
const bodyParser = require('body-parser');
const fs = require('fs');
const Logger = require('./modules/logger');
const Backup = require('./modules/backup');
const Factions = require("./modules/factions")
const debugMode = false;
const PORT = process.env.PORT || 80;
const ATTACK_INTERVAL = debugMode ? 100 : 1000 * 60 * 60 // 1 l'ora
const OLD_USERS = 5300;
const app = express();
let users = [];
let votedIp = {};
let owners = {};
let lastAttackTime = Date.now();
const getIp = request => {
const forwarded = request.headers['x-forwarded-for'];
const clientIp = forwarded ? forwarded.split(/, /)[0] : request.connection.remoteAddress;
return clientIp;
}
// rende pubblici i files in client
app.use(express.static('client'));
// permette il listing di backups
app.use('/backups', serveIndex('client/backups', {'icons': true}));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// routes for data
app.get("/data", (req, res) => {
res.send(Factions.getData());
users.push(getIp(req));
});
// routes countdown
// get info about next attack {attacker: attackingDepartment, defender: defendingDepartment, timeLeft: number}
let nextAttack;
app.get("/next-attack", (req, res) =>
res.send( Object.assign( // { timeLeft: Number } unione ( nextAttack o se vuoto {attacker: null, defender: null} )
{ timeLeft: lastAttackTime + ATTACK_INTERVAL - Date.now() },
nextAttack || {attacker: null, defender: null}
) )
);
// get war history
app.get("/logs", (req, res) => res.send(Logger.getLogs()));
// post vote for a faction
app.post("/vote", (req, res) => {
const clientIp = getIp(req)
const faction = req.body.faction;
if(votedIp[clientIp]) {
res.status(401 /* Not Authorized */).send("Non puoi votare in questo momento, hai già votato.");
} else if(Factions.addVotes(faction)) {
votedIp[clientIp] = true;
res.send("Voto per " + faction + " aggiunto con successo.");
} else {
res.status(400 /* Bad Request */).send("La fazione non esiste.");
}
});
// check if as voted
app.get("/has-voted", (req, res) => res.send(votedIp[getIp(req)] ? true : false));
// get count of unique ips that voted
app.get("/unique-ips", (req, res) => {
res.send(String(users.length + OLD_USERS));
});
// on server start
const server = app.listen(PORT, () => {
console.log("server listening on localhost:" + PORT);
});
function isSingular(sentence) {
let firstWord = sentence.split(" ")[0];
return ["A", "E", "O"].some(letter => letter === firstWord[firstWord.length - 1].toUpperCase());
}
const doAttack = (attackingDepartment /* name */, defendingDepartment /* name */) => {
const attacker = Factions.getOwner(attackingDepartment);
const defender /* name */ = Factions.getOwner(defendingDepartment);
if(attacker === defender || attacker === "nessuno") {
return;
}
const hasAttackerWon = (Math.random() <= (0.5 + (Factions.getVotes(attacker) - Factions.getVotes(defender)) / 100));
if (hasAttackerWon || defender === "nessuno") {
Factions.addBonus(attacker);
Factions.setOwner(defendingDepartment, attacker)
Logger.log(attacker + (isSingular(attacker) ? " ha" : " hanno") + " CONQUISTATO il dipartimento di " + defendingDepartment + " " + Factions.getCustomText(attacker));
}
else Logger.log(defender + (isSingular(defender) ? " ha" : " hanno") + " DIFESO il dipartimento da " + attacker);
}
// INIZIALIZZA I DATI DELLE FAZIONI
//Factions.initializeFactionsAndDepartments()
const attack = function() {
// ATTACCA FINO A FINIRE I CANDIDATI
nextAttack = Factions.getRandomAttack();
if(nextAttack) {
let attackInterval = setInterval(() => {
doAttack(nextAttack.attacker, nextAttack.defender);
nextAttack = Factions.getRandomAttack();
if(!nextAttack)
clearInterval(attackInterval)
// aggiornamento il tempo alla fine cosi' che il client non perda l'aggiornamento
// se avvenissero strani interleaving (molto improbabili)
lastAttackTime = Date.now();
}, ATTACK_INTERVAL);
} else {
// abbiamo inizializzato le fazioni per la prima volta e non ci sono fazioni adiacenti
throw "Non ci sono fazioni nemiche adiacenti all'avvio!"
}
}
Backup.loadBackup(attack);
setInterval(() => {
Backup.saveBackup();
}, 1000 * 60 * 45);
/* OGNI 24 ORE:
- resetta la mappa degli ip
- ogni fazione perde il proprio bonus */
setInterval(() => {
votedIp = {};
}, 1000 * 60 * 60 * 24);
/* DON'T SLEEP - 5 min - non serve più ma per scaramanzia non lo tolgo */
const http = require("http");
setInterval(function() {
http.get("http://sapienza-warbot.herokuapp.com/");
}, 300000);