File: Calendar.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 (99 lines) | stat: -rw-r--r-- 2,916 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
#ifdef HAVE_CONFIG_H
	#include "config.h"
#endif

#include <Eris/Calendar.h>
#include <Eris/Avatar.h>
#include <Eris/Exceptions.h>
#include <Eris/View.h>
#include <Eris/Entity.h>
#include <Eris/Log.h>

using namespace Atlas::Message;

namespace Eris
{

Calendar::Calendar(Avatar* av) :
    m_avatar(av),
    m_daysPerMonth(0),
    m_monthsPerYear(0),
    m_hoursPerDay(0),
    m_minutesPerHour(0),
    m_secondsPerMinute(0)
{
    av->getView()->TopLevelEntityChanged.connect(
        sigc::mem_fun(this, &Calendar::topLevelEntityChanged));
    // hook up right now if currently valid
    if (av->getView()->getTopLevel()) topLevelEntityChanged();
}

void Calendar::topLevelEntityChanged()
{
    m_calendarObserver.disconnect();
    Entity* tl = m_avatar->getView()->getTopLevel();
    if (!tl || !tl->hasAttr("calendar")) return;
    
    m_calendarObserver = tl->observe("calendar", sigc::mem_fun(this, &Calendar::calendarAttrChanged));
    
    initFromCalendarAttr(tl->valueOfAttr("calendar").asMap());
}

void Calendar::calendarAttrChanged(const std::string&, const Element& value)
{
    if (!value.isMap()) throw InvalidAtlas("malformed calendar data", value);
    debug() << "initing calendar";
    initFromCalendarAttr(value.asMap());
}

void Calendar::initFromCalendarAttr(const MapType& cal)
{
    MapType::const_iterator it = cal.find("days_per_month");
    if (it == cal.end()) throw InvalidAtlas("malformed calendar data", cal);
    m_daysPerMonth = it->second.asInt();
    
    it = cal.find("hours_per_day");
    if (it == cal.end()) throw InvalidAtlas("malformed calendar data", cal);
    m_hoursPerDay = it->second.asInt();
    
    it = cal.find("minutes_per_hour");
    if (it == cal.end()) throw InvalidAtlas("malformed calendar data", cal);
    m_minutesPerHour = it->second.asInt();
    
    it = cal.find("months_per_year");
    if (it == cal.end()) throw InvalidAtlas("malformed calendar data", cal);
    m_monthsPerYear = it->second.asInt();
    
    it = cal.find("seconds_per_minute");
    if (it == cal.end()) throw InvalidAtlas("malformed calendar data", cal);
    m_secondsPerMinute = it->second.asInt();
}

DateTime Calendar::now() const
{
    DateTime n;
    // we don't have valid calendar data yet
    if (m_daysPerMonth == 0) return n;
    
    n.m_seconds = lrintf(m_avatar->getWorldTime());
    
    n.m_minutes = n.m_seconds / m_secondsPerMinute;
    n.m_seconds -= (n.m_minutes * m_secondsPerMinute);
    
    n.m_hours = n.m_minutes / m_minutesPerHour;
    n.m_minutes -= (n.m_hours * m_minutesPerHour);
    
    n.m_dayOfMonth = n.m_hours / m_hoursPerDay;
    n.m_hours -= (n.m_dayOfMonth * m_hoursPerDay);
    
    n.m_month = n.m_dayOfMonth / m_daysPerMonth;
    n.m_dayOfMonth -= (n.m_month * m_daysPerMonth);
    
    n.m_year = n.m_month / m_monthsPerYear;
    n.m_month -= (n.m_year * m_monthsPerYear);
    
    n.m_valid = true;
    return n;
}

} // of namespace Eris