Exemple #1
0
        public static void UpdateGame(MyContext db, UpdateGameInput input)
        {
            //Add new state
            var newstate = new GamesDataStates
            {
                StateId   = GuidGenerator.GenerateSequentialGuid(),
                GameId    = input.Gamerec.EntryId,
                State     = input.Gameobj.Serialize(),
                RenderRep = input.Gameobj.Render(),
                Timestamp = DateTime.UtcNow
            };

            db.GamesDataStates.Add(newstate);

            //Add chat
            if (!String.IsNullOrEmpty(input.Gameobj.ChatMsgs))
            {
                var newchat = new GamesDataChats
                {
                    ChatId  = GuidGenerator.GenerateSequentialGuid(),
                    GameId  = input.Gamerec.EntryId,
                    OwnerId = null,
                    Message = input.Gameobj.ChatMsgs
                };
                db.GamesDataChats.Add(newchat);
            }

            if (!input.Gameobj.Gameover)
            {
                //Update clocks
                var clock   = db.GamesDataClocks.Single(x => x.GameId.Equals(input.Gamerec.EntryId) && x.OwnerId.Equals(input.Mover));
                var newbank = clock.Bank + input.Gamerec.ClockInc;
                if (newbank > input.Gamerec.ClockMax)
                {
                    newbank = input.Gamerec.ClockMax;
                }
                clock.Bank = (short)newbank;
            }

            //Update whoseturn
            db.GamesDataWhoseturn.RemoveRange(db.GamesDataWhoseturn.Where(x => x.GameId.Equals(input.Gamerec.EntryId)));
            foreach (var p in input.Gameobj.Whoseturn())
            {
                var node = new GamesDataWhoseturn
                {
                    GameId  = input.Gamerec.EntryId,
                    OwnerId = GuidGenerator.HelperStringToBA(PlayerId2OwnerId(db, p))
                };
                db.GamesDataWhoseturn.Add(node);
            }

            //Check for end of game
            if (input.Gameobj.Gameover)
            {
                input.Gamerec.Closed = true;

                //Archive the game
                List <GamesDataStates> allstates = db.GamesDataStates.Where(x => x.GameId.Equals(input.Gamerec.EntryId)).ToList();
                allstates.Add(newstate);
                JObject rec = JObject.FromObject(new
                {
                    header = new
                    {
                        reportId = GuidGenerator.HelperBAToString(input.Gamerec.EntryId),
                        game     = new
                        {
                            id       = GuidGenerator.HelperBAToString(input.Gamerec.GameMetaId),
                            name     = input.Gamerec.GameMeta.Name,
                            variants = String.IsNullOrWhiteSpace(input.Gamerec.Variants) ? new string[0] : input.Gamerec.Variants.Split('\n'),
                        },
                        dates = new
                        {
                            start = allstates.First().Timestamp,
                            end   = allstates.Last().Timestamp.Truncate(TimeSpan.FromSeconds(1))
                        },
                        //Add `event` one day
                        timeControl = String.Join('/', new string[] { input.Gamerec.ClockStart.ToString(), input.Gamerec.ClockInc.ToString(), input.Gamerec.ClockMax.ToString() }),
                        players     = input.Gameobj.Players,
                        results     = input.Gameobj.Results()
                                      //Add `termination` one day
                    },
                    moves = input.Gameobj.MovesArchive(allstates.Select(x => x.State).ToArray())
                }
                                                 );

                GamesArchive archive = new GamesArchive
                {
                    ReportId = input.Gamerec.EntryId,
                    Json     = rec.ToString()
                };
                db.GamesArchive.Add(archive);
            }
        }
