[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Camino-devel] camino/src/client Game.h Game.cpp
From: |
Philippe Fremy |
Subject: |
[Camino-devel] camino/src/client Game.h Game.cpp |
Date: |
Thu, 27 Feb 2003 05:20:10 -0500 |
CVSROOT: /cvsroot/camino
Module name: camino
Changes by: Philippe Fremy <address@hidden> 03/02/27 05:20:09
Modified files:
src/client : Game.h Game.cpp
Log message:
merge client engine into game
Patches:
Index: camino/src/client/Game.cpp
diff -u camino/src/client/Game.cpp:1.16 camino/src/client/Game.cpp:1.17
--- camino/src/client/Game.cpp:1.16 Tue Feb 18 18:16:06 2003
+++ camino/src/client/Game.cpp Thu Feb 27 05:20:09 2003
@@ -2,7 +2,7 @@
**
** Camino
**
-** Version : $Id: Game.cpp,v 1.16 2003/02/18 23:16:06 pfremy Exp $
+** Version : $Id: Game.cpp,v 1.17 2003/02/27 10:20:09 pfremy Exp $
**
** Author(s) : Philippe Fremy, Pascal Audoux
** Creation : 15/01/2003
@@ -28,6 +28,7 @@
#include <qhbox.h>
#include <qvbox.h>
#include <qlabel.h>
+#include <qsocket.h>
// application specific include files
#include "common/log.h"
@@ -36,13 +37,18 @@
#include "client/BoardCanvas.h"
#include "client/BoardView.h"
#include "client/ChatWidget.h"
-#include "client/ClientEngine.h"
#include "client/TilePresentation.h"
Game::Game( QWidget * parent , const char * name )
: QWidget( parent, name, QWidget::WStyle_Customize |
QWidget::WStyle_DialogBorder )
{
- _engine = new ClientEngine( this );
+ _socket = new QSocket( this );
+ _state = StateNotConnected;
+ MsgDecoder::setDevice( _socket );
+ MsgCoder::setDevice( _socket );
+ connect( _socket, SIGNAL( connected() ), SLOT( slotServerConnected() )
);
+ connect( _socket, SIGNAL( connectionClosed() ), SLOT(
slotConnectionClosed() ) );
+ connect( _socket, SIGNAL( readyRead() ), SLOT( slotDecode() ) );
_board = new BoardCanvas( this );
@@ -65,12 +71,7 @@
_chat = new ChatWidget( vbox );
connect( _chat, SIGNAL( sig_message( const QString & ) ), SLOT(
slot_chatMessage( const QString & ) ) );
- //connect( _engine, SIGNAL( sig_message( const QString & ) ), SLOT(
slot_serverMessage( const QString & ) ) );
- connect( _engine, SIGNAL( sig_connected( bool ) ), SIGNAL(
sig_connected( bool ) ) );
- connect( _engine, SIGNAL( sig_connected( bool ) ), SLOT(
slot_clientConnected( bool ) ) );
-
- connect( _engine, SIGNAL( sig_recvTile( Tile::TileType ) ),
- _presentation, SLOT( slot_recvTile( Tile::TileType ) )
);
+ connect( this, SIGNAL( sig_connected( bool ) ), SLOT(
slot_clientConnected( bool ) ) );
connect( _presentation, SIGNAL( sig_tile( int ) ), SLOT( slot_tile( int
) ) );
connect( _actions, SIGNAL( sig_play() ), SLOT( slot_play() ) );
@@ -87,7 +88,7 @@
bool Game::isConnected()
{
- return _engine->isConnected();
+ return( _state != StateNotConnected );
}
void Game::reupdate()
@@ -99,13 +100,20 @@
void Game::connectToServer( QString host, int port, QString player )
{
- _engine->connectToServer( host, port, player );
+ qDebug("connecting to server...");
+ qDebug("Host : %s", host.latin1() );
+ qDebug("IP: %d", port );
+ qDebug("Player name : %s", player.latin1() );
+
+ _playerName = player;
+
+ _socket->connectToHost( host, port );
}
void Game::slot_chatMessage( const QString & msg )
{
- if( _engine->isConnected() ) {
- _engine->sendMessage( ChatMsg, msg );
+ if( isConnected() ) {
+ sendMessage( ChatMsg, msg );
} else {
_chat->newMessage( "(Not connected) : " + msg );
}
@@ -141,11 +149,6 @@
}
-void Game::slot_undo()
-{
- _engine->sendUndo();
-}
-
void Game::slot_tile( int row, int col )
{
printf( "%d %d\n", row, col );
@@ -154,8 +157,84 @@
void Game::slot_clientConnected( bool state )
{
if (state) {
- _playerLabel->setText( "Connected: " + _engine->getPlayerName()
);
+ _playerLabel->setText( "Connected: " + _playerName );
} else {
_playerLabel->setText( "Not Connected" );
}
+}
+
+void Game::slotServerConnected()
+{
+ _state = StateWaitStart;
+ qDebug("Game - Connection with server established!");
+ qDebug("Game - send player name " );
+ sendPlayerInfo( _playerName );
+ _state = StateWaitStart;
+ emit sig_connected( true );
+}
+
+void Game::slotConnectionClosed()
+{
+ qDebug("Game - Connection with server closed!");
+ _state = StateNotConnected;
+ emit sig_connected( false );
+}
+
+void Game::recvMsg( QString source, QString msg )
+{
+ QString typeString;
+ switch( _subtype ) {
+ case DebugMsg:
+ typeString = "Debug";
+ break;
+ case InfoMsg:
+ typeString = "Info";
+ break;
+ case ChatMsg:
+ typeString = "Chat";
+ break;
+ default:
+ typeString = "Unknown";
+ break;
+ }
+
+ qDebug( "Client - %s from %s : %s", typeString.latin1(),
source.latin1(), msg.latin1() );
+ QString text = source + " : " + msg;
+ //emit sig_message( text );
+ displayServerMessage( text );
+}
+
+void Game::sendMessage( NetMsgSubtype msgLevel, const QString & msg )
+{
+ MsgCoder::sendMsg( msgLevel, _playerName, msg );
+}
+
+void Game::sendUndo()
+{
+ if( _state == StatePlay ) {
+ MsgCoder::sendUndoRequest();
+ _state = StateUndoProcessing;
+ } else {
+ qDebug( "Game - sendUndo() called while in state %d", _state );
+ }
+}
+
+void Game::slotDecode()
+{
+// qDebug( "Game::slotDecode()" );
+// qDebug( "bytes available : %ld", _socket->bytesAvailable() );
+ while( _socket->bytesAvailable() ) {
+ MsgDecoder::slotDecode();
+ }
+}
+
+void Game::recvTilesForYou( Tile::TileType tile )
+{
+ qDebug( "Game::recvTile - %d", ( int )tile );
+ _presentation->addTile( tile );
+}
+
+void Game::slot_undo()
+{
+ sendUndo();
}
Index: camino/src/client/Game.h
diff -u camino/src/client/Game.h:1.13 camino/src/client/Game.h:1.14
--- camino/src/client/Game.h:1.13 Tue Feb 18 17:18:27 2003
+++ camino/src/client/Game.h Thu Feb 27 05:20:09 2003
@@ -5,7 +5,7 @@
** Game.h
** Main widget of the game
**
-** Version : $Id: Game.h,v 1.13 2003/02/18 22:18:27 Audoux Exp $
+** Version : $Id: Game.h,v 1.14 2003/02/27 10:20:09 pfremy Exp $
** Author(s) : Philippe Fremy, Pascal Audoux
** Creation : 15/01/2003
** Copyright: Pascal Audoux, Philippe Fremy 2003
@@ -38,14 +38,14 @@
class ActionList;
class BoardCanvas;
class ChatWidget;
-class ClientEngine;
class TilePresentation;
class QLabel;
+class QSocket;
/* ------------------------------
* Game
* ------------------------------ */
-class Game : public QWidget
+class Game : public QWidget, public MsgDecoder, public MsgCoder
{
Q_OBJECT
@@ -57,15 +57,17 @@
virtual ~Game();
void connectToServer( QString host, int port, QString player );
-
bool isConnected();
void reupdate();
-
void displayServerMessage( const QString & msg );
-
void addTile( Tile * tile );
+ // send messages to server
+ void sendMessage( NetMsgSubtype msgLevel, const QString & msg );
+ void sendUndo();
+
+
public slots:
void slot_chatMessage( const QString & msg );
@@ -82,16 +84,38 @@
void slot_clientConnected( bool state );
+protected slots:
+ void slotServerConnected();
+ void slotConnectionClosed();
+ void slotDecode();
+
signals:
void sig_connected( bool );
+ void sig_message( const QString & msg );
+ void sig_recvTile( Tile::TileType type );
+
protected:
+ void recvMsg( QString source, QString msg );
+ void recvTilesForYou( Tile::TileType tile );
+
ActionList * _actions;
TilePresentation * _presentation;
BoardCanvas * _board;
ChatWidget * _chat;
- ClientEngine * _engine;
QLabel * _playerLabel;
+
+ enum StateClient {
+ StateNotConnected,
+ StateWaitStart,
+ StateWaitTurn,
+ StateUndoRequested,
+ StateUndoProcessing,
+ StatePlay
+ } _state;
+
+ QSocket * _socket;
+ QString _playerName;
};
#endif // GAME_H