Table of Contents
回复内容:
Home Backend Development PHP Tutorial 文字播报型的自动战斗系统该如何保障不出现漏洞?

文字播报型的自动战斗系统该如何保障不出现漏洞?

Jun 06, 2016 pm 04:43 PM
php post socket

目前是想做一款纯文字类网页游戏,战斗模式采用文字播报模式
看到过一个别人写的是,本地进行战斗计算,最后把战斗结果post到服务器上,这种很容易造成漏洞,直接在本地修改结果不停的post
如果同步战斗状态的,感觉会太复杂,有没有方便的方法不容易出漏洞
我将php和c#用socket连接在一起,可以进行数据交换计算,可以利用这个做吗?

回复内容:

战斗过程中如果玩家不能介入的话,直接服务端算好整场战斗过程,一次性下发到客户端慢慢播文字就是了,你指的复杂是什么。

用socket的话,需要实现一套基于socket的游戏协议,好处是省流量、通信灵活,服务器主动推送信息容易。如果想简单点,可考虑直接用http短连接通信,但对于一些服务器主动下行的功能不好实现。 矮油,我觉得我应该很有资格回答这个问题,因为我做的游戏就是这样的。
看到楼主是用php,那么觉得我更有资格了。

首先分为两种情况。
1. 所有战斗都在服务器一次性完成,客户端只是简单的播报结果
这是最简单的,也是我现在用的方案,也是绝大多数古典页游用的方法。
客户端提交一个战斗请求,然后在服务器完全全部计算,将结果用json或者其他什么方法传给客户端,然后浏览器再用js解析出来。
注意:这种方法也不能完全的杜绝客户端作弊,最典型的例子,对于一些有消耗的战斗(无论是有冷却时间,还是入场需要消费资源),玩家开两个窗口然后同时点击战斗按钮,可能会出现双份奖励。我的游戏就曾经出现过这种bug。
此外,还存在我在后台改了数据没有刷新缓存导致前台用户玩的时候出现崩溃的问题。
总之这种方法难度不高,对服务器压力也小(因为是集中计算)。
缺点是无法响应玩家输入,也就是玩家并不能在战斗中进行控制,这就要看你的游戏是否要求玩家在战斗中进行操作了。

2.战斗会一边进行一边回传到客户端,响应客户端输入之后再传到服务器。
这种会比较难,其实这里麻烦的问题反而不是作弊,而是服务器负载……只要做好负载反而就没有什么问题。
方法有很多种,你说的socket是一种,但我不懂……(-_-|||)。
此外还有node.js系列的socket.io,更轻量,毕竟涉及C#就要涉及.net那种大而厚的东西,node.js相比之下更轻。
当然如果不选PHP的话,也有更多其他方案,比如纯asp.net或者纯java,甚至可以用现代的手段比如unity,当然这就扯远了……
所以如果要我来做的话,估计还是会选传统的js轮询ajax的办法,不断去服务器查。会更吃一点服务器资源(毕竟每次玩家操作都要走一次完整的http request),所以对于我们这种VPS放国外的屌丝可能会遭遇延迟问题。因此,我大概会把游戏做成延迟不影响游戏体验的模式。
举例就是做一个万智牌游戏,我点击出牌的时候,动画直接播放出牌效果(但此时还处于http request中),然后画面出现“电脑正在思考中”,然后播放电脑的出牌结果。
当然还有个办法就是你说的,把一些简单的东西放到客户端,但实际上这些内容必须还是要传到服务器在计算一次,最终回来的时候覆盖客户端算的结果,并且要保证从头到尾不使用客户端的数据。客户端计算的数据“仅仅是处于让玩家感受速度快”。
这点其实很常见,比如大型网站点赞,点赞的瞬间就+1了,其实只是客户端计算而已。刷新之后就会变成真实数据。
如果负载实在扛不住,可以考虑把游戏的一部分做成不消耗服务器的模式(例如你的游戏设定,在上面玩2小时可以获得100点游戏内战斗力,那么你就设置一个2小时的远征任务,这个任务挂好了之后就获得100点战斗力,又或者刷1天怪物平均可以获得3件装备,那么你可以设置一个副本,冷却时间24小时,必掉3件装备)。
总体上玩家的收益是相同的。
我倾向于把游戏做成不那么肝(也就是不需要反复算时间,一直在屏幕前点点点)。因此服务器负载倒不是那么大,512M1核的低配VPS,长期负载也就0.0x,所以还跑了几个其他业余利用资源……

有想到再补充

利益相关:RPG类型纯文字类网页游戏 莉诺达斯 - Linodas.com 制作人。
(PS:前几天打通了奥里与迷失森林,所以在首页放了一个全屏跳转推荐这款游戏,不要以为进错网站了。。 不要本地计算,用服务端计算后下发客户端就好。
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles