alexlcdee 8 rokov pred
rodič
commit
fc71a5373e
2 zmenil súbory, kde vykonal 86 pridanie a 68 odobranie
  1. 1 1
      src/App.ts
  2. 85 67
      src/Conversation.ts

+ 1 - 1
src/App.ts

@@ -127,10 +127,10 @@ class ClientsContainer {
                     let data = JSON.parse(response);
                     this.clients[data.id] = new Client(this.app, data);
                     this.clients[data.id].addSocket(socket);
-                    this.app.apiCall('/user', {id: data.id, action: 'setstatus', value: true});
                 });
         } else {
             this.clients[data.id].addSocket(socket);
+            this.app.apiCall('/user', {id: data.id, action: 'setstatus', value: true});
         }
     }
 

+ 85 - 67
src/Conversation.ts

@@ -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();
     }