我正在学习ASP.NET 3.5和C#,使用Visual Studio2008。我学到的大部分东西都是通过MSDN完成的。我正在尝试开发一个网页,将允许用户创建一个角色在RPG游戏中使用。用户应该能够分配属性,购买项目等。当用户完成后,网站将格式化一个可打印的字符表与用户的数据。
现在,我仍然在考虑这些东西,我想知道我是否在正确的轨道上-如果有人想看看我到目前为止所拥有的东西,并发表评论,那将是非常棒的。我感兴趣的是我正在做的任何错误或低效的事情,糟糕的设计,糟糕的代码,以及我可以改进的方法。但最重要的是,我只想知道我是否在正确的轨道上,没有滥用技术。
下面是我到目前为止拥有的代码。它允许用户将一定数量的点数分配给4个统计数据。
主页:
<%@ Page Language="C#"
AutoEventWireup="true"
CodeFile="Default.aspx.cs"
Inherits="_Default"
enableSessionState="true"
%>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<!-- MPCP/Bod/Evasion/Masking/Sensor -->
<asp:Label ID="MPCP_Rating" runat="server" Text="" ></asp:Label>
<br /> <br />
<asp:Label ID="PersonaPool" runat="server" Text="" ></asp:Label>
<br /> <br />
<!-- TODO: Format into table -->
Bod:
<asp:TextBox ID="Bod" runat="server" ontextchanged="Bod_TextChanged"
width="25px">0</asp:TextBox>
<asp:Button ID="BodInc" runat="server" Text="+"
OnClick="Bod_Inc" />
<asp:Button ID="BodDec" runat="server" Text="-"
OnClick="Bod_Dec"/>
<br /> <br />
Evasion:
<asp:TextBox ID="Evasion" runat="server" ontextchanged="Evasion_TextChanged"
width="25px">0</asp:TextBox>
<asp:Button ID="EvasionInc" runat="server" Text="+"
OnClick="Evasion_Inc" />
<asp:Button ID="EvasionDec" runat="server" Text="-"
OnClick="Evasion_Dec" />
<br /> <br />
Masking:
<asp:TextBox ID="Masking" runat="server" ontextchanged="Masking_TextChanged"
width="25px">0</asp:TextBox>
<asp:Button ID="MaskingInc" runat="server" Text="+"
OnClick="Masking_Inc" />
<asp:Button ID="MaskingDec" runat="server" Text="-"
OnClick="Masking_Dec" />
<br /> <br />
Sensor:
<asp:TextBox ID="Sensor" runat="server" ontextchanged="Sensor_TextChanged"
width="25px">0</asp:TextBox>
<asp:Button ID="SensorInc" runat="server" Text="+"
OnClick="Sensor_Inc" />
<asp:Button ID="SensorDec" runat="server" Text="-"
OnClick="Sensor_Dec" />
<br /> <br />
<asp:Button ID="Submit" runat="server" Text="Submit" />
</div>
</form>
</body>
</html>代码隐藏:
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
private DeckData deck;
public _Default()
{
}
// Page events
protected void Page_Load(object sender, EventArgs e)
{
deck = (DeckData)(System.Web.HttpContext.Current.Session["Deck"]);
MPCP_Rating.Text = "MPCP Rating: " + deck.MPCP.ToString();
UpdateAvailPersona();
}
protected void Unload(object sender, EventArgs e)
{
}
// Helper functions
protected void ChangeAttribute(DeckData.Attributes atr, bool inc)
{
if (inc == true)
deck.IncAttribute(atr);
else
deck.DecAttribute(atr);
UpdateAvailPersona();
switch (atr)
{
case DeckData.Attributes.Bod:
Bod.Text = deck.Bod.ToString();
break;
case DeckData.Attributes.Evasion:
Evasion.Text = deck.Evasion.ToString();
break;
case DeckData.Attributes.Masking:
Masking.Text = deck.Masking.ToString();
break;
case DeckData.Attributes.Sensor:
Sensor.Text = deck.Sensor.ToString();
break;
}
}
protected void UpdateAvailPersona()
{
PersonaPool.Text = "Persona Pool: " + deck.PersonaMax.ToString() +
" / " + (deck.CalculateAvailPersona()).ToString();
}
// Control Events
protected void Bod_Dec(object sender, EventArgs e)
{
ChangeAttribute(DeckData.Attributes.Bod, false);
}
protected void Bod_Inc(object sender, EventArgs e)
{
ChangeAttribute(DeckData.Attributes.Bod, true);
}
protected void Evasion_Dec(object sender, EventArgs e)
{
ChangeAttribute(DeckData.Attributes.Evasion, false);
}
protected void Evasion_Inc(object sender, EventArgs e)
{
ChangeAttribute(DeckData.Attributes.Evasion, true);
}
protected void Masking_Dec(object sender, EventArgs e)
{
ChangeAttribute(DeckData.Attributes.Masking, false);
}
protected void Masking_Inc(object sender, EventArgs e)
{
ChangeAttribute(DeckData.Attributes.Masking, true);
}
protected void Sensor_Dec(object sender, EventArgs e)
{
ChangeAttribute(DeckData.Attributes.Sensor, false);
}
protected void Sensor_Inc(object sender, EventArgs e)
{
ChangeAttribute(DeckData.Attributes.Sensor, true);
}App-Data (只有DeckData类)
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
/// <summary>
/// Store deck related data and provide methods for adjusting deck data
/// </summary>
public class DeckData
{
// Set persona multiplier, determines max persona pool
private const uint _PersonaMultiplier = 3;
public DeckData(uint MPCP)
{
_MPCP = MPCP;
_Bod = _Evasion = _Masking = _Sensor = 0;
CalculateMaxPersona();
}
// MPCP/Bod/Evasion/Masking/Sensor
public enum Attributes
{
MPCP,
Bod,
Evasion,
Masking,
Sensor
}
private uint _MPCP;
private uint _Bod;
private uint _Evasion;
private uint _Masking;
private uint _Sensor;
private uint _PersonaMax;
/// <summary>
/// Acessor/Modifiers
/// </summary>
public uint MPCP
{
get { return _MPCP; }
set { _MPCP = value; }
}
public uint Bod
{
get { return _Bod; }
set { _Bod = value; }
}
public uint Evasion
{
get { return _Evasion; }
set { _Evasion = value; }
}
public uint Masking
{
get { return _Masking; }
set { _Masking = value; }
}
public uint Sensor
{
get { return _Sensor; }
set { _Sensor = value; }
}
public uint PersonaMax
{
get { return _PersonaMax; }
}
/// <summary>
/// Calculate available persona. Must be called before changing attribs to ensure
/// persona pool remains valid
/// </summary>
/// <returns></returns>
public uint CalculateAvailPersona()
{
// Total deck attribs
uint attrTotal = _Bod + _Evasion + _Masking + _Sensor;
return _PersonaMax - attrTotal;
}
/// <summary>
/// Recalculate max persona
/// </summary>
private uint CalculateMaxPersona()
{
_PersonaMax = _MPCP * _PersonaMultiplier;
return _PersonaMax;
}
/// <summary>
/// Increment attribute by 1 point
/// </summary>
/// <param name="atr">
/// The attribute to increment
/// </param>
/// <returns>
/// false if no Persona available
/// true if attribute successfully incremented
/// </returns>
public bool DecAttribute(DeckData.Attributes atr)
{
uint availPersona = CalculateAvailPersona();
if (availPersona == _PersonaMax)
return false;
switch (atr)
{
case Attributes.MPCP:
break;
case Attributes.Bod:
if (_Bod > 0) // Check for underflow
_Bod -= 1;
break;
case Attributes.Evasion:
if (_Evasion > 0)
_Evasion -= 1;
break;
case Attributes.Masking:
if (_Masking > 0)
_Masking -= 1;
break;
case Attributes.Sensor:
if (Sensor > 0)
_Sensor -= 1;
break;
}
// Check to see if we updated an attribute using cached persona
if(availPersona != CalculateAvailPersona())
return true;
return false;
}
public bool IncAttribute(DeckData.Attributes atr)
{
uint availPersona = CalculateAvailPersona();
if (availPersona == 0)
return false;
switch (atr)
{
case Attributes.MPCP:
break;
case Attributes.Bod:
_Bod += 1;
break;
case Attributes.Evasion:
_Evasion += 1;
break;
case Attributes.Masking:
_Masking += 1;
break;
case Attributes.Sensor:
_Sensor += 1;
break;
}
return true;
}
}谢谢!
发布于 2009-02-25 12:29:34
如果你擅长html,那就放弃webforms,使用像asp.net mvc或fubu mvc这样的mvc框架。
如果你不擅长使用html,那就学习html,放弃webforms,使用mvc框架。
发布于 2009-02-25 12:33:55
我只是快速浏览了一下你的代码,有一件事确实突显出来了,那就是使用你的web应用程序所涉及的服务器回发的数量。属性的每一次更改都会导致页面被提交回服务器,虽然在技术上是可以的,但这并不能带来很好的用户体验。
您可能希望研究允许用户进行所有修改然后一起提交它们的技术(例如,在客户端使用javascript维护属性点的分布),或者考虑使用AJAX将修改异步回发到服务器,从而获得更流畅的用户体验。
编辑:对于您正在使用的模型,您可以使用Button controls Command事件而不是Click事件。这将允许您为每个按钮分配一个CommandName和CommandArgument值,这些值可以在后面的代码中恢复。这将允许您只为每个按钮使用一个事件方法,该方法可以从这些属性中决定要更改哪个属性以及如何更改:
<asp:Button ID="BodInc" runat="server" CommandArgument="Increase"
CommandName="Bod" oncommand="AttributeButton_Command" Text="+" />
<asp:Button ID="BodDec" runat="server" CommandArgument="Increase"
CommandName="Bod" oncommand="AttributeButton_Command" Text="-" />代码隐藏:
protected void AttributeButton_Command(object sender, CommandEventArgs e)
{
string attriubuteName = e.CommandName;
string action = e.CommandArgument;
// Do stuff
}发布于 2009-02-25 12:51:07
我的一个建议是使用javascript在标记中递增/递减计数器,然后在表单提交时使用文本框的值对模型进行更新。正如@Andy建议的那样,你也可以通过AJAX进行更新,以减少可见的UI闪烁,但考虑到你拥有的简单规则,我认为在客户端进行更新并发布一次是可行的。
当然,您需要更新代码来验证是否没有超过最大值。禁止用户直接输入将省去验证是否只输入数值的麻烦。
Bod + [0] =
Evasion + [0] -
Masking + [0] -
Sensor + [0] -
Total 0
Maximum ?您还需要考虑使用CSS类设置样式,而不是直接在标记中指定宽度。使用CSS将帮助您在整个应用程序中保持一致的外观和感觉,和允许您稍后通过(在大多数情况下)对CSS而不是应用程序代码本身的更改来快速轻松地更改外观和感觉。
https://stackoverflow.com/questions/585765
复制相似问题