From 926fed6a5c719e5ff363757626cdebe48d4f0561 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Mon, 27 Apr 2015 11:29:03 +0200 Subject: Add working viewcompetition logic using html files in gamevisuals/ --- .gitignore | 3 + competition.py | 34 ++++--- viewcompetition.cpp | 125 +++++++++++++------------- viewcompetition.css | 76 ++++++++++++++++ viewcompetition.js | 255 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 416 insertions(+), 77 deletions(-) create mode 100644 viewcompetition.css create mode 100644 viewcompetition.js diff --git a/.gitignore b/.gitignore index ccdaa2a..321bcea 100644 --- a/.gitignore +++ b/.gitignore @@ -1,10 +1,13 @@ competitions/ playerlogs/ +gamevisuals/ gluon neutrino randino photon +viewcompetition + *.o diff --git a/competition.py b/competition.py index ab72d20..2582288 100755 --- a/competition.py +++ b/competition.py @@ -12,7 +12,7 @@ Options: -C Do not write a Competition log -h Help. What you're looking at -q Quiet. Don't print so much - -v View the competition afterwards using ./viewcompetition + -V Do not view the competition, so don't write an html file """ import os,sys,subprocess,shlex,re,time @@ -115,7 +115,7 @@ def haswon(board,lastplayer): fname="" quiet=False -viewcompetition=False +viewcompetition=True complog=True if len(sys.argv)==1: #no args fname="competition.txt" @@ -129,7 +129,7 @@ else: print(__doc__) sys.exit(0) elif c=="q": quiet=True - elif c=="v": viewcompetition=True + elif c=="V": viewcompetition=False else: print("Unrecognised flag '"+c+"'.") print(__doc__) @@ -179,12 +179,12 @@ elif not os.path.isdir("playerlogs"): sys.exit(1) try: - logfname="playerlogs/"+re.sub(r"[^a-zA-Z0-9 ]","",p1fname)+"_white_vs_"+re.sub(r"[^a-zA-Z0-9 ]","",p2fname)+".txt" - p1errlog=open(logfname,mode="w") - logfname="playerlogs/"+re.sub(r"[^a-zA-Z0-9 ]","",p2fname)+"_black_vs_"+re.sub(r"[^a-zA-Z0-9 ]","",p1fname)+".txt" - p2errlog=open(logfname,mode="w") + plogfname="playerlogs"+os.path.sep+re.sub(r"[^a-zA-Z0-9 ]","",p1fname)+"_white_vs_"+re.sub(r"[^a-zA-Z0-9 ]","",p2fname)+".txt" + p1errlog=open(plogfname,mode="w") + plogfname="playerlogs"+os.path.sep+re.sub(r"[^a-zA-Z0-9 ]","",p2fname)+"_black_vs_"+re.sub(r"[^a-zA-Z0-9 ]","",p1fname)+".txt" + p2errlog=open(plogfname,mode="w") except: - print("Error: could not open log file '"+logfname+"'.") + print("Error: could not open log file '"+plogfname+"'.") sys.exit(1) #Start programs @@ -222,7 +222,7 @@ elif not os.path.isdir("competitions"): sys.exit(1) try: - logfname="competitions/game_"+re.sub(r"[^a-zA-Z0-9 ]","",p1fname)+"_vs_"+re.sub(r"[^a-zA-Z0-9 ]","",p2fname)+".txt" + logfname="competitions"+os.path.sep+"game_"+re.sub(r"[^a-zA-Z0-9 ]","",p1fname)+"_vs_"+re.sub(r"[^a-zA-Z0-9 ]","",p2fname)+".txt" logfile=open(logfname,mode="w") logfile.write("P1: "+p1fname+"\nP2: "+p2fname+"\n") except: @@ -299,6 +299,8 @@ while True: if not quiet: print("P"+str(player)+": "+movestr(move)+" ",end="") sys.stdout.flush() + if complog: + logfile.write("P"+str(player)+": "+movestr(move)+"\n") won=haswon(board,player) @@ -315,8 +317,6 @@ while True: except: pass sys.exit(1) else: raise e - if complog: - logfile.write("P"+str(player)+": "+movestr(move)+"\n") nummoves+=1 if won!=0: @@ -350,4 +350,14 @@ try: p2proc.terminate() except: pass if viewcompetition: - os.system("."+os.path.sep+"viewcompetition "+logfname) + if not os.path.exists("gamevisuals"): + try: + os.mkdir("gamevisuals") + except: + print("Error: could not create log directory 'gamevisuals'.") + sys.exit(1) + elif not os.path.isdir("gamevisuals"): + #Apparently, there's a file named "gamevisuals". Bastard. + print("Error: an existing file prohibits creation of log directory 'gamevisuals'.") + sys.exit(1) + os.system("."+os.path.sep+"viewcompetition "+logfname+" >gamevisuals"+os.path.sep+logfname[logfname.index(os.path.sep)+1:]+".html") diff --git a/viewcompetition.cpp b/viewcompetition.cpp index 40ba715..3f15c29 100644 --- a/viewcompetition.cpp +++ b/viewcompetition.cpp @@ -1,111 +1,106 @@ #include #include -#include -#include -#include -#ifdef _WIN32 -#include -#else -#include -#endif - -#define SYMBOLFOR(p) ((p)==P1?'X':(p)==P2?'O':(p)==PNONE?'.':'?') - -#define PNONE (0) -#define P1 (1) -#define P2 (2) +#include +#include using namespace std; -uint8_t board[81]={PNONE}; - -void dosleep(int ms){ -#ifdef _WIN32 - Sleep(ms); -#else - usleep(ms*1e3); -#endif -} - -void printboard(void){ - int x,y; - cout<<"+-------+-------+-------+"<\n\ +\n\ +\n\ +\n\ +Game\n\ +\n\ +\n\ +\n\ +\n\ +\n\ +

