static void WebServerOnData(int id, ArraySegment <byte> data)
        {
            _bitBuffer.Clear();
            _bitBuffer.FromArray(data.Array, data.Count);

            byte messageId = _bitBuffer.ReadByte();

            switch (messageId)
            {
            case 1:
            {
                uint qX = _bitBuffer.ReadUInt();
                uint qY = _bitBuffer.ReadUInt();

                PlayerData playerData = _playerDatas[id];
                playerData.qX = qX;
                playerData.qY = qY;

                // Send this position to everyone next state packet
                _dataToSend.Enqueue(playerData);

                break;
            }

            case 8:
            {
                string name = _bitBuffer.ReadString();
                _playerDatas[id].name = name;

                // Tell all the players this new client's name
                _bitBuffer.Clear();
                _bitBuffer.AddByte(9);
                _bitBuffer.AddUShort(_playerDatas[id].id);
                _bitBuffer.AddString(_playerDatas[id].name);
                _bitBuffer.ToArray(_buffer);
                _webServer.SendAll(_connectedIds, new ArraySegment <byte>(_buffer, 0, 23));

                break;
            }

            case 10:
            {
                ushort objectId = _bitBuffer.ReadUShort();
                _movedObjects.Add(objectId);

                break;
            }

            case 11:
            {
                short pointChange = _bitBuffer.ReadShort();
                _playerDatas[id].points += pointChange;

                break;
            }
            }
        }
        static void WebServerOnDisconnect(int id) {
            _connectedIds.Remove(id);
            _playerDatas.Remove(id);

            // Tell other players about the disconnection
            _bitBuffer.Clear();
            _bitBuffer.AddByte(4);
            _bitBuffer.AddUShort((ushort)id);
            _bitBuffer.ToArray(_buffer);
            _webServer.SendAll(_connectedIds, new ArraySegment<byte>(_buffer, 0, 3));
        }
        static void WebServerOnDisconnect(int id)
        {
            _connectedIds.Remove(id);
            _playerDatas.Remove(id);

            // Tell other players about the disconnection
            _bitBuffer.Clear();
            _bitBuffer.AddByte(4);
            _bitBuffer.AddUShort((ushort)id);
            _bitBuffer.ToArray(_buffer);
            _webServer.SendAll(_connectedIds, new ArraySegment <byte>(_buffer, 0, 3));

            // Check if we have less than 2 players and should cancel the game
            if (_connectedIds.Count < 2)
            {
            }
        }
示例#4
0
    private void WebServerOnonDisconnect(int id)
    {
        _connectionIds.Remove(id);

        ushort shortId = (ushort)id;

        if (_playerTransforms.ContainsKey(shortId))
        {
            _playerTransforms.Remove(shortId);
        }

        _bitBuffer.Clear();
        _bitBuffer.AddUShort(0); // send a disconnect message
        _bitBuffer.AddUShort(shortId);
        _bitBuffer.ToArray(_smallBuffer);
        _webServer.SendAll(_connectionIds, new ArraySegment <byte>(_smallBuffer));
    }
示例#5
0
        static void WebServerOnDisconnect(int id)
        {
            _connectedIds.Remove(id);
            _playerDatas.Remove(id);

            // Tell other players about the disconnection
            _bitBuffer.Clear();
            _bitBuffer.AddByte(4);
            _bitBuffer.AddUShort((ushort)id);
            _bitBuffer.ToArray(_buffer);
            _webServer.SendAll(_connectedIds, new ArraySegment <byte>(_buffer, 0, 3));

            // Check if we have less than 2 players and should cancel the game
            if (_currentState != GameState.Waiting && _connectedIds.Count < 2)
            {
                beginTimer?.Stop();
                _currentState = GameState.Waiting;
                SendStateUpdate(_currentState);
            }
        }
        private static void StateUpdateTimerOnElapsed(Object source, ElapsedEventArgs e)
        {
            _bitBuffer.Clear();
            _bitBuffer.AddByte(3);
            _bitBuffer.AddUShort((ushort)_dataToSend.Count);
            foreach (PlayerData playerData in _dataToSend)
            {
                _bitBuffer.AddUShort(playerData.id);
                _bitBuffer.AddUInt(playerData.qX);
                _bitBuffer.AddUInt(playerData.qY);
            }

            _bitBuffer.ToArray(_buffer);
            _webServer.SendAll(_connectedIds, new ArraySegment <byte>(_buffer, 0, 3 + 10 * _dataToSend.Count));
        }
