示例#1
0
        //listen and enque commands
        public void listenthread(object obj)
        {
            mutTable.WaitOne();
            TcpClient client = _client_table[obj] as TcpClient;
            mutTable.ReleaseMutex();
            while (true)
            {
                try
                {
                    NetworkStream nets = client.GetStream();
                    IFormatter formatter = new BinaryFormatter();
                    packet receiveobj = (packet)formatter.Deserialize(nets);

                    mutpac.WaitOne();
                    packetQ.Enqueue(receiveobj);
                    mutpac.ReleaseMutex();
                }
                catch
                {
                    packet exit = new packet("q!", obj as string, null, -1, 0, 0, false);
                    mutpac.WaitOne();
                    packetQ.Enqueue(exit);
                    mutpac.ReleaseMutex();
                    Thread.CurrentThread.Abort();
                }
            }
        }
示例#2
0
        /*private void game(packet data)
        {
            mutTable.WaitOne();
            try
            {
                Socket player = _client_table[cmd[1]] as Socket;
                string msg = cmd[0];
                for (int i = 2; i < cmd.Length; i++)
                {
                    msg += " " + cmd[i];
                }
                player.Send(Encoding.Unicode.GetBytes(msg));
            }
            catch { }
            mutTable.ReleaseMutex();
        }*/
        private void userlogin(packet data)
        {
            //string msg = string.Format("u+ {0},", cmd[1]);                //new comer's login msg

            string userpass = _client_password[data.Name] as string;
            if (_client_password.ContainsKey(data.Name) == false)
            {
                _client_password.Add(data.Name, data.Msg);
                using (StreamWriter sw = File.CreateText(path))
                {
                    foreach (DictionaryEntry de in _client_password)
                    {
                        string s = string.Format(de.Key + " " + de.Value);
                        sw.WriteLine(s);
                    }
                }

            }

            mutTable.WaitOne();
            Console.WriteLine("{0} 已登入,線上人數:{1}\n", data.Name, _client_table.Count);

            mutTable.ReleaseMutex();
        }
示例#3
0
        private void userlogout(packet data, bool expected)
        {
            mutTable.WaitOne();
            if (_client_table.ContainsKey(data.Name))
            {
                try
                {
                    availableusers.Remove(data.Name);
                }
                catch { }

                string[] temp=null;
                foreach (string[] ss in match)
                {
                    if (ss.Contains(data.Name))
                    {
                        string oppo = (ss[0] == data.Name) ? ss[1] : ss[0];
                        packet p = new packet("q", null, null, 0, 0, 0, false);
                        sendPacket(oppo, p);
                        temp = ss;
                    }
                }
                try
                {
                    match.Remove(temp);
                }
                catch { }

                TcpClient client = _client_table[data.Name] as TcpClient;

                _client_table.Remove(data.Name);
                string msg = string.Format("{0} {1},現在線上人數: {2} \n", data.Name, (expected ? "已離線" : "意外中止"), _client_table.Count);
                Console.WriteLine(msg);
                client.Close();
            }
            mutTable.ReleaseMutex();
        }
示例#4
0
        private void ping(object source, ElapsedEventArgs e)
        {
            mutTable.WaitOne();
            while ((availableusers.Count / 2) != 0)
            {
                try
                {
                    string player1 = availableusers[0] as string;
                    string player2 = availableusers[1] as string;
                    availableusers.Remove(player1);
                    availableusers.Remove(player2);

                    match.Add(new string[2] { player1, player2 });

                    Console.WriteLine("match");
                    int p1type=myrand.Next(0, 100) % 4;
                    int p2type=myrand.Next(0, 100) % 4;
                    packet p1 = new packet("match", player2, "", p1type, p2type, 0, false);
                    packet p2 = new packet("match", player1, "", p2type, p1type, 0, false);
                    sendPacket(player1, p1);
                    sendPacket(player2, p2);
                }
                catch { }
            }
            mutTable.ReleaseMutex();
        }
示例#5
0
 private void sendPacket(string user,packet senddata)
 {
     try
     {
         TcpClient client = _client_table[user] as TcpClient;
         NetworkStream nets = client.GetStream();
         IFormatter formatter = new BinaryFormatter();
         formatter.Serialize(nets, senddata);
     }
     catch { }
 }
