File: market.cpp

package info (click to toggle)
attal 0.9.2-1.2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,992 kB
  • ctags: 5,972
  • sloc: cpp: 44,510; sh: 160; makefile: 45
file content (337 lines) | stat: -rw-r--r-- 9,129 bytes parent folder | download | duplicates (2)
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/****************************************************************
**
** Attal : Lords of Doom
**
** market.cpp
** display market place
**
** Version : $Id: market.cpp,v 1.8 2004/06/15 21:35:50 lusum Exp $
**
** Author(s) : Pascal Audoux
**
** Date : 14/01/2001
**
** Licence :    
**	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, or (at your option)
**      any later version.
**
**	This program is distributed in the hope that it will be useful,
** 	but WITHOUT ANY WARRANTY; without even the implied warranty of
**	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**	GNU General Public License for more details.
**
****************************************************************/

#include "market.h"
 
// generic include files
// include files for QT
#include <qlayout.h>
#include <qpushbutton.h>
#include <qpixmap.h>
#include <qsignalmapper.h>
#include <qscrollbar.h>
// application specific include files
#include "libCommon/dataTheme.h"
#include "libCommon/log.h"
#include "libCommon/attalSocket.h"

#include "libClient/gui.h"
#include "libClient/widget.h"

extern QString IMAGE_PATH;
extern DataTheme DataTheme;

Market::Market( QWidget * parent, const char * name )
	: QDialog( parent, name, true )
{
	_socket=0;
	
	setCaption( "Marketplace" );
	
	QVBoxLayout * layout = new QVBoxLayout( this );
		     
	QHBoxLayout * layH1 = new QHBoxLayout();
	_own = new DisplayResources( this );
	_own->setTitle( "Kingdom resources" );
	layH1->addWidget( _own, 1 );

	_other = new DisplayResources( this );
	_other->setTitle( "Available for trade" );
	layH1->addWidget( _other, 1 );
	
	layout->addLayout( layH1, 1 );
	
	_exchange = new ExchangeResources( this );
	layout->addWidget( _exchange );
	
	layout->setMargin( 2 );
	layout->activate();
	
	connect( _exchange, SIGNAL( sig_buy(int , int , int ) ), SLOT( slot_buy(int , int,int) ) );
	connect( _exchange, SIGNAL( sig_quit() ), SLOT( accept() ) );
	connect( _own, SIGNAL( sig_resource( int ) ), SLOT( slot_ownResource( int ) ) );
	connect( _other, SIGNAL( sig_resource( int ) ), SLOT( slot_otherResource( int ) ) );
}

void Market::setPrices( PriceMarket * prices )
{
	_prices = prices;
	_exchange->setPrices( prices );
}

void Market::slot_ownResource( int num )
{
	_exchange->slot_ownResource( num );
	_other->setPrices( _prices, num );
}

void Market::slot_otherResource( int num )
{
	_exchange->slot_otherResource( num );
}

void Market::slot_buy( int own,int other, int value )
{
	//logDD("own %d, other %d, value %d",own,other,value);
	_socket->sendBaseMarket(own,other,value);
}

/*!  Constructs an empty

*/

DisplayResources::DisplayResources( QWidget * parent, const char * name )
	: QWidget( parent, name )
{
	uint nbResources = DataTheme.resources.count();

	QVBoxLayout * layout = new QVBoxLayout( this );
	_title = new Sentence( this );

	layout->addWidget( _title );
	layout->addStretch( 1 );

	_resources = new ResourceIcon * [ nbResources ];

	QSignalMapper * sigmap = new QSignalMapper( this );

	for( uint i = 0; i < nbResources; i+=3 ) {
		if( nbResources-i == 1 ) {
			_resources[i] = new ResourceIcon( this );
			_resources[i]->setResource( i );
			layout->addWidget( _resources[i] );
			sigmap->setMapping( _resources[i], i );
			connect( _resources[i], SIGNAL( sig_clicked() ), sigmap, SLOT( map() ) );
		} else if( nbResources-i == 2 ) {
			QHBoxLayout * layH = new QHBoxLayout();
			_resources[i] = new ResourceIcon( this );
			_resources[i]->setResource( i );
			layH->addStretch( 1 );
			layH->addWidget( _resources[i] );
			layH->addStretch( 1 );
			sigmap->setMapping( _resources[i], i );
			connect( _resources[i], SIGNAL( sig_clicked() ), sigmap, SLOT( map() ) );
			_resources[i+1] = new ResourceIcon( this );
			_resources[i+1]->setResource( i+1 );
			layH->addWidget( _resources[i+1] );
			layH->addStretch( 1 );
			sigmap->setMapping( _resources[i+1], i+1 );
			connect( _resources[i+1], SIGNAL( sig_clicked() ), sigmap, SLOT( map() ) );
			layout->addLayout( layH );
		} else {
			QHBoxLayout * layH = new QHBoxLayout();

			_resources[i] = new ResourceIcon( this );
			_resources[i]->setResource( i );
			layH->addWidget( _resources[i] );
			sigmap->setMapping( _resources[i], i );
			connect( _resources[i], SIGNAL( sig_clicked() ), sigmap, SLOT( map() ) );

			_resources[i+1] = new ResourceIcon( this );
			_resources[i+1]->setResource( i+1 );
			layH->addWidget( _resources[i+1] );
			sigmap->setMapping( _resources[i+1], i+1 );
			connect( _resources[i+1], SIGNAL( sig_clicked() ), sigmap, SLOT( map() ) );

			_resources[i+2] = new ResourceIcon( this );
			_resources[i+2]->setResource( i+2 );
			layH->addWidget( _resources[i+2] );
			sigmap->setMapping( _resources[i+2], i+2 );
			connect( _resources[i+2], SIGNAL( sig_clicked() ), sigmap, SLOT( map() ) );

			layout->addLayout( layH );
		}
	}


	layout->activate();

	connect( sigmap, SIGNAL( mapped( int ) ), SIGNAL( sig_resource( int ) ) );
}

