public override async Task OnRequest()
        {
            //Decode sent data
            ProfilesRequestData request = await DecodePOSTBody <ProfilesRequestData>();

            //Run updates on tribe
            List <WriteModel <DbTribe> > tribeUpdates = new List <WriteModel <DbTribe> >();

            foreach (var tribe in request.tribes)
            {
                //Get filter
                var filterBuilder = Builders <DbTribe> .Filter;
                var filter        = filterBuilder.Eq("tribe_id", tribe.tribe_id) & filterBuilder.Eq("server_id", server._id);

                //Get update
                var updateBuilder = Builders <DbTribe> .Update;
                var update        = updateBuilder.SetOnInsert("server_id", server._id)
                                    .Set("tribe_id", tribe.tribe_id)
                                    .Set("tribe_name", tribe.name)
                                    .Set("tribe_owner", 0)
                                    .Set("last_seen", DateTime.UtcNow);

                //Run
                var u = new UpdateOneModel <DbTribe>(filter, update);
                u.IsUpsert = true;
                tribeUpdates.Add(u);
            }

            //Run updates on players
            List <WriteModel <DbPlayerProfile> > playerUpdates = new List <WriteModel <DbPlayerProfile> >();

            foreach (var user in request.player_profiles)
            {
                //Get Steam info
                string steamName;
                string steamIcon;
                try
                {
                    var steam = await conn.GetSteamProfileById(user.steam_id);

                    steamName = steam.name;
                    steamIcon = steam.icon_url;
                } catch
                {
                    steamName = "STEAM_ERROR";
                    steamIcon = null;
                }

                //Get filter
                var filterBuilder = Builders <DbPlayerProfile> .Filter;
                var filter        = filterBuilder.Eq("steam_id", user.steam_id) & filterBuilder.Eq("server_id", server._id);

                //Get update
                var updateBuilder = Builders <DbPlayerProfile> .Update;
                var update        = updateBuilder.SetOnInsert("server_id", server._id)
                                    .Set("tribe_id", user.tribe_id)
                                    .Set("name", steamName)
                                    .Set("ig_name", user.ark_name)
                                    .Set("ark_id", user.ark_id)
                                    .Set("steam_id", user.steam_id)
                                    .Set("last_seen", DateTime.UtcNow)
                                    .Set("icon", steamIcon);

                //Run
                var u = new UpdateOneModel <DbPlayerProfile>(filter, update);
                u.IsUpsert = true;
                playerUpdates.Add(u);
            }

            //Apply these
            var tribeResponse = await conn.content_tribes.BulkWriteAsync(tribeUpdates);

            var playerResponse = await conn.content_player_profiles.BulkWriteAsync(playerUpdates);

            //Notify new users
            foreach (var p in playerResponse.Upserts)
            {
                //Fetch a user account
                var deltaAccount = await conn.GetUserBySteamIdAsync(request.player_profiles[p.Index].steam_id);

                if (deltaAccount != null)
                {
                    conn.events.NotifyUserGroupsUpdated(deltaAccount._id);
                    conn.events.OnUserServerJoined(server, deltaAccount);
                }
            }

            //Look for new admins
            foreach (var p in request.player_profiles)
            {
                if (p.is_admin)
                {
                    //Grant this player permission to become an admin
                    //This may potentially cause security problems, as ARK is forced to communicate without SSL
                    //Find a Delta account with this
                    var deltaAccount = await conn.GetUserBySteamIdAsync(p.steam_id);

                    if (deltaAccount == null)
                    {
                        continue;
                    }

                    //Check if they already have admin
                    if (server.admins.Contains(deltaAccount._id))
                    {
                        continue;
                    }

                    //Grant admin access
                    await server.GetUpdateBuilder(conn)
                    .AddAdmin(deltaAccount)
                    .Apply();
                }
            }

            //Respond
            await WriteIngestEndOfRequest();
        }