示例#6
0
文件: Form1.cs 项目: tuxoko/camfight
        public void mymove(packet receiveobj)
        {
            int sector=14-((receiveobj.Sector & 0xF));
            if (sector % 3 == 0) sector += 2;
            else if (sector % 3 == 2) sector -= 2;

            myplayer.update(sector);

            int i = 0;

            if (receiveobj.Big && (myplayer.Big_used==false) && myplayer.Life<big_threshold)
            {
                big_flash = 40;
                enemy.getHurt(big_damage);
                soundPunch.Play();

                if (gamestate == GameState.SINGLE && stage == 3)
                {
                }
                else
                {
                    myplayer.Big_used = true;
                }
            }
            else
            {
                //right
                if ((receiveobj.Sector & 0xF0) >> 4 == 0xF)
                {
                    int sector1 = 14 - ((receiveobj.Sector & 0xF00) >> 8);
                    if (sector1 % 3 == 0) sector1 += 2;
                    else if (sector1 % 3 == 2) sector1 -= 2;
                    enemy.isHit(sector1);
                    i = 2;
                    soundPunch.Play();
                }
                //left
                if ((receiveobj.Sector & 0xF00) >> 8 == 0xF)
                {
                    int sector1 = 14 - ((receiveobj.Sector & 0xF0) >> 4);
                    if (sector1 % 3 == 0) sector1 += 2;
                    else if (sector1 % 3 == 2) sector1 -= 2;
                    enemy.isHit(sector1);
                    i = 1;
                    soundPunch.Play();
                }
            }

            if (enemy.IsAlive == false)//win this game
            {
            //    this.Invoke(new InvokeFunction(this.quit), new object[] { });
                this.Invoke(new InvokeFunction3(this.GameOver), new object[] { "w" });
                soundDie.Play();
            }
        }
示例#7
0
 private void defaultcmd(packet data)
 {
     mutTable.WaitOne();
     try
     {
         sendPacket(data.Name,data);
         foreach (string[] ss in match)
         {
             if (ss.Contains(data.Name))
             {
                 string oppo = (ss[0] == data.Name) ? ss[1] : ss[0];
                 sendPacket(oppo, data);
             }
         }
     }
     catch { }
     mutTable.ReleaseMutex();
 }
示例#8
0
文件: Form1.cs 项目: tuxoko/camfight
 private void LoadingContent(packet pac)
 {
     switch (pac.Move)
     {
         case 0:
             myplayer = new Player("player2", Resources.player1, Resources.player1_lh, Resources.player1_rh, Resources.player1_left, Resources.player1_left_lh, Resources.player1_left_rh, Resources.player1_right, Resources.player1_right_lh, Resources.player1_right_rh, Resources.H1, Resources.BGL, Resources.BGR);
             break;
         case 1:
             myplayer = new Player("player2", Resources.player2, Resources.player2_lh, Resources.player2_rh, Resources.player2_left, Resources.player2_left_lh, Resources.player2_left_rh, Resources.player2_right, Resources.player2_right_lh, Resources.player2_right_rh, Resources.H2, Resources.BGL, Resources.BGR);
             break;
         case 2:
             myplayer = new Player("player2", Resources.player3, Resources.player3_lh, Resources.player3_rh, Resources.player3_left, Resources.player3_left_lh, Resources.player3_left_rh, Resources.player3_right, Resources.player3_right_lh, Resources.player3_right_rh, Resources.H3, Resources.BGL, Resources.BGR);
             break;
         case 3:
             myplayer = new Player("player2", Resources.player4, Resources.player4_lh, Resources.player4_rh, Resources.player4_left, Resources.player4_left_lh, Resources.player4_left_rh, Resources.player4_right, Resources.player4_right_lh, Resources.player4_right_rh, Resources.H4, Resources.BGL, Resources.BGR);
             break;
         default:
             myplayer = new Player("player2", Resources.player2, Resources.player2_lh, Resources.player2_rh, Resources.player2_left, Resources.player2_left_lh, Resources.player2_left_rh, Resources.player2_right, Resources.player2_right_lh, Resources.player2_right_rh, Resources.H2, Resources.BGL, Resources.BGR);
             break;
     }
 }