示例#7
0
        static void Main(string[] args)
        {
            _rand = new Random(Environment.TickCount);

            SslConfig sslConfig;
            TcpConfig tcpConfig = new TcpConfig(true, 5000, 20000);

            Console.WriteLine("Setting up secure server");
            sslConfig  = new SslConfig(true, "cert.pfx", "", SslProtocols.Tls12);
            _webServer = new SimpleWebServer(10000, tcpConfig, 16 * 1024, 3000, sslConfig);
            _webServer.Start(Constants.GAME_PORT);
            Console.WriteLine("Server started");

            _webServer.onConnect    += WebServerOnConnect;
            _webServer.onData       += WebServerOnData;
            _webServer.onDisconnect += WebServerOnDisconnect;

            Timer stateUpdateTimer = new Timer(1f / Constants.SERVER_TICKRATE * 1000);

            stateUpdateTimer.Elapsed  += StateUpdateTimerOnElapsed;
            stateUpdateTimer.AutoReset = true;
            stateUpdateTimer.Enabled   = true;

            while (!Console.KeyAvailable)
            {
                _webServer.ProcessMessageQueue();

                // GUARD, DONT DO STATE STUFF IF WE ARE WAITING
                if (_waitingOnStateTimer)
                {
                    continue;
                }
                switch (_currentState)
                {
                case GameState.Waiting:
                {
                    if (_handshakenClientCount >= 2 && _connectedIds.Count >= 2)
                    {
                        _currentState = GameState.Begin;
                        SendStateUpdate(_currentState);
                    }

                    break;
                }

                case GameState.Begin:
                {
                    // Set timer to go to builder state
                    beginTimer           = new Timer(SECONDS_WAITING_IN_BEGIN * 1000);
                    beginTimer.AutoReset = false;
                    beginTimer.Start();
                    _waitingOnStateTimer = true;

                    beginTimer.Elapsed += delegate(Object source, ElapsedEventArgs e) {
                        _waitingOnStateTimer = false;

                        _movedObjects = new Dictionary <ushort, Tuple <ushort, ushort> >();
                        _currentState = GameState.Builder;
                        SendStateUpdate(_currentState);
                    };
                    break;
                }

                case GameState.Builder:
                {
                    // Set timer to go to builder state
                    buildTimer           = new Timer(SECONDS_WAITING_IN_BUILD * 1000);
                    buildTimer.AutoReset = false;
                    buildTimer.Start();
                    _waitingOnStateTimer = true;

                    buildTimer.Elapsed += delegate(Object source, ElapsedEventArgs e) {
                        _waitingOnStateTimer = false;

                        // Reset everyones guesses
                        foreach (PlayerData playerData in _playerDatas.Values)
                        {
                            playerData.guesses.Clear();
                        }

                        // Make sure builder actually moved something,
                        // if he didnt, goto scoring and give everyone else a point
                        if (_movedObjects.Count > 0)
                        {
                            _currentState = GameState.Search;
                        }
                        else
                        {
                            foreach (PlayerData data in _playerDatas.Values)
                            {
                                if (data.id != _builderId)
                                {
                                    data.points += 1;
                                }
                                _playerDatas[_builderId].points -= (ushort)_handshakenClientCount;
                            }
                            _currentState = GameState.Scoring;
                        }

                        SendStateUpdate(_currentState);
                    };
                    break;
                }

                case GameState.Search:
                {
                    // Set timer to go to scoring state
                    searchTimer           = new Timer(SECONDS_WAITING_IN_SEARCH * 1000);
                    searchTimer.AutoReset = false;
                    searchTimer.Start();
                    _waitingOnStateTimer = true;

                    searchTimer.Elapsed += delegate(Object source, ElapsedEventArgs e) {
                        _waitingOnStateTimer = false;

                        _currentState = GameState.Scoring;
                        SendStateUpdate(_currentState);
                    };
                    break;
                }

                case GameState.Scoring:
                {
                    // Set timer to wait for points to come in from clients
                    scoringTimer           = new Timer(SECONDS_WAITING_IN_SCORING * 1000);
                    scoringTimer.AutoReset = false;
                    scoringTimer.Start();
                    _waitingOnStateTimer = true;

                    scoringTimer.Elapsed += delegate(Object source, ElapsedEventArgs e) {
                        _waitingOnStateTimer = false;

                        // Tell everyone everyones scores
                        _bitBuffer.Clear();
                        _bitBuffer.AddByte(7);
                        _bitBuffer.AddUShort((ushort)_playerDatas.Count);

                        foreach (PlayerData data in _playerDatas.Values)
                        {
                            _bitBuffer.AddUShort(data.id);
                            _bitBuffer.AddShort(data.points);
                        }

                        _bitBuffer.ToArray(_buffer);
                        _webServer.SendAll(_connectedIds, new ArraySegment <byte>(_buffer, 0, 3 + 4 * _playerDatas.Count));

                        if (_handshakenClientCount >= 2 && _connectedIds.Count >= 2)
                        {
                            _currentState = GameState.Begin;
                        }
                        else
                        {
                            _currentState = GameState.Waiting;
                        }
                        SendStateUpdate(_currentState);
                    };

                    break;
                }
                }
            }

            Console.WriteLine("Closing server");
            _webServer.Stop();
        }
