User:Allen Kerensky/Myriad Lite Preview 5/Flaws Server

From OpenSimulator

< User:Allen Kerensky | Myriad Lite Preview 5(Difference between revisions)
Jump to: navigation, search
(created)

Revision as of 10:28, 6 February 2012

Myriad Lite Flaws Server

Myriad_Lite_Flaws_Server-v0.0.1-20120129.lsl

// Myriad_Lite_Flaw_Server-v0.0.1-20120129.lsl
// The Myriad RPG System was designed, written, and illustrated by Ashok Desai
// Myriad RPG licensed under the Creative Commons Attribution 2.0 UK: England and Wales
// http://creativecommons.org/licenses/by/2.0/uk/
// Myriad Lite software Copyright (c) 2011 by Allen Kerensky (OSG/SL)
// Myriad Lite licensed under the
// Creative Commons Attribution-Share Alike-Non-Commercial 3.0 Unported
// http://creativecommons.org/licenses/by-nc-sa/3.0/

// CONSTANTS - DO NOT CHANGE DURING RUN
string VERSION = "0.0.1"; // Allen Kerensky's script version
string VERSIONDATE = "20120129"; // Allen Kerensky's script yyyymmdd
integer CHAN_MYRIAD = -999; // regionwide channel for Myriad events
string CARD = "Myriad_Lite_Flaws-v0.0.0-20100326.csv"; // notecard data table

// RUNTIME GLOBALS - CAN CHANGE DURING RUN
integer HAND_MYRIAD; // llListenRemove handle for Myriad regional channel 

list FLAWS; // names of flaws loaded
list TYPES; // flaw types indexed in name order
list GP_COSTS; // flaw costs indexed in name order
list MAX_LEVELS; // flaw max levels indexed in name order
list DESCRIPTIONS; // flaw descriptions indexed in name order


integer LINE = 0; // reading line number
key QUERY = NULL_KEY; // track notecard queries

// DEBUG - show debug messages
DEBUG(string dmessage) {
    llOwnerSay("DEBUG: "+dmessage);
}

// SETUP - begin
SETUP() {
    if ( HAND_MYRIAD != 0 ) llListenRemove(HAND_MYRIAD); // is there already a listener? clear it
    HAND_MYRIAD = llListen(CHAN_MYRIAD,"",NULL_KEY,""); // start region channel listener
    DEBUG("Flaw Server "+VERSION+" "+VERSIONDATE+" loading Flaws. Please wait..."); // tell player we're waiting for data server
    QUERY = llGetNotecardLine(CARD,LINE++); // ask for line from notecard and advance to next line
}

// RESET - reload
RESET() {
    llResetScript(); // now reset
}

LIST_FLAWS(key id) {
    integer replyto = (integer)("0x"+llGetSubString((string)id,0,6)); // calculate specific user channel to reply to
    // we have to work around llRegionSay max string length 1023 characters
    integer items = llGetListLength(FLAWS); // how many total items to send?
    integer count; // which item are we processing now?
    string out = "FLAWS|"; // start an output list
    integer firstflag = TRUE; // is this first item in new list?
    while (count < items ) { // do we still have items in list to send?
        string name = llList2String(FLAWS,count); // get the name of current flaw
        if ( llStringLength(out+","+name) <= 1016) { // is our output list under the string length limit?
            if ( firstflag == TRUE ) { // first item in list does not need a comma prefix
                out += name; // add this item as first in the output list
                firstflag = FALSE; // turn off first flag since next item won't be
            } else { // not first item in list, prefix with comma
                out += ","+name; // add a comma and this flaw to existing list
            }
        } else { // output string > 1016 chars long
            llRegionSay(replyto,out); // send current output string
            out = "FLAWS|"+name; // start a new one and add current item to that
        }
        count++; // done putting this item in a list, set counter to next
    }   
    llRegionSay(replyto,out); // say last line of output
}

