Пример #1
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)
                    {
                        _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();
                        }
                        _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:
                {
                    // 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);

                            Console.WriteLine($"Points: {data.id} {data.points}");
                        }

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

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

                    break;
                }
                }
            }

            Console.WriteLine("Closing server");
            _webServer.Stop();
        }
Пример #2
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();
        }
    public static void Pack(GameEntity e, BitBuffer buffer)
    {
        ushort counter = 0;

        var hasId = false;

        if (e.hasId)
        {
            hasId = true;
            counter++;
        }

        var hasPosition = false;

        if (e.hasPosition)
        {
            hasPosition = true;
            counter++;
        }

        var hasDirection = false;

        if (e.hasDirection)
        {
            hasDirection = true;
            counter++;
        }

        var hasSprite = false;

        if (e.hasSprite)
        {
            hasSprite = true;
            counter++;
        }

        var hasMover = false;

        if (e.isMover)
        {
            hasMover = true;
            counter++;
        }

        var hasMove = false;

        if (e.hasMove)
        {
            hasMove = true;
            counter++;
        }

        var hasLastMoveTick = false;

        if (e.hasLastMoveTick)
        {
            hasLastMoveTick = true;
            counter++;
        }

        var hasMoverID = false;

        if (e.hasMoverID)
        {
            hasMoverID = true;
            counter++;
        }

        var hasTick = false;

        if (e.hasTick)
        {
            hasTick = true;
            counter++;
        }

        var hasIce = false;

        if (e.hasIce)
        {
            hasIce = true;
            counter++;
        }

        var hasCharacter = false;

        if (e.isCharacter)
        {
            hasCharacter = true;
            counter++;
        }

        var hasControlledBy = false;

        if (e.hasControlledBy)
        {
            hasControlledBy = true;
            counter++;
        }

        var hasConnection = false;

        if (e.hasConnection)
        {
            hasConnection = true;
            counter++;
        }

        var hasSync = false;

        if (e.isSync)
        {
            hasSync = true;
            counter++;
        }


        buffer.AddUShort(counter);

        if (hasId)
        {
            e.id.Serialize(buffer);
        }

        if (hasPosition)
        {
            e.position.Serialize(buffer);
        }

        if (hasDirection)
        {
            e.direction.Serialize(buffer);
        }

        if (hasSprite)
        {
            e.sprite.Serialize(buffer);
        }

        if (hasMover)
        {
            buffer.AddUShort(4);
        }

        if (hasMove)
        {
            e.move.Serialize(buffer);
        }

        if (hasLastMoveTick)
        {
            e.lastMoveTick.Serialize(buffer);
        }

        if (hasMoverID)
        {
            e.moverID.Serialize(buffer);
        }

        if (hasTick)
        {
            e.tick.Serialize(buffer);
        }

        if (hasIce)
        {
            e.ice.Serialize(buffer);
        }

        if (hasCharacter)
        {
            buffer.AddUShort(10);
        }

        if (hasControlledBy)
        {
            e.controlledBy.Serialize(buffer);
        }

        if (hasConnection)
        {
            e.connection.Serialize(buffer);
        }

        if (hasSync)
        {
            buffer.AddUShort(13);
        }
    }
Пример #4
0
 public void Serialize(BitBuffer bitBuffer)
 {
     bitBuffer.AddUShort(10);
 }
Пример #5
0
 private static void StateUpdateTimerOnElapsed(Object source, ElapsedEventArgs e)
 {
     _bitBuffer.Clear();
     _bitBuffer.AddByte(3);
     _bitBuffer.AddUShort((ushort)_dataToSend.Count);
 }
    public void Serialize(BitBuffer bitBuffer)
    {
        bitBuffer.AddUShort(7);

        bitBuffer.AddString(value);
    }
    public void Serialize(BitBuffer bitBuffer)
    {
        bitBuffer.AddUShort(2);

        bitBuffer.AddUShort(Tickrate);
    }
Пример #8
0
    public static void Pack(GameEntity e, BitBuffer buffer)
    {
        ushort counter = 0;

        var hasId = false;

        if (e.hasId)
        {
            hasId = true;
            counter++;
        }

        var hasCharacter = false;

        if (e.isCharacter)
        {
            hasCharacter = true;
            counter++;
        }

        var hasControlledBy = false;

        if (e.hasControlledBy)
        {
            hasControlledBy = true;
            counter++;
        }

        var hasConnection = false;

        if (e.hasConnection)
        {
            hasConnection = true;
            counter++;
        }

        var hasSync = false;

        if (e.isSync)
        {
            hasSync = true;
            counter++;
        }


        buffer.AddUShort(counter);

        if (hasId)
        {
            e.id.Serialize(buffer);
        }

        if (hasCharacter)
        {
            buffer.AddUShort(1);
        }

        if (hasControlledBy)
        {
            e.controlledBy.Serialize(buffer);
        }

        if (hasConnection)
        {
            e.connection.Serialize(buffer);
        }

        if (hasSync)
        {
            buffer.AddUShort(4);
        }
    }
Пример #9
0
    public void Serialize(BitBuffer bitBuffer)
    {
        bitBuffer.AddUShort(2);

        bitBuffer.AddFloat(Value);
    }
    public void Serialize(BitBuffer bitBuffer)
    {
        bitBuffer.AddUShort(3);

        bitBuffer.AddString(Name);
    }
    public void Serialize(BitBuffer bitBuffer)
    {
        bitBuffer.AddUShort(6);

        bitBuffer.AddLong(value);
    }
Пример #12
0
    public void Serialize(BitBuffer bitBuffer)
    {
        bitBuffer.AddUShort(12);

        bitBuffer.AddUShort(Id);
    }
    public void Serialize(BitBuffer bitBuffer)
    {
        bitBuffer.AddUShort(0);

        bitBuffer.AddString(Message);
    }