我正在使用 Visual Studio 2008 学习 ASP.NET 3.5 和 C#。我学到的大部分东西都是通过 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;
}
}
谢谢!