The implementation of PK system in web game

Source: Internet
Author: User

In the game development process, wrote a simple PK system panel, involving front-end and back-end interaction, I will make their own process to share to everyone, probably the process is this: the front-end send PK invitation to the back end, the back end is requested to return the information to the front-end processing, first look at the whole flowchart and ideas:

The entire process, as shown, is determined by several interactions between the server and the client to determine the state of the PK and whether it can be attacked. The service side will give me five agreements, probably as follows:

Message m_role_pk_invite_tos<29301>[router=Role,mod_role_handle] {RequiredDoublerole_id =1;//Object ID of the invitation PK}message M_role_pk_invite_toc<29302>{required Int32 err_code=1;//0 The invitation has been sent to the other, please wait for the response}message M_role_pk_info_toc<29304>{RequiredstringInvite_name =1;//inviter nameRequiredDoubleinvite_id =1;//inviter IDRequired Int32 invite_level =1;//inviter LevelRequiredDoubleInvite_time =1;//Timing End Timestamp}message M_role_pk_answer_tos<29305>[router=Role,mod_role_handle] {Required Int32 back_type=1;//0 for consent, 1 for reject, 2 for timeoutRequiredDoubleto_id =1;//agreed/rejected/timed-out objects}message M_role_pk_answer_toc<29306>{required Int32 err_code=1;//0 for success (hint: start PK)RequiredDoubleto_id =1;//read PK Object ID when successful}

In which the TOC represents the data sent to the client by the service, and the TOS indicates that the client sends the message to the server, they are all well-packaged data structure information, these are the agreed protocols between us and the backend, usually back-end to the front end we will develop a err_code that indicates whether the protocol was successfully sent to the client.

The front-end code processing is relatively simple, in the coding process, the data and views are placed in different modules, easy to manage, after accepting the invitation, we will give the invitee a pop-up window display and give 60 seconds to deal with. Focus on the code that the server sends to the client for processing:

Package MODULES.INVITEPK {/*Five Protocols*/Import proto.m_role_pk_answer_toc; import proto.m_role_pk_answer_tos; import Proto.m_role_pk_info_toc; Import Proto.m_role_pk_invite_toc; Import Proto.m_role_pk_invite_tos;  Public classInvitepkcase extends Basemodule { Publicfunction Invitepkcase () {super (); }  Private varInvitepkpanel:invitepkpanel; Private Static varinstance:invitepkcase;  Public Staticfunction getinstance (): invitepkcase {returninstance | | =NewInvitepkcase ();//single-case mode  }  Override protectedfunction Initlisteners ():void{Addmessagelistener (MODULECOMMAND.INVITE_PK, toinvite);//Invoke Listen event invitation PKAddmessagelistener (Modulecommand.invite_answer, Toanswer) Addmessagelistener (Modulecommand.invite_openpanel, Toopenpanel)//out Panel   /*Monitor data sent from the server, including three Protocols*/Addsocketlistener (Socketcommand.role_pk_info, onpkinfo);   Addsocketlistener (Socketcommand.role_pk_invite, oninvite);  Addsocketlistener (Socketcommand.role_pk_answer, onanswer); }  Privatefunction Toopenpanel (VO:M_ROLE_PK_INFO_TOC):void{Invitepkpanel=NewInvitepkpanel (Vo.invite_name, vo.invite_id, Vo.invite_level, vo.invite_time);//New A templateInvitepkpanel.durtime =Vo.invite_time; Invitepkpanel.open (); //Open PanelInvitepkpanel.readtime ();//Start Timing  }  Privatefunction Onanswer (VO:M_ROLE_PK_ANSWER_TOC):void {   if(Vo.err_code = =0) {    if(Vo.pk_type! =0) {fightmodule.getinstance (). Toopenpanel (VO);//display PK Both sides of the information Invitepkmanager.pkroleid=vo.to_id; Mangertime.last_time= -;//Mangertime is the management time classMangertime.endtime ();//Read Time}Else{Invitepkmanager.pkroleid=0; Dispatcher.dispatch (Modulecommand.fight_closepanel); //send off Message    }   } Else{toptip.addmousetipsmsg (Errorcode.geterror (Vo.err_code)); }  }  Privatefunction Toanswer (back_type:int, Id:number):void {   varVo:m_role_pk_answer_tos =NewM_role_pk_answer_tos ();//sent to the service sideVo.back_type =Back_type; vo.to_id=ID;  Sendsocketmessage (VO); }  Privatefunction Onpkinfo (VO:M_ROLE_PK_INFO_TOC):void{dispatch (Modulecommand.invite_openpanel, VO);//Open the panel to show the inviter's information  }  Privatefunction Oninvite (VO:M_ROLE_PK_INVITE_TOC):void {   //send a message to the client prompting me to have invited you PK   if(Vo.err_code = =0) {toptip.addmousetipsmsg ("PK has been invited, please wait for response");//indicates successful delivery}Else{toptip.addmousetipsmsg (Errorcode.geterror (Vo.err_code));//return reason for failure   }  }  Privatefunction Toinvite (tarid:number):void {   //this has the caller ID, which is sent to the server by the client.   varVo:m_role_pk_invite_tos =NewM_role_pk_invite_tos (); vo.role_id=Tarid; Sendsocketmessage (VO); //Send Message  } }}

The above Mangertime class is mainly used for PK both sides of the limit, if there is no action within 30 seconds after both sides accept the invitation, or 30 seconds to attack but then stop to continue to attack, time and start again, I use a static class to handle:

Package modules.fight {import Com.managers.dispatcher;import com.scene.scenemanager.loopmanager;import modules.    Modulecommand;import Modules.fight.FightView;  Public classMangertime { Public Static varLast_time:int= -;//static 30 seconds, each attack play can reset this time         Public Staticfunction Updateendtime ():void{last_time--; if(Last_time <0) {Last_time=0; Loopmanager.removefromsceond ("UpdateTime"); Fightview.getinstance (). Toclosewindow (); //PK End, close panel   } }     Public Staticfunction Readtime ():void{Loopmanager.addtosecond ("UpdateTime", updateendtime);//Start a timer start timer    }  }}

When listening to the fight event sent by the server, you can change the last_time to reset the time, join the timer, 30 seconds to after the two sides can close the panel, but it is important to note that we use the dictionary in AS3 to store the name of the function, So in the name of the time must not repeat the name, otherwise it may cause the program to run an error or the PK side only close a panel.

Summary: In the encounter and the server interaction only need to respond to the corresponding protocol logical judgment, the code in a modular form to clearly express their own ideas, the development of the comrades behind the code will not be very difficult, the development of the game industry is very fast, a good framework for the development of the speed will be very fast, So usually should be more specialized in how these panel underlying mechanisms are implemented, and record some of their own ideas.

The implementation of PK system in web game

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.