summaryrefslogtreecommitdiffstats
path: root/kverbos/kverbos/kverbosuser.cpp
blob: 2794b05179be827bc7c6d2408de7c36e3309eb66 (plain)
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/***************************************************************************
                          kverbosuser.cpp  -  description
                             -------------------
    begin                : Tue Dec 18 2001
    copyright            : (C) 2001 by Arnold Kraschinski
    email                : arnold.k67@gmx.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "kverbosuser.h"

#include <cstdlib>
#include <tqfile.h>
#include <tqdatetime.h>

#include <kstandarddirs.h>


bool eintrag::operator== (const eintrag& e) const
{
	return (verb == e.verb);
}

KVerbosUser::KVerbosUser(spanishVerbList* pL, TQString n/*=DEFAULTUSER*/)
: name(n), auswahl(""), sessions(0)
{
	// try to read the user-information if there is one.
	int pos;
	while ((pos = n.find(' ')) != -1)
		n.tqreplace(pos, 1, "_");
	TQFile file(locateLocal("data", "/kverbos/data/"+name+".kverbos"));
	if ( file.open(IO_ReadOnly) )
	{
		TQString s;
		TQTextStream t( &file );
		name = t.readLine();
		s = t.readLine();	sessions = s.toInt();
		for (int i=0; i<MAX_RESULTS; i++)
		{
			date[i] = t.readLine();
			s = t.readLine(); result[i][0] = s.toInt();
			s = t.readLine(); result[i][1] = s.toInt();
		};
		while (!file.atEnd())	
		{
			eintrag e;
			e.verb = t.readLine();
			s = t.readLine(); e.status = s.toInt();
			s = t.readLine(); e.right = s.toInt();
			s = t.readLine(); e.wrong = s.toInt();
			if (e.status == 1)
				e.counter = STAY_IN_R_LIST;
			else
				e.counter = 0;
			e.used = false;
			it = liste.find(e);
			if (liste.end() == it)
				liste.append(e);
		};
		file.close();
	}
	else
		for (int i=0; i<MAX_RESULTS; i++)
		{
			date[i] = "";
			result[i][0] = result[i][1] = 0;
		};
	fillList(pL);
	it = liste.begin();
	sessions = (sessions + 1) & 1073741823;
}

KVerbosUser::~KVerbosUser()
{
}

// used zeigt an, ob dieses Verb in der Verbenliste des Hauptprogrammes vorkommt.
// Hier werden die Verben des Hauptprogrammes bertragen, deshalb bekommen alle Elemente
// used = true. Es k�nen aus der Benutzerinformation aber schon Verben in der Liste sein,
// die nicht in der Verbendatei des Hauptprogrammes vorkommen. Diese Verben erhalten alle
// used=false am Anfang. Wird ein Verb also in der Liste gefunden, dann muss zur Sicherheiet
// used=true gesetzt werden.
void KVerbosUser::fillList(spanishVerbList* pL)
{
	deaktivieren();
	for (spanishVerbList::Iterator it2 = pL->begin(); it2 != pL->end(); ++it2)
	{
		eintrag elem;
		elem.verb = (*it2).getSpanish();
		elem.right = elem.wrong = elem.counter = elem.status = 0;
		elem.used = true;
		it = liste.find(elem);
		if (liste.end() == it)
			liste.append(elem);
		else
			(*it).used = true;
	};
}

// alle Verben werden auf nicht genutzt gesetzt.
void KVerbosUser::deaktivieren()
{
	verbenListe::Iterator zeig;
	for (zeig=liste.begin(); zeig!=liste.end(); ++zeig)
		(*zeig).used = false;
}

// tests if verbs that have status RICHTIGGEMACHT have to get the status UNGELERNT.
// And calculates the kumulus of the verbs that have status UNGELERNT and FEFHLERHAFT.
// Der kumulus ist ein gewichteter Z�ler fr die Auswahl des Verbes. Fehlerhafte Verben
// haben ein gr�eres Gewicht und kommen deshalb eher wieder an die Reihe.
void KVerbosUser::updateKumulus()
{
	int k = 0;
	kumulus = 0;
	verbenListe::Iterator zeig;
	for (zeig=liste.begin(); zeig!=liste.end(); ++zeig)
	{
		if ((*zeig).status == RICHTIGGEMACHT)
		{
			if ((*zeig).used && ((*zeig).counter>1))
				(*zeig).counter--;
			else
			(*zeig).status--;
		};
		if ((*zeig).used && ((*zeig).status < RICHTIGGEMACHT))
		{
			kumulus = k;
			if ((*zeig).status == UNGELERNT)
				k = ((*zeig).right < (*zeig).wrong) ? k+2 : k+1;
			else
				k += ((*zeig).wrong) * FALSCHGEWICHT;
			(*zeig).counter = k;
		};
	};
}

