private static async Task <string> DealWith(string notifyJson) { Console.WriteLine($"notify receive:{notifyJson}"); File.AppendAllText("log/d.txt", $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}-{notifyJson}{Environment.NewLine}"); //File.AppendText("",) // CommonClass.TeamCreateFinish teamCreateFinish = Newtonsoft.Json.JsonConvert.DeserializeObject<CommonClass.TeamCreateFinish>(notifyJson); CommonClass.Command c = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.Command>(notifyJson); return(DealWithInterfaceAndObj(Program.rm, c, notifyJson)); }
private static async Task Echo(System.Net.WebSockets.WebSocket webSocket) { WebSocketReceiveResult wResult; { //byte[] buffer = new byte[size]; //var buffer = new ArraySegment<byte>(new byte[8192]); State s = new State(); s.WebsocketID = ConnectInfo.webSocketID++; s.Ls = LoginState.empty; s.roomIndex = -1; s.mapRoadAndCrossMd5 = ""; removeWsIsNotOnline(); addWs(webSocket, s.WebsocketID); var carsNames = new string[] { "车1", "车2", "车3", "车4", "车5" }; var playerName = "玩家" + Math.Abs(DateTime.Now.GetHashCode() % 10000); //if(s.Ls== LoginState.) do { try { var returnResult = await ReceiveStringAsync(webSocket, webWsSize); wResult = returnResult.wr; Console.WriteLine($"receive from web:{returnResult.result}"); CommonClass.Command c = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.Command>(returnResult.result); switch (c.c) { case "MapRoadAndCrossMd5": { if (s.Ls == LoginState.empty) { MapRoadAndCrossMd5 mapRoadAndCrossMd5 = Newtonsoft.Json.JsonConvert.DeserializeObject <MapRoadAndCrossMd5>(returnResult.result); s.mapRoadAndCrossMd5 = mapRoadAndCrossMd5.mapRoadAndCrossMd5; } }; break; case "CheckSession": { if (s.Ls == LoginState.empty) { CheckSession checkSession = Newtonsoft.Json.JsonConvert.DeserializeObject <CheckSession>(returnResult.result); var checkResult = await BLL.CheckSessionBLL.checkIsOK(checkSession, s); if (checkResult.CheckOK) { s.Key = checkResult.Key; s.roomIndex = checkResult.roomIndex; s = await Room.setOnLine(s, webSocket); } else { s = await Room.setState(s, webSocket, LoginState.selectSingleTeamJoin); } } }; break; case "JoinGameSingle": { JoinGameSingle joinType = Newtonsoft.Json.JsonConvert.DeserializeObject <JoinGameSingle>(returnResult.result); if (s.Ls == LoginState.selectSingleTeamJoin) { s = await Room.GetRoomThenStart(s, webSocket, playerName, carsNames); } }; break; case "CreateTeam": { CreateTeam ct = Newtonsoft.Json.JsonConvert.DeserializeObject <CreateTeam>(returnResult.result); if (s.Ls == LoginState.selectSingleTeamJoin) { { string command_start; CommonClass.TeamResult team; { s = await Room.setState(s, webSocket, LoginState.WaitingToStart); } { // command_start = CommonClass.Random.GetMD5HashFromStr(s.WebsocketID.ToString() + s.WebsocketID); team = await Team.createTeam2(s.WebsocketID, playerName, command_start); } { //var command_start = CommonClass.Random.GetMD5HashFromStr(s.WebsocketID.ToString() + s.WebsocketID); returnResult = await ReceiveStringAsync(webSocket, webWsSize); wResult = returnResult.wr; if (returnResult.result == command_start) { s = await Room.GetRoomThenStartAfterCreateTeam(s, webSocket, team, playerName, carsNames); } else { return; } } } } }; break; case "JoinTeam": { JoinTeam ct = Newtonsoft.Json.JsonConvert.DeserializeObject <JoinTeam>(returnResult.result); if (s.Ls == LoginState.selectSingleTeamJoin) { { string command_start; { //将状态设置为等待开始和等待加入 s = await Room.setState(s, webSocket, LoginState.WaitingToGetTeam); } { returnResult = await ReceiveStringAsync(webSocket, webWsSize); wResult = returnResult.wr; var teamID = returnResult.result; command_start = CommonClass.Random.GetMD5HashFromStr(s.WebsocketID.ToString() + s.WebsocketID + DateTime.Now.ToString()); var result = await Team.findTeam2(s.WebsocketID, playerName, command_start, teamID); if (result == "ok") { returnResult = await ReceiveStringAsync(webSocket, webWsSize); wResult = returnResult.wr; int roomIndex; if (Room.CheckSecret(returnResult.result, command_start, out roomIndex)) { s = await Room.GetRoomThenStartAfterJoinTeam(s, webSocket, roomIndex, playerName, carsNames); } else { return; } } else if (result == "game has begun") { s = await Room.setState(s, webSocket, LoginState.selectSingleTeamJoin); await Room.Alert(webSocket, $"他们已经开始了!"); } else if (result == "is not number") { s = await Room.setState(s, webSocket, LoginState.selectSingleTeamJoin); await Room.Alert(webSocket, $"请输入数字"); } else if (result == "not has the team") { s = await Room.setState(s, webSocket, LoginState.selectSingleTeamJoin); await Room.Alert(webSocket, $"没有该队伍({teamID})"); } else if (result == "team is full") { s = await Room.setState(s, webSocket, LoginState.selectSingleTeamJoin); await Room.Alert(webSocket, "该队伍已满员"); } else { s = await Room.setState(s, webSocket, LoginState.selectSingleTeamJoin); } } } } }; break; case "SetCarsName": { if (s.Ls == LoginState.selectSingleTeamJoin) { SetCarsName setCarsName = Newtonsoft.Json.JsonConvert.DeserializeObject <SetCarsName>(returnResult.result); for (var i = 0; i < 5; i++) { if (!string.IsNullOrEmpty(setCarsName.Names[i])) { if (setCarsName.Names[i].Trim().Length >= 2 && setCarsName.Names[i].Trim().Length < 7) { carsNames[i] = setCarsName.Names[i].Trim(); } } } } }; break; case "GetCarsName": { if (s.Ls == LoginState.selectSingleTeamJoin) { var msg = Newtonsoft.Json.JsonConvert.SerializeObject(new { c = "GetCarsName", names = carsNames }); var sendData = Encoding.UTF8.GetBytes(msg); await webSocket.SendAsync(new ArraySegment <byte>(sendData, 0, sendData.Length), WebSocketMessageType.Text, true, CancellationToken.None); } }; break; case "SetPlayerName": { if (s.Ls == LoginState.selectSingleTeamJoin) { SetPlayerName setPlayerName = Newtonsoft.Json.JsonConvert.DeserializeObject <SetPlayerName>(returnResult.result); playerName = setPlayerName.Name; } }; break; case "GetName": { if (s.Ls == LoginState.selectSingleTeamJoin) { var msg = Newtonsoft.Json.JsonConvert.SerializeObject(new { c = "GetName", name = playerName }); var sendData = Encoding.UTF8.GetBytes(msg); await webSocket.SendAsync(new ArraySegment <byte>(sendData, 0, sendData.Length), WebSocketMessageType.Text, true, CancellationToken.None); } }; break; //case "SetCarName": // { // if (s.Ls == LoginState.selectSingleTeamJoin) // { // SetCarName setCarName = Newtonsoft.Json.JsonConvert.DeserializeObject<SetCarName>(returnResult.result); // if (setCarName.Name.Trim().Length < 7 && setCarName.Name.Trim().Length > 1) // { // if (setCarName.CarIndex >= 0 && setCarName.CarIndex < 5) // { // carsNames[setCarName.CarIndex] = setCarName.Name; // } // } // //playerName = setPlayerName.Name; // } // }; break; case "Promote": { if (s.Ls == LoginState.OnLine) { Promote promote = Newtonsoft.Json.JsonConvert.DeserializeObject <Promote>(returnResult.result); await Room.setPromote(s, promote); } }; break; case "Collect": { if (s.Ls == LoginState.OnLine) { Collect collect = Newtonsoft.Json.JsonConvert.DeserializeObject <Collect>(returnResult.result); await Room.setCollect(s, collect); } }; break; case "Attack": { if (s.Ls == LoginState.OnLine) { Attack attack = Newtonsoft.Json.JsonConvert.DeserializeObject <Attack>(returnResult.result); await Room.setAttack(s, attack); } }; break; case "Tax": { if (s.Ls == LoginState.OnLine) { Tax tax = Newtonsoft.Json.JsonConvert.DeserializeObject <Tax>(returnResult.result); await Room.setToCollectTax(s, tax); } }; break; case "Msg": { if (s.Ls == LoginState.OnLine) { Msg msg = Newtonsoft.Json.JsonConvert.DeserializeObject <Msg>(returnResult.result); if (msg.MsgPass.Length < 120) { await Room.passMsg(s, msg); } } }; break; case "Ability": { if (s.Ls == LoginState.OnLine) { Ability a = Newtonsoft.Json.JsonConvert.DeserializeObject <Ability>(returnResult.result); await Room.setCarAbility(s, a); } }; break; case "SetCarReturn": { if (s.Ls == LoginState.OnLine) { SetCarReturn scr = Newtonsoft.Json.JsonConvert.DeserializeObject <SetCarReturn>(returnResult.result); await Room.setCarReturn(s, scr); } }; break; case "Donate": { if (s.Ls == LoginState.OnLine) { Donate donate = Newtonsoft.Json.JsonConvert.DeserializeObject <Donate>(returnResult.result); await Room.Donate(s, donate); } }; break; case "GetSubsidize": { if (s.Ls == LoginState.OnLine) { GetSubsidize getSubsidize = Newtonsoft.Json.JsonConvert.DeserializeObject <GetSubsidize>(returnResult.result); await Room.GetSubsidize(s, getSubsidize); } }; break; case "OrderToSubsidize": { if (s.Ls == LoginState.OnLine) { GetSubsidize getSubsidize = Newtonsoft.Json.JsonConvert.DeserializeObject <GetSubsidize>(returnResult.result); await Room.GetSubsidize(s, getSubsidize); } }; break; case "Bust": { if (s.Ls == LoginState.OnLine) { Bust bust = Newtonsoft.Json.JsonConvert.DeserializeObject <Bust>(returnResult.result); await Room.setBust(s, bust); } }; break; case "BuyDiamond": { if (s.Ls == LoginState.OnLine) { BuyDiamond bd = Newtonsoft.Json.JsonConvert.DeserializeObject <BuyDiamond>(returnResult.result); await Room.buyDiamond(s, bd); } }; break; case "SellDiamond": { if (s.Ls == LoginState.OnLine) { BuyDiamond bd = Newtonsoft.Json.JsonConvert.DeserializeObject <BuyDiamond>(returnResult.result); await Room.sellDiamond(s, bd); } }; break; } } catch (Exception e) { Console.WriteLine($"{ Newtonsoft.Json.JsonConvert.SerializeObject(e)}"); await Room.setOffLine(s); removeWs(s.WebsocketID); // Console.WriteLine($"step2:webSockets数量:{ BufferImage.webSockets.Count}"); // return; throw e; } }while (!wResult.CloseStatus.HasValue); await Room.setOffLine(s); removeWs(s.WebsocketID); //try //{ // // await webSocket.CloseAsync(wResult.CloseStatus.Value, wResult.CloseStatusDescription, CancellationToken.None); // // ConnectInfo.connectedWs.Remove(c.WebSocketID); //} //catch (Exception e) //{ // throw e; // // ConnectInfo.connectedWs.Remove(c.WebSocketID); // // return; //} }; }
private static async Task <string> DealWith(string notifyJson) { Console.WriteLine($"notify receive:{notifyJson}"); File.AppendAllText("log/d.txt", $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}-{notifyJson}{Environment.NewLine}"); //File.AppendText("",) // CommonClass.TeamCreateFinish teamCreateFinish = Newtonsoft.Json.JsonConvert.DeserializeObject<CommonClass.TeamCreateFinish>(notifyJson); string outPut = "haveNothingToReturn"; { CommonClass.Command c = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.Command>(notifyJson); switch (c.c) { case "PlayerAdd_V2": { CommonClass.PlayerAdd_V2 addItem = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.PlayerAdd_V2>(notifyJson); var result = Program.rm.AddPlayer(addItem); outPut = result; }; break; case "GetPosition": { CommonClass.GetPosition getPosition = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.GetPosition>(notifyJson); //string fromUrl; var GPResult = Program.rm.GetPosition(getPosition); if (GPResult.Success) { CommonClass.GetPositionNotify_v2 notify = new CommonClass.GetPositionNotify_v2() { c = "GetPositionNotify_v2", fp = GPResult.Fp, WebSocketID = GPResult.WebSocketID, key = getPosition.Key, PlayerName = GPResult.PlayerName, positionInStation = GPResult.positionInStation }; Startup.sendMsg(GPResult.FromUrl, Newtonsoft.Json.JsonConvert.SerializeObject(notify)); var notifyMsgs = GPResult.NotifyMsgs; for (var i = 0; i < notifyMsgs.Count; i += 2) { Startup.sendMsg(notifyMsgs[i], notifyMsgs[i + 1]); } } outPut = "ok"; }; break; case "SetPromote": { CommonClass.SetPromote sp = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.SetPromote>(notifyJson); var result = Program.rm.updatePromote(sp); outPut = "ok"; //await context.Response.WriteAsync("ok"); }; break; case "SetAbility": { CommonClass.SetAbility sa = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.SetAbility>(notifyJson); Program.rm.SetAbility(sa); outPut = "ok"; }; break; case "PlayerCheck": { CommonClass.PlayerCheck checkItem = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.PlayerCheck>(notifyJson); var result = Program.rm.UpdatePlayer(checkItem); outPut = result; }; break; case "SetCollect": { CommonClass.SetCollect sc = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.SetCollect>(notifyJson); var result = Program.rm.updateCollect(sc); outPut = "ok"; //await context.Response.WriteAsync("ok"); }; break; case "SetAttack": { CommonClass.SetAttack sa = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.SetAttack>(notifyJson); var result = Program.rm.updateAttack(sa); outPut = "ok"; //await context.Response.WriteAsync("ok"); }; break; case "SetBust": { CommonClass.SetBust sa = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.SetBust>(notifyJson); var result = Program.rm.updateBust(sa); outPut = "ok"; //await context.Response.WriteAsync("ok"); }; break; case "GetFrequency": { outPut = Program.rm.GetFrequency().ToString(); }; break; case "OrderToReturn": { CommonClass.OrderToReturn otr = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.OrderToReturn>(notifyJson); Program.rm.OrderToReturn(otr); outPut = "ok"; }; break; case "SaveMoney": { CommonClass.SaveMoney saveMoney = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.SaveMoney>(notifyJson); Program.rm.SaveMoney(saveMoney); outPut = "ok"; }; break; case "SetTax": { CommonClass.SetTax st = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.SetTax>(notifyJson); var result = Program.rm.updateTax(st); outPut = "ok"; //await context.Response.WriteAsync("ok"); }; break; case "MarketPrice": { CommonClass.MarketPrice sa = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.MarketPrice>(notifyJson); Program.rm.Market.Update(sa); }; break; case "SetBuyDiamond": { CommonClass.SetBuyDiamond bd = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.SetBuyDiamond>(notifyJson); Program.rm.Buy(bd); }; break; case "SetSellDiamond": { CommonClass.SetSellDiamond ss = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.SetSellDiamond>(notifyJson); Program.rm.Sell(ss); }; break; case "OrderToSubsidize": { CommonClass.OrderToSubsidize ots = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.OrderToSubsidize>(notifyJson); Program.rm.OrderToSubsidize(ots); outPut = "ok"; }; break; case "DialogMsg": { CommonClass.DialogMsg dm = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.DialogMsg>(notifyJson); Program.rm.SendMsg(dm); outPut = "ok"; }; break; } } { return(outPut); } }
static string DealWithInterfaceAndObj(interfaceOfHM.ListenInterface objI, CommonClass.Command c, string notifyJson) { string outPut = "haveNothingToReturn"; { // CommonClass.Command c = Newtonsoft.Json.JsonConvert.DeserializeObject<CommonClass.Command>(notifyJson); switch (c.c) { case "PlayerAdd_V2": { CommonClass.PlayerAdd_V2 addItem = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.PlayerAdd_V2>(notifyJson); var result = objI.AddPlayer(addItem); outPut = result; }; break; case "GetPosition": { CommonClass.GetPosition getPosition = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.GetPosition>(notifyJson); //string fromUrl; var GPResult = objI.GetPosition(getPosition); if (GPResult.Success) { CommonClass.GetPositionNotify_v2 notify = new CommonClass.GetPositionNotify_v2() { c = "GetPositionNotify_v2", fp = GPResult.Fp, WebSocketID = GPResult.WebSocketID, key = getPosition.Key, PlayerName = GPResult.PlayerName, positionInStation = GPResult.positionInStation, fPIndex = GPResult.fPIndex }; Startup.sendMsg(GPResult.FromUrl, Newtonsoft.Json.JsonConvert.SerializeObject(notify)); var notifyMsgs = GPResult.NotifyMsgs; for (var i = 0; i < notifyMsgs.Count; i += 2) { Startup.sendMsg(notifyMsgs[i], notifyMsgs[i + 1]); } } outPut = "ok"; }; break; case "SetPromote": { CommonClass.SetPromote sp = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.SetPromote>(notifyJson); var result = objI.updatePromote(sp); outPut = "ok"; //await context.Response.WriteAsync("ok"); }; break; case "SetAbility": { CommonClass.SetAbility sa = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.SetAbility>(notifyJson); objI.SetAbility(sa); outPut = "ok"; }; break; case "PlayerCheck": { CommonClass.PlayerCheck checkItem = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.PlayerCheck>(notifyJson); var result = objI.UpdatePlayer(checkItem); outPut = result; }; break; case "SetCollect": { CommonClass.SetCollect sc = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.SetCollect>(notifyJson); var result = objI.updateCollect(sc); outPut = "ok"; //await context.Response.WriteAsync("ok"); }; break; case "SetAttack": { CommonClass.SetAttack sa = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.SetAttack>(notifyJson); var result = objI.updateAttack(sa); outPut = "ok"; //await context.Response.WriteAsync("ok"); }; break; //case "SetBust": // { // CommonClass.SetBust sa = Newtonsoft.Json.JsonConvert.DeserializeObject<CommonClass.SetBust>(notifyJson); // var result = objI.updateBust(sa); // outPut = "ok"; // //await context.Response.WriteAsync("ok"); // }; break; case "GetFrequency": { outPut = Program.rm.GetFrequency().ToString(); }; break; case "OrderToReturn": { CommonClass.OrderToReturn otr = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.OrderToReturn>(notifyJson); objI.OrderToReturn(otr); outPut = "ok"; }; break; case "SaveMoney": { CommonClass.SaveMoney saveMoney = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.SaveMoney>(notifyJson); objI.SaveMoney(saveMoney); outPut = "ok"; }; break; //case "SetTax": // { // CommonClass.SetTax st = Newtonsoft.Json.JsonConvert.DeserializeObject<CommonClass.SetTax>(notifyJson); // var result = Program.rm.updateTax(st); // outPut = "ok"; // //await context.Response.WriteAsync("ok"); // }; break; case "MarketPrice": { CommonClass.MarketPrice sa = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.MarketPrice>(notifyJson); objI.MarketUpdate(sa); }; break; case "SetBuyDiamond": { CommonClass.SetBuyDiamond bd = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.SetBuyDiamond>(notifyJson); objI.Buy(bd); }; break; case "SetSellDiamond": { CommonClass.SetSellDiamond ss = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.SetSellDiamond>(notifyJson); objI.Sell(ss); }; break; case "OrderToSubsidize": { CommonClass.OrderToSubsidize ots = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.OrderToSubsidize>(notifyJson); objI.OrderToSubsidize(ots); outPut = "ok"; }; break; case "DialogMsg": { CommonClass.DialogMsg dm = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.DialogMsg>(notifyJson); objI.SendMsg(dm); outPut = "ok"; }; break; case "SetSelectDriver": { CommonClass.SetSelectDriver dm = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.SetSelectDriver>(notifyJson); objI.SelectDriver(dm); outPut = "ok"; }; break; case "MagicSkill": { CommonClass.MagicSkill ms = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.MagicSkill>(notifyJson); var result = objI.updateMagic(ms); outPut = "ok"; }; break; case "View": { CommonClass.View v = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.View>(notifyJson); var result = objI.updateView(v); outPut = "ok"; }; break; case "GetFirstRoad": { //CommonClass.View v = Newtonsoft.Json.JsonConvert.DeserializeObject<CommonClass.View>(notifyJson); var result = objI.GetFirstRoad(); outPut = result; }; break; case "DrawRoad": { CommonClass.MapEditor.DrawRoad v = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.MapEditor.DrawRoad>(notifyJson); outPut = objI.DrawRoad(v); }; break; case "NextCross": { CommonClass.MapEditor.NextCross v = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.MapEditor.NextCross>(notifyJson); outPut = objI.NextCross(v); }; break; case "PreviousCross": { CommonClass.MapEditor.PreviousCross v = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.MapEditor.PreviousCross>(notifyJson); outPut = objI.PreviousCross(v); }; break; case "GetCatege": { CommonClass.MapEditor.GetCatege gc = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.MapEditor.GetCatege>(notifyJson); outPut = objI.GetCatege(gc); // Console.WriteLine(outPut); }; break; case "GetAbtractModels": { CommonClass.MapEditor.GetAbtractModels gam = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.MapEditor.GetAbtractModels>(notifyJson); outPut = objI.GetAbtractModels(gam); // Console.WriteLine(outPut); }; break; case "SaveObjInfo": { CommonClass.MapEditor.SaveObjInfo soi = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.MapEditor.SaveObjInfo>(notifyJson); outPut = objI.SaveObjInfo(soi); }; break; case "ShowOBJFile": { CommonClass.MapEditor.ShowOBJFile sof = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.MapEditor.ShowOBJFile>(notifyJson); outPut = objI.ShowOBJFile(sof); }; break; case "UpdateObjInfo": { CommonClass.MapEditor.UpdateObjInfo uoi = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.MapEditor.UpdateObjInfo>(notifyJson); outPut = objI.UpdateObjInfo(uoi); }; break; case "DelObjInfo": { CommonClass.MapEditor.DelObjInfo doi = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.MapEditor.DelObjInfo>(notifyJson); outPut = objI.DelObjInfo(doi); }; break; } } { return(outPut); } }
public void startNewCommandThread(int startT, CommonClass.Command command, interfaceOfEngine.startNewCommandThread objNeedToStartNewCommandThread) { Thread th = new Thread(() => newThreadDoBefore(startT, command, objNeedToStartNewCommandThread)); th.Start(); }
private static void notify(IApplicationBuilder app) { throw new Exception("调用了已作废的方法!"); //app.Run(async context => //{ // if (context.WebSockets.IsWebSocketRequest) // { // var webSocket = await context.WebSockets.AcceptWebSocketAsync(); // await dealWithNotify(webSocket); // } //}); //return; app.Run(async context => { if (context.Request.Method.ToLower() == "post") { var notifyJson = getBodyStr(context); var t = Convert.ToInt32((DateTime.Now - Program.startTime).TotalMilliseconds); //File.AppendAllText("debugLog.txt", Newtonsoft.Json.JsonConvert.SerializeObject // ( // new { t = t, notifyJson = notifyJson } // )); File.AppendAllText("debugLog.txt", $"Common.awaitF({t}, startTime);" + Environment.NewLine); File.AppendAllText("debugLog.txt", $"await Common.SendInfomation(url, \"{notifyJson.Replace("\"", "\\\"")}\");" + Environment.NewLine); File.AppendAllText("debugLog.txt", "" + Environment.NewLine); Console.WriteLine($"notify receive:{notifyJson}"); CommonClass.Command c = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.Command>(notifyJson); switch (c.c) { case "PlayerAdd": { CommonClass.PlayerAdd addItem = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.PlayerAdd>(notifyJson); var result = BaseInfomation.rm.AddPlayer(addItem); await context.Response.WriteAsync(result); }; break; case "PlayerCheck": { CommonClass.PlayerCheck checkItem = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.PlayerCheck>(notifyJson); var result = BaseInfomation.rm.UpdatePlayer(checkItem); await context.Response.WriteAsync(result); }; break; case "Map": { CommonClass.Map map = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.Map>(notifyJson); switch (map.DataType) { case "All": { // public void getAll(out List<double[]> meshPoints, out List<object> listOfCrosses) List <double[]> meshPoints; List <object> listOfCrosses; Program.dt.getAll(out meshPoints, out listOfCrosses); var json = Newtonsoft.Json.JsonConvert.SerializeObject(new { meshPoints = meshPoints, listOfCrosses = listOfCrosses }); await context.Response.WriteAsync(json); }; break; } }; break; case "GetPosition": { CommonClass.GetPosition getPosition = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.GetPosition>(notifyJson); //string fromUrl; var GPResult = await BaseInfomation.rm.GetPosition(getPosition); if (GPResult.Success) { CommonClass.GetPositionNotify notify = new CommonClass.GetPositionNotify() { c = "GetPositionNotify", fp = GPResult.Fp, WebSocketID = GPResult.WebSocketID, carsNames = GPResult.CarsNames, key = getPosition.Key }; await sendMsg(GPResult.FromUrl, Newtonsoft.Json.JsonConvert.SerializeObject(notify)); var notifyMsgs = GPResult.NotifyMsgs; for (var i = 0; i < notifyMsgs.Count; i += 2) { await sendMsg(notifyMsgs[i], notifyMsgs[i + 1]); } } await context.Response.WriteAsync("ok"); }; break; //case "GetRightAndDuty": // { // };break; case "FinishTask": { }; break; case "SetPromote": { CommonClass.SetPromote sp = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.SetPromote>(notifyJson); var result = await BaseInfomation.rm.updatePromote(sp); await context.Response.WriteAsync("ok"); }; break; case "SetCollect": { CommonClass.SetCollect sc = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.SetCollect>(notifyJson); var result = await BaseInfomation.rm.updateCollect(sc); await context.Response.WriteAsync("ok"); }; break; case "SetAttack": { CommonClass.SetAttack sa = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.SetAttack>(notifyJson); var result = await BaseInfomation.rm.updateAttack(sa); await context.Response.WriteAsync("ok"); }; break; case "SetTax": { CommonClass.SetTax st = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.SetTax>(notifyJson); var result = await BaseInfomation.rm.updateTax(st); await context.Response.WriteAsync("ok"); }; break; case "CommandToReturn": { }; break; } } }); }
void newThreadDoBefore(int startT, CommonClass.Command command, interfaceOfEngine.startNewCommandThread objNeedToStartNewThread) { Thread.Sleep(startT); objNeedToStartNewThread.newThreadDo(command); }
private async Task <string> DealWith(string notifyJson) { Console.WriteLine($"DealWith-Msg-{notifyJson}"); CommonClass.Command c = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.Command>(notifyJson); switch (c.c) { case "MarketIn": { CommonClass.MarketIn mi = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.MarketIn>(notifyJson); switch (mi.pType) { case "mile": { var price1 = getPrice(this.mileCount); this.mileCount += mi.count; var price2 = getPrice(this.mileCount); if (price1 != price2) { await tellMarketItem(mi.pType); saveCount(mi.pType, this.mileCount); } }; break; case "business": { var price1 = getPrice(this.businessCount); this.businessCount += mi.count; var price2 = getPrice(this.businessCount); if (price1 != price2) { await tellMarketItem(mi.pType); saveCount(mi.pType, this.businessCount); } }; break; case "volume": { var price1 = getPrice(this.volumeCount); this.volumeCount += mi.count; var price2 = getPrice(this.volumeCount); if (price1 != price2) { await tellMarketItem(mi.pType); saveCount(mi.pType, this.volumeCount); } }; break; case "speed": { var price1 = getPrice(this.speedCount); this.speedCount += mi.count; var price2 = getPrice(this.speedCount); if (price1 != price2) { await tellMarketItem(mi.pType); saveCount(mi.pType, this.speedCount); } }; break; } }; break; case "MarketOut": { CommonClass.MarketOut mo = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.MarketOut>(notifyJson); switch (mo.pType) { case "mile": { var price1 = getPrice(this.mileCount); this.mileCount -= mo.count; var price2 = getPrice(this.mileCount); if (price1 != price2) { await tellMarketItem(mo.pType); saveCount(mo.pType, this.mileCount); } }; break; case "business": { var price1 = getPrice(this.businessCount); this.businessCount -= mo.count; var price2 = getPrice(this.businessCount); if (price1 != price2) { await tellMarketItem(mo.pType); saveCount(mo.pType, this.businessCount); } }; break; case "volume": { var price1 = getPrice(this.volumeCount); this.volumeCount -= mo.count; var price2 = getPrice(this.volumeCount); if (price1 != price2) { await tellMarketItem(mo.pType); saveCount(mo.pType, this.volumeCount); } }; break; case "speed": { var price1 = getPrice(this.speedCount); this.speedCount -= mo.count; var price2 = getPrice(this.speedCount); if (price1 != price2) { await tellMarketItem(mo.pType); saveCount(mo.pType, this.speedCount); } }; break; } }; break; } return(""); }
private static async Task <string> DealWith(string notifyJson) { Console.WriteLine($"notify receive:{notifyJson}"); // CommonClass.TeamCreateFinish teamCreateFinish = Newtonsoft.Json.JsonConvert.DeserializeObject<CommonClass.TeamCreateFinish>(notifyJson); string outPut = "haveNothingToReturn"; { { Console.WriteLine($"json:{notifyJson}"); var t = Convert.ToInt32((DateTime.Now - Program.startTime).TotalMilliseconds); //File.AppendAllText("debugLog.txt", Newtonsoft.Json.JsonConvert.SerializeObject // ( // new { t = t, notifyJson = notifyJson } // )); File.AppendAllText("debugLog.txt", $"awaitF({t})" + Environment.NewLine); File.AppendAllText("debugLog.txt", $"SendInfomation({notifyJson})" + Environment.NewLine); File.AppendAllText("debugLog.txt", "" + Environment.NewLine); Console.WriteLine($"notify receive:{notifyJson}"); CommonClass.Command c = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.Command>(notifyJson); switch (c.c) { case "PlayerAdd": { CommonClass.PlayerAdd addItem = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.PlayerAdd>(notifyJson); var result = BaseInfomation.rm.AddPlayer(addItem); outPut = result; }; break; case "PlayerCheck": { CommonClass.PlayerCheck checkItem = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.PlayerCheck>(notifyJson); var result = BaseInfomation.rm.UpdatePlayer(checkItem); outPut = result; }; break; case "Map": { CommonClass.Map map = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.Map>(notifyJson); switch (map.DataType) { case "All": { // public void getAll(out List<double[]> meshPoints, out List<object> listOfCrosses) List <double[]> meshPoints; List <object> listOfCrosses; Program.dt.getAll(out meshPoints, out listOfCrosses); var json = Newtonsoft.Json.JsonConvert.SerializeObject(new { meshPoints = meshPoints, listOfCrosses = listOfCrosses }); outPut = json; }; break; } }; break; case "GetPosition": { CommonClass.GetPosition getPosition = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.GetPosition>(notifyJson); //string fromUrl; var GPResult = await BaseInfomation.rm.GetPosition(getPosition); if (GPResult.Success) { CommonClass.GetPositionNotify notify = new CommonClass.GetPositionNotify() { c = "GetPositionNotify", fp = GPResult.Fp, WebSocketID = GPResult.WebSocketID, carsNames = GPResult.CarsNames, key = getPosition.Key, PlayerName = GPResult.PlayerName }; await sendMsg(GPResult.FromUrl, Newtonsoft.Json.JsonConvert.SerializeObject(notify), true); var notifyMsgs = GPResult.NotifyMsgs; for (var i = 0; i < notifyMsgs.Count; i += 2) { await sendMsg(notifyMsgs[i], notifyMsgs[i + 1], true); } } outPut = "ok"; }; break; case "FinishTask": { }; break; case "SetPromote": { CommonClass.SetPromote sp = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.SetPromote>(notifyJson); var result = await BaseInfomation.rm.updatePromote(sp); outPut = "ok"; //await context.Response.WriteAsync("ok"); }; break; case "SetCollect": { CommonClass.SetCollect sc = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.SetCollect>(notifyJson); var result = await BaseInfomation.rm.updateCollect(sc); outPut = "ok"; //await context.Response.WriteAsync("ok"); }; break; case "SetAttack": { CommonClass.SetAttack sa = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.SetAttack>(notifyJson); var result = await BaseInfomation.rm.updateAttack(sa); outPut = "ok"; //await context.Response.WriteAsync("ok"); }; break; case "SetBust": { CommonClass.SetBust sa = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.SetBust>(notifyJson); var result = await BaseInfomation.rm.updateBust(sa); outPut = "ok"; //await context.Response.WriteAsync("ok"); }; break; case "SetTax": { CommonClass.SetTax st = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.SetTax>(notifyJson); var result = await BaseInfomation.rm.updateTax(st); outPut = "ok"; //await context.Response.WriteAsync("ok"); }; break; case "DialogMsg": { CommonClass.DialogMsg dm = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.DialogMsg>(notifyJson); await BaseInfomation.rm.SendMsg(dm); outPut = "ok"; }; break; case "SetAbility": { CommonClass.SetAbility sa = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.SetAbility>(notifyJson); await BaseInfomation.rm.SetAbility(sa); outPut = "ok"; }; break; case "OrderToReturn": { CommonClass.OrderToReturn otr = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.OrderToReturn>(notifyJson); await BaseInfomation.rm.OrderToReturn(otr); outPut = "ok"; }; break; case "SaveMoney": { CommonClass.SaveMoney saveMoney = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.SaveMoney>(notifyJson); await BaseInfomation.rm.SaveMoney(saveMoney); outPut = "ok"; }; break; case "OrderToSubsidize": { CommonClass.OrderToSubsidize ots = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.OrderToSubsidize>(notifyJson); await BaseInfomation.rm.OrderToSubsidize(ots); outPut = "ok"; }; break; case "SetBustAttack": { CommonClass.SetAttack sa = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.SetAttack>(notifyJson); var result = await BaseInfomation.rm.updateAttack(sa); outPut = "ok"; //await context.Response.WriteAsync("ok"); }; break; case "GetFrequency": { outPut = BaseInfomation.rm.GetFrequency().ToString(); }; break; case "MarketPrice": { CommonClass.MarketPrice sa = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.MarketPrice>(notifyJson); BaseInfomation.rm.Market.Update(sa); }; break; case "SetBuyDiamond": { CommonClass.SetBuyDiamond bd = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.SetBuyDiamond>(notifyJson); BaseInfomation.rm.Buy(bd); }; break; case "SetSellDiamond": { CommonClass.SetSellDiamond ss = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.SetSellDiamond>(notifyJson); BaseInfomation.rm.Sell(ss); }; break; } } } { return(outPut); } }
private async Task dealWithFindTeam(WebSocket webSocketFromGameHandler) { WebSocketReceiveResult Wrr; do { var returnResult = await ReceiveStringAsync(webSocketFromGameHandler, 1024 * 1024 * 10); Wrr = returnResult.wr; //returnResult.wr; string outPut = "haveNothingToReturn"; { var notifyJson = returnResult.result; // var notifyJson = getBodyStr(context); Console.WriteLine($"createTeam receive:{notifyJson}"); CommonClass.Command c = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.Command>(notifyJson); switch (c.c) { case "TeamJoin": { /* * TeamJoin,如果传入的字段不是数字,返回 is not number * TeamJoin,如果传入的字段如果是数字,不存在这个队伍 返回not has the team * TeamJoin,如果队伍中人数已满,不存在这个队伍 返回team is full * */ CommonClass.TeamJoin teamJoin = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.TeamJoin>(notifyJson); int teamIndex; if (int.TryParse(teamJoin.TeamIndex, out teamIndex)) { bool memberIsFull = false; bool notHasTheTeam = false; Team t = null; lock (Program.teamLock) { if (Program.allTeams.ContainsKey(teamIndex)) { if (Program.allTeams[teamIndex].member.Count >= 4) { memberIsFull = true; } else { Program.allTeams[teamIndex].member.Add(teamJoin); t = Program.allTeams[teamIndex]; } } else { notHasTheTeam = true; } } if (memberIsFull) { outPut = "team is full"; // await context.Response.WriteAsync(""); } else if (notHasTheTeam) { outPut = "not has the team"; //await context.Response.WriteAsync(""); } else if (t.IsBegun) { outPut = "game has begun"; //t.IsBegun 必须在判断 notHasTheTeam 之后。否则t可能为null // await context.Response.WriteAsync(""); } else { var PlayerNames = new List <string>(); CommonClass.TeamJoinFinish teamJoinFinish = new CommonClass.TeamJoinFinish() { c = "TeamJoinFinish", PlayerNames = new List <string>(), TeamNum = t.TeamID, WebSocketID = teamJoin.WebSocketID // PlayerNames = }; { CommonClass.TeamJoinBroadInfo addInfomation = new CommonClass.TeamJoinBroadInfo() { c = "TeamJoinBroadInfo", PlayerName = teamJoin.PlayerName, WebSocketID = t.captain.WebSocketID }; await sendMsg(t.captain.FromUrl, Newtonsoft.Json.JsonConvert.SerializeObject(addInfomation)); } teamJoinFinish.PlayerNames.Add(t.captain.PlayerName); for (var i = 0; i < t.member.Count; i++) { teamJoinFinish.PlayerNames.Add(t.member[i].PlayerName); if (t.member[i].FromUrl == teamJoin.FromUrl && t.member[i].WebSocketID == teamJoin.WebSocketID) { } else { CommonClass.TeamJoinBroadInfo addInfomation = new CommonClass.TeamJoinBroadInfo() { c = "TeamJoinBroadInfo", PlayerName = teamJoin.PlayerName, WebSocketID = t.member[i].WebSocketID }; await sendMsg(t.member[i].FromUrl, Newtonsoft.Json.JsonConvert.SerializeObject(addInfomation)); } } await sendMsg(teamJoin.FromUrl, Newtonsoft.Json.JsonConvert.SerializeObject(teamJoinFinish)); outPut = "ok"; // await context.Response.WriteAsync("ok"); // t.captain. //await context.Response.WriteAsync("not has the team"); } } else { outPut = "is not number"; // await context.Response.WriteAsync(""); } }; break; } } { var sendData = Encoding.UTF8.GetBytes(outPut); await webSocketFromGameHandler.SendAsync(new ArraySegment <byte>(sendData, 0, sendData.Length), WebSocketMessageType.Text, true, CancellationToken.None); } } while (!Wrr.CloseStatus.HasValue); }
private static async Task <string> DealWith(string notifyJson) { Console.WriteLine($"notify receive:{notifyJson}"); //File.AppendAllText("log/d.txt", $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}-{notifyJson}{Environment.NewLine}"); //File.AppendText("",) // CommonClass.TeamCreateFinish teamCreateFinish = Newtonsoft.Json.JsonConvert.DeserializeObject<CommonClass.TeamCreateFinish>(notifyJson); string outPut = "haveNothingToReturn"; { CommonClass.Command c = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.Command>(notifyJson); switch (c.c) { case "TeamCreate": { CommonClass.TeamCreate teamCreate = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.TeamCreate>(notifyJson); int indexV; lock (Program.teamLock) { int maxValue = 10; //int indexV; do { indexV = Program.rm.Next(0, maxValue); maxValue *= 2; } while (Program.allTeams.ContainsKey(indexV)); Program.allTeams.Add(indexV, new Team() { captain = teamCreate, CreateTime = DateTime.Now, TeamID = indexV, member = new List <CommonClass.TeamJoin>(), IsBegun = false }); //Program.allTeams.Add() } var teamCreateFinish = new CommonClass.TeamCreateFinish() { c = "TeamCreateFinish", CommandStart = teamCreate.CommandStart, TeamNum = indexV, WebSocketID = teamCreate.WebSocketID, PlayerName = teamCreate.PlayerName }; await sendMsg(teamCreate.FromUrl, Newtonsoft.Json.JsonConvert.SerializeObject(teamCreateFinish)); //await (prot) // await sendInmationToUrl(addItem.FromUrl, notifyJson); CommonClass.TeamResult t = new CommonClass.TeamResult() { c = "TeamResult", FromUrl = teamCreate.FromUrl, TeamNumber = indexV, WebSocketID = teamCreate.WebSocketID }; outPut = Newtonsoft.Json.JsonConvert.SerializeObject(t); }; break; case "TeamBegain": { CommonClass.TeamBegain teamBegain = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.TeamBegain>(notifyJson); Team t = null; lock (Program.teamLock) { if (Program.allTeams.ContainsKey(teamBegain.TeamNum)) { t = Program.allTeams[teamBegain.TeamNum]; } //Program.allTeams.Add() } if (t == null) { outPut = "ng"; } else { for (var i = 0; i < t.member.Count; i++) { var secret = CommonClass.AES.AesEncrypt("team:" + teamBegain.RoomIndex.ToString(), t.member[i].CommandStart); CommonClass.TeamNumWithSecret teamNumWithSecret = new CommonClass.TeamNumWithSecret() { c = "TeamNumWithSecret", WebSocketID = t.member[i].WebSocketID, Secret = secret }; var json = Newtonsoft.Json.JsonConvert.SerializeObject(teamNumWithSecret); await sendMsg(t.member[i].FromUrl, json); } t.IsBegun = true; outPut = "ok"; } }; break; case "TeamJoin": { /* * TeamJoin,如果传入的字段不是数字,返回 is not number * TeamJoin,如果传入的字段如果是数字,不存在这个队伍 返回not has the team * TeamJoin,如果队伍中人数已满,不存在这个队伍 返回team is full * */ CommonClass.TeamJoin teamJoin = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.TeamJoin>(notifyJson); int teamIndex; if (int.TryParse(teamJoin.TeamIndex, out teamIndex)) { bool memberIsFull = false; bool notHasTheTeam = false; Team t = null; lock (Program.teamLock) { if (Program.allTeams.ContainsKey(teamIndex)) { if (Program.allTeams[teamIndex].member.Count >= 4) { memberIsFull = true; } else { Program.allTeams[teamIndex].member.Add(teamJoin); t = Program.allTeams[teamIndex]; } } else { notHasTheTeam = true; } } if (memberIsFull) { outPut = "team is full"; // await context.Response.WriteAsync(""); } else if (notHasTheTeam) { outPut = "not has the team"; //await context.Response.WriteAsync(""); } else if (t.IsBegun) { outPut = "game has begun"; //t.IsBegun 必须在判断 notHasTheTeam 之后。否则t可能为null // await context.Response.WriteAsync(""); } else { var PlayerNames = new List <string>(); CommonClass.TeamJoinFinish teamJoinFinish = new CommonClass.TeamJoinFinish() { c = "TeamJoinFinish", PlayerNames = new List <string>(), TeamNum = t.TeamID, WebSocketID = teamJoin.WebSocketID // PlayerNames = }; { CommonClass.TeamJoinBroadInfo addInfomation = new CommonClass.TeamJoinBroadInfo() { c = "TeamJoinBroadInfo", PlayerName = teamJoin.PlayerName, WebSocketID = t.captain.WebSocketID }; await sendMsg(t.captain.FromUrl, Newtonsoft.Json.JsonConvert.SerializeObject(addInfomation)); } teamJoinFinish.PlayerNames.Add(t.captain.PlayerName); for (var i = 0; i < t.member.Count; i++) { teamJoinFinish.PlayerNames.Add(t.member[i].PlayerName); if (t.member[i].FromUrl == teamJoin.FromUrl && t.member[i].WebSocketID == teamJoin.WebSocketID) { } else { CommonClass.TeamJoinBroadInfo addInfomation = new CommonClass.TeamJoinBroadInfo() { c = "TeamJoinBroadInfo", PlayerName = teamJoin.PlayerName, WebSocketID = t.member[i].WebSocketID }; await sendMsg(t.member[i].FromUrl, Newtonsoft.Json.JsonConvert.SerializeObject(addInfomation)); } } await sendMsg(teamJoin.FromUrl, Newtonsoft.Json.JsonConvert.SerializeObject(teamJoinFinish)); outPut = "ok"; // await context.Response.WriteAsync("ok"); // t.captain. //await context.Response.WriteAsync("not has the team"); } } else { outPut = "is not number"; // await context.Response.WriteAsync(""); } }; break; } } { return(outPut); } }
private void findTeam(IApplicationBuilder app) { //app.Run(async context => //{ //}); app.Run(async context => { if (context.WebSockets.IsWebSocketRequest) { var webSocket = await context.WebSockets.AcceptWebSocketAsync(); await dealWithFindTeam(webSocket); } }); return; app.Run(async context => { if (context.Request.Method.ToLower() == "post") { // await context.Response.WriteAsync("ok"); var notifyJson = getBodyStr(context); Console.WriteLine($"findTeam receive:{notifyJson}"); CommonClass.Command c = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.Command>(notifyJson); switch (c.c) { case "TeamJoin": { /* * TeamJoin,如果传入的字段不是数字,返回 is not number * TeamJoin,如果传入的字段如果是数字,不存在这个队伍 返回not has the team * TeamJoin,如果队伍中人数已满,不存在这个队伍 返回team is full * */ CommonClass.TeamJoin teamJoin = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.TeamJoin>(notifyJson); int teamIndex; if (int.TryParse(teamJoin.TeamIndex, out teamIndex)) { bool memberIsFull = false; bool notHasTheTeam = false; Team t = null; lock (Program.teamLock) { if (Program.allTeams.ContainsKey(teamIndex)) { if (Program.allTeams[teamIndex].member.Count >= 4) { memberIsFull = true; } else { Program.allTeams[teamIndex].member.Add(teamJoin); t = Program.allTeams[teamIndex]; } } else { notHasTheTeam = true; } } if (memberIsFull) { await context.Response.WriteAsync("team is full"); } else if (notHasTheTeam) { await context.Response.WriteAsync("not has the team"); } else if (t.IsBegun) { //t.IsBegun 必须在判断 notHasTheTeam 之后。否则t可能为null await context.Response.WriteAsync("game has begun"); } else { var PlayerNames = new List <string>(); CommonClass.TeamJoinFinish teamJoinFinish = new CommonClass.TeamJoinFinish() { c = "TeamJoinFinish", PlayerNames = new List <string>(), TeamNum = t.TeamID, WebSocketID = teamJoin.WebSocketID // PlayerNames = }; { CommonClass.TeamJoinBroadInfo addInfomation = new CommonClass.TeamJoinBroadInfo() { c = "TeamJoinBroadInfo", PlayerName = teamJoin.PlayerName, WebSocketID = t.captain.WebSocketID }; await sendMsg(t.captain.FromUrl, Newtonsoft.Json.JsonConvert.SerializeObject(addInfomation)); } teamJoinFinish.PlayerNames.Add(t.captain.PlayerName); for (var i = 0; i < t.member.Count; i++) { teamJoinFinish.PlayerNames.Add(t.member[i].PlayerName); if (t.member[i].FromUrl == teamJoin.FromUrl && t.member[i].WebSocketID == teamJoin.WebSocketID) { } else { CommonClass.TeamJoinBroadInfo addInfomation = new CommonClass.TeamJoinBroadInfo() { c = "TeamJoinBroadInfo", PlayerName = teamJoin.PlayerName, WebSocketID = t.member[i].WebSocketID }; await sendMsg(t.member[i].FromUrl, Newtonsoft.Json.JsonConvert.SerializeObject(addInfomation)); } } await sendMsg(teamJoin.FromUrl, Newtonsoft.Json.JsonConvert.SerializeObject(teamJoinFinish)); await context.Response.WriteAsync("ok"); // t.captain. //await context.Response.WriteAsync("not has the team"); } } else { await context.Response.WriteAsync("is not number"); } }; break; } //switch (c.c) //{ // case "TeamCreate": // { // CommonClass.TeamCreate teamCreate = Newtonsoft.Json.JsonConvert.DeserializeObject<CommonClass.TeamCreate>(notifyJson); // int indexV; // lock (Program.teamLock) // { // int maxValue = 10; // //int indexV; // do // { // indexV = Program.rm.Next(0, maxValue); // maxValue *= 2; // } while (Program.allTeams.ContainsKey(indexV)); // Program.allTeams.Add(indexV, new Team() // { // captain = teamCreate, // CreateTime = DateTime.Now, // TeamID = indexV // }); // //Program.allTeams.Add() // } // var teamCreateFinish = new CommonClass.TeamCreateFinish() // { // c = "TeamCreateFinish", // CommandStart = teamCreate.CommandStart, // TeamNum = indexV, // WebSocketID = teamCreate.WebSocketID, // PlayerName = teamCreate.PlayerName // }; // await sendMsg(teamCreate.FromUrl, Newtonsoft.Json.JsonConvert.SerializeObject(teamCreateFinish)); // //await (prot) // // await sendInmationToUrl(addItem.FromUrl, notifyJson); // CommonClass.TeamResult t = new CommonClass.TeamResult() // { // c = "TeamResult", // FromUrl = teamCreate.FromUrl, // TeamNumber = indexV, // WebSocketID = teamCreate.WebSocketID // }; // await context.Response.WriteAsync(Newtonsoft.Json.JsonConvert.SerializeObject(t)); // }; break; //} } }); }
private async Task dealWithTeamBegain(WebSocket webSocketFromGameHandler) { WebSocketReceiveResult Wrr; do { var returnResult = await ReceiveStringAsync(webSocketFromGameHandler, 1024 * 1024 * 10); Wrr = returnResult.wr; //returnResult.wr; string outPut = "haveNothingToReturn"; { var notifyJson = returnResult.result; // var notifyJson = getBodyStr(context); Console.WriteLine($"createTeam receive:{notifyJson}"); CommonClass.Command c = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.Command>(notifyJson); switch (c.c) { case "TeamBegain": { CommonClass.TeamBegain teamBegain = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.TeamBegain>(notifyJson); Team t = null; lock (Program.teamLock) { if (Program.allTeams.ContainsKey(teamBegain.TeamNum)) { t = Program.allTeams[teamBegain.TeamNum]; } //Program.allTeams.Add() } if (t == null) { outPut = "ng"; } else { for (var i = 0; i < t.member.Count; i++) { var secret = CommonClass.AES.AesEncrypt("team:" + teamBegain.RoomIndex.ToString(), t.member[i].CommandStart); CommonClass.TeamNumWithSecret teamNumWithSecret = new CommonClass.TeamNumWithSecret() { c = "TeamNumWithSecret", WebSocketID = t.member[i].WebSocketID, Secret = secret }; var json = Newtonsoft.Json.JsonConvert.SerializeObject(teamNumWithSecret); await sendMsg(t.captain.FromUrl, json); } t.IsBegun = true; outPut = "ok"; } }; break; } } { var sendData = Encoding.UTF8.GetBytes(outPut); await webSocketFromGameHandler.SendAsync(new ArraySegment <byte>(sendData, 0, sendData.Length), WebSocketMessageType.Text, true, CancellationToken.None); } } while (!Wrr.CloseStatus.HasValue); }
private void teambegain(IApplicationBuilder app) { //app.Run(async context => //{ //}); app.Run(async context => { if (context.WebSockets.IsWebSocketRequest) { var webSocket = await context.WebSockets.AcceptWebSocketAsync(); await dealWithTeamBegain(webSocket); } }); return; app.Run(async context => { if (context.Request.Method.ToLower() == "post") { // await context.Response.WriteAsync("ok"); var notifyJson = getBodyStr(context); Console.WriteLine($"teambegain receive:{notifyJson}"); CommonClass.Command c = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.Command>(notifyJson); switch (c.c) { case "TeamBegain": { CommonClass.TeamBegain teamBegain = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.TeamBegain>(notifyJson); Team t = null; lock (Program.teamLock) { if (Program.allTeams.ContainsKey(teamBegain.TeamNum)) { t = Program.allTeams[teamBegain.TeamNum]; } //Program.allTeams.Add() } if (t == null) { await context.Response.WriteAsync("ng"); } else { for (var i = 0; i < t.member.Count; i++) { var secret = CommonClass.AES.AesEncrypt("team:" + teamBegain.RoomIndex.ToString(), t.member[i].CommandStart); CommonClass.TeamNumWithSecret teamNumWithSecret = new CommonClass.TeamNumWithSecret() { c = "TeamNumWithSecret", WebSocketID = t.member[i].WebSocketID, Secret = secret }; var json = Newtonsoft.Json.JsonConvert.SerializeObject(teamNumWithSecret); await sendMsg(t.captain.FromUrl, json); } t.IsBegun = true; await context.Response.WriteAsync("ok"); } }; break; } } }); }
private async Task dealWithCreateTeam(System.Net.WebSockets.WebSocket webSocketFromGameHandler) { WebSocketReceiveResult Wrr; do { var returnResult = await ReceiveStringAsync(webSocketFromGameHandler, 1024 * 1024 * 10); Wrr = returnResult.wr; //returnResult.wr; string outPut = "haveNothingToReturn"; { var notifyJson = returnResult.result; // var notifyJson = getBodyStr(context); Console.WriteLine($"createTeam receive:{notifyJson}"); CommonClass.Command c = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.Command>(notifyJson); switch (c.c) { case "TeamCreate": { CommonClass.TeamCreate teamCreate = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.TeamCreate>(notifyJson); int indexV; lock (Program.teamLock) { int maxValue = 10; //int indexV; do { indexV = Program.rm.Next(0, maxValue); maxValue *= 2; } while (Program.allTeams.ContainsKey(indexV)); Program.allTeams.Add(indexV, new Team() { captain = teamCreate, CreateTime = DateTime.Now, TeamID = indexV, member = new List <CommonClass.TeamJoin>(), IsBegun = false }); //Program.allTeams.Add() } var teamCreateFinish = new CommonClass.TeamCreateFinish() { c = "TeamCreateFinish", CommandStart = teamCreate.CommandStart, TeamNum = indexV, WebSocketID = teamCreate.WebSocketID, PlayerName = teamCreate.PlayerName }; await sendMsg(teamCreate.FromUrl, Newtonsoft.Json.JsonConvert.SerializeObject(teamCreateFinish)); //await (prot) // await sendInmationToUrl(addItem.FromUrl, notifyJson); CommonClass.TeamResult t = new CommonClass.TeamResult() { c = "TeamResult", FromUrl = teamCreate.FromUrl, TeamNumber = indexV, WebSocketID = teamCreate.WebSocketID }; outPut = Newtonsoft.Json.JsonConvert.SerializeObject(t); }; break; } } { var sendData = Encoding.UTF8.GetBytes(outPut); await webSocketFromGameHandler.SendAsync(new ArraySegment <byte>(sendData, 0, sendData.Length), WebSocketMessageType.Text, true, CancellationToken.None); } } while (!Wrr.CloseStatus.HasValue); }
private void createTeam(IApplicationBuilder app) { //app.Run(async context => //{ //}); app.Run(async context => { if (context.WebSockets.IsWebSocketRequest) { var webSocket = await context.WebSockets.AcceptWebSocketAsync(); await dealWithCreateTeam(webSocket); } }); return; app.Run(async context => { if (context.WebSockets.IsWebSocketRequest) { var webSocket = await context.WebSockets.AcceptWebSocketAsync(); await dealWithCreateTeam(webSocket); } }); return; app.Run(async context => { if (context.Request.Method.ToLower() == "post") { var notifyJson = getBodyStr(context); Console.WriteLine($"createTeam receive:{notifyJson}"); CommonClass.Command c = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.Command>(notifyJson); switch (c.c) { case "TeamCreate": { CommonClass.TeamCreate teamCreate = Newtonsoft.Json.JsonConvert.DeserializeObject <CommonClass.TeamCreate>(notifyJson); int indexV; lock (Program.teamLock) { int maxValue = 10; //int indexV; do { indexV = Program.rm.Next(0, maxValue); maxValue *= 2; } while (Program.allTeams.ContainsKey(indexV)); Program.allTeams.Add(indexV, new Team() { captain = teamCreate, CreateTime = DateTime.Now, TeamID = indexV, member = new List <CommonClass.TeamJoin>(), IsBegun = false }); //Program.allTeams.Add() } var teamCreateFinish = new CommonClass.TeamCreateFinish() { c = "TeamCreateFinish", CommandStart = teamCreate.CommandStart, TeamNum = indexV, WebSocketID = teamCreate.WebSocketID, PlayerName = teamCreate.PlayerName }; await sendMsg(teamCreate.FromUrl, Newtonsoft.Json.JsonConvert.SerializeObject(teamCreateFinish)); //await (prot) // await sendInmationToUrl(addItem.FromUrl, notifyJson); CommonClass.TeamResult t = new CommonClass.TeamResult() { c = "TeamResult", FromUrl = teamCreate.FromUrl, TeamNumber = indexV, WebSocketID = teamCreate.WebSocketID }; await context.Response.WriteAsync(Newtonsoft.Json.JsonConvert.SerializeObject(t)); }; break; } } }); }