/* Score Server - DMS - Mar 98 - by Sylvain HUET */ defcom CsetHigh=setHigh S;; defcom CsetCur=setCur S;; defcom CendGame=endGame S I;; defcom Cedit=edit S;; defcom CsetName=setName S;; var nbhigh=30;; struct Score=[cliScore:CLIENT,nameScore:S,scoreScore:I]mkScore;; typeof clis=[CLIENT r1];; typeof edit=[CLIENT r1];; typeof current=[Score r1];; typeof high=[Score r1];; typeof savehigh=[Score r1];; typeof dmifile=S;; typeof session=S;; fun scorebycli(x,c)=x.cliScore==c;; fun chghigh(l,new)= if l==nil then nil else let l->[a n] in if strcmp hd a "high" then a::chghigh n new else ("high"::new::nil)::chghigh n new;; fun AToHigh(l)= if l==nil then nil else let l->[a n] in if nil==hd a then AToHigh n else (mkScore[nil hd a atoi hd tl a])::AToHigh n;; fun HighToA(l)= if l==nil then nil else let l->[a n] in ((if a.cliScore==nil then a.nameScore else _DMSgetLogin a.cliScore):: (itoa a.scoreScore)::nil)::HighToA n;; fun sortbyscore(a,b)= a.scoreScore>b.scoreScore;; fun cutlist(l,n)= if l==nil || n==0 then nil else (hd l)::cutlist tl l n-1;; fun broad(c,s)=_DMSsend this c s;; fun high_has_changed()= let strbuild HighToA high->new in (if session==nil then _storepack strbuild chghigh strextr _getpack _checkpack dmifile new dmifile else nil; apply_on_list clis @broad CsetHigh [new]);; fun killcurr(cli)= let search_in_list current @scorebycli cli -> s in if s==nil then nil else (set current=remove_from_list current s; apply_on_list clis @broad CsetCur [strbuild HighToA current]);; fun score(cli,n,i)= apply_on_list clis @broad CendGame [n i]; let nth_list high nbhigh-1 -> s in if s!=nil && s.scoreScore>i then nil else (set high=cutlist rquicksort @sortbyscore (mkScore[nil n i])::high nbhigh; high_has_changed); killcurr cli;; fun curscore(c,i)= let search_in_list current @scorebycli c -> s in if s==nil then (set current=(mkScore[c nil i])::current;0) else set s.scoreScore=i; set current=rquicksort @sortbyscore current; apply_on_list clis @broad CsetCur [strbuild HighToA current];; fun logout(cli)= if !findList clis cli then nil else (set clis=remove_from_list clis cli; set edit=remove_from_list edit cli; _DMSevent this cli "destroyed" nil nil) ;; fun activate(from,cli,action,param,rep)= if !strcmp action "start" then if _DMScreateClientDMI this cli session then (set clis=cli::clis; _DMSsend this cli CsetHigh [strbuild HighToA high]; _DMSsend this cli CsetCur [strbuild HighToA current]; _DMSevent this cli "entering" nil nil) else nil else if !strcmp action "destroy" then (_DMSdelClientDMI this cli; logout cli) else if !strcmp action "currentScore" then (curscore cli atoi param;0) else if !strcmp action "finalScore" then (score cli _DMSgetLogin cli atoi param; 0) else if !strcmp action "admin" then if !findList clis cli then nil else (set edit=cli::remove_from_list edit cli; _DMSsend this cli Cedit [session]) else nil;; fun IniDMI(file)= set dmifile=file; let strextr _getpack _checkpack file ->l in (set high=AToHigh strextr getInfo l "high"); _DMSregisterDMI this @activate @killcurr @logout nil;; fun __setHigh(s)= set high=cutlist rquicksort @sortbyscore AToHigh strextr s nbhigh; high_has_changed;; fun __startS(s)= if session!=nil then nil else (set session=s; apply_on_list clis @broad CsetName [session]; set savehigh=high; set high=nil; high_has_changed; _DMSevent this nil "reset" nil nil);; fun __endS()= if session==nil then nil else (set session=nil; apply_on_list clis @broad CsetName [session]; set high=savehigh; set savehigh=nil; high_has_changed);;