summaryrefslogtreecommitdiffstats
path: root/libksirtet/lib/mp_simple_interface.cpp
blob: 1b83be409ac529c7ed1aa19d50fc15fde187bb1b (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
#include "mp_simple_interface.h"
#include "mp_simple_interface.moc"

#include <kmessagebox.h>
#include <tqtimer.h>
#include <klocale.h>
#include <kaction.h>
#include <kmainwindow.h>


#define PAUSE_ACTION \
    ((KToggleAction *)((KMainWindow *)topLevelWidget())->action("game_pause"))

MPSimpleInterface::MPSimpleInterface(const MPGameInfo &gi,
                                     uint nbActions, const ActionData *data,
									 TQWidget *parent, const char *name)
: MPInterface(gi, nbActions, data, parent, name), state(SS_Standby)
{}

void MPSimpleInterface::init()
{
	if ( server() ) {
		state = SS_Standby;
		first_init = TRUE;
	}
	_init();
}

void MPSimpleInterface::start()
{
	// WARNING : multiple calls can happen here (because button
	// hiding is delayed)
	state = SS_Init;
}

void MPSimpleInterface::stop()
{
	state = SS_Standby;
	SC_Flag f1(SC_Flag::Stop);
	if ( server() ) dataToClientsStream() << f1;
	IO_Flag f2(IO_Flag::Stop);
	for (uint i=0; i<nbPlayers(); i++) writingStream(i) << f2;
	immediateWrite();
}

void MPSimpleInterface::addKeys(KKeyDialog &d)
{
    if ( !isPaused() ) pause();
    MPInterface::addKeys(d);
}

void MPSimpleInterface::pause()
{
	// WARNING : multiple calls can happen here (because button
	// hiding is delayed)
	switch (state) {
	 case SS_Play:
         state = SS_PauseAsked;
         break;
	 case SS_Pause:
         state = SS_UnpauseAsked;
         break;
	 default: break;
	}
}

void MPSimpleInterface::dataFromServer(TQDataStream &s)
{
	if ( s.atEnd() ) return; // no data

	SC_Flag scf;
	s >> scf;
	switch (scf.value()) {
	 case SC_Flag::Stop:
		KMessageBox::information(this, i18n("Server has left game!"));
		TQTimer::singleShot(0, this, TQT_SLOT(singleHuman()));
		return;
	 case SC_Flag::GameOver:
		_readGameOverData(s);
		_showGameOverData();
		return;
	}
}

void MPSimpleInterface::treatData()
{
	switch (state) {
	 case SS_Init:         treatInit(); break;
	 case SS_Play:         treatPlay(); break;
	 case SS_Pause:        break;
	 case SS_Stop:         treatStop(); break;
	 case SS_Standby:      break;
	 case SS_PauseAsked:   treatPause(TRUE); break;
	 case SS_UnpauseAsked: treatPause(FALSE); break;
	}
}

void MPSimpleInterface::treatInit()
{
	state = SS_Play;

	if (first_init) {
		_firstInit();
		first_init = FALSE;
	}

	IO_Flag f(IO_Flag::Init);
	for (uint i=0; i<nbPlayers(); i++) writingStream(i) << f;
	_treatInit();
}

void MPSimpleInterface::treatPlay()
{
    PAUSE_ACTION->setEnabled(true);
    PAUSE_ACTION->setChecked(false);

	bool end = _readPlayData();
	if (end) {
		state = SS_Stop;
		IO_Flag f(IO_Flag::GameOver);
		for (uint i=0; i<nbPlayers(); i++) writingStream(i) << f;
		return;
	}
	if ( nbPlayers()==1 ) return; // no need to send data for singleplayer game
	IO_Flag f(IO_Flag::Play);
	for(uint i=0; i<nbPlayers(); i++) writingStream(i) << f;
	_sendPlayData();
}

void MPSimpleInterface::treatPause(bool pause)
{
	state = (pause ? SS_Pause : SS_Play);
	IO_Flag f(IO_Flag::Pause);
	for (uint i=0; i<nbPlayers(); i++) writingStream(i) << f;

    PAUSE_ACTION->setChecked(pause);
}

void MPSimpleInterface::treatStop()
{
	state = SS_Standby;

	// read game over data + send them to all clients
	TQDataStream &s = dataToClientsStream();
	SC_Flag f(SC_Flag::GameOver);
	s << f;
	_sendGameOverData(s);
	_showGameOverData();

    PAUSE_ACTION->setEnabled(false);
    PAUSE_ACTION->setChecked(false);
}