-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcharacter_converter.lsl
More file actions
104 lines (91 loc) · 4.26 KB
/
character_converter.lsl
File metadata and controls
104 lines (91 loc) · 4.26 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
string gNotecard = "Characters";
string gNotecardData;
integer gNotecardLine;
integer gNotecardIndex;
integer gNotecardTotal;
string gCurrentNotecard;
string gCurrentCharacter;
list gNotecardNames;
list gValidProperties = ["name", "vigor", "swiftness", "toughness", "intellect", "cunning"];
string gCharacterData;
string gDataHandles;
string gJsonValue;
key gOwner;//During state_entry the user's key is placed here.
string gOwnerName;//During state_entry the user's name is placed here.
//Helper Functions
integer is_valid_key?(string json, string s){
/* This function takes a JSON string and a key. If the key exists in the JSON object,
the function will save it to the constant storage variable gJsonValue, allowing a
pattern such as string value;if(is_valid_key?(JSON,"key")){value = gJsonValue;} for
fetching values from JSON objects. This prevents JSON_INVALID from being operated on.*/
gJsonValue = llJsonGetValue(json, [s]);
return gJsonValue != JSON_INVALID;
}
request_data(string notecard_name, integer line, string identifier){
/*This function handles the registration of data handles for HTTP requests*/
key dataHandle = llGetNotecardLine(notecard_name,line);
gDataHandles = llJsonSetValue(gDataHandles,[dataHandle], identifier);
}
enumerate_notecards(){
integer numberOfNotecards = llGetInventoryNumber(INVENTORY_NOTECARD);
integer i;for(;i < numberOfNotecards;i++){gNotecardNames = gNotecardNames + llGetInventoryName(INVENTORY_NOTECARD,i);}
gNotecardTotal = numberOfNotecards;
llOwnerSay(llDumpList2String(gNotecardNames,","));
}
read_notecard(string notecard_name){
request_data(notecard_name, gNotecardLine, notecard_name);
}
default
{
on_rez(integer _start_param){llResetScript();}
changed(integer _change){if (_change & CHANGED_INVENTORY){llResetScript();}}
touch_start(integer _num_detected){llResetScript();}
state_entry(){
gOwner = llGetOwner();gOwnerName = llKey2Name(gOwner);
enumerate_notecards();
gCurrentNotecard = llList2String(gNotecardNames, gNotecardIndex);
read_notecard(gCurrentNotecard);
}
dataserver(key _request_id, string _data){
string identifier;if(is_valid_key?(gDataHandles,_request_id)){identifier = gJsonValue;}
if (_data != EOF) { // not at the end of the notecard
//NotecardData += _data + "\n";
list arguments = llParseString2List(_data, [":"], []);
string property = llToLower(llList2String(arguments,0));
string value = llList2String(arguments, 1);
if(llListFindList(gValidProperties, [property]) != -1){//This makes sure the property is a valid statistic to set
if(property == "name"){
gCurrentCharacter = value;
string character = llList2Json(JSON_OBJECT, [property, value]);
gCharacterData = llJsonSetValue(gCharacterData, [JSON_APPEND], character);
} else {
string statistic = llList2Json(JSON_OBJECT, ["name", property,"value", value]);
gCharacterData = llJsonSetValue(gCharacterData, [gNotecardIndex,"statistics",JSON_APPEND], statistic);
}
} else {
llOwnerSay("Invalid property: " + property + " in notecard " + gCurrentNotecard);
}
++gNotecardLine;// increase line count
request_data(gCurrentNotecard,gNotecardLine,"load_characters");//request next line
} else {
gNotecardData = "";
gNotecardLine = 0;
if (gNotecardIndex < gNotecardTotal-1){
gNotecardIndex++;
gCurrentNotecard = llList2String(gNotecardNames, gNotecardIndex);
read_notecard(gCurrentNotecard);
} else {
llOwnerSay("Notecard read done");
list characters = llJson2List(gCharacterData);
string output = "[\n";
integer i;
integer length = llGetListLength(characters);
for(;i < length;i++){
output += llList2String(characters, i) + "\n";
}
output += "]";
llOwnerSay(output);
}
}
}
}