GET_FLAW(key id,string msg) {
    integer replyto = (integer)("0x"+llGetSubString((string)id,0,6)); // calculate requestor-specific chat channel to reply to
    list tokens = llParseString2List(msg,["|"],[]); // split msg into list around pipe symbols
    string flaw = llList2String(tokens,1); // the name of the flaw to get
    integer listpos = llListFindList(FLAWS,[flaw]); // get the position of that flaw in the list
    string reply = "FLAW"; // start output flaw data NO PLURAL here - we're sending 1 flaw
    if ( listpos != -1 ) { // was boon name in the list?
        reply += "|NAME="+llList2String(FLAWS,listpos); // add name field to output
        reply += "|TYPE="+llList2String(TYPES,listpos); // add type field to output
        reply += "|GP_COST="+llList2String(GP_COSTS,listpos); // add cost field to output
        reply += "|MAX_LEVEL="+llList2String(MAX_LEVELS,listpos); // add max level to output
        reply += "|DESCRIPTION="+llList2String(DESCRIPTIONS,listpos); // add description to output
        llRegionSay(replyto,reply); // send it to requestor FIXME what if output greater than 1016 chars?
    } else {
        llRegionSay(replyto,"FLAWS|ERROR=Requested Flaw ("+flaw+") not found"); // boon requested does not exist, return an error
    }
}

SET_FLAW() {
    // FIXME - do we need this?    
}

// DEFAULT STATE
default {

    // STATE ENTRY - called on Reset
    state_entry() {
        SETUP(); // show credits and start notecard data load
    }

    // on_rez - when rezzed to ground or from inventory as attachment during login
    on_rez(integer params) {
        params = 0; // LSLINT
        RESET(); // force to go through state entry
    }

    // attach - when attached or detached from inventory or during login
    attach(key id) {
        id = NULL_KEY; // LSLINT
        RESET(); // force to go through state entry
    }

    // dataserver called for each line of notecard requested
    dataserver(key queryid,string data) {
        if ( queryid == QUERY ) { // dataserver gave us line we asked for?
            if ( data != EOF ) { // we're not at end of notecard file?
                if ( llGetSubString(data,0,0) == "#" ) { // does this line start with comment mark?
                    QUERY = llGetNotecardLine(CARD,LINE++); // ignore comment and ask for the next line
                    return; // return early since we skipping comments
                }
                // Parse non-comment lines in keyword = value[,value,...] format
                list fields = llParseString2List(data,[","],[]); // break line of text into = delimited fields
                FLAWS           = FLAWS + [llStringTrim(llList2String(fields,0),STRING_TRIM)]; // add name of flaw to name list
                TYPES           = TYPES + [llStringTrim(llList2String(fields,1),STRING_TRIM)]; // add type of flaw to type list
                GP_COSTS        = GP_COSTS + [llStringTrim(llList2String(fields,2),STRING_TRIM)]; // add cost of flaw to cost list
                MAX_LEVELS      = MAX_LEVELS + [llStringTrim(llList2String(fields,3),STRING_TRIM)]; // add max level of flaw to max level list
                DESCRIPTIONS    = DESCRIPTIONS + [llStringTrim(llList2String(fields,4),STRING_TRIM)]; // add description of boon to description list
                QUERY = llGetNotecardLine(CARD,LINE++); // finished with known keywords, get next line
            } else { // end of notecard
                DEBUG("Flaws Loaded. Server ready. Free Memory: "+(string)llGetFreeMemory()); // done, ready to serve
            } // end if data not equal eof
        } // end if query id equal
    } // end if data server event
    
    listen(integer channel,string name,key id,string msg) {
        channel = 0; // LSLINT
        name = ""; // LSLINT
        list tokens = llParseString2List(msg,["|"],[ ]); // split msg into list around pipe symbols
        string command = llList2String(tokens,0); // first field is some sort of command
        if ( command == "LIST_FLAWS" ) { // is this a list all flaw names request
            LIST_FLAWS(id); // call it
            return; // return early instead of processing more
        }
        if ( command == "GET_FLAW" ) { // GET_FLAW|string flawname
            GET_FLAW(id,msg); // call it
            return; // return early instead of processing more
        }
        if ( command == "SET_FLAW" ) { // is this a set-boon request?
            SET_FLAW(); // call it
            return; // return early in case we add more later
        }
    }
} // end default state

Personal tools
General
About This Wiki