-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rank.cpp
197 lines (156 loc) · 5.2 KB
/
Rank.cpp
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include "all.h"
#include<algorithm>
//已整理
//--------------------------------------------------------------
void Rank::setup(){
bg.loadImage("image/welcome.jpg");
bg.resize(ofGetWindowWidth(),ofGetWindowHeight());
ofTrueTypeFont::setGlobalDpi(72);
text.loadFont("COOLSLIP.ttf",35,1,1);
text.setLineHeight(18.0f);
text.setLetterSpacing(1.037);
FONTA.loadFont("BIG.ttf",50,1,1);
FONTA.setLineHeight(18.0f);
FONTA.setLetterSpacing(1.037);
title.loadFont("2.ttf", 80, true, true);
title.setLineHeight(84.0f);
title.setLetterSpacing(1.035);
button.loadFont("BIG.ttf",25,true,true);
//for loading daat from RANKDATA
ofBuffer buffer = ofBufferFromFile(PATH);//setup path
string line = buffer.getFirstLine();//自第一行開始讀起
for(int i=0;i<5;++i)//loading data
{
if(line.empty() == false) {
splitString = ofSplitString(line, " ");//這是個超好用的function阿!!!
datum[i].playerName=splitString[0];
datum[i].win=ofToInt(splitString[1]);
datum[i].lose=ofToInt(splitString[2]);
}
if(i!=4)//prevent read out of range
line = buffer.getNextLine();
}
bdatumLoaded = true;
}
//--------------------------------------------------------------
void Rank::update(int& scene){
if(bdatumLoaded == true){
calculate(datum);
}
else
{
ofBuffer buffer = ofBufferFromFile(PATH);//setup path
string line = buffer.getFirstLine();//自第一行開始讀起
for(int i=0;i<5;++i)//loading data
{
if(line.empty() == false) {
splitString = ofSplitString(line, " ");//這是個超好用的function阿!!!
datum[i].playerName=splitString[0];
datum[i].win=ofToInt(splitString[1]);
datum[i].lose=ofToInt(splitString[2]);
}
if(i!=4)//prevent read out of range
line = buffer.getNextLine();
}
bdatumLoaded = true;
}
}
//--------------------------------------------------------------
void Rank::draw(int& scene){
ofSetHexColor(0xffffff);
bg.draw(0,0);
ofSetHexColor(0x0000ff);
FONTA.drawString("Name",20,120);
FONTA.drawString("W",220,120);
FONTA.drawString("L",420,120);
FONTA.drawString("PCT",620,120);
FONTA.drawString("GB",860,120);
ofSetHexColor(0xFFFF51);
title.drawString("RANK", ofGetWidth()/2-160, 75);
for(int i=0;i<5;++i)
{
ofSetHexColor(0xff8800);
text.drawString(datum[i].playerName,20,200+75*i);
text.drawString(ofToString(datum[i].win),220,200+75*i);
text.drawString(ofToString(datum[i].lose),420,200+75*i);
text.drawString(".",620,200+75*i);
text.drawString(_Pct[i].n[0]+_Pct[i].n[1]+_Pct[i].n[2],630,200+75*i);
if(i==0)
text.drawString(" --",820,200+75*i);
else
text.drawString(ofToString(GB[i-1]),860,200+75*i);
}
ofSetHexColor(0x33ffff);
button.drawString("Return",ofGetWidth()-170,ofGetHeight()-75);
ofSetHexColor(0xffff00);
if(ofGetMouseX()<ofGetWidth()-90 && ofGetMouseX()>ofGetWidth()-180 && ofGetMouseY()>ofGetHeight()-100 && ofGetMouseY()<ofGetHeight()-60)
button.drawString("*",ofGetWidth()/2+330,ofGetHeight()-75);
}
//--------------------------------------------------------------
void Rank::keyPressed(int key,int& scene){
}
//--------------------------------------------------------------
void Rank::keyReleased(int key,int& scene){
}
//--------------------------------------------------------------
void Rank::mouseMoved(int x, int y,int& scene){
}
//--------------------------------------------------------------
void Rank::mouseDragged(int x, int y, int button,int& scene){
}
//--------------------------------------------------------------
void Rank::mousePressed(int x, int y, int button,int& scene){
if(ofGetMouseX()<ofGetWidth()-90 && ofGetMouseX()>ofGetWidth()-180 && ofGetMouseY()>ofGetHeight()-100 && ofGetMouseY()<ofGetHeight()-60 && button ==0){
scene = 0;
bdatumLoaded = false;
}
}
//--------------------------------------------------------------
void Rank::mouseReleased(int x, int y, int button,int& scene){
}
//--------------------------------------------------------------
void Rank::windowResized(int w, int h,int& scene){
}
//--------------------------------------------------------------
void Rank::gotMessage(ofMessage msg,int& scene){
}
//--------------------------------------------------------------
void Rank::dragEvent(ofDragInfo dragInfo,int& scene){
}
void Rank::calculate(RankData _datum[5])
{
double pctTmp[5];
for(int i=0;i<5;++i){
pctTmp[i] = (double)datum[i].win/(datum[i].win+datum[i].lose);
}
//sorting
int _loc;
for(int i=0;i<5;++i)
{
_loc = i;
for(int j=i+1;j<5;++j)
{
if(pctTmp[_loc] < pctTmp[j])
_loc = j;
}
swap(datum[_loc],datum[i]);
swap(pctTmp[_loc],pctTmp[i]);
}
//calculate GB
for(int i=0;i<4;++i)
{
double t=(datum[0].win - datum[i+1].win)*(0.5) + (datum[i+1].lose - datum[0].lose)*(0.5);
GB[i] = t;
}
//calculate PCT
for(int i=0;i<5;++i)
{
pctTmp[i]*=10;
for(int j=0;j<3;++j)
{
char tmp = '0'+((int)pctTmp[i]%10);//int turn to char
_Pct[i].n[j] = tmp;
pctTmp[i]*=10;
}
}
}