Ejemplo n.º 1
0
        public async Task MatchesID(string url, string token, int id)
        {
            Console.WriteLine("Lay thong tin tran dau: " + id);
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(url);
                client.DefaultRequestHeaders.Add("Authorization", this.token);
                //Console.WriteLine("---- Matches -----");
                //Console.WriteLine("toekn: " + token);
                var result = await client.GetAsync("matches/" + id);

                //Console.WriteLine("ket qua server tra ve: " + result);
                if (result.IsSuccessStatusCode)
                {
                    string body = await result.Content.ReadAsStringAsync();

                    //if (body == prevMatchesJson)
                    //{
                    //    return;
                    //} else
                    //{
                    //    prevMatchesJson = body;
                    //    lblCountDown.Text = "" + inf.turnMillis;
                    //}
                    //Console.WriteLine("body: " + body);

                    JObject job = JObject.Parse(body);
                    data = job.ToObject <MatchIDData>();
                    if (data.startedAtUnixTime == prevMatchesJson)
                    {
                        return;
                    }
                    else
                    {
                        prevMatchesJson   = data.startedAtUnixTime;
                        lblCountDown.Text = "" + inf.turnMillis;
                    }


                    int myTeamId = int.Parse(lb_idTeam.Text);
                    lb_currentTurn.Text = data.turn + "";
                    Console.WriteLine("Time server: " + data.startedAtUnixTime);


                    //ket qua moi
                    res = new ActionSubmit();

                    foreach (var item in data.teams)
                    {
                        //Console.WriteLine("team id: " + item.teamID);
                        if (item.teamID == myTeamId)
                        {
                            lb_teamIDTa.Text    = item.teamID.ToString();
                            lb_areaPointTa.Text = item.areaPoint.ToString();
                            lb_tilePointTa.Text = item.tilePoint.ToString();
                        }
                        else
                        {
                            lb_teamIDDich.Text    = item.teamID.ToString();
                            lb_areaPointDich.Text = item.areaPoint.ToString();
                            lb_tilePointDich.Text = item.tilePoint.ToString();
                        }
                    }
                    this.panel_map.Enabled = false;

                    Console.WriteLine("Ve Map");
                    DrawMap(data.height, data.width, data.tiled, data.points, myTeamId, data.obstacles, data.treasure);
                    //ve agent hien tai
                    foreach (var item in data.teams)
                    {
                        //if (item.teamID == myTeamId)
                        //{
                        foreach (var agent in item.agents)
                        {
                            int x = agent.x - 1;
                            int y = agent.y - 1;
                            //Console.WriteLine("x: " + x + " y: " + y);
                            Button btn = this.arrBtn[y][x];
                            btn.BackColor = (item.teamID == myTeamId) ? Color.Blue : Color.Red;

                            // Console.WriteLine("button: " + btn.GetHashCode
                            if (dictBtn.ContainsKey(btn))
                            {
                                dictBtn[btn].agentID = agent.agentID;
                            }

                            //neu la quan minh thi them vao list ket qua
                            // lay ket qua tu dong
                            if (item.teamID == myTeamId)
                            {
                                //this.res.actions.Add(new ActionSubmitFormat
                                //{
                                //    agentID = agent.agentID,
                                //    dx = 0,
                                //    dy = 0,
                                //    turn = data.turn,
                                //    type = "stay"
                                //});
                                this.res.actions.Add(GetMax8(data.width, data.height, data.tiled, data.points, x, y, agent.agentID, btn, item.teamID));
                            }
                        }

                        // }
                    }
                    //hien thi ket qua json
                    var json = JsonConvert.SerializeObject(this.res);
                    txt_jsonSubmit.Text    = json;
                    this.panel_map.Enabled = true;
                    //auto refresh
                }
            }
        }
Ejemplo n.º 2
0
        public async Task MatchesID(string url, string token, int id)
        {
            Console.WriteLine("Lay thong tin tran dau: " + id);
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(url);
                client.DefaultRequestHeaders.Add("Authorization", this.token);
                //Console.WriteLine("---- Matches -----");
                //Console.WriteLine("toekn: " + token);
                var result = await client.GetAsync("matches/" + id);

                //Console.WriteLine("ket qua server tra ve: " + result);
                if (result.IsSuccessStatusCode)
                {
                    string body = await result.Content.ReadAsStringAsync();

                    //Console.WriteLine("body: " + body);

                    JObject job = JObject.Parse(body);
                    data = job.ToObject <MatchIDData>();
                    //Console.WriteLine("heigth: " + data.height);
                    //Console.WriteLine("width: " + data.width;
                    int myTeamId = int.Parse(lb_idTeam.Text);
                    lb_currentTurn.Text = data.turn + "";
                    Console.WriteLine("Time server: " + data.startedAtUnixTime);

                    //bat dau dem nguoc de gui request
                    if (data.startedAtUnixTime != oldTime)
                    {
                        oldTime          = data.startedAtUnixTime;
                        this.currentTime = (inf.turnMillis) / 1000;
                        //timerRequest.Start();
                    }
                    //isSendRequset = false;
                    //timeServer = UnixTimeToDateTime(data.startedAtUnixTime);
                    //Console.WriteLine("gio server truoc khi cong: "+timeServer);
                    //timeServer = timeServer.AddMilliseconds(inf.turnMillis + inf.intervalMillis);
                    //ket qua moi
                    res = new ActionSubmit();

                    foreach (var item in data.teams)
                    {
                        Console.WriteLine("team id: " + item.teamID);
                        if (item.teamID == myTeamId)
                        {
                            lb_teamIDTa.Text    = item.teamID.ToString();
                            lb_areaPointTa.Text = item.areaPoint.ToString();
                            lb_tilePointTa.Text = item.tilePoint.ToString();
                        }
                        else
                        {
                            lb_teamIDDich.Text    = item.teamID.ToString();
                            lb_areaPointDich.Text = item.areaPoint.ToString();
                            lb_tilePointDich.Text = item.tilePoint.ToString();
                        }
                    }
                    this.panel_map.Enabled = false;

                    //ve map 1 lan, doi request
                    if (!startGame)
                    {
                        return;
                    }
                    Console.WriteLine("Ve Map");
                    DrawMap(data.height, data.width, data.tiled, data.points, myTeamId);
                    //ve agent hien tai
                    foreach (var item in data.teams)
                    {
                        //if (item.teamID == myTeamId)
                        //{
                        foreach (var agent in item.agents)
                        {
                            int x = agent.x - 1;
                            int y = agent.y - 1;
                            //Console.WriteLine("x: " + x + " y: " + y);
                            Button btn = this.arrBtn[y][x];
                            btn.BackColor = (item.teamID == myTeamId) ? Color.Blue : Color.Red;

                            // Console.WriteLine("button: " + btn.GetHashCode
                            if (dictBtn.ContainsKey(btn))
                            {
                                dictBtn[btn].agentID = agent.agentID;
                            }

                            //neu la quan minh thi them vao list ket qua
                            if (item.teamID == myTeamId)
                            {
                                this.res.actions.Add(new ActionSubmitFormat
                                {
                                    agentID = agent.agentID,
                                    dx      = 0,
                                    dy      = 0,
                                    turn    = data.turn,
                                    type    = "stay"
                                });
                            }
                        }

                        // }
                    }
                    //hien thi ket qua json
                    var json = JsonConvert.SerializeObject(this.res);
                    txt_jsonSubmit.Text    = json;
                    this.panel_map.Enabled = true;
                    //auto refresh
                }
            }
        }