Skip to content

Commit 2bd39c9

Browse files
Merge pull request #338 from jeffreykirchner/dev
Refactor chat text processing and display logic
2 parents 107cd6d + 21a89cd commit 2bd39c9

File tree

4 files changed

+62
-33
lines changed

4 files changed

+62
-33
lines changed

main/consumers/staff/session_consumer_mixins/subject_updates.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55

66
from asgiref.sync import sync_to_async
77
from decimal import Decimal
8-
from textwrap import TextWrapper
98

109
from django.db import transaction
11-
from django.db.models.fields.json import KT
10+
from django.utils.html import strip_tags
1211

1312
from main.models import SessionPlayer
1413
from main.models import Session
1514
from main.models import SessionEvent
16-
from django.utils.decorators import method_decorator
1715

1816
from datetime import datetime, timedelta
1917

@@ -68,14 +66,14 @@ async def chat(self, event):
6866
session_player = self.world_state_avatars_local["session_players"][str(player_id)]
6967
session_player["current_location"] = current_location
7068

71-
result["text"] = event_data["text"]
72-
result["text_limited"] = await self.do_limited_chat(event_data["text"])
69+
result["text"] = strip_tags(event_data["text"])
70+
result["text_limited"] = await self.do_limited_chat(strip_tags(event_data["text"]))
7371
result["nearby_players"] = []
7472

7573
#format text for chat bubbles
76-
wrapper = TextWrapper(width=13, max_lines=6)
77-
result['text'] = wrapper.fill(text=result['text'])
78-
result['text_limited'] = wrapper.fill(text=result['text_limited'])
74+
# wrapper = TextWrapper(width=13, max_lines=6)
75+
# result['text'] = wrapper.fill(text=result['text'])
76+
# result['text_limited'] = wrapper.fill(text=result['text_limited'])
7977

8078
#find nearby players
8179
session_players = self.world_state_avatars_local["session_players"]

main/templates/subject/subject_home/chat/chat_card.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,33 @@ send_chat: function send_chat(){
22

33
if(app.working) return;
44
if(app.chat_text.trim() == "") return;
5-
if(app.chat_text.trim().length > 200) return;
6-
if(app.session.world_state.avatars[app.session_player.id].sleeping) return;
5+
if(app.chat_text.trim().length > 100) return;
6+
7+
let chat_bubble_sprite = PIXI.Sprite.from(app.pixi_textures.sprite_sheet_2.textures["chat_bubble.png"]);
8+
let style = new PIXI.TextStyle({fontFamily : 'Arial',
9+
fontSize: 18,
10+
fill :0x000000,
11+
align : 'left',
12+
wordWrap: true,
13+
wordWrapWidth: chat_bubble_sprite.width-25,
14+
breakWords: true})
15+
const textMetrics = PIXI.CanvasTextMetrics.measureText(app.chat_text.trim(), style)
16+
17+
let chat_text_processed ="";
18+
19+
for(let i=0; i<textMetrics.lines.length; i++)
20+
{
21+
chat_text_processed += textMetrics.lines[i];
22+
23+
if(i==5 || i==textMetrics.lines.length-1) break;
24+
25+
chat_text_processed += "\n";
26+
}
27+
28+
if(textMetrics.lines.length > 6)
29+
{
30+
chat_text_processed += "...";
31+
}
732

833
if(app.session.world_state.current_experiment_phase == 'Instructions')
934
{
@@ -13,7 +38,7 @@ send_chat: function send_chat(){
1338
{
1439
app.working = true;
1540
app.send_message("chat",
16-
{"text" : app.chat_text.trim(),
41+
{"text" : chat_text_processed,
1742
"current_location" : app.session.world_state_avatars.session_players[app.session_player.id].current_location,},
1843
"group");
1944
}

main/templates/subject/subject_home/the_stage/avatars.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ setup_pixi_subjects: function setup_pixi_subjects()
278278
chat_container.addChild(chat_bubble_sprite);
279279
chat_container.addChild(chat_bubble_text);
280280

281-
chat_bubble_text.position.set(-14 * app.session.parameter_set.avatar_scale, -chat_container.height*.09)
281+
chat_bubble_text.position.set(app.session.parameter_set.avatar_scale, -chat_container.height*.085)
282282
chat_bubble_text.anchor.set(0.5);
283283

284284
pixi_avatars[i].chat_container = chat_container;

main/templates/subject/subject_home/the_stage/the_stage_card.html

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -100,27 +100,33 @@
100100
</button>
101101
</div>
102102

103-
<div class="col-md-4">
104-
<span class="input-group">
105-
<input type="text"
106-
class="form-control"
107-
placeholder="Chat Text"
108-
v-model="chat_text"
109-
aria-label="Chat Text"
110-
v-on:keyup.enter="send_chat()"
111-
aria-describedby="send_chat_id">
112-
<button class="btn btn-outline-primary"
113-
type="button"
114-
id="send_chat_id"
115-
title="Send Chat"
116-
v-bind:disabled="reconnecting ||
117-
working ||
118-
session.started===false ||
119-
session.world_state.finished ||
120-
session.world_state.avatars[session_player.id].sleeping"
121-
v-on:click="send_chat()">Chat <i class="far fa-comments"></i>
122-
</button>
123-
</span>
103+
<div class="col-4">
104+
<div class="row">
105+
<div class="col">
106+
<div class="input-group">
107+
<input type="text"
108+
class="form-control form-control-lg"
109+
placeholder="Chat Text"
110+
v-model="chat_text"
111+
id="id_chat_text"
112+
aria-label="Chat Text"
113+
v-on:keyup.enter="send_chat()"
114+
maxlength="100"
115+
aria-describedby="send_chat_id">
116+
<button class="btn btn-outline-primary btn-lg"
117+
type="button"
118+
id="send_chat_id"
119+
v-bind:disabled="reconnecting || working || session.started===false || session.world_state.finished"
120+
v-on:click="send_chat()">Chat <i class="far fa-comments"></i></button>
121+
</div>
122+
</div>
123+
</div>
124+
125+
<div class="row">
126+
<div class="col text-end pe-4 text-secondary">
127+
[[chat_text.length]]/100
128+
</div>
129+
</div>
124130
</div>
125131

126132
<div class="col-md-4">

0 commit comments

Comments
 (0)