File: Room.cpp

package info (click to toggle)
eris 1.2.1-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,360 kB
  • ctags: 1,348
  • sloc: sh: 8,289; cpp: 7,576; perl: 287; ansic: 172; makefile: 143
file content (364 lines) | stat: -rw-r--r-- 10,372 bytes parent folder | download
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#ifdef HAVE_CONFIG_H
	#include "config.h"
#endif

#include <Eris/Room.h>
#include <Eris/Lobby.h>
#include <Eris/Connection.h>
#include <Eris/Utils.h>
#include <Eris/Person.h>
#include <Eris/Log.h>

#include <Eris/SignalDispatcher.h>
#include <Eris/ClassDispatcher.h>
#include <Eris/ArgumentDispatcher.h>

#include <Eris/Exceptions.h>

#include <Atlas/Objects/Entity/RootEntity.h>

#include <Atlas/Objects/Operation/Appearance.h>
#include <Atlas/Objects/Operation/Disappearance.h>
#include <Atlas/Objects/Operation/Look.h>
#include <Atlas/Objects/Operation/Move.h>
#include <Atlas/Objects/Operation/Imaginary.h>
#include <Atlas/Objects/Operation/Talk.h>

#include <sigc++/object_slot.h>

#include <cassert>

using namespace Atlas;

typedef Atlas::Message::Element::ListType AtlasListType;
typedef Atlas::Message::Element::MapType AtlasMapType;

