DateTime DateTime::operator-(const TimeSpan& span) { return DateTime(unixtime()-span.totalseconds()); }
void PlayerCity::CalculateResources() { CalculateResourceStats(); uint64_t newtime, oldtime, diff; newtime = unixtime(); oldtime = m_timers.updateresources; diff = newtime - oldtime; m_timers.updateresources = newtime; double add; add = ((((m_production.food + m_resourcebaseproduction)*(m_productionefficiency/100))/60/60/1000) * (1 + (m_resourcemanagement/100)) * diff) - m_troopconsume; if (m_maxresources.food > m_resources.food) { if (m_maxresources.food < m_resources.food+add) { m_resources.food = m_maxresources.food; } else { m_resources.food += add; } } add = ((((m_production.wood + m_resourcebaseproduction)*(m_productionefficiency/100))/60/60/1000) * (1 + (m_resourcemanagement/100)) * diff); if (m_maxresources.wood > m_resources.wood) { if (m_maxresources.wood < m_resources.wood+add) { m_resources.wood = m_maxresources.wood; } else { m_resources.wood += add; } } add = ((((m_production.stone + m_resourcebaseproduction)*(m_productionefficiency/100))/60/60/1000) * (1 + (m_resourcemanagement/100)) * diff); if (m_maxresources.stone > m_resources.stone) { if (m_maxresources.stone < m_resources.stone+add) { m_resources.stone = m_maxresources.stone; } else { m_resources.stone += add; } } add = ((((m_production.iron + m_resourcebaseproduction)*(m_productionefficiency/100))/60/60/1000) * (1 + (m_resourcemanagement/100)) * diff); if (m_maxresources.iron > m_resources.iron) { if (m_maxresources.iron < m_resources.iron+add) { m_resources.iron = m_maxresources.iron; } else { m_resources.iron += add; } } int32_t herosalary = 0; for (int i = 0; i < 10; ++i) if (m_heroes[i]) herosalary += m_heroes[i]->m_level * 20; add = ((m_production.gold/60/60/1000) * diff) - herosalary; m_resources.gold += add; if (m_resources.gold < 0) m_resources.gold = 0; }
TimeSpan DateTime::operator-(const DateTime& right) { return TimeSpan(unixtime()-right.unixtime()); }
int main(int argc, char* argv[]) { #ifndef WIN32 struct sigaction sa = { 0 }; sa.sa_handler = &sigfunc; sigaction(SIGINT, &sa, 0); sigaction(SIGHUP, &sa, 0); sigaction(SIGUSR1, &sa, 0); sigaction(SIGUSR2, &sa, 0); #else if (SetConsoleCtrlHandler((PHANDLER_ROUTINE)ConsoleHandler, TRUE) == FALSE) { // unable to install handler... // display message to the user printf("Unable to install handler!\n"); return -1; } #endif srand(unixtime()); gserver = new Server(); gserver->serverstatus = SERVERSTATUS_STARTING; try { //Initialize server and load config files if (!gserver->Init()) { return 0; } //Connect to SQL server if (!gserver->ConnectSQL()) { return 0; } //Open sockets if (!gserver->InitSockets()) { return 0; } } catch (std::exception& e) { gserver->consoleLogger->fatal(Poco::format("Init() Exception: %s", (string)e.what())); return 0; } catch (...) { gserver->consoleLogger->fatal("Unspecified Init() Exception."); return 0; } //Server is finally online here gserver->serverstatus = SERVERSTATUS_ONLINE; // Run the server until stopped. try { gserver->run(); } catch (Poco::Data::MySQL::MySQLException * e) { gserver->consoleLogger->fatal(Poco::format("SQL Exception: %s", e->displayText())); } try { gserver->TimerThread(); } catch (Poco::Data::MySQL::MySQLException * e) { gserver->consoleLogger->fatal(Poco::format("TimerThread() SQL Exception: %s", e->displayText())); } catch (...) { gserver->consoleLogger->fatal("Unspecified TimerThread() Exception."); } gserver->consoleLogger->information("Shutting down"); //does this need a thread join here? gserver->stop_all(); delete gserver; printf("Exiting...\n"); return 0; }
void Window::notifyShowNext(NotifyWindow *remove) { if (App::quiting()) return; int32 count = NotifyWindowsCount; if (remove) { for (NotifyWindows::iterator i = notifyWindows.begin(), e = notifyWindows.end(); i != e; ++i) { if ((*i) == remove) { notifyWindows.erase(i); break; } } } uint64 ms = getms(), nextAlert = 0; bool alert = false; for (NotifyWhenAlerts::iterator i = notifyWhenAlerts.begin(); i != notifyWhenAlerts.end();) { while (!i.value().isEmpty() && *i.value().begin() <= ms) { i.value().erase(i.value().begin()); NotifySettingsPtr n = i.key()->peer->notify; if (n == EmptyNotifySettings || (n != UnknownNotifySettings && n->mute <= unixtime())) { alert = true; } } if (i.value().isEmpty()) { i = notifyWhenAlerts.erase(i); } else { if (!nextAlert || nextAlert > *i.value().begin()) { nextAlert = *i.value().begin(); } ++i; } } if (alert) { psFlash(); App::playSound(); } if (cCustomNotifies()) { for (NotifyWindows::const_iterator i = notifyWindows.cbegin(), e = notifyWindows.cend(); i != e; ++i) { int32 ind = (*i)->index(); if (ind < 0) continue; --count; } } if (count <= 0 || !cDesktopNotify()) { if (nextAlert) { notifyWaitTimer.start(nextAlert - ms); } return; } QRect r = psDesktopRect(); int32 x = r.x() + r.width() - st::notifyWidth - st::notifyDeltaX, y = r.y() + r.height() - st::notifyHeight - st::notifyDeltaY; while (count > 0) { uint64 next = 0; HistoryItem *notifyItem = 0; NotifyWaiters::iterator notifyWaiter; for (NotifyWaiters::iterator i = notifyWaiters.begin(); i != notifyWaiters.end(); ++i) { History *history = i.key(); if (history->currentNotification() && history->currentNotification()->id != i.value().msg) { NotifyWhenMaps::iterator j = notifyWhenMaps.find(history); if (j == notifyWhenMaps.end()) { history->clearNotifications(); i = notifyWaiters.erase(i); continue; } do { NotifyWhenMap::const_iterator k = j.value().constFind(history->currentNotification()->id); if (k != j.value().cend()) { i.value().msg = k.key(); i.value().when = k.value(); break; } history->skipNotification(); } while (history->currentNotification()); } if (!history->currentNotification()) { notifyWhenMaps.remove(history); i = notifyWaiters.erase(i); continue; } uint64 when = i.value().when; if (!notifyItem || next > when) { next = when; notifyItem = history->currentNotification(); notifyWaiter = i; } } if (notifyItem) { if (next > ms) { if (nextAlert && nextAlert < next) { next = nextAlert; nextAlert = 0; } notifyWaitTimer.start(next - ms); break; } else { if (cCustomNotifies()) { NotifyWindow *notify = new NotifyWindow(notifyItem, x, y); notifyWindows.push_back(notify); psNotifyShown(notify); --count; } else { psPlatformNotify(notifyItem); } uint64 ms = getms(); History *history = notifyItem->history(); history->skipNotification(); NotifyWhenMaps::iterator j = notifyWhenMaps.find(history); if (j == notifyWhenMaps.end() || !history->currentNotification()) { history->clearNotifications(); notifyWaiters.erase(notifyWaiter); if (j != notifyWhenMaps.end()) notifyWhenMaps.erase(j); continue; } j.value().remove(notifyItem->id); do { NotifyWhenMap::const_iterator k = j.value().constFind(history->currentNotification()->id); if (k != j.value().cend()) { notifyWaiter.value().msg = k.key(); notifyWaiter.value().when = k.value(); break; } history->skipNotification(); } while (history->currentNotification()); if (!history->currentNotification()) { notifyWaiters.erase(notifyWaiter); notifyWhenMaps.erase(j); continue; } } } else { break; } } if (nextAlert) { notifyWaitTimer.start(nextAlert - ms); } count = NotifyWindowsCount - count; for (NotifyWindows::const_iterator i = notifyWindows.cbegin(), e = notifyWindows.cend(); i != e; ++i) { int32 ind = (*i)->index(); if (ind < 0) continue; --count; (*i)->moveTo(x, y - count * (st::notifyHeight + st::notifyDeltaY)); } }
amf3object Client::ToObject() { amf3object obj = amf3object(); obj["newReportCount_trade"] = 0; obj["newMaileCount_system"] = 0; obj["newReportCount"] = 0; obj["isSetSecurityCode"] = false; obj["mapSizeX"] = gserver->mapsize; obj["mapSizeY"] = gserver->mapsize; obj["newReportCount_other"] = 0; obj["buffs"] = BuffsArray(); obj["gamblingItemIndex"] = 12; obj["changedFace"] = false; obj["castles"] = CastleArray(); obj["playerInfo"] = PlayerInfo(); obj["redCount"] = 0; obj["usePACIFY_SUCCOUR_OR_PACIFY_PRAY"] = 1;//always 1? obj["newMaileCount_inbox"] = 0; string s; { time_t ttime; time(&ttime); struct tm * timeinfo; timeinfo = localtime(&ttime); std::stringstream ss; ss << (timeinfo->tm_year + 1900) << "."; if (timeinfo->tm_mon < 9) ss << "0" << (timeinfo->tm_mon + 1); else ss << (timeinfo->tm_mon + 1); ss << "."; if (timeinfo->tm_mday < 10) ss << "0" << timeinfo->tm_mday; else ss << timeinfo->tm_mday; ss << " "; if (timeinfo->tm_hour < 10) ss << "0" << timeinfo->tm_hour; else ss << timeinfo->tm_hour; ss << "."; if (timeinfo->tm_min < 10) ss << "0" << timeinfo->tm_min; else ss << timeinfo->tm_min; ss << "."; if (timeinfo->tm_sec < 10) ss << "0" << timeinfo->tm_sec; else ss << timeinfo->tm_sec; s = ss.str(); } // obj["currentDateTime"] = "2011.07.27 03.20.32"; obj["currentDateTime"] = s.c_str(); obj["newReportCount_army"] = 0; obj["friendArmys"] = amf3array(); obj["saleTypeBeans"] = SaleTypeItems(); obj["autoFurlough"] = false; obj["castleSignBean"] = amf3array(); obj["furloughDay"] = 0; obj["tutorialStepId"] = 0;//10101; -- can set any tutorial obj["newReportCount_army"] = 0; obj["newMailCount"] = 0; obj["furlough"] = false; obj["gameSpeed"] = 5; obj["enemyArmys"] = amf3array(); obj["currentTime"] = (double)unixtime(); obj["items"] = Items(); obj["freshMan"] = false; obj["finishedQuestCount"] = 0; obj["selfArmys"] = amf3array(); obj["saleItemBeans"] = SaleItems(); return obj; }
void SensorManager::retrieveStatistics(bool ignoreshutdown) { const char* xmlpre = "<vermont>\n\t<sensorData time=\"%s\" epochtime=\"%d.%03d\" host=\"%s\">\n"; const char* xmlpost = "\t</sensorData>\n</vermont>\n"; const char* xmlglobals = "\t\t<%s>%s</%s>\n"; string lockfile = outputFilename + ".lock"; bool haveGraphLock; // we must not wait for the graph lock, else there may be a race condition with // the ConfigManager while (! (haveGraphLock = graphIS->tryLockGraph())) { if (smExitFlag) break; timespec timeout = { 0, 200000 }; nanosleep(&timeout, NULL); } if (!ignoreshutdown && smExitFlag) return; const char* openflags = (append ? "a" : "w"); FILE* file = fopen(outputFilename.c_str(), openflags); if (!file) { THROWEXCEPTION("failed to reopen file %s", outputFilename.c_str()); perror("error:"); } timeval tvcurtime = unixtime(); time_t curtime = tvcurtime.tv_sec; char curtimestr[100]; ctime_r(&curtime, curtimestr); curtimestr[strlen(curtimestr)-1] = 0; fprintf(file, xmlpre, curtimestr, curtime, tvcurtime.tv_usec/1000, hostname); char text[100]; snprintf(text, 100, "%u", static_cast<uint32_t>(getpid())); fprintf(file, xmlglobals, "pid", text, "pid"); char lasttimestr[100]; ctime_r(&lasttime, lasttimestr); lasttimestr[strlen(lasttimestr)-1] = 0; fprintf(file, xmlglobals, "lastTime", lasttimestr, "lastTime"); #if defined(__linux__) const char* xmlglobalsuint = "\t\t<%s>%u</%s>\n"; ThreadCPUInterface::SystemInfo si = ThreadCPUInterface::getSystemInfo(); fprintf(file, "\t\t<jiffyFrequency>%llu</jiffyFrequency>\n", (long long unsigned)hertzValue); fprintf(file, xmlglobalsuint, "processorAmount", si.noCPUs, "processorAmount"); for (uint16_t i=0; i<si.sysJiffies.size(); i++) { double sysutil = (si.sysJiffies[i]-lastSystemInfo.sysJiffies[i])/(static_cast<double>(curtime)-lasttime)/hertzValue*100; double userutil = (si.userJiffies[i]-lastSystemInfo.userJiffies[i])/(static_cast<double>(curtime)-lasttime)/hertzValue*100; fprintf(file, "\t\t<processor id=\"%u\"><util type=\"system\">%.2f</util><util type=\"user\">%.2f</util></processor>\n", i, sysutil, userutil); } fprintf(file, "\t\t<memory><free type=\"bytes\">%llu</free><total type=\"bytes\">%llu</total></memory>\n", (long long unsigned)si.freeMemory, (long long unsigned)si.totalMemory); lastSystemInfo = si; #endif //DPRINTF("*** sensor data at %s", ctime(&curtime)); Graph* g = graphIS->getGraph(); vector<CfgNode*> nodes = g->getNodes(); vector<CfgNode*>::iterator iter = nodes.begin(); while (iter != nodes.end()) { Cfg* cfg = (*iter)->getCfg(); Sensor* s = cfg->getInstance(); vector<uint32_t> nextids = cfg->getNext(); writeSensorXML(file, s, cfg->getName().c_str(), cfg->getID(), true, curtime, lasttime, &nextids); iter++; } // iterate through all non-module sensors mutex.lock(); list<SensorEntry>::const_iterator siter = sensors.begin(); while (siter != sensors.end()) { //DPRINTFL(MSG_ERROR, "non-module cfg->getName()=%s, s=%u", siter->name.c_str(), siter->sensor); writeSensorXML(file, siter->sensor, siter->name.c_str(), siter->id, false, curtime, lasttime, NULL); siter++; } mutex.unlock(); fprintf(file, "%s", xmlpost); fclose(file); if (haveGraphLock) graphIS->unlockGraph(); }
void Application::uploadProfilePhoto(const QImage &tosend, const PeerId &peerId) { PreparedPhotoThumbs photoThumbs; QVector<MTPPhotoSize> photoSizes; QPixmap thumb = QPixmap::fromImage(tosend.scaled(160, 160, Qt::KeepAspectRatio, Qt::SmoothTransformation)); photoThumbs.insert('a', thumb); photoSizes.push_back(MTP_photoSize(MTP_string("a"), MTP_fileLocationUnavailable(MTP_long(0), MTP_int(0), MTP_long(0)), MTP_int(thumb.width()), MTP_int(thumb.height()), MTP_int(0))); QPixmap medium = QPixmap::fromImage(tosend.scaled(320, 320, Qt::KeepAspectRatio, Qt::SmoothTransformation)); photoThumbs.insert('b', medium); photoSizes.push_back(MTP_photoSize(MTP_string("b"), MTP_fileLocationUnavailable(MTP_long(0), MTP_int(0), MTP_long(0)), MTP_int(medium.width()), MTP_int(medium.height()), MTP_int(0))); QPixmap full = QPixmap::fromImage(tosend); photoThumbs.insert('c', full); photoSizes.push_back(MTP_photoSize(MTP_string("c"), MTP_fileLocationUnavailable(MTP_long(0), MTP_int(0), MTP_long(0)), MTP_int(full.width()), MTP_int(full.height()), MTP_int(0))); QByteArray jpeg; QBuffer jpegBuffer(&jpeg); full.save(&jpegBuffer, "JPG", 87); PhotoId id = MTP::nonce<PhotoId>(); MTPPhoto photo(MTP_photo(MTP_long(id), MTP_long(0), MTP_int(MTP::authedId()), MTP_int(unixtime()), MTP_string(""), MTP_geoPointEmpty(), MTP_vector<MTPPhotoSize>(photoSizes))); QString file, filename; int32 filesize = 0; QByteArray data; ReadyLocalMedia ready(ToPreparePhoto, file, filename, filesize, data, id, id, peerId, photo, photoThumbs, MTP_documentEmpty(MTP_long(0)), jpeg, false); connect(App::uploader(), SIGNAL(photoReady(MsgId, const MTPInputFile &)), App::app(), SLOT(photoUpdated(MsgId, const MTPInputFile &)), Qt::UniqueConnection); MsgId newId = clientMsgId(); App::app()->regPhotoUpdate(peerId, newId); App::uploader()->uploadMedia(newId, ready); }
void AddParticipantInner::selectSkip(int32 dir) { _time = unixtime(); _mouseSel = false; int32 rh = st::profileListPhotoSize + st::profileListPadding.height() * 2, origDir = dir; if (_filter.isEmpty()) { if (_sel) { if (dir > 0) { while (dir && _sel->next->next) { _sel = _sel->next; --dir; } while (contactData(_sel)->inchat && _sel->next->next) { _sel = _sel->next; } if (contactData(_sel)->inchat) { while (contactData(_sel)->inchat && _sel->prev) { _sel = _sel->prev; } } } else { while (dir && _sel->prev) { _sel = _sel->prev; ++dir; } while (contactData(_sel)->inchat && _sel->prev) { _sel = _sel->prev; } if (contactData(_sel)->inchat) { while (contactData(_sel)->inchat && _sel->next->next) { _sel = _sel->next; } } } } else if (dir > 0 && _contacts->list.count) { _sel = _contacts->list.begin; while (contactData(_sel)->inchat && _sel->next->next) { _sel = _sel->next; } } if (_sel) { if (contactData(_sel)->inchat) { _sel = 0; } else { emit mustScrollTo(_sel->pos * rh, (_sel->pos + 1) * rh); } } } else { if (dir > 0) { if (_filteredSel < 0 && dir > 1) { _filteredSel = 0; } _filteredSel += dir; while (_filteredSel < _filtered.size() - 1 && contactData(_filtered[_filteredSel])->inchat) { ++_filteredSel; } if (_filteredSel >= _filtered.size()) { _filteredSel = _filtered.size() - 1; } while (_filteredSel > 0 && contactData(_filtered[_filteredSel])->inchat) { --_filteredSel; } } else if (_filteredSel > 0) { _filteredSel += dir; if (_filteredSel < 0) { _filteredSel = 0; } if (_filteredSel < _filtered.size() - 1) { while (_filteredSel > 0 && contactData(_filtered[_filteredSel])->inchat) { --_filteredSel; } } while (_filteredSel < _filtered.size() - 1 && contactData(_filtered[_filteredSel])->inchat) { ++_filteredSel; } } if (_filteredSel >= 0) { if (contactData(_filtered[_filteredSel])->inchat) { _filteredSel = -1; } else { emit mustScrollTo(_filteredSel * rh, (_filteredSel + 1) * rh); } } } parentWidget()->update(); }
void AddParticipantInner::updateFilter(QString filter) { _time = unixtime(); QStringList f; if (!filter.isEmpty()) { QStringList filterList = filter.split(cWordSplit(), QString::SkipEmptyParts); int l = filterList.size(); f.reserve(l); for (int i = 0; i < l; ++i) { QString filterName = filterList[i].trimmed(); if (filterName.isEmpty()) continue; f.push_back(filterName); } filter = f.join(' '); } if (_filter != filter) { int32 rh = (st::profileListPhotoSize + st::profileListPadding.height() * 2); _filter = filter; if (_filter.isEmpty()) { resize(width(), _contacts->list.count * rh); if (_contacts->list.count) { _sel = _contacts->list.begin; while (_sel->next->next &&& contactData(_sel)->inchat) { _sel = _sel->next; } } } else { QStringList::const_iterator fb = f.cbegin(), fe = f.cend(), fi; _filtered.clear(); if (!f.isEmpty()) { DialogsList *dialogsToFilter = 0; if (_contacts->list.count) { for (fi = fb; fi != fe; ++fi) { DialogsIndexed::DialogsIndex::iterator i = _contacts->index.find(fi->at(0)); if (i == _contacts->index.cend()) { dialogsToFilter = 0; break; } if (!dialogsToFilter || dialogsToFilter->count > i.value()->count) { dialogsToFilter = i.value(); } } } if (dialogsToFilter && dialogsToFilter->count) { _filtered.reserve(dialogsToFilter->count); for (DialogRow *i = dialogsToFilter->begin, *e = dialogsToFilter->end; i != e; i = i->next) { const PeerData::Names &names(i->history->peer->names); PeerData::Names::const_iterator nb = names.cbegin(), ne = names.cend(), ni; for (fi = fb; fi != fe; ++fi) { QString filterName(*fi); for (ni = nb; ni != ne; ++ni) { if (ni->startsWith(*fi)) { break; } } if (ni == ne) { break; } } if (fi == fe) { i->attached = 0; _filtered.push_back(i); } } } } _filteredSel = _filtered.isEmpty() ? -1 : 0; while (_filteredSel < _filtered.size() - 1 && contactData(_filtered[_filteredSel])->inchat) { ++_filteredSel; } resize(width(), _filtered.size() * rh); } if (parentWidget()) parentWidget()->update(); loadProfilePhotos(0); } }