示例#9
0
文件: Form1.cs 项目: tuxoko/camfight
 private void LoadingEnemyContent(packet pac)
 {
     switch (pac.Time)
     {
         case 0:
             enemy = new Player("player1", Resources.player1, Resources.player1_lh, Resources.player1_rh, Resources.player1_left, Resources.player1_left_lh, Resources.player1_left_rh, Resources.player1_right, Resources.player1_right_lh, Resources.player1_right_rh, Resources.H1, Resources.BGL, Resources.BGR);
             break;
         case 1:
             enemy = new Player("player1", Resources.player2, Resources.player2_lh, Resources.player2_rh, Resources.player2_left, Resources.player2_left_lh, Resources.player2_left_rh, Resources.player2_right, Resources.player2_right_lh, Resources.player2_right_rh, Resources.H2, Resources.BGL, Resources.BGR);
             break;
         case 2:
             enemy = new Player("player1", Resources.player3, Resources.player3_lh, Resources.player3_rh, Resources.player3_left, Resources.player3_left_lh, Resources.player3_left_rh, Resources.player3_right, Resources.player3_right_lh, Resources.player3_right_rh, Resources.H3, Resources.BGL, Resources.BGR);
             break;
         case 3:
             enemy = new Player("player1", Resources.player4, Resources.player4_lh, Resources.player4_rh, Resources.player4_left, Resources.player4_left_lh, Resources.player4_left_rh, Resources.player4_right, Resources.player4_right_lh, Resources.player4_right_rh, Resources.H4, Resources.BGL, Resources.BGR);
             break;
         default:
             enemy = new Player("player1", Resources.player2, Resources.player2_lh, Resources.player2_rh, Resources.player2_left, Resources.player2_left_lh, Resources.player2_left_rh, Resources.player2_right, Resources.player2_right_lh, Resources.player2_right_rh, Resources.H2, Resources.BGL, Resources.BGR);
             break;
     }
 }
示例#10
0
文件: Form1.cs 项目: tuxoko/camfight
        private void GameOver(string cmd)
        {
            try
            {
                packet mypacket = new packet("q", username, null, -1, 0, 0, false);
                SendPacket(mypacket);
            }
            catch { }
            try
            {
                if (listenthr != null)
                    listenthr.Abort();
                   listenthr = null;
                if (fpu_thr != null)
                    fpu_thr.Abort();
                fpu_thr = null;
            }
            catch { }

            if (cmd == "w")
            {
                gameoverIndex = 0;
            }
            else if(cmd=="l")
            {
                gameoverIndex = 1;
            }
            else
            {
                gameoverIndex = 2 ;
            }
            if (big_flash == 0 && hit_flash == 0)
            {
                if (gamestate == GameState.SINGLE && stage<3)
                {
                    stage++;
                    single_reset();
                }
                else
                {
                    gamestate = GameState.END;
                }
            }
        }
示例#11
0
文件: Form1.cs 项目: tuxoko/camfight
 private void GameStart(packet receiveobj)
 {
     big_move_q = new Queue<int>(30);
     big_flash = 0;
     hit_flash = 0;
     enemyname = receiveobj.Name;
     LoadingEnemyContent(receiveobj);
     LoadingContent(receiveobj);
     gamestate = GameState.GAME;
     //Application.Idle += new EventHandler(ProcessFrame);
     fpu_thr = new Thread(new ThreadStart(ProcessFrame));
     fpu_thr.Start();
     sw.Reset();
     sw.Start();
 }
示例#12
0
文件: Form1.cs 项目: tuxoko/camfight
 private void GameInputControl(KeyEventArgs e)
 {
     int mystate = 0;
     if (gamestate == GameState.GAME)
     {
         if (e.KeyData == Keys.J)
         {
             playerStateMutex.WaitOne();
             mystate = 3;
             myplayer.update(3);
             playerStateMutex.ReleaseMutex();
         }
         else if (e.KeyData == Keys.L)
         {
             playerStateMutex.WaitOne();
             mystate = 6;
             myplayer.update(6);
             playerStateMutex.ReleaseMutex();
         }
         packet play = new packet("play", enemyname, null, mystate,0,0,false);
         SendPacket(play);
         myplayerTimer.Start();
     }
 }
