|
|
@@ -52,81 +52,99 @@ export class Conversation {
|
|
|
this.recipient.conversations[this.id] = this;
|
|
|
}
|
|
|
if (value === Conversation.STATE_RUNNING) {
|
|
|
-
|
|
|
- if (this.interval === null) {
|
|
|
- let lastIntervalTick = Date.now();
|
|
|
- this.interval = setInterval(() => {
|
|
|
- let now = Date.now();
|
|
|
- let interval = (now - lastIntervalTick) / 1000;
|
|
|
- this.duration += interval;
|
|
|
- lastIntervalTick = now;
|
|
|
- this.initiator.payedTime -= Math.ceil(interval * this.recipient.coeficient);
|
|
|
- this.recipient.payedTime += Math.ceil(interval * this.recipient.coeficient);
|
|
|
- if (this.initiator.payedTime < 0) {
|
|
|
- this.state = Conversation.STATE_STOPPED;
|
|
|
- }
|
|
|
- }, 1000);
|
|
|
- }
|
|
|
- if (this.syncInterval === null) {
|
|
|
- this.syncInterval = setInterval(() => {
|
|
|
- this.initiator.send('chat-sync-timer', {
|
|
|
- conversationId: this.id,
|
|
|
- duration: this.duration
|
|
|
- });
|
|
|
-
|
|
|
- this.recipient.send('chat-sync-timer', {
|
|
|
- conversationId: this.id,
|
|
|
- duration: this.duration
|
|
|
- });
|
|
|
-
|
|
|
- this.app.apiCall('/conversations', {
|
|
|
- id: this.id,
|
|
|
- action: 'duration',
|
|
|
- duration: this.duration
|
|
|
- });
|
|
|
-
|
|
|
- this.app.apiCall('/user', {
|
|
|
- action: 'updatetime',
|
|
|
- id: this.initiator.id,
|
|
|
- value: this.initiator.payedTime
|
|
|
- });
|
|
|
-
|
|
|
- this.app.apiCall('/user', {
|
|
|
- action: 'updatetime',
|
|
|
- id: this.recipient.id,
|
|
|
- value: this.recipient.payedTime
|
|
|
- });
|
|
|
- }, 5000);
|
|
|
- }
|
|
|
-
|
|
|
- this.app.apiCall('/conversations', {id: this.id, action: 'start'});
|
|
|
+ this.setRunning();
|
|
|
}
|
|
|
if (value === Conversation.STATE_STOPPED || value === Conversation.STATE_PAUSED) {
|
|
|
- clearInterval(this.interval);
|
|
|
- this.interval = null;
|
|
|
- clearInterval(this.syncInterval);
|
|
|
- this.syncInterval = null;
|
|
|
- this.initiator.send('chat-conversation-stop', {id: this.id});
|
|
|
- this.recipient.send('chat-conversation-stop', {id: this.id});
|
|
|
+ this.setPaused();
|
|
|
}
|
|
|
if (value === Conversation.STATE_STOPPED) {
|
|
|
- delete this.initiator.conversations[this.id];
|
|
|
- delete this.recipient.conversations[this.id];
|
|
|
- delete this.app.conversations[this.id];
|
|
|
- this.app.apiCall('/conversations', {id: this.id, action: 'stop', duration: this.duration});
|
|
|
- this.app.apiCall('/user', {
|
|
|
- action: 'updatetime',
|
|
|
- id: this.initiator.id,
|
|
|
- value: this.initiator.payedTime
|
|
|
- });
|
|
|
- this.app.apiCall('/user', {
|
|
|
- action: 'updatetime', id: this.recipient.id,
|
|
|
- value: this.recipient.payedTime
|
|
|
- });
|
|
|
+ this.setStopped();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private setRunning()
|
|
|
+ {
|
|
|
+ if (this.interval === null) {
|
|
|
+ let lastIntervalTick = Date.now();
|
|
|
+ this.interval = setInterval(() => {
|
|
|
+ let now = Date.now();
|
|
|
+ let interval = (now - lastIntervalTick) / 1000;
|
|
|
+ this.duration += interval;
|
|
|
+ lastIntervalTick = now;
|
|
|
+ this.initiator.payedTime -= Math.ceil(interval * this.recipient.coeficient);
|
|
|
+ this.recipient.payedTime += Math.ceil(interval * this.recipient.coeficient);
|
|
|
+ if (this.initiator.payedTime < 0) {
|
|
|
+ this.state = Conversation.STATE_STOPPED;
|
|
|
+ }
|
|
|
+ }, 1000);
|
|
|
+ }
|
|
|
+ if (this.syncInterval === null) {
|
|
|
+ this.syncInterval = setInterval(() => {
|
|
|
+ this.sync();
|
|
|
+ }, 5000);
|
|
|
+ }
|
|
|
+
|
|
|
+ this.app.apiCall('/conversations', {id: this.id, action: 'start'});
|
|
|
+ }
|
|
|
+
|
|
|
+ private sync() {
|
|
|
+ this.initiator.send('chat-sync-timer', {
|
|
|
+ conversationId: this.id,
|
|
|
+ duration: this.duration
|
|
|
+ });
|
|
|
+
|
|
|
+ this.recipient.send('chat-sync-timer', {
|
|
|
+ conversationId: this.id,
|
|
|
+ duration: this.duration
|
|
|
+ });
|
|
|
+
|
|
|
+ this.app.apiCall('/conversations', {
|
|
|
+ id: this.id,
|
|
|
+ action: 'duration',
|
|
|
+ duration: this.duration
|
|
|
+ });
|
|
|
+
|
|
|
+ this.app.apiCall('/user', {
|
|
|
+ action: 'updatetime',
|
|
|
+ id: this.initiator.id,
|
|
|
+ value: this.initiator.payedTime
|
|
|
+ });
|
|
|
+
|
|
|
+ this.app.apiCall('/user', {
|
|
|
+ action: 'updatetime',
|
|
|
+ id: this.recipient.id,
|
|
|
+ value: this.recipient.payedTime
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private setPaused()
|
|
|
+ {
|
|
|
+ clearInterval(this.interval);
|
|
|
+ this.interval = null;
|
|
|
+ clearInterval(this.syncInterval);
|
|
|
+ this.syncInterval = null;
|
|
|
+ this.initiator.send('chat-conversation-stop', {id: this.id});
|
|
|
+ this.recipient.send('chat-conversation-stop', {id: this.id});
|
|
|
+ }
|
|
|
+
|
|
|
+ private setStopped()
|
|
|
+ {
|
|
|
+ delete this.initiator.conversations[this.id];
|
|
|
+ delete this.recipient.conversations[this.id];
|
|
|
+ delete this.app.conversations[this.id];
|
|
|
+ this.app.apiCall('/conversations', {id: this.id, action: 'stop', duration: this.duration});
|
|
|
+ this.app.apiCall('/user', {
|
|
|
+ action: 'updatetime',
|
|
|
+ id: this.initiator.id,
|
|
|
+ value: this.initiator.payedTime
|
|
|
+ });
|
|
|
+ this.app.apiCall('/user', {
|
|
|
+ action: 'updatetime', id: this.recipient.id,
|
|
|
+ value: this.recipient.payedTime
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
get state() {
|
|
|
return this.conversationState();
|
|
|
}
|