Skip to content

Commit 61addee

Browse files
committed
Store Autohosts in db
1 parent abee79b commit 61addee

7 files changed

Lines changed: 308 additions & 0 deletions

ZkData/Ef/Autohost.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using PlasmaShared;
2+
using System.Collections.Generic;
3+
using System.ComponentModel.DataAnnotations;
4+
using System.Linq;
5+
6+
namespace ZkData
7+
{
8+
public class Autohost
9+
{
10+
public Autohost()
11+
{
12+
13+
}
14+
15+
public int AutohostID { get; set; }
16+
public MapSupportLevel MinimumMapSupportLevel { get; set; }
17+
public AutohostMode AutohostMode { get; set; }
18+
public int InviteMMPlayers { get; set; } = 0;
19+
public int MaxElo { get; set; } = int.MaxValue;
20+
public int MinElo { get; set; } = int.MinValue;
21+
public int MaxLevel { get; set; } = int.MaxValue;
22+
public int MinLevel { get; set; } = int.MinValue;
23+
public int MaxRank { get; set; } = int.MaxValue;
24+
public int MinRank { get; set; } = int.MinValue;
25+
public int MaxPlayers { get; set; }
26+
27+
[StringLength(200)]
28+
public string Title { get; set; }
29+
}
30+
}

ZkData/Migrations/201810231530234_StoreAutohostsInDb.Designer.cs

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
namespace ZkData.Migrations
2+
{
3+
using System;
4+
using System.Data.Entity.Migrations;
5+
6+
public partial class StoreAutohostsInDb : DbMigration
7+
{
8+
public override void Up()
9+
{
10+
CreateTable(
11+
"dbo.Autohosts",
12+
c => new
13+
{
14+
AutohostID = c.Int(nullable: false, identity: true),
15+
MinimumMapSupportLevel = c.Int(nullable: false),
16+
AutohostMode = c.Int(nullable: false),
17+
InviteMMPlayers = c.Int(nullable: false),
18+
MaxElo = c.Int(nullable: false),
19+
MinElo = c.Int(nullable: false),
20+
MaxLevel = c.Int(nullable: false),
21+
MinLevel = c.Int(nullable: false),
22+
MaxRank = c.Int(nullable: false),
23+
MinRank = c.Int(nullable: false),
24+
MaxPlayers = c.Int(nullable: false),
25+
Title = c.String(maxLength: 200),
26+
})
27+
.PrimaryKey(t => t.AutohostID);
28+
29+
}
30+
31+
public override void Down()
32+
{
33+
DropTable("dbo.Autohosts");
34+
}
35+
}
36+
}

ZkData/Migrations/201810231530234_StoreAutohostsInDb.resx

Lines changed: 126 additions & 0 deletions
Large diffs are not rendered by default.

ZkData/ZkData.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
<ItemGroup>
7878
<Compile Include="DbCloner.cs" />
7979
<Compile Include="Ef\AccountRelation.cs" />
80+
<Compile Include="Ef\Autohost.cs" />
8081
<Compile Include="Ef\DynamicConfig.cs" />
8182
<Compile Include="Ef\LobbyChannelTopic.cs" />
8283
<Compile Include="Ef\LobbyNews.cs" />
@@ -402,6 +403,10 @@
402403
<Compile Include="Migrations\201810172106541_UpdateDynamicConfig.Designer.cs">
403404
<DependentUpon>201810172106541_UpdateDynamicConfig.cs</DependentUpon>
404405
</Compile>
406+
<Compile Include="Migrations\201810231530234_StoreAutohostsInDb.cs" />
407+
<Compile Include="Migrations\201810231530234_StoreAutohostsInDb.Designer.cs">
408+
<DependentUpon>201810231530234_StoreAutohostsInDb.cs</DependentUpon>
409+
</Compile>
405410
<Compile Include="SpringFilesUnitsyncAttempt.cs" />
406411
<Compile Include="SteamWebApi.cs" />
407412
<Compile Include="UserLanguageNoteAttribute.cs" />
@@ -770,6 +775,9 @@
770775
<EmbeddedResource Include="Migrations\201810172106541_UpdateDynamicConfig.resx">
771776
<DependentUpon>201810172106541_UpdateDynamicConfig.cs</DependentUpon>
772777
</EmbeddedResource>
778+
<EmbeddedResource Include="Migrations\201810231530234_StoreAutohostsInDb.resx">
779+
<DependentUpon>201810231530234_StoreAutohostsInDb.cs</DependentUpon>
780+
</EmbeddedResource>
773781
<EmbeddedResource Include="ZkDataResources.resx">
774782
<Generator>ResXFileCodeGenerator</Generator>
775783
<LastGenOutput>ZkDataResources.Designer.cs</LastGenOutput>

ZkData/ZkDataContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ public partial class ZkDataContext : DbContext
9292
public virtual DbSet<SpringBattleBot> SpringBattleBots { get; set; }
9393
public virtual DbSet<SpringFilesUnitsyncAttempt> SpringFilesUnitsyncAttempts { get; set; }
9494
public virtual DbSet<DynamicConfig> DynamicConfigs { get; set; }
95+
public virtual DbSet<Autohost> Autohosts { get; set; }
9596

9697
public virtual DbSet<LobbyChannelTopic> LobbyChannelTopics { get; set; }
9798

ZkLobbyServer/ServerBattle.cs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public class ServerBattle : Battle
4343
public Mod HostedModInfo;
4444

4545
private int hostingPort;
46+
private int? dbAutohostIndex;
4647

