-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChatLog.gd
167 lines (132 loc) · 4.18 KB
/
ChatLog.gd
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
extends PanelContainer
var chatmessage = preload("res://OpenSeed-Godot/MessageBox.tscn")
var box
var offset = 0
var key = ""
var room = ""
var SocialRoot
var currentuser = ""
var OpenSeed
var Thicket
var last = 0
signal change_user(currentuser)
var history_retrieved = false
var chat_history = {}
func _ready():
OpenSeed = get_node("/root/OpenSeed")
Thicket = get_node("/root/Thicket")
OpenSeed.connect("chatdata",self,"update_chat")
OpenSeed.connect("sent_chat",self,"chatbox_reset")
OpenSeed.connect("chat_history",self,"fast_chat_update")
OpenSeed.connect("conversations",self,"_on_conversations_update")
OpenSeed.connect("keydata",self,"key_recieved")
box = $VBoxContainer/ScrollContainer/VBoxContainer
$Timer.start()
if SocialRoot:
SocialRoot.connect("changeview",self,"_on_account_view")
func get_key():
if room != "":
OpenSeed.get_keys_for(OpenSeed.username+","+currentuser,room)
func _on_Timer_timeout():
if key != "" and key != "denied":
#print("from timeout:updating")
if currentuser and OpenSeed.username != currentuser:
room = OpenSeed.find_by_attendees([currentuser,OpenSeed.username])
# print("from timeout: "+room)
#print("from timeout: "+key)
if room != "" and history_retrieved == false :
#print("getting chat history")
OpenSeed.openSeedRequest("get_chat_history",[room,10,0])
else:
#print("getting chat")
OpenSeed.openSeedRequest("get_chat",[room,last])
func update_chat(data):
if key != "" and key != "denied":
var json = data
if json.has("index") and json["index"] != "-1":
var decrypted_message = OpenSeed.simp_decrypt(key,json["message"])
if decrypted_message != "no key":
box.get_parent().set_v_scroll(box.rect_size.y)
var newmessage = chatmessage.instance()
newmessage.date = json["date"]
newmessage.speaker = json["speaker"]
newmessage.message = decrypted_message
box.add_child(newmessage)
last = json["index"]
elif json and json.has("speaker"):
if json["speaker"] == "server":
match json["message"]:
"none":
$Timer.wait_time = 40
else:
$Timer.wait_time = 200
func _on_message_text_entered(new_text):
var returned = ""
if room != "":
if key:
OpenSeed.send_chat(OpenSeed.simp_crypt(key,new_text),room)
else:
print("no Key")
else:
print("no room")
func _on_account_view(account):
if account != OpenSeed.username:
self.show()
else:
self.hide()
func chatbox_reset(data):
$VBoxContainer/InputArea/message.text = ""
if data:
OpenSeed.openSeedRequest("get_chat",[room,last])
#else:
#_on_message_text_entered(text)
pass
func fast_chat_update(data):
print("from Fast Update")
var dat = data
if typeof(dat) == TYPE_ARRAY:
for line in dat:
#print(line)
update_chat(line)
history_retrieved = true
pass
func _on_ChatLog_change_user(current):
currentuser = current
history_retrieved = false
chat_history = []
key = ""
while box.get_child_count() > 0:
var thechild = box.get_child(box.get_child_count() -1)
box.remove_child(thechild)
room = OpenSeed.find_by_attendees([current,OpenSeed.username])
get_key()
if room != "" and key != "":
OpenSeed.openSeedRequest("get_chat_history",[room,10,0])
func from_conversations(data):
for conv in data:
if conv["room"] == room:
pass
func _on_VBoxContainer_resized():
var the_bottom = box.get_parent().get_child(2).max_value
box.get_parent().set("vertical_scroll",the_bottom)
box.get_parent().set_v_scroll(the_bottom)
pass
func _on_ScrollContainer_scroll_ended():
print("Scroll position: "+str(box.get_parent().get_v_scroll()))
func _on_conversations_update(_data):
for chatroom in OpenSeed.conversations:
if history_retrieved == true:
if room == "":
if chatroom["attendees"] == OpenSeed.username+","+currentuser or chatroom["attendees"] == currentuser+","+OpenSeed.username:
room = chatroom["room"]
if int(last) <= int(chatroom["index"]):
OpenSeed.openSeedRequest("get_chat",[room,last])
break
else:
if chatroom["room"] == room:
if int(last) <= int(chatroom["index"]):
OpenSeed.openSeedRequest("get_chat",[room,last])
break
func key_recieved(data):
if typeof(data) == TYPE_DICTIONARY and data["room"] == room:
key = data["key"]