It's been a long Sunday and thankfully I had some good motivation to play with CouchCMS.

I present you guys a few buttons that I call a 'driving simulator' :)

To experience this, visit http://drive.infinityfreeapp.com/ or use the code.

It looks like this — yes a tad primitive 8-)

Screenshot-2023-03-12-184541.598.png
Screenshot-2023-03-12-184541.598.png (9.48 KiB) Viewed 17455 times


You've got 2 types of fuel and 2 different speeds of driving. You can only drive if car has gas.

The faster you go the more miles you cover. And also more gas is spent.

Slower driving spends little gas and adds fewer miles.



And if you want to try it offline, copy-paste to a Couch installation (if you installed one within last 2-3 years it should work fine) the following code —

Code: Select all
<cms:func 'newCar' model='' ><cms:hide>

    <!-- create local object "this" -->
    <cms:set this='[]' is_json='1' scope='local'/>

    <!-- create a few public properties -->
    <cms:set this.Miles='[]' is_json='1' scope='parent'/>
    <cms:set this.GasTank='0' is_json='1' scope='parent'/>


    <!-- 'localize' object name -->
    <cms:set this.Name = model />


    <!-- add some gasoline at a gas station -->
    <cms:func _into='this.add_some_gas' self=this.Name _scope='parent'>
        <cms:get self into='this' into_scope='local'/>

        <!-- add some gasoline at a gas station -->
        <cms:set random_liters = "<cms:php>echo rand(25,40);</cms:php>" />

        <!-- combined capacity now -->
        <cms:set new_amount = "<cms:add random_liters this.GasTank />" />
        <cms:put var="<cms:show self />.GasTank" value=new_amount scope='parent'/>
    </cms:func>


    <!-- log a few miles covered -->
    <cms:func _into='this.add_some_miles' self=this.Name _scope='parent'>

        <!-- add some miles -->
        <cms:set random_miles = "<cms:php>echo rand(5,15);</cms:php>" />

        <cms:put var="<cms:show self />.Miles." value=random_miles scope='parent'/>
    </cms:func>


    <!-- count Miles -->
    <cms:func _into='this.countMiles' self=this.Name _scope='parent'>
        <cms:get self into='this' into_scope='local'/>

        <!-- count Miles -->
        <cms:set total = '0' />
        <cms:each this.Miles as='covered'><cms:incr total covered /></cms:each>

        <cms:show total />
    </cms:func>


    <!-- spend some gasoline driving like crazy -->
    <cms:func _into='this.drive_fast' self=this.Name _scope='parent'>
        <cms:get self into='this' into_scope='local'/>

        <cms:repeat '10'><cms:call this.drive_slow /></cms:repeat>
    </cms:func>


    <!-- spend some gasoline driving like grandpa -->
    <cms:func _into='this.drive_slow' self=this.Name _scope='parent'>
        <cms:get self into='this' into_scope='local'/>

        <!-- spend some gasoline driving like grandpa -->
        <cms:set random_liters = "<cms:php>echo rand(1,5);</cms:php>" />

        <!-- combined capacity now -->
        <cms:set new_amount = "<cms:sub this.GasTank random_liters />" />

        <cms:if new_amount le '0'><cms:set new_amount = '0' /></cms:if>

        <cms:put var="<cms:show self />.GasTank" value=new_amount scope='parent'/>

        <cms:call this.add_some_miles />
    </cms:func>



    <!-- return "this" as object -->
    <cms:put var=model value=this scope='parent'/>

</cms:hide>
</cms:func>


<cms:php>
    global $KSESSION, $CTX;
    $obj = $KSESSION->get_var('myRedCar');
    $CTX->set( 'myRedCar', $obj, 'global');
</cms:php>
<cms:if "<cms:not "<cms:is_array myRedCar />" />">
    <cms:call 'newCar' model='myRedCar' />
    <cms:set_session name='myRedCar' value=myRedCar />
    You just got yourself some wheelz, wow!
<cms:else_if myRedCar.GasTank gt '0' />Your car is ready to go!
<cms:else />Need gas!
</cms:if>

<h3>Status :: Miles <cms:call myRedCar.countMiles /> :: Gasoline <cms:show myRedCar.GasTank /></h3>



<cms:form name='o-l___lr__ltt-o-' anchor='0' method='post'>
    <cms:if k_success>
        <cms:if "<cms:gpc 'addgas' />">
            <cms:call myRedCar.add_some_gas />
        </cms:if>
        <cms:if "<cms:gpc 'drive_slow' />">
            <cms:call myRedCar.drive_slow />
        </cms:if>
        <cms:if "<cms:gpc 'drive_fast' />">
            <cms:call myRedCar.drive_fast />
        </cms:if>
        <cms:if "<cms:gpc 'cheetah' />">
            <cms:repeat count='5'>
                <cms:call myRedCar.add_some_gas />
            </cms:repeat>
        </cms:if>
        <cms:if "<cms:gpc 'park' />">
            <cms:set_session name='myRedCar' value="" />
        <cms:else />
            <cms:set_session name='myRedCar' value=myRedCar />
        </cms:if>
        <cms:redirect k_page_link />
    </cms:if>
    <cms:if myRedCar.GasTank gt '15' >
        <cms:input type='submit' name='drive_fast' value='Drive faassst!'/><br><br>
    <cms:else_if myRedCar.GasTank gt '0' />
        <cms:input type='submit' name='drive_slow' value='Drive like grandpa'/><br><br>
    <cms:else />
        <cms:input type='submit' name='addgas' value='Add gas!'/><br><br>
        <cms:input type='submit' name='cheetah' value='Pour a Cheetah Chug!'/><br><br>
        <cms:input type='submit' name='park' value='Restart'/><br><br>
    </cms:if>
</cms:form>



The code above is an implementation of the Object-oriented coding concept presented here viewtopic.php?f=8&t=13370

Hope you get a smile today!
I had a lot of fun driving my Red Car :lol: