File: Room.cpp

package info (click to toggle)
eris 1.3.10-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,592 kB
  • ctags: 1,516
  • sloc: cpp: 9,850; sh: 8,288; perl: 287; ansic: 165; makefile: 162
file content (320 lines) | stat: -rw-r--r-- 7,869 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
#ifdef HAVE_CONFIG_H
	#include "config.h"
#endif

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

#include <sigc++/slot.h>

#include <Atlas/Objects/Operation.h>
#include <Atlas/Objects/Anonymous.h>

#include <cassert>

using namespace Atlas::Objects::Operation;
using Atlas::Objects::Root;
using Atlas::Objects::Entity::RootEntity;
using Atlas::Objects::Entity::Anonymous;
using Atlas::Objects::smart_dynamic_cast;

namespace Eris
{
	
Room::Room(Lobby *l, const std::string& id) :
    m_roomId(id),
    m_entered(false),
    m_lobby(l)
{
    if (!id.empty())
        m_lobby->getConnection()->registerRouterForFrom(this, id);
}

Room::~Room()
{
    if (!m_roomId.empty())
        m_lobby->getConnection()->unregisterRouterForFrom(this, m_roomId);
}

#pragma mark -
// public command-issue wrappers

void Room::say(const std::string &tk)
{
    if (!m_lobby->getConnection()->isConnected())
    {
        error() << "talking in room " << m_roomId << ", but connection is down";
        return;
    }
	
    Anonymous speech;
    speech->setAttr("say", tk);
    speech->setAttr("loc", m_roomId);
	
    Talk t;
    t->setArgs1(speech);
    t->setTo(m_roomId);
    t->setFrom(m_lobby->getAccount()->getId());
    t->setSerialno(getNewSerialno());
	
    m_lobby->getConnection()->send(t);
}

void Room::emote(const std::string &em)
{
    if (!m_lobby->getConnection()->isConnected())
    {
        error() << "emoting in room " << m_roomId << ", but connection is down";
        return;
    }
	
    Imaginary im;
	
    Anonymous emote;
    emote->setId("emote");
    emote->setAttr("loc", m_roomId);
    emote->setAttr("description", em);
    
    im->setArgs1(emote);
    im->setTo(m_roomId);
    im->setFrom(m_lobby->getAccount()->getId());
    im->setSerialno(getNewSerialno());
	
    m_lobby->getConnection()->send(im);
}

void Room::leave()
{
    if (!m_lobby->getConnection()->isConnected())
    {
        error() << "leaving room " << m_roomId << ", but connection is down";
        return;
    }

    Move part;
    part->setFrom(m_lobby->getAccount()->getId());
    part->setSerialno(getNewSerialno());
    
    Anonymous args;
    args->setAttr("loc", m_roomId);
    args->setAttr("mode", "part");
    part->setArgs1(args);

    m_lobby->getConnection()->send(part);
}

Room* Room::createRoom(const std::string &name)
{
    if (!m_lobby->getConnection()->isConnected())
    {
        error() << "creating room in room  " << m_roomId << ", but connection is down";
        return NULL;
    }

    
    Create cr;
    cr->setFrom(m_lobby->getAccount()->getId());
    cr->setTo(m_roomId);
    cr->setSerialno(getNewSerialno());
    
    RootEntity room;
    room->setName(name);
    room->setParents(StringList(1, "room"));
    
    cr->setArgs1(room);
    m_lobby->getConnection()->send(cr);
    
    return NULL;
}

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

std::vector<Person*> Room::getPeople() const
{
    std::vector<Person*> people;
    
    for (IdPersonMap::const_iterator P=m_members.begin(); P != m_members.end(); ++P)
    {    
        if (P->second)
            people.push_back(P->second);
    }
    
    return people;
}

#pragma mark -

Router::RouterResult Room::handleOperation(const RootOperation& op)
{
    if (op->getTo() != m_lobby->getAccount()->getId()) {
        error() << "Room recived op TO account " << op->getTo() << ", not the account ID";
        return IGNORED;
    }

    const std::vector<Root>& args = op->getArgs();
    
    if (op->instanceOf(APPEARANCE_NO)) {
        for (unsigned int A=0; A < args.size(); ++A)
            appearance(args[A]->getId());

        return HANDLED;
    }

    if (op->instanceOf(DISAPPEARANCE_NO)) {
        for (unsigned int A=0; A < args.size(); ++A)
            disappearance(args[A]->getId());
        
        return HANDLED;
    }

    if (op->instanceOf(SIGHT_NO)) {
        assert(!args.empty());
        RootEntity ent = smart_dynamic_cast<RootEntity>(args.front());
            
        if (ent.isValid() && (ent->getId() == m_roomId)) {
            sight(ent);
            return HANDLED;
        }
    }

    return IGNORED;
}

void Room::sight(const RootEntity &room)
{
    if (m_entered)
        warning() << "got SIGHT of entered room " << m_roomId;
        
    m_name = room->getName();
    if (room->hasAttr("topic"))
        m_topic = room->getAttr("topic").asString();
    
    m_lobby->SightPerson.connect(sigc::mem_fun(this, &Room::notifyPersonSight));
    
    if (room->hasAttr("people"))
    {
        const Atlas::Message::ListType& people = room->getAttr("people").asList();
        for (Atlas::Message::ListType::const_iterator P=people.begin(); P!=people.end(); ++P)
            appearance(P->asString());
    }
    
    checkEntry();
    
    if (room->hasAttr("rooms"))
    {
        const Atlas::Message::ListType& rooms = room->getAttr("rooms").asList();
        for (Atlas::Message::ListType::const_iterator R=rooms.begin(); R!=rooms.end(); ++R)
        {
            m_subrooms.push_back(new Room(m_lobby, R->asString()));
        }
    }
}

void Room::handleSoundTalk(Person* p, const std::string& speech)
{
    assert(p);
    
    if (m_members.count(p->getAccount()) == 0) {
        error() << "room " << m_roomId << " got sound(talk) from non-member account";
        return;
    }
    
    Speech.emit(this, p, speech);
}

void Room::handleEmote(Person* p, const std::string& description)
{
    assert(p);
    
    if (m_members.count(p->getAccount()) == 0) {
        error() << "room " << m_roomId << " got sight(imaginary) from non-member account";
        return;
    }

    Emote.emit(this, p, description);
}

#pragma mark -
// room membership updates

void Room::appearance(const std::string& personId)
{
    IdPersonMap::iterator P = m_members.find(personId);
    if (P != m_members.end()) {
        error() << "duplicate appearance of person " << personId << " in room " << m_roomId;
        return;
    }
    
    Person* person = m_lobby->getPerson(personId);
    if (person)
    {
        m_members[personId] = person;
        if (m_entered)
            Appearance.emit(this, person);
    } else {
        m_members[personId] = NULL; // we know the person is here, but that's all
        // we'll find out more when we get the SightPerson signal from Lobby
    }
}

void Room::disappearance(const std::string& personId)
{
    IdPersonMap::iterator P = m_members.find(personId);
    if (P == m_members.end())
    {
        error() << "during disappearance, person " << personId << " not found in room " << m_roomId;
        return;
    }
    
    if (P->second) // don't emit if never got sight
        Disappearance.emit(this, P->second);
    
    m_members.erase(P);
}

void Room::notifyPersonSight(Person *p)
{
    assert(p);
    IdPersonMap::iterator P = m_members.find(p->getAccount());
    // for the moment, all rooms get spammed with sights of people, to avoid
    // the need for a counting / disconnect from SightPerson scheme
    if (P == m_members.end()) return;
    
    if (P->second == NULL) {
        P->second = p;
        
        if (m_entered)
            Appearance.emit(this, p);
        else
            checkEntry();
    } else {
        // fairly meaningless case, but I'm paranoid
        // could fire a 'changed' signal here, eg if they renamed?
        assert (P->second != p);
    }
}

void Room::checkEntry()
{
    assert(m_entered == false);
    
    bool anyPending = false;
    for (IdPersonMap::const_iterator P = m_members.begin(); P != m_members.end(); ++P)
        if (P->second == NULL) anyPending = true;
       
    if (!anyPending)
    {
        Entered.emit(this);
        m_entered = true;
    }
}

} // of Eris namespace