Ejemplo n.º 1
0
        [FunctionName(HTTP.Endpoints.MINE)]  /* API Endpoints: /api/mine?asteroid=12&ship=5&amount=44 */
        public static async Task <string> Mine([HttpTrigger(AuthorizationLevel.Anonymous, HTTP.POST, Route = HTTP.Endpoints.MINE)] HttpRequest req)
        {
            try {
                string ship         = req.Query[HTTP.Endpoints.Parameters.SHIP];
                string asteroid     = req.Query[HTTP.Endpoints.Parameters.ASTEROID];
                double mined_amount = double.Parse(req.Query[HTTP.Endpoints.Parameters.AMOUNT]);

                /* Gets current asteroid size to determine if mining fully depleted asteroid */
                double asteroid_size = 0;
                if (double.TryParse(SQLHandler.Select(new Dictionary <string, string> {
                    { SQL.TABLE, Asteroids.ALIAS },
                    { SQL.COLUMNS, Asteroids.SIZE },
                    { SQL.CONDITION, SQL.IsEqual(Asteroids.ID, asteroid) }
                }), out asteroid_size))
                {
                    /* Asteroid was not fully mined */
                    if (asteroid_size > mined_amount)
                    {
                        /* Reduce size of asteroid and pass to ship */
                        return(SQLHandler.Update(new Dictionary <string, string> {
                            { SQL.TABLE, Asteroids.ALIAS },
                            { SQL.CONDITION, SQL.IsEqual(Asteroids.ID, asteroid) },
                            { SQL.COLUMN, Asteroids.SIZE },
                            { SQL.VALUE, (asteroid_size - mined_amount).ToString("F") }
                        }) + SQLHandler.Update(new Dictionary <string, string> {
                            { SQL.TABLE, Ships.ALIAS },
                            { SQL.CONDITION, SQL.IsEqual(Ships.ID, ship) },
                            { SQL.COLUMN, Ships.DATA },
                            { SQL.VALUE, mined_amount.ToString("F") }
                        }) + SQLHandler.Insert(
                                   Mines.ALIAS,
                                   new List <string> ()
                        {
                            WrapValues(new string[] {
                                ship,
                                asteroid,
                                mined_amount.ToString("F"),
                                DateTime.UtcNow.ToString(SQL.Format.DATETIME)
                            })
                        }
                                   ));
                        /* Asteroid was fully depleted when mined */
                    }
                    else
                    {
                        /* Delete asteroid, give all size to ship */
                        return(SQLHandler.Delete(new Dictionary <string, string> {
                            { Asteroids.ALIAS, SQL.IsEqual(Asteroids.ID, asteroid) }
                        }) +
                               SQLHandler.Update(new Dictionary <string, string> {
                            { SQL.TABLE, Ships.ALIAS },
                            { SQL.CONDITION, SQL.IsEqual(Ships.ID, ship) },
                            { SQL.COLUMN, Ships.DATA },
                            { SQL.VALUE, mined_amount.ToString("F") }
                        }) + SQLHandler.Insert(
                                   Mines.ALIAS,
                                   new List <string> ()
                        {
                            WrapValues(new string[] {
                                ship,
                                asteroid,
                                mined_amount.ToString("F"),
                                DateTime.UtcNow.ToString(SQL.Format.DATETIME)
                            })
                        }
                                   ));
                    }
                }
                return("Not possible?");
            } catch (Exception ex) {
                return(ex.ToString());
            }
            return(new InvalidOperationException().ToString());  /* InvalidOperationException returned if attempting to mine more than asteroid has */
        }
Ejemplo n.º 2
0
        [FunctionName(HTTP.Endpoints.SET)]  /* API Endpoints: /api/set?flag=reset, /api/set?flag=add&table=players */
        public static async Task <string> Set([HttpTrigger(AuthorizationLevel.Anonymous, HTTP.POST, Route = HTTP.Endpoints.SET)] HttpRequest req)
        {
            try {
                /* Reads data into table and returns transaction receipt */
                string flag  = req.Query[HTTP.Endpoints.Parameters.FLAG];
                string table = req.Query[HTTP.Endpoints.Parameters.TABLE];

                switch (flag)
                {
                case HTTP.Endpoints.Parameters.Values.ADD:
                    switch (table)
                    {
                    case Ships.ALIAS:
                        dynamic ship = await GetBody(req.Body);

                        return(SQLHandler.Insert(
                                   Ships.ALIAS,
                                   new List <string> ()
                        {
                            WrapValues(new string[] {
                                ship.id, ship.player_id, ship.current_system, ship.name, ship.data, ship.position_x, ship.position_y
                            })
                        }
                                   ));
                    }
                    break;

                case HTTP.Endpoints.Parameters.Values.RESET:

                    /* Pulls galaxy JSON from HTTP Body */
                    dynamic galaxy = await GetBody(req.Body);

                    /* Initializing Table Values */
                    Dictionary <string, List <string> > values = new Dictionary <string, List <string> > {
                        { Galaxies.ALIAS, new List <string> () },
                        { Systems.ALIAS, new List <string> () },
                        { SystemConnections.ALIAS, new List <string> () },
                        { Planets.ALIAS, new List <string> () },
                        { Asteroids.ALIAS, new List <string> () }
                    };

                    /* Agregrates Table Values */
                    values[Galaxies.ALIAS].Add(WrapValues(new string[] {
                        galaxy.id, galaxy.seed
                    }));
                    foreach (dynamic system in galaxy.systems)
                    {
                        values[Systems.ALIAS].Add(WrapValues(new string[] {
                            system.id, galaxy.id, system.seed, system.position_x, system.position_y
                        }));
                        values[SystemConnections.ALIAS].AddRange(((IEnumerable <dynamic>)system.connected_systems).Select(
                                                                     connected_system => WrapValues(new string[] {
                            system.id, connected_system, "1"
                        })
                                                                     ));
                        values[Planets.ALIAS].AddRange(((IEnumerable <dynamic>)system.planets).Select(
                                                           planet => WrapValues(new string[] {
                            planet.id, system.id, planet.seed, planet.radius, planet.theta, planet.size, planet.density, planet.composition, planet.is_habitable, planet.is_inhabited, planet.kardashev_level, planet.economy_type
                        })
                                                           ));
                        values[Asteroids.ALIAS].AddRange(((IEnumerable <dynamic>)system.asteroids).Select(
                                                             asteroid => WrapValues(new string[] {
                            asteroid.id, system.id, asteroid.seed, asteroid.radius, asteroid.theta, asteroid.size, asteroid.density, asteroid.composition, asteroid.is_mineable, asteroid.is_regenerating
                        })
                                                             ));
                    }
                    return(SQLHandler.Delete(values.ToDictionary(value => value.Key, value => SQL.ALL)) + /* For every table referenced, clear all existing values */
                           SQLHandler.Insert(values));                                                    /* For every table referenced, inject values */
                }
            } catch (Exception ex) {
                return(ex.ToString());
            }
            return(new InvalidOperationException().ToString());  /* InvalidOperationException returned if attempting to add a value not supported yet */
        }