public GameResult ServerUpdate() { using (MemoryStream ms = new MemoryStream()) { using (BinaryWriter bw = new BinaryWriter(ms)) { bw.Write(PackageTag.S2C_UPDATE); bw.Write(main.tick); if (main.tick % CookConst.REQUIRE_PRODUCE_TIME == 0) { ushort randomSeed = (ushort)random.Next(ushort.MaxValue); bw.Write(randomSeed); main.SetSeed(randomSeed); } main.Update(); bw.Write((ushort)commandList.Count); for (int i = 0; i < commandList.Count; i++) { ICommand command = commandList[i]; command.ToBytes(bw); if (command is CommandChangeWorkerPos) { main.GetCommandChangeWorkerPos((CommandChangeWorkerPos)command); } else if (command is CommandChangeResultPos) { main.GetCommandChangeResultPos((CommandChangeResultPos)command); } else if (command is CommandCompleteDish) { main.GetCommandCompleteDish((CommandCompleteDish)command); } else { main.GetCommandCompleteRequirement((CommandCompleteRequirement)command); } } if (commandList.Count > 0) { } commandList.Clear(); serverSendDataCallBack(true, true, ms); serverSendDataCallBack(false, true, ms); GameResult gameResult; if (tick > CookConst.MAX_TIME * CookConst.TICK_NUM_PER_SECOND) { gameResult = main.GetGameResult(); } else { gameResult = GameResult.NOT_OVER; } return(gameResult); } } }
private void Update(BinaryReader _br) { ushort tick = _br.ReadUInt16(); if (tick != main.tick) { throw new Exception("tick is not match! client:" + main.tick + " server:" + tick); } if (tick % CookConst.REQUIRE_PRODUCE_TIME == 0) { ushort randomSeed = _br.ReadUInt16(); main.SetSeed(randomSeed); } main.Update(); ushort num = _br.ReadUInt16(); for (int i = 0; i < num; i++) { CommandType commandType = (CommandType)_br.ReadByte(); switch (commandType) { case CommandType.CHANGE_RESULT_POS: CommandChangeResultPos commandChangeResultPos = new CommandChangeResultPos(); commandChangeResultPos.FromBytes(_br); main.GetCommandChangeResultPos(commandChangeResultPos); break; case CommandType.CHANGE_WORKER_POS: CommandChangeWorkerPos commandChangeWorkerPos = new CommandChangeWorkerPos(); commandChangeWorkerPos.FromBytes(_br); main.GetCommandChangeWorkerPos(commandChangeWorkerPos); break; case CommandType.COMPLETE_DISH: CommandCompleteDish commandCompleteDish = new CommandCompleteDish(); commandCompleteDish.FromBytes(_br); main.GetCommandCompleteDish(commandCompleteDish); break; default: CommandCompleteRequirement commandCompleteRequirement = new CommandCompleteRequirement(); commandCompleteRequirement.FromBytes(_br); main.GetCommandCompleteRequirement(commandCompleteRequirement); break; } } if (num > 0) { CheckSync(); } GameResult gameResult; if (main.tick > CookConst.MAX_TIME * CookConst.TICK_NUM_PER_SECOND) { gameResult = main.GetGameResult(); } else { gameResult = GameResult.NOT_OVER; } client.UpdateCallBack(gameResult); }