示例#8
0
        static void WebServerOnData(int id, ArraySegment <byte> data)
        {
            _bitBuffer.Clear();
            _bitBuffer.FromArray(data.Array, data.Count);

            byte messageId = _bitBuffer.ReadByte();

            switch (messageId)
            {
            case 1:
            {
                uint qX = _bitBuffer.ReadUInt();
                uint qY = _bitBuffer.ReadUInt();

                PlayerData playerData = _playerDatas[id];
                playerData.qX = qX;
                playerData.qY = qY;

                // Send this position to everyone next state packet
                _dataToSend.Enqueue(playerData);

                break;
            }

            case 8:
            {
                string name = _bitBuffer.ReadString();
                _playerDatas[id].name       = name;
                _playerDatas[id].handshaked = true;

                _handshakenClientCount += 1;

                // Tell new client their id and the game state and everyone's name and points
                _bitBuffer.Clear();
                _bitBuffer.AddByte(2);
                _bitBuffer.AddUShort((ushort)id);
                _bitBuffer.AddByte((byte)_currentState);

                _bitBuffer.AddUShort((ushort)_playerDatas.Count);
                foreach (var playerData in _playerDatas.Values)
                {
                    _bitBuffer.AddUShort(playerData.id);
                    _bitBuffer.AddString(playerData.name);
                    _bitBuffer.AddShort(playerData.points);
                }

                _bitBuffer.ToArray(_buffer);
                _webServer.SendOne(id, new ArraySegment <byte>(_buffer, 0, 5 + _playerDatas.Count * 20));

                // Tell all the players this new client's name
                _bitBuffer.Clear();
                _bitBuffer.AddByte(9);
                _bitBuffer.AddUShort(_playerDatas[id].id);
                _bitBuffer.AddString(_playerDatas[id].name);
                _bitBuffer.ToArray(_buffer);
                _webServer.SendAll(_connectedIds, new ArraySegment <byte>(_buffer, 0, 23));

                break;
            }

            case 10:
            {
                ushort objectId = _bitBuffer.ReadUShort();
                ushort newX     = _bitBuffer.ReadUShort();
                ushort newY     = _bitBuffer.ReadUShort();
                _movedObjects[objectId] = new Tuple <ushort, ushort>(newX, newY);

                break;
            }

            case 11:
            {
                short pointChange = _bitBuffer.ReadShort();
                _playerDatas[id].points += pointChange;

                // If points are 0 or less, give builder a point
                if (pointChange <= 0)
                {
                    _playerDatas[_builderId].points += 1;
                }

                break;
            }

            case 12:
            {
                _bitBuffer.Clear();
                _bitBuffer.AddByte(13);
                _bitBuffer.AddUShort((ushort)id);
                _bitBuffer.ToArray(_buffer);
                _webServer.SendAll(_connectedIds, new ArraySegment <byte>(_buffer, 0, 3));

                break;
            }
            }
        }