\n\ +

\n\ +\n\ +
\n\ +
\n\ +
\n\ +\n\ +\n"}; int main(int argc,char **argv){ if(argc==1){ - cout<<"Pass the file name of the competition log as a command-line parameter."<2){ - cout<<"Multiple command-line arguments were passed."<>mv.neudir>>mv.from>>mv.dir; in.ignore(numeric_limits::max(),'\n'); - board[9*y+x]=player; - cout<<"\x1B["<<(5+y/3+y)<<';'<<(3+x/3*2+x*2)<<'H'<::max(),'\n'); + cout< div{ + height:75%; + margin:10px; + border-radius:30px; +} + +h1#header{ + letter-spacing:0.5px; +} +span.celltypeclr{ + border-radius:5px; + padding:4px; +} + +div#status{ + font-size:16px; + font-weight:bold; +} + +div.fgcell.celltype_1 > div{background-color:#ddd;} +span.celltypeclr_1 {background-color:#ddd;} +div.fgcell.celltype_2 > div{background-color:#2a2a2a;} +span.celltypeclr_2 {background-color:#2a2a2a;color:#eee;} +div.fgcell.celltype_3 > div{background-color:#11e;} + +div#movelist{ + border:1px black solid; + width:500px; +} +div#movelist table{ + border-spacing:0; + width:100%; +} +div#movelist tr:first-child{ + font-weight:bold; + background-color:#ccc; +} +div#movelist tr:first-child > td{ + border-bottom:1px black solid; +} +div#movelist tr:nth-child(odd){background-color:#e4e4e4;} +div#movelist tr:nth-child(even){background-color:#fff;} +div#movelist tr:nth-child(n+2):hover{background-color:#bbf;cursor:pointer;} diff --git a/viewcompetition.js b/viewcompetition.js new file mode 100644 index 0000000..ab05c3c --- /dev/null +++ b/viewcompetition.js @@ -0,0 +1,255 @@ +var moveidx=0; +var neuidxhist=[]; + +var board=Array.apply(null,new Array(S*S)).map(function(){return 0;}); +var fgcells=board.map(function(){return null;}); +var neuidx; + +var canclick=false; + +function init(){ + var tr,td,y,x,e,span, + movelisttbody, + header=document.getElementById("header"), + status=document.getElementById("status"), + movelist=document.getElementById("movelist"), + bgtbody=document.getElementById("bgtbody"); + + document.title="Game: "+PLAYERS[0]+" vs "+PLAYERS[1]; + + header.appendChild(document.createTextNode("Game: ")); + span=document.createElement("span"); + span.classList.add("celltypeclr"); span.classList.add("celltypeclr_1"); + span.appendChild(document.createTextNode(PLAYERS[0])); + header.appendChild(span); + header.appendChild(document.createTextNode(" vs ")); + span=document.createElement("span"); + span.classList.add("celltypeclr"); span.classList.add("celltypeclr_2"); + span.appendChild(document.createTextNode(PLAYERS[1])); + header.appendChild(span); + + status.appendChild(document.createTextNode("Game won by ")); + span=document.createElement("span"); + span.classList.add("celltypeclr"); span.classList.add("celltypeclr_"+WINNER); + span.appendChild(document.createTextNode(PLAYERS[WINNER-1])); + status.appendChild(span); + status.appendChild(document.createTextNode(".")); + + + e=document.createElement("table"); + movelisttbody=document.createElement("tbody"); + tr=document.createElement("tr"); + td=document.createElement("td"); td.innerHTML="Player"; tr.appendChild(td); + td=document.createElement("td"); td.innerHTML="Neutron"; tr.appendChild(td); + td=document.createElement("td"); td.innerHTML="Stone"; tr.appendChild(td); + movelisttbody.appendChild(tr); + e.appendChild(movelisttbody); + movelist.appendChild(e); + + tr=document.createElement("tr"); + td=document.createElement("td"); + td.setAttribute("colspan","3"); + td.innerHTML=" "; + tr.appendChild(td); + tr.addEventListener("click",function(ev){ + if(moveidx==0)return; + while(moveidx>0)prevmove(true); + }); + movelisttbody.appendChild(tr); + + MOVES.forEach(function(mv,i){ + var tr,td; + tr=document.createElement("tr"); + + td=document.createElement("td"); + td.innerHTML=i%2+1; + tr.appendChild(td); + + td=document.createElement("td"); + if(mv[0]==-1)td.innerHTML=" "; + else td.innerHTML=arrowfor(mv[0]); + tr.appendChild(td); + + td=document.createElement("td"); + td.innerHTML=mv[1]+": "+arrowfor(mv[2]); + tr.appendChild(td); + + tr.addEventListener("click",(function(target){return function(ev){ + if(moveidx==target)return; + while(moveidxtarget)prevmove(true); + };})(i+1)); + + movelisttbody.appendChild(tr); + }); + + + for(y=0;y=S||y2<0||y2>=S||board[S*y2+x2]!=0){ + if(overshoot)return S*y2+x2; + else return S*y+x; + } + x=x2; + y=y2; + } +} + +function setnextenabled(en){ + document.getElementById("nextmovebtn").disabled=!en; +} +function setprevenabled(en){ + document.getElementById("prevmovebtn").disabled=!en; +} + +function displaycurrentmove(){ + +} + +function nextmove(notimeout){ + var newidx; + if(moveidx==MOVES.length||!canclick)return false; + + if(MOVES[moveidx][0]!=-1){ + neuidxhist.push(neuidx); + newidx=indexafterslide(neuidx,MOVES[moveidx][0]); + movefgcell(fgcells[neuidx],newidx); + board[newidx]=board[neuidx]; + board[neuidx]=0; + fgcells[newidx]=fgcells[neuidx]; + fgcells[neuidx]=null; + neuidx=newidx; + + if(moveidx==MOVES.length-1)bgcell(neuidx).classList.add("win"); + } + + newidx=indexafterslide(MOVES[moveidx][1],MOVES[moveidx][2]); + if(MOVES[moveidx][0]!=-1&&!notimeout){ + setTimeout( + (function(e,idx){return function(){ + movefgcell(e,idx); + canclick=true; + };})(fgcells[MOVES[moveidx][1]],newidx), + 500 + ); + canclick=false; + } else { + movefgcell(fgcells[MOVES[moveidx][1]],newidx); + } + board[newidx]=board[MOVES[moveidx][1]]; + board[MOVES[moveidx][1]]=0; + fgcells[newidx]=fgcells[MOVES[moveidx][1]]; + fgcells[MOVES[moveidx][1]]=null; + + moveidx++; + setprevenabled(true); + if(moveidx==MOVES.length)setnextenabled(false); + return true; +} +function prevmove(notimeout){ + var item,oldneuidx,newidx,winGlowIdxToRemove=null; + if(moveidx==0||!canclick)return false; + moveidx--; + + if(MOVES[moveidx][0]!=-1&&moveidx==MOVES.length-1)winGlowIdxToRemove=neuidx; + + newidx=indexafterslide(MOVES[moveidx][1],MOVES[moveidx][2],true); + board[MOVES[moveidx][1]]=board[newidx]; + board[newidx]=0; + fgcells[MOVES[moveidx][1]]=fgcells[newidx]; + fgcells[newidx]=null; + movefgcell(fgcells[MOVES[moveidx][1]],MOVES[moveidx][1]); + + if(MOVES[moveidx][0]!=-1){ + oldneuidx=neuidxhist.pop(); + board[oldneuidx]=board[neuidx]; + board[neuidx]=0; + fgcells[oldneuidx]=fgcells[neuidx]; + fgcells[neuidx]=null; + if(notimeout){ + movefgcell(fgcells[oldneuidx],oldneuidx); + if(winGlowIdxToRemove!=null)bgcell(winGlowIdxToRemove).classList.remove("win"); + } else { + setTimeout( + (function(e,idx){return function(){ + movefgcell(e,idx); + canclick=true; + if(winGlowIdxToRemove!=null)bgcell(winGlowIdxToRemove).classList.remove("win"); + };})(fgcells[oldneuidx],oldneuidx), + 500 + ); + canclick=false; + } + neuidx=oldneuidx; + } + + setnextenabled(true); + if(moveidx==0)setprevenabled(false); + return true; +} + +window.addEventListener("load",init,false); -- cgit v1.2.3-54-g00ecf