示例#13
0
文件: Form1.cs 项目: tuxoko/camfight
 private void Form1_FormClosed(object sender, FormClosedEventArgs e)
 {
     try
     {
         packet mypacket = new packet("q",username,null,-1,0,0,false);
         SendPacket(mypacket);
         quit();
     }
     catch
     { }
 }
示例#14
0
文件: Form1.cs 项目: tuxoko/camfight
        private void ConnectToServer()
        {
            try
            {
                IPAddress serverip = IPAddress.Parse("140.112.18.203");
                IPEndPoint serverhost = new IPEndPoint(serverip, 800);
                _tcpl = new TcpClient();
                _tcpl.Connect(serverhost);
                nets = _tcpl.GetStream();

                packet mypacket = new packet("l",username,password,0,0,0,false);

                IFormatter formatter = new BinaryFormatter();
                formatter.Serialize(nets,mypacket);

                listenthr = new Thread(new ThreadStart(listenThread));
                listenthr.Start();

            }
            catch
            {
                MessageBox.Show("連線錯誤");
                quit();
            }
        }
示例#15
0
        public void startup()
        {
            _tcpl.Start(100);
            aTimer.Elapsed += new ElapsedEventHandler(ping);
            aTimer.Interval = 5000;
            aTimer.Enabled = true;

            try
            {
                using (StreamReader sr = File.OpenText(path))
                {
                    string s = "";
                    while ((s = sr.ReadLine()) != null)
                    {
                        try
                        {
                            string[] info = s.Split();
                            _client_password.Add(info[0], info[1]);
                        }
                        catch { }
                    }
                }
            }
            catch
            {
                FileStream fs = new FileStream(path, FileMode.Create);
                fs.Close();
            }
            Thread cmdthr = new Thread(new ThreadStart(cmdThread));
            cmdthr.Start();

                while (true)
                {

                    TcpClient clientcp = _tcpl.AcceptTcpClient();
                    NetworkStream nets = clientcp.GetStream();
                    IFormatter formatter = new BinaryFormatter();
                    packet logindata = (packet)formatter.Deserialize(nets);

                    string userpass = _client_password[logindata.Name] as string;

                    mutTable.WaitOne();
                    if (_client_table.ContainsKey(logindata.Name) == true)    // username has already existed
                    {
                        mutTable.ReleaseMutex();

                        packet error = new packet("e", null, null, -1,0,0,false);
                        formatter.Serialize(nets, error);
                        clientcp.Close();
                    }
                    else if (_client_password.ContainsKey(logindata.Name) == true && userpass != logindata.Msg)
                    {
                        packet mistake = new packet("m", null, null, -1,0,0,false);
                        formatter.Serialize(nets, mistake);
                        clientcp.Close();
                    }
                    else
                    {
                        _client_table.Add(logindata.Name, clientcp);
                        availableusers.Add(logindata.Name);
                        mutTable.ReleaseMutex();
                        mutpac.WaitOne();
                        packetQ.Enqueue(logindata);
                        mutpac.ReleaseMutex();

                    }
                }
        }
示例#16
0
文件: Form1.cs 项目: tuxoko/camfight
 private void SendPacket(packet senddata)
 {
     try
     {
         NetworkStream net = _tcpl.GetStream();
         IFormatter formatter = new BinaryFormatter();
         formatter.Serialize(net, senddata);
     }
     catch
     {
     }
 }