Exemple #2
0
        /// <summary>
        /// To /v1/profiles
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public static async Task OnHttpRequest(Microsoft.AspNetCore.Http.HttpContext e)
        {
            //Authenticate
            DbServer server = await Program.ForceAuthServer(e);

            if (server == null)
            {
                return;
            }

            //Read structures data
            ProfilesRequestData s = Program.DecodeStreamAsJson <ProfilesRequestData>(e.Request.Body);

            //Add all player profiles
            List <WriteModel <DbPlayerProfile> >            playerActions = new List <WriteModel <DbPlayerProfile> >();
            List <WriteModel <DbTribe> >                    tribeActions  = new List <WriteModel <DbTribe> >();
            List <RPCPayloadOnlinePlayers_Player>           rpcPlayers    = new List <RPCPayloadOnlinePlayers_Player>();
            Dictionary <int, RPCPayloadOnlinePlayers_Tribe> rpcTribes     = new Dictionary <int, RPCPayloadOnlinePlayers_Tribe>();

            foreach (var p in s.player_profiles)
            {
                //Fetch Steam info
                var steam = await Program.conn.GetSteamProfileById(p.steam_id);

                if (steam == null)
                {
                    continue;
                }

                //Convert
                DbPlayerProfile profile = new DbPlayerProfile
                {
                    ark_id     = ulong.Parse(p.ark_id),
                    ig_name    = p.ark_name,
                    last_login = p.last_login,
                    name       = steam.name,
                    server_id  = server._id,
                    steam_id   = p.steam_id,
                    tribe_id   = p.tribe_id,
                    icon       = steam.icon_url
                };

                //Create filter for updating this dino
                var filterBuilder = Builders <DbPlayerProfile> .Filter;
                var filter        = filterBuilder.Eq("steam_id", p.steam_id) & filterBuilder.Eq("server_id", server.id);

                //Now, add (or insert) this into the database
                var a = new ReplaceOneModel <DbPlayerProfile>(filter, profile);
                a.IsUpsert = true;
                playerActions.Add(a);

                //Add to RPC messages
                rpcPlayers.Add(new RPCPayloadOnlinePlayers_Player
                {
                    icon     = steam.icon_url,
                    name     = steam.name,
                    tribe_id = p.tribe_id
                });
            }

            //Add all tribe profiles
            foreach (var t in s.tribes)
            {
                //Convert
                DbTribe tribe = new DbTribe
                {
                    server_id   = server._id,
                    tribe_id    = t.tribe_id,
                    tribe_name  = t.name,
                    tribe_owner = 0
                };

                //Create filter for updating this dino
                var filterBuilder = Builders <DbTribe> .Filter;
                var filter        = filterBuilder.Eq("tribe_id", t.tribe_id) & filterBuilder.Eq("server_id", server.id);

                //Now, add (or insert) this into the database
                var a = new ReplaceOneModel <DbTribe>(filter, tribe);
                a.IsUpsert = true;
                tribeActions.Add(a);

                //Add to RPC messages
                rpcTribes.Add(t.tribe_id, new RPCPayloadOnlinePlayers_Tribe
                {
                    name = t.name
                });
            }

            //Apply actions
            if (tribeActions.Count > 0)
            {
                await Program.conn.content_tribes.BulkWriteAsync(tribeActions);

                tribeActions.Clear();
            }
            if (playerActions.Count > 0)
            {
                await Program.conn.content_player_profiles.BulkWriteAsync(playerActions);

                playerActions.Clear();
            }

            //Send RPC message

            /*Program.conn.GetRPC().SendRPCMessageToServer(LibDeltaSystem.RPC.RPCOpcode.PlayerListChanged, new RPCPayloadOnlinePlayers
             * {
             *  players = rpcPlayers,
             *  tribes = rpcTribes
             * }, server);*/

            //Write finished
            e.Response.StatusCode = 200;
            await Program.WriteStringToStream(e.Response.Body, "OK");
        }