Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
20 changes: 5 additions & 15 deletions CodeBattleCPP/CodeBattleCpp.sln
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25420.1
# Visual Studio Version 16
VisualStudioVersion = 16.0.29230.47
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CodeBattleCpp", "CodeBattleCpp\CodeBattleCpp.vcxproj", "{A9F8773C-9D2C-4F38-9E5F-0D1A84A7D36A}"
ProjectSection(ProjectDependencies) = postProject
{8F09F71F-E153-4C37-B12E-692A4CA8FE7A} = {8F09F71F-E153-4C37-B12E-692A4CA8FE7A}
EndProjectSection
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CodeBattleCppLibrary", "CodeBattleCppLibrary\CodeBattleCppLibrary.vcxproj", "{8F09F71F-E153-4C37-B12E-692A4CA8FE7A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -26,16 +21,11 @@ Global
{A9F8773C-9D2C-4F38-9E5F-0D1A84A7D36A}.Release|x64.Build.0 = Release|x64
{A9F8773C-9D2C-4F38-9E5F-0D1A84A7D36A}.Release|x86.ActiveCfg = Release|Win32
{A9F8773C-9D2C-4F38-9E5F-0D1A84A7D36A}.Release|x86.Build.0 = Release|Win32
{8F09F71F-E153-4C37-B12E-692A4CA8FE7A}.Debug|x64.ActiveCfg = Debug|x64
{8F09F71F-E153-4C37-B12E-692A4CA8FE7A}.Debug|x64.Build.0 = Debug|x64
{8F09F71F-E153-4C37-B12E-692A4CA8FE7A}.Debug|x86.ActiveCfg = Debug|Win32
{8F09F71F-E153-4C37-B12E-692A4CA8FE7A}.Debug|x86.Build.0 = Debug|Win32
{8F09F71F-E153-4C37-B12E-692A4CA8FE7A}.Release|x64.ActiveCfg = Release|x64
{8F09F71F-E153-4C37-B12E-692A4CA8FE7A}.Release|x64.Build.0 = Release|x64
{8F09F71F-E153-4C37-B12E-692A4CA8FE7A}.Release|x86.ActiveCfg = Release|Win32
{8F09F71F-E153-4C37-B12E-692A4CA8FE7A}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {48FA613C-AF86-4007-AA7E-59069CCAD5D6}
EndGlobalSection
EndGlobal
53 changes: 0 additions & 53 deletions CodeBattleCPP/CodeBattleCpp/CodeBattleCpp.cpp

This file was deleted.

41 changes: 33 additions & 8 deletions CodeBattleCPP/CodeBattleCpp/CodeBattleCpp.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<ProjectGuid>{A9F8773C-9D2C-4F38-9E5F-0D1A84A7D36A}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>CodeBattleCpp</RootNamespace>
<WindowsTargetPlatformVersion>10.0.17134.0</WindowsTargetPlatformVersion>
<WindowsTargetPlatformVersion>10.0.17763.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
Expand All @@ -41,7 +41,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
Expand Down Expand Up @@ -74,12 +74,16 @@
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
<LibraryPath>$(SolutionDir)Lib\x64;$(VC_LibraryPath_x64);$(WindowsSDK_LibraryPath_x64);$(NETFXKitsDir)Lib\um\x64</LibraryPath>
<IncludePath>$(SolutionDir)Include;$(VC_IncludePath);$(WindowsSDK_IncludePath);</IncludePath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
<IncludePath>$(SolutionDir)Include;$(VC_IncludePath);$(WindowsSDK_IncludePath);</IncludePath>
<LibraryPath>$(SolutionDir)Lib\x64;$(VC_LibraryPath_x64);$(WindowsSDK_LibraryPath_x64);$(NETFXKitsDir)Lib\um\x64</LibraryPath>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
Expand All @@ -104,12 +108,13 @@
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>..\CodeBattleCppLibrary;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>CodeBattleCppLibrary.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>..\x64\Debug;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
</Link>
</ItemDefinitionGroup>
Expand Down Expand Up @@ -143,18 +148,38 @@
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>..\CodeBattleCppLibrary;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<SubSystem>Windows</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>CodeBattleCppLibrary.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>..\x64\Release;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="CodeBattleCpp.cpp" />
<ClCompile Include="client.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="easywsclient\easywsclient.cpp" />
<ClCompile Include="game_board.cpp" />
<ClCompile Include="graphics.cpp" />
<ClCompile Include="vec.cpp" />
<ClCompile Include="window.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="client.hpp" />
<ClInclude Include="easywsclient\easywsclient.hpp" />
<ClInclude Include="game_board.hpp" />
<ClInclude Include="graphics.hpp" />
<ClInclude Include="json.hpp" />
<ClInclude Include="LodeRunnerBlocks.h" />
<ClInclude Include="PlayerAction.h" />
<ClInclude Include="player_command.hpp" />
<ClInclude Include="tile.hpp" />
<ClInclude Include="vec.hpp" />
<ClInclude Include="window.hpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
Expand Down
55 changes: 54 additions & 1 deletion CodeBattleCPP/CodeBattleCpp/CodeBattleCpp.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,61 @@
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="CodeBattleCpp.cpp">
<ClCompile Include="vec.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="easywsclient\easywsclient.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="client.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="game_board.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="graphics.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="window.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="player_command.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="PlayerAction.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="tile.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="vec.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="easywsclient\easywsclient.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="client.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="game_board.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="LodeRunnerBlocks.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="graphics.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="json.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="window.hpp">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
107 changes: 107 additions & 0 deletions CodeBattleCPP/CodeBattleCpp/client.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#include "client.hpp"
#include "json.hpp"
#include <iostream>

using namespace std::chrono_literals;

client::client(std::string_view _server, std::string_view _userId, std::string_view _code)
{
map_.clear();
board = nullptr;

path = "ws://";
path += _server;
path += "/codenjoy-contest/ws?user=";
path += _userId;
path += "&code=";
path += _code;
statistic_info = "ws://";
statistic_info += _server;
statistic_info += "/codenjoy-contest/screen-ws?user=";
statistic_info += _userId;
user_id = _userId;
is_running = false;
}


client::~client()
{
is_running = false;
work_thread->join();
}

void client::run(std::function<void()> _message_handler)
{
is_running = true;
work_thread = new std::thread(&client::update_func, this, _message_handler);
}

void client::update_func(std::function<void()> _message_handler)
{
#ifdef _WIN32
WSADATA wsaData;

if (WSAStartup(MAKEWORD(2, 2), &wsaData))
throw new std::exception("WSAStartup Failed.\n");
else
std::cout << "Connection established" << std::endl;
#endif

web_socket = easywsclient::WebSocket::from_url(path);
if (web_socket == nullptr)is_running = false;
nlohmann::json object;
object["allPlayersScreen"] = false;
object["gameName"] = "loderunner";
object["name"] = "getScreen";
object["players"][0] = user_id;
auto web_socket1 = easywsclient::WebSocket::from_url(statistic_info);
if (web_socket1)
web_socket1->send(object.dump());
while (is_running)
{
web_socket->poll();
web_socket->dispatch([&](const std::string& message)
{
int size_needed = MultiByteToWideChar(CP_UTF8, 0, &message[0], (int)message.size(), NULL, 0);
std::wstring wmessage(size_needed, 0);
MultiByteToWideChar(CP_UTF8, 0, &message[0], (int)message.size(), &wmessage[0], size_needed);

uint32_t size = sqrt(wmessage.size() - 6);
if (map_.size() != size)
{
map_.clear();
map_.resize(size);
//map = new tile*[map_size];
for (uint32_t j = 0; j < size; j++)
{
map_[j].resize(size);
for (uint32_t i = 0; i < size; i++)
map_[j][i] = tile(static_cast<tile::tile_id_>(tile::NONE));
}
}

uint32_t chr = 6;
for (uint32_t j = 0; j < map_.size(); j++)
for (uint32_t i = 0; i < map_.size(); i++)
map_[j][i] = tile(static_cast<tile::tile_id_>(wmessage[chr++]));
lock();
board = std::make_shared<game_board>(map_);
unlock();
_message_handler();
});
web_socket1->poll();
web_socket1->dispatch([&](const std::string& message) {
auto obj = nlohmann::json::parse(message, nullptr, false);
if (!obj.is_discarded()) {
std::cout << "score: " << obj[user_id]["score"].get<std::int32_t>() << "\n";
}
web_socket1->send(object.dump());
});
std::this_thread::sleep_for(1ms);
}
if (web_socket)web_socket->close();

#ifdef _WIN32
WSACleanup();
#endif
}
Loading