2009-05-08 13:28:41 +00:00
/*
* OpenClonk , http : //www.openclonk.org
*
2013-12-17 20:01:09 +00:00
* Copyright ( c ) 2001 - 2009 , RedWolf Design GmbH , http : //www.clonk.de/
2016-04-03 18:18:29 +00:00
* Copyright ( c ) 2010 - 2016 , The OpenClonk Team and contributors
2009-05-08 13:28:41 +00:00
*
2013-12-17 20:01:09 +00:00
* Distributed under the terms of the ISC license ; see accompanying file
* " COPYING " for details .
2009-05-08 13:28:41 +00:00
*
2013-12-17 20:01:09 +00:00
* " Clonk " is a registered trademark of Matthes Bender , used with permission .
* See accompanying file " TRADEMARK " for details .
2009-05-08 13:28:41 +00:00
*
2013-12-17 20:01:09 +00:00
* To redistribute this file separately , substitute the full license texts
* for the above references .
2009-05-08 13:28:41 +00:00
*/
# ifndef C4CLIENT_H
# define C4CLIENT_H
2017-04-30 08:49:09 +00:00
# include "lib/C4InputValidation.h"
2016-04-03 18:07:56 +00:00
# include "network/C4NetIO.h"
# include "network/C4PacketBase.h"
2009-05-08 13:28:41 +00:00
// special ids
const int32_t C4ClientIDUnknown = - 1 ,
2011-08-03 23:30:37 +00:00
C4ClientIDHost = 0 ,
C4ClientIDStart = 1 ;
2009-05-08 13:28:41 +00:00
// client core difference levels
2010-03-28 17:58:21 +00:00
const int32_t C4ClientCoreDL_None = 0 , // identical
2011-08-03 23:30:37 +00:00
C4ClientCoreDL_IDMatch = 1 , // status change
C4ClientCoreDL_IDChange = 2 , // identification changed (host only!)
C4ClientCoreDL_Different = 3 ; // really different
2009-05-08 13:28:41 +00:00
class C4ClientCore : public C4PacketBase
{
public :
C4ClientCore ( ) ;
2017-05-07 11:50:00 +00:00
~ C4ClientCore ( ) override ;
2009-05-08 13:28:41 +00:00
protected :
// identification
2017-05-07 11:50:00 +00:00
int32_t iID { - 1 } ;
2010-03-22 12:41:14 +00:00
ValidatedStdCopyStrBuf < C4InVal : : VAL_NameNoEmpty > Name , Nick ;
2009-05-08 13:28:41 +00:00
ValidatedStdCopyStrBuf < C4InVal : : VAL_NameAllowEmpty > CUID ;
2010-03-22 12:41:14 +00:00
ValidatedStdCopyStrBuf < C4InVal : : VAL_NameAllowEmpty > Revision ; // engine hg revision number
2009-05-08 13:28:41 +00:00
// version info
int iVersion [ 4 ] ;
// status
2017-05-07 11:50:00 +00:00
bool fActivated { false } , fObserver { false } , fLobbyReady { false } ;
2009-05-08 13:28:41 +00:00
public :
// status data
2010-03-28 17:58:21 +00:00
int32_t getID ( ) const { return iID ; }
bool isHost ( ) const { return iID = = C4ClientIDHost ; }
bool isActivated ( ) const { return fActivated ; }
bool isObserver ( ) const { return fObserver ; }
2015-11-19 03:49:59 +00:00
bool isLobbyReady ( ) const { return fLobbyReady ; }
2010-03-28 17:58:21 +00:00
void SetID ( int32_t inID ) { iID = inID ; }
void SetName ( const char * sznName ) { Name . CopyValidated ( sznName ) ; }
2009-05-08 13:28:41 +00:00
void SetActivated ( bool fnActivated ) { fActivated = fnActivated ; fObserver = false ; }
void SetObserver ( bool fnObserver ) { fActivated & = ! ( fObserver = fnObserver ) ; }
2015-11-22 04:58:15 +00:00
void SetLobbyReady ( bool fnLobbyReady ) { fLobbyReady = fnLobbyReady ; }
2009-05-08 13:28:41 +00:00
// misc
2010-03-28 17:58:21 +00:00
const char * getName ( ) const { return Name . getData ( ) ; }
const char * getCUID ( ) const { return CUID . getData ( ) ; }
const char * getNick ( ) const { return Nick . getData ( ) ; }
const char * getRevision ( ) const { return Revision . getData ( ) ; }
bool isRegistered ( ) const { return CUID . getLength ( ) > 0 ; }
2009-05-08 13:28:41 +00:00
// initialization
void SetLocal ( int32_t iID , bool fnActivated , bool fnObserver ) ;
void SetUnknown ( int32_t iID ) ;
int32_t getDiffLevel ( const C4ClientCore & CCore2 ) const ;
2017-05-07 11:50:00 +00:00
void CompileFunc ( StdCompiler * pComp ) override ;
2009-05-08 13:28:41 +00:00
} ;
class C4Client
{
friend class C4ClientList ;
public :
C4Client ( ) ;
C4Client ( const C4ClientCore & Core ) ;
~ C4Client ( ) ;
private :
C4ClientCore Core ;
2017-05-07 11:50:00 +00:00
bool fLocal { false } ; // Local, NoSync
class C4Network2Client * pNetClient { nullptr } ; // Local, NoSync
time_t last_lobby_ready_change { 0 } ; // Local, NoSync: Time when the lobby ready state was changed last through the SetLobbyReady call. 0 for never changed.
2009-05-08 13:28:41 +00:00
2017-05-07 11:50:00 +00:00
bool fIsIgnored { false } ; // Local, NoSync: chat messages from this client are suppressed
2010-03-28 19:33:51 +00:00
2009-05-08 13:28:41 +00:00
C4Client * pNext ;
public :
const C4ClientCore & getCore ( ) const { return Core ; }
2010-03-28 17:58:21 +00:00
int32_t getID ( ) const { return Core . getID ( ) ; }
bool isHost ( ) const { return Core . isHost ( ) ; }
const char * getName ( ) const { return Core . getName ( ) ; }
const char * getCUID ( ) const { return Core . getCUID ( ) ; }
const char * getNick ( ) const { return Core . getNick ( ) ; }
bool isActivated ( ) const { return Core . isActivated ( ) ; }
bool isObserver ( ) const { return Core . isObserver ( ) ; }
2015-11-19 03:49:59 +00:00
bool isLobbyReady ( ) const { return Core . isLobbyReady ( ) ; }
2009-05-08 13:28:41 +00:00
bool isRegistered ( ) const { return Core . isRegistered ( ) ; }
2010-03-28 19:33:51 +00:00
bool IsIgnored ( ) const { return fIsIgnored ; }
2009-05-08 13:28:41 +00:00
2010-03-28 17:58:21 +00:00
bool isLocal ( ) const { return fLocal ; }
2009-05-08 13:28:41 +00:00
C4Network2Client * getNetClient ( ) const { return pNetClient ; }
void SetCore ( const C4ClientCore & NewCore ) { Core = NewCore ; }
void SetID ( int32_t iID ) { Core . SetID ( iID ) ; }
void SetActivated ( bool fnActivated ) ;
void SetObserver ( ) { Core . SetObserver ( true ) ; }
2016-11-02 23:58:02 +00:00
void SetLobbyReady ( bool fnLobbyReady , time_t * time_since_last_change = nullptr ) ;
2009-05-08 13:28:41 +00:00
void SetLocal ( ) ;
2010-03-28 19:33:51 +00:00
void ToggleIgnore ( ) { fIsIgnored = ! fIsIgnored ; }
2016-11-02 23:58:02 +00:00
void UnlinkNetClient ( ) { pNetClient = nullptr ; }
2009-05-08 13:28:41 +00:00
void Remove ( ) ;
void CompileFunc ( StdCompiler * pComp ) ;
2010-03-28 19:33:51 +00:00
2009-05-08 13:28:41 +00:00
} ;
class C4ClientList
{
public :
C4ClientList ( ) ;
~ C4ClientList ( ) ;
C4ClientList & operator = ( const C4ClientList & List ) ;
void Clear ( ) ;
protected :
// client list
2017-05-07 11:50:00 +00:00
C4Client * pFirst { nullptr } ;
2009-05-08 13:28:41 +00:00
// pointer to local client
2017-05-07 11:50:00 +00:00
C4Client * pLocal { nullptr } ;
2009-05-08 13:28:41 +00:00
2016-11-02 23:58:02 +00:00
// network client list (may be nullptr if network is not active)
2017-05-07 11:50:00 +00:00
class C4Network2ClientList * pNetClients { nullptr } ;
2009-05-08 13:28:41 +00:00
void Add ( C4Client * pClient ) ;
public :
2016-11-02 23:58:02 +00:00
C4Client * getClient ( const C4Client * pAfter = nullptr ) const { return pAfter ? pAfter - > pNext : pFirst ; }
2009-05-08 13:28:41 +00:00
C4Client * getLocal ( ) const { return pLocal ; }
C4Client * getClientByID ( int32_t iID ) const ;
C4Client * getHost ( ) const { return getClientByID ( C4ClientIDHost ) ; }
C4Client * getNextClientByID ( int32_t iAfterID ) const ;
C4Client * getClientByName ( const char * szName ) const ;
int32_t getClientCnt ( ) const ;
const C4ClientCore & getLocalCore ( ) const { return getLocal ( ) - > getCore ( ) ; }
const char * getLocalName ( ) const { return pLocal ? getLocalCore ( ) . getName ( ) : " ??? " ; }
int32_t getLocalID ( ) const { return pLocal ? getLocalCore ( ) . getID ( ) : C4ClientIDUnknown ; }
2016-08-10 23:35:07 +00:00
StdStrBuf GetAllClientNames ( ) const ;
2009-05-08 13:28:41 +00:00
bool Init ( int32_t iLocalClientID = C4ClientIDHost ) ;
void InitNetwork ( class C4Network2ClientList * pNetClients ) ;
void ClearNetwork ( ) ;
bool Remove ( C4Client * pClient , bool fTemporary = false ) ;
C4Client * Add ( const C4ClientCore & Core ) ;
C4Client * AddLocal ( int32_t iID , bool fActivated , bool fObserver ) ;
void SetLocalID ( int32_t iID ) ;
void CtrlRemove ( const C4Client * pClient , const char * szReason ) ;
void RemoveRemote ( ) ;
void CompileFunc ( StdCompiler * pComp ) ;
} ;
# endif // C4CLIENT_H