Exemple #2
0
        public static void ExecuteCommand(MyContext db, ExecuteCommandInput input)
        {
            var cmd = (ConsoleCommands)input.Console.Command;
            //First check for unanimity
            bool passed = true;

            foreach (var vote in input.Console.ConsolesVotes)
            {
                if (!vote.Vote)
                {
                    passed = false;
                    break;
                }
            }
            if (!passed)
            {
                string chat = "";
                switch (cmd)
                {
                case ConsoleCommands.DRAW:
                    chat = "The draw offer was rejected.";
                    break;

                case ConsoleCommands.FREEZE:
                    chat = "The request to freeze the clocks was rejected.";
                    break;

                case ConsoleCommands.THAW:
                    chat = "The request to thaw the clocks was rejected.";
                    break;

                default:
                    throw new ArgumentException("The console command was not recognized. This should never happen.");
                }
                var newchat = new GamesDataChats
                {
                    ChatId  = GuidGenerator.GenerateSequentialGuid(),
                    GameId  = input.Gamerec.EntryId,
                    Game    = input.Gamerec,
                    Message = chat
                };
                input.Gamerec.GamesDataChats.Add(newchat);
            }
            else
            {
                switch (cmd)
                {
                case ConsoleCommands.DRAW:
                    Game newobj = input.Gameobj.Draw();
                    var  args   = new UpdateGameInput
                    {
                        Gameobj = newobj,
                        Gamerec = input.Gamerec
                    };
                    UpdateGame(db, args);
                    break;

                case ConsoleCommands.FREEZE:
                    input.Gamerec.ClockFrozen = true;
                    input.Gamerec.GamesDataChats.Add(
                        new GamesDataChats
                    {
                        ChatId  = GuidGenerator.GenerateSequentialGuid(),
                        GameId  = input.Gamerec.EntryId,
                        Game    = input.Gamerec,
                        Message = "The clocks have been frozen."
                    }
                        );
                    break;

                case ConsoleCommands.THAW:
                    input.Gamerec.ClockFrozen = false;
                    input.Gamerec.GamesDataChats.Add(
                        new GamesDataChats
                    {
                        ChatId  = GuidGenerator.GenerateSequentialGuid(),
                        GameId  = input.Gamerec.EntryId,
                        Game    = input.Gamerec,
                        Message = "The clocks have been thawed."
                    }
                        );
                    break;

                default:
                    throw new ArgumentException("The console command was not recognized. This should never happen.");
                }
            }
        }
