public void MontecarloDecide(TGame currentState) { Debug.Log("EMPEZANDO"); Ready = false; ActionToExecute = Utilities.Actions.None; simulator.StartTreeSearch(currentState); }
/// <summary> /// Receives a game in its state and trains from the given state /// </summary> /// <param name="game">Game in a certain state from where the train will begin</param> public void StartTraining(TGame game) { currentGame = game; Thread t = new Thread(Train); t.Start(); }
/// <summary> /// Same as take snapshot, but stores the current state in a new TGame and returns it /// DOES NOT OVERWRITE SNAPSHOT /// </summary> public TGame TakeAndGetSnapshot() { TEventEntity[] s_planets = new TEventEntity[planets.Length]; TGame game = new TGame(); for (int i = 0; i < planets.Length; i++) { s_planets[i] = planets[i].GetSnapshot(game); } TPlayer[] s_players = new TPlayer[players.Length]; List <TEventEntity> aux; for (int i = 0; i < players.Length; i++) { aux = new List <TEventEntity>(); s_players[i] = players[i].GetSnapshot(s_planets); foreach (TEventEntity ent in s_planets) { if (ent.CurrentPlayerOwner == s_players[i].Id) { aux.Add(ent); } } s_players[i].SetPlanets(aux); } List <TAttackInfo> s_attacks = new List <TAttackInfo>(pendingAttacks); game.Initialize(s_players, s_planets, s_attacks, weHaveAWinner, winner); return(game); }
public async Task <GenericResponse <BooleanResponse> > RemoveUserGame(TGame dbGame, string gameUsername, long userId) { try { TUserGame dbUserGame = _databaseContext.TUserGame .FirstOrDefault(ug => ug.UserId == userId && ug.GameId == dbGame.Id && ug.Username == gameUsername); // Error case if not found if (dbUserGame == null) { return(new GenericResponse <BooleanResponse>($"Cannot find {gameUsername} for game {dbGame.Id}", null)); } _databaseContext.TUserGame.Remove(dbUserGame); await _databaseContext.SaveChangesAsync(); return(new GenericResponse <BooleanResponse>(new BooleanResponse { Success = true })); } catch (DbUpdateException e) { return(new GenericResponse <BooleanResponse>("Error while removing game", e)); } }
public M_Node(TGame state, int position) { State = state; Visits = Score = 0; freeChildren = GlobalData.NUMBER_OF_ACTIONS; this.position = position; Available = true; }
/// <summary> /// Empties the tree and makes a new one with the provided state /// </summary> /// <param name="currentState">The new state of the game from wich the simulation will start</param> public void StartTreeSearch(TGame currentState) { stop = false; Clock.Instance.AddTimerForMontecarlo(new System.Timers.ElapsedEventHandler(TimesUp)); //We substract one because the mother thread will always be there currentActiveSimulations = 0; ThreadPool.QueueUserWorkItem(MotherThread, currentState); }
private void TestAdvanceTurnAndExecuteAction() { TGame game = DebugSnapshot(); print(""); M_FlowController test = new M_FlowController(); test.TESTAdvanceTurnAndExecuteActions(0, Utilities.Actions.Wait, game); DebugSnapshot(game); }
/// <summary> /// EF在修改一个数据,通过主键来修改 /// </summary> public void TestEFeditor() { var db = new GGN_NewsEntities(); var game = new TGame { Id = 1, Name = "天天跑酷", State = true }; db.TGame.Add(game); Console.WriteLine(db.SaveChanges()); }
public async static Task <TGame> Add() { var rpository = Game.Scene.GetComponent <GameRpository>(); var game = new TGame(); game.SetCreator("Admin"); game.SetUpdater("Admin"); await rpository.DBContext.InsertAsync(game); return(game); }
public void TestEFDelete() { var db = new GGN_NewsEntities(); var game = new TGame { Id = 2 }; db.TGame.Attach(game); db.TGame.Remove(game); Console.WriteLine(db.SaveChanges()); }
/// <summary> /// Creates a game and trains the given amount of times (1 by default) /// </summary> /// <param name="planetsInfo">Planets for the game</param> /// <param name="players">Players for the game</param> /// <param name="gamesToPlay">Games that will be played (default = 1)</param> public void StartTraining(TrainingPlanetInfo[] planetsInfo, PlayerSettings[] players, int gamesToPlay = 1) { Debug.Log("EMPEZANDO PARTIDA - CREANDO JUEGO"); TotalGamesToPlay = gamesToPlay; currentGame = new TGame(players, planetsInfo); Thread t = new Thread(Train); t.Start(); }
static void Main(string[] args) { string path = args.Length > 0 ? args[0] : string.Empty; var htmlReader = new HtmlReader(); var html = htmlReader.Read(path); var game = new TGame();; game.Create(800, 600, 0, html); game.Run(30.0); }
public void TestEFAdd() { var db = new GGN_NewsEntities(); var game = new TGame { Name = "水果忍者", State = true }; db.TGame.Add(game); Console.WriteLine(db.SaveChanges()); }
/// <summary> /// Generates the root and the first expansion of nodes /// </summary> /// <param name="gameStateAtRoot">THe game that will be in the root</param> /// <param name="id">Player id of the owner of the tree</param> /// <param name="maxConcurrentTasks">Maximmum number of simultaneous simulations</param> public MontecarloTree(TGame gameStateAtRoot, int id) { Debug.Log("Creando arbol"); tree = new M_Node[100]; tree[0] = new M_Node(gameStateAtRoot, 0); if (tree[0].State.SomeoneWon() != GlobalData.NO_PLAYER) { Debug.Log("VICTORIA EN LA RAIZ"); } this.id = id; flowController = new M_FlowController(); treeMutex = new Mutex(); ExtendNode(tree[0]); }
//Use this constructor to instantiate in a certain state public TPlanet(int currentUnits, int maxHealth, int currentHealth, int expForNextLevel, int currentLevel, Vector3 pos, int ownerId, int maxLevel, int currentExp, int currentContestsant, int id, TGame game) { this.currentUnits = currentUnits; this.maxHealth = maxHealth; this.currentHealth = currentHealth; this.expForNextLevel = expForNextLevel; this.currentLevel = currentLevel; position = pos; currentPlayerOwner = ownerId; this.maxLevel = maxLevel; this.currentExp = currentExp; this.currentContestantId = currentContestsant; this.id = id; TakeSnapshot(); currentGame = game; }
public TPlanet(int owner, Vector3 pos, int maxLevel, int id, TGame game) { currentUnits = 0; maxHealth = MAX_PLANET_HEALTH; currentHealth = MaxHealth; expForNextLevel = EXP_FOR_LEVEL_1; currentLevel = 0; position = pos; currentPlayerOwner = owner; this.maxLevel = maxLevel; currentExp = 0; currentContestantId = GlobalData.NO_PLAYER; this.id = id; TakeSnapshot(); currentGame = game; }
public async Task <GenericResponse <BooleanResponse> > RemoveUserGame(string shortName, string gameUsername, long userId) { // First check if the game exist TGame dbGame = await _gameDataAccess.GetDbGame(shortName); if (dbGame == null) { return(new GenericResponse <BooleanResponse>($"Game {shortName} is not valid", null)); } // Then delete the cache await _cacheDeleteHelper.DeleteUserCache(shortName, userId); // If it's the case then delete GenericResponse <BooleanResponse> res = await _userGameDataAccess.RemoveUserGame(dbGame, gameUsername, userId); return(res); }
/// <summary> /// This method will simulate a decision and advance as many turns as it needs for that the next turn after those triggers an AI tick. /// </summary> /// <param name="idOfSimulatedPlayer">The player that will execute the provided action</param> /// <param name="act">The action that the provided player will do</param> /// /// <param name="game">Optional. THe state of the game from which the simulation will start</param> /// <returns>New instance of TGame with the new state of the game</returns> public TGame AdvanceTurnAndExecuteActions(int idOfSimulatedPlayer, Actions act, TGame game = null) { //if no game was provided, we use the one that belongs to the class if (game == null) { game = currentGame; } ///////first we will siomulate one turn for the AIs to decide and for this AI to execute the provided action///// int turnsToAdvance = 1; //check pending attacks and get turns for the arrival of the next attack turnsToAdvance = game.CheckPendingAttacks(turnsToAdvance); //check for victory (IMPORTANT TO DO IT AFTER THE ATTACKS AND NOT BEFORE) game.SomeoneWon(); if (game.WeHaveAWinner) { return(game); } //advance the obtained amount of normal turns game.CreateUnits(turnsToAdvance); game.AITick(idOfSimulatedPlayer, act); ///////after that, we will advance the game enough turns that the next one will trigger an AITick////////// turnsToAdvance = (int)(GlobalData.MILISECONDS_BETWEEN__AI_TICKS / GlobalData.MILISECONDS_BETWEEN_TICKS) - 1; turnsToAdvance = game.CheckPendingAttacks(turnsToAdvance); //check for victory (IMPORTANT TO DO IT AFTER THE ATTACKS AND NOT BEFORE) game.SomeoneWon(); if (game.WeHaveAWinner) { return(game); } //advance the obtained amount of normal turns game.CreateUnits(turnsToAdvance); return(game); }
public void TestEFEditorOther() { //将对象加入ef容器,并获取当前ef 实体对象的 状态管理对象 var db = new GGN_NewsEntities(); //db.TGame.MergeOption = MergeOption.NoTracking; var game = new TGame { Id = 1, Name = "水果忍者修改", State = false }; //将对象加入 EF容器,并获取 当前实体对象 的 状态管理对象。单纯的使用Attach对象不能够获取ef 的状态管理对象 var entry = db.Entry(game); //设置 该对象 为未被修改过 entry.State = EntityState.Unchanged; //设置 该对象 为修改过 entry.Property("Name").IsModified = true; Console.WriteLine(db.SaveChanges()); }
private TGame DebugSnapshot(TGame g = null) { TGame snapshot; if (g == null) { print("TAKING SNAPSHOT...."); snapshot = currentGame.GetSnapshot(); } else { snapshot = g; } print("DEBUGGING PLANETS"); foreach (TPlanet pl in snapshot.Planets) { print("Planet " + pl.Id + " located in " + pl.Position + " belongs to " + pl.CurrentPlayerOwner); print("This planet has (health/exp/units) " + pl.CurrentHealth + "/" + pl.CurrentExp + "/" + pl.CurrentUnits); print("It is in level " + pl.CurrentLevel + " of a miaxximum of " + pl.MaxLevel + " and needs " + (pl.ExpForNextLevel - pl.CurrentExp) + "/" + pl.ExpForNextLevel + " to level up"); } print("DEBUGGING PLAYERS"); foreach (TPlayer pl in snapshot.Players) { print("Player " + pl.Id + " has a total of " + pl.Planets.Count + " planets under its domain"); print("This planets are:"); foreach (TPlanet plan in pl.Planets) { print("Planet " + plan.Id); } } print("DEBUGGING ATTACKS"); foreach (TAttackInfo att in snapshot.PendingAttacks) { print("Attack from player " + att.Player + " to planet " + att.Destiny + " with " + att.Units); print("Will reach objective in " + att.remainingTurns); } return(snapshot); }
public TGame GetSnapshot() { TEventEntity[] s_planets = new TEventEntity[planets.Length]; TGame game = new TGame(); for (int i = 0; i < planets.Length; i++) { s_planets[i] = planets[i].TakeSnapshot(game); } TPlayer[] s_players = new TPlayer[players.Length]; List <TEventEntity> aux; for (int i = 0; i < players.Length; i++) { aux = new List <TEventEntity>(); s_players[i] = players[i].GetSnapshotInstance(s_planets); foreach (TEventEntity ent in s_planets) { if (ent.CurrentPlayerOwner == s_players[i].Id) { aux.Add(ent); } } s_players[i].SetPlanets(aux); } List <TAttackInfo> s_attacks = new List <TAttackInfo>(); foreach (Transform att in attackPool.transform) { if (att.gameObject.activeSelf) { s_attacks.Add(att.GetComponent <Attack>().GetTrainingSnapshot()); } } game.Initialize(s_players, s_planets, s_attacks); return(game); }
public async Task <GenericResponse <BooleanResponse> > AddUserGame(string shortName, string gameUsername, string gameApiKey, long userId) { // First check if the game exist TGame dbGame = await _gameDataAccess.GetDbGame(shortName); if (dbGame == null) { return(new GenericResponse <BooleanResponse>($"Game {shortName} is not valid", null)); } // Check if the game requires a per-user API key or not if (dbGame.ApiKeyRequired && string.IsNullOrWhiteSpace(gameApiKey)) { return(new GenericResponse <BooleanResponse>($"Game {shortName} requires an API key", null)); } // If it's the case then add GenericResponse <BooleanResponse> res = await _userGameDataAccess.AddUserGame(dbGame, gameUsername, gameApiKey, userId); return(res); }
public async Task <GenericResponse <BooleanResponse> > AddUserGame(TGame dbGame, string username, string apiKey, long userId) { try { await _databaseContext.TUserGame.AddAsync(new TUserGame { GameId = dbGame.Id, Username = username, UserId = userId, ApiKey = apiKey }); await _databaseContext.SaveChangesAsync(); return(new GenericResponse <BooleanResponse>(new BooleanResponse { Success = true })); } catch (DbUpdateException e) { return(new GenericResponse <BooleanResponse>("Error while adding game", e)); } }
internal bool SetGame(TGame game) { return(Interlocked.CompareExchange(ref _game, value: game, comparand: null) == null); }
/// <summary> /// Returns a simplified class with the same exact state of this instance /// This state is also saved in the return class as an snapshot /// </summary> /// <returns></returns> public TEventEntity TakeSnapshot(TGame game) { return(new TPlanet(currentUnits, maxHealth, currentHealth, expForNextLevel, currentLevel, transform.position, currentPlayerOwner, maxLevel, currentExp, currentContestantId, id, game)); }
/// <summary> /// Trains without creating a new thread /// </summary> /// <param name="game"></param> public void StartTrainingInThisThread(TGame game) { currentGame = game; Train(); }
/// <summary> /// Extends a node by creating a child fore each available action /// </summary> /// <param name="node">Parent node</param> private void ExtendNode(M_Node node) { Debug.Log("Expandiendo nodos"); treeMutex.WaitOne(); //security check //if the game in the node has already been won we don't extend it if (node.State.SomeoneWon() != GlobalData.NO_PLAYER) { // Debug.Log("El nodo contiene victoria, no se expande"); return; } //if the array already has children if (tree[node.Position * 5 + 1] != null) { // Debug.LogError("EL NODO QUE SE HA EXPANDIDO YA TIENE HIJOS"); } //we get the state in the node TGame initial = node.State; M_Node aux; //for each action we... for (int i = 0; i < GlobalData.NUMBER_OF_ACTIONS; i++) { if (((Actions)i == Actions.AttackNeutral && !node.State.PlayerCanAttackNeutral()) || ((Actions)i == Actions.Heal && !node.State.PlayerCanHeal(id)) || ((Actions)i == Actions.Upgrade && !node.State.PlayerCanLevelUp(id))) { aux = new M_Node(null, ((node.Position * 5) + i + 1)); Debug.Log("El nodo " + aux.Position + " se ha hecho inalcanzable porque representa una accion que no puede ejecutarse"); aux.Available = false; node.freeChildren--; tree[aux.Position] = aux; } ///...execute the action and advance the corresponding turns... flowController.AdvanceTurnAndExecuteActions(id, (Actions)i, initial); ///...create the new node with a copy of this state aux = new M_Node(initial.TakeAndGetSnapshot(), ((node.Position * 5) + i + 1)); ///....restore the parent node state... initial.RestoreSnapshot(); ///...set the new node as a child tree[aux.Position] = aux; //if in this node someone already won... if (aux.State.SomeoneWon() != GlobalData.NO_PLAYER) { //if our player won, reward if (aux.State.SomeoneWon() == id) { aux.Score += GlobalData.MONTECARLO_REWARD; } else { aux.Score += GlobalData.MONTECARLO_PENALIZATION; } //backpropagate score BackpropagateScore(aux); //we mark th node as not available, so it wont be visited again aux.Available = false; node.freeChildren--; Debug.Log("El nodo " + aux.Position + " se ha hecho inalcanzable porque representa VICTORIA"); } Debug.Log("Se ha creado el nodo " + aux.Position); } treeMutex.ReleaseMutex(); }
public static void DebugSnapshot(TGame snapshot) { }
public static void AddActor(TGame game) { Game.Scene.GetComponent <ActorComponentStorage>().AddActor <GameComponent, TGame>(game); }
public void TESTAdvanceTurnAndExecuteActions(int id, Actions act, TGame game) { AdvanceTurnAndExecuteActions(id, act, game); }