public override Task OnDisconnected(bool stopCalled) { MutexGame.WaitOne(); ConnectGamer gamer = Gamer(); if (gamer != null) { Clients.Clients(GamersId(gamer)).DeleteGamer(gamer.GamerId); ConnectGame connectGame = gamer.Game; GameMechanic game = connectGame.Game; if (connectGame.Gamers.Count() == 1) { Keys.Add(game.Id); Games.Remove(connectGame); } else { game.RemoveGamer(gamer.GamerId); connectGame.Gamers.Remove(gamer); } } MutexGame.ReleaseMutex(); return(base.OnDisconnected(stopCalled)); }
private ConnectGamer Connection(long Id, Gamer gamer) { if (gamer == null) { return(null); } MutexGame.WaitOne(); string ConnectionId = Context.ConnectionId; ConnectGamer connectGamer = Gamer(ConnectionId) ?? GamerById(gamer.Id); ConnectGame connectGame = connectGamer?.Game; if (connectGamer == null) { if (Id != 0) { connectGame = Game(Id); if (connectGame == null) { connectGame = new ConnectGame(Id); Games.Add(connectGame); } else if (connectGame.Game.isRun) { Id = 0; } } if (Id == 0) { connectGame = Games.FirstOrDefault(x => x.Gamers.Count < 8 && !x.Game.isRun); if (connectGame == null) { connectGame = new ConnectGame(Key()); Games.Add(connectGame); } } connectGamer = new ConnectGamer() { Id = ConnectionId, Gamer = gamer, Game = connectGame }; connectGame.Game.AddGamer(connectGamer.GamerId, connectGamer.Gamer); connectGame.Gamers.Add(connectGamer); } else { connectGamer.Id = ConnectionId; } MutexGame.ReleaseMutex(); return(connectGamer); }
private async Task ConnectAsync(IUser playerTwo = null) { await Context.Message.DeleteAsync(); if (playerTwo == null) { BotConfig conf = BotConfig.Load(); var gconf = conf.GetConfig(Context.Guild.Id); await MessageUtil.SendErrorAsync((Context.Channel as ITextChannel), "Incorrect Command Usage", $"Correct Usage: `{gconf.Prefix}c4 <@user>`", false); return; } IUser playerOne = Context.User as IUser; if (playerTwo.IsBot || playerOne.Id == playerTwo.Id) { await MessageUtil.SendErrorAsync((Context.Channel as ITextChannel), "Connect4 Error", "You can not play against yourself or a bot.", false); return; } if (GameHandler.DoesGameExist(Context.Guild.Id, GameType.CONNECT)) { await MessageUtil.SendErrorAsync((Context.Channel as ITextChannel), "Connect4 Error", "There is already a game in this guild.", false); return; } ulong[] players = { playerOne.Id, playerTwo.Id }; ConnectGame newGame = new ConnectGame(Context.Guild.Id, players); GameHandler.AddNewGame(newGame); string render = newGame.RenderGame(); IUser currentPlayer = await Context.Guild.GetUserAsync(newGame.Players[newGame.Turn]); var msg = await Context.Channel.SendFileAsync(render, $"**Connect 4**\n" + $"Next Up: {currentPlayer.Mention}\n" + $"`{CommandHandler.GetPrefix(Context.Guild.Id)}c4 <column>` to take your turn\n`{CommandHandler.GetPrefix(Context.Guild.Id)}c4 end` to end the game"); newGame.RenderId = msg.Id; }
protected void TimerStart(object Object) { ConnectGamer gamer = (ConnectGamer)Object; GameMechanic game = gamer.Game.Game; ConnectGame cgame = gamer.Game; int time = 1; do { Clients.Clients(GamersId(gamer)).SetTimer(time--); Thread.Sleep(1000); } while (time > 0 && !game.IsAllAnswer); if (!game.IsAllAnswer) { foreach (Gamer item in game.Gamers.Where(x => !game.Answers.Any(z => z.SenderId == x.Id))) { SetAnswer(item.Id, item.Id); } } }
private async Task ConnectTurnAsunc(int column = -1) { await Context.Message.DeleteAsync(); if (column < 1 || column > 7) { BotConfig conf = BotConfig.Load(); var gconf = conf.GetConfig(Context.Guild.Id); await MessageUtil.SendErrorAsync((Context.Channel as ITextChannel), "Incorrect Command Usage", $"Correct Usage: `{gconf.Prefix}c4 <column>` - Column must be 1 - 7", false); return; } if (!GameHandler.DoesGameExist(Context.Guild.Id, GameType.CONNECT)) { await MessageUtil.SendErrorAsync((Context.Channel as ITextChannel), "Connect4 Error", "There is not a game in this guild.", false); return; } ConnectGame game = (ConnectGame)GameHandler.GetGame(Context.Guild.Id, GameType.CONNECT); if (game == null) { await MessageUtil.SendErrorAsync((Context.Channel as ITextChannel), "Connect4 Error", "The game could not be found.", false); return; } if (game.Players[0] != Context.User.Id && game.Players[1] != Context.User.Id) { await MessageUtil.SendErrorAsync((Context.Channel as ITextChannel), "Connect4 Error", "You are not part of this game..."); return; } if (game.Players[game.Turn] != Context.User.Id) { await MessageUtil.SendErrorAsync((Context.Channel as ITextChannel), "Connect4 Error", "It is not your turn..."); return; } GameHandler.TakeTurn(Context.Guild.Id, GameType.CONNECT, Context.User.Id, column); ulong winner = GameHandler.CheckForWinner(Context.Guild.Id, GameType.CONNECT); bool draw = GameHandler.CheckForDraw(Context.Guild.Id, GameType.CONNECT); IMessage oldMsg = await Context.Channel.GetMessageAsync(game.RenderId); await oldMsg.DeleteAsync(); if (winner == 0L) { if (!draw) { var nextUp = await Context.Guild.GetUserAsync(game.Players[game.Turn]); string render = game.RenderGame(); var msg = await Context.Channel.SendFileAsync(render, $"**Connect 4**\n" + $"Next Up: {nextUp.Mention}\n" + $"`{CommandHandler.GetPrefix(Context.Guild.Id)}c4 <column>` to take your turn\n`{CommandHandler.GetPrefix(Context.Guild.Id)}c4 end` to end the game"); game.RenderId = msg.Id; } else { string render = game.RenderGame(); await Context.Channel.SendFileAsync(render, $"**Connect 4**\n" + $"DRAW ({(await Context.Guild.GetUserAsync(game.Players[0])).Mention} v {(await Context.Guild.GetUserAsync(game.Players[1])).Mention})"); if (ProfileDatabase.GetUser(game.Players[0]) != null) { ProfileDatabase.AddCurrency(game.Players[0], 100); } if (ProfileDatabase.GetUser(game.Players[1]) != null) { ProfileDatabase.AddCurrency(game.Players[1], 100); } GameHandler.EndGame(game); } } else { string render = game.RenderGame(); await Context.Channel.SendFileAsync(render, $"**Connect 4**\n" + $"Game Won by " + (await Context.Guild.GetUserAsync(winner)).Mention); var winwin = ProfileDatabase.GetUser(winner); if (winwin != null) { ProfileDatabase.AddCurrency(winner, 250); await LeaderboardDatabase.CheckAsync(winner, winwin.Name, "Connect 4"); await LeaderboardDatabase.AddScoreAsync(winner, "Connect 4"); } GameHandler.EndGame(game); } }