2018 ソフトウェア設計及び演習用の班Wiki

18::gr08::connection

var PORT = 6010;    // ポートが競合する場合は値を変更すればOK

var http = require('http');
var fs   = require('fs');
var path = require('path');
var socketio = require('socket.io');

var mime = {
    // 必要に応じてMIMEタイプを追加する
    ".html": "text/html",
    ".css": "text/css",
    ".js": "text/javascript"
};

var server = http.createServer(function(request, response) {

    var filePath = (request.url == '/')? '/start.html' : request.url;
    var fullPath = __dirname + filePath;

    response.writeHead(200, {'Content-Type' : mime[path.extname(fullPath)] || "text/plain"});
    fs.readFile(fullPath, function(err, data) {
        if(!err) {
            response.end(data, 'UTF-8');
        }
    })

}).listen(PORT);

var io = socketio.listen(server);


var username =[null,null,null,null,null,null,null,null,null,null];
var job = [];
var user =[{},{},{},{},{},{},{},{},{},{}];
var num;
var numOfuser =0;

var roomcreate = "no";

//勝利判定(脱落者)
function winjudge(){
  var lvwolf = 0;
  var lvuser = live();

  for(var j=0;j<10;j++){
      if(user[j].work==7 && user[j].lod==0){
		      lvwolf++;	      
	}
  }
        if(lvwolf==0){
          io.sockets.emit("leftout" , {value:"a"});
          reset();
        }   
        else if(lvuser-lvwolf <= lvwolf){
 		io.sockets.emit("leftout" , {value:"w"});
 		reset();
	}
}

function reset(){
    username =[null,null,null,null,null,null,null,null,null,null];
    userHash = {};
    numOfuser =0;
    wlfnum = 0;
    wlfso = [];
    v_num = 0;
    voted = 0;
    new_dead = "";
    w = 30;
    knight = 0; 
    roomcreate = "no";
}

io.sockets.on("connection", function (socket) {

//ユーザー情報の取得
  socket.on("Uinfo_inqury", function(data){

  var Uinfo='';
  for(var i=0;i<user.length - 1;i++){
    Uinfo += user[i].name + "," +user[i].lod+"," ;
  }
  Uinfo +=  user[user.length - 1].name + "," +user[user.length - 1].lod;
  io.to(socket.id).emit("Uinfo_respons",{value:Uinfo});
});

  socket.on("UJinfo_inqury", function(data){

  var UJinfo='';
  for(var i=0;i<user.length - 1;i++){
    UJinfo += user[i].name + "," +user[i].work+"," ;
  }
  UJinfo +=  user[user.length - 1].name + "," +user[user.length - 1].work;
  io.to(socket.id).emit("UJinfo_respons",{value:UJinfo});
});
//自分の生死
  socket.on("mylod_inqury", function(data){
    io.to(socket.id).emit("mylod_respons",{value:user[data].lod});
  });
//直近の死者の名前を取得
  socket.on("newdead_inqury", function(data){
    io.to(socket.id).emit("newdead_respons",{value:new_dead});
  });
//部屋が複数個できるのを阻止
    socket.on("room_inquiry",function(data){
      if(roomcreate == "no" && data.value == 1){
        io.to(socket.id).emit("room_respons1" , 0);
      }else if(roomcreate == "yes" && data.value == 2){
          io.to(socket.id).emit("room_respons2" , 0)
      }
    
    });
    socket.on("roomcreate?",function(){
        io.to(socket.id).emit("roomcreate?",{value:roomcreate});
    });
    
//役職配布
  socket.on("shuffle" , function (data){
    console.log(data.value);
    job = [];
    user =[{},{},{},{},{},{},{},{},{},{}];
    job = data.value.split(",");
    numOfuser = job.length;
    for(var i=0;i<numOfuser;i++){
      user[i].work = job[i];
      user[i].lod = 0;
    }
    roomcreate = "yes";
    newjudge =0;
  });
//ユーザー登録
  socket.on("cname" , function (data){
    var check =0;
    if(username[numOfuser - 1] != null){
      check = -1;
    }
    else{
      for(var i=0;i<username.length;i++){
        if(username[i] == null){
          num = i;break;
        }
        if(username[i] == data.value){
          check++;
        }
      }
      if(check == 0){
        io.to(socket.id).emit("namecheck" , 0);
        username[num] = data.value;
        user[num].name = data.value;
        console.log(data.value);
      }
      else if(check >0){
        io.to(socket.id).emit("namecheck" , 1);
      }
      else{
        io.to(socket.id).emit("namecheck" , 2);
      }
    
    }  
  });
//ストーリー表示
  socket.on("readstory_c" , function (name){
    for(var i=0;i<numOfuser;i++){
      if(user[i].name == name.value){
        user[i].story = 1;
      }
    }
    var story = 0;
    for(var i=0;i<numOfuser;i++){
      if(user[i].story == 1){
        story += 1;
      }
    }
    if(story >= numOfuser){
      io.sockets.emit("readstory_s" , 0);
    }
  });
  
//役職確認
 /* 1:あか 2:ぱん 3:えすぱー 4:ごーすと 5:スーパー 6:サイコ 7:ウルフ*/
  socket.on("work_inquiry" , function (data){
    for(var i=0;i<user.length;i++){
      if(user[i].name == data.value){
	  var work =  user[i].work;
	  io.to(socket.id).emit("work_respons",{value:work});
	  io.to(socket.id).emit("num_respons",{value:i});
	  console.log(data.value);
          console.log(work);
          console.log(i);
      }
    }     
  });
  socket.on("readwork_c" , function (num){
    user[num.value].rw = 1;
    var rw = 0;
    for(var i=0;i<numOfuser;i++){
      if(user[i].rw == 1){
        rw += 1;
      }
    }
    if(rw >= numOfuser){
      io.sockets.emit("readwork_s" , 0);
    }
  });


最終更新日:2018/08/03 12:48:50