HTML5 + node.js + socket.io 打造联网斯诺克
发布于 8 年前 作者 idukehui 8267 次浏览 最后一次编辑是 4 年前

简单写了个斯诺克联网游戏,还未完成,希望大家多多指教。

标题: enter image description here 菜单: enter image description here 房间列表: enter image description here 进入无人房间: enter image description here 房间状态更新: enter image description here 游戏开始: enter image description here

server.js

    var rooms = new Array(),
    roomMax = 5;

function Room(index, token) {
    var players = new Array(),
        socket,
        status = 0;
        token = token || "no token";
    this.checkIn = function(playerName, socket) {
        socket = socket;
        console.log("checkIn player: " + playerName);
        if (status < 2) {
            var playerToken = createPlayerToken(players.length);
            players.push(new Player(players.length, playerName, playerToken, socket));
            status++;
            var roomReady = status == 2 ? true : false;
            var data = { roomId: index,roomToken: token, roomReady: roomReady, playerid: players.length - 1, playerToken: playerToken };
            socket.emit("checkIn", data);
        }
        if (status == 2) {
            var data = {};
            var goPlayer = players[0];
            goPlayer.go(data);
        }

        socket.on("cueBall", function(data) {
            console.log(data);
            var currentPlayer = data.playerid;
            var toPlayer = currentPlayer === 0 ? players[1] : players[0];
            console.log(toPlayer);
            toPlayer.emit("cueBall", data);
        });
        socket.on('disconnect', function () {
            //io.sockets.emit('user disconnected');
        });
    };


    this.checkOut = function(playerid, playerToken) {
        var player = players[playerid];
        console.log(playerToken);
        console.log(player);
        if (player.playerToken == playerToken) {
            if (players.length == 1)
                players = new Array();
            players.slice(playerid, 1);
            status--;
        }
    };

    this.getIndex = function() {
        return index;
    };

    this.getStatus = function() {
        return status;
    }
}

function Player(id, name, playerToken, socket) {

    this.id = id;
    this.name = name;
    this.playerToken = playerToken;
    var socket = socket;
    this.emit = function(emitName, data) {
        socket.emit("cueBall", { cueBall: data.cueBall });
    };
    this.go = function(data) {
        socket.emit("go", data);
    }
}

function roomList() {
    var length = rooms.length,
        list = new Array();

    for (var i = 0; i < length; i++) {
        var room = rooms[i];
        list.push({
            id: room.getIndex(),
            status: room.getStatus()
        });
    }
    return list;
}

function initRooms(callback) {
    for (var i = 0; i < roomMax; i++) {
        var token = createToken(i);
        rooms[i] = new Room(i, token);
    }
    callback();
}

function checkInRoom(id) {

}

function createPlayerToken(i) {
    return "player token of player No." + i;
}

function createToken(i) {
    return "room token of room No." + i;
}

initRooms(function() {
    var io = require('socket.io').listen(8010);
    io.set('log level', 1);
    io.sockets.on('connection', function (socket) {
        socket.on("roomList", function(data) {
            var list = roomList();
            socket.emit("roomList", { roomList: list });
        });

        socket.on("checkIn", function(data) {
            var roomIndex = data.roomId,
                playerName = data.playerName,
                room = rooms[roomIndex];

            if (room.getStatus() < 2) {
                room.checkIn(playerName, socket);
            }
        });

        socket.on("checkOut", function(data) {
            console.log(data);
            var roomIndex = data.roomId,
                room = rooms[roomIndex],
                playerid = data.playerid,
                playerToken = data.playerToken;

            if (room.getStatus() > 0) {
                room.checkOut(playerid, playerToken);
            }
        });

    });
});

ps:求个NAE的邀请码

8 回复

邀请码已发。 欢迎在NAE上部署好,让大家来玩玩!

看起来不错. 放到github上吧.

wow,求链接!

不错啊,话说我也想搞个 web game 来玩玩

楼主在杭州么,或者说楼主在阿里技术嘉年华期间在杭州么。我们可以搭个机器,现场让大家玩哦。

Node 用 socket 把游戏都连在一起很带劲啊, 我看到个支持上千用户的.
http://seb.ly/2012/04/node-js-experiment-mmo-asteroids/
http://seb.ly/demos/MMOsteroids.html

这么给力的说

这个要顶的。

回到顶部