Profile Answers

-Add Ability For Profile Answers To Save
-Add WoJS response for ``GetQuestions``
-Add ``Pain`` Mood To Question Data
This commit is contained in:
Alan Moon 2023-12-13 19:45:47 -08:00 committed by Robert Paciorek
parent 1f190dd19a
commit cc802043ec
8 changed files with 2568 additions and 45 deletions

View File

@ -11,9 +11,11 @@ public class ProfileController : Controller {
private readonly DBContext ctx;
private AchievementService achievementService;
public ProfileController(DBContext ctx, AchievementService achievementService) {
private ProfileService profileService;
public ProfileController(DBContext ctx, AchievementService achievementService, ProfileService profileService) {
this.ctx = ctx;
this.achievementService = achievementService;
this.profileService = profileService;
}
[HttpPost]
@ -55,49 +57,61 @@ public class ProfileController : Controller {
}
[HttpPost]
[Produces("application/xml")]
// [Produces("application/xml")]
[Route("ProfileWebService.asmx/GetQuestions")]
public IActionResult GetQuestions() {
return Ok(new ProfileQuestionData {
Lists = new ProfileQuestionList[] {
new ProfileQuestionList {
ID = 4,
Questions = new ProfileQuestion[] {
new ProfileQuestion {
CategoryID = 3,
IsActive = "true", // this is a string, which makes me sad
Locale = "en-US",
Ordinal = 1,
ID = 48,
DisplayText = "How Did You Hear About US ?",
Answers = new ProfileAnswer[] {
new ProfileAnswer {
ID = 320,
DisplayText = "TV Commercial",
Locale = "en-US",
Ordinal = 1,
QuestionID = 48
},
new ProfileAnswer {
ID = 324,
DisplayText = "I bought the RIders Of Berk DVD",
Locale = "en-US",
Ordinal = 5,
QuestionID = 48
},
new ProfileAnswer {
ID = 325,
DisplayText = "I bought the Defenders of Berk DVD",
Locale = "en-US",
Ordinal = 6,
QuestionID = 48
return Ok(XmlUtil.ReadResourceXmlString("questiondata"));
//return Ok(new ProfileQuestionData {
// Lists = new ProfileQuestionList[] {
// new ProfileQuestionList {
// ID = 4,
// Questions = new ProfileQuestion[] {
// new ProfileQuestion {
// CategoryID = 3,
// IsActive = "true", // this is a string, which makes me sad
// Locale = "en-US",
// Ordinal = 1,
// ID = 48,
// DisplayText = "How Did You Hear About US ?",
// Answers = new ProfileAnswer[] {
// new ProfileAnswer {
// ID = 320,
// DisplayText = "TV Commercial",
// Locale = "en-US",
// Ordinal = 1,
// QuestionID = 48
// },
// new ProfileAnswer {
// ID = 324,
// DisplayText = "I bought the RIders Of Berk DVD",
// Locale = "en-US",
// Ordinal = 5,
// QuestionID = 48
// },
// new ProfileAnswer {
// ID = 325,
// DisplayText = "I bought the Defenders of Berk DVD",
// Locale = "en-US",
// Ordinal = 6,
// QuestionID = 48
// }
// }
// }
// }
// }
// }
// });
}
}
}
}
}
}
});
[HttpPost]
[Produces("application/xml")]
[Route("ProfileWebService.asmx/SetUserProfileAnswers")]
[VikingSession]
public IActionResult SetUserProfileAnswers(Viking viking, [FromForm] int profileAnswerIDs)
{
ProfileQuestion questionFromaId = profileService.GetQuestionFromAnswerId(profileAnswerIDs);
return Ok(profileService.SetAnswer(viking, questionFromaId.ID, profileAnswerIDs));
}
[HttpPost]
@ -141,7 +155,7 @@ public class ProfileController : Controller {
FirstName = viking.Name,
MultiplayerEnabled = ClientVersion.IsMultiplayerSupported(apiKey),
Locale = "en-US", // placeholder
GenderID = Gender.Male, // placeholder
GenderID = viking.Gender,
OpenChatEnabled = true,
IsApproved = true,
RegistrationDate = new DateTime(DateTime.Now.Ticks), // placeholder
@ -174,7 +188,7 @@ public class ProfileController : Controller {
AvatarInfo = avatar,
AchievementCount = 0,
MythieCount = 0,
AnswerData = new UserAnswerData { UserID = viking.Uid.ToString() },
AnswerData = new UserAnswerData { UserID = viking.Uid.ToString(), Answers = profileService.GetUserAnswers(viking)},
GameCurrency = currency.GameCurrency,
CashCurrency = currency.CashCurrency,
ActivityCount = 0,

View File

@ -19,6 +19,7 @@ public class DBContext : DbContext {
public DbSet<GameData> GameData { get; set; } = null!;
public DbSet<GameDataPair> GameDataPairs { get; set; } = null!;
public DbSet<AchievementPoints> AchievementPoints { get; set; } = null!;
public DbSet<ProfileAnswer> ProfileAnswers { get; set; } = null!;
private readonly IOptions<ApiServerConfig> config;
public DBContext(IOptions<ApiServerConfig> config) {
@ -199,5 +200,9 @@ public class DBContext : DbContext {
.HasOne(e => e.Viking)
.WithMany(e => e.AchievementPoints)
.HasForeignKey(e => e.VikingId);
builder.Entity<ProfileAnswer>().HasOne(i => i.Viking)
.WithMany(i => i.ProfileAnswers)
.HasForeignKey(e => e.VikingId);
}
}

View File

@ -0,0 +1,15 @@
using System.ComponentModel.DataAnnotations;
namespace sodoff.Model
{
public class ProfileAnswer
{
[Key]
public int Id { get; set; }
public int VikingId { get; set; }
public int QuestionID { get; set; }
public int AnswerID { get; set; }
public virtual Viking Viking { get; set; } = null!;
}
}

View File

@ -1,4 +1,4 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
namespace sodoff.Model;
@ -31,5 +31,6 @@ public class Viking {
public virtual ICollection<PairData> PairData { get; set; } = null!;
public virtual ICollection<InventoryItem> InventoryItems { get; set; } = null!;
public virtual ICollection<GameData> GameData { get; set; } = null!;
public virtual ICollection<ProfileAnswer> ProfileAnswers { get; set; } = null!;
public virtual Dragon? SelectedDragon { get; set; }
}

View File

@ -33,6 +33,7 @@ builder.Services.AddScoped<RoomService>();
builder.Services.AddScoped<InventoryService>();
builder.Services.AddScoped<AchievementService>();
builder.Services.AddScoped<GameDataService>();
builder.Services.AddScoped<ProfileService>();
bool assetServer = builder.Configuration.GetSection("AssetServer").GetValue<bool>("Enabled");
string assetIP = builder.Configuration.GetSection("AssetServer").GetValue<string>("ListenIP");

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,109 @@
using sodoff.Model;
using sodoff.Schema;
using sodoff.Util;
namespace sodoff.Services
{
public class ProfileService
{
private readonly DBContext ctx;
public ProfileService(DBContext ctx)
{
this.ctx = ctx;
}
public bool SetAnswer(Viking viking, int qId, int aId)
{
// check if answer is in the database already, edit it with new answer id if it does
Model.ProfileAnswer? existingAnswer = viking.ProfileAnswers.FirstOrDefault(e => e.QuestionID == qId);
if(existingAnswer != null)
{
existingAnswer.AnswerID = aId;
ctx.SaveChanges();
return true;
}
// create an answer and store it in database
Model.ProfileAnswer answer = new Model.ProfileAnswer
{
VikingId = viking.Id,
AnswerID = aId,
QuestionID = qId,
};
viking.ProfileAnswers.Add(answer);
ctx.SaveChanges();
return true;
}
public ProfileUserAnswer[] GetUserAnswers(Viking viking)
{
// create a profile user answer based on each answer on viking
List<ProfileUserAnswer> userAnswers = new List<ProfileUserAnswer>();
foreach(Model.ProfileAnswer answer in viking.ProfileAnswers)
{
ProfileUserAnswer userAnswer = new ProfileUserAnswer
{
AnswerID = answer.AnswerID,
QuestionID = answer.QuestionID
};
userAnswers.Add(userAnswer);
}
return userAnswers.ToArray();
}
public ProfileUserAnswer GetUserAnswerFromQuestionId(Viking viking, int qId)
{
// check if answer exists
Model.ProfileAnswer profileAnswer = viking.ProfileAnswers.FirstOrDefault(e => e.QuestionID == qId);
if(profileAnswer != null)
{
ProfileUserAnswer profileUserAnswer = new ProfileUserAnswer
{
QuestionID = profileAnswer.QuestionID,
AnswerID = profileAnswer.AnswerID
};
return profileUserAnswer;
}
return null;
}
public ProfileQuestion GetQuestionFromAnswerId(int aId)
{
ProfileQuestionData questionData = XmlUtil.DeserializeXml<ProfileQuestionData>(XmlUtil.ReadResourceXmlString("questiondata"));
List<Schema.ProfileAnswer> allAnswersFromData = new List<Schema.ProfileAnswer>();
List<ProfileQuestion> allQuestionsFromData = new List<ProfileQuestion>();
foreach(var list in questionData.Lists)
{
foreach(var question in list.Questions)
{
allQuestionsFromData.Add(question);
foreach(var answer in question.Answers)
{
allAnswersFromData.Add(answer);
}
}
}
Schema.ProfileAnswer profileAnswer = allAnswersFromData.FirstOrDefault(e => e.ID == aId);
if (profileAnswer != null)
{
ProfileQuestion questionFromAnswer = allQuestionsFromData.FirstOrDefault(e => e.ID == profileAnswer.QuestionID);
if (questionFromAnswer != null) return questionFromAnswer;
else return null!;
}
return null!;
}
}
}

View File

@ -50,6 +50,7 @@
<None Remove="Resources\defaultmissionlist.xml" />
<None Remove="Resources\defaultmissionlistv1.xml" />
<None Remove="Resources\defaultmissionlistmam.xml" />
<None Remove="Resources\questiondata.xml" />
</ItemGroup>
<ItemGroup>
<None Update="Resources\childlist.xml">
@ -117,5 +118,8 @@
<EmbeddedResource Include="Resources\defaultmissionlistmam.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="Resources\questiondata.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedResource>
</ItemGroup>
</Project>