2019-05-13 21:07:57 -05:00
|
|
|
import {compose} from '@avocado/core';
|
2019-09-22 18:45:52 -05:00
|
|
|
import {SynchronizedMixin} from '@avocado/net';
|
2019-04-04 07:31:21 -05:00
|
|
|
import {Ticker} from '@avocado/timing';
|
|
|
|
|
2019-09-30 15:57:28 -05:00
|
|
|
import {WorldTimePacket} from './packets/world-time.packet';
|
2019-05-13 21:07:57 -05:00
|
|
|
|
2019-04-20 01:55:47 -05:00
|
|
|
const MAGIC_TO_FIT_HOUR_INTO_USHORT = 2730;
|
|
|
|
|
2019-05-13 21:07:57 -05:00
|
|
|
const decorate = compose(
|
2019-09-22 18:45:52 -05:00
|
|
|
SynchronizedMixin,
|
2019-05-13 21:07:57 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
export class WorldTime extends decorate(class {}) {
|
2019-04-04 07:31:21 -05:00
|
|
|
|
2019-09-22 18:45:52 -05:00
|
|
|
constructor(json) {
|
2019-05-13 21:07:57 -05:00
|
|
|
super();
|
2019-04-08 08:01:39 -05:00
|
|
|
this._hour = 0;
|
2019-05-13 21:07:57 -05:00
|
|
|
this._isUpdateReady = true;
|
|
|
|
this._lastHour = 0;
|
2019-04-23 15:26:56 -05:00
|
|
|
this.ticker = new Ticker(0.25);
|
2019-04-04 07:31:21 -05:00
|
|
|
this.ticker.on('tick', () => {
|
2019-05-13 21:07:57 -05:00
|
|
|
this._isUpdateReady = true;
|
2019-04-04 07:31:21 -05:00
|
|
|
});
|
2019-09-22 18:45:52 -05:00
|
|
|
if ('undefined' !== typeof json) {
|
|
|
|
this.fromJSON(json);
|
|
|
|
}
|
2019-04-04 07:31:21 -05:00
|
|
|
}
|
|
|
|
|
2019-05-13 21:07:57 -05:00
|
|
|
acceptPacket(packet) {
|
|
|
|
if (packet instanceof WorldTimePacket) {
|
2019-09-29 13:20:08 -05:00
|
|
|
this.fromNetwork(packet.data);
|
2019-05-13 21:07:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-22 18:45:52 -05:00
|
|
|
cleanPackets() {
|
|
|
|
super.cleanPackets();
|
|
|
|
this._isUpdateReady = false;
|
|
|
|
}
|
|
|
|
|
2019-04-12 11:01:55 -05:00
|
|
|
static format(time) {
|
|
|
|
const rawHour = (time + 12) % 12;
|
|
|
|
const hour = Math.floor(0 === Math.floor(rawHour) ? 12 : rawHour);
|
|
|
|
const minutes = String(Math.floor((rawHour - Math.floor(rawHour)) * 60)).padStart(2, '0');
|
|
|
|
if (time >= 12) {
|
|
|
|
return `${hour}:${minutes} pm`;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return `${hour}:${minutes} am`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-22 18:45:52 -05:00
|
|
|
fromJSON(json) {
|
|
|
|
if (json.hour) {
|
2019-09-29 13:20:08 -05:00
|
|
|
this._hour = json.hour / MAGIC_TO_FIT_HOUR_INTO_USHORT;
|
2019-09-22 18:45:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-08 08:01:39 -05:00
|
|
|
get hour() {
|
|
|
|
return this._hour;
|
|
|
|
}
|
|
|
|
|
|
|
|
set hour(hour) {
|
|
|
|
this._hour = hour;
|
2019-04-04 07:31:21 -05:00
|
|
|
}
|
|
|
|
|
2019-09-22 18:45:52 -05:00
|
|
|
packets(informed) {
|
|
|
|
if (!this._isUpdateReady) {
|
|
|
|
return;
|
2019-05-13 21:07:57 -05:00
|
|
|
}
|
2019-09-29 13:20:08 -05:00
|
|
|
return new WorldTimePacket(this.toNetwork(informed));
|
2019-04-05 15:16:50 -05:00
|
|
|
}
|
|
|
|
|
2019-04-04 07:31:21 -05:00
|
|
|
secondsPerHour() {
|
|
|
|
return 60;
|
|
|
|
}
|
|
|
|
|
|
|
|
tick(elapsed) {
|
2019-04-08 08:01:39 -05:00
|
|
|
this._hour += (elapsed / this.secondsPerHour());
|
|
|
|
this._hour = this._hour % 24;
|
2019-04-04 07:31:21 -05:00
|
|
|
this.ticker.tick(elapsed);
|
|
|
|
}
|
|
|
|
|
2019-09-29 13:20:08 -05:00
|
|
|
toJSON() {
|
2019-09-22 22:34:24 -05:00
|
|
|
return {
|
|
|
|
hour: (this._hour * MAGIC_TO_FIT_HOUR_INTO_USHORT) >> 0,
|
|
|
|
};
|
2019-09-22 18:45:52 -05:00
|
|
|
}
|
|
|
|
|
2019-04-04 07:31:21 -05:00
|
|
|
}
|