Пример #1
0
        /// <summary>
        /// Creates a new owned ship.
        /// Ship starts in drydocked status.
        /// </summary>
        /// <param name="output"></param>
        /// <param name="user"></param>
        /// <param name="hull"></param>
        /// <param name="insurance"></param>
        /// <param name="name"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public static bool Create(ref UserShip output, int user, int hull,
                                  int insurance, string name, int status)
        {
            int result = DBI.DoPreparedAction(
                $@"INSERT INTO UserShip (user, hull, insurance, number, name, 
				status, statusDate, final) 
				VALUES (@user, @hull, @insurance, 
				COALESCE((
					SELECT MAX(number)+1 FROM UserShip 
					WHERE hull IN (
						SELECT h1.id FROM Hull h1, Hull h2 
						WHERE h1.symbol = h2.symbol AND h2.id = @hull
					)),1), @name, @status, 
				strftime('%s', 'now'), 0);"                ,
                new Tuple <string, object>("@user", user),
                new Tuple <string, object>("@hull", hull),
                new Tuple <string, object>("@insurance", insurance),
                new Tuple <string, object>("@name", name),
                new Tuple <string, object>("@status", status));

            if (result == 1)
            {
                return(UserShip.FetchById(ref output, DBI.LastInsertRowId));
            }
            return(false);
        }
Пример #2
0
        ProcessChangeShipStatus(ANWI.Messaging.IMessagePayload p)
        {
            ANWI.Messaging.ChangeShipStatus css
                = p as ANWI.Messaging.ChangeShipStatus;

            bool success = false;

            Datamodel.UserShip ship = null;
            if (Datamodel.UserShip.FetchById(ref ship, css.shipId))
            {
                ship.status = (int)css.status;
                if (Datamodel.UserShip.StoreUpdateStatus(ship))
                {
                    success = true;
                    logger.Info($"Updated ship {css.shipId} status" +
                                $" to {css.status}");
                }
                else
                {
                    logger.Error("Failed to update status for " +
                                 $"ship {css.shipId}");
                }
            }
            else
            {
                logger.Error($"Failed to fetch ship {css.shipId} " +
                             "to update status");
            }

            return(new ANWI.Messaging.ConfirmUpdate(p, success, css.shipId));
        }
Пример #3
0
 /// <summary>
 /// Gets a vessel by name
 /// </summary>
 /// <param name="name"></param>
 /// <returns></returns>
 public static Vessel FetchByName(string name)
 {
     Datamodel.UserShip s = null;
     if (Datamodel.UserShip.FetchByName(ref s, name))
     {
         return(new Vessel(s));
     }
     else
     {
         return(null);
     }
 }
Пример #4
0
 /// <summary>
 /// Gets a vessel by ID
 /// </summary>
 /// <param name="id"></param>
 /// <returns></returns>
 public static Vessel FetchById(int id)
 {
     Datamodel.UserShip s = null;
     if (Datamodel.UserShip.FetchById(ref s, id))
     {
         return(new Vessel(s));
     }
     else
     {
         return(null);
     }
 }
Пример #5
0
        /// <summary>
        /// Gets an owned ship by name
        /// </summary>
        /// <param name="output"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static bool FetchByName(ref UserShip output, string name)
        {
            SQLiteDataReader reader = DBI.DoPreparedQuery(
                @"SELECT * FROM UserShip WHERE name = @name LIMIT 1;",
                new Tuple <string, object>("@name", name));

            if (reader != null && reader.Read())
            {
                output = UserShip.Factory(reader);
                return(true);
            }
            return(false);
        }
Пример #6
0
        /// <summary>
        /// Gets an owned ship by ID
        /// </summary>
        /// <param name="output"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool FetchById(ref UserShip output, int id)
        {
            SQLiteDataReader reader = DBI.DoPreparedQuery(
                "SELECT * FROM UserShip WHERE id = @id LIMIT 1;",
                new Tuple <string, object>("@id", id));

            if (reader != null && reader.Read())
            {
                output = UserShip.Factory(reader);
                return(true);
            }
            return(false);
        }
Пример #7
0
        public static UserShip Factory(SQLiteDataReader reader)
        {
            UserShip result = new UserShip(
                id: Convert.ToInt32(reader["id"]),
                user: Convert.ToInt32(reader["user"]),
                hull: Convert.ToInt32(reader["hull"]),
                insurance: Convert.ToInt32(reader["insurance"]),
                number: Convert.ToInt32(reader["number"]),
                name: (string)reader["name"],
                status: Convert.ToInt32(reader["status"]),
                statusDate: Convert.ToInt64(reader["statusDate"]),
                final: Convert.ToBoolean(reader["final"])
                );

            return(result);
        }
Пример #8
0
        public static UserShip Factory()
        {
            UserShip result = new UserShip(
                id: -1,
                user: -1,
                hull: -1,
                insurance: 0,
                number: 0,
                name: "",
                status: 0,
                statusDate: 0,
                final: false
                );

            return(result);
        }
