private void RegisterModules(GameDef gameDef, IEnumerable <IGameTickModule> gameTickModules) { var gameTickModuleDefs = gameDef.GameTickModules; foreach (var moduleDef in gameTickModuleDefs) { var module = gameTickModules.SingleOrDefault(x => x.Name == moduleDef.Name); if (module == null) { throw new InvalidGameDefException($"GameTickModule with name '{moduleDef.Name}' is not registered. Check name and dependency injection."); } RegisterModule(module, moduleDef); } }
private void hgl_OnGameClick(object sender, EventArgs e) { if (Program.PlayWindow != null) { return; } var hg = sender as HostedGameData; Data.Game theGame = Program.GamesRepository.AllGames.FirstOrDefault(g => hg != null && g.Id == hg.GameGuid); if (theGame == null) { return; } if (_joiningGame) { return; } _joiningGame = true; Program.IsHost = false; Program.Game = new Game(GameDef.FromO8G(theGame.FullPath)); var ad = Dns.GetHostAddresses(Skylabs.Lobby.Client.Host); IPAddress ip = ad[0]; if (ad.Length <= 0) { return; } try { if (hg != null) { Program.Client = new Client(ip, hg.Port); } Program.Client.Connect(); Dispatcher.Invoke(new Action(() => frame1.Navigate(new StartGame()))); _joiningGame = false; } catch (Exception ex) { _joiningGame = false; Debug.WriteLine(ex); if (Debugger.IsAttached) { Debugger.Break(); } } }
public UnitsController(ILogger <UnitsController> logger , CurrentUserContext currentUserContext , PlayerRepository playerRepository , UnitRepository unitRepository , UnitRepositoryWrite unitRepositoryWrite , GameDef gameDef ) { this.logger = logger; this.currentUserContext = currentUserContext; this.playerRepository = playerRepository; this.unitRepository = unitRepository; this.unitRepositoryWrite = unitRepositoryWrite; this.gameDef = gameDef; }
public ResourceGrowthSco(ILogger <ResourceGrowthSco> logger , GameDef gameDef , ResourceRepository resourceRepository , ResourceRepositoryWrite resourceRepositoryWrite , AssetRepository assetRepository , UnitRepository unitRepository ) { this.logger = logger; this.gameDef = gameDef; this.resourceRepository = resourceRepository; this.resourceRepositoryWrite = resourceRepositoryWrite; this.assetRepository = assetRepository; this.unitRepository = unitRepository; }
private void VerifyPlayer(GameDef gameDef, PlayerImmutable player) { gameDef.ValidatePlayerType(player.PlayerType, $"Player '{player.PlayerId}' PlayerType"); foreach (var x in player.State.Resources.Keys.ToList()) { VerifyResource(gameDef, player.PlayerId, x); } foreach (var x in player.State.Units) { VerifyUnit(gameDef, player, x); } foreach (var x in player.State.Assets) { VerifyAsset(gameDef, player, x); } }
public UnitRepositoryWrite(ILogger <UnitRepositoryWrite> logger , WorldState world , GameDef gameDef , UnitRepository unitRepository , ResourceRepositoryWrite resourceRepositoryWrite , PlayerRepository playerRepository , IBattleBehavior battleBehavior ) { this.logger = logger; this.world = world; this.gameDef = gameDef; this.unitRepository = unitRepository; this.resourceRepositoryWrite = resourceRepositoryWrite; this.playerRepository = playerRepository; this.battleBehavior = battleBehavior; }
public AssetRepositoryWrite(ILogger <AssetRepositoryWrite> logger , WorldState world , AssetRepository assetRepository , ResourceRepository resourceRepository , ResourceRepositoryWrite resourceRepositoryWrite , ActionQueueRepository actionQueueRepository , GameDef gameDef ) { this.logger = logger; this.world = world; this.assetRepository = assetRepository; this.resourceRepository = resourceRepository; this.resourceRepositoryWrite = resourceRepositoryWrite; this.actionQueueRepository = actionQueueRepository; this.gameDef = gameDef; }
public BattleController(ILogger <PlayerRankingController> logger , GameDef gameDef , CurrentUserContext currentUserContext , ScoreRepository scoreRepository , PlayerRepository playerRepository , UnitRepository unitRepository , UnitRepositoryWrite unitRepositoryWrite ) { this.logger = logger; this.gameDef = gameDef; this.currentUserContext = currentUserContext; this.scoreRepository = scoreRepository; this.playerRepository = playerRepository; this.unitRepository = unitRepository; this.unitRepositoryWrite = unitRepositoryWrite; }
private void ConnectOfflineGameDialogOnClose(object o, DialogResult dialogResult) { BorderButtons.IsEnabled = true; if (dialogResult == DialogResult.OK) { if (connectOfflineGameDialog.Successful) { if (Program.PreGameLobbyWindow == null) { Program.IsHost = false; Program.Game = new Octgn.Game(GameDef.FromO8G(connectOfflineGameDialog.Game.FullPath), null, true); Program.PreGameLobbyWindow = new PreGameLobbyWindow(); Program.PreGameLobbyWindow.Setup(true, Program.MainWindowNew); } } } }
public void InvalidGameDefAssetUniqueness() { var gameDef = new GameDef { Assets = new List <AssetDef> { new AssetDef { Id = Id.AssetDef("asset1") }, new AssetDef { Id = Id.AssetDef("asset2") }, new AssetDef { Id = Id.AssetDef("asset1") }, } }; Assert.Throws <InvalidGameDefException>(() => new GameDefVerifier().Verify(gameDef)); }
public void InvalidGameDefUnitUniqueness() { var gameDef = new GameDef { Units = new List <UnitDef> { new UnitDef { Id = Id.UnitDef("unit1") }, new UnitDef { Id = Id.UnitDef("unit2") }, new UnitDef { Id = Id.UnitDef("unit1") }, } }; Assert.Throws <InvalidGameDefException>(() => new GameDefVerifier().Verify(gameDef)); }
public AssetsController(ILogger <AssetsController> logger , CurrentUserContext currentUserContext , PlayerRepository playerRepository , AssetRepository assetRepository , AssetRepositoryWrite assetRepositoryWrite , ResourceRepository resourceRepository , UnitRepository unitRepository , GameDef gameDef ) { this.logger = logger; this.currentUserContext = currentUserContext; this.playerRepository = playerRepository; this.assetRepository = assetRepository; this.assetRepositoryWrite = assetRepositoryWrite; this.resourceRepository = resourceRepository; this.unitRepository = unitRepository; this.gameDef = gameDef; }
public static bool CheckGameDef(GameDef game) { Program.Game = new Game(game); Program.Game.TestBegin(); var engine = new Engine(true); string[] terr = engine.TestScripts(Program.Game); Program.Game.End(); if (terr.Length > 0) { String ewe = terr.Aggregate("", (current, s) => current + (s + Environment.NewLine)); var er = new Windows.ErrorWindow(ewe); er.ShowDialog(); } return(terr.Length == 0); }
private void GOoffConnOnGameClick(object sender, EventArgs eventArgs) { var hg = sender as Octgn.Data.Game; if (hg == null || Program.PlayWindow != null) { Program.LauncherWindow.NavigationService.Navigate(new Login()); return; } Program.IsHost = false; Data.Game theGame = Program.GamesRepository.Games.FirstOrDefault(g => g.Id == hg.Id); if (theGame == null) { Program.LauncherWindow.Navigate(new Login()); return; } Program.Game = new Game(GameDef.FromO8G(theGame.FullPath), true); Program.LauncherWindow.Navigate(new ConnectLocalGame()); }
public Game(GameDef def, bool isLocal = false) { IsLocal = isLocal; _definition = def; _table = new Table(def.TableDefinition); Variables = new Dictionary <string, int>(); foreach (VariableDef varDef in def.Variables.Where(v => v.Global)) { Variables.Add(varDef.Name, varDef.DefaultValue); } GlobalVariables = new Dictionary <string, string>(); foreach (GlobalVariableDef varDef in def.GlobalVariables) { GlobalVariables.Add(varDef.Name, varDef.DefaultValue); } if (IsLocal) { var i = new InputDlg("Choose a nickname", "Choose a nickname", "User" + new Random().Next().ToString(CultureInfo.InvariantCulture)); var ret = i.GetString(); if (ret == "") { ret = "User" + new Random().Next().ToString(CultureInfo.InvariantCulture); } nick = ret; } else { if (Program.LobbyClient == null || Program.LobbyClient.Me == null) { nick = "User" + new Random().Next().ToString(CultureInfo.InvariantCulture); } else { nick = Program.LobbyClient.Me.User.User; } } }
// C'tor for global items internal Player(GameDef g) { SharedDef globalDef = g.GlobalDefinition; // Register the lPlayer all.Add(this); // Init fields _name = "Global"; Id = 0; PublicKey = 0; if (GlobalVariables == null) { // Create global variables GlobalVariables = new Dictionary <string, string>(); foreach (GlobalVariableDef varD in g.PlayerDefinition.GlobalVariables) { GlobalVariables.Add(varD.Name, varD.Value); } } // Create counters _counters = new Counter[globalDef.Counters != null ? globalDef.Counters.Length : 0]; for (int i = 0; i < Counters.Length; i++) { if (globalDef.Counters != null) { Counters[i] = new Counter(this, globalDef.Counters[i]); } } // Create global's lPlayer groups // TODO: This could fail with a run-time exception on write, make it safe _groups = new Pile[globalDef.Groups != null ? g.GlobalDefinition.Groups.Length + 1 : 0]; for (int i = 1; i < IndexedGroups.Length; i++) { if (globalDef.Groups != null) { _groups[i] = new Pile(this, globalDef.Groups[i - 1]); } } }
private void StartJoinGame(HostedGameViewModel hostedGame, Data.Game game) { Program.IsHost = false; Program.Game = new Game(GameDef.FromO8G(game.FullPath), Program.LobbyClient.Me.UserName); Program.CurrentOnlineGameName = hostedGame.Name; IPAddress hostAddress = Dns.GetHostAddresses(Program.GameServerPath).FirstOrDefault(); if (hostAddress == null) { throw new UserMessageException("There was a problem with your DNS. Please try again."); } try { Program.Client = new Client(hostAddress, hostedGame.Port); Program.Client.Connect(); } catch (Exception) { throw new UserMessageException("Could not connect. Please try again."); } }
private void VerifyResource(GameDef gameDef, PlayerId playerId, ResourceDefId resourceDefId) { gameDef.ValidateResourceDefId(resourceDefId, $"Player '{playerId}' Resources"); }
public static AssetDef?GetAssetDef(this GameDef gameDef, AssetDefId assetDefId) { return(gameDef.Assets.SingleOrDefault(x => x.Id.Equals(assetDefId))); }
//Temporarily store group visibility information for LoadDeck. //bug (google) #20 public void LoadDeck(Deck deck) { Player player = deck.IsShared ? Player.GlobalPlayer : Player.LocalPlayer; GameDef def = Program.Game.Definition; DeckDef deckDef = deck.IsShared ? def.SharedDeckDefinition : def.DeckDefinition; CardDef cardDef = def.CardDefinition; int nCards = deck.CardCount; var ids = new int[nCards]; var keys = new ulong[nCards]; var cards = new Card[nCards]; var groups = new Group[nCards]; var gtmps = new List <GrpTmp>(); //for temp groups visibility int j = 0; foreach (Deck.Section section in deck.Sections) { DeckSectionDef sectionDef = deckDef.Sections[section.Name]; if (sectionDef == null) { throw new InvalidFileFormatException("Invalid section '" + section.Name + "' in deck file."); } Group group = player.Groups.First(x => x.Name == sectionDef.Group); //In order to make the clients know what the card is (if visibility is set so that they can see it), //we have to set the visibility to Nobody, and then after the cards are sent, set the visibility back //to what it was. //bug (google) #20 var gt = new GrpTmp(group, group.Visibility, group.Viewers.ToList()); if (!gtmps.Contains(gt)) { gtmps.Add(gt); group.SetVisibility(false, false); } foreach (Deck.Element element in section.Cards) { CardModel mod = Database.GetCardById(element.Card.Id); for (int i = 0; i < element.Quantity; i++) { //for every card in the deck, generate a unique key for it, ID for it ulong key = ((ulong)Crypto.PositiveRandom()) << 32 | element.Card.Id.Condense(); int id = GenerateCardId(); ids[j] = id; keys[j] = Crypto.ModExp(key); groups[j] = group; var card = new Card(player, id, key, cardDef, mod, true); cards[j++] = card; group.AddAt(card, group.Count); } // Load images in the background string pictureUri = element.Card.Picture; Dispatcher.CurrentDispatcher.BeginInvoke( new Func <string, BitmapImage>(ImageUtils.CreateFrozenBitmap), DispatcherPriority.ApplicationIdle, pictureUri); } } Program.Client.Rpc.LoadDeck(ids, keys, groups); //reset the visibility to what it was before pushing the deck to everybody. //bug (google) #20 foreach (GrpTmp g in gtmps) { switch (g.Visibility) { case GroupVisibility.Everybody: g.Group.SetVisibility(true, false); break; case GroupVisibility.Nobody: g.Group.SetVisibility(false, false); break; default: foreach (Player p in g.Viewers) { g.Group.AddViewer(p, false); } break; } } gtmps.Clear(); gtmps.TrimExcess(); }
public static UnitDef?GetUnitDef(this GameDef gameDef, UnitDefId unitDefId) { return(gameDef.Units.SingleOrDefault(x => x.Id.Equals(unitDefId))); }
public ScoreRepository(GameDef gameDef, WorldState world) { this.gameDef = gameDef; this.world = world; }
// The first asset in prerequisites is the primary asset, where the unit can be built public static IEnumerable <UnitDef> GetUnitsForAsset(this GameDef gameDef, AssetDefId id) { return(gameDef.Units.Where(x => x.Prerequisites.Count > 0 && x.Prerequisites.First().Equals(id))); }
public static IEnumerable <UnitDef> GetUnitsByPlayerType(this GameDef gameDef, PlayerTypeDefId playerTypeId) { return(gameDef.Units.Where(x => x.PlayerTypeRestriction.Equals(playerTypeId))); }
public static UnitViewModel ToUnitViewModel(this UnitImmutable unit, UnitRepository unitRepository, CurrentUserContext currentUserContext, GameDef gameDef) { var unitDef = gameDef.GetUnitDef(unit.UnitDefId); if (unitDef == null) { throw new InvalidGameDefException($"Unit '{unit.UnitDefId}' not found"); } return(new UnitViewModel { UnitId = unit.UnitId.Id, Definition = UnitDefinitionViewModel.Create(unitDef, unitRepository.PrerequisitesMet(currentUserContext.PlayerId, unitDef)), Count = unit.Count, PositionPlayerId = unit.Position?.Id }); }
public void InstallGame() { var ofd = new OpenFileDialog { Filter = "Game definition files (*.o8g)|*.o8g" }; if (ofd.ShowDialog() != true) { return; } //TODO Everything below here now exists in GameDef.Install(), so you could make a def from the file and install. //Fix def filename String newFilename = Uri.UnescapeDataString(ofd.FileName); if (!newFilename.ToLower().Equals(ofd.FileName.ToLower())) { try { File.Move(ofd.FileName, newFilename); } catch (Exception) { MessageBox.Show( "This file is currently in use. Please close whatever application is using it and try again."); return; } } try { GameDef game = GameDef.FromO8G(newFilename); //Move the definition file to a new location, so that the old one can be deleted string path = Path.Combine(Prefs.DataDirectory, "Games", game.Id.ToString(), "Defs"); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } var fi = new FileInfo(newFilename); string copyto = Path.Combine(path, fi.Name); try { if (newFilename.ToLower() != copyto.ToLower()) { File.Copy(newFilename, copyto, true); } } catch (Exception) { MessageBox.Show( "File in use. You shouldn't install games or sets when in the deck editor or when playing a game."); return; } newFilename = copyto; // Open the archive game = GameDef.FromO8G(newFilename); if (!game.CheckVersion()) { return; } //Check game scripts if (!Windows.UpdateChecker.CheckGameDef(game)) { return; } // Check if the game already exists if (Program.GamesRepository.Games.Any(g => g.Id == game.Id)) { if ( MessageBox.Show("This game already exists.\r\nDo you want to overwrite it?", "Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Exclamation) != MessageBoxResult.Yes) { return; } } var gameData = new Data.Game { Id = game.Id, Name = game.Name, Filename = new FileInfo(newFilename).Name, Version = game.Version, CardWidth = game.CardDefinition.Width, CardHeight = game.CardDefinition.Height, CardBack = game.CardDefinition.Back, DeckSections = game.DeckDefinition.Sections.Keys, SharedDeckSections = game.SharedDeckDefinition == null ? null : game.SharedDeckDefinition.Sections.Keys, Repository = Program.GamesRepository, FileHash = game.FileHash }; Program.GamesRepository.InstallGame(gameData, game.CardDefinition.Properties.Values); } catch (FileFormatException) { //Removed ex.Message. The user doesn't need to see the exception MessageBox.Show("Your game definition file is corrupt. Please redownload it.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
public static IEnumerable <string> GetAssetNames(this GameDef gameDef, IList <AssetDefId> assetDefIds) { return(assetDefIds.Select(x => gameDef.GetAssetDef(x)).Where(x => x != null).Select(x => x.Name)); }
private void VerifyAllDefs() { UpdateStatus("Loading Game Definitions..."); try { var g2R = new List <Data.Game>(); using (MD5 md5 = new MD5CryptoServiceProvider()) { foreach (Data.Game g in Program.GamesRepository.Games) { string fhash = ""; UpdateStatus("Checking Game: " + g.Name); var fpath = g.FullPath; if (!File.Exists(fpath)) { _errors.Add("[" + g.Name + "]: Def file doesn't exist at " + fpath); continue; } using (var file = new FileStream(fpath, FileMode.Open)) { byte[] retVal = md5.ComputeHash(file); fhash = BitConverter.ToString(retVal).Replace("-", ""); // hex string } if (fhash.ToLower() == g.FileHash.ToLower()) { continue; } Program.Game = new Game(GameDef.FromO8G(fpath)); Program.Game.TestBegin(); //IEnumerable<Player> plz = Player.All; var engine = new Engine(true); string[] terr = engine.TestScripts(Program.Game); Program.Game.End(); if (terr.Length <= 0) { Program.GamesRepository.UpdateGameHash(g, fhash); continue; } _errors.AddRange(terr); g2R.Add(g); } } foreach (Data.Game g in g2R) { Program.GamesRepository.Games.Remove(g); } if (_errors.Count > 0) { Dispatcher.BeginInvoke(new Action(() => { String ewe = _errors.Aggregate("", (current, s) => current + (s + Environment.NewLine)); var er = new Windows.ErrorWindow(ewe); er.ShowDialog(); })); } } catch (Exception e) { Debug.WriteLine(e); if (Debugger.IsAttached) { Debugger.Break(); } } }
public static void Open(GameDef game, bool readOnly) { OpenedGame = Program.GamesRepository.Games.First(g => g.Id == game.Id); }
public static ResourceDef?GetResourceDef(this GameDef gameDef, ResourceDefId resourceDefId) { return(gameDef.Resources.SingleOrDefault(x => x.Id.Equals(resourceDefId))); }