-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameScript.cs
More file actions
1074 lines (900 loc) · 36.4 KB
/
Copy pathGameScript.cs
File metadata and controls
1074 lines (900 loc) · 36.4 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Photon.Realtime;
using Photon.Pun;
using TMPro;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using TMPro.Examples;
using System.Linq;
using System.IO;
using System.Text;
using UnityEngine.Networking;
using UnityEngine.SceneManagement;
namespace ICSI.FrameNet.FrameGame {
public class GameScript : MonoBehaviour
{
#region callback definitions
public delegate void OnReceivedCallback(string json);
#endregion
#region items in scene
// Uncomment if using photon
// [SerializeField] private PhotonView photonView;
// See editor for equivalent objects
[SerializeField] private TMP_Text PlayerText;
[SerializeField] private Camera mainCamera;
[SerializeField] private TMP_Text Result;
[SerializeField] private GameObject Field;
[SerializeField] private TMP_InputField WritingBox;
public TMP_InputField ReadingBox;
private CustomTextSelector customText;
public GameObject FrameDesc;
public GameObject FrameInfo;
public GameObject StoryBG;
public GameObject InfoBG;
public TextMeshProUGUI Story;
public TextMeshProUGUI Info;
public GameObject InfoTab;
// Frame Description
public TMP_Text frameName;
private TMP_Text frameCount;
private TMP_Text frameSaved;
// Info tab
public TMP_Text frameNameI;
public TMP_Text frameCountI;
public TMP_InputField descI;
// FE Description
private TMP_Text feName;
private TMP_Text feCount;
private TMP_Text feSaved;
// Alert boxes
public GameObject Alert;
public TMP_Text AlertText;
public GameObject Error;
public TMP_Text ErrorText;
// Save buttons
public GameObject UnlockedFE;
public GameObject LockedFE;
public GameObject UnlockedF;
public GameObject LockedF;
// text boxes
public GameObject WriteBoxObj;
public GameObject ReadBoxObj;
public GameObject Annotate;
public GameObject FrameMenu;
// Loading
public GameObject FrameBox;
public GameObject Loading;
public GameObject FELeft;
public GameObject FERight;
#endregion
#region helper variables
// Indices of frame user is looking at (info tab and editing tab are separate)
private int currentFrame = 0;
private int infoFrame = 0;
private int currentFE = 0;
// How many frames or fes the user has annotated in this session
private int completeFrame = 0;
private int completeFE = 0;
// The game object that stores the information about this session
// (See StoredInfo.cs)
private FGame game;
// Used to determine whether to display the annotating or info tab
private bool isInfo = false;
// Stores the official colors of frame elements
private Dictionary<string, Dictionary<string, Color>> FEColors;
// Stores whether or not an FE has been annotated
private Dictionary<FE, bool> isComplete = new Dictionary<FE, bool>();
// The current sentence that the user has been annotating
private AnnotatedSentence currentSentence;
// Object that stores information about the player
private PlayerInfo playerObject;
// Helper variable for preventing simultaenous fade in/fade out
private bool isFading = false;
// Helper variable for preventing users from navigating away
// while the frames load
private bool hasStarted = false;
// used to enable CustomTextSelector.cs in annotating mode
private bool isAnnotating = false;
// the unedited text after the user saves their story
// TODO: save story in backend
string story;
// highlighting
// Dictionary is organized first by frame name
// within each entry, there is a dictionary organized by FE name
// each entry of the sub-dictionary has start/end tag as well as start/end in marked up version
public Dictionary<string, Dictionary<string, ((int, int), (int, int))>> highlights = new Dictionary<string, Dictionary<string, ((int, int), (int, int))>>();
#endregion
#region example initializations
// Just an example of how an FGame (see StoredInfo.cs) can be initialized
// from scratch. Unused in final version but kept for reference.
private FGame createTestGame()
{
//FEs
FE target = new FE("Target", "");
FE co_part = new FE("Co-participant", "The Co-participant is the accompanying entity (person or object).", null, new string[1]{"Participants"});
FE part = new FE("Participant", "The Participant is the accompanied entity (person or object).", null, new string[1]{"Participants"});
FE parts = new FE("Participants", "Two or more entities construed as symmetrically and usually equally participating in an event or relation.");
AnnotatedSentence sent1 = new AnnotatedSentence("The mayor was killed ALONG WITH three bodyguards and his driver.", new Dictionary<string, (int, int)>());
sent1.labels["Target"] = (21, 10);
sent1.labels["Co-participant"] = (32, 31);
sent1.labels["Participant"] = (0,9);
// Debug.Log(sent1.text.Substring(sent1.labels["Target"].Item1, sent1.labels["Target"].Item2));
// Debug.Log(sent1.text.Substring(sent1.labels["Co-participant"].Item1, sent1.labels["Co-participant"].Item2));
AnnotatedSentence sent2 = new AnnotatedSentence("The doctor told me to take my regular pill IN COMBINATION with the new drug and I will be cured of my symptoms.");
sent2.labels["Co-participant"] = (27, 16);
sent2.labels["Participant"] = (58,18);
sent2.labels["Target"] = (43, 14);
// Debug.Log(sent2.text.Substring(sent2.labels["Target"].Item1, sent2.labels["Target"].Item2));
// Debug.Log(sent2.text.Substring(sent2.labels["Participant"].Item1, sent2.labels["Participant"].Item2));
// Debug.Log(sent2.text.Substring(sent2.labels["Co-participant"].Item1,sent2.labels["Co-participant"].Item2));
AnnotatedSentence sent3 = new AnnotatedSentence("Lao Tzu and Confucious built the house TOGETHER.");
sent3.labels["Participants"] = (0,22);
sent3.labels["Target"] = (39, 8);
// Debug.Log(sent3.text.Substring(sent3.labels["Target"].Item1, sent3.labels["Target"].Item2));
// Debug.Log(sent3.text.Substring(sent3.labels["Participants"].Item1, sent3.labels["Participants"].Item2));
AnnotatedSentence[] examples = new AnnotatedSentence[3]{sent1, sent2, sent3};
FE[] fes = new FE[4]{target, co_part, part, parts};
string[] LUs = new string[6]{"alone", "along with", "in combination", "singly", "together", "with"};
Frame Accompaniment = new Frame("Accompaniment", "258", "A Co-participant fills the same role as the Participant in an event or relation.", examples, LUs, null, fes);
// Game
FGame game = new FGame(new Frame[1]{Accompaniment});
return game;
}
#endregion
#region actual initializations
// Query the backend to get the array of frames
private void GetFrames()
{
StartCoroutine(GetRequest("https://frame-game-backend.herokuapp.com/lookup/!", CreateFrameArray));
}
// Parses the array of frames from the backend
private void CreateFrameArray(string jsonData)
{
FEColors = new Dictionary<string, Dictionary<string, Color>>();
object resultValue = JsonUtility.FromJson<FrameList>(jsonData);
FrameList flist = (FrameList)Convert.ChangeType(resultValue, typeof(FrameList));
Frame[] frames = new Frame[flist.Frames.Length];
for(int j = 0; j < flist.Frames.Length; j++)
{
FrameInfo f = flist.Frames[j];
AnnotatedSentence[] examples = new AnnotatedSentence[f.examples.Length];
for(int i = 0; i < f.examples.Length; i++)
{
Example ex = f.examples[i];
AnnotatedSentence annot = new AnnotatedSentence(ex.text);
annot.labels = new Dictionary<string, (int, int)>();
for(int k = 0; k < ex.labels.Length; k++)
{
Label entry = ex.labels[k];
annot.labels[entry.title] = (entry.start, entry.length);
}
examples[i] = annot;
}
FE[] FEs = new FE[f.FEs.Length+1];
FEs[0] = new FE("Target", f.name);
FEColors[f.name] = new Dictionary<string, Color>();
for(int i = 1; i < f.FEs.Length+1; i++)
{
FEInfo inf = f.FEs[i-1];
FE fe = new FE(inf.name, inf.description, inf.requires, inf.excludes);
FEs[i] = fe;
Color c;
if (!ColorUtility.TryParseHtmlString("#" + inf.color + "FF", out c))
{
c = UnityEngine.Random.ColorHSV(0f, 1.0f, 1.0f, 1.0f, 0.75f, 1.0f, 0.25f, 0.25f);
}
c.a = 0.25f;
FEColors[f.name][fe.name] = c;
}
frames[j] = new Frame(f.name, f.ID, f.def, examples, f.LUs, f.allLUs, FEs);
}
FGame fGame = new FGame(frames);
game = fGame;
Loading.SetActive(false);
FrameBox.SetActive(true);
FrameMenu.SetActive(true);
InfoTab.SetActive(true);
hasStarted = true;
PlayGame();
}
#endregion
#region basic functions
// Initialize components
void Awake()
{
string PID = PlayerPrefs.GetString("player_id");
StartCoroutine(GetRequest("https://frame-game-backend.herokuapp.com/players/get/"+PID, UpdatePlayerInfo));
customText = Field.GetComponent<CustomTextSelector>();
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
// Debug.Log("currentFrame: " + currentFrame + " currentFE: " + currentFE);
if(isAnnotating && hasStarted && game.frames.Length > 0 && game.frames[currentFrame].FEs.Length > 0 && !isComplete[game.frames[currentFrame].FEs[currentFE]])
{
customText.CheckAndSelect();
}
}
// Helper callbacks for getting and saving
private void UpdatePlayerInfo(string jsonData)
{
object resultValue = JsonUtility.FromJson<PlayerInfo>(jsonData);
playerObject = (PlayerInfo)Convert.ChangeType(resultValue, typeof(PlayerInfo));
PlayerText.text = playerObject.username + "\'s Cafe";
// Debug.Log(playerObject.id);
GetFrames();
}
// Load view of other's sentences
public void onViewModeEnable()
{
if(hasStarted)
{
SceneManager.LoadScene("ViewerMode");
} else {
ErrorText.text = "Cannot switch modes while frames are still loading.";
if(!isFading)
{
StartFadeIn(Error);
StartTextFadeIn(Error, ErrorText);
StartFadeOut(Error);
StartTextFadeOut(Error, ErrorText);
}
}
}
// Load homepage
public void onStartEnable()
{
if(hasStarted)
{
SceneManager.LoadScene("StartScreen");
} else {
ErrorText.text = "Cannot switch modes while frames are still loading.";
if(!isFading)
{
StartFadeIn(Error);
StartTextFadeIn(Error, ErrorText);
StartFadeOut(Error);
StartTextFadeOut(Error, ErrorText);
}
}
}
#endregion
#region change display text
// Used to adjust the text after the user increments/decrements
// frames/frame elements in annotating mode
void UpdateText()
{
if(!isAnnotating)
{
frameName.text = (game.frames[currentFrame]).name;
} else if(!isInfo && game.frames.Length > 0)
{
(GameObject.Find("FELeft")).SetActive(true);
(GameObject.Find("FERight")).SetActive(true);
frameName.text = (game.frames[currentFrame]).name;
frameCount.text = "" + (currentFrame+1) +"/"+ game.frames.Length;
frameSaved.text = "" + completeFrame + " done";
FE fe = game.frames[currentFrame].FEs[currentFE];
Dictionary<string, Color> colors = FEColors[(game.frames[currentFrame]).name];
string nameFE = fe.name;
if(nameFE != "Target")
{
string color = ColorUtility.ToHtmlStringRGBA(colors[nameFE]);
feName.text = "<mark=#" + color + ">" + nameFE + "</mark>";
} else
{
feName.text = "<b>" + nameFE + "</b>";
}
feCount.text = "" + (currentFE+1) +"/"+ game.frames[currentFrame].FEs.Length;
feSaved.text = "" + completeFE + " done";
// if the frame element is locked in, do not let user select
// different text, change color
if(isComplete[fe])
{
(int, int) indices = currentSentence.labels[fe.name];
if(indices.Item1 >= 0)
{
Result.text = (currentSentence.text).Substring(indices.Item1, indices.Item2);
} else {
Result.text = "Not in sentence";
}
if(fe.name != "Target")
{
Result.color = FEColors[(game.frames[currentFrame]).name][fe.name];
Color c = Result.color;
c.a = 1.0f;
c.r += 0.3f;
c.g += 0.3f;
c.b += 0.3f;
Result.color = c;
} else {
Result.text = "<b>" + Result.text + "</b>";
Result.color = Color.white;
}
UnlockedFE.SetActive(false);
LockedFE.SetActive(true);
} else {
UnlockedFE.SetActive(true);
LockedFE.SetActive(false);
Result.color = Color.white;
}
} else
{
frameName.text = "All Frames Completed";
frameCount.text = "";
frameSaved.text = "" + completeFrame + " done";
Result.text = "Not in sentence.";
Result.color = Color.white;
feCount.text = "";
feSaved.text = "";
UnlockedFE.SetActive(false);
LockedFE.SetActive(false);
FELeft.SetActive(false);
FERight.SetActive(false);
}
}
// Used to display the frame information page when the user increments or
// decrements the info page
void UpdateInfo()
{
if(game.frames.Length > 0)
{
frameNameI.text = (game.frames[infoFrame]).name;
frameCountI.text = "" + (infoFrame+1) +"/"+ game.frames.Length;
string desc = "<u><b>Examples</b></u>\n";
Dictionary<string, Color> colors = FEColors[(game.frames[infoFrame]).name];
string last_example = "";
try {
foreach(AnnotatedSentence a in game.frames[infoFrame].examples)
{
List<(int, int)> shifts = new List<(int, int)>();
string sent = a.text;
string original = a.text;
last_example = a.text;
int totalShift = 0;
foreach(KeyValuePair<string, (int,int)> entry in a.labels)
{
bool isCore = false;
foreach(FE fe in game.frames[infoFrame].FEs)
{
if(fe.name == entry.Key) isCore = true;
}
if(isCore && entry.Key != "Target" && entry.Key != "Support")
{
int start = entry.Value.Item1+totalShift;
int len = entry.Value.Item2;
string color = ColorUtility.ToHtmlStringRGBA(colors[entry.Key]);
sent = sent.Insert(start, "<mark=#" + color + ">");
sent = sent.Insert(start+len+("<mark=#" + color + ">").Length, "</mark>");
totalShift += ("<mark=#" + color + ">").Length + "</mark>".Length;
}
}
if(a.labels.Keys.Contains("Target"))
{
string targetText = original.Substring(a.labels["Target"].Item1, a.labels["Target"].Item2);
sent = sent.Insert(sent.IndexOf(targetText), "<b>");
sent = sent.Insert(sent.IndexOf(targetText)+targetText.Length, "</b>");
}
if(a.labels.Keys.Contains("Support"))
{
string supportText = original.Substring(a.labels["Support"].Item1, a.labels["Support"].Item2);
sent = sent.Insert(sent.IndexOf(supportText), "<i>");
sent = sent.Insert(sent.IndexOf(supportText)+supportText.Length, "</i>");
}
desc += sent + "\n\n";
}
desc += "\n<u><b>Target</b></u>\n";
desc += "The target is the word or phrase that evokes this frame. Acceptable targets for this frame include:\n\n";
foreach(string LU in game.frames[infoFrame].LUs)
{
desc += LU + "\n";
}
desc += "\n<u><b>Frame Elements</b></u>\n";
foreach(FE fe in game.frames[infoFrame].FEs)
{
if(fe.name != "Target")
{
string color = ColorUtility.ToHtmlStringRGBA(colors[fe.name]);
desc += "<mark=#" + color + ">" + fe.name + "</mark>" + ": " + fe.description +"\n\n";
}
}
desc += "\n<u><b>Description</b></u>\n" + game.frames[infoFrame].def + "\n";
descI.text = desc;
} catch (Exception e) {
descI.text = "Sorry, this frame is currently unavailable. Please see the FrameNet frame index <u><b><link=\"/fndrupal/frameIndex\">here</link></b></u>.";
ErrorInfo ei = new ErrorInfo();
ei.last_text = last_example;
ei.error_message = e.Message;
ei.error_source = e.Source;
ei.frame = (game.frames[infoFrame]).name;
string json = JsonUtility.ToJson(ei);
StartCoroutine(PostRequest("https://frame-game-backend.herokuapp.com/frames/error", json));
}
} else {
frameNameI.text = "All Frames Completed";
frameCountI.text = "";
descI.text = "Nothing to see here!";
}
}
#endregion
#region general gameplay
// Set the initial state of the game, ensures that each fe is initially
// incomplete
private void PlayGame()
{
if(!isInfo)
{
foreach(Frame frame in game.frames)
{
highlights[frame.name] = new Dictionary<string, ((int, int), (int, int))>();
foreach(FE fe in frame.FEs)
{
isComplete[fe] = false;
}
}
UpdateText();
}
}
#endregion
#region create user annotation
// Saves the FE, uses create label to create a label if it is valid
public void onFESave()
{
if(!isComplete[game.frames[currentFrame].FEs[currentFE]])
{
string FEName = game.frames[currentFrame].FEs[currentFE].name;
string annotation = Result.text;
(int, int) newLabel = (customText.startIndex, customText.endIndex);
if(FEName == "Target")
{
string noPunct = "";
foreach(char c in annotation)
{
if(char.IsLetter(c) || c == ' ')
{
noPunct += c;
}
}
noPunct = noPunct.ToLower();
// Check if the string (punctuation removed) is an acceptable LU
foreach(string lu in game.frames[currentFrame].allLUs)
{
bool containedInLU = true;
// All of the words in the word have to be contained in
// the LU, however, all words in the LU do not have to
// appear in the word
foreach(string word in noPunct.Split(" "))
{
if(!lu.Contains(word)) containedInLU = false;
}
if(containedInLU)
{
AlertText.text = "Saved " + game.frames[currentFrame].FEs[currentFE].name;
if(!isFading)
{
StartFadeIn(Alert);
StartTextFadeIn(Alert, AlertText);
StartFadeOut(Alert);
StartTextFadeOut(Alert, AlertText);
}
Alert.SetActive(true);
isComplete[game.frames[currentFrame].FEs[currentFE]] = true;
createLabel(newLabel);
Result.text = annotation;
break;
}
}
// if not valid, issue an alert
if(!isComplete[game.frames[currentFrame].FEs[currentFE]])
{
ErrorText.text = "Not a valid target. Click info tab for more. ";
if(!isFading)
{
StartFadeIn(Error);
StartTextFadeIn(Error, ErrorText);
StartFadeOut(Error);
StartTextFadeOut(Error, ErrorText);
}
customText.ClearText();
}
} else {
createLabel(newLabel);
Result.text = annotation;
}
} else {
// If the user presses the save button again, it will erase
// highlights and revert the frame element to an incomplete
// state
isComplete[game.frames[currentFrame].FEs[currentFE]] = false;
if(completeFE <= 1)
{
currentSentence = default(AnnotatedSentence);
}
customText.ClearText();
string f_name = game.frames[currentFrame].name;
string fe_name = game.frames[currentFrame].FEs[currentFE].name;
customText.destroyHighlights(f_name, fe_name);
completeFE--;
}
UpdateText();
}
// Sends the frame off to the backend after creating an AnnotationInfo
// object (Note that the user cannot access the frame after this function
// is called)
public void onFrameSave()
{
if(game.frames.Length > 0 && completeFE == game.frames[currentFrame].FEs.Length)
{
UnlockedF.SetActive(false);
LockedF.SetActive(false);
AnnotationInfo[] annotations = new AnnotationInfo[game.frames[currentFrame].FEs.Length];
foreach(KeyValuePair<string, (int,int)> entry in currentSentence.labels)
{
AnnotationInfo annot = new AnnotationInfo();
annot.author_id = playerObject.id;
annot.fe_id = entry.Key;
annot.startIndex = entry.Value.Item1;
annot.length = entry.Value.Item2;
annot.text = currentSentence.text;
string json = JsonUtility.ToJson(annot);
StartCoroutine(PostRequest("https://frame-game-backend.herokuapp.com/frames/update/"+game.frames[currentFrame].id, json));
}
List<Frame> frames = new List<Frame>(game.frames);
foreach(FE fe in game.frames[currentFrame].FEs)
{
isComplete[fe] = false;
}
frames.RemoveAt(currentFrame);
game.frames = frames.ToArray();
completeFrame++;
currentFE = 0;
completeFE = 0;
Result.text = "Not in sentence";
onRightFClick();
onLeftFIClick();
UpdateText();
currentSentence = default(AnnotatedSentence);
UnlockedF.SetActive(true);
LockedF.SetActive(true);
} else {
ErrorText.text = "All FEs must be completed before annotation is sent off.";
if(!isFading)
{
StartFadeIn(Error);
StartTextFadeIn(Error, ErrorText);
StartFadeOut(Error);
StartTextFadeOut(Error, ErrorText);
}
}
}
// Helper function that creates a label for an FE
private void createLabel((int, int) label)
{
string name = game.frames[currentFrame].FEs[currentFE].name;
Dictionary<string, Color> colors = FEColors[(game.frames[currentFrame]).name];
// If start < -1, then the FE does not exist in the sentence
if(label.Item1 < 0)
{
// Checks if it is the first element in the sentence
if(currentSentence.Equals(default(AnnotatedSentence)))
{
currentSentence = new AnnotatedSentence("");
}
// adds to labels
isComplete[game.frames[currentFrame].FEs[currentFE]] = true;
currentSentence.labels[name] = (-1, -1);
completeFE++;
} else {
// If the frame element is not null-instantiated, we need to detect
// the sentence in which this FE exists
// Find the start and end of the sentence
// Find the last index before the start of the fe
int[] potentialStart = new int[3]{story.LastIndexOf('.',label.Item1),
story.LastIndexOf('?', label.Item1),
story.LastIndexOf('!', label.Item1)};
int start;
if(story.Length > potentialStart.Max()+1 && story[potentialStart.Max()+1] == ' ')
{
// If there is a space between punctuation and start of sentence
start = potentialStart.Max()+2;
} else if(potentialStart.Max() < 0)
{
// If there is no punctuation before the index
start = 0;
} else {
// If there is no space between punctuation and start of sentence
start = potentialStart.Max()+1;
}
// Find the first index before the end of the fe
int[] potentialEnd = new int[3]{story.IndexOf('.',label.Item2),
story.IndexOf('?', label.Item2),
story.IndexOf('!', label.Item2)};
// Since we're using .Min, we need to remove -1 (not found) values
int[] filtered = potentialEnd.Where(e => e >= 0).ToArray();
int end;
if(filtered.Length == 0)
{
// No puncuation found after
end = story.Length;
} else {
// The end of the sentence
end = filtered.Min()+1;
}
// The sentence
string sent = story.Substring(start, end-start);
// If there have been no other (non-null instantiated) FEs
if(currentSentence.Equals(default(AnnotatedSentence)) || currentSentence.text == "")
{
// Add a highlight
if(name != "Target")
{
customText.addHighlight(game.frames[currentFrame].name, name, colors[name], customText.startIndex, customText.endIndex);
}
// if the current sentence if uninitialized
if(currentSentence.Equals(default(AnnotatedSentence))) currentSentence = new AnnotatedSentence(sent);
// set the sentence text to the sentence that contains this fe
currentSentence.text = sent;
// Complete current label
currentSentence.labels[name] = (label.Item1-start, label.Item2-label.Item1);
isComplete[game.frames[currentFrame].FEs[currentFE]] = true;
// Clears the text
customText.ClearText();
completeFE++;
} else
{
// The current sentence matches the sentence of this fe
if(currentSentence.text == sent)
{
if(name != "Target")
{
customText.addHighlight(game.frames[currentFrame].name, name, colors[name], customText.startIndex, customText.endIndex);
}
// (see above), adds label and clears text
currentSentence.labels[name] = (label.Item1-start, label.Item2-label.Item1);
isComplete[game.frames[currentFrame].FEs[currentFE]] = true;
customText.ClearText();
completeFE++;
} else {
// show error, fes must be in same frame
ErrorText.text = "Frame elements must be in the same sentence. Click info tab for more.";
if(!isFading)
{
StartFadeIn(Error);
StartTextFadeIn(Error, ErrorText);
StartFadeOut(Error);
StartTextFadeOut(Error, ErrorText);
}
customText.ClearText();
}
}
}
}
#endregion
#region info screen
// Enable the info screen
public void onInfoClick()
{
customText.ClearText();
FrameDesc.SetActive(false);
FrameInfo.SetActive(true);
StoryBG.SetActive(false);
InfoBG.SetActive(true);
Info.color = Color.black;
Story.color = Color.white;
UpdateInfo();
}
// Enable the story writing screen
//(not to be confused with the starting screen scene)
public void onHomeClick()
{
FrameDesc.SetActive(true);
FrameInfo.SetActive(false);
StoryBG.SetActive(true);
InfoBG.SetActive(false);
Info.color = Color.white;
Story.color = Color.black;
UpdateText();
}
#endregion
#region get and post requests helpers
IEnumerator GetRequest(string uri, OnReceivedCallback callback)
{
using (UnityWebRequest webRequest = UnityWebRequest.Get(uri))
{
yield return webRequest.SendWebRequest();
if(webRequest.result == UnityWebRequest.Result.ConnectionError)
{
Debug.Log("Network Error: " + webRequest.error);
} else {
//Convert to json string
string jsonData = Encoding.ASCII.GetString(webRequest.downloadHandler.data);
// Send back
webRequest.downloadHandler.Dispose();
callback(jsonData);
}
}
}
IEnumerator PostRequest(string url, string json)
{
using (UnityWebRequest uwr = new UnityWebRequest(url, "POST"))
{
byte[] jsonToSend = new System.Text.UTF8Encoding().GetBytes(json);
uwr.uploadHandler = (UploadHandler)new UploadHandlerRaw(jsonToSend);
uwr.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
uwr.SetRequestHeader("Content-Type", "application/json");
//Send the request then wait here until it returns
yield return uwr.SendWebRequest();
if (uwr.result == UnityWebRequest.Result.ConnectionError)
{
Debug.Log("Error While Sending: " + uwr.error);
}
else
{
// Debug.Log("Received: " + uwr.downloadHandler.text);
}
}
}
#endregion
#region writing to annotating
// Called when the user decides to "lock in" their story for annotating
public void onStoryFinish()
{
isAnnotating = true;
ReadBoxObj.SetActive(true);
ReadingBox.text = WritingBox.text;
WriteBoxObj.SetActive(false);
Annotate.SetActive(true);
FrameMenu.SetActive(false);
frameCount = (GameObject.Find("FrameCount")).GetComponent<TMP_Text>();
frameSaved = (GameObject.Find("FrameSaved")).GetComponent<TMP_Text>();
feName = (GameObject.Find("FEName")).GetComponent<TMP_Text>();
feCount = (GameObject.Find("FECount")).GetComponent<TMP_Text>();
feSaved = (GameObject.Find("FESaved")).GetComponent<TMP_Text>();
story = Field.GetComponent<TMP_Text>().text;
customText.SetString(story);
UpdateText();
}
#endregion
#region increment/decrement frames/FEs
// Increments (with looping), called by arrow button objects
public void onLeftFIClick()
{
if(infoFrame == 0) infoFrame = game.frames.Length-1;
else infoFrame--;
UpdateInfo();
}
public void onRightFIClick()
{
if(infoFrame == game.frames.Length-1) infoFrame = 0;
else infoFrame++;
UpdateInfo();
}
public void onLeftFClick()
{
if(currentFrame <= 0) currentFrame = game.frames.Length-1;
else currentFrame--;
currentFE = 0;
completeFE = 0;
foreach(FE fe in game.frames[currentFrame].FEs)
{
if(isComplete[fe]) completeFE++;
}
UpdateText();
}
public void onRightFClick()
{
currentFE = 0;
if(currentFrame >= game.frames.Length-1) currentFrame = 0;
else currentFrame++;
completeFE = 0;
if(game.frames.Length > 0)
{
foreach(FE fe in game.frames[currentFrame].FEs)
{
if(isComplete[fe]) completeFE++;
}
}
UpdateText();
}
public void onLeftFEClick()
{
if(currentFE <= 0) currentFE = game.frames[currentFrame].FEs.Length-1;
else currentFE--;
customText.ClearText();
UpdateText();
}
public void onRightFEClick()
{
if(currentFE >= game.frames[currentFrame].FEs.Length-1) currentFE = 0;
else currentFE++;
customText.ClearText();
UpdateText();
}
#endregion
#region text fade in/out
// These are general functions that cause a game object or text to fade out
private void StartTextFadeOut(GameObject g_obj, TMP_Text obj)
{
IEnumerator coroutine = TextFadeOut(g_obj, obj);
StartCoroutine(coroutine);
}
IEnumerator TextFadeOut(GameObject g_obj, TMP_Text to_fade)
{
for (float f = 1; f >= 0.0; f-=0.02f)
{
Color c = to_fade.color;
c.a = f;
to_fade.color = c;
yield return new WaitForSeconds(0.05f);
}
g_obj.SetActive(false);
}
private void StartTextFadeIn(GameObject g_obj, TMP_Text obj)
{