4748
protected bool isZombie;
4849
protected bool isPostBattleDiscussion => IsAutohost && DateTime.UtcNow.Subtract(EndedSince).TotalSeconds < DiscussionTime;
@@ -102,6 +103,42 @@ public ServerBattle(ZkLobbyServer server, string founder)
102103
PickHostingPort();
103104
}
104105

106+
public void SaveToDb()
107+
{
108+
if (!IsAutohost) return;
109+
using (var db = new ZkDataContext())
110+
{
111+
Autohost autohost = null;
112+
bool insert = false;
113+
if (dbAutohostIndex.HasValue)
114+
{
115+
autohost = db.Autohosts.Where(x => x.AutohostID == dbAutohostIndex).FirstOrDefault();
116+
}
117+
if (autohost == null)
118+
{
119+
insert = true;
120+
autohost = new Autohost();
121+
}
122+
autohost.MinimumMapSupportLevel = MinimalMapSupportLevelAutohost;
123+
autohost.AutohostMode = Mode;
124+
autohost.InviteMMPlayers = InviteMMPlayers;
125+
autohost.MaxElo = MaxElo;
126+
autohost.MinElo = MinElo;
127+
autohost.MaxLevel = MaxLevel;
128+
autohost.MinLevel = MinLevel;
129+
autohost.MaxRank = MaxRank;
130+
autohost.MinRank = MinRank;
131+
autohost.Title = Title;
132+
autohost.MaxPlayers = MaxPlayers;
133+
if (insert)
134+
{
135+
db.Autohosts.Add(autohost);
136+
}
137+
db.SaveChanges();
138+
dbAutohostIndex = autohost.AutohostID;
139+
}
140+
}
141+
105142
public string GenerateClientScriptPassword(string name)
106143
{
107144
return Hash.HashString(battleInstanceGuid + name).ToString();
@@ -190,11 +227,19 @@ public void SwitchAutohost(bool autohost, string founder)
190227
{
191228
IsAutohost = true;
192229
FounderName = "Autohost #" + BattleID;
230+
SaveToDb();
193231
}
194232
else
195233
{
196234
IsAutohost = false;
197235
FounderName = founder;
236+
if (dbAutohostIndex.HasValue)
237+
{
238+
using (var db = new ZkDataContext())
239+
{
240+
db.Autohosts.Remove(db.Autohosts.Where(x => x.AutohostID == dbAutohostIndex).FirstOrDefault());
241+
}
242+
}
198243
}
199244
}
200245

@@ -525,6 +570,7 @@ public async Task SwitchGameType(AutohostMode type)
525570
MapName = null;
526571
ValidateAndFillDetails();
527572
await server.Broadcast(server.ConnectedUsers.Values, new BattleUpdate() { Header = GetHeader() });
573+
SaveToDb();
528574
// do a full update - mode can also change map/players
529575
}
530576

@@ -544,45 +590,54 @@ public async Task SwitchMaxPlayers(int cnt)
544590
await
545591
server.Broadcast(server.ConnectedUsers.Values,
546592
new BattleUpdate() { Header = new BattleHeader() { BattleID = BattleID, MaxPlayers = MaxPlayers } });
593+
SaveToDb();
547594
}
548595
public async Task SwitchInviteMmPlayers(int players)
549596
{
550597
InviteMMPlayers = players;
598+
SaveToDb();
551599
}
552600

553601
public async Task SwitchMaxElo(int elo)
554602
{
555603
MaxElo = elo;
604+
SaveToDb();
556605
}
557606

558607
public async Task SwitchMinElo(int elo)
559608
{
560609
MinElo = elo;
610+
SaveToDb();
561611
}
562612

563613
public async Task SwitchMaxLevel(int lvl)
564614
{
565615
MaxLevel = lvl;
616+
SaveToDb();
566617
}
567618

568619
public async Task SwitchMinLevel(int lvl)
569620
{
570621
MinLevel = lvl;
622+
SaveToDb();
571623
}
572624

573625
public async Task SwitchMaxRank(int rank)
574626
{
575627
MaxRank = rank;
628+
SaveToDb();
576629
}
577630

578631
public async Task SwitchMinRank(int rank)
579632
{
580633
MinRank = rank;
634+
SaveToDb();
581635
}
582636

583637
public async Task SwitchMinMapSupportLevel(MapSupportLevel lvl)
584638
{
585639
MinimalMapSupportLevelAutohost = lvl;
640+
SaveToDb();
586641
}
587642

588643
public async Task SwitchPassword(string pwd)
@@ -599,6 +654,29 @@ public async Task SwitchTitle(string title)
599654
await
600655
server.Broadcast(server.ConnectedUsers.Values,
601656
new BattleUpdate() { Header = new BattleHeader() { BattleID = BattleID, Title = Title } });
657+
SaveToDb();
658+
}
659+
660+
661+
public void UpdateWith(Autohost autohost)
662+
{
663+
IsAutohost = true;
664+
MinimalMapSupportLevelAutohost = autohost.MinimumMapSupportLevel;
665+
Mode = autohost.AutohostMode;
666+
InviteMMPlayers = autohost.InviteMMPlayers;
667+
MaxElo = autohost.MaxElo;
668+
MinElo = autohost.MinElo;
669+
MaxLevel = autohost.MaxLevel;
670+
MinLevel = autohost.MinLevel;
671+
MaxRank = autohost.MaxRank;
672+
MinRank = autohost.MinRank;
673+
Title = autohost.Title;
674+
MaxPlayers = autohost.MaxPlayers;
675+
dbAutohostIndex = autohost.AutohostID;
676+
FounderName = "Autohost #" + BattleID;
677+
ValidateAndFillDetails();
678+
679+
RunCommandDirectly<CmdMap>(null);
602680
}
603681

604682
public override void UpdateWith(BattleHeader h)

0 commit comments

Comments
 (0)