void DisplayResources::setResources( GenericPlayer * player )
{
	uint nbResources = DataTheme.resources.count();
	for( uint i = 0; i < nbResources; i++ ) {
		_resources[i]->setValue( QString::number( player->getResource( i ) ) );
	}
}

void DisplayResources::setPrices( PriceMarket * prices, uchar resource )
{
	uint nbResources = DataTheme.resources.count();
	for( uint i = 0; i < nbResources; i++ ) {
		if( resource == i ) {
			_resources[i]->setValue( "n/a" );
		}else {
			if(prices->getResourcePrice( resource ) >= prices->getResourcePrice( i )){
				_resources[i]->setValue( QString::number( prices->getResourceInResource(  resource,i ) ) );
			} else {
				_resources[i]->setValue( QString("1/%1").arg(  prices->getResourceInResource( i, resource ) ));
			}
		}
	}
}

void DisplayResources::setTitle( QString title )
{
	_title->setText( title );
}

/*!  Constructs an empty

*/

ExchangeResources::ExchangeResources( QWidget * parent, const char * name )
	: QWidget( parent, name )
{
	setFixedHeight( 200 );
	
	QVBoxLayout * layout = new QVBoxLayout( this );
	
	QHBoxLayout * layH1 = new QHBoxLayout();
	
	layH1->addStretch( 2 );
	_icoLeft = new ResourceIcon( this );
	layH1->addWidget( _icoLeft );
	layH1->addSpacing( 25 );
	
	_scroll = new QScrollBar( QScrollBar::Horizontal, this );
	layH1->addWidget( _scroll, 1 );
	layH1->addSpacing( 25 );
	
	_icoRight = new ResourceIcon( this );
	layH1->addWidget( _icoRight );
	layH1->addStretch( 2 );
	
	layout->addLayout( layH1 );
	
	QHBoxLayout * layH2 = new QHBoxLayout();
	
	layH2->addStretch( 1 );
	
	_butAll = new QPushButton( this );
	_butAll->setText( "All" );
	_butAll->setFixedSize( 50, 40 );
	layH2->addWidget( _butAll );
	layH2->addSpacing( 50 );
	
	_butBuy = new QPushButton( this );
	_butBuy->setText( "Buy" );
	_butBuy->setFixedSize( 50, 40 );
	layH2->addWidget( _butBuy );
	
	layH2->addStretch( 1 );

	QPushButton * butOk = createButtonOk( this );
	layH2->addWidget( butOk );
	
	layout->addLayout( layH2 );
	
	layout->activate();
	
	connect( butOk, SIGNAL( clicked() ), SIGNAL( sig_quit() ) );
	connect( _butAll, SIGNAL( clicked() ), SLOT( slot_all() ) );
	connect( _butBuy, SIGNAL( clicked() ), SLOT( slot_buy() ) );
	connect( _scroll, SIGNAL( valueChanged( int ) ), SLOT( slot_value( int ) ) );
	
	clear();
}

void ExchangeResources::slot_all()
{
	
}

void ExchangeResources::slot_buy()
{
	emit sig_buy(_resource, _other, _value);
	clear();
}

void ExchangeResources::slot_ownResource( int num )
{
	_icoLeft->setResource( num );
	_isLeft = true;
	_resource = num;
	setValue( 0 );
	_icoRight->setValue( QString::number( 0 ) );
	if( _isRight ) {
		 if(_prices->getResourcePrice( _resource ) > _prices->getResourcePrice( _other )){
			_cost = _prices->getResourceInResource(  _resource ,_other);
		 } else {
			_cost = _prices->getResourceInResource( _other,_resource);
		 }
		_scroll->setEnabled( true );
		_butBuy->setEnabled( true );
		_butAll->setEnabled( true );
	}
}

void ExchangeResources::slot_otherResource( int num )
{
	_other= num;
	_icoRight->setResource( num );
	_isRight = true;
	_icoRight->setValue( QString::number( 0 ) );
	_icoLeft->setValue( QString::number( 0 ) );
	if( _isLeft ) {
		if(_prices->getResourcePrice( _other ) > _prices->getResourcePrice( _resource )){
			_cost = _prices->getResourceInResource(  _other , _resource);
			_first=false;
		 } else {
			_cost = _prices->getResourceInResource(  _resource ,_other);
			_first=true;
		 }
		_scroll->setEnabled( true );
		_butBuy->setEnabled( true );
		_butAll->setEnabled( true );
	}
}

void ExchangeResources::clear()
{
	_icoRight->clear();
	_icoLeft->clear();
	_scroll->setEnabled( false );
	_butBuy->setEnabled( false );
	_butAll->setEnabled( false );
	_isRight = false;
	_isLeft = false;
}

void ExchangeResources::setValue( int val )
{
	_value=val;
	_scroll->setValue( val );
	if(_first==false){
		_icoLeft->setValue( QString::number( val * _cost ) );
		_icoRight->setValue( QString::number( val ) );
	} else {
		_icoRight->setValue( QString::number( val * _cost ) );
		_icoLeft->setValue( QString::number( val ) );
	}
}