forked from kinmemodoki/private-bot
-
Notifications
You must be signed in to change notification settings - Fork 3
/
player.js
138 lines (108 loc) · 2.89 KB
/
player.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
137
138
var glicko2 = require('glicko2');
var Player = function(player, glk) {
this.name = player.nickname;
this.pid = player.principal_id;
this.glicko = glk;
this.win = player.win || 0;
this.count = player.count || 0;
this.prevPower = glk.getRating();
this.prevWon = null;
this.todayWin = 0;
this.todayCount = 0;
this.todayStartPower = glk.getRating();
}
Player.setConfig = function(config) {
this.config = {
"calculating_count": config.calculating_count,
"calculating_visible": config.calculating_visible
};
}
Player.prototype.getName = function() {
return this.name;
}
Player.prototype.updateName = function(player) {
this.name = player.nickname;
}
Player.prototype.getGlicko = function() {
return this.glicko;
}
Player.prototype.getPower = function() {
return this.glicko.getRating();
}
Player.prototype.getDiffPower = function() {
return this.prevPower === null ? null : this.getPower() - this.prevPower;
}
Player.prototype.preservePower = function() {
this.prevPower = this.getPower();
}
Player.prototype.updatePower = function() {
let diff = this.getPower() - this.prevPower;
if(diff == 0) {
this.prevWon = false;
return;
}
this.count += 1;
this.todayCount += 1;
if(diff > 0) {
this.win += 1;
this.todayWin += 1;
this.prevWon = true;
} else {
this.prevWon = false;
}
}
Player.prototype.str = function() {
var s = "";
s += this.name + ":\n ";
var powerstr = () => {
var s = this.getPower().toFixed(1);
let i = this.getDiffPower();
if(i !== null && i !== 0){
let diffstr = i > 0 ? "+"+i.toFixed(1)+" :arrow_upper_right: " : i.toFixed(1)+" :arrow_lower_right: ";
s += " (" + diffstr + ")";
}
return s;
};
if (this.count < Player.config.calculating_count) {
var calcstr = "【計測中 " + this.count + "/" + Player.config.calculating_count +"】";
if(Player.config.calculating_visible){
s += calcstr + " *" + powerstr() + "*";
}else{
s += calcstr;
}
} else {
s += powerstr();
}
return s;
}
Player.prototype.toJSON = function(key) {
var res = {
nickname: this.name,
principal_id: this.pid,
win: this.win,
count: this.count
};
const glk_params = {
rating: this.glicko.getRating(),
rd: this.glicko.getRd(),
vol: this.glicko.getVol()
};
res.rate = glk_params;
return res;
}
Player.prototype.todaySummary = function() {
var s = "";
s += this.name + ": " + this.getPower().toFixed(1) + "\n ";
s += this.todayWin + "勝" + (this.todayCount - this.todayWin) + "敗";
let diffPower = this.getPower() - this.todayStartPower;
let i = diffPower.toFixed(1);
let diffstr = i > 0 ? "+"+i+" :arrow_upper_right: " : i+" :arrow_lower_right: ";
s += " (" + diffstr + ")";
return s;
}
Player.prototype.summary = function() {
var s = "";
s += this.name + ": " + this.getPower().toFixed(1);
return s;
}
module.exports = Player;