示例#17
0
        private void ProcessFrame()
        {
            while (true)
            {
                Image<Bgr, Byte> frame = _capture.QueryFrame();
                FPU.ProcessFrame(frame);

                fpu_mutex.WaitOne();
                fpu_container.SetVar(FPU);
                fpu_mutex.ReleaseMutex();

                int face_sector = GetSector(new Point(FPU.face.X + FPU.face.Width / 2, FPU.face.Y + FPU.face.Height / 2),frame.Size);
                int left_sector = GetSector(FPU.center[1],frame.Size);
                int right_sector = GetSector(FPU.center[0],frame.Size);

                int sector = (right_sector << 8) + (left_sector << 4) + face_sector;

                if (FPU.have_left_punch == true)
                {
                    sector = sector | 0xF00;
                }
                else if (FPU.have_right_punch == true)
                {
                    sector = sector | 0xF0;
                }
                bool big = false;
                if (FPU.have_right)
                {
                    int this_big = sec_to_big(right_sector);
                    if (this_big != last_big_move)
                    {
                        if (big_move_q.Count >= 30)
                        {
                            big_move_q.Dequeue();
                        }
                        big_move_q.Enqueue(this_big);
                        big = check_big_move();
                        last_big_move = this_big;
                    }
                }
                else if (FPU.have_left)
                {
                    int this_big = sec_to_big(left_sector);
                    if (this_big != last_big_move)
                    {
                        if (big_move_q.Count >= 30)
                        {
                            big_move_q.Dequeue();
                        }
                        big_move_q.Enqueue(this_big);
                        big = check_big_move();
                        last_big_move = this_big;
                    }
                }

                frame.Draw(FPU.face, new Bgr(Color.Blue), 2);
                if (FPU.have_left)
                {
                    frame.Draw(FPU.left, new Bgr(Color.Green), 2);
                }
                if (FPU.have_right)
                {
                    frame.Draw(FPU.right, new Bgr(Color.Yellow), 2);
                }
                frame = frame.Flip(FLIP.HORIZONTAL);

                if (show_back)
                {
                    pictureBox1.Image = FPU.backproject.Bitmap;
                }
                else
                {
                    pictureBox1.Image = frame.Bitmap;
                }
                this.Invoke(new InvokeFunction(pictureBox1.Refresh));
                this.Invoke(new InvokeFunction(pictureBox1.Show));
                //pictureBox1.Refresh();
                //pictureBox1.Show();

                packet pac = new packet("play", enemyname, "", 0, 0, sector, big);

                if (gamestate == GameState.GAME)
                {
                    SendPacket(pac);
                }
                else if (gamestate == GameState.SINGLE)
                {
                    this.Invoke(new InvokeFunction2(this.mymove), pac);
                }

            }
        }
示例#18
0
文件: Form1.cs 项目: tuxoko/camfight
        public void EnemyMove(packet receiveobj)
        {
            enemy.update(receiveobj.Sector & 0xF);
            int i = 0;

            if (receiveobj.Big && (enemy.Big_used==false) && enemy.Life<big_threshold)
            {
                big_flash = 40;
                myplayer.getHurt(big_damage);
                enemy.Big_used = true;
                soundPunch.Play();
            }
            else
            {
                //right
                if ((receiveobj.Sector & 0xF0) >> 4 == 0xF)
                {
                    ishit=myplayer.isHit((receiveobj.Sector & 0xF00) >> 8);
                    i = 2;
                    soundPunch.Play();
                }
                //left
                if ((receiveobj.Sector & 0xF00) >> 8 == 0xF)
                {
                    ishit=myplayer.isHit((receiveobj.Sector & 0xF0) >> 4);
                    i = 1;
                    soundPunch.Play();
                }
            }
            int face_sec = receiveobj.Sector & 0xF;

            if (face_sec / 3 < 2)
            {
                i += 6;
            }
            else if (face_sec / 3 > 2)
            {
                i += 3;
            }
             /*
                ArrayList seq = animationMove[i].Clone() as ArrayList;
                aniMutex.WaitOne();
                myAnimation.Enqueue(new Animation("player1", seq));
                aniMutex.ReleaseMutex();
            */

            if (ishit == true)
            {
                hit_flash = 18;
                ishit = false;
            }

            if (myplayer.IsAlive == false)//win this game
            {
               // this.Invoke(new InvokeFunction(this.quit), new object[] { });
                this.Invoke(new InvokeFunction3(this.GameOver), new object[] {"l" });
                soundDie.Play();
            }

            aniMutex.WaitOne();
            playstate = i;
            aniMutex.ReleaseMutex();
        }