From 837341921ae81def54ada7a4adc9468a41ca6f8c Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Fri, 8 May 2015 17:44:08 +0200 Subject: Start gameserver project --- .gitignore | 3 + gameserver/gameserver.html | 613 +++++++++++++++++++++++++++++++++++++++++++++ gameserver/gameserver.js | 251 +++++++++++++++++++ gameserver/package.json | 15 ++ 4 files changed, 882 insertions(+) create mode 100644 gameserver/gameserver.html create mode 100755 gameserver/gameserver.js create mode 100644 gameserver/package.json diff --git a/.gitignore b/.gitignore index 99f7820..d4a5d5b 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,6 @@ viewcompetition *.o competition.log + + +node_modules diff --git a/gameserver/gameserver.html b/gameserver/gameserver.html new file mode 100644 index 0000000..d8528ab --- /dev/null +++ b/gameserver/gameserver.html @@ -0,0 +1,613 @@ + + + + +Game + + + + +

+
+ + + + + + + + + + + + + + + + +
Neu
+ + + + + + + + + + + + + + + + +
+
+
+
+ +
+
+
+ +
+
+
+ + diff --git a/gameserver/gameserver.js b/gameserver/gameserver.js new file mode 100755 index 0000000..ddc6f0b --- /dev/null +++ b/gameserver/gameserver.js @@ -0,0 +1,251 @@ +#!/usr/bin/env node + +"use strict"; + +var fs=require("fs"), + http=require("http"), + util=require("util"), + assert=require("assert"), + WebSocketServer=require("ws").Server, + Naampje=require("naampje").name; +var HTTPPORT=83,WSPORT=8383; + +var conns=[],games=[]; + +var genid=(function(){ + var $=1; + return function(){return $++;}; +})(); + +function findby(a,key,val){ + for(var i=0;i=5||y2<0||y2>=5||bd[5*y2+x2]!=0)return 5*y+x; + x=x2; y=y2; + } +} + +function applyMove(bd,mv){ + var neuidx=-1,newidx,i; + bd=bd.slice(); + for(i=0;i<25;i++)if(bd[i]==3){neuidx=i;break;} + assert(neuidx!=-1); + if(mv[0]!=-1){ + newidx=idxAfterSlide(bd,neuidx,mv[0]); + bd[newidx]=3; + bd[neuidx]=0; + } + if(mv[1]!=-1&&mv[2]!=-1){ + newidx=idxAfterSlide(bd,mv[1],mv[2]); + bd[newidx]=bd[mv[1]]; + bd[mv[1]]=0; + } + return bd; +} + +function moveIsLegal(bd,mv,forpl){ + var neuidx=-1,newidx,i; + if(bd[mv[1]]!=forpl)return false; + for(i=0;i<25;i++)if(bd[i]==3){neuidx=i;break;} + assert(neuidx!=-1); + if(mv[0]!=-1){ + newidx=idxAfterSlide(bd,neuidx,mv[0]); + if(newidx==neuidx)return false; + } + newidx=idxAfterSlide(applyMove(bd,[mv[0],-1,-1]),mv[1],mv[2]); + if(newidx==mv[1])return false; + return true; +} + +function handleMessage(conn,msg){ + var cmd=msg.split(" "),list,i,game,c2,mv; + switch(cmd[0]){ + case "nick": + conn.ws.send("nick \""+conn.nick+"\""); + break; + /*case "list_free_users": + list=[]; + for(i=0;i=8) || + mv[1]==undefined||isNaN(mv[1])||mv[1]%1!=0||mv[1]<0||mv[1]>=25|| + mv[2]==undefined||isNaN(mv[2])||mv[2]%1!=0||mv[2]<0||mv[2]>=8){ + conn.ws.send("game_doturn [false,\"Invalid move\"]"); + break; + } + if(!moveIsLegal(game.board,mv,game.onturn)){ + conn.ws.send("game_doturn [false,\"Illegal move\"]"); + break; + } + game.board=applyMove(game.board,mv); + console.log("new board:"); + console.log(game.board); + game.onturn=3-game.onturn; + conn.ws.send("game_doturn "+JSON.stringify([true,mv])); + c2=conns[findby(conns,"id",conn.id==game.i1?game.i2:game.i1)]; + c2.ws.send("your_turn "+JSON.stringify(mv)); + game.firstmove=false; + break; + default: + conn.ws.send("error "+cmd[0]); + break; + } +} + +new WebSocketServer({port:WSPORT}).on("connection",function(ws){ + var conn=new Connection(ws); + conns.push(conn); + ws.on("message",function(msg){ + console.log(msg,util.inspect(conn,{depth:0})); + handleMessage(conn,msg); + }); + ws.on("open",function(){ + //hi! + }); + ws.on("close",function(){ + if(conn.gameid!=null){ + var gameidx=findby(games,"id",conn.gameid); + var game=games[gameidx]; + var otherplayeridx=findby(conns,"id",game.i1==conn.id?game.i2:game.i1) + console.log("otherplayeridx="+otherplayeridx); + conns[otherplayeridx].gameid=null; + conn.gameid=null; + games.splice(gameidx,1); + } + }); +}); +console.log("Websocket server started on port "+WSPORT+"."); + +http.createServer(function(req,res){ + if(req.url=="/"){ + res.writeHead(200,{"Content-Type":"text/html"}); + res.end(String(fs.readFileSync("gameserver.html"))); + } else { + res.writeHead(404,{"Content-Type":"text/plain"}); + res.end("404 Not found"); + } +}).listen(HTTPPORT); +console.log("HTTP server started on port "+HTTPPORT+"."); diff --git a/gameserver/package.json b/gameserver/package.json new file mode 100644 index 0000000..0ef588f --- /dev/null +++ b/gameserver/package.json @@ -0,0 +1,15 @@ +{ + "name": "gameserver", + "version": "1.0.0", + "description": "", + "main": "gameserver.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "Tom Smeding (http://tomsmeding.com)", + "license": "ISC", + "dependencies": { + "naampje": "^1.0.1", + "ws": "^0.7.1" + } +} -- cgit v1.2.3-70-g09d2