コード例 #1
0
        public GameInstance(Database.Game game, Database.GameDevice device, UdpClient client, IPEndPoint endPoint)
        {
            _masterClient = new GamePlayer(device, client, endPoint);
            _masterClient.GotGameUpdate += new EventHandler<GameUpdateEventArgs>(_masterClient_GotGameUpdate);
            _masterClient.ClientDisconnected += new EventHandler(_masterClient_ClientDisconnected);
            _allPlayers.Add(_masterClient);
            _endPointMap.Add(endPoint.ToString().GetHashCode(), _masterClient);

            _game = game;
        }
コード例 #2
0
        public static GameInstance CreateInstance(UdpClient client, IPEndPoint endPoint, NameValueCollection queryString, byte[] mapData)
        {
            lock (_gameDb)
            {
                var games = (from delGame in _gameDb.Games
                             where
                                 delGame.User == queryString["user"]
                             select delGame);

                foreach (Database.Game delGame in games)
                {
                    delGame.Completed = true;
                    //_gameDb.Games.DeleteOnSubmit(delGame);
                }

                Database.Game game = new Database.Game();
                game.PkGame = Guid.NewGuid();
                game.MapData = System.Text.Encoding.ASCII.GetString(mapData);
                game.User = queryString["user"];

                if (string.IsNullOrEmpty(game.User))
                {
                    game.User = queryString["id"];
                }

                game.Completed = false;
                game.Ready = false;
                game.Date = DateTime.Now;
                game.Width = Convert.ToInt32(queryString["width"]);
                game.Height = Convert.ToInt32(queryString["height"]);

                float latitude = 0, longitude = 0;
                float.TryParse(queryString["latitude"], out latitude);
                game.Latitude = latitude;
                float.TryParse(queryString["longitude"], out longitude);
                game.Longitude = longitude;
                game.GameId = Convert.ToInt32(queryString["gameId"]);

                // TODO: lookup here
                float version = 0;
                float.TryParse(queryString["gameversion"], out version);

                Database.GameDevice device = new Database.GameDevice();
                device.PkGameDevice = Guid.NewGuid();
                device.Game = game;
                device.User = queryString["user"];

                if (string.IsNullOrEmpty(device.User))
                {
                    device.User = queryString["id"];
                }
                device.Latitude = game.Latitude;
                device.Longitude = game.Longitude;
                device.LocalAddress = queryString["address"];
                device.LocalPort = int.Parse(queryString["port"]);
                device.Device = queryString["id"];
                device.Date = DateTime.Now;
                device.Ready = true;
                device.Accepted = true;

                _gameDb.Games.InsertOnSubmit(game);
                _gameDb.GameDevices.InsertOnSubmit(device);

                _gameDb.SubmitChanges();

                Trace.WriteLine("Starting Game: " + game.User);

                GameInstance instance = new GameInstance(game, device, client, endPoint);

                GamePlayer.SendUpdate(GameMessage.CreateResponse, client, endPoint, Encoding.ASCII.GetBytes(game.PkGame.ToString()), 1);
                ActiveGames.Add(game.PkGame, instance);

                Thread gameThread = new Thread(new ParameterizedThreadStart(LaunchGame));
                gameThread.IsBackground = true;
                gameThread.Start(instance);

                return instance;
            }
        }
コード例 #3
0
        public static void Handle(ulong clientId, Packet packet)
        {
            var client = ClientManager.GetClient(clientId);

            Dictionary<string, Tdf> data = Utilities.DecodePayload(packet.payload);

            TdfMap attr = (TdfMap)data["ATTR"];
            TdfString gnam = (TdfString)data["GNAM"];
            TdfInteger gset = (TdfInteger)data["GSET"];
            TdfList pcap = (TdfList)data["PCAP"];
            TdfInteger igno = (TdfInteger)data["IGNO"];
            TdfInteger pmax = (TdfInteger)data["PMAX"];
            TdfInteger nres = (TdfInteger)data["NRES"];

            // network topology
            TdfInteger ntop = (TdfInteger)data["NTOP"];
            TdfInteger voip = (TdfInteger)data["VOIP"];

            TdfInteger pres = (TdfInteger)data["PRES"]; // TdfMin
            TdfInteger qcap = (TdfInteger)data["QCAP"];
            //TdfString uuid = (TdfString)data["UUID"];

            TdfList hnet = (TdfList)data["HNET"];

            TdfStruct exip = (TdfStruct)hnet.list[0];
            TdfInteger exipIP = (TdfInteger)exip.data.Find(tdf => tdf.label == "IP");
            TdfInteger exipPort = (TdfInteger)exip.data.Find(tdf => tdf.label == "PORT");

            TdfStruct inip = (TdfStruct)hnet.list[1];
            TdfInteger inipIP = (TdfInteger)inip.data.Find(tdf => tdf.label == "IP");
            TdfInteger inipPort = (TdfInteger)inip.data.Find(tdf => tdf.label == "PORT");

            // TODO: don't get gameId as result but get by clientId after creating the game
            /* ulong gameId = GameManager.CreateGame(

                (int)gset.value,
                (int)igno.value,
                (int)nres.value,
                (int)ntop.value,
                "714b05dc-93bc-49ac-961c-cb38b574f30a"
            ); */

            var level = attr.map["level"].ToString();
            var gametype = attr.map["levellocation"].ToString();

            var game = new Database.Game();
            game.clientId = clientId;
            game.name = gnam.value;
            game.attributes = attr.map;
            game.capacity = pcap.list;
            game.level = attr.map["level"].ToString();
            game.gametype = attr.map["levellocation"].ToString();
            game.maxPlayers = (ushort)pmax.value;
            game.notResetable = (byte)nres.value;
            game.queueCapacity = (ushort)qcap.value;
            game.presenceMode = (PresenceMode)pres.value;
            game.state = GameState.INITIALIZING;
            game.networkTopology = (GameNetworkTopology)ntop.value;
            game.voipTopology = (VoipTopology)voip.value;

            Database.NetworkInfo internalNetworkInfo = new Database.NetworkInfo();
            internalNetworkInfo.ip = inipIP.value;
            internalNetworkInfo.port = (ushort)inipPort.value;

            Database.NetworkInfo externalNetworkInfo = new Database.NetworkInfo();
            externalNetworkInfo.ip = exipIP.value;
            externalNetworkInfo.port = (ushort)exipPort.value;

            game.internalNetworkInfo = internalNetworkInfo;
            game.externalNetworkInfo = externalNetworkInfo;

            ulong gameId = Database.CreateGame(game);

            TdfEncoder encoder = new TdfEncoder();

            encoder.WriteTdf(new List<Tdf>
            {
                // this one is tdfmin
                new TdfInteger("GID", (ulong)gameId)
            });

            byte[] payload = encoder.Encode();

            Utilities.SendPacket(new Packet
            {
                componentId = Component.GAMEMANAGER,
                commandId = 0x1,
                errorCode = 0,
                msgType = MessageType.REPLY,
                msgNum = packet.msgNum,

                payload = payload,
                payloadSize = payload.Length
            }, client.stream);

            GameStateChangeNotification.Notify(gameId, game.state, client.stream);
            GameSetupNotification.Notify(clientId, gameId, client.stream);
        }