Beispiel #1
0
        public static void initTCP()
        {
            packets[0x01] = new x01PINGFROMHOST();
            packets[0x04] = new x04SLOTINFOJOIN();
            packets[0x05] = new x05REJECTJOIN();
            packets[0x06] = new x06PLAYERINFO();
            packets[0x07] = new x07PLAYERLEAVE();
            packets[0x08] = new x08OTHERGAMELOADED();
            packets[0x09] = new x09SLOTINFO();
            packets[0x0A] = new x0aCOUNTDOWNSTART();
            packets[0x0B] = new x0bCOUNTDOWNEND();
            packets[0x0c] = new x0cACTIONBROADCAST();
            packets[0x0F] = new x0fCHATFROMHOST();

            packets[0x1E] = new x1eJOINREQUEST();

            packets[0x21] = new x21PLAYERLEAVEREQ();
            packets[0x23] = new x23OWNGAMELOADED();
            packets[0x26] = new x26CLIENTACTION();
            packets[0x27] = new x27CLIENTKEEPALIVE();
            packets[0x28] = new x28CHATTOHOST();
            packets[0x3d] = new x3dMAPCHECK();

            packets[0x42] = new x42MAPSIZEVERIFY();
            packets[0x46] = new x46PONGTOHOST();
            packets[0x48] = new x48EXTRAACTIONBROADCAST();
        }
Beispiel #2
0
        public void tick()
        {
            if (gameState == GameState.INGAME && players.Count == 0)
            {
                Close();
            }

            foreach (var pp in this.potentialPlayers)
            {
                if (pp.shouldDelete || pp.hasError || !pp.socket.Connected)
                {
                    continue;
                }
                if (!pp.isConnected) //new player, handle them.
                {
                    if (gameState != GameState.LOBBY)
                    {
                        pp.rejectJoin((int)REJECTJOIN.STARTED);
                        pp.shouldDelete = true;
                    }
                    pp.isConnected = true;

                    if (players.Count + 1 >= gameMap.MapNumPlayers)
                    {
                        RemoveFakeHost();
                    }

                    byte pid = GetNextPID();
                    byte sid = GetOpenSlotID();


                    if (sid == 0xff || pid == 0xff) //open slot not found, open pid not found
                    {
                        pp.rejectJoin((int)REJECTJOIN.FULL);
                        return;
                    }

                    slots[sid].computer   = 0;
                    slots[sid].color      = GetUnusedColor();
                    slots[sid].pid        = pid;
                    slots[sid].slotStatus = (byte)SlotStatus.OCCUPIED;
                    UpdateSlots();

                    var pl = new ConnectedPlayer(pp, pid, pp.joinReq.name);
                    players.Add(pl);

                    //SENDING SLOTINFOJOIN
                    x04SLOTINFOJOIN p = new x04SLOTINFOJOIN();
                    p.slots       = this.slots;
                    p.port        = 0; //(ushort)((IPEndPoint) pp.socket.Client.RemoteEndPoint).Port; //could also do pp.joinreq.port and pp.joinreq.ip
                    p.ip          = 0; //(uint)((IPEndPoint) pp.socket.Client.RemoteEndPoint).Address.Address; //this gives warning no matter what apparently.
                    p.pid         = pid;
                    p.playerSlots = (byte)gameMap.MapNumPlayers;
                    p.layoutStyle = gameMap.GetMapLayoutStyle();
                    p.randomseed  = randomseed; //This handles what race you spawn as
                    pp.player     = pl;
                    pl.queuePacket(p);

                    //SENDING MAPCHECK
                    x3dMAPCHECK MCp = new x3dMAPCHECK();
                    MCp.mapPath = this.gameMap.MapPath;
                    MCp.mapCRC  = this.gameMap.MapCRC;
                    MCp.mapInfo = this.gameMap.MapInfo;
                    MCp.mapSHA1 = this.gameMap.MapSha;
                    MCp.mapSize = this.gameMap.MapSize;
                    pl.queuePacket(MCp);

                    //SENDING FAKEHOST INFO (IF EXISTS)
                    if (fakeHostPID != 255)
                    {
                        pl.queuePacket(GetPlayerInfo(fakeHostPID));
                    }

                    var plinfo = GetPlayerInfo(pl);

                    //SENDING OTHER PLAYERS
                    foreach (var otherplayer in players)
                    {
                        if (otherplayer == pl)
                        {
                            continue;
                        }

                        pl.queuePacket(GetPlayerInfo(otherplayer));
                        otherplayer.queuePacket(plinfo);
                    }

                    //quickly throw a ping at em to get shit rollin.

                    //PING Algorithm explained in Player.cs->OnPingPacket();
                    x01PINGFROMHOST pingpkt = new x01PINGFROMHOST();
                    pingpkt.nonce = (int)(GetTimeMS() % 1000000);
                    pl.queuePacket(pingpkt);
                }
            }

            //TODO fakehost is lobby only
            if (gameState == GameState.LOBBY && fakeHostPID == 255 && players.Count < gameMap.MapNumPlayers)
            {
                CreateFakeHost();
            }


            foreach (ConnectedPlayer p in this.players)
            {
                if (p.pp.shouldDelete || p.pp.hasError || !p.pp.socket.Connected)
                {
                    continue;
                }
                p.tick();
            }

            ConnectedPlayer[] badplayers = players.Where(p => p.pp.shouldDelete || p.pp.hasError || !p.pp.socket.Connected).ToArray();
            for (var x = 0; x < badplayers.Length; x++)
            {
                RemovePlayer(badplayers[x], null);
                Console.WriteLine("*** Attempting to remove " + badplayers[x].name);
                if (this.potentialPlayers.TryTake(out badplayers[x].pp) && this.players.TryTake(out badplayers[x]))
                {
                    badplayers[x] = null;
                    Console.WriteLine("Removed player because either error,left,or dc.");
                }
                else
                {
                    Console.WriteLine("Could not remove player " + badplayers[x].name + "!");
                }
            }

            if (needToUpdateSlots && gameState == GameState.LOBBY)
            {
                //TODO(LATER)
                //implement mechanism so that download % is only constantly updated on the person downloading, and once per second on everyone else.
                // in theory could be simple as long as i make another flag "slotDLStatusUpdate" and set it to true if there's a download change
                needToUpdateSlots = false;
                Console.WriteLine("Sending updated slot info as requested");
                CleanSlots(); //make sure they are following the rules
                x09SLOTINFO sinfo = new x09SLOTINFO();
                sinfo.slots       = slots;
                sinfo.playerSlots = (byte)gameMap.MapNumPlayers;
                sinfo.layoutStyle = gameMap.GetMapLayoutStyle();
                sinfo.randomseed  = randomseed;
                SendAll(sinfo);
            }

            if (gameState == GameState.INGAME)
            {
                ProcessGameActions();
            }
        }