namespace Eris
{
	
Room::Room(Lobby *l, const std::string &id) :
    _id(id), 
    _lobby(l),
    _parted(false)
{
    if (_id.empty()) return;
    assert(l);
    setup();
}

Room::~Room()
{
	/* avoid exceptions from further down if the Lobby never recieved the OOG entry. This
	happens if a Lobby object is created but the connection never goes live, for example. */
    if (_id.empty()) return;
    
	if (!_parted) // brutal tear down
		leave();
	
	Connection *con = _lobby->getConnection();
	std::string rid = "room_" + _id;
	
	// delete *everything* below our node
	con->removeDispatcherByPath("op:oog:sound", rid);
	con->removeDispatcherByPath("op:oog:sight:op", rid);
	con->removeDispatcherByPath("op:oog:appearance", rid);
	con->removeDispatcherByPath("op:oog:disappearance", rid);
}

void Room::setup()
{
	assert(!_id.empty());
	std::string rid("room_" + _id);
	// setup the dispatchers
	Connection *con = _lobby->getConnection();
	
	Dispatcher *sound = con->getDispatcherByPath("op:oog:sound");
	sound = sound->addSubdispatch(new ArgumentDispatcher("room_" + _id, "loc", _id));
	
// talk 
	sound->addSubdispatch(new SignalDispatcher<Atlas::Objects::Operation::Talk>("foo",
		SigC::slot(*this, &Room::recvSoundTalk)
	));

// visual stuff (sights)
    
    Dispatcher *sightOp = con->getDispatcherByPath("op:oog:sight:op");

    Dispatcher *cd = sightOp->addSubdispatch(ClassDispatcher::newAnonymous(con));
    Dispatcher *location = cd->addSubdispatch(new ArgumentDispatcher(rid, "loc", _id), "imaginary");
    
    location->addSubdispatch(new SignalDispatcher<Atlas::Objects::Operation::Imaginary>("imag",
		SigC::slot(*this, &Room::recvSightImaginary)
    ));

// appearance
	Dispatcher *apd = con->getDispatcherByPath("op:oog:appearance");
	apd = apd->addSubdispatch(new ArgumentDispatcher(rid, "loc", _id));
	apd->addSubdispatch(new SignalDispatcher<Atlas::Objects::Operation::Appearance>("foo",
		SigC::slot(*this, &Room::recvAppear)
	));
	
// disappearance
	Dispatcher *disd = con->getDispatcherByPath("op:oog:disappearance");
	disd = disd->addSubdispatch(new ArgumentDispatcher(rid, "loc", _id));
	disd->addSubdispatch(new SignalDispatcher<Atlas::Objects::Operation::Disappearance>("foo",
		SigC::slot(*this, &Room::recvDisappear)
	));
	
// initial look	
	if (_lobby != this) // not actually necessary, but avoids a duplicate initial look
		_lobby->look(_id);
}

void Room::say(const std::string &tk)
{
	if (!_lobby->getConnection()->isConnected())
		// FIXME - provide some feed-back here
		return;
	
	Atlas::Objects::Operation::Talk t;
	
	Atlas::Message::Element::MapType speech;
	speech["say"] = tk;
	speech["loc"] = _id;
	
	t.setArgs(Atlas::Message::Element::ListType(1, speech));
	t.setTo(_id);
	t.setFrom(_lobby->getAccountID());
	t.setSerialno(getNewSerialno());
	
	_lobby->getConnection()->send(t);
}

void Room::emote(const std::string &em)
{
	if (!_lobby->getConnection()->isConnected())
		// FIXME - provide some feed-back here
		return;
	
	Atlas::Objects::Operation::Imaginary im;
	
	Atlas::Message::Element::MapType emote;
	emote["id"] = "emote";
	emote["description"] = em;
	emote["loc"] = _id;
	
	im.setArgs(Atlas::Message::Element::ListType(1, emote));
	im.setTo(_id);
	im.setFrom(_lobby->getAccountID());
	im.setSerialno(getNewSerialno());
	
	_lobby->getConnection()->send(im);
}

void Room::leave()
{
	Connection *c = _lobby->getConnection();
	if (!c->isConnected())
		throw InvalidOperation("Not connected to server");
		
	Atlas::Objects::Operation::Move part;
	part.setFrom(_lobby->getAccountID());
	part.setSerialno(getNewSerialno());
	
	Message::Element::MapType args;
	args["loc"] = _id;
	args["mode"] = "part";
	part.setArgs(Message::Element::ListType(1, args));
	
	c->send(part);
	// FIXME - confirm the part somehow?
	_parted = true;
}

Room* Room::createRoom(const std::string &name)
{
    Connection *c = _lobby->getConnection();
    if (!c->isConnected())
		throw InvalidOperation("Not connected to server");
    
    Atlas::Objects::Operation::Create cr;
    cr.setFrom(_lobby->getAccountID());
    cr.setTo(_id);
    int serial = getNewSerialno();
    cr.setSerialno(serial);
    
    Message::Element::MapType args;
    args["parents"] = Message::Element::ListType(1, "room");
    args["name"] = name;
    
    cr.setArgs(Message::Element::ListType(1, args));
    c->send(cr);
    
    Room *r = new Room(_lobby);
    // this lets the lobby do stitch up as necessary
    _lobby->addPendingCreate(r, serial);
    r->_name = name;	// I'm setting this since we have it already, and it might
    // help someone identify the room prior to the ID going valid.
    return r;
}

void Room::sight(const Atlas::Objects::Entity::RootEntity &room)
{
	log(LOG_NOTICE, "Got sight of room %s", _id.c_str());
	_initialGet = true;
		
	_name = room.getName();
	//_creator = room.getAttr("creator").asString();

	// extract the current people list
	if (room.hasAttr("people")) {
		Message::Element::ListType people = room.getAttr("people").asList();
			
		for (Message::Element::ListType::const_iterator i=people.begin(); i!=people.end(); ++i) {
	
			std::string account = i->asString();
			_people.insert(account);
		
			if (_lobby->getPerson(account) == NULL) {
				// mark as pending
				_pending.insert(account);
			}
		}
	}
	
	if (_pending.empty()) {
		log(LOG_NOTICE, "Doing immediate entry to room %s", _id.c_str());
		// FIXME  - this code will cause OOG entry, even if the server
		// doesn't really support it (just to an empty lobby). The option
		// is not to emit the 'entered' signal at all. I don't know which
		// makes more sense.
		Entered.emit(this);
		_initialGet = false;
	}

	// wire up the signal for initial get
	_lobby->SightPerson.connect(SigC::slot(*this, &Room::notifyPersonSight));
	
	if (room.hasAttr("rooms")) {
	    Message::Element::ListType rooms = room.getAttr("rooms").asList();
	    // rattle through the list, jamming them into our set
	    for (unsigned int R=0; R<rooms.size();R++)
		_subrooms.insert(rooms[R].asString());
	}
}

void Room::notifyPersonSight(Person *p)
{
	assert(p);
	_pending.erase(p->getAccount());
	
	if (_pending.empty()) {
		if (_initialGet) {
			Entered.emit(this);
			_initialGet = false;
		} else {
			// we were waiting to show an appearance, we assume
			Appearance.emit(this, p->getAccount());
		}
	}
}

void Room::recvSoundTalk(const Atlas::Objects::Operation::Talk &tk)
{
	const Atlas::Message::Element &obj = getArg(tk, 0);
	Message::Element::MapType::const_iterator m = obj.asMap().find("say");
	if (m == obj.asMap().end())
		throw IllegalObject(tk, "No sound object in arg 0");
	std::string say = m->second.asString();
	
	// quick sanity check
	if (_pending.find(tk.getFrom()) != _pending.end()) {
		// supress this talk until we have the name
		// FIXME - buffer these and spool back?
		return;
	}
	// hit this assert if get a talk from somone we know *nothing* about
	if (_people.find(tk.getFrom()) == _people.end()) {
	    log(LOG_DEBUG, "unknown FROM %s in TALK operation");
	    assert(false);
	}
	
	// get the player name and emit the signal already
	Person *p = _lobby->getPerson(tk.getFrom());
	assert(p);
	Talk.emit(this, p->getAccount(), say);
}

void Room::recvSightImaginary(const Atlas::Objects::Operation::Imaginary &im)
{
    const Atlas::Message::Element &obj = getArg(im, 0);
    Message::Element::MapType::const_iterator m = obj.asMap().find("description");
    if (m == obj.asMap().end())
	return;
    const std::string & description = m->second.asString();
    // quick sanity check
    if (_pending.find(im.getFrom()) != _pending.end()) {
    	// supress this talk until we have the name
    	// FIXME - buffer these and spool back?
    	return;
    }
    // hit this assert if get a talk from somone we know *nothing* about
    if (_people.find(im.getFrom()) == _people.end()) {
        log(LOG_DEBUG, "unknown FROM %s in TALK operation");
        assert(false);
    }
    Person *p = _lobby->getPerson(im.getFrom());
    Emote.emit(this, p->getAccount(), description);
}

void Room::recvAppear(const Atlas::Objects::Operation::Appearance &ap)
{
	const AtlasListType &args = ap.getArgs();
	for (AtlasListType::const_iterator A=args.begin();A!=args.end();++A) {
		const AtlasMapType &app = A->asMap();
		AtlasMapType::const_iterator V(app.find("id"));
		std::string account(V->second.asString());
		
		_people.insert(account);
		if (_lobby->getPerson(account)) {
			// player is already known, can emit right now
			Appearance.emit(this, account);
		} else {
			// need to wait on the lookup
			if (_pending.empty())
				_lobby->SightPerson.connect(SigC::slot(*this, &Room::notifyPersonSight));
			
			_pending.insert(account);
		}
	}
}

void Room::recvDisappear(const Atlas::Objects::Operation::Disappearance &dis)
{
	
	const AtlasListType &args = dis.getArgs();
	for (AtlasListType::const_iterator A=args.begin();A!=args.end();++A) {
		const AtlasMapType &app = A->asMap();
		AtlasMapType::const_iterator V(app.find("id"));
		std::string account(V->second.asString());
		
		if (_people.find(account) == _people.end())
			throw IllegalObject(dis, "room disappearance for unknown person");
	
		_people.erase(account);
		Disappearance.emit(this, account);
	}
}

const std::string& Room::getID() const
{
    if (_id.empty() || _id == "") {
	throw InvalidOperation("called Room::getID() before the ID was available \
	    (wait till Entered signal is emitted");
    }
    
    return _id;
}

Person* Room::getPersonByUID(const std::string& uid)
{
    return _lobby->getPerson(uid);
}

} // of Eris namespace