示例#9
0
        static void Main(string[] args)
        {
            _rand = new Random(Environment.TickCount);

            SslConfig sslConfig;
            TcpConfig tcpConfig = new TcpConfig(true, 5000, 20000);

            Console.WriteLine("Setting up secure server");
            sslConfig  = new SslConfig(true, "cert.pfx", "", SslProtocols.Tls12);
            _webServer = new SimpleWebServer(10000, tcpConfig, 16 * 1024, 3000, sslConfig);
            _webServer.Start(Constants.GAME_PORT);
            Console.WriteLine("Server started");

            _webServer.onConnect    += WebServerOnConnect;
            _webServer.onData       += WebServerOnData;
            _webServer.onDisconnect += WebServerOnDisconnect;

            Timer stateUpdateTimer = new Timer(1f / Constants.SERVER_TICKRATE * 1000);

            stateUpdateTimer.Elapsed  += StateUpdateTimerOnElapsed;
            stateUpdateTimer.AutoReset = true;
            stateUpdateTimer.Enabled   = true;

            while (!Console.KeyAvailable)
            {
                _webServer.ProcessMessageQueue();

                // GUARD, DONT DO STATE STUFF IF WE ARE WAITING
                if (_waitingOnStateTimer)
                {
                    continue;
                }
                switch (_currentState)
                {
                case GameState.Waiting:
                {
                    if (_connectedIds.Count >= 2)
                    {
                        _currentState = GameState.Begin;
                        SendStateUpdate(_currentState);
                    }

                    break;
                }

                case GameState.Begin:
                {
                    // Set timer to go to builder state
                    beginTimer           = new Timer(SECONDS_WAITING_IN_BEGIN * 1000);
                    beginTimer.AutoReset = false;
                    beginTimer.Start();
                    _waitingOnStateTimer = true;

                    beginTimer.Elapsed += delegate(Object source, ElapsedEventArgs e) {
                        _waitingOnStateTimer = false;

                        _movedObjects = new List <ushort>();
                        _currentState = GameState.Builder;
                        SendStateUpdate(_currentState);
                    };
                    break;
                }

                case GameState.Builder:
                {
                    // Set timer to go to builder state
                    buildTimer           = new Timer(SECONDS_WAITING_IN_BUILD * 1000);
                    buildTimer.AutoReset = false;
                    buildTimer.Start();
                    _waitingOnStateTimer = true;

                    buildTimer.Elapsed += delegate(Object source, ElapsedEventArgs e) {
                        _waitingOnStateTimer = false;

                        // Reset everyones guesses
                        foreach (PlayerData playerData in _playerDatas.Values)
                        {
                            playerData.guesses.Clear();
                        }
                        _currentState = GameState.Search;
                        SendStateUpdate(_currentState);
                    };
                    break;
                }

                case GameState.Search:
                {
                    // Set timer to go to scoring state
                    searchTimer           = new Timer(SECONDS_WAITING_IN_SEARCH * 1000);
                    searchTimer.AutoReset = false;
                    searchTimer.Start();
                    _waitingOnStateTimer = true;

                    searchTimer.Elapsed += delegate(Object source, ElapsedEventArgs e) {
                        _waitingOnStateTimer = false;

                        _currentState = GameState.Scoring;
                        SendStateUpdate(_currentState);
                    };
                    break;
                }

                case GameState.Scoring:
                {
                    short builderPoints = 0;

                    _bitBuffer.Clear();
                    _bitBuffer.AddByte(7);
                    _bitBuffer.AddUShort((ushort)_playerDatas.Count);


                    foreach (var playerData in _playerDatas.Values)
                    {
                        // GUARD, DON'T SCORE THE BUILDER THIS WAY
                        if (playerData.id == _builderId)
                        {
                            continue;
                        }

                        // Free points for objects builder couldnt move
                        // A point for a correct guess, minus point for a wrong guess
                        int numCorrect = _movedObjects.Distinct().Intersect(playerData.guesses).Count();
                        int newPoints  = (numCorrect * 2) - playerData.guesses.Count + (NUMBER_OF_MOVEABLE_OBJECTS - _movedObjects.Count);
                        playerData.points += (short)newPoints;

                        // Builder gets a point for each player who couldnt find any differences
                        if (numCorrect == 0)
                        {
                            builderPoints += 1;
                        }

                        _bitBuffer.AddUShort(playerData.id);
                        _bitBuffer.AddShort(playerData.points);
                    }

                    _playerDatas[_builderId].points += builderPoints;
                    _bitBuffer.AddUShort((ushort)_builderId);
                    _bitBuffer.AddShort(_playerDatas[_builderId].points);

                    _bitBuffer.ToArray(_buffer);
                    _webServer.SendAll(_connectedIds, new ArraySegment <byte>(_buffer, 0, 3 + 2 * _playerDatas.Count));

                    if (_connectedIds.Count >= 2)
                    {
                        _currentState = GameState.Begin;
                    }
                    else
                    {
                        _currentState = GameState.Waiting;
                    }
                    SendStateUpdate(_currentState);

                    break;
                }
                }
            }

            Console.WriteLine("Closing server");
            _webServer.Stop();
        }
