messagedistribute.svc.cs

来自「经典游戏程序设计:visual c++ 上的杀人游戏源代码」· CS 代码 · 共 447 行 · 第 1/2 页

CS
447
字号
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using Utility;
using KillingManager;
using DAL;
using System.Threading;
using KillingClientUI;

namespace ServerLib
{
    // NOTE: If you change the class name "MessageDistribute" here, you must also update the reference to "MessageDistribute" in Web.config.
    [ServiceBehavior]
    public partial class MessageDistribute : IMessageDistribute
    {
        //Save current game 
        public static Game CurrentGame = null;//new Game();
        //Save current Activity
        public static Activity CurrentActivity = null;//new Activity();
        //All Player Count
        public static int AllPlayersCount;

        //Player client -> Server host
        [OperationBehavior]
        public bool DistribChatMsg(ChatFormat chat)
        {

            //If the game has not been started.
            if (CurrentGame == null)
            {
                IList<User> listPlayerOnline = Business.GetUser(IsOnlineFlag.Online);
                foreach (User player in listPlayerOnline)
                {
                    string strMsg = chat.Sender+ " says: " + chat.MsgBody;
                    SendMessageThread(player.IP, strMsg);
                }

                return true;
            }
            //TODO:
            //Day Time

            //Player player = GetPlayerInfo(chat.Sender);
            Player pig = Business.GetPlayer(chat.Sender, CurrentGame);
            IList<PlayerEntity> listPlayers = null;


            if (GetCurrentTimeFlag() == TimeFlag.DayTime)
            {
                listPlayers = Business.GetPlayerEntities(RoleFlag.Civilian | RoleFlag.Killer | RoleFlag.Police, IsActiveFlag.Active, CurrentGame);
            }
            else
            {
                if (pig.RoleID == (int)RoleFlag.Killer)
                    listPlayers = Business.GetPlayerEntities(RoleFlag.Killer, IsActiveFlag.Active, CurrentGame);
            }

            if (listPlayers == null)
                return true;

            foreach (PlayerEntity play in listPlayers)
            {
                string ip = play.IP;
                string sender = chat.Sender;
                string mesgbody = chat.MsgBody;
                Thread workerThread = new Thread(()=>
                {
                    ConnectToServer(ip).ReceiveNewsMsg(sender + " says: " + mesgbody);
                });
                workerThread.Start();
            }

            return true;
        }

        private void SendMessageThread(string IP, string strMsg)
        {
            string ip = IP;
            string strMessage = strMsg;
            MessageDistribution proxyClientHost = ConnectToServer(ip);
            Thread workerThread = new Thread(()=>
            {
                proxyClientHost.ReceiveNewsMsg(strMessage);
            });
            workerThread.Start();
            return;
        }

        public TimeFlag GetCurrentTimeFlag()
        {
            if (CurrentActivity.ActivityTypeID == (int)ActivityTypeFlag.Vote)
                return TimeFlag.DayTime;
            else if ((CurrentActivity.ActivityTypeID == (int)ActivityTypeFlag.Kill) ||
                (CurrentActivity.ActivityTypeID == (int)ActivityTypeFlag.Investigate))
                return TimeFlag.Night;
            else return TimeFlag.DayTime;
        }

        public TimeFlag GetNextActivityTimeFlag()
        {
            if (CurrentActivity.ActivityTypeID == (int)ActivityTypeFlag.Vote)
                return TimeFlag.Night;
            else if ((CurrentActivity.ActivityTypeID == (int)ActivityTypeFlag.Kill) ||
                (CurrentActivity.ActivityTypeID == (int)ActivityTypeFlag.Investigate))
                return TimeFlag.DayTime;
            else return TimeFlag.DayTime;
        }



        //If vote is finished, return the dead person's name
        //Elese return "";
        public User SaveVote(VoteFormat vote)
        {
            return null;
        }

