Exemplo n.º 1
0
        public static ulong CreateGame(Game game)
        {
            MySqlConnection conn   = null;
            MySqlDataReader reader = null;

            ulong gameId = 0;

            try
            {
                conn = new MySqlConnection(connectionString);
                conn.Open();

                MySqlCommand cmd = new MySqlCommand();
                cmd.Connection  = conn;
                cmd.CommandText = "INSERT INTO games(clientid, name, attributes, capacity, level, gametype, max_players, not_resetable, queue_capacity, presence_mode, state, network_topology, voip_topology, internal_ip, internal_port, external_ip, external_port) VALUES(@clientid, @name, @attributes, @capacity, @level, @gametype, @max_players, @not_resetable, @queue_capacity, @presence_mode, @state, @network_topology, @voip_topology, @internal_ip, @internal_port, @external_ip, @external_port); SELECT LAST_INSERT_ID();";
                cmd.Prepare();

                cmd.Parameters.AddWithValue("@clientid", game.clientId);
                cmd.Parameters.AddWithValue("@name", game.name);
                cmd.Parameters.AddWithValue("@attributes", JsonConvert.SerializeObject(game.attributes));
                cmd.Parameters.AddWithValue("@capacity", JsonConvert.SerializeObject(game.capacity));
                cmd.Parameters.AddWithValue("@level", game.level);
                cmd.Parameters.AddWithValue("@gametype", game.gametype);
                cmd.Parameters.AddWithValue("@max_players", game.maxPlayers);
                cmd.Parameters.AddWithValue("@not_resetable", game.notResetable);
                cmd.Parameters.AddWithValue("@queue_capacity", game.queueCapacity);
                cmd.Parameters.AddWithValue("@presence_mode", game.presenceMode);
                cmd.Parameters.AddWithValue("@state", game.state);
                cmd.Parameters.AddWithValue("@network_topology", game.networkTopology);
                cmd.Parameters.AddWithValue("@voip_topology", game.voipTopology);

                cmd.Parameters.AddWithValue("@internal_ip", game.internalNetworkInfo.ip);
                cmd.Parameters.AddWithValue("@internal_port", game.internalNetworkInfo.port);

                cmd.Parameters.AddWithValue("@external_ip", game.externalNetworkInfo.ip);
                cmd.Parameters.AddWithValue("@external_port", game.externalNetworkInfo.port);

                reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    gameId = reader.GetUInt64(0);
                }
            }
            catch (MySqlException ex)
            {
                Log.Error(ex.ToString());
            }
            finally
            {
                if (conn != null)
                {
                    conn.Dispose();
                }

                if (reader != null)
                {
                    reader.Dispose();
                }
            }

            ClientManager.UpdateClientGameID(game.clientId, gameId);

            Log.Info(string.Format("Created game {0} with name '{1}'.", gameId, game.name));

            return(gameId);
        }
Exemplo n.º 2
0
        public static void Handle(ulong clientId, Packet packet, SslStream stream)
        {
            Dictionary <string, Tdf> data = Utilities.DecodePayload(packet.payload);

            TdfInteger gid  = (TdfInteger)data["GID"];
            TdfUnion   pnet = (TdfUnion)data["PNET"];
            TdfStruct  valu = (TdfStruct)pnet.data.Find(tdf => tdf.label == "VALU");

            TdfStruct  exip     = (TdfStruct)valu.data.Find(tdf => tdf.label == "EXIP");
            TdfInteger exipIP   = (TdfInteger)exip.data.Find(tdf => tdf.label == "IP");
            TdfInteger exipPort = (TdfInteger)exip.data.Find(tdf => tdf.label == "PORT");

            TdfStruct  inip     = (TdfStruct)valu.data.Find(tdf => tdf.label == "INIP");
            TdfInteger inipIP   = (TdfInteger)inip.data.Find(tdf => tdf.label == "IP");
            TdfInteger inipPort = (TdfInteger)inip.data.Find(tdf => tdf.label == "PORT");

            var client = ClientManager.GetClient(clientId);

            if (Database.GameExists(gid.value))
            {
                // update stuff
                ClientManager.UpdateClientInternalNetworkData(clientId, inipIP.value, (ushort)inipPort.value);
                ClientManager.UpdateClientExternalNetworkData(clientId, exipIP.value, (ushort)inipPort.value);
                ClientManager.UpdateClientGameID(clientId, (ulong)gid.value);

                Log.Info(string.Format("User {0} is joining game {1}.", client.user.id, gid.value));

                TdfEncoder encoder = new TdfEncoder();

                encoder.WriteTdf(new List <Tdf>
                {
                    new TdfInteger("GID", (ulong)gid.value),
                    new TdfInteger("JGS", 0)
                });

                byte[] payload = encoder.Encode();

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

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

                var game       = Database.GetGameByID(gid.value);
                var gameClient = ClientManager.GetClient(game.clientId);

                // TODO: check if only userupdated needed
                UserAddedNotification.Notify(clientId, stream, true);
                UserUpdatedNotification.Notify(client.persona.id, stream);

                JoiningPlayerInitiateConnectionsNotification.Notify(clientId, stream);
                UserSessionExtendedDataUpdateNotification.Notify(clientId, stream, true);

                PlayerJoiningNotification.Notify(clientId, gameClient.stream);
                PlayerClaimingReservationNotification.Notify(clientId, gameClient.stream);
                UserSessionExtendedDataUpdateNotification.Notify(clientId, gameClient.stream, true);
            }
            else
            {
                Log.Warn(string.Format("User {0} wanted to a game that doesn't exist ({1}).", client.user.id, gid.value));

                /*
                 * not sure if we should set the error code to GAMEMANAGER_ERR_NO_DEDICATED_SERVER_FOUND (0x12D0004)
                 * or GAMEMANAGER_ERR_INVALID_GAME_ID (0x20004)
                 * */

                Utilities.SendPacket(new Packet
                {
                    componentId = Component.GAMEMANAGER,
                    commandId   = 0x9,
                    errorCode   = 0x20004, // GAMEMANAGER_ERR_INVALID_GAME_ID
                    msgType     = MessageType.ERROR_REPLY,
                    msgNum      = packet.msgNum,

                    payload     = null,
                    payloadSize = 0
                }, stream);
            }
        }