// selects a verb out of the verbs that are in the U and F list. That means a
// verb that hasn't been studied yet or one that was wrong.
TQString KVerbosUser::suggestVerb()
{
	auswahl = "";
	it = liste.end();
	updateKumulus();
	if ((kumulus == 0) && !liste.isEmpty())
	{
		verbenListe::Iterator zeig;
		for (zeig=liste.begin(); zeig!=liste.end(); ++zeig)
			(*zeig).status = 0;
		// rekursiver Aufruf. Es ist kein Problem, weil die Liste nicht leer ist und alle
		// Elemente den status 0 bekommen haben, also kann eine Auswahl stattfinden.
		auswahl = suggestVerb();
	};
	if (kumulus != 0)
	{
		// RAND_MAX ist auf meinem System 231;
		int i = rand() % kumulus;
		verbenListe::Iterator zeig = liste.begin();
		while ((zeig != liste.end()) && (((*zeig).status==RICHTIGGEMACHT)||((*zeig).counter < i)))
			zeig++;
		auswahl = (*zeig).verb;
		it = zeig;
	};
	return auswahl;
}

// The solution for the suggested verb was right. Now the verb should be deleted from
// the list of unstudied verbs but added to the list with the right verbs.
void KVerbosUser::right()
{
	if (!auswahl.isEmpty())
	{
		(*it).right = ((*it).right+1) & MAX_WERT;
		if ((*it).wrong > 0)
			(*it).wrong--;
		if ((*it).status < RICHTIGGEMACHT)
			(*it).status++;
		if ((*it).status == RICHTIGGEMACHT)
			(*it).counter = STAY_IN_R_LIST;
	};
}

// The opposite function to the above one. The solution was wrong and the verb has to
// be moved to the list with the wrong verbs.
void KVerbosUser::wrong()
{
	if (!auswahl.isEmpty())
	{
		(*it).wrong = ((*it).wrong+1) & MAX_WERT;
		if ((*it).right > 0)
			(*it).right--;
		if ((*it).status > FEHLERHAFT)
			(*it).status--;
	};
}

// Saves all the user information to the $KDEHOME/apps/kverbos/data/username.kverbos file
bool KVerbosUser::saveUser(const int& res, const int& num)
{
	bool re = false;
	if (name != DEFAULTUSER)
	{
		int pos;
		while ((pos = name.find(' ')) != -1)
			name.tqreplace(pos, 1, "_");		
		TQFile file(locateLocal("data", "/kverbos/data/"+name+".kverbos"));
		if ( file.open(IO_WriteOnly) )
		{
			TQTextStream t( &file );
			t << name << endl;
			sessions = (num == 0) ? sessions - 1 : sessions;
			t << sessions << endl;
			int j = (num == 0) ? 0 : 1;
			for (int i=j; i<MAX_RESULTS; i++)
			{
				t << date[i] << endl;
				t << result[i][0] << endl;
				t << result[i][1] << endl;
			};
			if (j == 1)
			{
				TQDate d = TQDate::tqcurrentDate();
				t << d.toString() << endl;
				t << res << endl;
				t << num << endl;
			};
			for (it=liste.begin(); it!=liste.end(); ++it)	
		{
			if ((*it).status != UNGELERNT)
			{
				t << (*it).verb << endl;
				t << (*it).status << endl << (*it).right << endl << (*it).wrong << endl;
			};
		};
			re = true;
			file.close();	
		};
	};
	return re;
}

bool KVerbosUser::getResults(int& s, TQString d[], int r[][2])
{
	s = sessions;
	for (int i=0; i<MAX_RESULTS; i++)
	{
		d[i] = date[i];
		for (int j=0; j<2; j++)
			r[i][j] = result[i][j];
	};
	return true;
}