-
Notifications
You must be signed in to change notification settings - Fork 1
/
qQuiz.cpp
238 lines (180 loc) · 5.68 KB
/
qQuiz.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include "qQuiz.h"
bool QuizCheck::ServerRun()
{
return st;
}
void QuizCheck::Next()
{
QuizTimer->stop();
sendMessage("Îòâåò: "+CurrAsk,bQuiz);
QEventLoop loop; QTimer::singleShot(1000, &loop, SLOT(quit())); loop.exec();
GetQuestion();
}
void QuizCheck::Answer(QString msg, QString ip)
{
if (msg==CurrAsk)
{
QuizTimer->stop();
QString nick = 0;
quint16 idt = ChatIp.indexOf(ip);
if (idt != -1)
nick = ChatNick.at(idt);
if (AskCount.at(idt) == 0)
AskCount[idt] = UserStats->value("all_stat/"+nick).toInt(); // çàãðóçêà çíà÷åíèÿ èç ôàéëà
AskCount[idt] += 1;
sendMessage("âåðíî, "+nick,bQuiz);
sendMessage("Âñåãî ïðàâèëüíûõ îòâåòîâ "+QString::number(AskCount.at(idt)),bQuiz);
UserStats->setValue("all_stat/"+nick,AskCount.at(idt));
for (quint8 i=0; i<10;++i)
{
if (AskCount[idt] > TopCount[i])
{
if (TopNick[i] != nick)
{
if (i < 9) //åñëè óæå â òîïå, òî ïîìåñòèòü íà óðîâåíü âûøå, åñëè íåò, äîáàâèòü íà ïîñëåäíåå ìåñòî
{
TopNick[i+1] = TopNick[i];
TopCount[i+1] = TopCount[i];
UserStats->setValue("top/pos"+QString::number(i+1)+"n",TopNick[i+1]);
UserStats->setValue("top/pos"+QString::number(i+1)+"c",TopCount[i+1]);
}
TopNick[i] = nick;
UserStats->setValue("top/pos"+QString::number(i)+"n",TopNick[i]);
sendMessage("Âû çàíÿëè "+QString::number(i+1)+"-å ìåñòî",bQuiz);
}
TopCount[i] = AskCount[idt];
UserStats->setValue("top/pos"+QString::number(i)+"c",TopCount[i]);
break;
}
}
QEventLoop loop; QTimer::singleShot(1000, &loop, SLOT(quit())); loop.exec(); //ïîäîæäàòü íåìíîæêî, äî âûäà÷è ñëåäóþùåãî âîïðîñà
GetQuestion();
}
}
void QuizCheck::Top()
{
sendMessage("Òîï âèêòîðèíû:",bQuiz);
for (quint8 i=0;i != 10;++i)
{
sendMessage(TopNick[i]+" - "+(TopCount[i]+48),bQuiz);
}
}
void QuizCheck::GetHint()
{
if (!st)
return;
AutoHint();
QuizTimer->start(5000);
}
void QuizCheck::Repeat()
{
sendMessage(CurrQuestion+" "+QString::number(CurrAsk.length())+" áóêâ",bQuiz);
}
void QuizCheck::AutoHint()
{
if (HintCount+2 == CurrAsk.length())
{
sendMessage("Íèêòî íå îòâåòèë íà âîïðîñ",bQuiz);
sendMessage("Îòâåò: "+CurrAsk,bQuiz);
QuizTimer->stop();
GetQuestion();
return;
}
int k=0;
do
{
k=qrand()%(CurrAsk.length()-1);
}
while (MaskAsk[k] == CurrAsk[k]);
MaskAsk[k]=CurrAsk[k];
HintCount++;
sendMessage("Ïîäñêàçêà: "+MaskAsk,bQuiz);
}
void QuizCheck::GetQuestion()
{
quint32 k;
bool m=false;
char pfix=' ';
HintCount = 0;
CurrAsk.clear();
CurrQuestion.clear();
MaskAsk.clear();
k=qrand()%count;
for (int i=0;i<Questions[k].length();i++)
{
if (Questions[k].at(i)=='*')
{
m=true;
continue;
}
if (m)
{
CurrAsk.append(Questions[k].at(i));
if ((Questions[k].at(i)==' ')||(Questions[k].at(i)=='-'))
MaskAsk.append(Questions[k].at(i));
else
MaskAsk.append("*");
}
else
CurrQuestion.append(Questions[k].at(i));
}
CurrAsk.remove(CurrAsk.length()-1,1);
MaskAsk.remove(MaskAsk.length()-1,1);
if (CurrAsk.length()==1) pfix='a'; else //âñóíóòü ïðàâèëüíîå îêîí÷àíèå ñëîâà "áóêâà"
if ((CurrAsk.length()==2)||(CurrAsk.length()==3)||(CurrAsk.length()==4)) pfix='û'; else
if ((CurrAsk.length()>20)&&(CurrAsk.length()%10==1)) pfix='a'; else
if ((CurrAsk.length()>20)&&((CurrAsk.length()%10==2)||(CurrAsk.length()%10==3)||(CurrAsk.length()%10==4))) pfix='û';
sendMessage(CurrQuestion+" "+QString::number(CurrAsk.length())+" áóêâ"+QString(pfix),bQuiz);
QuizTimer->start(5000);
}
void QuizCheck::Stop()
{
if (st)
{
QuizTimer->stop();
st = false;
sendMessage("Èãðà îñòàíîâëåíà",bQuiz);
sendMessage("Ïðàâèëüíûé îòâåò: "+CurrAsk,bQuiz);
}
}
void QuizCheck::Start ()
{
sendMessage("Â áàçå "+QString::number(count)+" âîïðîñîâ.",bQuiz);
sendMessage("Íà÷èíàåì",bQuiz);
//QuizTimer->start(5000);
GetQuestion();
st=1;
}
QuizCheck::QuizCheck (QObject * parent): QObject(parent)
{
BaseName.setFileName("questions.txt");
BaseName.open(QIODevice::ReadOnly|QIODevice::Text);
count=0;
while (BaseName.pos()<BaseName.size()) //ïîñ÷èòàòü êîë-âî ñòðîê
{
BaseName.readLine(500);
count++;
}
BaseName.seek(0);
Questions = new QString [count]; //ñîçäàòü íóæíûé ìàññèâ
for (quint32 i=0;BaseName.pos()<BaseName.size();i++) //çàïîëíèòü åãî
Questions[i] = QString(BaseName.readLine(500));
BaseName.close();
sendMessage("Çàãðóæåíî "+QString::number(count)+" âîïðîñîâ.",bGeneral);
st=0;
qsrand(QTime::currentTime().msecsTo(QTime()));
QuizTimer = new QTimer(this);
QObject::connect(QuizTimer,SIGNAL(timeout()),this,SLOT(AutoHint()));
UserStats = new QSettings("userstats.ini",QSettings::IniFormat);
for (quint8 i=0; i<10;++i) //çàïîëíèòü òîï
{
TopNick[i]=UserStats->value("top/pos"+QString::number(i)+"n").toString();
TopCount[i]=UserStats->value("top/pos"+QString::number(i)+"c").toInt();
}
}
QuizCheck::~QuizCheck()
{
delete QuizTimer;
delete UserStats;
delete[] Questions;
}