package org.drools.games.adventures

import org.drools.games.adventures.Request;
import org.drools.games.adventures.Response;
import org.drools.games.adventures.SessionCharacter

global org.drools.games.adventures.Counter counter

dialect "mvel"

rule insertRequestCommand no-loop when
    $rq : Request()
    $cmd : Command() from $rq.object
then
    modify( $rq ) { localId = counter.getAndIncrement() };    
    $cmd.requestId = $rq.localId;
    insert( $cmd );
end


rule retractRequest  no-loop salience -100 when
    $rq : Request()
    not Command( requestId == $rq.localId )
then
    retract ( $rq );
end

rule handleResponse when
    $rq : Request()
    $rs : Response( localId == $rq.localId )
then
    $rq.session.channels["output"].send( $rs.object );
    retract ( $rs );
end