        //Player client -> Server host
        //The return value is the name who is killed.
        [OperationBehavior]
        public bool DistribVoteMsg(VoteFormat voteFormate)
        {
            MessageDistribution proxyClientHost = null;
            //TODO:
            //Fulfill the vote into database
            //Send message to everyone
            IList<PlayerEntity> listPlayers = null;
            Vote vote = new Vote();
            vote.Voter = voteFormate.Voter;
            vote.Candidate = voteFormate.Condidate;
            vote.ActivityNo = CurrentActivity.ActivityNo;
            vote.RoundNo = CurrentActivity.RoundCount;

            List<PlayerEntity> listCondidates =  Business.Vote(vote, CurrentActivity);


            //Notice to everyone somebody has voted.
            if(GetCurrentTimeFlag() == TimeFlag.DayTime)
                listPlayers = Business.GetPlayerEntities(RoleFlag.Civilian | RoleFlag.Killer | RoleFlag.Police, IsActiveFlag.Active | IsActiveFlag.Dead, CurrentGame);
            else
                listPlayers = Business.GetPlayerEntities(RoleFlag.Killer, IsActiveFlag.Active, CurrentGame);
            foreach (PlayerEntity play in listPlayers)
            {
                SendMessageThread(play.IP, "Judger says: " + voteFormate.Voter+ " has already voted!");
            }

            //if select one to be killed
            #region if count = 1;
            if (listCondidates.Count == 1)
            {
                VoteResult voteResult = new VoteResult();
                voteResult.IsThisVoteValid = true;
                voteResult.TimeFlag = GetNextActivityTimeFlag();

                listPlayers = Business.GetPlayerEntities(RoleFlag.Civilian | RoleFlag.Killer | RoleFlag.Police, IsActiveFlag.Active | IsActiveFlag.Dead, CurrentGame);
                foreach (PlayerEntity play in listPlayers)
                {
                    proxyClientHost = ConnectToServer(play.IP);
                    voteResult.KilledPlayer = listCondidates[0].UserID;
                    voteResult.ActiveTypeFlag = GetNextActivityType(true);
                    voteResult.TimeFlag = GetNextActivityTimeFlag();
                    voteResult.IsThisVoteValid = true;
                    proxyClientHost.ReceiveVoteResult(voteResult);


                    if (play.UserID == listCondidates[0].UserID)
                    {
                        SendMessageThread(play.IP, "Judger says: " + listCondidates[0].UserID + ", You are killed!");
                    }
                    else
                    {
                        SendMessageThread(play.IP, "Judger says: " + listCondidates[0].UserID + " is killed!");
                    }
                }

                //Game over
                //listPlayers.Remove(listPlayers.Select(c => c.PlayerID == listCondidates[0].PlayerID).Single());
                int i=0;
                for(i = 0; i < listPlayers.Count; i++)
                {
                    if (listPlayers[i].UserID == listCondidates[0].UserID)
                        break;
                }
                listPlayers.RemoveAt(i);
                
                //listPlayers.Select(p=>p.PlayerID==listCondidates[0].PlayerID)
                if(listPlayers.Select(c=>c.RoleID).Distinct().Count() <= 1)
                {
                    foreach (PlayerEntity play in listPlayers)
                    {
                        SendMessageThread(play.IP, "Judger says: Game over!" + ((RoleFlag)play.RoleID).ToString() + " wins this game!");
                    }
                    SendMessageThread(listCondidates[0].IP, "Judger says: Game over!" + ((RoleFlag)listPlayers[0].RoleID).ToString() + " wins this game!");


                    return true;
                }
                


                //Enter into next activity
                if ((ActivityTypeFlag)CurrentActivity.ActivityTypeID == ActivityTypeFlag.Vote)
                {
                    listPlayers = Business.GetPlayerEntities(RoleFlag.Civilian | RoleFlag.Killer | RoleFlag.Police, IsActiveFlag.Active, CurrentGame);
                    foreach (PlayerEntity play in listPlayers)
                    {
                        SendMessageThread(play.IP, "Judger says: Voting end, this is night, killers will kill somebody!");
                    }
                    CurrentActivity = Business.CreateNewActivity(CurrentGame);//, ActivityTypeFlag.Kill);
                }
                else if ((ActivityTypeFlag)CurrentActivity.ActivityTypeID == ActivityTypeFlag.Kill)
                {
                    listPlayers = Business.GetPlayerEntities(RoleFlag.Civilian | RoleFlag.Killer | RoleFlag.Police, IsActiveFlag.Active, CurrentGame);

                    foreach (PlayerEntity play in listPlayers)
                    {
                        SendMessageThread(play.IP, "Judger says: Killing end, this is night, vote anyone who you think is killer!");
                    }
                    CurrentActivity = Business.CreateNewActivity(CurrentGame);//, ActivityTypeFlag.Vote);
                }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?