示例#1
0
 public WeaponResponseComm(char X, int Y, FireResponse res, FireResult?result) : base("WeaponResponseComms")
 {
     x           = X;
     y           = Y;
     response    = res;
     this.result = result;
 }
示例#2
0
        public void RespondToShot(FireResponse resp, char x, int y, string player, string region, FireResult?result = null)
        {
            WeaponResponseComm c   = new WeaponResponseComm(x, y, resp, result);
            string             msg = JsonConvert.SerializeObject(c);

            client.Publish(gametopic + "/" + region + "/" + player, Encoding.ASCII.GetBytes(msg));
        }
示例#3
0
 public static FireResponseDto ToDto(this FireResponse model) =>
 new FireResponseDto
 {
     EnemyMap = model.EnemyMap.ToMapForEnemyDto()
 };
示例#4
0
    public static void Connect(int lobbyId)
    {
        joinLobby = false;
        ws        = new WebSocket(uri);
        List <string> log = new List <string>();

        ws.OnMessage += (senders, d) =>
        {
            try
            {
                WSResponse res;
                res = JsonConvert.DeserializeObject <WSResponse>(d.Data.ToString());
                //Console.Title = res.identifier;

                if (res.identifier == "order")
                {
                    //Console.Title = "Recieved order";
                    if (res.msg == "reload")
                    {
                        getLobby = true;
                    }
                    if (res.msg == "start")
                    {
                        Terminal.WriteLine("Received start order");
                        start = true;
                        //Console.Title = "Starting game!";
                    }
                }

                if (res.identifier == "setUserId")
                {
                    if (res.type == "txt")
                    {
                        Lobby.user.userId = int.Parse(res.msg);
                    }
                }

                if (res.identifier == "update")
                {
                    try
                    {
                        GameInfo gameInfo = JsonConvert.DeserializeObject <GameInfo>(res.msg);
                        //File.WriteAllLines("res.json", new string[] { JsonConvert.SerializeObject(gameInfo) });

                        players = gameInfo.players;
                        if (playerCount != players.Count)
                        {
                            placePlayers = true;
                        }
                        playerCount = players.Count;

                        map    = gameInfo.mapIndex;
                        update = true;
                    }
                    catch (System.Exception ex)
                    {
                        Terminal.WriteLine(ex.Message);
                    }
                }

                if (res.identifier == "fireResponse")
                {
                    FireResponse fireResponse = JsonConvert.DeserializeObject <FireResponse>(res.msg);
                    Terminal.WriteLine("Fireresponse: Status: " + fireResponse.status + ", msg: " + fireResponse.msg);
                }

                if (res.identifier == "fireInfo")
                {
                    Terminal.WriteLine("Received bullet info - " + res.msg);
                    try
                    {
                        FireInfo _fireInfo = JsonConvert.DeserializeObject <FireInfo>(res.msg);
                        fireInfo = _fireInfo;
                        fired    = true;
                    }
                    catch (System.Exception ex)
                    {
                        Terminal.WriteLine(ex.Message);
                    }
                }

                if (res.identifier == "joinResponse")
                {
                    JoinRequestResponse joinRequestResponse = JsonConvert.DeserializeObject <JoinRequestResponse>(res.msg);

                    if (joinRequestResponse.status == "success")
                    {
                        joinLobby = true;
                        joinId    = lobbyId;
                    }
                    else
                    {
                        display        = true;
                        displayMessage = joinRequestResponse.msg;
                        displayTitle   = joinRequestResponse.status;
                    }
                }

                if (res.identifier == "promoteResponse")
                {
                    PromoteRequestResponse promoteRequestResponse = JsonConvert.DeserializeObject <PromoteRequestResponse>(res.msg);

                    //Console.Title = (kickRequestResponse.msg);
                    if (promoteRequestResponse.status == "success")
                    {
                    }
                    else
                    {
                        display        = true;
                        displayMessage = promoteRequestResponse.msg;
                        displayTitle   = promoteRequestResponse.status;
                    }
                }
                if (res.identifier == "swapTeamResponse")
                {
                    SwapTeamRequestResponse swapTeamRequestResponse = JsonConvert.DeserializeObject <SwapTeamRequestResponse>(res.msg);

                    //Console.Title = (kickRequestResponse.msg);
                    if (swapTeamRequestResponse.status == "success")
                    {
                    }
                    else
                    {
                        display        = true;
                        displayMessage = swapTeamRequestResponse.msg;
                        displayTitle   = swapTeamRequestResponse.status;
                    }
                }
                if (res.identifier == "kickResponse")
                {
                    KickRequestResponse kickRequestResponse = JsonConvert.DeserializeObject <KickRequestResponse>(res.msg);

                    //Console.Title = (kickRequestResponse.msg);

                    if (kickRequestResponse.status == "success")
                    {
                        if (int.Parse(kickRequestResponse.msg) == Lobby.user.userId)
                        {
                            Disconnect();
                            kicked         = true;
                            display        = true;
                            displayTitle   = "Oops";
                            displayMessage = "You were kicked";
                        }
                    }
                }
                if (res.identifier == "startResponse")
                {
                    StartRequestResponse startRequestResponse = JsonConvert.DeserializeObject <StartRequestResponse>(res.msg);

                    //Console.Title = (kickRequestResponse.msg);

                    if (startRequestResponse.status == "success")
                    {
                    }
                    else
                    {
                        display        = true;
                        displayMessage = startRequestResponse.msg;
                        displayTitle   = startRequestResponse.status;
                    }
                }

                if (res.identifier == "displayUpdateResponse")
                {
                    UpdataDisplayInfoResponse updataDisplayInfoResponse = JsonConvert.DeserializeObject <UpdataDisplayInfoResponse>(res.msg);

                    if (updataDisplayInfoResponse.status == "success")
                    {
                        //Console.Title = "I666 - Updated display info";
                    }
                }

                if (res.identifier == "positionUpdateResponse")
                {
                    PositionUpdateResponse updataDisplayInfoResponse = JsonConvert.DeserializeObject <PositionUpdateResponse>(res.msg);

                    //Console.Title = (kickRequestResponse.msg);
                    if (updataDisplayInfoResponse.status == "success")
                    {
                    }
                    else
                    {
                        Console.Title = updataDisplayInfoResponse.msg;
                    }
                }

                if (res.identifier == "pingResponse")
                {
                    Lobby.user.ping = (int)stopWatch.ElapsedMilliseconds;
                    Lobby.DisplayUpdate();
                    stopWatch.Stop();
                    stopWatch.Reset();
                    getLobby = true;
                    //Console.Title = "I666 : " + Lobby.user.ping;
                }

                log.Add("Received message -  " + d.Data.ToString() + " " + senders.ToString());
                //File.WriteAllLines("res.json", log.ToArray());
            }
            catch (System.Exception ex)
            {
                Terminal.WriteLine(ex.Message);
                //File.WriteAllLines("res.json", new string[] { "Something went wrong - " + ex.Message + ", " + d.Data.ToString() });
            }
        };

        ws.OnClose += (senders, d) =>
        {
            Terminal.WriteLine("Closed connection");
            log.Add("Connection closed - " + d.ToString() + " " + senders.ToString());
            //File.WriteAllLines("res.json", log.ToArray());

            return;
        };

        ws.Connect();
        Lobby.Join(lobbyId);
    }