-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2682 from Legomenon-gh/mm-veto
Simple implementation of MM map veto
- Loading branch information
Showing
18 changed files
with
485 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace ZkData | ||
{ | ||
public class MapBanConfig | ||
{ | ||
private static Dictionary<int, int> BansPerPlayerByGameSize = new Dictionary<int, int>() { | ||
// 6 bans per player in a 2 player game, 3 bans per player in a 4 or 6 player game | ||
{ 2, 6 }, | ||
{ 4, 3 }, | ||
{ 6, 3 }, | ||
{ 8, 2 }, | ||
{ 10, 1 }, | ||
{ 12, 1 } | ||
}; | ||
|
||
public static int GetPlayerBanCount(int gameSize) | ||
{ | ||
int value; | ||
return BansPerPlayerByGameSize.TryGetValue(gameSize, out value) ? value : 1; | ||
} | ||
|
||
public static int GetMaxBanCount() | ||
{ | ||
return GetPlayerBanCount(2); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Web.Mvc; | ||
using ZkData; | ||
|
||
namespace ZeroKWeb.Controllers | ||
{ | ||
public class MapBansController : Controller | ||
{ | ||
[Auth] | ||
/// <summary> | ||
/// Returns current user's map bans and allows editing them. | ||
/// </summary> | ||
/// | ||
public ActionResult Index() | ||
{ | ||
var db = new ZkDataContext(); | ||
List<AccountMapBan> bans = db.AccountMapBans | ||
.Where(x => x.AccountID == Global.AccountID) | ||
.OrderBy(x => x.Rank) | ||
.ToList(); | ||
|
||
var viewModel = new MapBansViewModel | ||
{ | ||
accountBans = bans, | ||
maxBans = MapBanConfig.GetMaxBanCount() | ||
}; | ||
|
||
return View("MapBansIndex", viewModel); | ||
} | ||
|
||
[Auth] | ||
/// <summary> | ||
/// Overrides current user's map bans. | ||
/// </summary> | ||
/// | ||
|
||
public ActionResult Update(List<String> mapName) | ||
{ | ||
if (mapName == null) return Content("No input given"); | ||
|
||
// Duplicates would not break anything but they're probably a sign of user error so validate against them. | ||
var hasDuplicate = mapName.Where(x => x != "").GroupBy(x => x).Any(g => g.Count() > 1); | ||
if (hasDuplicate) | ||
{ | ||
return Content("The same map cannot be banned multiple times."); | ||
} | ||
|
||
// TODO: It would be nicer to use IDs instead of the map names to drive this | ||
// Fetch the actual resources to sanity check user input and silently ignore | ||
// any input that does not actually exist. | ||
// Filter against current matchmaker to remove any existing bans for a map | ||
// that has been removed from the MM pool. | ||
var db = new ZkDataContext(); | ||
var mapIDs = db.Resources | ||
.Where(x => x.TypeID == ResourceType.Map && mapName.Contains(x.InternalName) && x.MapSupportLevel == MapSupportLevel.MatchMaker) | ||
.ToList(); | ||
|
||
// Ban rank matters so sort the maps according to the user provided ordering | ||
mapIDs = mapIDs.OrderBy(x => mapName.IndexOf(x.InternalName)).ToList(); | ||
|
||
var newMapBans = new List<AccountMapBan>(); | ||
for (int i = 0; i < mapIDs.Count; i++) | ||
{ | ||
var ban = new AccountMapBan | ||
{ | ||
Rank = i + 1, | ||
AccountID = Global.AccountID, | ||
BannedMapResourceID = mapIDs[i].ResourceID | ||
}; | ||
newMapBans.Add(ban); | ||
} | ||
|
||
// Since users may only have a few bans and will not often update them, | ||
// recreate them from scratch on submission. This shortcut simplifies handling | ||
// partial updates that modify a ban's rank and handling multiple bans for the same map. | ||
db.AccountMapBans.RemoveRange(db.AccountMapBans.Where(x => x.AccountID == Global.AccountID)); | ||
db.AccountMapBans.InsertAllOnSubmit(newMapBans); | ||
db.SaveChanges(); | ||
|
||
return RedirectToAction("Index"); | ||
} | ||
} | ||
|
||
public class MapBansViewModel | ||
{ | ||
public List<AccountMapBan> accountBans; | ||
public int maxBans; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
@using ZeroKWeb.Controllers | ||
@model MapBansViewModel | ||
@{ | ||
Page.Title = "Map bans"; | ||
} | ||
|
||
<h2>Map bans</h2> | ||
|
||
<p>You may select up to @(Model.maxBans) maps to ban from your matchmaking games. | ||
Remove an existing ban by clearing the box and hitting Save. </p> | ||
|
||
<p> For 1v1 games, all your bans will be used. | ||
For team games, your first few unique bans will be used depending on the size of the game. </p> | ||
|
||
<form action="@Url.Action("Update")" method="post"> | ||
@for (int i = 0; i < Model.maxBans; i++) | ||
{ | ||
String mapName = i < Model.accountBans.Count ? Model.accountBans[i].Resource.InternalName : null; | ||
|
||
<div style="margin-bottom:10px"> | ||
<span style="padding-right:10px;">Ban @(i+1):</span> | ||
<span>@Html.TextBox("mapName", mapName, new { data_autocomplete = Url.Action("MatchMakerMaps", "Autocomplete")})</span> | ||
</div> | ||
} | ||
<input type="submit" value="Save Bans" /> | ||
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.ComponentModel.DataAnnotations.Schema; | ||
|
||
namespace ZkData | ||
{ | ||
public class AccountMapBan | ||
{ | ||
[Key] | ||
[Column(Order = 0)] | ||
[DatabaseGenerated(DatabaseGeneratedOption.None)] | ||
public int AccountID { get; set; } | ||
[Key] | ||
[Column(Order = 1)] | ||
[DatabaseGenerated(DatabaseGeneratedOption.None)] | ||
public int BannedMapResourceID { get; set; } | ||
[Key] | ||
[Column(Order = 2)] | ||
[DatabaseGenerated(DatabaseGeneratedOption.None)] | ||
public int Rank { get; set; } | ||
|
||
public virtual Account Account { get; set; } | ||
public virtual Resource Resource { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
ZkData/Migrations/202006162227037_AddAccountMapBans.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
namespace ZkData.Migrations | ||
{ | ||
using System; | ||
using System.Data.Entity.Migrations; | ||
|
||
public partial class AddAccountMapBans : DbMigration | ||
{ | ||
public override void Up() | ||
{ | ||
CreateTable( | ||
"dbo.AccountMapBans", | ||
c => new | ||
{ | ||
AccountID = c.Int(nullable: false), | ||
BannedMapResourceID = c.Int(nullable: false), | ||
Rank = c.Int(nullable: false), | ||
}) | ||
.PrimaryKey(t => new { t.AccountID, t.BannedMapResourceID, t.Rank }) | ||
.ForeignKey("dbo.Resources", t => t.BannedMapResourceID) | ||
.ForeignKey("dbo.Accounts", t => t.AccountID) | ||
.Index(t => t.AccountID); | ||
|
||
} | ||
|
||
public override void Down() | ||
{ | ||
DropForeignKey("dbo.AccountMapBans", "AccountID", "dbo.Accounts"); | ||
DropForeignKey("dbo.AccountMapBans", "BannedMapResourceID", "dbo.Resources"); | ||
DropIndex("dbo.AccountMapBans", new[] { "AccountID" }); | ||
DropTable("dbo.AccountMapBans"); | ||
} | ||
} | ||
} |
Oops, something went wrong.