# Variable calculations should be done in this file.
# It is read and interpreted by wims for all valid calls to the module.

colorU=red
colorS=green
colorE=white
slen=!itemcnt $slist

!if $cmd=reply and $status!=waiting
 error=double_reply
 !exit
!endif

!if $cmd=reply and $status=waiting
 mystep=!trim $mystep
 !if $mystep=redo and $this<$slen
  !advance this
  ss=!item $this of $slist
  filled=$ss
  !exit
 !endif
 !if $mystep=back and $this>0
  this=$[$this-1]
  !if $this=0
   filled=0
  !else
   ss=!item $this of $slist
   filled=$ss
  !endif
  !exit
 !endif
 !if $mystep=restart and $this>0
  this=0
  filled=0
  !exit
 !endif
 !if $mystep=$empty
  error=empty_data
  !exit
 !endif
 mystep=$[rint($mystep)]
 !if NaN isin $mystep or $mystep<$min or $mystep>$step
  error=bad_step
  !exit
 !endif
 flist=!char 1 to $filled of $flist
 add=!char 1 to $mystep of UUUUUUUUUUUUUUUUUUUUU
 flist=$flist$add
 filled=$[$filled+min($mystep,$total-$filled)]
 !if $filled=$total
  # User termine
  filled=$total
  status=user
 !else
  # A moi de jouer!
  rest=$[$object%($step+1)]
  r=$[$filled%($step+1)]
  !ifval $r=$rest or $filled<(100-$level)*$total/100
   # Un pas stupide.
   s=!randint $min,$step
  !else
   # Je suis parti pour gagner.
   s=$[max($min,($rest-$r+$step+1)%($step+1))]
  !endif
  add=!char 1 to $s of SSSSSSSSSSSSSSSSSSSSSS
  flist=$flist$add
  serverput=$[min($s,$total-$filled)]
  filled=$[$filled+$serverput]
  !if $filled>=$total
   # Serveur termine
   status=server
  !endif
 !endif
 slist=!item 1 to $this of $slist
 slist=!append item $filled to $slist
 !advance this
 !if $status!=waiting
  !if $status=$last
   status=win
  !else
   status=lose
  !endif
  wims_module_log=$status
 !endif
!endif

