24#include "kgamepropertyhandler.h"
25#include "kgameproperty.h"
26#include "kgamemessage.h"
29#include <tqptrqueue.h>
34#define KPLAYERHANDLER_LOAD_COOKIE 6239
37class KGamePropertyHandlerPrivate
40 KGamePropertyHandlerPrivate()
44 TQMap<int, TQString> mNameMap;
45 TQIntDict<KGamePropertyBase> mIdDict;
49 bool mDefaultUserspace;
51 TQPtrQueue<KGamePropertyBase> mSignalQueue;
65KGamePropertyHandler::~KGamePropertyHandler()
71void KGamePropertyHandler::init()
73 kdDebug(11001) << k_funcinfo <<
": this=" <<
this << endl;
74 d =
new KGamePropertyHandlerPrivate;
76 d->mUniqueId=KGamePropertyBase::IdAutomatic;
77 d->mDefaultPolicy=KGamePropertyBase::PolicyLocal;
78 d->mDefaultUserspace=
true;
96 if (receiver && sendf) {
97 kdDebug(11001) <<
"Connecting TQ_SLOT " << sendf << endl;
98 connect(
this, TQ_SIGNAL(
signalSendMessage(
int, TQDataStream &,
bool*)), receiver, sendf);
100 if (receiver && emitf) {
101 kdDebug(11001) <<
"Connecting TQ_SLOT " << emitf << endl;
114 KGameMessage::extractPropertyHeader(stream, propertyId);
116 if (propertyId==KGamePropertyBase::IdCommand) {
118 KGameMessage::extractPropertyCommand(stream, propertyId, cmd);
120 p = d->mIdDict.find(propertyId);
122 if (!isSender || p->
policy()==KGamePropertyBase::PolicyClean) {
123 p->
command(stream, cmd, isSender);
126 kdError(11001) << k_funcinfo <<
": (cmd): property " << propertyId <<
" not found" << endl;
130 p = d->mIdDict.find(propertyId);
133 if (!isSender || p->
policy()==KGamePropertyBase::PolicyClean) {
137 kdError(11001) << k_funcinfo <<
": property " << propertyId <<
" not found" << endl;
147 d->mNameMap.erase(data->
id());
148 return d->mIdDict.remove(data->
id());
154 if (d->mIdDict.find(data->
id())) {
156 kdError(11001) <<
" -> cannot add property " << data->
id() << endl;
159 d->mIdDict.insert(data->
id(), data);
162 if (!name.isNull()) {
163 d->mNameMap[data->
id()] = name;
175 if (d->mIdDict.find(
id)) {
176 if (d->mNameMap.contains(
id)) {
177 s = i18n(
"%1 (%2)").arg(d->mNameMap[
id]).arg(
id);
179 s = i18n(
"Unnamed - ID: %1").arg(
id);
183 s = i18n(
"%1 unregistered").arg(
id);
194 kdDebug(11001) << k_funcinfo <<
": " << count <<
" KGameProperty objects " << endl;
195 for (i = 0; i < count; i++) {
200 if (cookie == KPLAYERHANDLER_LOAD_COOKIE) {
201 kdDebug(11001) <<
" KGamePropertyHandler loaded propertly"<<endl;
203 kdError(11001) <<
"KGamePropertyHandler loading error. probably format error"<<endl;
212 kdDebug(11001) << k_funcinfo <<
": " << d->mIdDict.count() <<
" KGameProperty objects " << endl;
213 stream << (uint)d->mIdDict.count();
214 TQIntDictIterator<KGamePropertyBase> it(d->mIdDict);
215 while (it.current()) {
218 KGameMessage::createPropertyHeader(stream, base->
id());
223 stream << (TQ_INT16)KPLAYERHANDLER_LOAD_COOKIE;
230 return d->mDefaultPolicy;
236 d->mDefaultUserspace=userspace;
237 TQIntDictIterator<KGamePropertyBase> it(d->mIdDict);
238 while (it.current()) {
239 if (!userspace || it.current()->id()>=KGamePropertyBase::IdUser) {
248 TQIntDictIterator<KGamePropertyBase> it(d->mIdDict);
249 while (it.current()) {
250 it.current()->unlock();
257 TQIntDictIterator<KGamePropertyBase> it(d->mIdDict);
258 while (it.current()) {
259 it.current()->lock();
266 return d->mUniqueId++;
271 TQIntDictIterator<KGamePropertyBase> it(d->mIdDict);
272 while (it.current()) {
273 if (it.current()->isDirty()) {
274 it.current()->sendProperty();
292 if (d->mIndirectEmit<=0)
295 while((prop=d->mSignalQueue.dequeue()) != 0)
310 if (d->mIndirectEmit>0)
313 d->mSignalQueue.enqueue(prop);
331 return d->mIdDict.find(
id);
336 kdDebug(11001) << k_funcinfo <<
id() << endl;
337 TQIntDictIterator<KGamePropertyBase> it(d->mIdDict);
338 while (it.toFirst()) {
341 if (d->mIdDict.find(p->
id())) {
357 return i18n(
"NULL pointer");
364 const type_info* t = prop->
typeinfo();
365 if (*t ==
typeid(
int)) {
366 value = TQString::number(((KGamePropertyInt*)prop)->value());
367 }
else if (*t ==
typeid(
unsigned int)) {
368 value = TQString::number(((KGamePropertyUInt *)prop)->value());
369 }
else if (*t ==
typeid(
long int)) {
371 }
else if (*t ==
typeid(
unsigned long int)) {
373 }
else if (*t ==
typeid(TQString)) {
374 value = ((KGamePropertyTQString*)prop)->value();
375 }
else if (*t ==
typeid(TQ_INT8)) {
376 value = ((KGamePropertyBool*)prop)->value() ? i18n(
"True") : i18n(
"False");
381 if (value.isNull()) {
382 value = i18n(
"Unknown");
389 kdDebug(11001) <<
"-----------------------------------------------------------" << endl;
390 kdDebug(11001) <<
"KGamePropertyHandler:: Debug this=" <<
this << endl;
392 kdDebug(11001) <<
" Registered properties: (Policy,Lock,Emit,Optimized, Dirty)" << endl;
393 TQIntDictIterator<KGamePropertyBase> it(d->mIdDict);
394 while (it.current()) {
396 kdDebug(11001) <<
" "<< p->
id() <<
": p=" << p->
policy()
404 kdDebug(11001) <<
"-----------------------------------------------------------" << endl;
407#include "kgamepropertyhandler.moc"
Base class of KGameProperty.
virtual void save(TQDataStream &s)=0
Write the value into a stream.
bool isOptimized() const
See also setOptimize.
PropertyPolicy
The policy of the property.
PropertyPolicy policy() const
bool isEmittingSignal() const
See also setEmittingSignal.
virtual void load(TQDataStream &s)=0
This will read the value of this property from the stream.
virtual void command(TQDataStream &stream, int msgid, bool isSender=false)
send a command to advanced properties like arrays
virtual const type_info * typeinfo()
bool isLocked() const
A locked property can only be changed by the player who has set the lock.
void setId(int id)
Use id as new ID for this KGamePropertyHandler.
TQString propertyValue(KGamePropertyBase *property)
In several situations you just want to have a TQString of a KGameProperty object.
virtual bool load(TQDataStream &stream)
Loads properties from the datastream.
void setPolicy(KGamePropertyBase::PropertyPolicy p, bool userspace=true)
Set the policy for all kgame variables which are currently registerd in the KGame proeprty handler.
void lockProperties()
Calls KGamePropertyBase::setReadOnly(true) for all properties of this handler.
void signalSendMessage(int msgid, TQDataStream &, bool *sent)
This signal is emitted when a property needs to be sent.
int uniquePropertyId()
returns a unique property ID starting called usually with a base of KGamePropertyBase::IdAutomatic.
bool removeProperty(KGamePropertyBase *data)
Removes a property from the handler.
void flush()
Sends all properties which are marked dirty over the network.
TQIntDict< KGamePropertyBase > & dict() const
Reference to the internal dictionary.
void unlockProperties()
Calls KGamePropertyBase::setReadOnly(false) for all properties of this player.
void signalRequestValue(KGamePropertyBase *property, TQString &value)
If you call propertyValue with a non-standard KGameProperty it is possible that the value cannot auto...
void unlockDirectEmit()
Removes the lock from the emitting of property signals.
KGamePropertyBase * find(int id)
void registerHandler(int id, const TQObject *receiver, const char *send, const char *emit)
Register the handler with a parent.
void lockDirectEmit()
Called by the KGame or KPlayer object or the handler itself to delay emmiting of signals.
bool sendProperty(TQDataStream &s)
called by a property to send itself into the datastream.
void clear()
Clear the KGamePropertyHandler.
virtual bool save(TQDataStream &stream)
Saves properties into the datastream.
bool processMessage(TQDataStream &stream, int id, bool isSender)
Main message process function.
void emitSignal(KGamePropertyBase *data)
called by a property to emit a signal This call is simply forwarded to the parent object
void signalPropertyChanged(KGamePropertyBase *)
This is emitted by a property.
void Debug()
Writes some debug output to the console.
TQString propertyName(int id) const
KGamePropertyHandler(TQObject *parent=0)
Construct an unregistered KGamePropertyHandler.
KGamePropertyBase::PropertyPolicy policy()
Returns the default policy for this property handler.
bool addProperty(KGamePropertyBase *data, TQString name=0)
Adds a KGameProperty property to the handler.
A class for network transparent games.