File: setupHelpers.cpp

package info (click to toggle)
eris 1.3.14-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,016 kB
  • ctags: 1,819
  • sloc: cpp: 13,153; sh: 9,106; perl: 287; makefile: 201; ansic: 165
file content (146 lines) | stat: -rw-r--r-- 4,052 bytes parent folder | download | duplicates (6)
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
#include "setupHelpers.h"
#include "signalHelpers.h"
#include <cassert>
#include <Eris/PollDefault.h>
#include <Eris/Entity.h>
#include <sigc++/functors/mem_fun.h>
#include <iostream>
#include <wfmath/timestamp.h>

using WFMath::TimeStamp;
using WFMath::TimeDiff;

using std::cout;
using std::endl;

AutoConnection stdConnect()
{
    AutoConnection con(new Eris::Connection("eris-test", "localhost", 7450, false));
    SignalCounter0 connectCount;
    con->Connected.connect(sigc::mem_fun(connectCount, &SignalCounter0::fired));
    
    SignalCounter1<const std::string&> fail;
    con->Failure.connect(sigc::mem_fun(fail, &SignalCounter1<const std::string&>::fired));
    
    con->connect();
    
    while (!connectCount.fireCount() && !fail.fireCount())
    {
        Eris::PollDefault::poll();
    }
    
    assert(con->isConnected());
    return con;
}

AutoAccount stdLogin(const std::string& uname, const std::string& pwd, Eris::Connection* con)
{
    AutoAccount player(new Eris::Account(con));
    
    SignalCounter0 loginCount;
    player->LoginSuccess.connect(sigc::mem_fun(loginCount, &SignalCounter0::fired));
   
    SignalCounter1<const std::string&> loginErrorCounter;
    player->LoginFailure.connect(sigc::mem_fun(loginErrorCounter, &SignalCounter1<const std::string&>::fired));
    
    player->login(uname, pwd);

    while (!loginCount.fireCount() && !loginErrorCounter.fireCount())
    {
        Eris::PollDefault::poll();
    }
    
    assert(loginErrorCounter.fireCount() == 0);
    return player;
}

AvatarGetter::AvatarGetter(Eris::Account* acc) : 
    m_acc(acc),
    m_expectFail(false),
    m_failed(false),
    m_earlyReturn(false)
{
    m_acc->AvatarSuccess.connect(sigc::mem_fun(*this, &AvatarGetter::success));
    m_acc->AvatarFailure.connect(sigc::mem_fun(*this, &AvatarGetter::failure));
}
    
AutoAvatar AvatarGetter::take(const std::string& charId)
{        
    m_waiting = true;
    m_failed = false;
    
    m_acc->takeCharacter(charId);
    
    while (m_waiting) Eris::PollDefault::poll();
    
    // we wanted to fail, bail out now
    if (m_failed && m_expectFail) return AutoAvatar();
    
    assert(m_av->getEntity() == NULL); // shouldn't have the entity yet
    assert(m_av->getId() == charId); // but should have it's ID

    if (m_earlyReturn) return m_av;

    SignalCounter1<Eris::Entity*> gotChar;
    m_av->GotCharacterEntity.connect(sigc::mem_fun(gotChar, &SignalCounter1<Eris::Entity*>::fired));

    TimeStamp end = TimeStamp::now() + TimeDiff(2 * 1000);
    
    while ((gotChar.fireCount() == 0) && (TimeStamp::now() < end)) {
        Eris::PollDefault::poll();
    }
    
    if (gotChar.fireCount() == 0) cout << "timed-out waiting to go in-game" << endl;
    
    assert(m_av->getEntity());
    assert(m_av->getEntity()->getId() == charId);

    return m_av;
}

AutoAvatar AvatarGetter::create(const Atlas::Objects::Entity::RootEntity& charEnt)
{        
    m_waiting = true;
    m_failed = false;
    
    m_acc->createCharacter(charEnt);
    
    while (m_waiting) Eris::PollDefault::poll();
    
    // we wanted to fail, bail out now
    if (m_failed && m_expectFail) return AutoAvatar();
    
    assert(m_av->getEntity() == NULL); // shouldn't have the entity yet

    if (m_earlyReturn) return m_av;

    SignalCounter1<Eris::Entity*> gotChar;
    m_av->GotCharacterEntity.connect(sigc::mem_fun(gotChar, &SignalCounter1<Eris::Entity*>::fired));

    TimeStamp end = TimeStamp::now() + TimeDiff(2 * 1000);
    
    while ((gotChar.fireCount() == 0) && (TimeStamp::now() < end)) {
        Eris::PollDefault::poll();
    }
    
    if (gotChar.fireCount() == 0) cout << "timed-out waiting to go in-game" << endl;
    
    assert(m_av->getEntity());

    return m_av;
}
    
void AvatarGetter::success(Eris::Avatar* av)
{
    m_av.reset(av);
    m_waiting = false;
}

void AvatarGetter::failure(const std::string& msg)
{
    if (msg != "deliberate") {
        std::cerr << "failure getting an avatar: " << msg << std::endl;
    }
    m_waiting = false;
    m_failed = true;
}