Exemple #1
0
 protected override void OnMessage(MessageEventArgs e)
 {
     Program.LogMsg($"Getting Lock..", Discord.LogSeverity.Critical, $"Con::{REF}");
     if (!OnlineLock.WaitOne(6 * 1000))
     {
         Program.LogMsg($"Failed Lock..", Discord.LogSeverity.Critical, $"Con::{REF}");
         Context.WebSocket.Close(CloseStatusCode.ServerError, "Unable to get lock");
         return;
     }
     Program.LogMsg($"Got Lock..", Discord.LogSeverity.Critical, $"Con::{REF}");
     try
     {
         log($"{REF} >>> {e.Data}");
         Program.LogMsg($"{e.Data}", Discord.LogSeverity.Critical, REF);
         var jobj   = JObject.Parse(e.Data);
         var packet = new ChessPacket(jobj);
         handleMessage(packet);
     } catch (Exception ex)
     {
         Program.LogMsg($"ChessCon:{Player?.Name ?? "na"}", ex);
     } finally
     {
         OnlineLock.Release();
         Program.LogMsg($"Released Lock..", Discord.LogSeverity.Critical, $"Con::{REF}");
     }
 }
Exemple #2
0
 public void CreateNewGame()
 {
     Program.LogMsg($"Getting Lock..", Discord.LogSeverity.Critical, "CreateNewGame");
     if (!OnlineLock.WaitOne(60 * 1000))
     {
         Program.LogMsg($"Failed Lock..", Discord.LogSeverity.Critical, "CreateNewGame");
         RespondRaw("Unable to get lock", 500);
         return;
     }
     try
     {
         Program.LogMsg($"Got Lock..", Discord.LogSeverity.Critical, "CreateNewGame");
         if (CurrentGame != null)
         {
             RespondRaw("Game already in progress", 400);
             return;
         }
         CurrentGame = new OnlineGame();
         Program.LogMsg($"Released Lock..", Discord.LogSeverity.Critical, "CreateNewGame");
         CurrentGame.SendLogStart(SelfPlayer.Name);
         WebSockets.ChessConnection.log($"Game created and thus White is: {SelfPlayer.Name}");
         RespondRaw("", 201);
     }
     catch
     {
         throw;
     }
     finally
     {
         OnlineLock.Release();
     }
 }
Exemple #3
0
        public void OnlineBase()
        {
            if (SelfPlayer == null)
            {
                HTTPError(System.Net.HttpStatusCode.BadRequest, "No Chess", "No linked chess account");
                return;
            }
            if (SelfPlayer.IsBuiltInAccount)
            {
                HTTPError(System.Net.HttpStatusCode.Forbidden, "Bad Account", "Account not able to do that");
                return;
            }
            if (SelfPlayer.IsBanned)
            {
                HTTPError(System.Net.HttpStatusCode.UnavailableForLegalReasons, "Banned", "You are banned and may not play games.");
            }
            Program.LogMsg($"Getting Lock..", Discord.LogSeverity.Critical, "OnlineBase");
            if (!OnlineLock.WaitOne(5 * 1000))
            {
                Program.LogMsg($"Failed..", Discord.LogSeverity.Critical, "OnlineBase");
                HTTPError(System.Net.HttpStatusCode.InternalServerError, "Thread-Safe Halt", "Unable to get lock");
                return;
            }
            Program.LogMsg($"Got Lock", Discord.LogSeverity.Critical, "OnlineBase");
            string token = getToken();
            string TABLE = "";
            string ROW   = "";

            if (CurrentGame == null)
            {
                ROW = "<tr><td colspan='4'>" + aLink($"chess://create/{token}", "Create New Game") + "</td></tr>";
            }
            else
            {
                ROW += $"<td>{(CurrentGame.White?.Player?.Name ?? "No white")}</td>";
                ROW += $"<td>{(CurrentGame.Black?.Player?.Name ?? "No black")}</td>";
                ROW += $"<td>{ aLink($"chess://join/{token}", "Join Game")}</td>";
                ROW += $"<td>{aLink($"chess://spectate/{token}", "Spectate Game")}</td></tr>";
            }
            TABLE += ROW;
            OnlineLock.Release();
            Program.LogMsg($"Relased Lock", Discord.LogSeverity.Critical, "OnlineBase");
            ReplyFile("online.html", 200, new Replacements().Add("table", TABLE));
        }
Exemple #4
0
 public void OnlineGame()
 {
     if (!OnlineLock.WaitOne())
     {
         RespondRaw("Failed thread safe lock", System.Net.HttpStatusCode.InternalServerError);
         return;
     }
     try
     {
         if (ChessService.CurrentGame == null)
         {
             ChessService.CurrentGame = new OnlineGame();
             CurrentGame.SendLogStart(SelfPlayer.Name);
             WebSockets.ChessConnection.log($"Game created OG and thus White is: {SelfPlayer.Name}");
         }
         var g     = ChessService.CurrentGame;
         var side  = g?.GetPlayer(SelfPlayer.Id)?.Side.ToString().ToLower()[0].ToString() ?? "";
         var white = g?.White?.Player?.Name ?? "";
         var black = g?.Black?.Player?.Name ?? "";
         var token = getToken();
         var fen   = g?.InnerGame?.generate_fen() ?? "";
         ReplyFile("game.html", 200, new Replacements()
                   .Add("side", side)
                   .Add("white", white)
                   .Add("black", black)
                   .Add("token", token)
                   .Add("game_fen", fen));
     }
     catch (Exception ex)
     {
         Program.LogMsg("Online_Game", ex);
     }
     finally
     {
         OnlineLock.Release();
     }
 }