Пример #1
0
def parsePrompt(chunk):
    global tab, vars, storyName, promptIdCounter

    #print(chunk)
    #find the offset, and then change the chunk to a standard form
    offset = 0
    for c in chunk[0]:
        if c == tab:
            offset += 1
        else:
            break

    #standardize
    for i in range(len(chunk)):
        chunk[i] = chunk[i][offset:]

    #print(chunk)

    prompt = Prompt(promptText=chunk[0],
                    storyId=storyName,
                    option1="",
                    promptId=promptIdCounter)
    promptIdCounter += 1
    prompt.save()

    #starting with first line of first option, iterate through the lines
    for j in range(1, len(chunk)):
        line = chunk[j]
        #print(line)

        # check if the line is set off by a tab
        # if not, then we found an option
        if line[0] != tab and prompt.option1 == "":
            #set the option1 for the prompt to this line
            prompt.option1 = line
            #then create the response object
            option1 = Response(storyId=storyName,
                               option1or2="1",
                               previousPrompt=prompt,
                               ifStatement="")
            print("response created for option1", option1)
            option1.save()
            optionsToPassUp1 = [option1]
            #now we start from what comes under the option
            #these sublines will be offset by 1 tab
            k = j + 1
            for notk in range(j + 1, len(chunk)):
                subline = chunk[k]
                #print("subline1",[subline.split()])
                #if there is no tab, then we have reached the end
                if subline[0] != tab:
                    #print('move on')
                    break
                #this means that we have found the nextPrompt
                elif subline[1] == tab:
                    #print('go deeper')
                    countForNextPrompt = 0
                    for l in range(k, len(chunk) + 1):
                        #print(chunk[l])
                        # the nextPrompt will be in atleast 2 deep, anything shallower is not a part of it
                        if l == len(chunk) or chunk[l][1] != tab:
                            #print(k,l)
                            option1.nextPrompt, subOptionsToPassUp1 = parsePrompt(
                                chunk[k:l])
                            print("retrieved response from deeper option1")
                            optionsToPassUp1.pop(-1)
                            optionsToPassUp1.extend(subOptionsToPassUp1)
                            #print(optionsToPassUp1)
                            #print("returned up")
                            k = l - 1
                            #print("option1: ",option1)
                            option1.save()
                            break
                #if statement
                elif subline[1:3] == "if" and subline[-1] == ":":
                    if option1.ifStatement == "":
                        option1.ifStatement = subline[1:]
                    else:
                        option1.save()
                        option1 = Response(storyId=storyName,
                                           option1or2="1",
                                           previousPrompt=prompt)
                        print("created an if response for options 1", option1)
                        option1.ifStatement = subline[1:]
                        option1.save()
                        optionsToPassUp1.append(option1)
                #set variable statement
                elif len(subline[1:].split()) > 2 and subline[1:].split(
                )[1] == ":=" and subline.split()[0][1:] in vars:
                    option1.setVariable = subline[1:]
                #otherwise, it's (hopefully) the response text, if any
                else:
                    #print("response text: ",subline)
                    option1.responseText = subline[1:]
                k += 1
                if k >= len(chunk): break
            option1.save()

        #for option 2
        elif line[0] != tab and prompt.option1 != "":
            #set the option2 for the prompt to this line
            prompt.option2 = line
            #then create the response object
            option2 = Response(storyId=storyName,
                               option1or2="2",
                               previousPrompt=prompt,
                               ifStatement="")
            print("response created for option2", option2)
            option2.save()
            optionsToPassUp2 = [option2]
            #now we start from what comes under the option
            #these sublines will be offset by 1 tab
            k = j + 1
            for notk in range(j + 1, len(chunk)):
                subline = chunk[k]
                #print("subline2",[subline.split()])
                #if there is no tab, then we have reached the end
                if subline[0] != tab:
                    #print('move on')
                    break
                #this means that we have found the nextPrompt
                elif subline[1] == tab:
                    print('go deeper')
                    print(chunk)
                    countForNextPrompt = 0
                    for l in range(k, len(chunk) + 1):
                        #print(chunk[l])
                        # the nextPrompt will be in atleast 2 deep, anything shallower is not a part of it
                        if l == len(chunk) or chunk[l][1] != tab:
                            #print(k,l)
                            option2.nextPrompt, subOptionsToPassUp2 = parsePrompt(
                                chunk[k:l])
                            print("retrieved response from deeper option2")
                            optionsToPassUp2.pop(-1)
                            optionsToPassUp2.extend(subOptionsToPassUp2)
                            #print(optionsToPassUp2)
                            print("returned up")
                            k = l - 1
                            #print("option1: ",option1)
                            option2.save()
                            break
                #if statement
                elif subline[1:3] == "if" and subline[-1] == ":":
                    if option2.ifStatement == "":
                        option2.ifStatement = subline[1:]
                    else:
                        option2.save()
                        option2 = Response(storyId=storyName,
                                           option1or2="2",
                                           previousPrompt=prompt)
                        print("created an if response for options 2", option2)
                        option2.ifStatement = subline[1:]
                        option2.save()
                        optionsToPassUp2.append(option2)
                #set variable statement
                elif len(subline[1:].split()) > 2 and subline[1:].split(
                )[1] == ":=" and subline.split()[0][1:] in vars:
                    option2.setVariable = subline[1:]
                #otherwise, it's (hopefully) the response text, if any
                else:
                    #print("response text: ",subline)
                    option2.responseText = subline[1:]
                k += 1
                if k >= len(chunk): break
            option2.save()

    prompt.save()
    optionsToPassUp2.extend(optionsToPassUp1)
    return prompt, optionsToPassUp2