示例#10
0
        static void Main(string[] args)
        {
            _rand = new Random(Environment.TickCount);

            SslConfig sslConfig;
            TcpConfig tcpConfig = new TcpConfig(true, 5000, 20000);

            Console.WriteLine("Setting up secure server");
            sslConfig  = new SslConfig(true, "cert.pfx", "", SslProtocols.Tls12);
            _webServer = new SimpleWebServer(10000, tcpConfig, 16 * 1024, 3000, sslConfig);
            _webServer.Start(Constants.GAME_PORT);
            Console.WriteLine("Server started");

            _webServer.onConnect    += WebServerOnConnect;
            _webServer.onData       += WebServerOnData;
            _webServer.onDisconnect += WebServerOnDisconnect;

            Timer stateUpdateTimer = new Timer(1f / Constants.SERVER_TICKRATE * 1000);

            stateUpdateTimer.Elapsed  += StateUpdateTimerOnElapsed;
            stateUpdateTimer.AutoReset = true;
            stateUpdateTimer.Enabled   = true;

            while (!Console.KeyAvailable)
            {
                _webServer.ProcessMessageQueue();

                // GUARD, DONT DO STATE STUFF IF WE ARE WAITING
                if (_waitingOnStateTimer)
                {
                    continue;
                }
                switch (_currentState)
                {
                case GameState.Waiting:
                {
                    if (_connectedIds.Count >= 2)
                    {
                        _currentState = GameState.Begin;
                        SendStateUpdate(_currentState);
                    }

                    break;
                }

                case GameState.Begin:
                {
                    // Set timer to go to builder state
                    beginTimer           = new Timer(SECONDS_WAITING_IN_BEGIN * 1000);
                    beginTimer.AutoReset = false;
                    beginTimer.Start();
                    _waitingOnStateTimer = true;

                    beginTimer.Elapsed += delegate(Object source, ElapsedEventArgs e) {
                        _waitingOnStateTimer = false;

                        _movedObjects = new HashSet <ushort>();
                        _currentState = GameState.Builder;
                        SendStateUpdate(_currentState);
                    };
                    break;
                }

                case GameState.Builder:
                {
                    // Set timer to go to builder state
                    buildTimer           = new Timer(SECONDS_WAITING_IN_BUILD * 1000);
                    buildTimer.AutoReset = false;
                    buildTimer.Start();
                    _waitingOnStateTimer = true;

                    buildTimer.Elapsed += delegate(Object source, ElapsedEventArgs e) {
                        _waitingOnStateTimer = false;

                        // Reset everyones guesses
                        foreach (PlayerData playerData in _playerDatas.Values)
                        {
                            playerData.guesses.Clear();
                        }
                        _currentState = GameState.Search;
                        SendStateUpdate(_currentState);
                    };
                    break;
                }

                case GameState.Search:
                {
                    // Set timer to go to scoring state
                    searchTimer           = new Timer(SECONDS_WAITING_IN_SEARCH * 1000);
                    searchTimer.AutoReset = false;
                    searchTimer.Start();
                    _waitingOnStateTimer = true;

                    searchTimer.Elapsed += delegate(Object source, ElapsedEventArgs e) {
                        _waitingOnStateTimer = false;

                        _currentState = GameState.Scoring;
                        SendStateUpdate(_currentState);
                    };
                    break;
                }

                case GameState.Scoring:
                {
                    short builderPoints = 0;

                    _bitBuffer.Clear();
                    _bitBuffer.AddByte(7);
                    _bitBuffer.AddUShort((ushort)_playerDatas.Count);

                    _bitBuffer.ToArray(_buffer);
                    _webServer.SendAll(_connectedIds, new ArraySegment <byte>(_buffer, 0, 3 + 2 * _playerDatas.Count));

                    if (_connectedIds.Count >= 2)
                    {
                        _currentState = GameState.Begin;
                    }
                    else
                    {
                        _currentState = GameState.Waiting;
                    }
                    SendStateUpdate(_currentState);

                    break;
                }
                }
            }

            Console.WriteLine("Closing server");
            _webServer.Stop();
        }
