c# - 如何在 C# ASP.Net 网站中通过浏览器游戏分配玩家名称?

标签 c# asp.net forms browser

您好,如何通过浏览器分配名称。

想象一下,您尝试在浏览器文本框中注册一个名称,以便加入游戏,并将您的名称作为 cookie 保存在浏览器中。可以有多个玩家可以签名,但游戏只有两个玩家。我如何设置访问我的网站的玩家名称,在保存到 cookie 的文本框中输入他的名字,然后他按加入按钮向主机发送请求以寻找游戏的其他玩家.

我的问题是,由于玩家是两个玩家,实际上是井字游戏,在我将他们选择的名称设置为变量字符串后,如何将他们的名称分配给游戏中的物理玩家 1 和玩家 2,因为一名玩家先行,一名玩家后行。

我认为谁开始发送寻找另一个玩家的请求就成为第一个玩家(X),所以谁回答这个请求,就会成为第二个玩家,即 O 玩家。这可以吗?

我正在运行 Visual Studios '08,使用 ASP.NET 网站表单。因此通常使用文本框、按钮、用于数据的 MapPathing Stream Reader 和 cookie。

最佳答案

这需要查看很多内容,但应该为您提供一个非常好的起点:

ASPX:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MultipleUsers.aspx.cs" Inherits="WebApplicationCS2.MultipleUsers" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager runat="server" ID="ScriptManager1"></asp:ScriptManager>
        <div style="float:left; border:1px solid gray; min-height:200px; width:40%;">
            <asp:Timer runat="server" ID="tmrUsersList" Interval="1000"></asp:Timer>
            <asp:UpdatePanel runat="server" ID="pnlAjaxUserList">
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="tmrUsersList" />
                </Triggers>
                <ContentTemplate>
                    <table style="width:100%;">
                        <asp:Repeater runat="server" ID="rptUsersList">
                            <ItemTemplate>
                                <tr>
                                    <td>
                                        <asp:Label runat="server" ID="lblUserName" Text='<%# Eval("UserName")%>' />
                                    </td>
                                    <td>
                                        <asp:LinkButton runat="server" ID="lnkChallenge" Enabled='<%# Eval("CanChallenge")%>' 
                                            Text="Challenge" CommandArgument='<%# Eval("UserName")%>'
                                            OnCommand="lnkChallenge_Command" />
                                    </td>
                                </tr>
                            </ItemTemplate>
                        </asp:Repeater>
                    </table>
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>
        <div style="float:left;">
            Choose username:
            <asp:TextBox runat="server" ID="txtChosenName"></asp:TextBox>
            <asp:Button runat="server" ID="btnSignin" Text="Sign In" 
                OnClick="btnSignin_Click" />
            <br />
            <asp:Label runat="server" ID="lblMessage" ForeColor="Red" Visible="false" EnableViewState="false"></asp:Label>
        </div>
    </div>
    </form>
</body>
</html>

隐藏代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplicationCS2
{
    public partial class MultipleUsers : System.Web.UI.Page
    {
        private const string UserNameKey = "SitePlayer_UserName";
        private string CurrentUserName;
        private List<GameUser> lGameUsers;

        protected void Page_Load(object sender, EventArgs e)
        {
            HttpCookie playerCookie = Request.Cookies[UserNameKey];
            CurrentUserName = (playerCookie != null ? playerCookie.Value : null);
            if (CurrentUserName != null)
            {
                // Update cache to indicate user is still online.
                Cache.Add(UserNameKey + CurrentUserName, CurrentUserName, null,
                          System.Web.Caching.Cache.NoAbsoluteExpiration,
                          TimeSpan.FromMinutes(1), System.Web.Caching.CacheItemPriority.Normal, null);
            }

            lGameUsers = GetUserList();
            rptUsersList.DataSource = lGameUsers;
            rptUsersList.DataBind();
        }

        protected void btnSignin_Click(object sender, EventArgs e)
        {
            string chosenName = txtChosenName.Text.Trim();
            foreach (GameUser u in lGameUsers)
            {
                if (string.Compare(chosenName, u.UserName, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    lblMessage.Text = "Username already in use.";
                    lblMessage.Visible = true;
                    return;
                }
            }
            Cache.Add(UserNameKey + chosenName, chosenName, null,
                      System.Web.Caching.Cache.NoAbsoluteExpiration, 
                      TimeSpan.FromMinutes(1), System.Web.Caching.CacheItemPriority.Normal, null);
            Response.AppendCookie(new HttpCookie(UserNameKey, chosenName));
        }

        protected void lnkChallenge_Command(object sender, CommandEventArgs e)
        {

        }

        class GameUser
        {
            public string UserName { get; set; }
            public bool CanChallenge { get; set; }
        }
        private List<GameUser> GetUserList()
        {
            List<GameUser> userList = new List<GameUser>();
            foreach (System.Collections.DictionaryEntry cacheItem in Cache)
            {
                if (cacheItem.Key.ToString().StartsWith(UserNameKey))
                {
                    string name = cacheItem.Value.ToString();
                    if (string.Compare(CurrentUserName, name, StringComparison.OrdinalIgnoreCase) != 0)
                    {
                        GameUser u = new GameUser() 
                        {
                            UserName = name,
                            CanChallenge = (CurrentUserName != null)
                        };
                        userList.Add(u);
                    }
                }
            }
            return userList;
        }
    }
}

要查看正在运行的代码,请运行网站,浏览到表单,然后使用两种不同的浏览器登录。

您需要执行的步骤包括验证用户仅登录一次、连接“挑战”链接按钮等。

关于c# - 如何在 C# ASP.Net 网站中通过浏览器游戏分配玩家名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10099753/

相关文章:

c# - 如何在没有编译时常量的情况下设置可选参数

asp.net - web.config 错误 "location"

c# - 如何将数据绑定(bind)到 HTML 选择框

javascript - 如何将 HTML 表单数据添加到 mailto 链接

php - 为循环选择的选项值

php - Firefox 上传表单问题

c# - 服务器的图像文件路径不是 C : drive MVC5

c# - 如何从日期时间中提取时间

c# - 运行多个 C# 任务异步

c# - Html.OpenIdSelectorScripts 辅助方法抛出 NullReferenceException