private static async Task <(string gid, string pid, int pidx)> JoinNewGame(bool isMatch, string map, string name, int color, string openid) { var player = PlayerHelper.CreatePlayer(name, color); if (player == null) { throw new GameJoinedException(); } var args = new Dictionary <string, object>(); args.Add("Feedback", HelperConfig.Current.WxInterfaceHost + "api/GameHelper/Finish"); var gameid = GameHelper.CreateGame(map, args); GameHelper.JoinGame(gameid, player.Id); var score = new GameScore() { oid = openid, pid = player.Id, gid = gameid, pidx = player.Index, ismatch = isMatch, score = 0 }; await DbEntityManager.Insert(score); return(gid : gameid, pid : player.Id, pidx : player.Index); }
private Player CreatePlayer() { ScenarioContext.Current.Should().ContainKey("brandId"); var brandId = ScenarioContext.Current.Get <Guid>("brandId"); return(PlayerHelper.CreatePlayer(true, brandId)); }
static void Main(string[] args) { string input = string.Empty; //ServerHelper.Initialize("http://106.75.33.221:6000/"); ServerHelper.Initialize("http://localhost:6000/"); map = MapHelper.GetMap("RectSmall"); game = new Game(map.Rows.Count, map.Rows[0].Count); Player: Console.WriteLine("Enter nickname:"); input = Console.ReadLine(); string name = input.Trim(); Console.WriteLine("Enter color(0~9):"); input = Console.ReadLine(); int color = Int32.Parse(input); self = PlayerHelper.CreatePlayer(name, color); if (self == null) { Console.WriteLine("Player has already existed with same name. Try to get a new name."); goto Player; } string gameId = string.Empty; Console.WriteLine("1: Create a new game"); Console.WriteLine("2: Join a game"); input = Console.ReadLine(); if (input == "1") { gameId = GameHelper.CreateGame("RectSmall"); } else { Console.WriteLine("Game list:"); List: var list = GameHelper.GameList(); if (list == null || list.Length == 0) { Thread.Sleep(1000); goto List; } else { for (int i = 0; i < list.Length; i++) { Console.WriteLine(i.ToString() + " : " + list[i]); } } Console.WriteLine("Select a game to join:"); input = Console.ReadLine(); if (Int32.TryParse(input.Trim(), out int sel) && sel >= 0 && sel < list.Length) { gameId = list[sel]; } else { Console.WriteLine("Select error."); goto List; } } game.Id = gameId; if (!GameHelper.JoinGame(gameId, self.Id)) { Console.WriteLine("Join game fail."); } else { Console.WriteLine("Join game Ok."); } PlayerHelper.GetPlayer(ref self); Console.WriteLine("Self info updated."); GameHelper.GetGame(gameId, ref game); RushAttack(); }
static void Main(string[] args) { string input = string.Empty; //ServerHelper.Initialize("http://106.75.33.221:7000/"); ServerHelper.Initialize("http://dev.magcore.clawit.com/"); Player: Console.WriteLine("Enter nickname:"); input = Console.ReadLine(); string name = input.Trim(); Console.WriteLine("Enter color(0~9):"); input = Console.ReadLine(); int color = Int32.Parse(input); self = PlayerHelper.CreatePlayer(name, color); if (self == null) { Console.WriteLine("Player has already existed with same name. Try to get a new name."); goto Player; } string gameId = string.Empty; string mapName = string.Empty; Console.WriteLine("1: Create a new game"); Console.WriteLine("2: Join a game"); input = Console.ReadLine(); if (input == "1") { map = MapHelper.GetMap("RectSmall"); game = new Game(map.Rows.Count, map.Rows[0].Count); gameId = GameHelper.CreateGame("RectSmall"); } else { Console.WriteLine("Game list:"); List: var list = GameHelper.GameList(); if (list == null || list.Length == 0) { Thread.Sleep(1000); goto List; } else { for (int i = 0; i < list.Length; i++) { if (list[i].state == 0) { Console.WriteLine("{0} : {1} 地图:{2}", i, list[i].id.ToString(), list[i].map.ToString()); } } } Console.WriteLine("Select a game to join:"); input = Console.ReadLine(); if (Int32.TryParse(input.Trim(), out int sel) && sel >= 0 && sel < list.Length) { gameId = list[sel].id.ToString(); mapName = list[sel].map.ToString(); map = MapHelper.GetMap(mapName); game = new Game(map.Rows.Count, map.Rows[0].Count); } else { Console.WriteLine("Select error."); goto List; } } game.Id = gameId; if (!GameHelper.JoinGame(gameId, self.Id)) { Console.WriteLine("Join game fail."); } else { Console.WriteLine("Join game Ok."); } PlayerHelper.GetPlayer(ref self); Console.WriteLine("Self info updated."); //找到地图上所有的基地, 为杀手准备 GameHelper.GetGame(game.Id, ref game); _enemies.Clear(); foreach (var row in game.Rows) { foreach (var cell in row.Cells) { if (cell.Type == 2 && cell.OwnerIndex != self.Index) { _enemies.Add(cell.Position.ToString(), cell.Position); } } } //新开一个线程用于更新最新战斗数据 Task.Factory.StartNew(() => { Thread.CurrentThread.IsBackground = true; while (true) { GameHelper.GetGame(game.Id, ref game); if (game.State > 1) { Console.WriteLine("Game over"); break; } Thread.Sleep(500); } }); Attack(); }
public void TestInitialize() { _board = BoardHelper.GetBoard(); _player = PlayerHelper.CreatePlayer("Car", _board); }
public static async Task <(string gid, string pid, int pidx)> JoinOneGame(bool isMatch, string map, string name, int color, string openid) { var host = string.Empty; if (isMatch) { host = HelperConfig.Current.MagcoreApiServer; } else { host = HelperConfig.Current.MagcoreApiTrain; } ServerHelper.Initialize(host); var gameList = GameHelper.GameList(); if (gameList == null || gameList.Length == 0) { //no games exist return(await JoinNewGame(isMatch, map, name, color, openid)); } else { var gameMap = MapHelper.GetMap(map); for (int i = 0; i < gameList.Length; i++) { if (gameList[i].state == 0 && gameList[i].map == map) { Game game = new Game(gameMap.Rows.Count, gameMap.Rows[0].Count); if (GameHelper.GetGame(gameList[i].id.ToString(), ref game) && game != null) { if (!game.Players.Exists(p => p.Color == color)) { //found a game match var player = PlayerHelper.CreatePlayer(name, color); if (player == null) { throw new GameJoinedException(); } GameHelper.JoinGame(game.Id, player.Id); var score = new GameScore() { oid = openid, pid = player.Id, gid = game.Id, pidx = player.Index, ismatch = isMatch, score = 0 }; await DbEntityManager.Insert(score); return(gid : game.Id, pid : player.Id, pidx : player.Index); } } } } //found no game match return(await JoinNewGame(isMatch, map, name, color, openid)); } }