Exemple #3
0
        private void GamesData(MyContext db)
        {
            Field <GamesDataType>(
                "moveGame",
                description: "Submit a move to a game",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <MoveGameInputType> > {
                Name = "input"
            }
                    ),
                resolve: _ => {
                var context = (UserContext)_.UserContext;
                var input   = _.GetArgument <MoveGameDTO>("input");

                var user = db.Owners.SingleOrDefault(x => x.CognitoId.Equals(context.cognitoId));
                if (user == null)
                {
                    throw new ExecutionError("You don't appear to have a user account! Only registered users can play.");
                }

                byte[] binaryid;
                try
                {
                    binaryid = GuidGenerator.HelperStringToBA(input.id);
                }
                catch
                {
                    throw new ExecutionError("The game ID you provided is malformed. Please verify and try again.");
                }

                //Does this game id exist?
                var game = db.GamesData.SingleOrDefault(x => x.EntryId.Equals(binaryid));
                if (game == null)
                {
                    throw new ExecutionError("The game id you provided (" + input.id + ") does not appear to exist.");
                }

                if (game.Closed)
                {
                    throw new ExecutionError("This game has ended. No further moves are possible.");
                }

                //Load the latest game state
                Game gameobj;
                try
                {
                    gameobj = GameFactory.LoadGame(game.GameMeta.Shortcode, game.GamesDataStates.Last().State);
                }
                catch (Exception e)
                {
                    throw new ExecutionError("An error occurred while trying to load the game. Please alert the administrators. The game code said the following: " + e.Message);
                }

                //Is the move legal?
                Game newgameobj;
                try
                {
                    newgameobj = gameobj.Move(GuidGenerator.HelperBAToString(user.PlayerId), input.move);
                }
                catch (Exception e)
                {
                    throw new ExecutionError("Your move was not accepted. The game code said the following: " + e.Message);
                }

                //Build game object
                var uginput = new UpdateGameInput
                {
                    Gameobj = newgameobj,
                    Gamerec = game,
                    Mover   = user.OwnerId
                };
                DBFuncs.UpdateGame(db, uginput);
                db.SaveChanges();

                return(game);
            }
                );
            Field <GamesDataPreviewType>(
                "moveGamePreview",
                description: "Preview a move to a game",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <MoveGameInputType> > {
                Name = "input"
            }
                    ),
                resolve: _ => {
                var context = (UserContext)_.UserContext;
                var input   = _.GetArgument <MoveGameDTO>("input");

                var user = db.Owners.SingleOrDefault(x => x.CognitoId.Equals(context.cognitoId));
                if (user == null)
                {
                    throw new ExecutionError("You don't appear to have a user account! Only registered users can play.");
                }

                byte[] binaryid;
                try
                {
                    binaryid = GuidGenerator.HelperStringToBA(input.id);
                }
                catch
                {
                    throw new ExecutionError("The game ID you provided is malformed. Please verify and try again.");
                }

                //Does this game id exist?
                var game = db.GamesData.SingleOrDefault(x => x.EntryId.Equals(binaryid));
                if (game == null)
                {
                    throw new ExecutionError("The game id you provided (" + input.id + ") does not appear to exist.");
                }

                if (game.Closed)
                {
                    throw new ExecutionError("This game has ended. No further moves are possible.");
                }

                //Load the latest game state
                Game gameobj;
                try
                {
                    gameobj = GameFactory.LoadGame(game.GameMeta.Shortcode, game.GamesDataStates.Last().State);
                }
                catch (Exception e)
                {
                    throw new ExecutionError("An error occurred while trying to load the game. Please alert the administrators. The game code said the following: " + e.Message);
                }

                //Is the move legal?
                Game newgameobj;
                try
                {
                    newgameobj = gameobj.Move(GuidGenerator.HelperBAToString(user.PlayerId), input.move);
                }
                catch (Exception e)
                {
                    throw new ExecutionError("Your move was not accepted. The game code said the following: " + e.Message);
                }

                return(newgameobj);
            }
                );
            Field <GamesDataChatType>(
                "newChat",
                description: "Post a new chat message to a game",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <NewChatInputType> > {
                Name = "input"
            }
                    ),
                resolve: _ => {
                var context = (UserContext)_.UserContext;
                var input   = _.GetArgument <NewChatDTO>("input");

                var user = db.Owners.SingleOrDefault(x => x.CognitoId.Equals(context.cognitoId));
                if (user == null)
                {
                    throw new ExecutionError("You don't appear to have a user account! Only registered users can chat in games.");
                }

                byte[] binaryid;
                try
                {
                    binaryid = GuidGenerator.HelperStringToBA(input.id);
                }
                catch
                {
                    throw new ExecutionError("The game ID you provided is malformed. Please verify and try again.");
                }

                //Does this game id exist?
                var game = db.GamesData.SingleOrDefault(x => x.EntryId.Equals(binaryid));
                if (game == null)
                {
                    throw new ExecutionError("The game id you provided (" + input.id + ") does not appear to exist.");
                }

                var rec = new GamesDataChats
                {
                    ChatId  = GuidGenerator.GenerateSequentialGuid(),
                    GameId  = binaryid,
                    OwnerId = user.OwnerId,
                    Message = input.message
                };
                db.GamesDataChats.Add(rec);
                db.SaveChanges();
                return(rec);
            }
                );
            Field <GamesDataType>(
                "resignGame",
                description: "Resign from a game in progress",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <ResignGameInputType> > {
                Name = "input"
            }
                    ),
                resolve: _ => {
                var context = (UserContext)_.UserContext;
                var input   = _.GetArgument <ResignGameDTO>("input");

                var user = db.Owners.SingleOrDefault(x => x.CognitoId.Equals(context.cognitoId));
                if (user == null)
                {
                    throw new ExecutionError("You don't appear to have a user account! Only registered users can play.");
                }

                if (!input.confirmed)
                {
                    throw new ExecutionError("You must set `confirmed` to `true` to proceed. This action cannot be undone!");
                }

                byte[] binaryid;
                try
                {
                    binaryid = GuidGenerator.HelperStringToBA(input.id);
                }
                catch
                {
                    throw new ExecutionError("The game ID you provided is malformed. Please verify and try again.");
                }

                //Does this game id exist?
                var game = db.GamesData.SingleOrDefault(x => x.EntryId.Equals(binaryid));
                if (game == null)
                {
                    throw new ExecutionError("The game id you provided (" + input.id + ") does not appear to exist.");
                }

                if (game.Closed)
                {
                    throw new ExecutionError("This game has ended. No further moves are possible.");
                }

                //Load the latest game state
                Game gameobj;
                try
                {
                    gameobj = GameFactory.LoadGame(game.GameMeta.Shortcode, game.GamesDataStates.Last().State);
                }
                catch (Exception e)
                {
                    throw new ExecutionError("An error occurred while trying to load the game. Please alert the administrators. The game code said the following: " + e.Message);
                }

                //Is the move legal?
                Game newgameobj;
                try
                {
                    newgameobj = gameobj.Resign(GuidGenerator.HelperBAToString(user.PlayerId));
                }
                catch (Exception e)
                {
                    throw new ExecutionError("The resignation failed. The game code said the following: " + e.Message);
                }

                //Build game object
                var uginput = new UpdateGameInput
                {
                    Gameobj = newgameobj,
                    Gamerec = game,
                    Mover   = user.OwnerId
                };
                DBFuncs.UpdateGame(db, uginput);
                db.SaveChanges();

                return(game);
            }
                );
        }