public void MsgtoSer_JoinRoom(NetworkConnection cliconn, Msg_JoinRoom msg) { H.klog($"Got req to join from {cliconn.connectionId} , with name {msg.mename}"); if (GameMan._ins.roomTab_ser.ContainsKey(msg.idtojoin)) { //prepare for message to send GameMan.Room ro = GameMan._ins.roomTab_ser[msg.idtojoin]; List <int> memid = new List <int>(); List <string> memname = new List <string>(); List <bool> memstate = new List <bool>(); //addin LEADER to the list //memid.Add(ro.leader.connectionId); //memname.Add(ro.nameslist[ro.leader.connectionId]); //H.klog($"Memcount = {ro.members.Count}"); if (ro.members.Count != 0) { for (int i = 0; i < ro.members.Count; i++) { //add conn id to member id list to send to cliconn memid.Add(ro.members[i].connectionId); memname.Add(ro.nameslist[ro.members[i].connectionId]); //send a msg to each member of room, new one join ro.members[i].Send(new Msg_Lobby { playerconnid = cliconn.connectionId, playername = msg.mename, comcode = ComCode.JoinRoom }); // adding ready state to right client conn id if (ro.readyflags.ContainsKey(ro.members[i].connectionId)) { memstate.Add(ro.readyflags[ro.members[i].connectionId]); } else { memstate.Add(false); } } } //send message to leader to add new mem ro.leader.Send(new Msg_Lobby { playerconnid = cliconn.connectionId, playername = msg.mename, comcode = ComCode.JoinRoom }); //reset start ui button ro.leader.Send(new Msg_Lobby { comcode = ComCode.AllReady, enaRoomStart = false }); //H.klog($"Memid count = {memid.Count}"); Msg_JoinRoom mess = new Msg_JoinRoom { idtojoin = msg.idtojoin, members = memid, roommemnames = memname, memcount = memid.Count, memstate = memstate, leaderid = ro.leader.connectionId, leadername = ro.nameslist[ro.leader.connectionId], error = false }; cliconn.Send(mess); // adding cliconn as room member on server GameMan._ins.AddRoomPlayer_Ser(msg.idtojoin, cliconn, msg.mename); //adding player to player table GameMan._ins.playerTab_ser.Add(cliconn.connectionId, msg.idtojoin); } }
// SERVER CALLS THIS TO REMOVE AND PROMOTE NEW LEADER OR REMOVE ROOM FROM RECORD /// <summary> /// /// </summary> /// <param name="roomid"></param> /// <param name="cliconn"></param> /// <param name="mess"> Contains the id of conn that leave</param> private void LeaveAndPromote(string roomid, NetworkConnection cliconn, Msg_Lobby mess) { if (GameMan._ins.roomTab_ser.ContainsKey(roomid)) { // send msg to all players in the room GameMan.Room ro = GameMan._ins.roomTab_ser[roomid]; if (ro.members.Count == 0) { // remove the room from room list, cuz no one else left GameMan._ins.roomTab_ser.Remove(roomid); } else { if (cliconn != ro.leader) { ro.leader.Send(mess); ro.members.Remove(cliconn); ro.nameslist.Remove(cliconn.connectionId); } else { // promote the next one to be leader, need to be in different order ro.nameslist.Remove(ro.leader.connectionId); // remove name of conn as leader ro.leader = ro.members[0]; // promote ro.leader.Send(mess); // send msg to enable leader ui ro.leader.Send(new Msg_Lobby { comcode = ComCode.SetLeader, ureleader = true }); ro.members.Remove(ro.leader); // remove id of new leader from ready flags list if (ro.readyflags.ContainsKey(ro.leader.connectionId)) { ro.readyflags.Remove(ro.leader.connectionId); } } // loop over members and send them msg to remove from their local lobby for (int i = 0; i < ro.members.Count; i++) { ro.members[i].Send(mess); // send another message to update all with the new leader ro.members[i].Send(new Msg_Lobby { comcode = ComCode.ChangeLeader, playerconnid = ro.leader.connectionId }); } // remove flag from flag list if (ro.readyflags.ContainsKey(cliconn.connectionId)) { ro.readyflags.Remove(cliconn.connectionId); H.klog($"Remove Flag of players that left the room, remaining {ro.readyflags.Count}"); } // check ro ready flags, to enable start button for new leader CheckRoomReadyFlags(ro); } } }
public void MsgtoSer_StartGame(NetworkConnection cliconn, Msg_StartGame climsg) { if (climsg.comcode == ComCode.StartGame) { H.klog($"Server got msg to start game at room {climsg.roomid}"); if (GameMan._ins.roomTab_ser.ContainsKey(climsg.roomid)) { GameMan.Room ro = GameMan._ins.roomTab_ser[climsg.roomid]; // sending loading message to leader and all other member Msg_StartGame mess = new Msg_StartGame { sceneName = "Scene_001", comcode = ComCode.StartGame, load_scene_command = true }; ro.leader.Send(mess); for (int i = 0; i < ro.members.Count; i++) { ro.members[i].Send(mess); } GameMan._ins.SetupMatchIns(climsg.roomid); } } else if (climsg.comcode == ComCode.SceneLoading) { H.klog($"This client {cliconn.connectionId} finishes loading scene"); if (GameMan._ins.roomTab_ser.ContainsKey(climsg.roomid)) { GameMan.Room ro = GameMan._ins.roomTab_ser[climsg.roomid]; if (ro.members.Count == 0) { ro.leader.Send(new Msg_StartGame { comcode = ComCode.StartGame, rdyNspawn = true }); } else { ro.allDoneLoading += 1; if (ro.allDoneLoading == (ro.members.Count + 1)) { ro.allDoneLoading = 0; Msg_StartGame msg = new Msg_StartGame { comcode = ComCode.StartGame, rdyNspawn = true, load_scene_command = false }; ro.leader.Send(msg); for (int i = 0; i < ro.members.Count; i++) { ro.members[i].Send(msg); } } } } } }
public void MsgtoSer_Lobby(NetworkConnection cliconn, Msg_Lobby climsg) { if (climsg.comcode == ComCode.ReadyFlag) { if (GameMan._ins.roomTab_ser.ContainsKey(climsg.roomid)) { GameMan.Room ro = GameMan._ins.roomTab_ser[climsg.roomid]; if (ro.readyflags.ContainsKey(cliconn.connectionId)) { ro.readyflags[cliconn.connectionId] = climsg.rdyFlag; } else { ro.readyflags.Add(cliconn.connectionId, climsg.rdyFlag); } // loop over member in room and update that player's state Msg_Lobby messtocli = new Msg_Lobby { playerconnid = cliconn.connectionId, comcode = ComCode.ReadyFlag }; messtocli.rdyornot = climsg.rdyFlag ? "Ready" : "Not Ready"; if (ro.leader != cliconn) { ro.leader.Send(messtocli); } for (int i = 0; i < ro.members.Count; i++) { ro.members[i].Send(messtocli); } // in case if leader id exist in ready list, remove it if (ro.readyflags.ContainsKey(ro.leader.connectionId)) { ro.readyflags.Remove(ro.leader.connectionId); } // enable start button if all ready CheckRoomReadyFlags(ro); } } }
private void CheckRoomReadyFlags(GameMan.Room ro) { if ((ro.members.Count) == ro.readyflags.Count) { // check bool of all flags // set this to true, cuz if there no room member then leader only should have this button on to start by itself bool roomrdy = true; for (int i = 0; i < ro.members.Count; i++) { //if no members click on ready butt => readyflags list is empty, this will complain, should have a guard here if (ro.readyflags.Count != 0) { if (ro.readyflags.ContainsKey(ro.members[i].connectionId)) { roomrdy &= ro.readyflags[ro.members[i].connectionId]; } } else { roomrdy = false; //there are members but no one click ready so default should make it false } } H.klog($"Value of room ready Flag {roomrdy.ToString()}"); if (roomrdy) { ro.leader.Send(new Msg_Lobby { comcode = ComCode.AllReady, enaRoomStart = true }); } else { ro.leader.Send(new Msg_Lobby { comcode = ComCode.AllReady, enaRoomStart = false }); } } }