示例#11
0
        static void WebServerOnData(int id, ArraySegment<byte> data) {
            _bitBuffer.Clear();
            _bitBuffer.FromArray(data.Array, data.Count);

            byte messageId = _bitBuffer.ReadByte();
            switch (messageId) {
                case 1:
                {
                    uint qX = _bitBuffer.ReadUInt();
                    uint qY = _bitBuffer.ReadUInt();

                    PlayerData playerData = _playerDatas[id];
                    playerData.qX = qX;
                    playerData.qY = qY;

                    // Send this position to everyone next state packet
                    _dataToSend.Enqueue(playerData);
                    
                    break;
                }
                case 8:
                {
                    string name = _bitBuffer.ReadString();
                    _playerDatas[id].name = name;

                    _handshakenClientCount += 1;

                    // Tell all the players this new client's name
                    _bitBuffer.Clear();
                    _bitBuffer.AddByte(9);
                    _bitBuffer.AddUShort(_playerDatas[id].id);
                    _bitBuffer.AddString(_playerDatas[id].name);
                    _bitBuffer.ToArray(_buffer);
                    _webServer.SendAll(_connectedIds, new ArraySegment<byte>(_buffer, 0, 23));

                    break;
                }
                case 10:
                {
                    ushort objectId = _bitBuffer.ReadUShort();
                    ushort newX = _bitBuffer.ReadUShort();
                    ushort newY = _bitBuffer.ReadUShort();
                    _movedObjects[objectId] = new Tuple<ushort, ushort>(newX, newY);

                    break;
                }
                case 11:
                {
                    short pointChange = _bitBuffer.ReadShort();
                    _playerDatas[id].points += pointChange;

                    Console.WriteLine($"Got point change {id} {pointChange}");

                    // If points are 0 or less, give builder a point
                    if (pointChange <= 0) {
                        _playerDatas[_builderId].points += 1;
                    }

                    break;
                }
            }
        }