Пример #9
0
        /// <summary>
        /// Gets the list of all ships which are active or drydocked.
        /// </summary>
        /// <param name="output"></param>
        /// <returns></returns>
        public static bool FetchAvailable(ref List <UserShip> output)
        {
            output = new List <UserShip>();

            SQLiteDataReader reader = DBI.DoQuery(
                @"SELECT * FROM UserShip
				WHERE status = 0 OR status = 3;"                );

            while (reader != null && reader.Read())
            {
                UserShip us = UserShip.Factory(reader);
                output.Add(us);
            }

            return(true);
        }
Пример #10
0
        /// <summary>
        /// Gets the list of all ships.
        /// Does not include ships which were destroyed or decommed
        /// more than a month ago
        /// </summary>
        /// <param name="output"></param>
        /// <returns></returns>
        public static bool FetchRegistry(ref List <UserShip> output)
        {
            output = new List <UserShip>();

            SQLiteDataReader reader = DBI.DoQuery(
                @"SELECT * FROM UserShip
				WHERE (status != 1 AND status != 4)
				OR statusDate > strftime('%s', 'now', '-7 days');"                );

            while (reader != null && reader.Read())
            {
                UserShip us = UserShip.Factory(reader);
                output.Add(us);
            }

            return(true);
        }
Пример #11
0
        private LiteVessel(Datamodel.UserShip s)
        {
            id         = s.id;
            name       = s.name;
            isLTI      = Convert.ToBoolean(s.insurance);
            hullNumber = s.number;
            status     = (VesselStatus)s.status;

            Datamodel.User u = null;
            if (!Datamodel.User.FetchById(ref u, s.user))
            {
                throw new ArgumentException("Ship does not have valid owner ID");
            }
            owner = u.name;

            _hullId = s.hull;
        }
Пример #12
0
        public static UserShip Factory(int id, int user, int hull,
                                       int insurance, int number, string name, int status,
                                       long statusDate, bool final)
        {
            UserShip result = new UserShip(
                id: id,
                user: user,
                hull: hull,
                insurance: insurance,
                number: number,
                name: name,
                status: status,
                statusDate: statusDate,
                final: final
                );

            return(result);
        }
Пример #13
0
        /// <summary>
        /// Updates only the status and as of date for a ship
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool StoreUpdateStatus(UserShip input)
        {
            int result = DBI.DoPreparedAction(
                @"UPDATE UserShip 
				SET status = @status, statusDate = strftime('%s', 'now')
				WHERE id = @id;"                ,
                new Tuple <string, object>("@status", input.status),
                new Tuple <string, object>("@id", input.id));

            if (result == 1)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Пример #14
0
        ProcessNewShip(ANWI.Messaging.IMessagePayload p)
        {
            ANWI.Messaging.NewShip ns = p as ANWI.Messaging.NewShip;

            logger.Info($"Creating new ship {ns.name}");

            bool   success   = false;
            int    confirmId = 0;
            string message   = null;

            // Check that the ship name is available
            if (Datamodel.UserShip.IsNameAvailable(ns.name))
            {
                // If any older ships exist with this name set their final
                // flag so they cannot be brought back into service
                Datamodel.UserShip.FinalizeOlderShips(ns.name);

                // Create the new ship
                Datamodel.UserShip ship = null;
                if (Datamodel.UserShip.Create(ref ship, ns.ownerId, ns.hullId,
                                              Convert.ToInt32(ns.LTI), ns.name,
                                              (int)VesselStatus.DRYDOCKED))
                {
                    success   = true;
                    confirmId = ship.id;
                    logger.Info($"Create successful");
                }
                else
                {
                    logger.Error($"Create failed");
                    message = "Server error";
                }
            }
            else
            {
                logger.Error("Name not available");
                message = "Another ship with this name is currently in an" +
                          " active (not destroyed or decommed) state.";
            }

            return(new ANWI.Messaging.ConfirmUpdate(p, success, confirmId, message));
        }
Пример #15
0
        private Vessel(Datamodel.UserShip s)
        {
            id         = s.id;
            name       = s.name;
            isLTI      = Convert.ToBoolean(s.insurance);
            hullNumber = s.number;
            status     = (VesselStatus)s.status;
            statusDate =
                DateTimeOffset.FromUnixTimeSeconds(s.statusDate).DateTime;
            final   = s.final;
            _hullId = s.hull;

            Datamodel.User u = null;
            if (!Datamodel.User.FetchById(ref u, s.user))
            {
                throw new ArgumentException(
                          "Ship does not have valid owner ID");
            }
            ownerId   = u.id;
            ownerName = u.name;
        }
Пример #16
0
        /// <summary>
        /// Updates a ship
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool Store(UserShip input)
        {
            int result = DBI.DoPreparedAction(
                @"UPDATE UserShip SET user = @user, hull = @hull,
				insurance = @insurance, number = @number, name = @name, 
				status = @status, statusDate = @statusDate, final = @final 
				WHERE id = @id;"                ,
                new Tuple <string, object>("@user", input.user),
                new Tuple <string, object>("@hull", input.hull),
                new Tuple <string, object>("@insurance", input.insurance),
                new Tuple <string, object>("@number", input.number),
                new Tuple <string, object>("@name", input.name),
                new Tuple <string, object>("@status", input.status),
                new Tuple <string, object>("@statusDate", input.statusDate),
                new Tuple <string, object>("@final", input.final),
                new Tuple <string, object>("@id", input.id));

            if (result == 1)
            {
                return(true);
            }
            return(false);
        }